]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_bootp.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
29 * - be a BOOTP client over a particular interface to retrieve
30 * the IP address, netmask, and router
34 * Modification History
36 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
37 * - completely rewritten
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <mach/boolean.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
47 #include <sys/ioctl.h>
50 #include <sys/vnode.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/uio_internal.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/udp.h>
64 #include <netinet/udp_var.h>
65 #include <netinet/ip_icmp.h>
66 #include <netinet/bootp.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
69 #include <netinet/dhcp_options.h>
71 #include <kern/kern_types.h>
72 #include <kern/kalloc.h>
75 #define dprintf(x) printf x;
76 #else /* !BOOTP_DEBUG */
78 #endif /* BOOTP_DEBUG */
80 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
81 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
83 struct mbuf
* ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
);
84 int receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
);
87 /* ip address formatting macros */
88 #define IP_FORMAT "%d.%d.%d.%d"
89 #define IP_CH(ip) ((u_char *)ip)
90 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
92 static __inline__
struct sockaddr_in
95 struct sockaddr_in blank
= { sizeof(struct sockaddr_in
), AF_INET
};
99 static __inline__
void
100 print_reply(struct bootp
*bp
, __unused
int bp_len
)
105 if (bp
->bp_op
== BOOTREQUEST
) printf("BOOTREQUEST\n");
106 else if (bp
->bp_op
== BOOTREPLY
) printf("BOOTREPLY\n");
114 printf("bp_htype = %d\n", i
);
117 printf("bp_hlen = %d\n", len
);
120 printf("bp_hops = %d\n", i
);
122 printf("bp_xid = %lu\n", bp
->bp_xid
);
124 printf("bp_secs = %u\n", bp
->bp_secs
);
126 printf("bp_ciaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_ciaddr
));
127 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
128 printf("bp_siaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_siaddr
));
129 printf("bp_giaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_giaddr
));
131 printf("bp_chaddr = ");
132 for (j
= 0; j
< len
; j
++)
134 i
= bp
->bp_chaddr
[j
];
136 if (j
< (len
- 1)) printf(":");
140 printf("bp_sname = %s\n", bp
->bp_sname
);
141 printf("bp_file = %s\n", bp
->bp_file
);
144 static __inline__
void
145 print_reply_short(struct bootp
*bp
, __unused
int bp_len
)
147 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
148 printf("bp_sname = %s\n", bp
->bp_sname
);
152 static __inline__
long
153 random_range(long bottom
, long top
)
155 long number
= top
- bottom
+ 1;
156 long range_size
= LONG_MAX
/ number
;
157 return (((long)random()) / range_size
+ bottom
);
161 * Function: make_bootp_request
163 * Create a "blank" bootp packet.
166 make_bootp_request(struct bootp_packet
* pkt
,
167 u_char
* hwaddr
, u_char hwtype
, u_char hwlen
)
169 char rfc_magic
[4] = RFC_OPTIONS_MAGIC
;
171 bzero(pkt
, sizeof (*pkt
));
172 pkt
->bp_ip
.ip_v
= IPVERSION
;
173 pkt
->bp_ip
.ip_hl
= sizeof (struct ip
) >> 2;
175 pkt
->bp_ip
.ip_id
= ip_randomid();
177 pkt
->bp_ip
.ip_id
= htons(ip_id
++);
179 pkt
->bp_ip
.ip_ttl
= MAXTTL
;
180 pkt
->bp_ip
.ip_p
= IPPROTO_UDP
;
181 pkt
->bp_ip
.ip_src
.s_addr
= 0;
182 pkt
->bp_ip
.ip_dst
.s_addr
= htonl(INADDR_BROADCAST
);
183 pkt
->bp_udp
.uh_sport
= htons(IPPORT_BOOTPC
);
184 pkt
->bp_udp
.uh_dport
= htons(IPPORT_BOOTPS
);
185 pkt
->bp_udp
.uh_sum
= 0;
186 pkt
->bp_bootp
.bp_op
= BOOTREQUEST
;
187 pkt
->bp_bootp
.bp_htype
= hwtype
;
188 pkt
->bp_bootp
.bp_hlen
= hwlen
;
189 pkt
->bp_bootp
.bp_ciaddr
.s_addr
= 0;
190 bcopy(hwaddr
, pkt
->bp_bootp
.bp_chaddr
, hwlen
);
191 bcopy(rfc_magic
, pkt
->bp_bootp
.bp_vend
, sizeof(rfc_magic
));
192 pkt
->bp_bootp
.bp_vend
[4] = dhcptag_end_e
;
193 pkt
->bp_udp
.uh_ulen
= htons(sizeof(pkt
->bp_udp
) + sizeof(pkt
->bp_bootp
));
194 pkt
->bp_ip
.ip_len
= htons(sizeof(struct ip
) + ntohs(pkt
->bp_udp
.uh_ulen
));
195 pkt
->bp_ip
.ip_sum
= 0;
200 * Function: ip_pkt_to_mbuf
202 * Put the given IP packet into an mbuf, calculate the
206 ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
)
211 m
= (struct mbuf
*)m_devget(pkt
, pktsize
, 0, 0, 0);
213 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
216 m
->m_flags
|= M_BCAST
;
217 /* Compute the checksum */
218 ip
= mtod(m
, struct ip
*);
220 ip
->ip_sum
= in_cksum(m
, sizeof (struct ip
));
224 static __inline__ u_char
*
225 link_address(struct sockaddr_dl
* dl_p
)
227 return (dl_p
->sdl_data
+ dl_p
->sdl_nlen
);
230 static __inline__
void
231 link_print(struct sockaddr_dl
* dl_p
)
236 printf("len %d index %d family %d type 0x%x nlen %d alen %d"
237 " slen %d addr ", dl_p
->sdl_len
,
238 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
239 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
241 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
242 printf("%s%x", i
? ":" : "",
243 (link_address(dl_p
))[i
]);
248 static struct sockaddr_dl
*
249 link_from_ifnet(struct ifnet
* ifp
)
251 struct ifaddr
* addr
;
253 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
255 ifnet_lock_shared(ifp
);
256 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
) {
257 if (addr
->ifa_addr
->sa_family
== AF_LINK
) {
258 struct sockaddr_dl
* dl_p
= (struct sockaddr_dl
*)(addr
->ifa_addr
);
260 ifnet_lock_done(ifp
);
264 ifnet_lock_done(ifp
);
269 * Function: send_bootp_request
271 * Send the request by calling the interface's output routine
272 * bypassing routing code.
275 send_bootp_request(struct ifnet
* ifp
, __unused
struct socket
* so
,
276 struct bootp_packet
* pkt
)
279 struct sockaddr_in sin
;
281 /* Address to send to */
283 sin
.sin_port
= htons(IPPORT_BOOTPS
);
284 sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
286 m
= ip_pkt_to_mbuf((caddr_t
)pkt
, sizeof(*pkt
));
287 return dlil_output(ifp
, PF_INET
, m
, 0, (struct sockaddr
*)&sin
, 0);
291 * Function: receive_packet
293 * Return a received packet or an error if none available.
296 receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
)
301 char uio_buf
[ UIO_SIZEOF(1) ];
303 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
304 &uio_buf
[0], sizeof(uio_buf
));
305 uio_addiov(auio
, CAST_USER_ADDR_T(pp
), psize
);
306 rcvflg
= MSG_WAITALL
;
308 error
= soreceive(so
, (struct sockaddr
**) 0, auio
, 0, 0, &rcvflg
);
309 *actual_size
= psize
- uio_resid(auio
);
314 * Function: bootp_timeout
316 * Wakeup the process waiting for something on a socket.
319 bootp_timeout(void * arg
)
321 struct socket
* * socketflag
= (struct socket
* *)arg
;
322 struct socket
* so
= *socketflag
;
324 dprintf(("bootp: timeout\n"));
328 sowakeup(so
, &so
->so_rcv
);
329 socket_unlock(so
, 1);
334 * Function: rate_packet
336 * Return an integer point rating value for the given bootp packet.
337 * If yiaddr non-zero, the packet gets a rating of 1.
338 * Another point is given if the packet contains the subnet mask,
339 * and another if the router is present.
341 #define GOOD_RATING 3
342 static __inline__
int
343 rate_packet(__unused
struct bootp
* pkt
, __unused
int pkt_size
, dhcpol_t
* options_p
)
348 if (dhcpol_find(options_p
, dhcptag_subnet_mask_e
, &len
, NULL
) != NULL
) {
351 if (dhcpol_find(options_p
, dhcptag_router_e
, &len
, NULL
) != NULL
) {
357 #define INITIAL_WAIT_SECS 4
358 #define MAX_WAIT_SECS 64
359 #define GATHER_TIME_SECS 2
360 #define RAND_TICKS (hz) /* one second */
363 * Function: bootp_loop
365 * Do the actual BOOTP protocol.
366 * The algorithm sends out a packet, waits for a response.
367 * We try max_try times, waiting in an exponentially increasing
368 * amount of time. Once we receive a good response, we start
369 * a new time period called the "gather time", during which we
370 * either find the perfect packet (one that has ip, mask and router)
371 * or we continue to gather responses. At the end of the gather period,
372 * we use the best response gathered.
375 bootp_loop(struct socket
* so
, struct ifnet
* ifp
, int max_try
,
376 struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
377 struct in_addr
* router_p
)
379 struct timeval current_time
;
380 struct sockaddr_dl
* dl_p
;
385 struct bootp_packet
* request
= NULL
;
386 struct bootp
* reply
= NULL
;
387 int reply_size
= DHCP_PACKET_MIN
;
388 struct timeval start_time
;
391 struct socket
* timeflag
;
392 int wait_ticks
= INITIAL_WAIT_SECS
* hz
;
394 /* get the hardware address from the interface */
395 dl_p
= link_from_ifnet(ifp
);
397 printf("bootp: can't get link address\n");
401 printf("bootp: h/w addr ");
404 hwaddr
= link_address(dl_p
);
405 hwlen
= dl_p
->sdl_alen
;
406 switch (dl_p
->sdl_type
) {
408 hwtype
= ARPHRD_ETHER
;
411 printf("bootp: hardware type %d not supported\n",
413 panic("bootp: hardware type not supported");
417 /* set transaction id and remember the start time */
418 microtime(&start_time
);
419 current_time
= start_time
;
422 /* make a request/reply packet */
423 request
= (struct bootp_packet
*)kalloc(sizeof(*request
));
424 make_bootp_request(request
, hwaddr
, hwtype
, hwlen
);
425 reply
= (struct bootp
*)kalloc(reply_size
);
427 printf("bootp: sending request");
428 for (retry
= 0; retry
< max_try
; retry
++) {
429 int gather_count
= 0;
432 /* Send the request */
434 request
->bp_bootp
.bp_secs
= htons((u_short
)(current_time
.tv_sec
435 - start_time
.tv_sec
));
436 request
->bp_bootp
.bp_xid
= htonl(xid
);
437 error
= send_bootp_request(ifp
, so
, request
);
442 wait_ticks
+= random_range(-RAND_TICKS
, RAND_TICKS
);
443 dprintf(("bootp: waiting %d ticks\n", wait_ticks
));
444 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
, wait_ticks
);
449 error
= receive_packet(so
, (caddr_t
)reply
, reply_size
, &n
);
451 dprintf(("\nbootp: received packet\n"));
452 if (ntohl(reply
->bp_xid
) == xid
453 && reply
->bp_yiaddr
.s_addr
454 && bcmp(reply
->bp_chaddr
, hwaddr
, hwlen
) == 0) {
459 print_reply_short(reply
, n
);
460 #endif /* BOOTP_DEBUG */
461 (void)dhcpol_parse_packet(&options
, (struct dhcp
*)reply
,
463 rating
= rate_packet(reply
, n
, &options
);
464 if (rating
> last_rating
) {
468 *iaddr_p
= reply
->bp_yiaddr
;
469 ip
= (struct in_addr
*)
470 dhcpol_find(&options
,
471 dhcptag_subnet_mask_e
, &len
, NULL
);
475 ip
= (struct in_addr
*)
476 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
480 printf("%sbootp: got "
481 "response from %s (" IP_FORMAT
")\n",
482 last_rating
== 0 ? "\n" : "",
484 IP_LIST(&reply
->bp_siaddr
));
486 dhcpol_free(&options
);
487 if (rating
>= GOOD_RATING
) {
488 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
491 if (gather_count
== 0) {
492 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
494 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
,
495 hz
* GATHER_TIME_SECS
);
500 dprintf(("bootp: packet ignored\n"));
503 else if ((error
!= EWOULDBLOCK
)) {
506 else if (timeflag
== NULL
) { /* timed out */
508 dprintf(("bootp: gathering time has expired"));
509 goto done
; /* we have a packet */
515 error
= sbwait(&so
->so_rcv
);
516 socket_unlock(so
, 1);
519 if (error
&& (error
!= EWOULDBLOCK
)) {
520 dprintf(("bootp: failed to receive packets: %d\n", error
));
521 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
525 if (wait_ticks
> (MAX_WAIT_SECS
* hz
))
526 wait_ticks
= MAX_WAIT_SECS
* hz
;
528 microtime(¤t_time
);
538 kfree(request
, sizeof (*request
));
540 kfree(reply
, reply_size
);
547 * Use the BOOTP protocol to resolve what our IP address should be
548 * on a particular interface.
550 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
551 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
554 boolean_t addr_set
= FALSE
;
557 struct socket
* so
= NULL
;
560 error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0);
562 dprintf(("bootp: socreate failed %d\n", error
));
566 /* assign the all-zeroes address */
567 bzero(&ifr
, sizeof(ifr
));
568 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
569 *((struct sockaddr_in
*)&ifr
.ifr_addr
) = blank_sin();
570 error
= ifioctl(so
, SIOCSIFADDR
, (caddr_t
)&ifr
, procp
);
572 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
576 dprintf(("bootp: all-zeroes IP address assigned\n"));
579 { /* bind the socket */
580 struct sockaddr_in
* sin
;
582 sin
= _MALLOC(sizeof(struct sockaddr_in
), M_IFADDR
, M_WAIT
);
587 sin
->sin_len
= sizeof(struct sockaddr_in
);
588 sin
->sin_family
= AF_INET
;
589 sin
->sin_port
= htons(IPPORT_BOOTPC
);
590 sin
->sin_addr
.s_addr
= INADDR_ANY
;
591 error
= sobind(so
, (struct sockaddr
*) sin
);
595 dprintf(("bootp: sobind failed, %d\n", error
));
599 so
->so_state
|= SS_NBIO
;
600 socket_unlock(so
, 1);
602 /* do the protocol */
603 error
= bootp_loop(so
, ifp
, max_try
, iaddr_p
, netmask_p
, router_p
);
608 (void) ifioctl(so
, SIOCDIFADDR
, (caddr_t
) &ifr
, procp
);