]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_bootp.c
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
36 * - be a BOOTP client over a particular interface to retrieve
37 * the IP address, netmask, and router
41 * Modification History
43 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
44 * - completely rewritten
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <mach/boolean.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
54 #include <sys/ioctl.h>
57 #include <sys/vnode.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/uio_internal.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
72 #include <netinet/ip_icmp.h>
73 #include <netinet/bootp.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <netinet/dhcp_options.h>
78 #include <kern/kern_types.h>
79 #include <kern/kalloc.h>
82 #define dprintf(x) printf x;
83 #else /* !BOOTP_DEBUG */
85 #endif /* BOOTP_DEBUG */
87 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
88 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
90 struct mbuf
* ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
);
91 int receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
);
94 /* ip address formatting macros */
95 #define IP_FORMAT "%d.%d.%d.%d"
96 #define IP_CH(ip) ((u_char *)ip)
97 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
99 static __inline__
struct sockaddr_in
102 struct sockaddr_in blank
= { sizeof(struct sockaddr_in
), AF_INET
};
106 static __inline__
void
107 print_reply(struct bootp
*bp
, __unused
int bp_len
)
112 if (bp
->bp_op
== BOOTREQUEST
) printf("BOOTREQUEST\n");
113 else if (bp
->bp_op
== BOOTREPLY
) printf("BOOTREPLY\n");
121 printf("bp_htype = %d\n", i
);
124 printf("bp_hlen = %d\n", len
);
127 printf("bp_hops = %d\n", i
);
129 printf("bp_xid = %lu\n", bp
->bp_xid
);
131 printf("bp_secs = %u\n", bp
->bp_secs
);
133 printf("bp_ciaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_ciaddr
));
134 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
135 printf("bp_siaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_siaddr
));
136 printf("bp_giaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_giaddr
));
138 printf("bp_chaddr = ");
139 for (j
= 0; j
< len
; j
++)
141 i
= bp
->bp_chaddr
[j
];
143 if (j
< (len
- 1)) printf(":");
147 printf("bp_sname = %s\n", bp
->bp_sname
);
148 printf("bp_file = %s\n", bp
->bp_file
);
151 static __inline__
void
152 print_reply_short(struct bootp
*bp
, __unused
int bp_len
)
154 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
155 printf("bp_sname = %s\n", bp
->bp_sname
);
159 static __inline__
long
160 random_range(long bottom
, long top
)
162 long number
= top
- bottom
+ 1;
163 long range_size
= LONG_MAX
/ number
;
164 return (((long)random()) / range_size
+ bottom
);
168 * Function: make_bootp_request
170 * Create a "blank" bootp packet.
173 make_bootp_request(struct bootp_packet
* pkt
,
174 u_char
* hwaddr
, u_char hwtype
, u_char hwlen
)
176 char rfc_magic
[4] = RFC_OPTIONS_MAGIC
;
178 bzero(pkt
, sizeof (*pkt
));
179 pkt
->bp_ip
.ip_v
= IPVERSION
;
180 pkt
->bp_ip
.ip_hl
= sizeof (struct ip
) >> 2;
182 pkt
->bp_ip
.ip_id
= ip_randomid();
184 pkt
->bp_ip
.ip_id
= htons(ip_id
++);
186 pkt
->bp_ip
.ip_ttl
= MAXTTL
;
187 pkt
->bp_ip
.ip_p
= IPPROTO_UDP
;
188 pkt
->bp_ip
.ip_src
.s_addr
= 0;
189 pkt
->bp_ip
.ip_dst
.s_addr
= htonl(INADDR_BROADCAST
);
190 pkt
->bp_udp
.uh_sport
= htons(IPPORT_BOOTPC
);
191 pkt
->bp_udp
.uh_dport
= htons(IPPORT_BOOTPS
);
192 pkt
->bp_udp
.uh_sum
= 0;
193 pkt
->bp_bootp
.bp_op
= BOOTREQUEST
;
194 pkt
->bp_bootp
.bp_htype
= hwtype
;
195 pkt
->bp_bootp
.bp_hlen
= hwlen
;
196 pkt
->bp_bootp
.bp_ciaddr
.s_addr
= 0;
197 bcopy(hwaddr
, pkt
->bp_bootp
.bp_chaddr
, hwlen
);
198 bcopy(rfc_magic
, pkt
->bp_bootp
.bp_vend
, sizeof(rfc_magic
));
199 pkt
->bp_bootp
.bp_vend
[4] = dhcptag_end_e
;
200 pkt
->bp_udp
.uh_ulen
= htons(sizeof(pkt
->bp_udp
) + sizeof(pkt
->bp_bootp
));
201 pkt
->bp_ip
.ip_len
= htons(sizeof(struct ip
) + ntohs(pkt
->bp_udp
.uh_ulen
));
202 pkt
->bp_ip
.ip_sum
= 0;
207 * Function: ip_pkt_to_mbuf
209 * Put the given IP packet into an mbuf, calculate the
213 ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
)
218 m
= (struct mbuf
*)m_devget(pkt
, pktsize
, 0, 0, 0);
220 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
223 m
->m_flags
|= M_BCAST
;
224 /* Compute the checksum */
225 ip
= mtod(m
, struct ip
*);
227 ip
->ip_sum
= in_cksum(m
, sizeof (struct ip
));
231 static __inline__ u_char
*
232 link_address(struct sockaddr_dl
* dl_p
)
234 return (dl_p
->sdl_data
+ dl_p
->sdl_nlen
);
237 static __inline__
void
238 link_print(struct sockaddr_dl
* dl_p
)
243 printf("len %d index %d family %d type 0x%x nlen %d alen %d"
244 " slen %d addr ", dl_p
->sdl_len
,
245 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
246 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
248 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
249 printf("%s%x", i
? ":" : "",
250 (link_address(dl_p
))[i
]);
255 static struct sockaddr_dl
*
256 link_from_ifnet(struct ifnet
* ifp
)
258 struct ifaddr
* addr
;
260 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
262 ifnet_lock_shared(ifp
);
263 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
) {
264 if (addr
->ifa_addr
->sa_family
== AF_LINK
) {
265 struct sockaddr_dl
* dl_p
= (struct sockaddr_dl
*)(addr
->ifa_addr
);
267 ifnet_lock_done(ifp
);
271 ifnet_lock_done(ifp
);
276 * Function: send_bootp_request
278 * Send the request by calling the interface's output routine
279 * bypassing routing code.
282 send_bootp_request(struct ifnet
* ifp
, __unused
struct socket
* so
,
283 struct bootp_packet
* pkt
)
286 struct sockaddr_in sin
;
288 /* Address to send to */
290 sin
.sin_port
= htons(IPPORT_BOOTPS
);
291 sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
293 m
= ip_pkt_to_mbuf((caddr_t
)pkt
, sizeof(*pkt
));
294 return dlil_output(ifp
, PF_INET
, m
, 0, (struct sockaddr
*)&sin
, 0);
298 * Function: receive_packet
300 * Return a received packet or an error if none available.
303 receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
)
308 char uio_buf
[ UIO_SIZEOF(1) ];
310 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
311 &uio_buf
[0], sizeof(uio_buf
));
312 uio_addiov(auio
, CAST_USER_ADDR_T(pp
), psize
);
313 rcvflg
= MSG_WAITALL
;
315 error
= soreceive(so
, (struct sockaddr
**) 0, auio
, 0, 0, &rcvflg
);
316 *actual_size
= psize
- uio_resid(auio
);
321 * Function: bootp_timeout
323 * Wakeup the process waiting for something on a socket.
326 bootp_timeout(void * arg
)
328 struct socket
* * socketflag
= (struct socket
* *)arg
;
329 struct socket
* so
= *socketflag
;
331 dprintf(("bootp: timeout\n"));
335 sowakeup(so
, &so
->so_rcv
);
336 socket_unlock(so
, 1);
341 * Function: rate_packet
343 * Return an integer point rating value for the given bootp packet.
344 * If yiaddr non-zero, the packet gets a rating of 1.
345 * Another point is given if the packet contains the subnet mask,
346 * and another if the router is present.
348 #define GOOD_RATING 3
349 static __inline__
int
350 rate_packet(__unused
struct bootp
* pkt
, __unused
int pkt_size
, dhcpol_t
* options_p
)
355 if (dhcpol_find(options_p
, dhcptag_subnet_mask_e
, &len
, NULL
) != NULL
) {
358 if (dhcpol_find(options_p
, dhcptag_router_e
, &len
, NULL
) != NULL
) {
364 #define INITIAL_WAIT_SECS 4
365 #define MAX_WAIT_SECS 64
366 #define GATHER_TIME_SECS 2
367 #define RAND_TICKS (hz) /* one second */
370 * Function: bootp_loop
372 * Do the actual BOOTP protocol.
373 * The algorithm sends out a packet, waits for a response.
374 * We try max_try times, waiting in an exponentially increasing
375 * amount of time. Once we receive a good response, we start
376 * a new time period called the "gather time", during which we
377 * either find the perfect packet (one that has ip, mask and router)
378 * or we continue to gather responses. At the end of the gather period,
379 * we use the best response gathered.
382 bootp_loop(struct socket
* so
, struct ifnet
* ifp
, int max_try
,
383 struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
384 struct in_addr
* router_p
)
386 struct timeval current_time
;
387 struct sockaddr_dl
* dl_p
;
392 struct bootp_packet
* request
= NULL
;
393 struct bootp
* reply
= NULL
;
394 int reply_size
= DHCP_PACKET_MIN
;
395 struct timeval start_time
;
398 struct socket
* timeflag
;
399 int wait_ticks
= INITIAL_WAIT_SECS
* hz
;
401 /* get the hardware address from the interface */
402 dl_p
= link_from_ifnet(ifp
);
404 printf("bootp: can't get link address\n");
408 printf("bootp: h/w addr ");
411 hwaddr
= link_address(dl_p
);
412 hwlen
= dl_p
->sdl_alen
;
413 switch (dl_p
->sdl_type
) {
415 hwtype
= ARPHRD_ETHER
;
418 printf("bootp: hardware type %d not supported\n",
420 panic("bootp: hardware type not supported");
424 /* set transaction id and remember the start time */
425 microtime(&start_time
);
426 current_time
= start_time
;
429 /* make a request/reply packet */
430 request
= (struct bootp_packet
*)kalloc(sizeof(*request
));
431 make_bootp_request(request
, hwaddr
, hwtype
, hwlen
);
432 reply
= (struct bootp
*)kalloc(reply_size
);
434 printf("bootp: sending request");
435 for (retry
= 0; retry
< max_try
; retry
++) {
436 int gather_count
= 0;
439 /* Send the request */
441 request
->bp_bootp
.bp_secs
= htons((u_short
)(current_time
.tv_sec
442 - start_time
.tv_sec
));
443 request
->bp_bootp
.bp_xid
= htonl(xid
);
444 error
= send_bootp_request(ifp
, so
, request
);
449 wait_ticks
+= random_range(-RAND_TICKS
, RAND_TICKS
);
450 dprintf(("bootp: waiting %d ticks\n", wait_ticks
));
451 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
, wait_ticks
);
456 error
= receive_packet(so
, (caddr_t
)reply
, reply_size
, &n
);
458 dprintf(("\nbootp: received packet\n"));
459 if (ntohl(reply
->bp_xid
) == xid
460 && reply
->bp_yiaddr
.s_addr
461 && bcmp(reply
->bp_chaddr
, hwaddr
, hwlen
) == 0) {
466 print_reply_short(reply
, n
);
467 #endif /* BOOTP_DEBUG */
468 (void)dhcpol_parse_packet(&options
, (struct dhcp
*)reply
,
470 rating
= rate_packet(reply
, n
, &options
);
471 if (rating
> last_rating
) {
475 *iaddr_p
= reply
->bp_yiaddr
;
476 ip
= (struct in_addr
*)
477 dhcpol_find(&options
,
478 dhcptag_subnet_mask_e
, &len
, NULL
);
482 ip
= (struct in_addr
*)
483 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
487 printf("%sbootp: got "
488 "response from %s (" IP_FORMAT
")\n",
489 last_rating
== 0 ? "\n" : "",
491 IP_LIST(&reply
->bp_siaddr
));
493 dhcpol_free(&options
);
494 if (rating
>= GOOD_RATING
) {
495 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
498 if (gather_count
== 0) {
499 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
501 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
,
502 hz
* GATHER_TIME_SECS
);
507 dprintf(("bootp: packet ignored\n"));
510 else if ((error
!= EWOULDBLOCK
)) {
513 else if (timeflag
== NULL
) { /* timed out */
515 dprintf(("bootp: gathering time has expired"));
516 goto done
; /* we have a packet */
522 error
= sbwait(&so
->so_rcv
);
523 socket_unlock(so
, 1);
526 if (error
&& (error
!= EWOULDBLOCK
)) {
527 dprintf(("bootp: failed to receive packets: %d\n", error
));
528 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
532 if (wait_ticks
> (MAX_WAIT_SECS
* hz
))
533 wait_ticks
= MAX_WAIT_SECS
* hz
;
535 microtime(¤t_time
);
545 kfree(request
, sizeof (*request
));
547 kfree(reply
, reply_size
);
554 * Use the BOOTP protocol to resolve what our IP address should be
555 * on a particular interface.
557 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
558 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
561 boolean_t addr_set
= FALSE
;
564 struct socket
* so
= NULL
;
567 error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0);
569 dprintf(("bootp: socreate failed %d\n", error
));
573 /* assign the all-zeroes address */
574 bzero(&ifr
, sizeof(ifr
));
575 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
576 *((struct sockaddr_in
*)&ifr
.ifr_addr
) = blank_sin();
577 error
= ifioctl(so
, SIOCSIFADDR
, (caddr_t
)&ifr
, procp
);
579 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
583 dprintf(("bootp: all-zeroes IP address assigned\n"));
586 { /* bind the socket */
587 struct sockaddr_in
* sin
;
589 sin
= _MALLOC(sizeof(struct sockaddr_in
), M_IFADDR
, M_WAIT
);
594 sin
->sin_len
= sizeof(struct sockaddr_in
);
595 sin
->sin_family
= AF_INET
;
596 sin
->sin_port
= htons(IPPORT_BOOTPC
);
597 sin
->sin_addr
.s_addr
= INADDR_ANY
;
598 error
= sobind(so
, (struct sockaddr
*) sin
);
602 dprintf(("bootp: sobind failed, %d\n", error
));
606 so
->so_state
|= SS_NBIO
;
607 socket_unlock(so
, 1);
609 /* do the protocol */
610 error
= bootp_loop(so
, ifp
, max_try
, iaddr_p
, netmask_p
, router_p
);
615 (void) ifioctl(so
, SIOCDIFADDR
, (caddr_t
) &ifr
, procp
);