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