nfs_boot.c revision 1.29 1 1.29 fvdl /* $NetBSD: nfs_boot.c,v 1.29 1996/10/20 13:13:25 fvdl Exp $ */
2 1.5 cgd
3 1.1 glass /*
4 1.16 gwr * Copyright (c) 1995 Adam Glass, Gordon Ross
5 1.1 glass * All rights reserved.
6 1.1 glass *
7 1.1 glass * Redistribution and use in source and binary forms, with or without
8 1.1 glass * modification, are permitted provided that the following conditions
9 1.1 glass * are met:
10 1.1 glass * 1. Redistributions of source code must retain the above copyright
11 1.1 glass * notice, this list of conditions and the following disclaimer.
12 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 glass * notice, this list of conditions and the following disclaimer in the
14 1.1 glass * documentation and/or other materials provided with the distribution.
15 1.3 gwr * 3. The name of the authors may not be used to endorse or promote products
16 1.1 glass * derived from this software without specific prior written permission.
17 1.1 glass *
18 1.3 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 1.3 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.3 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.3 gwr * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.3 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.3 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.3 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.3 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.3 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.3 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 glass */
29 1.1 glass
30 1.2 cgd #include <sys/param.h>
31 1.1 glass #include <sys/systm.h>
32 1.1 glass #include <sys/kernel.h>
33 1.1 glass #include <sys/conf.h>
34 1.1 glass #include <sys/ioctl.h>
35 1.1 glass #include <sys/proc.h>
36 1.1 glass #include <sys/mount.h>
37 1.1 glass #include <sys/mbuf.h>
38 1.16 gwr #include <sys/reboot.h>
39 1.1 glass #include <sys/socket.h>
40 1.16 gwr #include <sys/socketvar.h>
41 1.1 glass
42 1.1 glass #include <net/if.h>
43 1.3 gwr #include <net/route.h>
44 1.3 gwr
45 1.1 glass #include <netinet/in.h>
46 1.3 gwr #include <netinet/if_ether.h>
47 1.1 glass
48 1.1 glass #include <nfs/rpcv2.h>
49 1.25 fvdl #include <nfs/nfsproto.h>
50 1.1 glass #include <nfs/nfs.h>
51 1.1 glass #include <nfs/nfsdiskless.h>
52 1.11 gwr #include <nfs/krpc.h>
53 1.17 mycroft #include <nfs/xdr_subs.h>
54 1.21 christos #include <nfs/nfs_var.h>
55 1.1 glass
56 1.7 paulus #include "ether.h"
57 1.11 gwr #if NETHER == 0
58 1.11 gwr
59 1.11 gwr int nfs_boot_init(nd, procp)
60 1.11 gwr struct nfs_diskless *nd;
61 1.11 gwr struct proc *procp;
62 1.11 gwr {
63 1.11 gwr panic("nfs_boot_init: no ether");
64 1.24 gwr }
65 1.24 gwr
66 1.24 gwr void
67 1.24 gwr nfs_boot_getfh(bpsin, key, ndmntp)
68 1.24 gwr struct sockaddr_in *bpsin;
69 1.24 gwr char *key;
70 1.24 gwr struct nfs_dlmount *ndmntp;
71 1.24 gwr {
72 1.24 gwr /* can not get here */
73 1.11 gwr }
74 1.11 gwr
75 1.11 gwr #else /* NETHER */
76 1.7 paulus
77 1.1 glass /*
78 1.1 glass * Support for NFS diskless booting, specifically getting information
79 1.1 glass * about where to boot from, what pathnames, etc.
80 1.1 glass *
81 1.3 gwr * This implememtation uses RARP and the bootparam RPC.
82 1.3 gwr * We are forced to implement RPC anyway (to get file handles)
83 1.3 gwr * so we might as well take advantage of it for bootparam too.
84 1.1 glass *
85 1.3 gwr * The diskless boot sequence goes as follows:
86 1.15 gwr * (1) Use RARP to get our interface address
87 1.15 gwr * (2) Use RPC/bootparam/whoami to get our hostname,
88 1.15 gwr * our IP address, and the server's IP address.
89 1.15 gwr * (3) Use RPC/bootparam/getfile to get the root path
90 1.15 gwr * (4) Use RPC/mountd to get the root file handle
91 1.15 gwr * (5) Use RPC/bootparam/getfile to get the swap path
92 1.15 gwr * (6) Use RPC/mountd to get the swap file handle
93 1.1 glass *
94 1.3 gwr * (This happens to be the way Sun does it too.)
95 1.1 glass */
96 1.1 glass
97 1.3 gwr /* bootparam RPC */
98 1.6 deraadt static int bp_whoami __P((struct sockaddr_in *bpsin,
99 1.6 deraadt struct in_addr *my_ip, struct in_addr *gw_ip));
100 1.6 deraadt static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
101 1.6 deraadt struct sockaddr_in *mdsin, char *servname, char *path));
102 1.3 gwr
103 1.3 gwr /* mountd RPC */
104 1.6 deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
105 1.29 fvdl struct nfs_args *argp));
106 1.3 gwr
107 1.13 pk char *nfsbootdevname;
108 1.13 pk
109 1.1 glass /*
110 1.3 gwr * Called with an empty nfs_diskless struct to be filled in.
111 1.1 glass */
112 1.6 deraadt int
113 1.6 deraadt nfs_boot_init(nd, procp)
114 1.3 gwr struct nfs_diskless *nd;
115 1.3 gwr struct proc *procp;
116 1.1 glass {
117 1.3 gwr struct ifreq ireq;
118 1.11 gwr struct in_addr my_ip, gw_ip;
119 1.3 gwr struct sockaddr_in bp_sin;
120 1.1 glass struct sockaddr_in *sin;
121 1.3 gwr struct ifnet *ifp;
122 1.3 gwr struct socket *so;
123 1.16 gwr int error;
124 1.3 gwr
125 1.3 gwr /*
126 1.3 gwr * Find an interface, rarp for its ip address, stuff it, the
127 1.3 gwr * implied broadcast addr, and netmask into a nfs_diskless struct.
128 1.3 gwr *
129 1.3 gwr * This was moved here from nfs_vfsops.c because this procedure
130 1.3 gwr * would be quite different if someone decides to write (i.e.) a
131 1.15 gwr * BOOTP version of this file (might not use RARP, etc.)
132 1.3 gwr */
133 1.3 gwr
134 1.3 gwr /*
135 1.3 gwr * Find a network interface.
136 1.3 gwr */
137 1.13 pk if (nfsbootdevname)
138 1.13 pk ifp = ifunit(nfsbootdevname);
139 1.13 pk else
140 1.19 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
141 1.13 pk if ((ifp->if_flags &
142 1.13 pk (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
143 1.13 pk break;
144 1.3 gwr if (ifp == NULL)
145 1.3 gwr panic("nfs_boot: no suitable interface");
146 1.26 thorpej bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
147 1.28 christos printf("nfs_boot: using network interface '%s'\n",
148 1.6 deraadt ireq.ifr_name);
149 1.3 gwr
150 1.3 gwr /*
151 1.3 gwr * Bring up the interface.
152 1.18 cgd *
153 1.18 cgd * Get the old interface flags and or IFF_UP into them; if
154 1.18 cgd * IFF_UP set blindly, interface selection can be clobbered.
155 1.3 gwr */
156 1.3 gwr if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
157 1.3 gwr panic("nfs_boot: socreate, error=%d", error);
158 1.18 cgd error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
159 1.18 cgd if (error)
160 1.18 cgd panic("nfs_boot: GIFFLAGS, error=%d", error);
161 1.18 cgd ireq.ifr_flags |= IFF_UP;
162 1.3 gwr error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
163 1.6 deraadt if (error)
164 1.6 deraadt panic("nfs_boot: SIFFLAGS, error=%d", error);
165 1.3 gwr
166 1.3 gwr /*
167 1.15 gwr * Do RARP for the interface address.
168 1.3 gwr */
169 1.11 gwr if ((error = revarpwhoami(&my_ip, ifp)) != 0)
170 1.3 gwr panic("revarp failed, error=%d", error);
171 1.28 christos printf("nfs_boot: client_addr=0x%x\n", (u_int32_t)ntohl(my_ip.s_addr));
172 1.3 gwr
173 1.3 gwr /*
174 1.15 gwr * Do enough of ifconfig(8) so that the chosen interface
175 1.15 gwr * can talk to the servers. (just set the address)
176 1.3 gwr */
177 1.3 gwr sin = (struct sockaddr_in *)&ireq.ifr_addr;
178 1.10 mycroft bzero((caddr_t)sin, sizeof(*sin));
179 1.3 gwr sin->sin_len = sizeof(*sin);
180 1.3 gwr sin->sin_family = AF_INET;
181 1.3 gwr sin->sin_addr.s_addr = my_ip.s_addr;
182 1.3 gwr error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
183 1.6 deraadt if (error)
184 1.6 deraadt panic("nfs_boot: set if addr, error=%d", error);
185 1.3 gwr
186 1.3 gwr soclose(so);
187 1.3 gwr
188 1.3 gwr /*
189 1.3 gwr * Get client name and gateway address.
190 1.3 gwr * RPC: bootparam/whoami
191 1.15 gwr * Use the old broadcast address for the WHOAMI
192 1.15 gwr * call because we do not yet know our netmask.
193 1.15 gwr * The server address returned by the WHOAMI call
194 1.15 gwr * is used for all subsequent booptaram RPCs.
195 1.3 gwr */
196 1.10 mycroft bzero((caddr_t)&bp_sin, sizeof(bp_sin));
197 1.3 gwr bp_sin.sin_len = sizeof(bp_sin);
198 1.3 gwr bp_sin.sin_family = AF_INET;
199 1.15 gwr bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
200 1.3 gwr hostnamelen = MAXHOSTNAMELEN;
201 1.6 deraadt
202 1.6 deraadt /* this returns gateway IP address */
203 1.6 deraadt error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
204 1.3 gwr if (error)
205 1.3 gwr panic("nfs_boot: bootparam whoami, error=%d", error);
206 1.28 christos printf("nfs_boot: server_addr=0x%x\n",
207 1.25 fvdl (u_int32_t)ntohl(bp_sin.sin_addr.s_addr));
208 1.28 christos printf("nfs_boot: hostname=%s\n", hostname);
209 1.1 glass
210 1.3 gwr #ifdef NFS_BOOT_GATEWAY
211 1.1 glass /*
212 1.8 gwr * XXX - This code is conditionally compiled only because
213 1.8 gwr * many bootparam servers (in particular, SunOS 4.1.3)
214 1.8 gwr * always set the gateway address to their own address.
215 1.8 gwr * The bootparam server is not necessarily the gateway.
216 1.8 gwr * We could just believe the server, and at worst you would
217 1.8 gwr * need to delete the incorrect default route before adding
218 1.8 gwr * the correct one, but for simplicity, ignore the gateway.
219 1.3 gwr * If your server is OK, you can turn on this option.
220 1.3 gwr *
221 1.3 gwr * If the gateway address is set, add a default route.
222 1.3 gwr * (The mountd RPCs may go across a gateway.)
223 1.1 glass */
224 1.3 gwr if (gw_ip.s_addr) {
225 1.8 gwr struct sockaddr dst, gw, mask;
226 1.3 gwr /* Destination: (default) */
227 1.10 mycroft bzero((caddr_t)&dst, sizeof(dst));
228 1.8 gwr dst.sa_len = sizeof(dst);
229 1.8 gwr dst.sa_family = AF_INET;
230 1.3 gwr /* Gateway: */
231 1.10 mycroft bzero((caddr_t)&gw, sizeof(gw));
232 1.8 gwr sin = (struct sockaddr_in *)&gw;
233 1.8 gwr sin->sin_len = sizeof(gw);
234 1.8 gwr sin->sin_family = AF_INET;
235 1.8 gwr sin->sin_addr.s_addr = gw_ip.s_addr;
236 1.8 gwr /* Mask: (zero length) */
237 1.8 gwr bzero(&mask, sizeof(mask));
238 1.6 deraadt
239 1.28 christos printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
240 1.3 gwr /* add, dest, gw, mask, flags, 0 */
241 1.8 gwr error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
242 1.8 gwr &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
243 1.3 gwr if (error)
244 1.28 christos printf("nfs_boot: add route, error=%d\n", error);
245 1.3 gwr }
246 1.3 gwr #endif
247 1.3 gwr
248 1.23 gwr bcopy(&bp_sin, &nd->nd_boot, sizeof(bp_sin));
249 1.1 glass
250 1.3 gwr return (0);
251 1.3 gwr }
252 1.1 glass
253 1.23 gwr void
254 1.23 gwr nfs_boot_getfh(bpsin, key, ndmntp)
255 1.3 gwr struct sockaddr_in *bpsin; /* bootparam server */
256 1.6 deraadt char *key; /* root or swap */
257 1.6 deraadt struct nfs_dlmount *ndmntp; /* output */
258 1.3 gwr {
259 1.29 fvdl struct nfs_args *args;
260 1.3 gwr char pathname[MAXPATHLEN];
261 1.4 pk char *sp, *dp, *endp;
262 1.23 gwr struct sockaddr_in *sin;
263 1.3 gwr int error;
264 1.1 glass
265 1.29 fvdl args = &ndmntp->ndm_args;
266 1.29 fvdl
267 1.29 fvdl /* Initialize mount args. */
268 1.29 fvdl bzero((caddr_t) args, sizeof(*args));
269 1.29 fvdl args->addr = &ndmntp->ndm_saddr;
270 1.29 fvdl args->addrlen = args->addr->sa_len;
271 1.29 fvdl #ifdef NFS_BOOT_TCP
272 1.29 fvdl args->sotype = SOCK_STREAM;
273 1.29 fvdl #else
274 1.29 fvdl args->sotype = SOCK_DGRAM;
275 1.29 fvdl #endif
276 1.29 fvdl args->fh = ndmntp->ndm_fh;
277 1.29 fvdl args->hostname = ndmntp->ndm_host;
278 1.29 fvdl args->flags = NFSMNT_RESVPORT | NFSMNT_NFSV3;
279 1.29 fvdl
280 1.29 fvdl #ifdef NFS_BOOT_OPTIONS
281 1.29 fvdl args->flags |= NFS_BOOT_OPTIONS;
282 1.29 fvdl #endif
283 1.29 fvdl #ifdef NFS_BOOT_RWSIZE
284 1.29 fvdl /*
285 1.29 fvdl * Reduce rsize,wsize for interfaces that consistently
286 1.29 fvdl * drop fragments of long UDP messages. (i.e. wd8003).
287 1.29 fvdl * You can always change these later via remount.
288 1.29 fvdl */
289 1.29 fvdl args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
290 1.29 fvdl args->wsize = NFS_BOOT_RWSIZE;
291 1.29 fvdl args->rsize = NFS_BOOT_RWSIZE;
292 1.29 fvdl #endif
293 1.29 fvdl
294 1.29 fvdl sin = (void*)&ndmntp->ndm_saddr;
295 1.23 gwr
296 1.3 gwr /*
297 1.3 gwr * Get server:pathname for "key" (root or swap)
298 1.3 gwr * using RPC to bootparam/getfile
299 1.3 gwr */
300 1.25 fvdl error = bp_getfile(bpsin, key, sin, ndmntp->ndm_host, pathname);
301 1.3 gwr if (error)
302 1.3 gwr panic("nfs_boot: bootparam get %s: %d", key, error);
303 1.1 glass
304 1.3 gwr /*
305 1.3 gwr * Get file handle for "key" (root or swap)
306 1.3 gwr * using RPC to mountd/mount
307 1.3 gwr */
308 1.29 fvdl error = md_mount(sin, pathname, args);
309 1.3 gwr if (error)
310 1.3 gwr panic("nfs_boot: mountd %s, error=%d", key, error);
311 1.4 pk
312 1.23 gwr /* Set port number for NFS use. */
313 1.23 gwr /* XXX: NFS port is always 2049, right? */
314 1.29 fvdl #ifdef NFS_BOOT_TCP
315 1.29 fvdl retry:
316 1.29 fvdl #endif
317 1.29 fvdl error = krpc_portmap(sin, NFS_PROG,
318 1.29 fvdl (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
319 1.29 fvdl (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
320 1.29 fvdl &sin->sin_port);
321 1.29 fvdl
322 1.29 fvdl if (error || (sin->sin_port == htons(0))) {
323 1.29 fvdl #ifdef NFS_BOOT_TCP
324 1.29 fvdl if (args->sotype == SOCK_STREAM) {
325 1.29 fvdl args->sotype = SOCK_DGRAM;
326 1.29 fvdl goto retry;
327 1.29 fvdl }
328 1.29 fvdl #endif
329 1.29 fvdl panic("nfs_boot: portmap NFS, error=%d", error);
330 1.29 fvdl }
331 1.23 gwr
332 1.4 pk /* Construct remote path (for getmntinfo(3)) */
333 1.4 pk dp = ndmntp->ndm_host;
334 1.4 pk endp = dp + MNAMELEN - 1;
335 1.4 pk dp += strlen(dp);
336 1.4 pk *dp++ = ':';
337 1.4 pk for (sp = pathname; *sp && dp < endp;)
338 1.4 pk *dp++ = *sp++;
339 1.4 pk *dp = '\0';
340 1.6 deraadt
341 1.1 glass }
342 1.1 glass
343 1.3 gwr
344 1.1 glass /*
345 1.3 gwr * RPC: bootparam/whoami
346 1.3 gwr * Given client IP address, get:
347 1.3 gwr * client name (hostname)
348 1.3 gwr * domain name (domainname)
349 1.3 gwr * gateway address
350 1.3 gwr *
351 1.15 gwr * The hostname and domainname are set here for convenience.
352 1.11 gwr *
353 1.11 gwr * Note - bpsin is initialized to the broadcast address,
354 1.11 gwr * and will be replaced with the bootparam server address
355 1.11 gwr * after this call is complete. Have to use PMAP_PROC_CALL
356 1.11 gwr * to make sure we get responses only from a servers that
357 1.11 gwr * know about us (don't want to broadcast a getport call).
358 1.3 gwr */
359 1.3 gwr static int
360 1.6 deraadt bp_whoami(bpsin, my_ip, gw_ip)
361 1.6 deraadt struct sockaddr_in *bpsin;
362 1.6 deraadt struct in_addr *my_ip;
363 1.6 deraadt struct in_addr *gw_ip;
364 1.1 glass {
365 1.11 gwr /* RPC structures for PMAPPROC_CALLIT */
366 1.11 gwr struct whoami_call {
367 1.16 gwr u_int32_t call_prog;
368 1.16 gwr u_int32_t call_vers;
369 1.16 gwr u_int32_t call_proc;
370 1.16 gwr u_int32_t call_arglen;
371 1.11 gwr } *call;
372 1.16 gwr struct callit_reply {
373 1.16 gwr u_int32_t port;
374 1.16 gwr u_int32_t encap_len;
375 1.16 gwr /* encapsulated data here */
376 1.16 gwr } *reply;
377 1.11 gwr
378 1.11 gwr struct mbuf *m, *from;
379 1.3 gwr struct sockaddr_in *sin;
380 1.3 gwr int error, msg_len;
381 1.16 gwr int16_t port;
382 1.1 glass
383 1.3 gwr /*
384 1.11 gwr * Build request message for PMAPPROC_CALLIT.
385 1.3 gwr */
386 1.16 gwr m = m_get(M_WAIT, MT_DATA);
387 1.11 gwr call = mtod(m, struct whoami_call *);
388 1.16 gwr m->m_len = sizeof(*call);
389 1.17 mycroft call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
390 1.17 mycroft call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
391 1.17 mycroft call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
392 1.11 gwr
393 1.16 gwr /*
394 1.16 gwr * append encapsulated data (client IP address)
395 1.16 gwr */
396 1.16 gwr m->m_next = xdr_inaddr_encode(my_ip);
397 1.17 mycroft call->call_arglen = txdr_unsigned(m->m_next->m_len);
398 1.11 gwr
399 1.11 gwr /* RPC: portmap/callit */
400 1.11 gwr bpsin->sin_port = htons(PMAPPORT);
401 1.11 gwr from = NULL;
402 1.11 gwr error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
403 1.11 gwr PMAPPROC_CALLIT, &m, &from);
404 1.1 glass if (error)
405 1.1 glass return error;
406 1.3 gwr
407 1.3 gwr /*
408 1.3 gwr * Parse result message.
409 1.3 gwr */
410 1.16 gwr if (m->m_len < sizeof(*reply)) {
411 1.16 gwr m = m_pullup(m, sizeof(*reply));
412 1.16 gwr if (m == NULL)
413 1.16 gwr goto bad;
414 1.16 gwr }
415 1.16 gwr reply = mtod(m, struct callit_reply *);
416 1.17 mycroft port = fxdr_unsigned(u_int32_t, reply->port);
417 1.17 mycroft msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
418 1.16 gwr m_adj(m, sizeof(*reply));
419 1.11 gwr
420 1.16 gwr /*
421 1.16 gwr * Save bootparam server address
422 1.16 gwr */
423 1.11 gwr sin = mtod(from, struct sockaddr_in *);
424 1.16 gwr bpsin->sin_port = htons(port);
425 1.11 gwr bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
426 1.11 gwr
427 1.3 gwr /* client name */
428 1.16 gwr hostnamelen = MAXHOSTNAMELEN-1;
429 1.16 gwr m = xdr_string_decode(m, hostname, &hostnamelen);
430 1.16 gwr if (m == NULL)
431 1.3 gwr goto bad;
432 1.3 gwr
433 1.3 gwr /* domain name */
434 1.16 gwr domainnamelen = MAXHOSTNAMELEN-1;
435 1.16 gwr m = xdr_string_decode(m, domainname, &domainnamelen);
436 1.16 gwr if (m == NULL)
437 1.3 gwr goto bad;
438 1.3 gwr
439 1.3 gwr /* gateway address */
440 1.16 gwr m = xdr_inaddr_decode(m, gw_ip);
441 1.16 gwr if (m == NULL)
442 1.3 gwr goto bad;
443 1.16 gwr
444 1.16 gwr /* success */
445 1.3 gwr goto out;
446 1.3 gwr
447 1.6 deraadt bad:
448 1.28 christos printf("nfs_boot: bootparam_whoami: bad reply\n");
449 1.3 gwr error = EBADRPC;
450 1.3 gwr
451 1.6 deraadt out:
452 1.11 gwr if (from)
453 1.11 gwr m_freem(from);
454 1.16 gwr if (m)
455 1.16 gwr m_freem(m);
456 1.3 gwr return(error);
457 1.1 glass }
458 1.1 glass
459 1.3 gwr
460 1.1 glass /*
461 1.3 gwr * RPC: bootparam/getfile
462 1.3 gwr * Given client name and file "key", get:
463 1.3 gwr * server name
464 1.3 gwr * server IP address
465 1.3 gwr * server pathname
466 1.1 glass */
467 1.3 gwr static int
468 1.6 deraadt bp_getfile(bpsin, key, md_sin, serv_name, pathname)
469 1.6 deraadt struct sockaddr_in *bpsin;
470 1.6 deraadt char *key;
471 1.6 deraadt struct sockaddr_in *md_sin;
472 1.6 deraadt char *serv_name;
473 1.6 deraadt char *pathname;
474 1.1 glass {
475 1.3 gwr struct mbuf *m;
476 1.1 glass struct sockaddr_in *sin;
477 1.16 gwr struct in_addr inaddr;
478 1.16 gwr int error, sn_len, path_len;
479 1.1 glass
480 1.3 gwr /*
481 1.16 gwr * Build request message.
482 1.3 gwr */
483 1.1 glass
484 1.3 gwr /* client name (hostname) */
485 1.16 gwr m = xdr_string_encode(hostname, hostnamelen);
486 1.22 pk if (m == NULL)
487 1.22 pk return (ENOMEM);
488 1.16 gwr
489 1.3 gwr /* key name (root or swap) */
490 1.16 gwr m->m_next = xdr_string_encode(key, strlen(key));
491 1.22 pk if (m->m_next == NULL)
492 1.22 pk return (ENOMEM);
493 1.3 gwr
494 1.3 gwr /* RPC: bootparam/getfile */
495 1.11 gwr error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
496 1.11 gwr BOOTPARAM_GETFILE, &m, NULL);
497 1.3 gwr if (error)
498 1.1 glass return error;
499 1.3 gwr
500 1.3 gwr /*
501 1.3 gwr * Parse result message.
502 1.3 gwr */
503 1.3 gwr
504 1.3 gwr /* server name */
505 1.16 gwr sn_len = MNAMELEN-1;
506 1.16 gwr m = xdr_string_decode(m, serv_name, &sn_len);
507 1.16 gwr if (m == NULL)
508 1.3 gwr goto bad;
509 1.16 gwr
510 1.16 gwr /* server IP address (mountd/NFS) */
511 1.16 gwr m = xdr_inaddr_decode(m, &inaddr);
512 1.16 gwr if (m == NULL)
513 1.3 gwr goto bad;
514 1.16 gwr
515 1.16 gwr /* server pathname */
516 1.16 gwr path_len = MAXPATHLEN-1;
517 1.16 gwr m = xdr_string_decode(m, pathname, &path_len);
518 1.16 gwr if (m == NULL)
519 1.3 gwr goto bad;
520 1.3 gwr
521 1.16 gwr /* setup server socket address */
522 1.3 gwr sin = md_sin;
523 1.10 mycroft bzero((caddr_t)sin, sizeof(*sin));
524 1.3 gwr sin->sin_len = sizeof(*sin);
525 1.1 glass sin->sin_family = AF_INET;
526 1.16 gwr sin->sin_addr = inaddr;
527 1.3 gwr
528 1.16 gwr /* success */
529 1.3 gwr goto out;
530 1.3 gwr
531 1.6 deraadt bad:
532 1.28 christos printf("nfs_boot: bootparam_getfile: bad reply\n");
533 1.3 gwr error = EBADRPC;
534 1.1 glass
535 1.6 deraadt out:
536 1.3 gwr m_freem(m);
537 1.3 gwr return(0);
538 1.3 gwr }
539 1.1 glass
540 1.1 glass
541 1.1 glass /*
542 1.3 gwr * RPC: mountd/mount
543 1.3 gwr * Given a server pathname, get an NFS file handle.
544 1.3 gwr * Also, sets sin->sin_port to the NFS service port.
545 1.1 glass */
546 1.3 gwr static int
547 1.29 fvdl md_mount(mdsin, path, argp)
548 1.3 gwr struct sockaddr_in *mdsin; /* mountd server address */
549 1.3 gwr char *path;
550 1.29 fvdl struct nfs_args *argp;
551 1.1 glass {
552 1.3 gwr /* The RPC structures */
553 1.3 gwr struct rdata {
554 1.20 cgd u_int32_t errno;
555 1.29 fvdl union {
556 1.29 fvdl u_int8_t v2fh[NFSX_V2FH];
557 1.29 fvdl struct {
558 1.29 fvdl u_int32_t fhlen;
559 1.29 fvdl u_int8_t fh[1];
560 1.29 fvdl } v3fh;
561 1.29 fvdl } fh;
562 1.3 gwr } *rdata;
563 1.3 gwr struct mbuf *m;
564 1.29 fvdl u_int8_t *fh;
565 1.29 fvdl int minlen, error;
566 1.3 gwr
567 1.29 fvdl retry:
568 1.11 gwr /* Get port number for MOUNTD. */
569 1.29 fvdl error = krpc_portmap(mdsin, RPCPROG_MNT,
570 1.29 fvdl (argp->flags & NFSMNT_NFSV3) ? RPCMNT_VER3 : RPCMNT_VER1,
571 1.29 fvdl IPPROTO_UDP, &mdsin->sin_port);
572 1.29 fvdl if (error)
573 1.29 fvdl return error;
574 1.11 gwr
575 1.16 gwr m = xdr_string_encode(path, strlen(path));
576 1.22 pk if (m == NULL)
577 1.25 fvdl return ENOMEM;
578 1.3 gwr
579 1.3 gwr /* Do RPC to mountd. */
580 1.29 fvdl error = krpc_call(mdsin, RPCPROG_MNT, (argp->flags & NFSMNT_NFSV3) ?
581 1.29 fvdl RPCMNT_VER3 : RPCMNT_VER1, RPCMNT_MOUNT, &m, NULL);
582 1.29 fvdl if (error) {
583 1.29 fvdl if ((error==RPC_PROGMISMATCH) && (argp->flags & NFSMNT_NFSV3)) {
584 1.29 fvdl argp->flags &= ~NFSMNT_NFSV3;
585 1.29 fvdl goto retry;
586 1.29 fvdl }
587 1.3 gwr return error; /* message already freed */
588 1.29 fvdl }
589 1.1 glass
590 1.23 gwr /* The reply might have only the errno. */
591 1.23 gwr if (m->m_len < 4)
592 1.23 gwr goto bad;
593 1.23 gwr /* Have at least errno, so check that. */
594 1.23 gwr rdata = mtod(m, struct rdata *);
595 1.23 gwr error = fxdr_unsigned(u_int32_t, rdata->errno);
596 1.23 gwr if (error)
597 1.23 gwr goto out;
598 1.23 gwr
599 1.25 fvdl /* Have errno==0, so the fh must be there. */
600 1.29 fvdl if (argp->flags & NFSMNT_NFSV3){
601 1.29 fvdl argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
602 1.29 fvdl if (argp->fhsize > NFSX_V3FHMAX)
603 1.29 fvdl goto bad;
604 1.29 fvdl minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
605 1.29 fvdl } else {
606 1.29 fvdl argp->fhsize = NFSX_V2FH;
607 1.29 fvdl minlen = sizeof(u_int32_t) + argp->fhsize;
608 1.29 fvdl }
609 1.29 fvdl
610 1.29 fvdl if (m->m_len < minlen) {
611 1.29 fvdl m = m_pullup(m, minlen);
612 1.16 gwr if (m == NULL)
613 1.29 fvdl return(EBADRPC);
614 1.23 gwr rdata = mtod(m, struct rdata *);
615 1.16 gwr }
616 1.29 fvdl
617 1.29 fvdl fh = (argp->flags & NFSMNT_NFSV3) ? rdata->fh.v3fh.fh : rdata->fh.v2fh;
618 1.29 fvdl bcopy(fh, argp->fh, argp->fhsize);
619 1.29 fvdl
620 1.3 gwr goto out;
621 1.1 glass
622 1.6 deraadt bad:
623 1.3 gwr error = EBADRPC;
624 1.1 glass
625 1.6 deraadt out:
626 1.3 gwr m_freem(m);
627 1.3 gwr return error;
628 1.7 paulus }
629 1.7 paulus
630 1.7 paulus #endif /* NETHER */
631