]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
28 * - be a BOOTP client over a particular interface to retrieve
29 * the IP address, netmask, and router
33 * Modification History
35 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
36 * - completely rewritten
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <mach/boolean.h>
42 #include <sys/kernel.h>
43 #include <sys/errno.h>
46 #include <sys/ioctl.h>
49 #include <sys/vnode.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/udp.h>
62 #include <netinet/udp_var.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/bootp.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <netinet/dhcp_options.h>
70 #define dprintf(x) printf x;
71 #else /* !BOOTP_DEBUG */
73 #endif /* BOOTP_DEBUG */
75 /* ip address formatting macros */
76 #define IP_FORMAT "%d.%d.%d.%d"
77 #define IP_CH(ip) ((u_char *)ip)
78 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
80 static __inline__
struct sockaddr_in
83 struct sockaddr_in blank
= { sizeof(struct sockaddr_in
), AF_INET
};
87 static __inline__
void
88 print_reply(struct bootp
*bp
, int bp_len
)
93 if (bp
->bp_op
== BOOTREQUEST
) printf("BOOTREQUEST\n");
94 else if (bp
->bp_op
== BOOTREPLY
) printf("BOOTREPLY\n");
102 printf("bp_htype = %d\n", i
);
105 printf("bp_hlen = %d\n", len
);
108 printf("bp_hops = %d\n", i
);
110 printf("bp_xid = %lu\n", bp
->bp_xid
);
112 printf("bp_secs = %u\n", bp
->bp_secs
);
114 printf("bp_ciaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_ciaddr
));
115 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
116 printf("bp_siaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_siaddr
));
117 printf("bp_giaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_giaddr
));
119 printf("bp_chaddr = ");
120 for (j
= 0; j
< len
; j
++)
122 i
= bp
->bp_chaddr
[j
];
124 if (j
< (len
- 1)) printf(":");
128 printf("bp_sname = %s\n", bp
->bp_sname
);
129 printf("bp_file = %s\n", bp
->bp_file
);
132 static __inline__
void
133 print_reply_short(struct bootp
*bp
, int bp_len
)
135 printf("bp_yiaddr = " IP_FORMAT
"\n", IP_LIST(&bp
->bp_yiaddr
));
136 printf("bp_sname = %s\n", bp
->bp_sname
);
140 static __inline__
long
141 random_range(long bottom
, long top
)
143 long number
= top
- bottom
+ 1;
144 long range_size
= LONG_MAX
/ number
;
145 return (((long)random()) / range_size
+ bottom
);
149 * Function: make_bootp_request
151 * Create a "blank" bootp packet.
154 make_bootp_request(struct bootp_packet
* pkt
,
155 u_char
* hwaddr
, u_char hwtype
, u_char hwlen
)
157 char rfc_magic
[4] = RFC_OPTIONS_MAGIC
;
159 bzero(pkt
, sizeof (*pkt
));
160 pkt
->bp_ip
.ip_v
= IPVERSION
;
161 pkt
->bp_ip
.ip_hl
= sizeof (struct ip
) >> 2;
163 pkt
->bp_ip
.ip_id
= ip_randomid();
165 pkt
->bp_ip
.ip_id
= htons(ip_id
++);
167 pkt
->bp_ip
.ip_ttl
= MAXTTL
;
168 pkt
->bp_ip
.ip_p
= IPPROTO_UDP
;
169 pkt
->bp_ip
.ip_src
.s_addr
= 0;
170 pkt
->bp_ip
.ip_dst
.s_addr
= htonl(INADDR_BROADCAST
);
171 pkt
->bp_udp
.uh_sport
= htons(IPPORT_BOOTPC
);
172 pkt
->bp_udp
.uh_dport
= htons(IPPORT_BOOTPS
);
173 pkt
->bp_udp
.uh_sum
= 0;
174 pkt
->bp_bootp
.bp_op
= BOOTREQUEST
;
175 pkt
->bp_bootp
.bp_htype
= hwtype
;
176 pkt
->bp_bootp
.bp_hlen
= hwlen
;
177 pkt
->bp_bootp
.bp_ciaddr
.s_addr
= 0;
178 bcopy(hwaddr
, pkt
->bp_bootp
.bp_chaddr
, hwlen
);
179 bcopy(rfc_magic
, pkt
->bp_bootp
.bp_vend
, sizeof(rfc_magic
));
180 pkt
->bp_bootp
.bp_vend
[4] = dhcptag_end_e
;
181 pkt
->bp_udp
.uh_ulen
= htons(sizeof(pkt
->bp_udp
) + sizeof(pkt
->bp_bootp
));
182 pkt
->bp_ip
.ip_len
= htons(sizeof(struct ip
) + ntohs(pkt
->bp_udp
.uh_ulen
));
183 pkt
->bp_ip
.ip_sum
= 0;
188 * Function: ip_pkt_to_mbuf
190 * Put the given IP packet into an mbuf, calculate the
194 ip_pkt_to_mbuf(caddr_t pkt
, int pktsize
)
199 m
= (struct mbuf
*)m_devget(pkt
, pktsize
, 0, 0, 0);
201 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
204 m
->m_flags
|= M_BCAST
;
205 /* Compute the checksum */
206 ip
= mtod(m
, struct ip
*);
208 ip
->ip_sum
= in_cksum(m
, sizeof (struct ip
));
212 static __inline__ u_char
*
213 link_address(struct sockaddr_dl
* dl_p
)
215 return (dl_p
->sdl_data
+ dl_p
->sdl_nlen
);
218 static __inline__
void
219 link_print(struct sockaddr_dl
* dl_p
)
224 printf("len %d index %d family %d type 0x%x nlen %d alen %d"
225 " slen %d addr ", dl_p
->sdl_len
,
226 dl_p
->sdl_index
, dl_p
->sdl_family
, dl_p
->sdl_type
,
227 dl_p
->sdl_nlen
, dl_p
->sdl_alen
, dl_p
->sdl_slen
);
229 for (i
= 0; i
< dl_p
->sdl_alen
; i
++)
230 printf("%s%x", i
? ":" : "",
231 (link_address(dl_p
))[i
]);
236 static struct sockaddr_dl
*
237 link_from_ifnet(struct ifnet
* ifp
)
239 struct ifaddr
* addr
;
241 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
243 TAILQ_FOREACH(addr
, &ifp
->if_addrhead
, ifa_link
) {
244 if (addr
->ifa_addr
->sa_family
== AF_LINK
) {
245 struct sockaddr_dl
* dl_p
= (struct sockaddr_dl
*)(addr
->ifa_addr
);
254 * Function: send_bootp_request
256 * Send the request by calling the interface's output routine
257 * bypassing routing code.
260 send_bootp_request(struct ifnet
* ifp
, struct socket
* so
,
261 struct bootp_packet
* pkt
)
264 struct sockaddr_in sin
;
266 /* Address to send to */
268 sin
.sin_port
= htons(IPPORT_BOOTPS
);
269 sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
271 m
= ip_pkt_to_mbuf((caddr_t
)pkt
, sizeof(*pkt
));
272 return (dlil_output(ifptodlt(ifp
, PF_INET
), m
, 0, (struct sockaddr
*)&sin
, 0));
276 * Function: receive_packet
278 * Return a received packet or an error if none available.
281 receive_packet(struct socket
* so
, caddr_t pp
, int psize
, int * actual_size
)
289 aiov
.iov_len
= psize
;
290 auio
.uio_iov
= &aiov
;
292 auio
.uio_segflg
= UIO_SYSSPACE
;
294 auio
.uio_resid
= psize
;
295 auio
.uio_rw
= UIO_READ
;
296 rcvflg
= MSG_WAITALL
;
298 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, 0, 0, &rcvflg
);
299 *actual_size
= psize
- auio
.uio_resid
;
304 * Function: bootp_timeout
306 * Wakeup the process waiting for something on a socket.
309 bootp_timeout(void * arg
)
311 struct socket
* * socketflag
= (struct socket
* *)arg
;
312 struct socket
* so
= *socketflag
;
313 boolean_t funnel_state
;
315 dprintf(("bootp: timeout\n"));
317 funnel_state
= thread_funnel_set(network_flock
,TRUE
);
319 sowakeup(so
, &so
->so_rcv
);
320 (void) thread_funnel_set(network_flock
, FALSE
);
325 * Function: rate_packet
327 * Return an integer point rating value for the given bootp packet.
328 * If yiaddr non-zero, the packet gets a rating of 1.
329 * Another point is given if the packet contains the subnet mask,
330 * and another if the router is present.
332 #define GOOD_RATING 3
333 static __inline__
int
334 rate_packet(struct bootp
* pkt
, int pkt_size
, dhcpol_t
* options_p
)
339 if (dhcpol_find(options_p
, dhcptag_subnet_mask_e
, &len
, NULL
) != NULL
) {
342 if (dhcpol_find(options_p
, dhcptag_router_e
, &len
, NULL
) != NULL
) {
348 #define INITIAL_WAIT_SECS 4
349 #define MAX_WAIT_SECS 64
350 #define GATHER_TIME_SECS 2
351 #define RAND_TICKS (hz) /* one second */
354 * Function: bootp_loop
356 * Do the actual BOOTP protocol.
357 * The algorithm sends out a packet, waits for a response.
358 * We try max_try times, waiting in an exponentially increasing
359 * amount of time. Once we receive a good response, we start
360 * a new time period called the "gather time", during which we
361 * either find the perfect packet (one that has ip, mask and router)
362 * or we continue to gather responses. At the end of the gather period,
363 * we use the best response gathered.
366 bootp_loop(struct socket
* so
, struct ifnet
* ifp
, int max_try
,
367 struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
368 struct in_addr
* router_p
)
370 struct timeval current_time
;
371 struct sockaddr_dl
* dl_p
;
376 struct bootp_packet
* request
= NULL
;
377 struct bootp
* reply
= NULL
;
378 int reply_size
= DHCP_PACKET_MIN
;
379 struct timeval start_time
;
382 struct socket
* timeflag
;
383 int wait_ticks
= INITIAL_WAIT_SECS
* hz
;
385 /* get the hardware address from the interface */
386 dl_p
= link_from_ifnet(ifp
);
388 printf("bootp: can't get link address\n");
392 printf("bootp: h/w addr ");
395 hwaddr
= link_address(dl_p
);
396 hwlen
= dl_p
->sdl_alen
;
397 switch (dl_p
->sdl_type
) {
399 hwtype
= ARPHRD_ETHER
;
402 printf("bootp: hardware type %d not supported\n",
404 panic("bootp: hardware type not supported");
408 /* set transaction id and remember the start time */
409 microtime(&start_time
);
410 current_time
= start_time
;
413 /* make a request/reply packet */
414 request
= (struct bootp_packet
*)kalloc(sizeof(*request
));
415 make_bootp_request(request
, hwaddr
, hwtype
, hwlen
);
416 reply
= (struct bootp
*)kalloc(reply_size
);
418 printf("bootp: sending request");
419 for (retry
= 0; retry
< max_try
; retry
++) {
420 int gather_count
= 0;
423 /* Send the request */
425 request
->bp_bootp
.bp_secs
= htons((u_short
)(current_time
.tv_sec
426 - start_time
.tv_sec
));
427 request
->bp_bootp
.bp_xid
= htonl(xid
);
428 error
= send_bootp_request(ifp
, so
, request
);
433 wait_ticks
+= random_range(-RAND_TICKS
, RAND_TICKS
);
434 dprintf(("bootp: waiting %d ticks\n", wait_ticks
));
435 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
, wait_ticks
);
440 error
= receive_packet(so
, (caddr_t
)reply
, reply_size
, &n
);
442 dprintf(("\nbootp: received packet\n"));
443 if (ntohl(reply
->bp_xid
) == xid
444 && reply
->bp_yiaddr
.s_addr
445 && bcmp(reply
->bp_chaddr
, hwaddr
, hwlen
) == 0) {
450 print_reply_short(reply
, n
);
451 #endif /* BOOTP_DEBUG */
452 (void)dhcpol_parse_packet(&options
, (struct dhcp
*)reply
,
454 rating
= rate_packet(reply
, n
, &options
);
455 if (rating
> last_rating
) {
459 *iaddr_p
= reply
->bp_yiaddr
;
460 ip
= (struct in_addr
*)
461 dhcpol_find(&options
,
462 dhcptag_subnet_mask_e
, &len
, NULL
);
466 ip
= (struct in_addr
*)
467 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
471 printf("%sbootp: got "
472 "response from %s (" IP_FORMAT
")\n",
473 last_rating
== 0 ? "\n" : "",
475 IP_LIST(&reply
->bp_siaddr
));
477 dhcpol_free(&options
);
478 if (rating
>= GOOD_RATING
) {
479 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
482 if (gather_count
== 0) {
483 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
485 timeout((timeout_fcn_t
)bootp_timeout
, &timeflag
,
486 hz
* GATHER_TIME_SECS
);
491 dprintf(("bootp: packet ignored\n"));
494 else if ((error
!= EWOULDBLOCK
)) {
497 else if (timeflag
== NULL
) { /* timed out */
499 dprintf(("bootp: gathering time has expired"));
500 goto done
; /* we have a packet */
507 if (error
&& (error
!= EWOULDBLOCK
)) {
508 dprintf(("bootp: failed to receive packets: %d\n", error
));
509 untimeout((timeout_fcn_t
)bootp_timeout
, &timeflag
);
513 if (wait_ticks
> (MAX_WAIT_SECS
* hz
))
514 wait_ticks
= MAX_WAIT_SECS
* hz
;
516 microtime(¤t_time
);
526 kfree((caddr_t
)request
, sizeof (*request
));
528 kfree((caddr_t
)reply
, reply_size
);
535 * Use the BOOTP protocol to resolve what our IP address should be
536 * on a particular interface.
538 int bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_try
,
539 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
542 boolean_t addr_set
= FALSE
;
545 struct socket
* so
= NULL
;
548 error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0);
550 dprintf(("bootp: socreate failed %d\n", error
));
554 /* assign the all-zeroes address */
555 bzero(&ifr
, sizeof(ifr
));
556 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
557 *((struct sockaddr_in
*)&ifr
.ifr_addr
) = blank_sin();
558 error
= ifioctl(so
, SIOCSIFADDR
, (caddr_t
)&ifr
, procp
);
560 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
564 dprintf(("bootp: all-zeroes IP address assigned\n"));
567 { /* bind the socket */
568 struct sockaddr_in
* sin
;
570 sin
= _MALLOC(sizeof(struct sockaddr_in
), M_IFADDR
, M_WAIT
);
575 sin
->sin_len
= sizeof(struct sockaddr_in
);
576 sin
->sin_family
= AF_INET
;
577 sin
->sin_port
= htons(IPPORT_BOOTPC
);
578 sin
->sin_addr
.s_addr
= INADDR_ANY
;
579 error
= sobind(so
, (struct sockaddr
*) sin
);
583 dprintf(("bootp: sobind failed, %d\n", error
));
586 so
->so_state
|= SS_NBIO
;
588 /* do the protocol */
589 error
= bootp_loop(so
, ifp
, max_try
, iaddr_p
, netmask_p
, router_p
);
594 (void) ifioctl(so
, SIOCDIFADDR
, (caddr_t
) &ifr
, procp
);