]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 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 License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1988-1999 Apple Computer, Inc. All Rights Reserved | |
30 | */ | |
31 | ||
32 | /* | |
33 | * bootp.c | |
34 | * - be a BOOTP client over a particular interface to retrieve | |
35 | * the IP address, netmask, and router | |
36 | */ | |
37 | ||
38 | /* | |
39 | * Modification History | |
40 | * | |
41 | * February 19, 1999 Dieter Siegmund (dieter@apple.com) | |
42 | * - completely rewritten | |
43 | */ | |
44 | ||
45 | #include <sys/param.h> | |
46 | #include <sys/types.h> | |
47 | #include <mach/boolean.h> | |
48 | #include <sys/kernel.h> | |
49 | #include <sys/errno.h> | |
50 | #include <sys/file.h> | |
51 | #include <sys/uio.h> | |
52 | #include <sys/ioctl.h> | |
53 | #include <sys/time.h> | |
54 | #include <sys/mbuf.h> | |
55 | #include <sys/vnode.h> | |
56 | #include <sys/socket.h> | |
57 | #include <sys/socketvar.h> | |
91447636 | 58 | #include <sys/uio_internal.h> |
1c79356b A |
59 | #include <net/if.h> |
60 | #include <net/if_dl.h> | |
61 | #include <net/if_types.h> | |
62 | #include <net/route.h> | |
63 | #include <netinet/in.h> | |
64 | #include <netinet/in_systm.h> | |
65 | #include <netinet/if_ether.h> | |
66 | #include <netinet/ip.h> | |
67 | #include <netinet/ip_var.h> | |
68 | #include <netinet/udp.h> | |
69 | #include <netinet/udp_var.h> | |
70 | #include <netinet/ip_icmp.h> | |
71 | #include <netinet/bootp.h> | |
72 | #include <sys/systm.h> | |
73 | #include <sys/malloc.h> | |
9bccf70c | 74 | #include <netinet/dhcp_options.h> |
1c79356b | 75 | |
91447636 A |
76 | #include <kern/kern_types.h> |
77 | #include <kern/kalloc.h> | |
78 | ||
1c79356b A |
79 | #ifdef BOOTP_DEBUG |
80 | #define dprintf(x) printf x; | |
55e303ae | 81 | #else /* !BOOTP_DEBUG */ |
1c79356b | 82 | #define dprintf(x) |
55e303ae | 83 | #endif /* BOOTP_DEBUG */ |
1c79356b | 84 | |
91447636 A |
85 | int bootp(struct ifnet * ifp, struct in_addr * iaddr_p, int max_try, |
86 | struct in_addr * netmask_p, struct in_addr * router_p, | |
87 | struct proc * procp); | |
88 | struct mbuf * ip_pkt_to_mbuf(caddr_t pkt, int pktsize); | |
89 | int receive_packet(struct socket * so, caddr_t pp, int psize, int * actual_size); | |
90 | ||
91 | ||
1c79356b A |
92 | /* ip address formatting macros */ |
93 | #define IP_FORMAT "%d.%d.%d.%d" | |
94 | #define IP_CH(ip) ((u_char *)ip) | |
95 | #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3] | |
96 | ||
9bccf70c A |
97 | static __inline__ struct sockaddr_in |
98 | blank_sin() | |
99 | { | |
100 | struct sockaddr_in blank = { sizeof(struct sockaddr_in), AF_INET }; | |
101 | return (blank); | |
102 | } | |
1c79356b A |
103 | |
104 | static __inline__ void | |
91447636 | 105 | print_reply(struct bootp *bp, __unused int bp_len) |
1c79356b A |
106 | { |
107 | int i, j, len; | |
108 | ||
109 | printf("bp_op = "); | |
110 | if (bp->bp_op == BOOTREQUEST) printf("BOOTREQUEST\n"); | |
111 | else if (bp->bp_op == BOOTREPLY) printf("BOOTREPLY\n"); | |
112 | else | |
113 | { | |
114 | i = bp->bp_op; | |
115 | printf("%d\n", i); | |
116 | } | |
117 | ||
118 | i = bp->bp_htype; | |
119 | printf("bp_htype = %d\n", i); | |
120 | ||
121 | len = bp->bp_hlen; | |
122 | printf("bp_hlen = %d\n", len); | |
123 | ||
124 | i = bp->bp_hops; | |
125 | printf("bp_hops = %d\n", i); | |
126 | ||
127 | printf("bp_xid = %lu\n", bp->bp_xid); | |
128 | ||
129 | printf("bp_secs = %u\n", bp->bp_secs); | |
130 | ||
131 | printf("bp_ciaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_ciaddr)); | |
132 | printf("bp_yiaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_yiaddr)); | |
133 | printf("bp_siaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_siaddr)); | |
134 | printf("bp_giaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_giaddr)); | |
135 | ||
136 | printf("bp_chaddr = "); | |
137 | for (j = 0; j < len; j++) | |
138 | { | |
139 | i = bp->bp_chaddr[j]; | |
140 | printf("%0x", i); | |
141 | if (j < (len - 1)) printf(":"); | |
142 | } | |
143 | printf("\n"); | |
144 | ||
145 | printf("bp_sname = %s\n", bp->bp_sname); | |
146 | printf("bp_file = %s\n", bp->bp_file); | |
147 | } | |
148 | ||
149 | static __inline__ void | |
91447636 | 150 | print_reply_short(struct bootp *bp, __unused int bp_len) |
1c79356b A |
151 | { |
152 | printf("bp_yiaddr = " IP_FORMAT "\n", IP_LIST(&bp->bp_yiaddr)); | |
153 | printf("bp_sname = %s\n", bp->bp_sname); | |
154 | } | |
155 | ||
156 | ||
157 | static __inline__ long | |
158 | random_range(long bottom, long top) | |
159 | { | |
160 | long number = top - bottom + 1; | |
161 | long range_size = LONG_MAX / number; | |
162 | return (((long)random()) / range_size + bottom); | |
163 | } | |
164 | ||
165 | /* | |
166 | * Function: make_bootp_request | |
167 | * Purpose: | |
168 | * Create a "blank" bootp packet. | |
169 | */ | |
170 | static void | |
171 | make_bootp_request(struct bootp_packet * pkt, | |
172 | u_char * hwaddr, u_char hwtype, u_char hwlen) | |
173 | { | |
9bccf70c A |
174 | char rfc_magic[4] = RFC_OPTIONS_MAGIC; |
175 | ||
1c79356b A |
176 | bzero(pkt, sizeof (*pkt)); |
177 | pkt->bp_ip.ip_v = IPVERSION; | |
178 | pkt->bp_ip.ip_hl = sizeof (struct ip) >> 2; | |
9bccf70c A |
179 | #ifdef RANDOM_IP_ID |
180 | pkt->bp_ip.ip_id = ip_randomid(); | |
181 | #else | |
1c79356b | 182 | pkt->bp_ip.ip_id = htons(ip_id++); |
9bccf70c | 183 | #endif |
1c79356b A |
184 | pkt->bp_ip.ip_ttl = MAXTTL; |
185 | pkt->bp_ip.ip_p = IPPROTO_UDP; | |
186 | pkt->bp_ip.ip_src.s_addr = 0; | |
187 | pkt->bp_ip.ip_dst.s_addr = htonl(INADDR_BROADCAST); | |
188 | pkt->bp_udp.uh_sport = htons(IPPORT_BOOTPC); | |
189 | pkt->bp_udp.uh_dport = htons(IPPORT_BOOTPS); | |
190 | pkt->bp_udp.uh_sum = 0; | |
191 | pkt->bp_bootp.bp_op = BOOTREQUEST; | |
192 | pkt->bp_bootp.bp_htype = hwtype; | |
193 | pkt->bp_bootp.bp_hlen = hwlen; | |
194 | pkt->bp_bootp.bp_ciaddr.s_addr = 0; | |
195 | bcopy(hwaddr, pkt->bp_bootp.bp_chaddr, hwlen); | |
196 | bcopy(rfc_magic, pkt->bp_bootp.bp_vend, sizeof(rfc_magic)); | |
9bccf70c | 197 | pkt->bp_bootp.bp_vend[4] = dhcptag_end_e; |
1c79356b A |
198 | pkt->bp_udp.uh_ulen = htons(sizeof(pkt->bp_udp) + sizeof(pkt->bp_bootp)); |
199 | pkt->bp_ip.ip_len = htons(sizeof(struct ip) + ntohs(pkt->bp_udp.uh_ulen)); | |
200 | pkt->bp_ip.ip_sum = 0; | |
201 | return; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Function: ip_pkt_to_mbuf | |
206 | * Purpose: | |
207 | * Put the given IP packet into an mbuf, calculate the | |
208 | * IP checksum. | |
209 | */ | |
210 | struct mbuf * | |
211 | ip_pkt_to_mbuf(caddr_t pkt, int pktsize) | |
212 | { | |
213 | struct ip * ip; | |
214 | struct mbuf * m; | |
215 | ||
216 | m = (struct mbuf *)m_devget(pkt, pktsize, 0, 0, 0); | |
217 | if (m == 0) { | |
218 | printf("bootp: ip_pkt_to_mbuf: m_devget failed\n"); | |
219 | return 0; | |
220 | } | |
221 | m->m_flags |= M_BCAST; | |
222 | /* Compute the checksum */ | |
223 | ip = mtod(m, struct ip *); | |
224 | ip->ip_sum = 0; | |
225 | ip->ip_sum = in_cksum(m, sizeof (struct ip)); | |
226 | return (m); | |
227 | } | |
228 | ||
229 | static __inline__ u_char * | |
230 | link_address(struct sockaddr_dl * dl_p) | |
231 | { | |
232 | return (dl_p->sdl_data + dl_p->sdl_nlen); | |
233 | } | |
234 | ||
235 | static __inline__ void | |
236 | link_print(struct sockaddr_dl * dl_p) | |
237 | { | |
238 | int i; | |
239 | ||
240 | #if 0 | |
241 | printf("len %d index %d family %d type 0x%x nlen %d alen %d" | |
242 | " slen %d addr ", dl_p->sdl_len, | |
243 | dl_p->sdl_index, dl_p->sdl_family, dl_p->sdl_type, | |
244 | dl_p->sdl_nlen, dl_p->sdl_alen, dl_p->sdl_slen); | |
55e303ae | 245 | #endif |
1c79356b A |
246 | for (i = 0; i < dl_p->sdl_alen; i++) |
247 | printf("%s%x", i ? ":" : "", | |
248 | (link_address(dl_p))[i]); | |
249 | printf("\n"); | |
250 | return; | |
251 | } | |
252 | ||
253 | static struct sockaddr_dl * | |
254 | link_from_ifnet(struct ifnet * ifp) | |
255 | { | |
256 | struct ifaddr * addr; | |
257 | ||
258 | /* for (addr = ifp->if_addrlist; addr; addr = addr->ifa_next) */ | |
259 | ||
91447636 | 260 | ifnet_lock_shared(ifp); |
1c79356b A |
261 | TAILQ_FOREACH(addr, &ifp->if_addrhead, ifa_link) { |
262 | if (addr->ifa_addr->sa_family == AF_LINK) { | |
263 | struct sockaddr_dl * dl_p = (struct sockaddr_dl *)(addr->ifa_addr); | |
264 | ||
91447636 | 265 | ifnet_lock_done(ifp); |
1c79356b A |
266 | return (dl_p); |
267 | } | |
268 | } | |
91447636 | 269 | ifnet_lock_done(ifp); |
1c79356b A |
270 | return (NULL); |
271 | } | |
272 | ||
273 | /* | |
274 | * Function: send_bootp_request | |
275 | * Purpose: | |
276 | * Send the request by calling the interface's output routine | |
277 | * bypassing routing code. | |
278 | */ | |
279 | static int | |
91447636 | 280 | send_bootp_request(struct ifnet * ifp, __unused struct socket * so, |
1c79356b A |
281 | struct bootp_packet * pkt) |
282 | { | |
283 | struct mbuf * m; | |
284 | struct sockaddr_in sin; | |
285 | ||
286 | /* Address to send to */ | |
9bccf70c | 287 | sin = blank_sin(); |
1c79356b A |
288 | sin.sin_port = htons(IPPORT_BOOTPS); |
289 | sin.sin_addr.s_addr = INADDR_BROADCAST; | |
290 | ||
291 | m = ip_pkt_to_mbuf((caddr_t)pkt, sizeof(*pkt)); | |
91447636 | 292 | return dlil_output(ifp, PF_INET, m, 0, (struct sockaddr *)&sin, 0); |
1c79356b A |
293 | } |
294 | ||
295 | /* | |
296 | * Function: receive_packet | |
297 | * Purpose: | |
298 | * Return a received packet or an error if none available. | |
299 | */ | |
300 | int | |
9bccf70c | 301 | receive_packet(struct socket * so, caddr_t pp, int psize, int * actual_size) |
1c79356b | 302 | { |
91447636 | 303 | uio_t auio; |
1c79356b A |
304 | int rcvflg; |
305 | int error; | |
91447636 | 306 | char uio_buf[ UIO_SIZEOF(1) ]; |
1c79356b | 307 | |
91447636 A |
308 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, |
309 | &uio_buf[0], sizeof(uio_buf)); | |
310 | uio_addiov(auio, CAST_USER_ADDR_T(pp), psize); | |
1c79356b A |
311 | rcvflg = MSG_WAITALL; |
312 | ||
91447636 A |
313 | error = soreceive(so, (struct sockaddr **) 0, auio, 0, 0, &rcvflg); |
314 | *actual_size = psize - uio_resid(auio); | |
1c79356b A |
315 | return (error); |
316 | } | |
317 | ||
318 | /* | |
319 | * Function: bootp_timeout | |
320 | * Purpose: | |
321 | * Wakeup the process waiting for something on a socket. | |
322 | */ | |
323 | static void | |
9bccf70c | 324 | bootp_timeout(void * arg) |
1c79356b | 325 | { |
9bccf70c | 326 | struct socket * * socketflag = (struct socket * *)arg; |
1c79356b | 327 | struct socket * so = *socketflag; |
1c79356b A |
328 | |
329 | dprintf(("bootp: timeout\n")); | |
330 | ||
1c79356b | 331 | *socketflag = NULL; |
91447636 | 332 | socket_lock(so, 1); |
1c79356b | 333 | sowakeup(so, &so->so_rcv); |
91447636 | 334 | socket_unlock(so, 1); |
1c79356b A |
335 | return; |
336 | } | |
337 | ||
1c79356b A |
338 | /* |
339 | * Function: rate_packet | |
340 | * Purpose: | |
341 | * Return an integer point rating value for the given bootp packet. | |
342 | * If yiaddr non-zero, the packet gets a rating of 1. | |
343 | * Another point is given if the packet contains the subnet mask, | |
344 | * and another if the router is present. | |
345 | */ | |
346 | #define GOOD_RATING 3 | |
347 | static __inline__ int | |
91447636 | 348 | rate_packet(__unused struct bootp * pkt, __unused int pkt_size, dhcpol_t * options_p) |
1c79356b | 349 | { |
9bccf70c A |
350 | int len; |
351 | int rating = 1; | |
1c79356b | 352 | |
9bccf70c A |
353 | if (dhcpol_find(options_p, dhcptag_subnet_mask_e, &len, NULL) != NULL) { |
354 | rating++; | |
355 | } | |
356 | if (dhcpol_find(options_p, dhcptag_router_e, &len, NULL) != NULL) { | |
1c79356b | 357 | rating++; |
1c79356b A |
358 | } |
359 | return (rating); | |
360 | } | |
361 | ||
362 | #define INITIAL_WAIT_SECS 4 | |
363 | #define MAX_WAIT_SECS 64 | |
364 | #define GATHER_TIME_SECS 2 | |
365 | #define RAND_TICKS (hz) /* one second */ | |
366 | ||
367 | /* | |
368 | * Function: bootp_loop | |
369 | * Purpose: | |
370 | * Do the actual BOOTP protocol. | |
371 | * The algorithm sends out a packet, waits for a response. | |
372 | * We try max_try times, waiting in an exponentially increasing | |
373 | * amount of time. Once we receive a good response, we start | |
374 | * a new time period called the "gather time", during which we | |
375 | * either find the perfect packet (one that has ip, mask and router) | |
376 | * or we continue to gather responses. At the end of the gather period, | |
377 | * we use the best response gathered. | |
378 | */ | |
379 | static int | |
380 | bootp_loop(struct socket * so, struct ifnet * ifp, int max_try, | |
381 | struct in_addr * iaddr_p, struct in_addr * netmask_p, | |
382 | struct in_addr * router_p) | |
383 | { | |
384 | struct timeval current_time; | |
385 | struct sockaddr_dl * dl_p; | |
386 | int error = 0; | |
387 | char * hwaddr; | |
388 | int hwlen; | |
389 | char hwtype = 0; | |
390 | struct bootp_packet * request = NULL; | |
391 | struct bootp * reply = NULL; | |
9bccf70c | 392 | int reply_size = DHCP_PACKET_MIN; |
1c79356b A |
393 | struct timeval start_time; |
394 | u_long xid; | |
395 | int retry; | |
396 | struct socket * timeflag; | |
397 | int wait_ticks = INITIAL_WAIT_SECS * hz; | |
398 | ||
399 | /* get the hardware address from the interface */ | |
400 | dl_p = link_from_ifnet(ifp); | |
401 | if (dl_p == NULL) { | |
402 | printf("bootp: can't get link address\n"); | |
403 | return (ENXIO); | |
404 | } | |
405 | ||
406 | printf("bootp: h/w addr "); | |
407 | link_print(dl_p); | |
408 | ||
409 | hwaddr = link_address(dl_p); | |
410 | hwlen = dl_p->sdl_alen; | |
411 | switch (dl_p->sdl_type) { | |
412 | case IFT_ETHER: | |
413 | hwtype = ARPHRD_ETHER; | |
414 | break; | |
415 | default: | |
416 | printf("bootp: hardware type %d not supported\n", | |
417 | dl_p->sdl_type); | |
418 | panic("bootp: hardware type not supported"); | |
419 | break; | |
420 | } | |
421 | ||
422 | /* set transaction id and remember the start time */ | |
423 | microtime(&start_time); | |
424 | current_time = start_time; | |
425 | xid = random(); | |
426 | ||
427 | /* make a request/reply packet */ | |
428 | request = (struct bootp_packet *)kalloc(sizeof(*request)); | |
429 | make_bootp_request(request, hwaddr, hwtype, hwlen); | |
9bccf70c | 430 | reply = (struct bootp *)kalloc(reply_size); |
1c79356b A |
431 | iaddr_p->s_addr = 0; |
432 | printf("bootp: sending request"); | |
433 | for (retry = 0; retry < max_try; retry++) { | |
434 | int gather_count = 0; | |
435 | int last_rating = 0; | |
436 | ||
437 | /* Send the request */ | |
438 | printf("."); | |
439 | request->bp_bootp.bp_secs = htons((u_short)(current_time.tv_sec | |
440 | - start_time.tv_sec)); | |
441 | request->bp_bootp.bp_xid = htonl(xid); | |
442 | error = send_bootp_request(ifp, so, request); | |
443 | if (error) | |
444 | goto cleanup; | |
445 | ||
446 | timeflag = so; | |
447 | wait_ticks += random_range(-RAND_TICKS, RAND_TICKS); | |
448 | dprintf(("bootp: waiting %d ticks\n", wait_ticks)); | |
9bccf70c | 449 | timeout((timeout_fcn_t)bootp_timeout, &timeflag, wait_ticks); |
1c79356b A |
450 | |
451 | while (TRUE) { | |
9bccf70c A |
452 | int n = 0; |
453 | ||
454 | error = receive_packet(so, (caddr_t)reply, reply_size, &n); | |
1c79356b A |
455 | if (error == 0) { |
456 | dprintf(("\nbootp: received packet\n")); | |
457 | if (ntohl(reply->bp_xid) == xid | |
458 | && reply->bp_yiaddr.s_addr | |
459 | && bcmp(reply->bp_chaddr, hwaddr, hwlen) == 0) { | |
9bccf70c A |
460 | int rating; |
461 | dhcpol_t options; | |
462 | ||
1c79356b | 463 | #ifdef BOOTP_DEBUG |
9bccf70c | 464 | print_reply_short(reply, n); |
55e303ae | 465 | #endif /* BOOTP_DEBUG */ |
9bccf70c A |
466 | (void)dhcpol_parse_packet(&options, (struct dhcp *)reply, |
467 | n, NULL); | |
468 | rating = rate_packet(reply, n, &options); | |
469 | if (rating > last_rating) { | |
470 | struct in_addr * ip; | |
471 | int len; | |
472 | ||
473 | *iaddr_p = reply->bp_yiaddr; | |
474 | ip = (struct in_addr *) | |
475 | dhcpol_find(&options, | |
476 | dhcptag_subnet_mask_e, &len, NULL); | |
477 | if (ip) { | |
478 | *netmask_p = *ip; | |
479 | } | |
480 | ip = (struct in_addr *) | |
481 | dhcpol_find(&options, dhcptag_router_e, &len, NULL); | |
482 | if (ip) { | |
483 | *router_p = *ip; | |
484 | } | |
485 | printf("%sbootp: got " | |
486 | "response from %s (" IP_FORMAT ")\n", | |
487 | last_rating == 0 ? "\n" : "", | |
488 | reply->bp_sname, | |
489 | IP_LIST(&reply->bp_siaddr)); | |
490 | } | |
491 | dhcpol_free(&options); | |
1c79356b | 492 | if (rating >= GOOD_RATING) { |
9bccf70c A |
493 | untimeout((timeout_fcn_t)bootp_timeout, &timeflag); |
494 | goto done; | |
1c79356b A |
495 | } |
496 | if (gather_count == 0) { | |
9bccf70c | 497 | untimeout((timeout_fcn_t)bootp_timeout, &timeflag); |
1c79356b | 498 | timeflag = so; |
9bccf70c | 499 | timeout((timeout_fcn_t)bootp_timeout, &timeflag, |
1c79356b A |
500 | hz * GATHER_TIME_SECS); |
501 | } | |
502 | gather_count++; | |
503 | } | |
504 | else { | |
505 | dprintf(("bootp: packet ignored\n")); | |
506 | } | |
507 | } | |
508 | else if ((error != EWOULDBLOCK)) { | |
509 | break; | |
510 | } | |
511 | else if (timeflag == NULL) { /* timed out */ | |
512 | if (gather_count) { | |
513 | dprintf(("bootp: gathering time has expired")); | |
9bccf70c | 514 | goto done; /* we have a packet */ |
1c79356b A |
515 | } |
516 | break; /* retry */ | |
517 | } | |
91447636 A |
518 | else { |
519 | socket_lock(so, 1); | |
520 | error = sbwait(&so->so_rcv); | |
521 | socket_unlock(so, 1); | |
522 | } | |
1c79356b A |
523 | } |
524 | if (error && (error != EWOULDBLOCK)) { | |
525 | dprintf(("bootp: failed to receive packets: %d\n", error)); | |
9bccf70c | 526 | untimeout((timeout_fcn_t)bootp_timeout, &timeflag); |
1c79356b A |
527 | goto cleanup; |
528 | } | |
529 | wait_ticks *= 2; | |
530 | if (wait_ticks > (MAX_WAIT_SECS * hz)) | |
531 | wait_ticks = MAX_WAIT_SECS * hz; | |
532 | xid++; | |
533 | microtime(¤t_time); | |
534 | } | |
535 | error = ETIMEDOUT; | |
536 | goto cleanup; | |
537 | ||
9bccf70c | 538 | done: |
1c79356b | 539 | error = 0; |
1c79356b A |
540 | |
541 | cleanup: | |
542 | if (request) | |
91447636 | 543 | kfree(request, sizeof (*request)); |
1c79356b | 544 | if (reply) |
91447636 | 545 | kfree(reply, reply_size); |
1c79356b A |
546 | return (error); |
547 | } | |
548 | ||
549 | /* | |
550 | * Routine: bootp | |
551 | * Function: | |
552 | * Use the BOOTP protocol to resolve what our IP address should be | |
553 | * on a particular interface. | |
554 | */ | |
555 | int bootp(struct ifnet * ifp, struct in_addr * iaddr_p, int max_try, | |
556 | struct in_addr * netmask_p, struct in_addr * router_p, | |
557 | struct proc * procp) | |
558 | { | |
559 | boolean_t addr_set = FALSE; | |
560 | struct ifreq ifr; | |
561 | int error; | |
562 | struct socket * so = NULL; | |
563 | ||
564 | /* get a socket */ | |
565 | error = socreate(AF_INET, &so, SOCK_DGRAM, 0); | |
566 | if (error) { | |
567 | dprintf(("bootp: socreate failed %d\n", error)); | |
568 | return (error); | |
569 | } | |
570 | ||
571 | /* assign the all-zeroes address */ | |
572 | bzero(&ifr, sizeof(ifr)); | |
573 | sprintf(ifr.ifr_name, "%s%d", ifp->if_name, ifp->if_unit); | |
9bccf70c | 574 | *((struct sockaddr_in *)&ifr.ifr_addr) = blank_sin(); |
1c79356b A |
575 | error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ifr, procp); |
576 | if (error) { | |
577 | dprintf(("bootp: SIOCSIFADDR all-zeroes IP failed: %d\n", | |
578 | error)); | |
579 | goto cleanup; | |
580 | } | |
581 | dprintf(("bootp: all-zeroes IP address assigned\n")); | |
582 | addr_set = TRUE; | |
583 | ||
584 | { /* bind the socket */ | |
585 | struct sockaddr_in * sin; | |
586 | ||
0b4e3aa0 A |
587 | sin = _MALLOC(sizeof(struct sockaddr_in), M_IFADDR, M_WAIT); |
588 | if (sin == NULL) { | |
589 | error = ENOMEM; | |
590 | goto cleanup; | |
591 | } | |
1c79356b A |
592 | sin->sin_len = sizeof(struct sockaddr_in); |
593 | sin->sin_family = AF_INET; | |
594 | sin->sin_port = htons(IPPORT_BOOTPC); | |
595 | sin->sin_addr.s_addr = INADDR_ANY; | |
596 | error = sobind(so, (struct sockaddr *) sin); | |
597 | ||
598 | FREE(sin, M_IFADDR); | |
599 | if (error) { | |
600 | dprintf(("bootp: sobind failed, %d\n", error)); | |
601 | goto cleanup; | |
602 | } | |
91447636 | 603 | socket_lock(so, 1); |
1c79356b | 604 | so->so_state |= SS_NBIO; |
91447636 | 605 | socket_unlock(so, 1); |
1c79356b A |
606 | } |
607 | /* do the protocol */ | |
608 | error = bootp_loop(so, ifp, max_try, iaddr_p, netmask_p, router_p); | |
609 | ||
610 | cleanup: | |
611 | if (so) { | |
612 | if (addr_set) { | |
613 | (void) ifioctl(so, SIOCDIFADDR, (caddr_t) &ifr, procp); | |
614 | } | |
615 | soclose(so); | |
616 | } | |
617 | return (error); | |
618 | } |