nfs_bootdhcp.c revision 1.1 1 1.1 gwr /* $NetBSD: nfs_bootdhcp.c,v 1.1 1997/08/29 16:10:31 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*-
4 1.1 gwr * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gwr * by Adam Glass and Gordon W. Ross.
9 1.1 gwr *
10 1.1 gwr * Redistribution and use in source and binary forms, with or without
11 1.1 gwr * modification, are permitted provided that the following conditions
12 1.1 gwr * are met:
13 1.1 gwr * 1. Redistributions of source code must retain the above copyright
14 1.1 gwr * notice, this list of conditions and the following disclaimer.
15 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gwr * notice, this list of conditions and the following disclaimer in the
17 1.1 gwr * documentation and/or other materials provided with the distribution.
18 1.1 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 gwr * must display the following acknowledgement:
20 1.1 gwr * This product includes software developed by the NetBSD
21 1.1 gwr * Foundation, Inc. and its contributors.
22 1.1 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 gwr * contributors may be used to endorse or promote products derived
24 1.1 gwr * from this software without specific prior written permission.
25 1.1 gwr *
26 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 gwr */
38 1.1 gwr
39 1.1 gwr /*
40 1.1 gwr * Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
41 1.1 gwr *
42 1.1 gwr * History:
43 1.1 gwr *
44 1.1 gwr * Tor Egge developed the initial version of this code based on
45 1.1 gwr * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
46 1.1 gwr * submitted that work to NetBSD as bugreport "kern/2351" on
47 1.1 gwr * 29 Apr 1996.
48 1.1 gwr *
49 1.1 gwr * Gordon Ross reorganized Tor's version into this form and
50 1.1 gwr * integrated it into the NetBSD sources during Aug 1997.
51 1.1 gwr */
52 1.1 gwr
53 1.1 gwr #include <sys/param.h>
54 1.1 gwr #include <sys/systm.h>
55 1.1 gwr #include <sys/kernel.h>
56 1.1 gwr #include <sys/conf.h>
57 1.1 gwr #include <sys/device.h>
58 1.1 gwr #include <sys/ioctl.h>
59 1.1 gwr #include <sys/proc.h>
60 1.1 gwr #include <sys/mount.h>
61 1.1 gwr #include <sys/mbuf.h>
62 1.1 gwr #include <sys/reboot.h>
63 1.1 gwr #include <sys/socket.h>
64 1.1 gwr #include <sys/socketvar.h>
65 1.1 gwr
66 1.1 gwr #include <net/if.h>
67 1.1 gwr #include <net/if_arp.h> /* ARPHRD_ETHER, etc. */
68 1.1 gwr #include <net/if_dl.h>
69 1.1 gwr #include <net/if_ether.h>
70 1.1 gwr #include <net/route.h>
71 1.1 gwr
72 1.1 gwr #include <netinet/in.h>
73 1.1 gwr #include <netinet/if_inarp.h>
74 1.1 gwr
75 1.1 gwr #include <nfs/nfsproto.h>
76 1.1 gwr /* #include <nfs/nfs.h> */
77 1.1 gwr #include <nfs/nfsdiskless.h>
78 1.1 gwr
79 1.1 gwr /*
80 1.1 gwr * There are two implementations of NFS diskless boot.
81 1.1 gwr * This implementation uses BOOTP (RFC951, RFC1048), and
82 1.1 gwr * the other uses Sun RPC/bootparams (nfs_bootparam.c).
83 1.1 gwr *
84 1.1 gwr * This method gets everything it needs with one BOOTP
85 1.1 gwr * request and reply. Note that this actually uses only
86 1.1 gwr * the old BOOTP functionality subset of DHCP. It is not
87 1.1 gwr * clear that DHCP provides any advantage over BOOTP for
88 1.1 gwr * diskless boot. DHCP allows the server to assign an IP
89 1.1 gwr * address without any a-priori knowledge of the client,
90 1.1 gwr * but we require that the server has a-priori knowledge
91 1.1 gwr * of the client so it can export our (unique) NFS root.
92 1.1 gwr * Given that the server needs a-priori knowledge about
93 1.1 gwr * the client anyway, it might as well assign a fixed IP
94 1.1 gwr * address for the client and support BOOTP.
95 1.1 gwr *
96 1.1 gwr * On the other hand, disk-FULL clients may use DHCP, but
97 1.1 gwr * in that case the DHCP client should be user-mode code,
98 1.1 gwr * and has no bearing on the code below. -gwr
99 1.1 gwr */
100 1.1 gwr
101 1.1 gwr /* Begin stuff from bootp.h */
102 1.1 gwr /* Definitions from RFC951 */
103 1.1 gwr #define BP_CHADDR_LEN 16
104 1.1 gwr #define BP_SNAME_LEN 64
105 1.1 gwr #define BP_FILE_LEN 128
106 1.1 gwr #define BP_VEND_LEN 64
107 1.1 gwr struct bootp {
108 1.1 gwr u_int8_t bp_op; /* packet opcode type */
109 1.1 gwr u_int8_t bp_htype; /* hardware addr type */
110 1.1 gwr u_int8_t bp_hlen; /* hardware addr length */
111 1.1 gwr u_int8_t bp_hops; /* gateway hops */
112 1.1 gwr u_int32_t bp_xid; /* transaction ID */
113 1.1 gwr u_int16_t bp_secs; /* seconds since boot began */
114 1.1 gwr u_int16_t bp_flags; /* RFC1532 broadcast, etc. */
115 1.1 gwr struct in_addr bp_ciaddr; /* client IP address */
116 1.1 gwr struct in_addr bp_yiaddr; /* 'your' IP address */
117 1.1 gwr struct in_addr bp_siaddr; /* server IP address */
118 1.1 gwr struct in_addr bp_giaddr; /* gateway IP address */
119 1.1 gwr u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
120 1.1 gwr char bp_sname[BP_SNAME_LEN]; /* server host name */
121 1.1 gwr char bp_file[BP_FILE_LEN]; /* boot file name */
122 1.1 gwr u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */
123 1.1 gwr /*
124 1.1 gwr * Note that BOOTP packets are allowed to be longer
125 1.1 gwr * (see RFC 1532 sect. 2.1) and common practice is to
126 1.1 gwr * allow the option data in bp_vend to extend into the
127 1.1 gwr * additional space provided in longer packets.
128 1.1 gwr */
129 1.1 gwr };
130 1.1 gwr
131 1.1 gwr #define IPPORT_BOOTPS 67
132 1.1 gwr #define IPPORT_BOOTPC 68
133 1.1 gwr
134 1.1 gwr #define BOOTREQUEST 1
135 1.1 gwr #define BOOTREPLY 2
136 1.1 gwr
137 1.1 gwr /*
138 1.1 gwr * Is this available from the sockaddr_dl somehow?
139 1.1 gwr * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
140 1.1 gwr * The interface has ->if_type but not the ARP fmt.
141 1.1 gwr */
142 1.1 gwr #define HTYPE_ETHERNET 1
143 1.1 gwr
144 1.1 gwr /*
145 1.1 gwr * Vendor magic cookie (v_magic) for RFC1048
146 1.1 gwr */
147 1.1 gwr static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
148 1.1 gwr
149 1.1 gwr /*
150 1.1 gwr * Tag values used to specify what information is being supplied in
151 1.1 gwr * the vendor (options) data area of the packet.
152 1.1 gwr */
153 1.1 gwr /* RFC 1048 */
154 1.1 gwr #define TAG_END ((unsigned char) 255)
155 1.1 gwr #define TAG_PAD ((unsigned char) 0)
156 1.1 gwr #define TAG_SUBNET_MASK ((unsigned char) 1)
157 1.1 gwr #define TAG_TIME_OFFSET ((unsigned char) 2)
158 1.1 gwr #define TAG_GATEWAY ((unsigned char) 3)
159 1.1 gwr #define TAG_TIME_SERVER ((unsigned char) 4)
160 1.1 gwr #define TAG_NAME_SERVER ((unsigned char) 5)
161 1.1 gwr #define TAG_DOMAIN_SERVER ((unsigned char) 6)
162 1.1 gwr #define TAG_LOG_SERVER ((unsigned char) 7)
163 1.1 gwr #define TAG_COOKIE_SERVER ((unsigned char) 8)
164 1.1 gwr #define TAG_LPR_SERVER ((unsigned char) 9)
165 1.1 gwr #define TAG_IMPRESS_SERVER ((unsigned char) 10)
166 1.1 gwr #define TAG_RLP_SERVER ((unsigned char) 11)
167 1.1 gwr #define TAG_HOST_NAME ((unsigned char) 12)
168 1.1 gwr #define TAG_BOOT_SIZE ((unsigned char) 13)
169 1.1 gwr /* RFC 1395 */
170 1.1 gwr #define TAG_DUMP_FILE ((unsigned char) 14)
171 1.1 gwr #define TAG_DOMAIN_NAME ((unsigned char) 15)
172 1.1 gwr #define TAG_SWAP_SERVER ((unsigned char) 16)
173 1.1 gwr #define TAG_ROOT_PATH ((unsigned char) 17)
174 1.1 gwr /* End of stuff from bootp.h */
175 1.1 gwr
176 1.1 gwr
177 1.1 gwr /*
178 1.1 gwr * The "extended" size is somewhat arbitrary, but is
179 1.1 gwr * constrained by the maximum message size specified
180 1.1 gwr * by RFC1533 (567 total). This value increases the
181 1.1 gwr * space for options from 64 bytes to 256 bytes.
182 1.1 gwr */
183 1.1 gwr #define BOOTP_SIZE_MAX (sizeof(struct bootp)+192)
184 1.1 gwr #define BOOTP_SIZE_MIN (sizeof(struct bootp))
185 1.1 gwr
186 1.1 gwr /*
187 1.1 gwr * What is the longest we will wait before re-sending a request?
188 1.1 gwr * Note this is also the frequency of "BOOTP timeout" messages.
189 1.1 gwr * The re-send loop counts up linearly to this maximum, so the
190 1.1 gwr * first complaint will happen after (1+2+3+4+5)=15 seconds.
191 1.1 gwr */
192 1.1 gwr #define MAX_RESEND_DELAY 5 /* seconds */
193 1.1 gwr #define TOTAL_TIMEOUT 30 /* seconds */
194 1.1 gwr
195 1.1 gwr /* Convenience macro */
196 1.1 gwr #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
197 1.1 gwr
198 1.1 gwr static int bootpc_call __P((struct socket *, struct ifnet *,
199 1.1 gwr struct nfs_diskless *, struct proc *));
200 1.1 gwr
201 1.1 gwr
202 1.1 gwr /* #define DEBUG XXX */
203 1.1 gwr
204 1.1 gwr #ifdef DEBUG
205 1.1 gwr #define DPRINT(s) printf("nfs_boot: %s\n", s)
206 1.1 gwr #else
207 1.1 gwr #define DPRINT(s) (void)0
208 1.1 gwr #endif
209 1.1 gwr
210 1.1 gwr
211 1.1 gwr /*
212 1.1 gwr * Get our boot parameters using BOOTP.
213 1.1 gwr */
214 1.1 gwr int
215 1.1 gwr nfs_bootdhcp(ifp, nd, procp)
216 1.1 gwr struct ifnet *ifp;
217 1.1 gwr struct nfs_diskless *nd;
218 1.1 gwr struct proc *procp;
219 1.1 gwr {
220 1.1 gwr struct ifaliasreq iareq;
221 1.1 gwr struct socket *so;
222 1.1 gwr struct sockaddr_in *sin;
223 1.1 gwr int error;
224 1.1 gwr
225 1.1 gwr /*
226 1.1 gwr * Get a socket to use for various things in here.
227 1.1 gwr * After this, use "goto out" to cleanup and return.
228 1.1 gwr */
229 1.1 gwr error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
230 1.1 gwr if (error) {
231 1.1 gwr printf("nfs_boot: socreate, error=%d\n", error);
232 1.1 gwr return (error);
233 1.1 gwr }
234 1.1 gwr
235 1.1 gwr /*
236 1.1 gwr * Do enough of ifconfig(8) so that the chosen interface
237 1.1 gwr * can talk to the servers. Use address zero for now.
238 1.1 gwr */
239 1.1 gwr bzero(&iareq, sizeof(iareq));
240 1.1 gwr bcopy(ifp->if_xname, iareq.ifra_name, IFNAMSIZ);
241 1.1 gwr /* Set the I/F address */
242 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_addr;
243 1.1 gwr sin->sin_len = sizeof(*sin);
244 1.1 gwr sin->sin_family = AF_INET;
245 1.1 gwr sin->sin_addr.s_addr = INADDR_ANY;
246 1.1 gwr /* Leave subnetmask unspecified (len=0) */
247 1.1 gwr /* Set the broadcast addr. */
248 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
249 1.1 gwr sin->sin_len = sizeof(*sin);
250 1.1 gwr sin->sin_family = AF_INET;
251 1.1 gwr sin->sin_addr.s_addr = INADDR_BROADCAST;
252 1.1 gwr error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
253 1.1 gwr if (error) {
254 1.1 gwr printf("nfs_boot: set ifaddr zero, error=%d\n", error);
255 1.1 gwr goto out;
256 1.1 gwr }
257 1.1 gwr
258 1.1 gwr /* This function call does the real send/recv work. */
259 1.1 gwr error = bootpc_call(so, ifp, nd, procp);
260 1.1 gwr /* Get rid of the temporary (zero) IP address. */
261 1.1 gwr (void) ifioctl(so, SIOCDIFADDR, (caddr_t)&iareq, procp);
262 1.1 gwr /* NOW we can test the error from bootpc_call. */
263 1.1 gwr if (error)
264 1.1 gwr goto out;
265 1.1 gwr
266 1.1 gwr /*
267 1.1 gwr * Do ifconfig with our real IP address and mask.
268 1.1 gwr */
269 1.1 gwr /* I/F address */
270 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_addr;
271 1.1 gwr sin->sin_addr = nd->nd_myip;
272 1.1 gwr /* subnetmask */
273 1.1 gwr if (nd->nd_mask.s_addr) {
274 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_mask;
275 1.1 gwr sin->sin_len = sizeof(*sin);
276 1.1 gwr sin->sin_family = AF_INET;
277 1.1 gwr sin->sin_addr = nd->nd_mask;
278 1.1 gwr }
279 1.1 gwr /* Let ifioctl() default the broadcast address. */
280 1.1 gwr sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
281 1.1 gwr sin->sin_len = 0;
282 1.1 gwr sin->sin_family = 0;
283 1.1 gwr sin->sin_addr.s_addr = 0;
284 1.1 gwr error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
285 1.1 gwr if (error) {
286 1.1 gwr printf("nfs_boot: set ifaddr real, error=%d\n", error);
287 1.1 gwr goto out;
288 1.1 gwr }
289 1.1 gwr
290 1.1 gwr out:
291 1.1 gwr soclose(so);
292 1.1 gwr return (error);
293 1.1 gwr }
294 1.1 gwr
295 1.1 gwr static int
296 1.1 gwr bootpc_call(so, ifp, nd, procp)
297 1.1 gwr struct socket *so;
298 1.1 gwr struct ifnet *ifp;
299 1.1 gwr struct nfs_diskless *nd;
300 1.1 gwr struct proc *procp;
301 1.1 gwr {
302 1.1 gwr static u_int32_t xid = ~0xFF;
303 1.1 gwr struct uio uio;
304 1.1 gwr struct iovec iov;
305 1.1 gwr struct in_addr netmask;
306 1.1 gwr struct in_addr gateway;
307 1.1 gwr char *myname; /* my hostname */
308 1.1 gwr char *mydomain; /* my domainname */
309 1.1 gwr char *rootpath;
310 1.1 gwr int mynamelen;
311 1.1 gwr int mydomainlen;
312 1.1 gwr int rootpathlen;
313 1.1 gwr struct bootp *bootp; /* request and reply */
314 1.1 gwr struct mbuf *m, *nam;
315 1.1 gwr struct sockaddr_in *sin;
316 1.1 gwr int error, rcvflg, timo, secs, waited;
317 1.1 gwr int sendlen, recvlen;
318 1.1 gwr u_int tag, len;
319 1.1 gwr u_char *p, *limit;
320 1.1 gwr u_char *haddr;
321 1.1 gwr u_char hafmt, halen;
322 1.1 gwr
323 1.1 gwr /*
324 1.1 gwr * Initialize to NULL anything that will hold an allocation,
325 1.1 gwr * and free each at the end if not null.
326 1.1 gwr */
327 1.1 gwr bootp = NULL;
328 1.1 gwr m = nam = NULL;
329 1.1 gwr
330 1.1 gwr /* Record our H/W (Ethernet) address. */
331 1.1 gwr { struct sockaddr_dl *sdl = ifp->if_sadl;
332 1.1 gwr hafmt = HTYPE_ETHERNET; /* XXX: sdl->sdl_type? */
333 1.1 gwr halen = sdl->sdl_alen;
334 1.1 gwr haddr = (unsigned char *)LLADDR(sdl);
335 1.1 gwr }
336 1.1 gwr
337 1.1 gwr /*
338 1.1 gwr * Skip the route table when sending on this socket.
339 1.1 gwr * If this is not done, ip_output finds the loopback
340 1.1 gwr * interface (why?) and then fails because broadcast
341 1.1 gwr * is not supported on that interface...
342 1.1 gwr */
343 1.1 gwr { int32_t *opt;
344 1.1 gwr m = m_get(M_WAIT, MT_SOOPTS);
345 1.1 gwr opt = mtod(m, int32_t *);
346 1.1 gwr m->m_len = sizeof(*opt);
347 1.1 gwr *opt = 1;
348 1.1 gwr error = sosetopt(so, SOL_SOCKET, SO_DONTROUTE, m);
349 1.1 gwr m = NULL; /* was consumed */
350 1.1 gwr }
351 1.1 gwr if (error) {
352 1.1 gwr DPRINT("SO_DONTROUTE");
353 1.1 gwr goto out;
354 1.1 gwr }
355 1.1 gwr
356 1.1 gwr /* Enable broadcast. */
357 1.1 gwr { int32_t *opt;
358 1.1 gwr m = m_get(M_WAIT, MT_SOOPTS);
359 1.1 gwr opt = mtod(m, int32_t *);
360 1.1 gwr m->m_len = sizeof(*opt);
361 1.1 gwr *opt = 1;
362 1.1 gwr error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m);
363 1.1 gwr m = NULL; /* was consumed */
364 1.1 gwr }
365 1.1 gwr if (error) {
366 1.1 gwr DPRINT("SO_BROADCAST");
367 1.1 gwr goto out;
368 1.1 gwr }
369 1.1 gwr
370 1.1 gwr /* Set the receive timeout for the socket. */
371 1.1 gwr { struct timeval *tv;
372 1.1 gwr m = m_get(M_WAIT, MT_SOOPTS);
373 1.1 gwr tv = mtod(m, struct timeval *);
374 1.1 gwr m->m_len = sizeof(*tv);
375 1.1 gwr tv->tv_sec = 1;
376 1.1 gwr tv->tv_usec = 0;
377 1.1 gwr error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m);
378 1.1 gwr m = NULL; /* was consumed */
379 1.1 gwr }
380 1.1 gwr if (error) {
381 1.1 gwr DPRINT("SO_RCVTIMEO");
382 1.1 gwr goto out;
383 1.1 gwr }
384 1.1 gwr
385 1.1 gwr /*
386 1.1 gwr * Bind the local endpoint to a bootp client port.
387 1.1 gwr */
388 1.1 gwr m = m_getclr(M_WAIT, MT_SONAME);
389 1.1 gwr sin = mtod(m, struct sockaddr_in *);
390 1.1 gwr sin->sin_len = m->m_len = sizeof(*sin);
391 1.1 gwr sin->sin_family = AF_INET;
392 1.1 gwr sin->sin_addr.s_addr = INADDR_ANY;
393 1.1 gwr sin->sin_port = htons(IPPORT_BOOTPC);
394 1.1 gwr error = sobind(so, m);
395 1.1 gwr m_freem(m);
396 1.1 gwr if (error) {
397 1.1 gwr DPRINT("bind failed\n");
398 1.1 gwr goto out;
399 1.1 gwr }
400 1.1 gwr
401 1.1 gwr /*
402 1.1 gwr * Setup socket address for the server.
403 1.1 gwr */
404 1.1 gwr nam = m_get(M_WAIT, MT_SONAME);
405 1.1 gwr sin = mtod(nam, struct sockaddr_in *);
406 1.1 gwr sin->sin_len = nam->m_len = sizeof(*sin);
407 1.1 gwr sin->sin_family = AF_INET;
408 1.1 gwr sin->sin_addr.s_addr = INADDR_BROADCAST;
409 1.1 gwr sin->sin_port = htons(IPPORT_BOOTPS);
410 1.1 gwr
411 1.1 gwr /*
412 1.1 gwr * Allocate buffer used for request/reply
413 1.1 gwr */
414 1.1 gwr bootp = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
415 1.1 gwr if (bootp == NULL)
416 1.1 gwr panic("nfs_boot: malloc buf");
417 1.1 gwr
418 1.1 gwr /*
419 1.1 gwr * Send it, repeatedly, until a reply is received,
420 1.1 gwr * but delay each re-send by an increasing amount.
421 1.1 gwr * If the delay hits the maximum, start complaining.
422 1.1 gwr * Try extended-length request first, and if that
423 1.1 gwr * results in total timeout, start over with the
424 1.1 gwr * standard-length BOOTP request.
425 1.1 gwr */
426 1.1 gwr sendlen = BOOTP_SIZE_MAX;
427 1.1 gwr waited = timo = 0;
428 1.1 gwr send_again:
429 1.1 gwr waited += timo;
430 1.1 gwr if (waited >= TOTAL_TIMEOUT) {
431 1.1 gwr if (sendlen > BOOTP_SIZE_MIN) {
432 1.1 gwr sendlen = BOOTP_SIZE_MIN;
433 1.1 gwr waited = timo = 0;
434 1.1 gwr printf("nfs_boot: trying short requests\n");
435 1.1 gwr goto send_again;
436 1.1 gwr }
437 1.1 gwr error = ETIMEDOUT;
438 1.1 gwr goto out;
439 1.1 gwr }
440 1.1 gwr /* Determine new timeout. */
441 1.1 gwr if (timo < MAX_RESEND_DELAY)
442 1.1 gwr timo++;
443 1.1 gwr else
444 1.1 gwr printf("nfs_boot: BOOTP timeout...\n");
445 1.1 gwr
446 1.1 gwr /*
447 1.1 gwr * Build the BOOTP reqest message. Do it every time
448 1.1 gwr * we send a new request because we reuse the buffer.
449 1.1 gwr * Also, use a new transaction ID for each request.
450 1.1 gwr * Note: xid is host order! (opaque to server)
451 1.1 gwr */
452 1.1 gwr bzero((caddr_t)bootp, BOOTP_SIZE_MAX);
453 1.1 gwr bootp->bp_op = BOOTREQUEST;
454 1.1 gwr bootp->bp_htype = hafmt;
455 1.1 gwr bootp->bp_hlen = halen; /* Hardware address length */
456 1.1 gwr bootp->bp_xid = ++xid;
457 1.1 gwr bootp->bp_secs = htons(waited);
458 1.1 gwr bcopy(haddr, bootp->bp_chaddr, halen);
459 1.1 gwr /* Fill-in the vendor data. */
460 1.1 gwr bcopy(vm_rfc1048, bootp->bp_vend, 4);
461 1.1 gwr bootp->bp_vend[4] = TAG_END;
462 1.1 gwr
463 1.1 gwr /*
464 1.1 gwr * The BOOTP request is complete. Send it.
465 1.1 gwr */
466 1.1 gwr iov.iov_base = (caddr_t) bootp;
467 1.1 gwr iov.iov_len = sendlen;
468 1.1 gwr uio.uio_iov = &iov;
469 1.1 gwr uio.uio_iovcnt = 1;
470 1.1 gwr uio.uio_segflg = UIO_SYSSPACE;
471 1.1 gwr uio.uio_rw = UIO_WRITE;
472 1.1 gwr uio.uio_offset = 0;
473 1.1 gwr uio.uio_resid = sendlen;
474 1.1 gwr uio.uio_procp = procp;
475 1.1 gwr error = sosend(so, nam, &uio, NULL, NULL, 0);
476 1.1 gwr if (error) {
477 1.1 gwr printf("nfs_boot: sosend: %d\n", error);
478 1.1 gwr goto out;
479 1.1 gwr }
480 1.1 gwr
481 1.1 gwr /*
482 1.1 gwr * Wait for up to timo seconds for a reply.
483 1.1 gwr * The socket receive timeout was set to 1 second.
484 1.1 gwr */
485 1.1 gwr secs = timo;
486 1.1 gwr for (;;) {
487 1.1 gwr iov.iov_base = (caddr_t) bootp;
488 1.1 gwr iov.iov_len = BOOTP_SIZE_MAX;
489 1.1 gwr uio.uio_iov = &iov;
490 1.1 gwr uio.uio_iovcnt = 1;
491 1.1 gwr uio.uio_segflg = UIO_SYSSPACE;
492 1.1 gwr uio.uio_rw = UIO_READ;
493 1.1 gwr uio.uio_offset = 0;
494 1.1 gwr uio.uio_resid = BOOTP_SIZE_MAX;
495 1.1 gwr uio.uio_procp = procp;
496 1.1 gwr
497 1.1 gwr rcvflg = 0;
498 1.1 gwr error = soreceive(so, NULL, &uio, NULL, NULL, &rcvflg);
499 1.1 gwr if (error == EWOULDBLOCK) {
500 1.1 gwr if (--secs <= 0)
501 1.1 gwr goto send_again;
502 1.1 gwr continue;
503 1.1 gwr }
504 1.1 gwr if (error)
505 1.1 gwr goto out;
506 1.1 gwr recvlen = BOOTP_SIZE_MAX - uio.uio_resid;
507 1.1 gwr
508 1.1 gwr /*
509 1.1 gwr * Is this a valid reply?
510 1.1 gwr */
511 1.1 gwr if (recvlen < BOOTP_SIZE_MIN) {
512 1.1 gwr DPRINT("short packet");
513 1.1 gwr continue;
514 1.1 gwr }
515 1.1 gwr if (bootp->bp_op != BOOTREPLY) {
516 1.1 gwr DPRINT("not reply");
517 1.1 gwr continue;
518 1.1 gwr }
519 1.1 gwr if (bootp->bp_hlen != halen) {
520 1.1 gwr DPRINT("bad hwa_len");
521 1.1 gwr continue;
522 1.1 gwr }
523 1.1 gwr if (bcmp(bootp->bp_chaddr, haddr, halen)) {
524 1.1 gwr DPRINT("wrong hwaddr");
525 1.1 gwr continue;
526 1.1 gwr }
527 1.1 gwr if (bootp->bp_xid != xid) {
528 1.1 gwr DPRINT("wrong xid");
529 1.1 gwr continue;
530 1.1 gwr }
531 1.1 gwr
532 1.1 gwr /*
533 1.1 gwr * OK, we have a valid reply.
534 1.1 gwr * Decode the vendor data.
535 1.1 gwr */
536 1.1 gwr if (bcmp(bootp->bp_vend, vm_rfc1048, 4)) {
537 1.1 gwr printf("nfs_boot: reply missing options\n");
538 1.1 gwr continue;
539 1.1 gwr }
540 1.1 gwr /* Default these to "unspecified". */
541 1.1 gwr netmask.s_addr = 0;
542 1.1 gwr gateway.s_addr = 0;
543 1.1 gwr mydomain = myname = rootpath = NULL;
544 1.1 gwr mydomainlen = mynamelen = rootpathlen = 0;
545 1.1 gwr p = &bootp->bp_vend[4];
546 1.1 gwr limit = ((char*)bootp) + recvlen;
547 1.1 gwr while (p < limit) {
548 1.1 gwr tag = *p++;
549 1.1 gwr if (tag == TAG_END)
550 1.1 gwr break;
551 1.1 gwr if (tag == TAG_PAD)
552 1.1 gwr continue;
553 1.1 gwr len = *p++;
554 1.1 gwr if (len == 0)
555 1.1 gwr continue;
556 1.1 gwr if ((p + len) > limit) {
557 1.1 gwr printf("nfs_boot: option %d too long\n", tag);
558 1.1 gwr break;
559 1.1 gwr }
560 1.1 gwr switch (tag) {
561 1.1 gwr case TAG_SUBNET_MASK:
562 1.1 gwr bcopy(p, &netmask, 4);
563 1.1 gwr break;
564 1.1 gwr case TAG_GATEWAY:
565 1.1 gwr /* Routers */
566 1.1 gwr bcopy(p, &gateway, 4);
567 1.1 gwr break;
568 1.1 gwr case TAG_HOST_NAME:
569 1.1 gwr if (len >= sizeof(hostname)) {
570 1.1 gwr printf("nfs_boot: host name >=%d bytes",
571 1.1 gwr sizeof(hostname));
572 1.1 gwr break;
573 1.1 gwr }
574 1.1 gwr myname = p;
575 1.1 gwr mynamelen = len;
576 1.1 gwr break;
577 1.1 gwr case TAG_DOMAIN_NAME:
578 1.1 gwr if (len >= sizeof(domainname)) {
579 1.1 gwr printf("nfs_boot: domain name >=%d bytes",
580 1.1 gwr sizeof(domainname));
581 1.1 gwr break;
582 1.1 gwr }
583 1.1 gwr mydomain = p;
584 1.1 gwr mydomainlen = len;
585 1.1 gwr break;
586 1.1 gwr case TAG_ROOT_PATH:
587 1.1 gwr /* Leave some room for the server name. */
588 1.1 gwr if (len >= (MNAMELEN-10)) {
589 1.1 gwr printf("nfs_boot: rootpath >=%d bytes",
590 1.1 gwr (MNAMELEN-10));
591 1.1 gwr break;
592 1.1 gwr }
593 1.1 gwr rootpath = p;
594 1.1 gwr rootpathlen = len;
595 1.1 gwr break;
596 1.1 gwr default:
597 1.1 gwr break;
598 1.1 gwr }
599 1.1 gwr p += len;
600 1.1 gwr }
601 1.1 gwr
602 1.1 gwr if (rootpath == NULL) {
603 1.1 gwr printf("nfs_boot: No root path offered\n");
604 1.1 gwr continue;
605 1.1 gwr }
606 1.1 gwr
607 1.1 gwr /* OK, the reply has everything we need. */
608 1.1 gwr break;
609 1.1 gwr }
610 1.1 gwr rootpath[rootpathlen] = '\0';
611 1.1 gwr
612 1.1 gwr /*
613 1.1 gwr * Store and print network config info.
614 1.1 gwr */
615 1.1 gwr if (myname) {
616 1.1 gwr myname[mynamelen] = '\0';
617 1.1 gwr strncpy(hostname, myname, sizeof(hostname));
618 1.1 gwr hostnamelen = mynamelen;
619 1.1 gwr printf("nfs_boot: my_name=%s\n", hostname);
620 1.1 gwr }
621 1.1 gwr if (mydomain) {
622 1.1 gwr mydomain[mydomainlen] = '\0';
623 1.1 gwr strncpy(domainname, mydomain, sizeof(domainname));
624 1.1 gwr domainnamelen = mydomainlen;
625 1.1 gwr printf("nfs_boot: my_domain=%s\n", domainname);
626 1.1 gwr }
627 1.1 gwr nd->nd_myip = bootp->bp_yiaddr;
628 1.1 gwr if (nd->nd_myip.s_addr)
629 1.1 gwr printf("nfs_boot: my_addr=0x%x\n", INTOHL(nd->nd_myip));
630 1.1 gwr nd->nd_mask = netmask;
631 1.1 gwr if (nd->nd_mask.s_addr)
632 1.1 gwr printf("nfs_boot: my_mask=0x%x\n", INTOHL(nd->nd_mask));
633 1.1 gwr nd->nd_gwip = gateway;
634 1.1 gwr if (nd->nd_gwip.s_addr)
635 1.1 gwr printf("nfs_boot: gateway=0x%x\n", INTOHL(nd->nd_gwip));
636 1.1 gwr
637 1.1 gwr /*
638 1.1 gwr * Store the information about our NFS root mount.
639 1.1 gwr * The caller will print it, so be silent here.
640 1.1 gwr */
641 1.1 gwr {
642 1.1 gwr struct nfs_dlmount *ndm = &nd->nd_root;
643 1.1 gwr
644 1.1 gwr /* Server IP address. */
645 1.1 gwr sin = (struct sockaddr_in *) &ndm->ndm_saddr;
646 1.1 gwr bzero((caddr_t)sin, sizeof(*sin));
647 1.1 gwr sin->sin_len = sizeof(*sin);
648 1.1 gwr sin->sin_family = AF_INET;
649 1.1 gwr sin->sin_addr = bootp->bp_siaddr;
650 1.1 gwr /* Server name. */
651 1.1 gwr strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
652 1.1 gwr len = strlen(ndm->ndm_host);
653 1.1 gwr if ((len + 1 + rootpathlen + 1) > sizeof(ndm->ndm_host)) {
654 1.1 gwr /* Show the server IP address numerically. */
655 1.1 gwr sprintf(ndm->ndm_host, "0x%8x",
656 1.1 gwr INTOHL(bootp->bp_siaddr));
657 1.1 gwr len = strlen(ndm->ndm_host);
658 1.1 gwr }
659 1.1 gwr ndm->ndm_host[len++] = ':';
660 1.1 gwr strncpy(ndm->ndm_host + len,
661 1.1 gwr rootpath, rootpathlen + 1);
662 1.1 gwr }
663 1.1 gwr error = 0;
664 1.1 gwr
665 1.1 gwr out:
666 1.1 gwr if (bootp)
667 1.1 gwr free(bootp, M_DEVBUF);
668 1.1 gwr if (nam)
669 1.1 gwr m_freem(nam);
670 1.1 gwr return (error);
671 1.1 gwr }
672