]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_bootp.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / netinet / in_bootp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
27 */
28
29 /*
30 * bootp.c
31 * - be a BOOTP client over a particular interface to retrieve
32 * the IP address, netmask, and router
33 */
34
35 /*
36 * Modification History
37 *
38 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
39 * - completely rewritten
40 */
41
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>
47 #include <sys/file.h>
48 #include <sys/uio.h>
49 #include <sys/ioctl.h>
50 #include <sys/time.h>
51 #include <sys/mbuf.h>
52 #include <sys/vnode.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <net/if.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>
71
72 #ifdef BOOTP_DEBUG
73 #define dprintf(x) printf x;
74 #else /* !BOOTP_DEBUG */
75 #define dprintf(x)
76 #endif /* BOOTP_DEBUG */
77
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]
82
83 static __inline__ struct sockaddr_in
84 blank_sin()
85 {
86 struct sockaddr_in blank = { sizeof(struct sockaddr_in), AF_INET };
87 return (blank);
88 }
89
90 static __inline__ void
91 print_reply(struct bootp *bp, int bp_len)
92 {
93 int i, j, len;
94
95 printf("bp_op = ");
96 if (bp->bp_op == BOOTREQUEST) printf("BOOTREQUEST\n");
97 else if (bp->bp_op == BOOTREPLY) printf("BOOTREPLY\n");
98 else
99 {
100 i = bp->bp_op;
101 printf("%d\n", i);
102 }
103
104 i = bp->bp_htype;
105 printf("bp_htype = %d\n", i);
106
107 len = bp->bp_hlen;
108 printf("bp_hlen = %d\n", len);
109
110 i = bp->bp_hops;
111 printf("bp_hops = %d\n", i);
112
113 printf("bp_xid = %lu\n", bp->bp_xid);
114
115 printf("bp_secs = %u\n", bp->bp_secs);
116
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));
121
122 printf("bp_chaddr = ");
123 for (j = 0; j < len; j++)
124 {
125 i = bp->bp_chaddr[j];
126 printf("%0x", i);
127 if (j < (len - 1)) printf(":");
128 }
129 printf("\n");
130
131 printf("bp_sname = %s\n", bp->bp_sname);
132 printf("bp_file = %s\n", bp->bp_file);
133 }
134
135 static __inline__ void
136 print_reply_short(struct bootp *bp, int bp_len)
137 {
138 printf("bp_yiaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_yiaddr));
139 printf("bp_sname = %s\n", bp->bp_sname);
140 }
141
142
143 static __inline__ long
144 random_range(long bottom, long top)
145 {
146 long number = top - bottom + 1;
147 long range_size = LONG_MAX / number;
148 return (((long)random()) / range_size + bottom);
149 }
150
151 /*
152 * Function: make_bootp_request
153 * Purpose:
154 * Create a "blank" bootp packet.
155 */
156 static void
157 make_bootp_request(struct bootp_packet * pkt,
158 u_char * hwaddr, u_char hwtype, u_char hwlen)
159 {
160 char rfc_magic[4] = RFC_OPTIONS_MAGIC;
161
162 bzero(pkt, sizeof (*pkt));
163 pkt->bp_ip.ip_v = IPVERSION;
164 pkt->bp_ip.ip_hl = sizeof (struct ip) >> 2;
165 #ifdef RANDOM_IP_ID
166 pkt->bp_ip.ip_id = ip_randomid();
167 #else
168 pkt->bp_ip.ip_id = htons(ip_id++);
169 #endif
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;
187 return;
188 }
189
190 /*
191 * Function: ip_pkt_to_mbuf
192 * Purpose:
193 * Put the given IP packet into an mbuf, calculate the
194 * IP checksum.
195 */
196 struct mbuf *
197 ip_pkt_to_mbuf(caddr_t pkt, int pktsize)
198 {
199 struct ip * ip;
200 struct mbuf * m;
201
202 m = (struct mbuf *)m_devget(pkt, pktsize, 0, 0, 0);
203 if (m == 0) {
204 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
205 return 0;
206 }
207 m->m_flags |= M_BCAST;
208 /* Compute the checksum */
209 ip = mtod(m, struct ip *);
210 ip->ip_sum = 0;
211 ip->ip_sum = in_cksum(m, sizeof (struct ip));
212 return (m);
213 }
214
215 static __inline__ u_char *
216 link_address(struct sockaddr_dl * dl_p)
217 {
218 return (dl_p->sdl_data + dl_p->sdl_nlen);
219 }
220
221 static __inline__ void
222 link_print(struct sockaddr_dl * dl_p)
223 {
224 int i;
225
226 #if 0
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);
231 #endif
232 for (i = 0; i < dl_p->sdl_alen; i++)
233 printf("%s%x", i ? ":" : "",
234 (link_address(dl_p))[i]);
235 printf("\n");
236 return;
237 }
238
239 static struct sockaddr_dl *
240 link_from_ifnet(struct ifnet * ifp)
241 {
242 struct ifaddr * addr;
243
244 /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
245
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);
249
250 return (dl_p);
251 }
252 }
253 return (NULL);
254 }
255
256 /*
257 * Function: send_bootp_request
258 * Purpose:
259 * Send the request by calling the interface's output routine
260 * bypassing routing code.
261 */
262 static int
263 send_bootp_request(struct ifnet * ifp, struct socket * so,
264 struct bootp_packet * pkt)
265 {
266 struct mbuf * m;
267 struct sockaddr_in sin;
268
269 /* Address to send to */
270 sin = blank_sin();
271 sin.sin_port = htons(IPPORT_BOOTPS);
272 sin.sin_addr.s_addr = INADDR_BROADCAST;
273
274 m = ip_pkt_to_mbuf((caddr_t)pkt, sizeof(*pkt));
275 return (dlil_output(ifptodlt(ifp, PF_INET), m, 0, (struct sockaddr *)&sin, 0));
276 }
277
278 /*
279 * Function: receive_packet
280 * Purpose:
281 * Return a received packet or an error if none available.
282 */
283 int
284 receive_packet(struct socket * so, caddr_t pp, int psize, int * actual_size)
285 {
286 struct iovec aiov;
287 struct uio auio;
288 int rcvflg;
289 int error;
290
291 aiov.iov_base = pp;
292 aiov.iov_len = psize;
293 auio.uio_iov = &aiov;
294 auio.uio_iovcnt = 1;
295 auio.uio_segflg = UIO_SYSSPACE;
296 auio.uio_offset = 0;
297 auio.uio_resid = psize;
298 auio.uio_rw = UIO_READ;
299 rcvflg = MSG_WAITALL;
300
301 error = soreceive(so, (struct sockaddr **) 0, &auio, 0, 0, &rcvflg);
302 *actual_size = psize - auio.uio_resid;
303 return (error);
304 }
305
306 /*
307 * Function: bootp_timeout
308 * Purpose:
309 * Wakeup the process waiting for something on a socket.
310 */
311 static void
312 bootp_timeout(void * arg)
313 {
314 struct socket * * socketflag = (struct socket * *)arg;
315 struct socket * so = *socketflag;
316 boolean_t funnel_state;
317
318 dprintf(("bootp: timeout\n"));
319
320 funnel_state = thread_funnel_set(network_flock,TRUE);
321 *socketflag = NULL;
322 sowakeup(so, &so->so_rcv);
323 (void) thread_funnel_set(network_flock, FALSE);
324 return;
325 }
326
327 /*
328 * Function: rate_packet
329 * Purpose:
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.
334 */
335 #define GOOD_RATING 3
336 static __inline__ int
337 rate_packet(struct bootp * pkt, int pkt_size, dhcpol_t * options_p)
338 {
339 int len;
340 int rating = 1;
341
342 if (dhcpol_find(options_p, dhcptag_subnet_mask_e, &len, NULL) != NULL) {
343 rating++;
344 }
345 if (dhcpol_find(options_p, dhcptag_router_e, &len, NULL) != NULL) {
346 rating++;
347 }
348 return (rating);
349 }
350
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 */
355
356 /*
357 * Function: bootp_loop
358 * Purpose:
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.
367 */
368 static int
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)
372 {
373 struct timeval current_time;
374 struct sockaddr_dl * dl_p;
375 int error = 0;
376 char * hwaddr;
377 int hwlen;
378 char hwtype = 0;
379 struct bootp_packet * request = NULL;
380 struct bootp * reply = NULL;
381 int reply_size = DHCP_PACKET_MIN;
382 struct timeval start_time;
383 u_long xid;
384 int retry;
385 struct socket * timeflag;
386 int wait_ticks = INITIAL_WAIT_SECS * hz;
387
388 /* get the hardware address from the interface */
389 dl_p = link_from_ifnet(ifp);
390 if (dl_p == NULL) {
391 printf("bootp: can't get link address\n");
392 return (ENXIO);
393 }
394
395 printf("bootp: h/w addr ");
396 link_print(dl_p);
397
398 hwaddr = link_address(dl_p);
399 hwlen = dl_p->sdl_alen;
400 switch (dl_p->sdl_type) {
401 case IFT_ETHER:
402 hwtype = ARPHRD_ETHER;
403 break;
404 default:
405 printf("bootp: hardware type %d not supported\n",
406 dl_p->sdl_type);
407 panic("bootp: hardware type not supported");
408 break;
409 }
410
411 /* set transaction id and remember the start time */
412 microtime(&start_time);
413 current_time = start_time;
414 xid = random();
415
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);
420 iaddr_p->s_addr = 0;
421 printf("bootp: sending request");
422 for (retry = 0; retry < max_try; retry++) {
423 int gather_count = 0;
424 int last_rating = 0;
425
426 /* Send the request */
427 printf(".");
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);
432 if (error)
433 goto cleanup;
434
435 timeflag = so;
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);
439
440 while (TRUE) {
441 int n = 0;
442
443 error = receive_packet(so, (caddr_t)reply, reply_size, &n);
444 if (error == 0) {
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) {
449 int rating;
450 dhcpol_t options;
451
452 #ifdef BOOTP_DEBUG
453 print_reply_short(reply, n);
454 #endif /* BOOTP_DEBUG */
455 (void)dhcpol_parse_packet(&options, (struct dhcp *)reply,
456 n, NULL);
457 rating = rate_packet(reply, n, &options);
458 if (rating > last_rating) {
459 struct in_addr * ip;
460 int len;
461
462 *iaddr_p = reply->bp_yiaddr;
463 ip = (struct in_addr *)
464 dhcpol_find(&options,
465 dhcptag_subnet_mask_e, &len, NULL);
466 if (ip) {
467 *netmask_p = *ip;
468 }
469 ip = (struct in_addr *)
470 dhcpol_find(&options, dhcptag_router_e, &len, NULL);
471 if (ip) {
472 *router_p = *ip;
473 }
474 printf("%sbootp: got "
475 "response from %s (" IP_FORMAT ")\n",
476 last_rating == 0 ? "\n" : "",
477 reply->bp_sname,
478 IP_LIST(&reply->bp_siaddr));
479 }
480 dhcpol_free(&options);
481 if (rating >= GOOD_RATING) {
482 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
483 goto done;
484 }
485 if (gather_count == 0) {
486 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
487 timeflag = so;
488 timeout((timeout_fcn_t)bootp_timeout, &timeflag,
489 hz * GATHER_TIME_SECS);
490 }
491 gather_count++;
492 }
493 else {
494 dprintf(("bootp: packet ignored\n"));
495 }
496 }
497 else if ((error != EWOULDBLOCK)) {
498 break;
499 }
500 else if (timeflag == NULL) { /* timed out */
501 if (gather_count) {
502 dprintf(("bootp: gathering time has expired"));
503 goto done; /* we have a packet */
504 }
505 break; /* retry */
506 }
507 else
508 sbwait(&so->so_rcv);
509 }
510 if (error && (error != EWOULDBLOCK)) {
511 dprintf(("bootp: failed to receive packets: %d\n", error));
512 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
513 goto cleanup;
514 }
515 wait_ticks *= 2;
516 if (wait_ticks > (MAX_WAIT_SECS * hz))
517 wait_ticks = MAX_WAIT_SECS * hz;
518 xid++;
519 microtime(&current_time);
520 }
521 error = ETIMEDOUT;
522 goto cleanup;
523
524 done:
525 error = 0;
526
527 cleanup:
528 if (request)
529 kfree((caddr_t)request, sizeof (*request));
530 if (reply)
531 kfree((caddr_t)reply, reply_size);
532 return (error);
533 }
534
535 /*
536 * Routine: bootp
537 * Function:
538 * Use the BOOTP protocol to resolve what our IP address should be
539 * on a particular interface.
540 */
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,
543 struct proc * procp)
544 {
545 boolean_t addr_set = FALSE;
546 struct ifreq ifr;
547 int error;
548 struct socket * so = NULL;
549
550 /* get a socket */
551 error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
552 if (error) {
553 dprintf(("bootp: socreate failed %d\n", error));
554 return (error);
555 }
556
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);
562 if (error) {
563 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
564 error));
565 goto cleanup;
566 }
567 dprintf(("bootp: all-zeroes IP address assigned\n"));
568 addr_set = TRUE;
569
570 { /* bind the socket */
571 struct sockaddr_in * sin;
572
573 sin = _MALLOC(sizeof(struct sockaddr_in), M_IFADDR, M_WAIT);
574 if (sin == NULL) {
575 error = ENOMEM;
576 goto cleanup;
577 }
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);
583
584 FREE(sin, M_IFADDR);
585 if (error) {
586 dprintf(("bootp: sobind failed, %d\n", error));
587 goto cleanup;
588 }
589 so->so_state |= SS_NBIO;
590 }
591 /* do the protocol */
592 error = bootp_loop(so, ifp, max_try, iaddr_p, netmask_p, router_p);
593
594 cleanup:
595 if (so) {
596 if (addr_set) {
597 (void) ifioctl(so, SIOCDIFADDR, (caddr_t) &ifr, procp);
598 }
599 soclose(so);
600 }
601 return (error);
602 }