nfs_boot.c revision 1.69 1 1.69 dyoung /* $NetBSD: nfs_boot.c,v 1.69 2007/08/31 22:02:58 dyoung Exp $ */
2 1.5 cgd
3 1.35 gwr /*-
4 1.35 gwr * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 1.1 glass * All rights reserved.
6 1.1 glass *
7 1.35 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.35 gwr * by Adam Glass and Gordon W. Ross.
9 1.35 gwr *
10 1.1 glass * Redistribution and use in source and binary forms, with or without
11 1.1 glass * modification, are permitted provided that the following conditions
12 1.1 glass * are met:
13 1.1 glass * 1. Redistributions of source code must retain the above copyright
14 1.1 glass * notice, this list of conditions and the following disclaimer.
15 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer in the
17 1.1 glass * documentation and/or other materials provided with the distribution.
18 1.35 gwr * 3. All advertising materials mentioning features or use of this software
19 1.35 gwr * must display the following acknowledgement:
20 1.49 christos * This product includes software developed by the NetBSD
21 1.49 christos * Foundation, Inc. and its contributors.
22 1.35 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.35 gwr * contributors may be used to endorse or promote products derived
24 1.35 gwr * from this software without specific prior written permission.
25 1.1 glass *
26 1.35 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.35 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.35 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.35 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.35 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.35 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.35 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.35 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.35 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.35 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.35 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.35 gwr */
38 1.35 gwr
39 1.35 gwr /*
40 1.35 gwr * Support for NFS diskless booting, specifically getting information
41 1.35 gwr * about where to mount root from, what pathnames, etc.
42 1.1 glass */
43 1.57 lukem
44 1.57 lukem #include <sys/cdefs.h>
45 1.69 dyoung __KERNEL_RCSID(0, "$NetBSD: nfs_boot.c,v 1.69 2007/08/31 22:02:58 dyoung Exp $");
46 1.1 glass
47 1.54 bjh21 #include "opt_nfs.h"
48 1.65 manu #include "opt_tftproot.h"
49 1.42 scottr #include "opt_nfs_boot.h"
50 1.42 scottr
51 1.2 cgd #include <sys/param.h>
52 1.1 glass #include <sys/systm.h>
53 1.1 glass #include <sys/kernel.h>
54 1.30 thorpej #include <sys/device.h>
55 1.1 glass #include <sys/ioctl.h>
56 1.1 glass #include <sys/proc.h>
57 1.1 glass #include <sys/mount.h>
58 1.1 glass #include <sys/mbuf.h>
59 1.16 gwr #include <sys/reboot.h>
60 1.1 glass #include <sys/socket.h>
61 1.16 gwr #include <sys/socketvar.h>
62 1.1 glass
63 1.1 glass #include <net/if.h>
64 1.3 gwr #include <net/route.h>
65 1.31 is #include <net/if_ether.h>
66 1.35 gwr #include <net/if_types.h>
67 1.31 is
68 1.1 glass #include <netinet/in.h>
69 1.31 is #include <netinet/if_inarp.h>
70 1.1 glass
71 1.1 glass #include <nfs/rpcv2.h>
72 1.37 gwr #include <nfs/krpc.h>
73 1.37 gwr #include <nfs/xdr_subs.h>
74 1.37 gwr
75 1.25 fvdl #include <nfs/nfsproto.h>
76 1.44 fvdl #include <nfs/nfs.h>
77 1.44 fvdl #include <nfs/nfsmount.h>
78 1.1 glass #include <nfs/nfsdiskless.h>
79 1.7 paulus
80 1.1 glass /*
81 1.35 gwr * There are two implementations of NFS diskless boot.
82 1.35 gwr * One implementation uses BOOTP (RFC951, RFC1048),
83 1.35 gwr * the other uses Sun RPC/bootparams. See the files:
84 1.35 gwr * nfs_bootp.c: BOOTP (RFC951, RFC1048)
85 1.35 gwr * nfs_bootsun.c: Sun RPC/bootparams
86 1.1 glass */
87 1.41 scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
88 1.40 drochner int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
89 1.40 drochner #endif
90 1.40 drochner #ifdef NFS_BOOT_BOOTPARAM
91 1.40 drochner int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
92 1.40 drochner #endif
93 1.60 cl #ifdef NFS_BOOT_BOOTSTATIC
94 1.60 cl int nfs_boot_bootstatic = 1; /* BOOTSTATIC enabled (default) */
95 1.60 cl #endif
96 1.3 gwr
97 1.3 gwr /* mountd RPC */
98 1.67 dyoung static int md_mount(struct sockaddr_in *mdsin, char *path,
99 1.67 dyoung struct nfs_args *argp, struct lwp *l);
100 1.3 gwr
101 1.68 dyoung static int nfs_boot_delroute(struct rtentry *, void *);
102 1.67 dyoung static void nfs_boot_defrt(struct in_addr *);
103 1.67 dyoung static int nfs_boot_getfh(struct nfs_dlmount *ndm, struct lwp *);
104 1.35 gwr
105 1.35 gwr
106 1.1 glass /*
107 1.3 gwr * Called with an empty nfs_diskless struct to be filled in.
108 1.35 gwr * Find an interface, determine its ip address (etc.) and
109 1.35 gwr * save all the boot parameters in the nfs_diskless struct.
110 1.1 glass */
111 1.6 deraadt int
112 1.62 christos nfs_boot_init(nd, lwp)
113 1.3 gwr struct nfs_diskless *nd;
114 1.62 christos struct lwp *lwp;
115 1.1 glass {
116 1.3 gwr struct ifnet *ifp;
117 1.65 manu int error = 0;
118 1.3 gwr
119 1.3 gwr /*
120 1.35 gwr * Find the network interface.
121 1.3 gwr */
122 1.30 thorpej ifp = ifunit(root_device->dv_xname);
123 1.30 thorpej if (ifp == NULL) {
124 1.35 gwr printf("nfs_boot: '%s' not found\n",
125 1.35 gwr root_device->dv_xname);
126 1.30 thorpej return (ENXIO);
127 1.30 thorpej }
128 1.50 drochner nd->nd_ifp = ifp;
129 1.3 gwr
130 1.40 drochner error = EADDRNOTAVAIL; /* ??? */
131 1.60 cl #if defined(NFS_BOOT_BOOTSTATIC)
132 1.60 cl if (error && nfs_boot_bootstatic) {
133 1.60 cl printf("nfs_boot: trying static\n");
134 1.62 christos error = nfs_bootstatic(nd, lwp);
135 1.60 cl }
136 1.60 cl #endif
137 1.41 scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
138 1.47 drochner if (error && nfs_boot_rfc951) {
139 1.43 cgd #if defined(NFS_BOOT_DHCP)
140 1.43 cgd printf("nfs_boot: trying DHCP/BOOTP\n");
141 1.43 cgd #else
142 1.43 cgd printf("nfs_boot: trying BOOTP\n");
143 1.43 cgd #endif
144 1.62 christos error = nfs_bootdhcp(nd, lwp);
145 1.40 drochner }
146 1.40 drochner #endif
147 1.40 drochner #ifdef NFS_BOOT_BOOTPARAM
148 1.47 drochner if (error && nfs_boot_bootparam) {
149 1.35 gwr printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
150 1.62 christos error = nfs_bootparam(nd, lwp);
151 1.30 thorpej }
152 1.40 drochner #endif
153 1.46 tv if (error)
154 1.46 tv return (error);
155 1.3 gwr
156 1.3 gwr /*
157 1.3 gwr * If the gateway address is set, add a default route.
158 1.3 gwr * (The mountd RPCs may go across a gateway.)
159 1.1 glass */
160 1.35 gwr if (nd->nd_gwip.s_addr)
161 1.35 gwr nfs_boot_defrt(&nd->nd_gwip);
162 1.3 gwr
163 1.65 manu #ifdef TFTPROOT
164 1.65 manu if (nd->nd_nomount)
165 1.65 manu goto out;
166 1.65 manu #endif
167 1.37 gwr /*
168 1.37 gwr * Now fetch the NFS file handles as appropriate.
169 1.37 gwr */
170 1.62 christos error = nfs_boot_getfh(&nd->nd_root, lwp);
171 1.37 gwr
172 1.50 drochner if (error)
173 1.62 christos nfs_boot_cleanup(nd, lwp);
174 1.50 drochner
175 1.66 manu #ifdef TFTPROOT
176 1.65 manu out:
177 1.66 manu #endif
178 1.37 gwr return (error);
179 1.39 drochner }
180 1.39 drochner
181 1.50 drochner void
182 1.62 christos nfs_boot_cleanup(nd, lwp)
183 1.50 drochner struct nfs_diskless *nd;
184 1.62 christos struct lwp *lwp;
185 1.50 drochner {
186 1.50 drochner
187 1.62 christos nfs_boot_deladdress(nd->nd_ifp, lwp, nd->nd_myip.s_addr);
188 1.62 christos nfs_boot_ifupdown(nd->nd_ifp, lwp, 0);
189 1.50 drochner nfs_boot_flushrt(nd->nd_ifp);
190 1.50 drochner }
191 1.50 drochner
192 1.50 drochner int
193 1.62 christos nfs_boot_ifupdown(ifp, lwp, up)
194 1.50 drochner struct ifnet *ifp;
195 1.62 christos struct lwp *lwp;
196 1.50 drochner int up;
197 1.50 drochner {
198 1.50 drochner struct socket *so;
199 1.50 drochner struct ifreq ireq;
200 1.50 drochner int error;
201 1.50 drochner
202 1.50 drochner memset(&ireq, 0, sizeof(ireq));
203 1.50 drochner memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
204 1.50 drochner
205 1.50 drochner /*
206 1.50 drochner * Get a socket to use for various things in here.
207 1.50 drochner * After this, use "goto out" to cleanup and return.
208 1.50 drochner */
209 1.62 christos error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp);
210 1.50 drochner if (error) {
211 1.50 drochner printf("ifupdown: socreate, error=%d\n", error);
212 1.50 drochner return (error);
213 1.50 drochner }
214 1.50 drochner
215 1.50 drochner /*
216 1.50 drochner * Bring up the interface. (just set the "up" flag)
217 1.50 drochner * Get the old interface flags and or IFF_UP into them so
218 1.50 drochner * things like media selection flags are not clobbered.
219 1.50 drochner */
220 1.64 christos error = ifioctl(so, SIOCGIFFLAGS, (void *)&ireq, lwp);
221 1.50 drochner if (error) {
222 1.50 drochner printf("ifupdown: GIFFLAGS, error=%d\n", error);
223 1.50 drochner goto out;
224 1.50 drochner }
225 1.50 drochner if (up)
226 1.50 drochner ireq.ifr_flags |= IFF_UP;
227 1.50 drochner else
228 1.50 drochner ireq.ifr_flags &= ~IFF_UP;
229 1.64 christos error = ifioctl(so, SIOCSIFFLAGS, (void *)&ireq, lwp);
230 1.50 drochner if (error) {
231 1.50 drochner printf("ifupdown: SIFFLAGS, error=%d\n", error);
232 1.50 drochner goto out;
233 1.50 drochner }
234 1.50 drochner
235 1.52 drochner if (up)
236 1.56 enami /* give the link some time to get up */
237 1.56 enami tsleep(nfs_boot_ifupdown, PZERO, "nfsbif", 3 * hz);
238 1.50 drochner out:
239 1.50 drochner soclose(so);
240 1.50 drochner return (error);
241 1.50 drochner }
242 1.50 drochner
243 1.50 drochner int
244 1.62 christos nfs_boot_setaddress(ifp, lwp, addr, netmask, braddr)
245 1.50 drochner struct ifnet *ifp;
246 1.62 christos struct lwp *lwp;
247 1.50 drochner u_int32_t addr, netmask, braddr;
248 1.50 drochner {
249 1.50 drochner struct socket *so;
250 1.50 drochner struct ifaliasreq iareq;
251 1.50 drochner struct sockaddr_in *sin;
252 1.50 drochner int error;
253 1.50 drochner
254 1.50 drochner /*
255 1.50 drochner * Get a socket to use for various things in here.
256 1.50 drochner * After this, use "goto out" to cleanup and return.
257 1.50 drochner */
258 1.62 christos error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp);
259 1.50 drochner if (error) {
260 1.50 drochner printf("setaddress: socreate, error=%d\n", error);
261 1.50 drochner return (error);
262 1.50 drochner }
263 1.50 drochner
264 1.50 drochner memset(&iareq, 0, sizeof(iareq));
265 1.50 drochner memcpy(iareq.ifra_name, ifp->if_xname, IFNAMSIZ);
266 1.50 drochner
267 1.50 drochner /* Set the I/F address */
268 1.50 drochner sin = (struct sockaddr_in *)&iareq.ifra_addr;
269 1.50 drochner sin->sin_len = sizeof(*sin);
270 1.50 drochner sin->sin_family = AF_INET;
271 1.50 drochner sin->sin_addr.s_addr = addr;
272 1.50 drochner
273 1.50 drochner /* Set the netmask */
274 1.50 drochner if (netmask != INADDR_ANY) {
275 1.50 drochner sin = (struct sockaddr_in *)&iareq.ifra_mask;
276 1.50 drochner sin->sin_len = sizeof(*sin);
277 1.50 drochner sin->sin_family = AF_INET;
278 1.50 drochner sin->sin_addr.s_addr = netmask;
279 1.50 drochner } /* else leave subnetmask unspecified (len=0) */
280 1.50 drochner
281 1.50 drochner /* Set the broadcast addr. */
282 1.50 drochner if (braddr != INADDR_ANY) {
283 1.50 drochner sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
284 1.50 drochner sin->sin_len = sizeof(*sin);
285 1.50 drochner sin->sin_family = AF_INET;
286 1.50 drochner sin->sin_addr.s_addr = braddr;
287 1.50 drochner } /* else leave broadcast addr unspecified (len=0) */
288 1.50 drochner
289 1.64 christos error = ifioctl(so, SIOCAIFADDR, (void *)&iareq, lwp);
290 1.50 drochner if (error) {
291 1.50 drochner printf("setaddress, error=%d\n", error);
292 1.50 drochner goto out;
293 1.50 drochner }
294 1.50 drochner
295 1.56 enami /* give the link some time to get up */
296 1.56 enami tsleep(nfs_boot_setaddress, PZERO, "nfsbtd", 3 * hz);
297 1.50 drochner out:
298 1.50 drochner soclose(so);
299 1.50 drochner return (error);
300 1.50 drochner }
301 1.50 drochner
302 1.50 drochner int
303 1.62 christos nfs_boot_deladdress(ifp, lwp, addr)
304 1.50 drochner struct ifnet *ifp;
305 1.62 christos struct lwp *lwp;
306 1.50 drochner u_int32_t addr;
307 1.50 drochner {
308 1.50 drochner struct socket *so;
309 1.69 dyoung struct ifreq ifr;
310 1.69 dyoung struct sockaddr_in sin;
311 1.69 dyoung struct in_addr ia = {.s_addr = addr};
312 1.50 drochner int error;
313 1.50 drochner
314 1.50 drochner /*
315 1.50 drochner * Get a socket to use for various things in here.
316 1.50 drochner * After this, use "goto out" to cleanup and return.
317 1.50 drochner */
318 1.62 christos error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp);
319 1.50 drochner if (error) {
320 1.50 drochner printf("deladdress: socreate, error=%d\n", error);
321 1.50 drochner return (error);
322 1.50 drochner }
323 1.50 drochner
324 1.69 dyoung memset(&ifr, 0, sizeof(ifr));
325 1.69 dyoung memcpy(ifr.ifr_name, ifp->if_xname, IFNAMSIZ);
326 1.50 drochner
327 1.69 dyoung sockaddr_in_init(&sin, &ia, 0);
328 1.69 dyoung ifreq_setaddr(SIOCDIFADDR, &ifr, sintocsa(&sin));
329 1.50 drochner
330 1.69 dyoung error = ifioctl(so, SIOCDIFADDR, &ifr, lwp);
331 1.50 drochner if (error) {
332 1.50 drochner printf("deladdress, error=%d\n", error);
333 1.50 drochner goto out;
334 1.50 drochner }
335 1.50 drochner
336 1.50 drochner out:
337 1.50 drochner soclose(so);
338 1.50 drochner return (error);
339 1.50 drochner }
340 1.50 drochner
341 1.50 drochner int
342 1.50 drochner nfs_boot_setrecvtimo(so)
343 1.50 drochner struct socket *so;
344 1.39 drochner {
345 1.39 drochner struct mbuf *m;
346 1.39 drochner struct timeval *tv;
347 1.39 drochner
348 1.39 drochner m = m_get(M_WAIT, MT_SOOPTS);
349 1.39 drochner tv = mtod(m, struct timeval *);
350 1.39 drochner m->m_len = sizeof(*tv);
351 1.39 drochner tv->tv_sec = 1;
352 1.39 drochner tv->tv_usec = 0;
353 1.50 drochner return (sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m));
354 1.39 drochner }
355 1.39 drochner
356 1.50 drochner int
357 1.50 drochner nfs_boot_enbroadcast(so)
358 1.50 drochner struct socket *so;
359 1.39 drochner {
360 1.39 drochner struct mbuf *m;
361 1.39 drochner int32_t *on;
362 1.39 drochner
363 1.39 drochner m = m_get(M_WAIT, MT_SOOPTS);
364 1.39 drochner on = mtod(m, int32_t *);
365 1.39 drochner m->m_len = sizeof(*on);
366 1.39 drochner *on = 1;
367 1.50 drochner return (sosetopt(so, SOL_SOCKET, SO_BROADCAST, m));
368 1.39 drochner }
369 1.39 drochner
370 1.50 drochner int
371 1.62 christos nfs_boot_sobind_ipport(so, port, l)
372 1.50 drochner struct socket *so;
373 1.50 drochner u_int16_t port;
374 1.62 christos struct lwp *l;
375 1.39 drochner {
376 1.39 drochner struct mbuf *m;
377 1.39 drochner struct sockaddr_in *sin;
378 1.39 drochner int error;
379 1.39 drochner
380 1.39 drochner m = m_getclr(M_WAIT, MT_SONAME);
381 1.39 drochner sin = mtod(m, struct sockaddr_in *);
382 1.39 drochner sin->sin_len = m->m_len = sizeof(*sin);
383 1.39 drochner sin->sin_family = AF_INET;
384 1.39 drochner sin->sin_addr.s_addr = INADDR_ANY;
385 1.39 drochner sin->sin_port = htons(port);
386 1.62 christos error = sobind(so, m, l);
387 1.39 drochner m_freem(m);
388 1.50 drochner return (error);
389 1.39 drochner }
390 1.39 drochner
391 1.39 drochner /*
392 1.39 drochner * What is the longest we will wait before re-sending a request?
393 1.39 drochner * Note this is also the frequency of "timeout" messages.
394 1.39 drochner * The re-send loop counts up linearly to this maximum, so the
395 1.39 drochner * first complaint will happen after (1+2+3+4+5)=15 seconds.
396 1.39 drochner */
397 1.39 drochner #define MAX_RESEND_DELAY 5 /* seconds */
398 1.39 drochner #define TOTAL_TIMEOUT 30 /* seconds */
399 1.39 drochner
400 1.50 drochner int
401 1.62 christos nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv, from_p, context, lwp)
402 1.50 drochner struct socket *so;
403 1.50 drochner struct mbuf *nam;
404 1.67 dyoung int (*sndproc)(struct mbuf*, void*, int);
405 1.50 drochner struct mbuf *snd;
406 1.67 dyoung int (*rcvproc)(struct mbuf*, void*);
407 1.50 drochner struct mbuf **rcv, **from_p;
408 1.50 drochner void *context;
409 1.62 christos struct lwp *lwp;
410 1.39 drochner {
411 1.39 drochner int error, rcvflg, timo, secs, waited;
412 1.39 drochner struct mbuf *m, *from;
413 1.39 drochner struct uio uio;
414 1.39 drochner
415 1.39 drochner /* Free at end if not null. */
416 1.39 drochner from = NULL;
417 1.39 drochner
418 1.39 drochner /*
419 1.39 drochner * Send it, repeatedly, until a reply is received,
420 1.39 drochner * but delay each re-send by an increasing amount.
421 1.39 drochner * If the delay hits the maximum, start complaining.
422 1.39 drochner */
423 1.39 drochner waited = timo = 0;
424 1.39 drochner send_again:
425 1.39 drochner waited += timo;
426 1.39 drochner if (waited >= TOTAL_TIMEOUT)
427 1.50 drochner return (ETIMEDOUT);
428 1.39 drochner
429 1.39 drochner /* Determine new timeout. */
430 1.39 drochner if (timo < MAX_RESEND_DELAY)
431 1.39 drochner timo++;
432 1.39 drochner else
433 1.39 drochner printf("nfs_boot: timeout...\n");
434 1.39 drochner
435 1.39 drochner if (sndproc) {
436 1.39 drochner error = (*sndproc)(snd, context, waited);
437 1.39 drochner if (error)
438 1.39 drochner goto out;
439 1.39 drochner }
440 1.39 drochner
441 1.39 drochner /* Send request (or re-send). */
442 1.39 drochner m = m_copypacket(snd, M_WAIT);
443 1.39 drochner if (m == NULL) {
444 1.39 drochner error = ENOBUFS;
445 1.39 drochner goto out;
446 1.39 drochner }
447 1.62 christos error = (*so->so_send)(so, nam, NULL, m, NULL, 0, lwp);
448 1.39 drochner if (error) {
449 1.39 drochner printf("nfs_boot: sosend: %d\n", error);
450 1.39 drochner goto out;
451 1.39 drochner }
452 1.39 drochner m = NULL;
453 1.39 drochner
454 1.39 drochner /*
455 1.39 drochner * Wait for up to timo seconds for a reply.
456 1.39 drochner * The socket receive timeout was set to 1 second.
457 1.39 drochner */
458 1.39 drochner
459 1.39 drochner secs = timo;
460 1.39 drochner for (;;) {
461 1.39 drochner if (from) {
462 1.39 drochner m_freem(from);
463 1.39 drochner from = NULL;
464 1.39 drochner }
465 1.39 drochner if (m) {
466 1.39 drochner m_freem(m);
467 1.39 drochner m = NULL;
468 1.39 drochner }
469 1.39 drochner uio.uio_resid = 1 << 16; /* ??? */
470 1.39 drochner rcvflg = 0;
471 1.45 matt error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg);
472 1.39 drochner if (error == EWOULDBLOCK) {
473 1.39 drochner if (--secs <= 0)
474 1.39 drochner goto send_again;
475 1.39 drochner continue;
476 1.39 drochner }
477 1.39 drochner if (error)
478 1.39 drochner goto out;
479 1.39 drochner #ifdef DIAGNOSTIC
480 1.39 drochner if (!m || !(m->m_flags & M_PKTHDR)
481 1.39 drochner || (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
482 1.39 drochner panic("nfs_boot_sendrecv: return size");
483 1.39 drochner #endif
484 1.39 drochner
485 1.39 drochner if ((*rcvproc)(m, context))
486 1.39 drochner continue;
487 1.39 drochner
488 1.39 drochner if (rcv)
489 1.39 drochner *rcv = m;
490 1.39 drochner else
491 1.39 drochner m_freem(m);
492 1.39 drochner if (from_p) {
493 1.39 drochner *from_p = from;
494 1.39 drochner from = NULL;
495 1.39 drochner }
496 1.39 drochner break;
497 1.39 drochner }
498 1.39 drochner out:
499 1.39 drochner if (from) m_freem(from);
500 1.50 drochner return (error);
501 1.35 gwr }
502 1.1 glass
503 1.35 gwr /*
504 1.35 gwr * Install a default route to the passed IP address.
505 1.35 gwr */
506 1.35 gwr static void
507 1.35 gwr nfs_boot_defrt(gw_ip)
508 1.35 gwr struct in_addr *gw_ip;
509 1.35 gwr {
510 1.35 gwr struct sockaddr dst, gw, mask;
511 1.35 gwr struct sockaddr_in *sin;
512 1.35 gwr int error;
513 1.33 gwr
514 1.35 gwr /* Destination: (default) */
515 1.64 christos memset((void *)&dst, 0, sizeof(dst));
516 1.35 gwr dst.sa_len = sizeof(dst);
517 1.35 gwr dst.sa_family = AF_INET;
518 1.35 gwr /* Gateway: */
519 1.64 christos memset((void *)&gw, 0, sizeof(gw));
520 1.35 gwr sin = (struct sockaddr_in *)&gw;
521 1.35 gwr sin->sin_len = sizeof(*sin);
522 1.35 gwr sin->sin_family = AF_INET;
523 1.35 gwr sin->sin_addr.s_addr = gw_ip->s_addr;
524 1.35 gwr /* Mask: (zero length) */
525 1.35 gwr /* XXX - Just pass a null pointer? */
526 1.48 perry memset(&mask, 0, sizeof(mask));
527 1.35 gwr
528 1.35 gwr /* add, dest, gw, mask, flags, 0 */
529 1.35 gwr error = rtrequest(RTM_ADD, &dst, &gw, &mask,
530 1.50 drochner (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
531 1.33 gwr if (error) {
532 1.35 gwr printf("nfs_boot: add route, error=%d\n", error);
533 1.33 gwr error = 0;
534 1.33 gwr }
535 1.50 drochner }
536 1.50 drochner
537 1.50 drochner static int
538 1.68 dyoung nfs_boot_delroute(struct rtentry *rt, void *w)
539 1.50 drochner {
540 1.50 drochner int error;
541 1.50 drochner
542 1.68 dyoung if ((void *)rt->rt_ifp != w)
543 1.68 dyoung return 0;
544 1.50 drochner
545 1.68 dyoung error = rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0,
546 1.68 dyoung NULL);
547 1.68 dyoung if (error != 0)
548 1.68 dyoung printf("%s: del route, error=%d\n", __func__, error);
549 1.50 drochner
550 1.68 dyoung return 0;
551 1.50 drochner }
552 1.50 drochner
553 1.50 drochner void
554 1.67 dyoung nfs_boot_flushrt(struct ifnet *ifp)
555 1.50 drochner {
556 1.50 drochner
557 1.68 dyoung rt_walktree(AF_INET, nfs_boot_delroute, ifp);
558 1.3 gwr }
559 1.1 glass
560 1.35 gwr /*
561 1.35 gwr * Get an initial NFS file handle using Sun RPC/mountd.
562 1.35 gwr * Separate function because we used to call it twice.
563 1.35 gwr * (once for root and once for swap)
564 1.35 gwr */
565 1.37 gwr static int
566 1.62 christos nfs_boot_getfh(ndm, l)
567 1.33 gwr struct nfs_dlmount *ndm; /* output */
568 1.62 christos struct lwp *l;
569 1.3 gwr {
570 1.29 fvdl struct nfs_args *args;
571 1.23 gwr struct sockaddr_in *sin;
572 1.33 gwr char *pathname;
573 1.3 gwr int error;
574 1.35 gwr u_int16_t port;
575 1.1 glass
576 1.33 gwr args = &ndm->ndm_args;
577 1.29 fvdl
578 1.29 fvdl /* Initialize mount args. */
579 1.64 christos memset((void *) args, 0, sizeof(*args));
580 1.33 gwr args->addr = &ndm->ndm_saddr;
581 1.29 fvdl args->addrlen = args->addr->sa_len;
582 1.29 fvdl #ifdef NFS_BOOT_TCP
583 1.29 fvdl args->sotype = SOCK_STREAM;
584 1.29 fvdl #else
585 1.29 fvdl args->sotype = SOCK_DGRAM;
586 1.29 fvdl #endif
587 1.33 gwr args->fh = ndm->ndm_fh;
588 1.33 gwr args->hostname = ndm->ndm_host;
589 1.54 bjh21 args->flags = NFSMNT_NOCONN | NFSMNT_RESVPORT;
590 1.29 fvdl
591 1.54 bjh21 #ifndef NFS_V2_ONLY
592 1.54 bjh21 args->flags |= NFSMNT_NFSV3;
593 1.54 bjh21 #endif
594 1.29 fvdl #ifdef NFS_BOOT_OPTIONS
595 1.29 fvdl args->flags |= NFS_BOOT_OPTIONS;
596 1.29 fvdl #endif
597 1.29 fvdl #ifdef NFS_BOOT_RWSIZE
598 1.29 fvdl /*
599 1.29 fvdl * Reduce rsize,wsize for interfaces that consistently
600 1.29 fvdl * drop fragments of long UDP messages. (i.e. wd8003).
601 1.29 fvdl * You can always change these later via remount.
602 1.29 fvdl */
603 1.29 fvdl args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
604 1.29 fvdl args->wsize = NFS_BOOT_RWSIZE;
605 1.29 fvdl args->rsize = NFS_BOOT_RWSIZE;
606 1.29 fvdl #endif
607 1.29 fvdl
608 1.3 gwr /*
609 1.33 gwr * Find the pathname part of the "server:pathname"
610 1.33 gwr * string left in ndm->ndm_host by nfs_boot_init.
611 1.3 gwr */
612 1.33 gwr pathname = strchr(ndm->ndm_host, ':');
613 1.33 gwr if (pathname == 0) {
614 1.33 gwr printf("nfs_boot: getfh - no pathname\n");
615 1.33 gwr return (EIO);
616 1.33 gwr }
617 1.33 gwr pathname++;
618 1.1 glass
619 1.3 gwr /*
620 1.33 gwr * Get file handle using RPC to mountd/mount
621 1.3 gwr */
622 1.33 gwr sin = (struct sockaddr_in *)&ndm->ndm_saddr;
623 1.62 christos error = md_mount(sin, pathname, args, l);
624 1.33 gwr if (error) {
625 1.33 gwr printf("nfs_boot: mountd `%s', error=%d\n",
626 1.35 gwr ndm->ndm_host, error);
627 1.33 gwr return (error);
628 1.33 gwr }
629 1.4 pk
630 1.23 gwr /* Set port number for NFS use. */
631 1.23 gwr /* XXX: NFS port is always 2049, right? */
632 1.29 fvdl #ifdef NFS_BOOT_TCP
633 1.29 fvdl retry:
634 1.29 fvdl #endif
635 1.29 fvdl error = krpc_portmap(sin, NFS_PROG,
636 1.29 fvdl (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
637 1.29 fvdl (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
638 1.62 christos &port, l);
639 1.35 gwr if (port == htons(0))
640 1.33 gwr error = EIO;
641 1.35 gwr if (error) {
642 1.29 fvdl #ifdef NFS_BOOT_TCP
643 1.29 fvdl if (args->sotype == SOCK_STREAM) {
644 1.29 fvdl args->sotype = SOCK_DGRAM;
645 1.29 fvdl goto retry;
646 1.29 fvdl }
647 1.29 fvdl #endif
648 1.33 gwr printf("nfs_boot: portmap NFS, error=%d\n", error);
649 1.35 gwr return (error);
650 1.29 fvdl }
651 1.35 gwr sin->sin_port = port;
652 1.35 gwr return (0);
653 1.3 gwr }
654 1.1 glass
655 1.1 glass
656 1.1 glass /*
657 1.3 gwr * RPC: mountd/mount
658 1.3 gwr * Given a server pathname, get an NFS file handle.
659 1.3 gwr * Also, sets sin->sin_port to the NFS service port.
660 1.1 glass */
661 1.3 gwr static int
662 1.62 christos md_mount(mdsin, path, argp, lwp)
663 1.3 gwr struct sockaddr_in *mdsin; /* mountd server address */
664 1.3 gwr char *path;
665 1.29 fvdl struct nfs_args *argp;
666 1.62 christos struct lwp *lwp;
667 1.1 glass {
668 1.3 gwr /* The RPC structures */
669 1.3 gwr struct rdata {
670 1.20 cgd u_int32_t errno;
671 1.29 fvdl union {
672 1.29 fvdl u_int8_t v2fh[NFSX_V2FH];
673 1.29 fvdl struct {
674 1.29 fvdl u_int32_t fhlen;
675 1.29 fvdl u_int8_t fh[1];
676 1.29 fvdl } v3fh;
677 1.29 fvdl } fh;
678 1.3 gwr } *rdata;
679 1.3 gwr struct mbuf *m;
680 1.29 fvdl u_int8_t *fh;
681 1.29 fvdl int minlen, error;
682 1.35 gwr int mntver;
683 1.3 gwr
684 1.35 gwr mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
685 1.35 gwr do {
686 1.35 gwr /*
687 1.35 gwr * Get port number for MOUNTD.
688 1.35 gwr */
689 1.35 gwr error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
690 1.62 christos IPPROTO_UDP, &mdsin->sin_port, lwp);
691 1.35 gwr if (error)
692 1.35 gwr continue;
693 1.35 gwr
694 1.35 gwr /* This mbuf is consumed by krpc_call. */
695 1.35 gwr m = xdr_string_encode(path, strlen(path));
696 1.35 gwr if (m == NULL)
697 1.35 gwr return ENOMEM;
698 1.11 gwr
699 1.35 gwr /* Do RPC to mountd. */
700 1.35 gwr error = krpc_call(mdsin, RPCPROG_MNT, mntver,
701 1.62 christos RPCMNT_MOUNT, &m, NULL, lwp);
702 1.35 gwr if (error != EPROGMISMATCH)
703 1.35 gwr break;
704 1.35 gwr /* Try lower version of mountd. */
705 1.35 gwr } while (--mntver >= 1);
706 1.29 fvdl if (error) {
707 1.35 gwr printf("nfs_boot: mountd error=%d\n", error);
708 1.35 gwr return error;
709 1.29 fvdl }
710 1.35 gwr if (mntver != 3)
711 1.35 gwr argp->flags &= ~NFSMNT_NFSV3;
712 1.1 glass
713 1.23 gwr /* The reply might have only the errno. */
714 1.23 gwr if (m->m_len < 4)
715 1.23 gwr goto bad;
716 1.23 gwr /* Have at least errno, so check that. */
717 1.23 gwr rdata = mtod(m, struct rdata *);
718 1.23 gwr error = fxdr_unsigned(u_int32_t, rdata->errno);
719 1.23 gwr if (error)
720 1.23 gwr goto out;
721 1.23 gwr
722 1.35 gwr /* Have errno==0, so the fh must be there. */
723 1.35 gwr if (mntver == 3) {
724 1.29 fvdl argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
725 1.29 fvdl if (argp->fhsize > NFSX_V3FHMAX)
726 1.29 fvdl goto bad;
727 1.29 fvdl minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
728 1.29 fvdl } else {
729 1.29 fvdl argp->fhsize = NFSX_V2FH;
730 1.29 fvdl minlen = sizeof(u_int32_t) + argp->fhsize;
731 1.29 fvdl }
732 1.29 fvdl
733 1.29 fvdl if (m->m_len < minlen) {
734 1.29 fvdl m = m_pullup(m, minlen);
735 1.16 gwr if (m == NULL)
736 1.29 fvdl return(EBADRPC);
737 1.23 gwr rdata = mtod(m, struct rdata *);
738 1.16 gwr }
739 1.29 fvdl
740 1.35 gwr fh = (mntver == 3) ?
741 1.35 gwr rdata->fh.v3fh.fh : rdata->fh.v2fh;
742 1.48 perry memcpy(argp->fh, fh, argp->fhsize);
743 1.29 fvdl
744 1.3 gwr goto out;
745 1.1 glass
746 1.6 deraadt bad:
747 1.3 gwr error = EBADRPC;
748 1.1 glass
749 1.6 deraadt out:
750 1.3 gwr m_freem(m);
751 1.3 gwr return error;
752 1.7 paulus }
753