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