]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_bootp.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
31 * - be a BOOTP client over a particular interface to retrieve
32 * the IP address, netmask, and router
36 * Modification History
38 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
39 * - completely rewritten
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <mach/boolean.h>
45 #include <sys/kernel.h>
46 #include <sys/errno.h>
49 #include <sys/ioctl.h>
52 #include <sys/vnode.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/if_ether.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66 #include <netinet/ip_icmp.h>
67 #include <netinet/bootp.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <netinet/dhcp_options.h>
73 #define dprintf(x) printf x;
78 /* ip address formatting macros */
79 #define IP_FORMAT "%d.%d.%d.%d"
80 #define IP_CH(ip) ((u_char *)ip)
81 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
83 static __inline__
struct sockaddr_in
86 struct sockaddr_in blank
= { sizeof(struct sockaddr_in
), AF_INET
};
90 static __inline__
void
91 print_reply(struct bootp
*bp
, int bp_len
)
96 if (bp
->bp_op
== BOOTREQUEST
) printf("BOOTREQUEST\n");
97 else if (bp
->bp_op
== BOOTREPLY
) printf("BOOTREPLY\n");
105 printf("bp_htype = %d\n", i
);
108 printf("bp_hlen = %d\n", len
);
111 printf("bp_hops = %d\n", i
);
113 printf("bp_xid = %lu\n", bp
->bp_xid
);
115 printf("bp_secs = %u\n", bp
->bp_secs
);
117 printf("bp_ciaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_ciaddr
));
118 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
119 printf("bp_siaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_siaddr
));
120 printf("bp_giaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_giaddr
));
122 printf("bp_chaddr = ");
123 for (j
= 0; j
< len
; j
++)
125 i
= bp
->bp_chaddr
[j
];
127 if (j
< (len
- 1)) printf(":");
131 printf("bp_sname = %s\n", bp
->bp_sname
);
132 printf("bp_file = %s\n", bp
->bp_file
);
135 static __inline__
void
136 print_reply_short(struct bootp
*bp
, int bp_len
)
138 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
139 printf("bp_sname = %s\n", bp
->bp_sname
);
143 static __inline__
long
144 random_range(long bottom
, long top
)
146 long number
= top
- bottom
+ 1;
147 long range_size
= LONG_MAX
/ number
;
148 return (((long)random()) / range_size
+ bottom
);
152 * Function: make_bootp_request
154 * Create a "blank" bootp packet.
157 make_bootp_request(struct bootp_packet
* pkt
,
158 u_char
* hwaddr
, u_char hwtype
, u_char hwlen
)
160 char rfc_magic
[4] = RFC_OPTIONS_MAGIC
;
162 bzero(pkt
, sizeof (*pkt
));
163 pkt
->bp_ip
.ip_v
= IPVERSION
;
164 pkt
->bp_ip
.ip_hl
= sizeof (struct ip
) >> 2;
166 pkt
->bp_ip
.ip_id
= ip_randomid();
168 pkt
->bp_ip
.ip_id
= htons(ip_id
++);
170 pkt
->bp_ip
.ip_ttl
= MAXTTL
;
171 pkt
->bp_ip
.ip_p
= IPPROTO_UDP
;
172 pkt
->bp_ip
.ip_src
.s_addr
= 0;
173 pkt
->bp_ip
.ip_dst
.s_addr
= htonl(INADDR_BROADCAST
);
174 pkt
->bp_udp
.uh_sport
= htons(IPPORT_BOOTPC
);
175 pkt
->bp_udp
.uh_dport
= htons(IPPORT_BOOTPS
);
176 pkt
->bp_udp
.uh_sum
= 0;
177 pkt
->bp_bootp
.bp_op
= BOOTREQUEST
;
178 pkt
->bp_bootp
.bp_htype
= hwtype
;
179 pkt
->bp_bootp
.bp_hlen
= hwlen
;
180 pkt
->bp_bootp
.bp_ciaddr
.s_addr
= 0;
181 bcopy(hwaddr
, pkt
->bp_bootp
.bp_chaddr
, hwlen
);
182 bcopy(rfc_magic
, pkt
->bp_bootp
.bp_vend
, sizeof(rfc_magic
));
183 pkt
->bp_bootp
.bp_vend
[4] = dhcptag_end_e
;
184 pkt
->bp_udp
.uh_ulen
= htons(sizeof(pkt
->bp_udp
) + sizeof(pkt
->bp_bootp
));
185 pkt
->bp_ip
.ip_len
= htons(sizeof(struct ip
) + ntohs(pkt
->bp_udp
.uh_ulen
));
186 pkt
->bp_ip
.ip_sum
= 0;
191 * Function: ip_pkt_to_mbuf
193 * Put the given IP packet into an mbuf, calculate the
197 ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
)
202 m
= (struct mbuf
*)m_devget(pkt
, pktsize
, 0, 0, 0);
204 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
207 m
->m_flags
|= M_BCAST
;
208 /* Compute the checksum */
209 ip
= mtod(m
, struct ip
*);
211 ip
->ip_sum
= in_cksum(m
, sizeof (struct ip
));
215 static __inline__ u_char
*
216 link_address(struct sockaddr_dl
* dl_p
)
218 return (dl_p
->sdl_data
+ dl_p
->sdl_nlen
);
221 static __inline__
void
222 link_print(struct sockaddr_dl
* dl_p
)
227 printf("len %d index %d family %d type 0x%x nlen %d alen %d"
228 " slen %d addr ", dl_p
->sdl_len
,
229 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
230 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
232 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
233 printf("%s%x", i
? ":" : "",
234 (link_address(dl_p
))[i
]);
239 static struct sockaddr_dl
*
240 link_from_ifnet(struct ifnet
* ifp
)
242 struct ifaddr
* addr
;
244 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
246 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
) {
247 if (addr
->ifa_addr
->sa_family
== AF_LINK
) {
248 struct sockaddr_dl
* dl_p
= (struct sockaddr_dl
*)(addr
->ifa_addr
);
257 * Function: send_bootp_request
259 * Send the request by calling the interface's output routine
260 * bypassing routing code.
263 send_bootp_request(struct ifnet
* ifp
, struct socket
* so
,
264 struct bootp_packet
* pkt
)
267 struct sockaddr_in sin
;
269 /* Address to send to */
271 sin
.sin_port
= htons(IPPORT_BOOTPS
);
272 sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
274 m
= ip_pkt_to_mbuf((caddr_t
)pkt
, sizeof(*pkt
));
275 return (dlil_output(ifp
->if_data
.default_proto
, m
, 0, (struct sockaddr
*)&sin
, 0));
279 * Function: receive_packet
281 * Return a received packet or an error if none available.
284 receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
)
292 aiov
.iov_len
= psize
;
293 auio
.uio_iov
= &aiov
;
295 auio
.uio_segflg
= UIO_SYSSPACE
;
297 auio
.uio_resid
= psize
;
298 auio
.uio_rw
= UIO_READ
;
299 rcvflg
= MSG_WAITALL
;
301 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, 0, 0, &rcvflg
);
302 *actual_size
= psize
- auio
.uio_resid
;
307 * Function: bootp_timeout
309 * Wakeup the process waiting for something on a socket.
312 bootp_timeout(void * arg
)
314 struct socket
* * socketflag
= (struct socket
* *)arg
;
315 struct socket
* so
= *socketflag
;
316 boolean_t funnel_state
;
318 dprintf(("bootp: timeout\n"));
320 funnel_state
= thread_funnel_set(network_flock
,TRUE
);
322 sowakeup(so
, &so
->so_rcv
);
323 (void) thread_funnel_set(network_flock
, FALSE
);
328 * Function: rate_packet
330 * Return an integer point rating value for the given bootp packet.
331 * If yiaddr non-zero, the packet gets a rating of 1.
332 * Another point is given if the packet contains the subnet mask,
333 * and another if the router is present.
335 #define GOOD_RATING 3
336 static __inline__
int
337 rate_packet(struct bootp
* pkt
, int pkt_size
, dhcpol_t
* options_p
)
342 if (dhcpol_find(options_p
, dhcptag_subnet_mask_e
, &len
, NULL
) != NULL
) {
345 if (dhcpol_find(options_p
, dhcptag_router_e
, &len
, NULL
) != NULL
) {
351 #define INITIAL_WAIT_SECS 4
352 #define MAX_WAIT_SECS 64
353 #define GATHER_TIME_SECS 2
354 #define RAND_TICKS (hz) /* one second */
357 * Function: bootp_loop
359 * Do the actual BOOTP protocol.
360 * The algorithm sends out a packet, waits for a response.
361 * We try max_try times, waiting in an exponentially increasing
362 * amount of time. Once we receive a good response, we start
363 * a new time period called the "gather time", during which we
364 * either find the perfect packet (one that has ip, mask and router)
365 * or we continue to gather responses. At the end of the gather period,
366 * we use the best response gathered.
369 bootp_loop(struct socket
* so
, struct ifnet
* ifp
, int max_try
,
370 struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
371 struct in_addr
* router_p
)
373 struct timeval current_time
;
374 struct sockaddr_dl
* dl_p
;
379 struct bootp_packet
* request
= NULL
;
380 struct bootp
* reply
= NULL
;
381 int reply_size
= DHCP_PACKET_MIN
;
382 struct timeval start_time
;
385 struct socket
* timeflag
;
386 int wait_ticks
= INITIAL_WAIT_SECS
* hz
;
388 /* get the hardware address from the interface */
389 dl_p
= link_from_ifnet(ifp
);
391 printf("bootp: can't get link address\n");
395 printf("bootp: h/w addr ");
398 hwaddr
= link_address(dl_p
);
399 hwlen
= dl_p
->sdl_alen
;
400 switch (dl_p
->sdl_type
) {
402 hwtype
= ARPHRD_ETHER
;
405 printf("bootp: hardware type %d not supported\n",
407 panic("bootp: hardware type not supported");
411 /* set transaction id and remember the start time */
412 microtime(&start_time
);
413 current_time
= start_time
;
416 /* make a request/reply packet */
417 request
= (struct bootp_packet
*)kalloc(sizeof(*request
));
418 make_bootp_request(request
, hwaddr
, hwtype
, hwlen
);
419 reply
= (struct bootp
*)kalloc(reply_size
);
421 printf("bootp: sending request");
422 for (retry
= 0; retry
< max_try
; retry
++) {
423 int gather_count
= 0;
426 /* Send the request */
428 request
->bp_bootp
.bp_secs
= htons((u_short
)(current_time
.tv_sec
429 - start_time
.tv_sec
));
430 request
->bp_bootp
.bp_xid
= htonl(xid
);
431 error
= send_bootp_request(ifp
, so
, request
);
436 wait_ticks
+= random_range(-RAND_TICKS
, RAND_TICKS
);
437 dprintf(("bootp: waiting %d ticks\n", wait_ticks
));
438 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
, wait_ticks
);
443 error
= receive_packet(so
, (caddr_t
)reply
, reply_size
, &n
);
445 dprintf(("\nbootp: received packet\n"));
446 if (ntohl(reply
->bp_xid
) == xid
447 && reply
->bp_yiaddr
.s_addr
448 && bcmp(reply
->bp_chaddr
, hwaddr
, hwlen
) == 0) {
453 print_reply_short(reply
, n
);
455 (void)dhcpol_parse_packet(&options
, (struct dhcp
*)reply
,
457 rating
= rate_packet(reply
, n
, &options
);
458 if (rating
> last_rating
) {
462 *iaddr_p
= reply
->bp_yiaddr
;
463 ip
= (struct in_addr
*)
464 dhcpol_find(&options
,
465 dhcptag_subnet_mask_e
, &len
, NULL
);
469 ip
= (struct in_addr
*)
470 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
474 printf("%sbootp: got "
475 "response from %s (" IP_FORMAT
")\n",
476 last_rating
== 0 ? "\n" : "",
478 IP_LIST(&reply
->bp_siaddr
));
480 dhcpol_free(&options
);
481 if (rating
>= GOOD_RATING
) {
482 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
485 if (gather_count
== 0) {
486 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
488 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
,
489 hz
* GATHER_TIME_SECS
);
494 dprintf(("bootp: packet ignored\n"));
497 else if ((error
!= EWOULDBLOCK
)) {
500 else if (timeflag
== NULL
) { /* timed out */
502 dprintf(("bootp: gathering time has expired"));
503 goto done
; /* we have a packet */
510 if (error
&& (error
!= EWOULDBLOCK
)) {
511 dprintf(("bootp: failed to receive packets: %d\n", error
));
512 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
516 if (wait_ticks
> (MAX_WAIT_SECS
* hz
))
517 wait_ticks
= MAX_WAIT_SECS
* hz
;
519 microtime(¤t_time
);
529 kfree((caddr_t
)request
, sizeof (*request
));
531 kfree((caddr_t
)reply
, reply_size
);
538 * Use the BOOTP protocol to resolve what our IP address should be
539 * on a particular interface.
541 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
542 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
545 boolean_t addr_set
= FALSE
;
548 struct socket
* so
= NULL
;
551 error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0);
553 dprintf(("bootp: socreate failed %d\n", error
));
557 /* assign the all-zeroes address */
558 bzero(&ifr
, sizeof(ifr
));
559 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
560 *((struct sockaddr_in
*)&ifr
.ifr_addr
) = blank_sin();
561 error
= ifioctl(so
, SIOCSIFADDR
, (caddr_t
)&ifr
, procp
);
563 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
567 dprintf(("bootp: all-zeroes IP address assigned\n"));
570 { /* bind the socket */
571 struct sockaddr_in
* sin
;
573 sin
= _MALLOC(sizeof(struct sockaddr_in
), M_IFADDR
, M_WAIT
);
578 sin
->sin_len
= sizeof(struct sockaddr_in
);
579 sin
->sin_family
= AF_INET
;
580 sin
->sin_port
= htons(IPPORT_BOOTPC
);
581 sin
->sin_addr
.s_addr
= INADDR_ANY
;
582 error
= sobind(so
, (struct sockaddr
*) sin
);
586 dprintf(("bootp: sobind failed, %d\n", error
));
589 so
->so_state
|= SS_NBIO
;
591 /* do the protocol */
592 error
= bootp_loop(so
, ifp
, max_try
, iaddr_p
, netmask_p
, router_p
);
597 (void) ifioctl(so
, SIOCDIFADDR
, (caddr_t
) &ifr
, procp
);