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