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