]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_bootp.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / netinet / in_bootp.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/*
31 * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved
32 */
33
34/*
35 * bootp.c
36 * - be a BOOTP client over a particular interface to retrieve
37 * the IP address, netmask, and router
38 */
39
40/*
41 * Modification History
42 *
43 * February 19, 1999 Dieter Siegmund (dieter@apple.com)
44 * - completely rewritten
45 */
46
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>
52#include <sys/file.h>
53#include <sys/uio.h>
54#include <sys/ioctl.h>
55#include <sys/time.h>
56#include <sys/mbuf.h>
57#include <sys/vnode.h>
58#include <sys/socket.h>
59#include <sys/socketvar.h>
91447636 60#include <sys/uio_internal.h>
1c79356b
A
61#include <net/if.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>
9bccf70c 76#include <netinet/dhcp_options.h>
1c79356b 77
91447636
A
78#include <kern/kern_types.h>
79#include <kern/kalloc.h>
80
1c79356b
A
81#ifdef BOOTP_DEBUG
82#define dprintf(x) printf x;
55e303ae 83#else /* !BOOTP_DEBUG */
1c79356b 84#define dprintf(x)
55e303ae 85#endif /* BOOTP_DEBUG */
1c79356b 86
91447636
A
87int bootp(struct ifnet * ifp, struct in_addr * iaddr_p, int max_try,
88 struct in_addr * netmask_p, struct in_addr * router_p,
89 struct proc * procp);
90struct mbuf * ip_pkt_to_mbuf(caddr_t pkt, int pktsize);
91int receive_packet(struct socket * so, caddr_t pp, int psize, int * actual_size);
92
93
1c79356b
A
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]
98
9bccf70c
A
99static __inline__ struct sockaddr_in
100blank_sin()
101{
102 struct sockaddr_in blank = { sizeof(struct sockaddr_in), AF_INET };
103 return (blank);
104}
1c79356b
A
105
106static __inline__ void
91447636 107print_reply(struct bootp *bp, __unused int bp_len)
1c79356b
A
108{
109 int i, j, len;
110
111 printf("bp_op = ");
112 if (bp->bp_op == BOOTREQUEST) printf("BOOTREQUEST\n");
113 else if (bp->bp_op == BOOTREPLY) printf("BOOTREPLY\n");
114 else
115 {
116 i = bp->bp_op;
117 printf("%d\n", i);
118 }
119
120 i = bp->bp_htype;
121 printf("bp_htype = %d\n", i);
122
123 len = bp->bp_hlen;
124 printf("bp_hlen = %d\n", len);
125
126 i = bp->bp_hops;
127 printf("bp_hops = %d\n", i);
128
129 printf("bp_xid = %lu\n", bp->bp_xid);
130
131 printf("bp_secs = %u\n", bp->bp_secs);
132
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));
137
138 printf("bp_chaddr = ");
139 for (j = 0; j < len; j++)
140 {
141 i = bp->bp_chaddr[j];
142 printf("%0x", i);
143 if (j < (len - 1)) printf(":");
144 }
145 printf("\n");
146
147 printf("bp_sname = %s\n", bp->bp_sname);
148 printf("bp_file = %s\n", bp->bp_file);
149}
150
151static __inline__ void
91447636 152print_reply_short(struct bootp *bp, __unused int bp_len)
1c79356b
A
153{
154 printf("bp_yiaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_yiaddr));
155 printf("bp_sname = %s\n", bp->bp_sname);
156}
157
158
159static __inline__ long
160random_range(long bottom, long top)
161{
162 long number = top - bottom + 1;
163 long range_size = LONG_MAX / number;
164 return (((long)random()) / range_size + bottom);
165}
166
167/*
168 * Function: make_bootp_request
169 * Purpose:
170 * Create a "blank" bootp packet.
171 */
172static void
173make_bootp_request(struct bootp_packet * pkt,
174 u_char * hwaddr, u_char hwtype, u_char hwlen)
175{
9bccf70c
A
176 char rfc_magic[4] = RFC_OPTIONS_MAGIC;
177
1c79356b
A
178 bzero(pkt, sizeof (*pkt));
179 pkt->bp_ip.ip_v = IPVERSION;
180 pkt->bp_ip.ip_hl = sizeof (struct ip) >> 2;
9bccf70c
A
181#ifdef RANDOM_IP_ID
182 pkt->bp_ip.ip_id = ip_randomid();
183#else
1c79356b 184 pkt->bp_ip.ip_id = htons(ip_id++);
9bccf70c 185#endif
1c79356b
A
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));
9bccf70c 199 pkt->bp_bootp.bp_vend[4] = dhcptag_end_e;
1c79356b
A
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;
203 return;
204}
205
206/*
207 * Function: ip_pkt_to_mbuf
208 * Purpose:
209 * Put the given IP packet into an mbuf, calculate the
210 * IP checksum.
211 */
212struct mbuf *
213ip_pkt_to_mbuf(caddr_t pkt, int pktsize)
214{
215 struct ip * ip;
216 struct mbuf * m;
217
218 m = (struct mbuf *)m_devget(pkt, pktsize, 0, 0, 0);
219 if (m == 0) {
220 printf("bootp: ip_pkt_to_mbuf: m_devget failed\n");
221 return 0;
222 }
223 m->m_flags |= M_BCAST;
224 /* Compute the checksum */
225 ip = mtod(m, struct ip *);
226 ip->ip_sum = 0;
227 ip->ip_sum = in_cksum(m, sizeof (struct ip));
228 return (m);
229}
230
231static __inline__ u_char *
232link_address(struct sockaddr_dl * dl_p)
233{
234 return (dl_p->sdl_data + dl_p->sdl_nlen);
235}
236
237static __inline__ void
238link_print(struct sockaddr_dl * dl_p)
239{
240 int i;
241
242#if 0
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);
55e303ae 247#endif
1c79356b
A
248 for (i = 0; i < dl_p->sdl_alen; i++)
249 printf("%s%x", i ? ":" : "",
250 (link_address(dl_p))[i]);
251 printf("\n");
252 return;
253}
254
255static struct sockaddr_dl *
256link_from_ifnet(struct ifnet * ifp)
257{
258 struct ifaddr * addr;
259
260/* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */
261
91447636 262 ifnet_lock_shared(ifp);
1c79356b
A
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);
266
91447636 267 ifnet_lock_done(ifp);
1c79356b
A
268 return (dl_p);
269 }
270 }
91447636 271 ifnet_lock_done(ifp);
1c79356b
A
272 return (NULL);
273}
274
275/*
276 * Function: send_bootp_request
277 * Purpose:
278 * Send the request by calling the interface's output routine
279 * bypassing routing code.
280 */
281static int
91447636 282send_bootp_request(struct ifnet * ifp, __unused struct socket * so,
1c79356b
A
283 struct bootp_packet * pkt)
284{
285 struct mbuf * m;
286 struct sockaddr_in sin;
287
288 /* Address to send to */
9bccf70c 289 sin = blank_sin();
1c79356b
A
290 sin.sin_port = htons(IPPORT_BOOTPS);
291 sin.sin_addr.s_addr = INADDR_BROADCAST;
292
293 m = ip_pkt_to_mbuf((caddr_t)pkt, sizeof(*pkt));
91447636 294 return dlil_output(ifp, PF_INET, m, 0, (struct sockaddr *)&sin, 0);
1c79356b
A
295}
296
297/*
298 * Function: receive_packet
299 * Purpose:
300 * Return a received packet or an error if none available.
301 */
302int
9bccf70c 303receive_packet(struct socket * so, caddr_t pp, int psize, int * actual_size)
1c79356b 304{
91447636 305 uio_t auio;
1c79356b
A
306 int rcvflg;
307 int error;
91447636 308 char uio_buf[ UIO_SIZEOF(1) ];
1c79356b 309
91447636
A
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);
1c79356b
A
313 rcvflg = MSG_WAITALL;
314
91447636
A
315 error = soreceive(so, (struct sockaddr **) 0, auio, 0, 0, &rcvflg);
316 *actual_size = psize - uio_resid(auio);
1c79356b
A
317 return (error);
318}
319
320/*
321 * Function: bootp_timeout
322 * Purpose:
323 * Wakeup the process waiting for something on a socket.
324 */
325static void
9bccf70c 326bootp_timeout(void * arg)
1c79356b 327{
9bccf70c 328 struct socket * * socketflag = (struct socket * *)arg;
1c79356b 329 struct socket * so = *socketflag;
1c79356b
A
330
331 dprintf(("bootp: timeout\n"));
332
1c79356b 333 *socketflag = NULL;
91447636 334 socket_lock(so, 1);
1c79356b 335 sowakeup(so, &so->so_rcv);
91447636 336 socket_unlock(so, 1);
1c79356b
A
337 return;
338}
339
1c79356b
A
340/*
341 * Function: rate_packet
342 * Purpose:
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.
347 */
348#define GOOD_RATING 3
349static __inline__ int
91447636 350rate_packet(__unused struct bootp * pkt, __unused int pkt_size, dhcpol_t * options_p)
1c79356b 351{
9bccf70c
A
352 int len;
353 int rating = 1;
1c79356b 354
9bccf70c
A
355 if (dhcpol_find(options_p, dhcptag_subnet_mask_e, &len, NULL) != NULL) {
356 rating++;
357 }
358 if (dhcpol_find(options_p, dhcptag_router_e, &len, NULL) != NULL) {
1c79356b 359 rating++;
1c79356b
A
360 }
361 return (rating);
362}
363
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 */
368
369/*
370 * Function: bootp_loop
371 * Purpose:
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.
380 */
381static int
382bootp_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)
385{
386 struct timeval current_time;
387 struct sockaddr_dl * dl_p;
388 int error = 0;
389 char * hwaddr;
390 int hwlen;
391 char hwtype = 0;
392 struct bootp_packet * request = NULL;
393 struct bootp * reply = NULL;
9bccf70c 394 int reply_size = DHCP_PACKET_MIN;
1c79356b
A
395 struct timeval start_time;
396 u_long xid;
397 int retry;
398 struct socket * timeflag;
399 int wait_ticks = INITIAL_WAIT_SECS * hz;
400
401 /* get the hardware address from the interface */
402 dl_p = link_from_ifnet(ifp);
403 if (dl_p == NULL) {
404 printf("bootp: can't get link address\n");
405 return (ENXIO);
406 }
407
408 printf("bootp: h/w addr ");
409 link_print(dl_p);
410
411 hwaddr = link_address(dl_p);
412 hwlen = dl_p->sdl_alen;
413 switch (dl_p->sdl_type) {
414 case IFT_ETHER:
415 hwtype = ARPHRD_ETHER;
416 break;
417 default:
418 printf("bootp: hardware type %d not supported\n",
419 dl_p->sdl_type);
420 panic("bootp: hardware type not supported");
421 break;
422 }
423
424 /* set transaction id and remember the start time */
425 microtime(&start_time);
426 current_time = start_time;
427 xid = random();
428
429 /* make a request/reply packet */
430 request = (struct bootp_packet *)kalloc(sizeof(*request));
431 make_bootp_request(request, hwaddr, hwtype, hwlen);
9bccf70c 432 reply = (struct bootp *)kalloc(reply_size);
1c79356b
A
433 iaddr_p->s_addr = 0;
434 printf("bootp: sending request");
435 for (retry = 0; retry < max_try; retry++) {
436 int gather_count = 0;
437 int last_rating = 0;
438
439 /* Send the request */
440 printf(".");
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);
445 if (error)
446 goto cleanup;
447
448 timeflag = so;
449 wait_ticks += random_range(-RAND_TICKS, RAND_TICKS);
450 dprintf(("bootp: waiting %d ticks\n", wait_ticks));
9bccf70c 451 timeout((timeout_fcn_t)bootp_timeout, &timeflag, wait_ticks);
1c79356b
A
452
453 while (TRUE) {
9bccf70c
A
454 int n = 0;
455
456 error = receive_packet(so, (caddr_t)reply, reply_size, &n);
1c79356b
A
457 if (error == 0) {
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) {
9bccf70c
A
462 int rating;
463 dhcpol_t options;
464
1c79356b 465#ifdef BOOTP_DEBUG
9bccf70c 466 print_reply_short(reply, n);
55e303ae 467#endif /* BOOTP_DEBUG */
9bccf70c
A
468 (void)dhcpol_parse_packet(&options, (struct dhcp *)reply,
469 n, NULL);
470 rating = rate_packet(reply, n, &options);
471 if (rating > last_rating) {
472 struct in_addr * ip;
473 int len;
474
475 *iaddr_p = reply->bp_yiaddr;
476 ip = (struct in_addr *)
477 dhcpol_find(&options,
478 dhcptag_subnet_mask_e, &len, NULL);
479 if (ip) {
480 *netmask_p = *ip;
481 }
482 ip = (struct in_addr *)
483 dhcpol_find(&options, dhcptag_router_e, &len, NULL);
484 if (ip) {
485 *router_p = *ip;
486 }
487 printf("%sbootp: got "
488 "response from %s (" IP_FORMAT ")\n",
489 last_rating == 0 ? "\n" : "",
490 reply->bp_sname,
491 IP_LIST(&reply->bp_siaddr));
492 }
493 dhcpol_free(&options);
1c79356b 494 if (rating >= GOOD_RATING) {
9bccf70c
A
495 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
496 goto done;
1c79356b
A
497 }
498 if (gather_count == 0) {
9bccf70c 499 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
1c79356b 500 timeflag = so;
9bccf70c 501 timeout((timeout_fcn_t)bootp_timeout, &timeflag,
1c79356b
A
502 hz * GATHER_TIME_SECS);
503 }
504 gather_count++;
505 }
506 else {
507 dprintf(("bootp: packet ignored\n"));
508 }
509 }
510 else if ((error != EWOULDBLOCK)) {
511 break;
512 }
513 else if (timeflag == NULL) { /* timed out */
514 if (gather_count) {
515 dprintf(("bootp: gathering time has expired"));
9bccf70c 516 goto done; /* we have a packet */
1c79356b
A
517 }
518 break; /* retry */
519 }
91447636
A
520 else {
521 socket_lock(so, 1);
522 error = sbwait(&so->so_rcv);
523 socket_unlock(so, 1);
524 }
1c79356b
A
525 }
526 if (error && (error != EWOULDBLOCK)) {
527 dprintf(("bootp: failed to receive packets: %d\n", error));
9bccf70c 528 untimeout((timeout_fcn_t)bootp_timeout, &timeflag);
1c79356b
A
529 goto cleanup;
530 }
531 wait_ticks *= 2;
532 if (wait_ticks > (MAX_WAIT_SECS * hz))
533 wait_ticks = MAX_WAIT_SECS * hz;
534 xid++;
535 microtime(&current_time);
536 }
537 error = ETIMEDOUT;
538 goto cleanup;
539
9bccf70c 540 done:
1c79356b 541 error = 0;
1c79356b
A
542
543 cleanup:
544 if (request)
91447636 545 kfree(request, sizeof (*request));
1c79356b 546 if (reply)
91447636 547 kfree(reply, reply_size);
1c79356b
A
548 return (error);
549}
550
551/*
552 * Routine: bootp
553 * Function:
554 * Use the BOOTP protocol to resolve what our IP address should be
555 * on a particular interface.
556 */
557int bootp(struct ifnet * ifp, struct in_addr * iaddr_p, int max_try,
558 struct in_addr * netmask_p, struct in_addr * router_p,
559 struct proc * procp)
560{
561 boolean_t addr_set = FALSE;
562 struct ifreq ifr;
563 int error;
564 struct socket * so = NULL;
565
566 /* get a socket */
567 error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
568 if (error) {
569 dprintf(("bootp: socreate failed %d\n", error));
570 return (error);
571 }
572
573 /* assign the all-zeroes address */
574 bzero(&ifr, sizeof(ifr));
575 sprintf(ifr.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
9bccf70c 576 *((struct sockaddr_in *)&ifr.ifr_addr) = blank_sin();
1c79356b
A
577 error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ifr, procp);
578 if (error) {
579 dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n",
580 error));
581 goto cleanup;
582 }
583 dprintf(("bootp: all-zeroes IP address assigned\n"));
584 addr_set = TRUE;
585
586 { /* bind the socket */
587 struct sockaddr_in * sin;
588
0b4e3aa0
A
589 sin = _MALLOC(sizeof(struct sockaddr_in), M_IFADDR, M_WAIT);
590 if (sin == NULL) {
591 error = ENOMEM;
592 goto cleanup;
593 }
1c79356b
A
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);
599
600 FREE(sin, M_IFADDR);
601 if (error) {
602 dprintf(("bootp: sobind failed, %d\n", error));
603 goto cleanup;
604 }
91447636 605 socket_lock(so, 1);
1c79356b 606 so->so_state |= SS_NBIO;
91447636 607 socket_unlock(so, 1);
1c79356b
A
608 }
609 /* do the protocol */
610 error = bootp_loop(so, ifp, max_try, iaddr_p, netmask_p, router_p);
611
612 cleanup:
613 if (so) {
614 if (addr_set) {
615 (void) ifioctl(so, SIOCDIFADDR, (caddr_t) &ifr, procp);
616 }
617 soclose(so);
618 }
619 return (error);
620}