]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_bootp.c
c885d74a3ddf1321fdf6fd7737dc11ba20cb627f
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
34 * - be a BOOTP client over a particular interface to retrieve
35 * the IP address, netmask, and router
39 * Modification History
41 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
42 * - completely rewritten
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <mach/boolean.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
52 #include <sys/ioctl.h>
55 #include <sys/vnode.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/uio_internal.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 #include <netinet/ip_icmp.h>
71 #include <netinet/bootp.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <netinet/dhcp_options.h>
76 #include <kern/kern_types.h>
77 #include <kern/kalloc.h>
80 #define dprintf(x) printf x;
81 #else /* !BOOTP_DEBUG */
83 #endif /* BOOTP_DEBUG */
85 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
86 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
88 struct mbuf
* ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
);
89 int receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
);
92 /* ip address formatting macros */
93 #define IP_FORMAT "%d.%d.%d.%d"
94 #define IP_CH(ip) ((u_char *)ip)
95 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
97 static __inline__
struct sockaddr_in
100 struct sockaddr_in blank
= { sizeof(struct sockaddr_in
), AF_INET
};
104 static __inline__
void
105 print_reply(struct bootp
*bp
, __unused
int bp_len
)
110 if (bp
->bp_op
== BOOTREQUEST
) printf("BOOTREQUEST\n");
111 else if (bp
->bp_op
== BOOTREPLY
) printf("BOOTREPLY\n");
119 printf("bp_htype = %d\n", i
);
122 printf("bp_hlen = %d\n", len
);
125 printf("bp_hops = %d\n", i
);
127 printf("bp_xid = %lu\n", bp
->bp_xid
);
129 printf("bp_secs = %u\n", bp
->bp_secs
);
131 printf("bp_ciaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_ciaddr
));
132 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
133 printf("bp_siaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_siaddr
));
134 printf("bp_giaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_giaddr
));
136 printf("bp_chaddr = ");
137 for (j
= 0; j
< len
; j
++)
139 i
= bp
->bp_chaddr
[j
];
141 if (j
< (len
- 1)) printf(":");
145 printf("bp_sname = %s\n", bp
->bp_sname
);
146 printf("bp_file = %s\n", bp
->bp_file
);
149 static __inline__
void
150 print_reply_short(struct bootp
*bp
, __unused
int bp_len
)
152 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
153 printf("bp_sname = %s\n", bp
->bp_sname
);
157 static __inline__
long
158 random_range(long bottom
, long top
)
160 long number
= top
- bottom
+ 1;
161 long range_size
= LONG_MAX
/ number
;
162 return (((long)random()) / range_size
+ bottom
);
166 * Function: make_bootp_request
168 * Create a "blank" bootp packet.
171 make_bootp_request(struct bootp_packet
* pkt
,
172 u_char
* hwaddr
, u_char hwtype
, u_char hwlen
)
174 char rfc_magic
[4] = RFC_OPTIONS_MAGIC
;
176 bzero(pkt
, sizeof (*pkt
));
177 pkt
->bp_ip
.ip_v
= IPVERSION
;
178 pkt
->bp_ip
.ip_hl
= sizeof (struct ip
) >> 2;
180 pkt
->bp_ip
.ip_id
= ip_randomid();
182 pkt
->bp_ip
.ip_id
= htons(ip_id
++);
184 pkt
->bp_ip
.ip_ttl
= MAXTTL
;
185 pkt
->bp_ip
.ip_p
= IPPROTO_UDP
;
186 pkt
->bp_ip
.ip_src
.s_addr
= 0;
187 pkt
->bp_ip
.ip_dst
.s_addr
= htonl(INADDR_BROADCAST
);
188 pkt
->bp_udp
.uh_sport
= htons(IPPORT_BOOTPC
);
189 pkt
->bp_udp
.uh_dport
= htons(IPPORT_BOOTPS
);
190 pkt
->bp_udp
.uh_sum
= 0;
191 pkt
->bp_bootp
.bp_op
= BOOTREQUEST
;
192 pkt
->bp_bootp
.bp_htype
= hwtype
;
193 pkt
->bp_bootp
.bp_hlen
= hwlen
;
194 pkt
->bp_bootp
.bp_ciaddr
.s_addr
= 0;
195 bcopy(hwaddr
, pkt
->bp_bootp
.bp_chaddr
, hwlen
);
196 bcopy(rfc_magic
, pkt
->bp_bootp
.bp_vend
, sizeof(rfc_magic
));
197 pkt
->bp_bootp
.bp_vend
[4] = dhcptag_end_e
;
198 pkt
->bp_udp
.uh_ulen
= htons(sizeof(pkt
->bp_udp
) + sizeof(pkt
->bp_bootp
));
199 pkt
->bp_ip
.ip_len
= htons(sizeof(struct ip
) + ntohs(pkt
->bp_udp
.uh_ulen
));
200 pkt
->bp_ip
.ip_sum
= 0;
205 * Function: ip_pkt_to_mbuf
207 * Put the given IP packet into an mbuf, calculate the
211 ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
)
216 m
= (struct mbuf
*)m_devget(pkt
, pktsize
, 0, 0, 0);
218 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
221 m
->m_flags
|= M_BCAST
;
222 /* Compute the checksum */
223 ip
= mtod(m
, struct ip
*);
225 ip
->ip_sum
= in_cksum(m
, sizeof (struct ip
));
229 static __inline__ u_char
*
230 link_address(struct sockaddr_dl
* dl_p
)
232 return (dl_p
->sdl_data
+ dl_p
->sdl_nlen
);
235 static __inline__
void
236 link_print(struct sockaddr_dl
* dl_p
)
241 printf("len %d index %d family %d type 0x%x nlen %d alen %d"
242 " slen %d addr ", dl_p
->sdl_len
,
243 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
244 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
246 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
247 printf("%s%x", i
? ":" : "",
248 (link_address(dl_p
))[i
]);
253 static struct sockaddr_dl
*
254 link_from_ifnet(struct ifnet
* ifp
)
256 struct ifaddr
* addr
;
258 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
260 ifnet_lock_shared(ifp
);
261 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
) {
262 if (addr
->ifa_addr
->sa_family
== AF_LINK
) {
263 struct sockaddr_dl
* dl_p
= (struct sockaddr_dl
*)(addr
->ifa_addr
);
265 ifnet_lock_done(ifp
);
269 ifnet_lock_done(ifp
);
274 * Function: send_bootp_request
276 * Send the request by calling the interface's output routine
277 * bypassing routing code.
280 send_bootp_request(struct ifnet
* ifp
, __unused
struct socket
* so
,
281 struct bootp_packet
* pkt
)
284 struct sockaddr_in sin
;
286 /* Address to send to */
288 sin
.sin_port
= htons(IPPORT_BOOTPS
);
289 sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
291 m
= ip_pkt_to_mbuf((caddr_t
)pkt
, sizeof(*pkt
));
292 return dlil_output(ifp
, PF_INET
, m
, 0, (struct sockaddr
*)&sin
, 0);
296 * Function: receive_packet
298 * Return a received packet or an error if none available.
301 receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
)
306 char uio_buf
[ UIO_SIZEOF(1) ];
308 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
309 &uio_buf
[0], sizeof(uio_buf
));
310 uio_addiov(auio
, CAST_USER_ADDR_T(pp
), psize
);
311 rcvflg
= MSG_WAITALL
;
313 error
= soreceive(so
, (struct sockaddr
**) 0, auio
, 0, 0, &rcvflg
);
314 *actual_size
= psize
- uio_resid(auio
);
319 * Function: bootp_timeout
321 * Wakeup the process waiting for something on a socket.
324 bootp_timeout(void * arg
)
326 struct socket
* * socketflag
= (struct socket
* *)arg
;
327 struct socket
* so
= *socketflag
;
329 dprintf(("bootp: timeout\n"));
333 sowakeup(so
, &so
->so_rcv
);
334 socket_unlock(so
, 1);
339 * Function: rate_packet
341 * Return an integer point rating value for the given bootp packet.
342 * If yiaddr non-zero, the packet gets a rating of 1.
343 * Another point is given if the packet contains the subnet mask,
344 * and another if the router is present.
346 #define GOOD_RATING 3
347 static __inline__
int
348 rate_packet(__unused
struct bootp
* pkt
, __unused
int pkt_size
, dhcpol_t
* options_p
)
353 if (dhcpol_find(options_p
, dhcptag_subnet_mask_e
, &len
, NULL
) != NULL
) {
356 if (dhcpol_find(options_p
, dhcptag_router_e
, &len
, NULL
) != NULL
) {
362 #define INITIAL_WAIT_SECS 4
363 #define MAX_WAIT_SECS 64
364 #define GATHER_TIME_SECS 2
365 #define RAND_TICKS (hz) /* one second */
368 * Function: bootp_loop
370 * Do the actual BOOTP protocol.
371 * The algorithm sends out a packet, waits for a response.
372 * We try max_try times, waiting in an exponentially increasing
373 * amount of time. Once we receive a good response, we start
374 * a new time period called the "gather time", during which we
375 * either find the perfect packet (one that has ip, mask and router)
376 * or we continue to gather responses. At the end of the gather period,
377 * we use the best response gathered.
380 bootp_loop(struct socket
* so
, struct ifnet
* ifp
, int max_try
,
381 struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
382 struct in_addr
* router_p
)
384 struct timeval current_time
;
385 struct sockaddr_dl
* dl_p
;
390 struct bootp_packet
* request
= NULL
;
391 struct bootp
* reply
= NULL
;
392 int reply_size
= DHCP_PACKET_MIN
;
393 struct timeval start_time
;
396 struct socket
* timeflag
;
397 int wait_ticks
= INITIAL_WAIT_SECS
* hz
;
399 /* get the hardware address from the interface */
400 dl_p
= link_from_ifnet(ifp
);
402 printf("bootp: can't get link address\n");
406 printf("bootp: h/w addr ");
409 hwaddr
= link_address(dl_p
);
410 hwlen
= dl_p
->sdl_alen
;
411 switch (dl_p
->sdl_type
) {
413 hwtype
= ARPHRD_ETHER
;
416 printf("bootp: hardware type %d not supported\n",
418 panic("bootp: hardware type not supported");
422 /* set transaction id and remember the start time */
423 microtime(&start_time
);
424 current_time
= start_time
;
427 /* make a request/reply packet */
428 request
= (struct bootp_packet
*)kalloc(sizeof(*request
));
429 make_bootp_request(request
, hwaddr
, hwtype
, hwlen
);
430 reply
= (struct bootp
*)kalloc(reply_size
);
432 printf("bootp: sending request");
433 for (retry
= 0; retry
< max_try
; retry
++) {
434 int gather_count
= 0;
437 /* Send the request */
439 request
->bp_bootp
.bp_secs
= htons((u_short
)(current_time
.tv_sec
440 - start_time
.tv_sec
));
441 request
->bp_bootp
.bp_xid
= htonl(xid
);
442 error
= send_bootp_request(ifp
, so
, request
);
447 wait_ticks
+= random_range(-RAND_TICKS
, RAND_TICKS
);
448 dprintf(("bootp: waiting %d ticks\n", wait_ticks
));
449 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
, wait_ticks
);
454 error
= receive_packet(so
, (caddr_t
)reply
, reply_size
, &n
);
456 dprintf(("\nbootp: received packet\n"));
457 if (ntohl(reply
->bp_xid
) == xid
458 && reply
->bp_yiaddr
.s_addr
459 && bcmp(reply
->bp_chaddr
, hwaddr
, hwlen
) == 0) {
464 print_reply_short(reply
, n
);
465 #endif /* BOOTP_DEBUG */
466 (void)dhcpol_parse_packet(&options
, (struct dhcp
*)reply
,
468 rating
= rate_packet(reply
, n
, &options
);
469 if (rating
> last_rating
) {
473 *iaddr_p
= reply
->bp_yiaddr
;
474 ip
= (struct in_addr
*)
475 dhcpol_find(&options
,
476 dhcptag_subnet_mask_e
, &len
, NULL
);
480 ip
= (struct in_addr
*)
481 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
485 printf("%sbootp: got "
486 "response from %s (" IP_FORMAT
")\n",
487 last_rating
== 0 ? "\n" : "",
489 IP_LIST(&reply
->bp_siaddr
));
491 dhcpol_free(&options
);
492 if (rating
>= GOOD_RATING
) {
493 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
496 if (gather_count
== 0) {
497 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
499 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
,
500 hz
* GATHER_TIME_SECS
);
505 dprintf(("bootp: packet ignored\n"));
508 else if ((error
!= EWOULDBLOCK
)) {
511 else if (timeflag
== NULL
) { /* timed out */
513 dprintf(("bootp: gathering time has expired"));
514 goto done
; /* we have a packet */
520 error
= sbwait(&so
->so_rcv
);
521 socket_unlock(so
, 1);
524 if (error
&& (error
!= EWOULDBLOCK
)) {
525 dprintf(("bootp: failed to receive packets: %d\n", error
));
526 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
530 if (wait_ticks
> (MAX_WAIT_SECS
* hz
))
531 wait_ticks
= MAX_WAIT_SECS
* hz
;
533 microtime(¤t_time
);
543 kfree(request
, sizeof (*request
));
545 kfree(reply
, reply_size
);
552 * Use the BOOTP protocol to resolve what our IP address should be
553 * on a particular interface.
555 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
556 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
559 boolean_t addr_set
= FALSE
;
562 struct socket
* so
= NULL
;
565 error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0);
567 dprintf(("bootp: socreate failed %d\n", error
));
571 /* assign the all-zeroes address */
572 bzero(&ifr
, sizeof(ifr
));
573 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
574 *((struct sockaddr_in
*)&ifr
.ifr_addr
) = blank_sin();
575 error
= ifioctl(so
, SIOCSIFADDR
, (caddr_t
)&ifr
, procp
);
577 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
581 dprintf(("bootp: all-zeroes IP address assigned\n"));
584 { /* bind the socket */
585 struct sockaddr_in
* sin
;
587 sin
= _MALLOC(sizeof(struct sockaddr_in
), M_IFADDR
, M_WAIT
);
592 sin
->sin_len
= sizeof(struct sockaddr_in
);
593 sin
->sin_family
= AF_INET
;
594 sin
->sin_port
= htons(IPPORT_BOOTPC
);
595 sin
->sin_addr
.s_addr
= INADDR_ANY
;
596 error
= sobind(so
, (struct sockaddr
*) sin
);
600 dprintf(("bootp: sobind failed, %d\n", error
));
604 so
->so_state
|= SS_NBIO
;
605 socket_unlock(so
, 1);
607 /* do the protocol */
608 error
= bootp_loop(so
, ifp
, max_try
, iaddr_p
, netmask_p
, router_p
);
613 (void) ifioctl(so
, SIOCDIFADDR
, (caddr_t
) &ifr
, procp
);