nfs_boot.c revision 1.13 1 1.13 pk /* $NetBSD: nfs_boot.c,v 1.13 1995/02/16 21:43:15 pk Exp $ */
2 1.5 cgd
3 1.1 glass /*
4 1.1 glass * Copyright (c) 1994 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.1 glass #include <sys/socket.h>
39 1.1 glass #include <sys/reboot.h>
40 1.1 glass
41 1.1 glass #include <net/if.h>
42 1.3 gwr #include <net/route.h>
43 1.3 gwr
44 1.1 glass #include <netinet/in.h>
45 1.3 gwr #include <netinet/if_ether.h>
46 1.1 glass
47 1.1 glass #include <nfs/rpcv2.h>
48 1.1 glass #include <nfs/nfsv2.h>
49 1.1 glass #include <nfs/nfs.h>
50 1.1 glass #include <nfs/nfsdiskless.h>
51 1.11 gwr #include <nfs/krpc.h>
52 1.1 glass
53 1.7 paulus #include "ether.h"
54 1.11 gwr #if NETHER == 0
55 1.11 gwr
56 1.11 gwr int nfs_boot_init(nd, procp)
57 1.11 gwr struct nfs_diskless *nd;
58 1.11 gwr struct proc *procp;
59 1.11 gwr {
60 1.11 gwr panic("nfs_boot_init: no ether");
61 1.11 gwr }
62 1.11 gwr
63 1.11 gwr #else /* NETHER */
64 1.7 paulus
65 1.1 glass /*
66 1.1 glass * Support for NFS diskless booting, specifically getting information
67 1.1 glass * about where to boot from, what pathnames, etc.
68 1.1 glass *
69 1.3 gwr * This implememtation uses RARP and the bootparam RPC.
70 1.3 gwr * We are forced to implement RPC anyway (to get file handles)
71 1.3 gwr * so we might as well take advantage of it for bootparam too.
72 1.1 glass *
73 1.3 gwr * The diskless boot sequence goes as follows:
74 1.3 gwr * (1) Get our interface address using RARP
75 1.3 gwr * (also save the address of the RARP server)
76 1.3 gwr * (2) Get our hostname using RPC/bootparam/whoami
77 1.3 gwr * (all boopararms RPCs to the RARP server)
78 1.3 gwr * (3) Get the root path using RPC/bootparam/getfile
79 1.3 gwr * (4) Get the root file handle using RPC/mountd
80 1.3 gwr * (5) Get the swap path using RPC/bootparam/getfile
81 1.3 gwr * (6) Get the swap file handle using RPC/mountd
82 1.1 glass *
83 1.3 gwr * (This happens to be the way Sun does it too.)
84 1.1 glass */
85 1.1 glass
86 1.3 gwr /* bootparam RPC */
87 1.6 deraadt static int bp_whoami __P((struct sockaddr_in *bpsin,
88 1.6 deraadt struct in_addr *my_ip, struct in_addr *gw_ip));
89 1.6 deraadt static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
90 1.6 deraadt struct sockaddr_in *mdsin, char *servname, char *path));
91 1.3 gwr
92 1.3 gwr /* mountd RPC */
93 1.6 deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
94 1.6 deraadt u_char *fh));
95 1.3 gwr
96 1.3 gwr /* other helpers */
97 1.6 deraadt static void get_path_and_handle __P((struct sockaddr_in *bpsin,
98 1.6 deraadt char *key, struct nfs_dlmount *ndmntp));
99 1.1 glass
100 1.13 pk char *nfsbootdevname;
101 1.13 pk
102 1.1 glass /*
103 1.3 gwr * Called with an empty nfs_diskless struct to be filled in.
104 1.1 glass */
105 1.6 deraadt int
106 1.6 deraadt nfs_boot_init(nd, procp)
107 1.3 gwr struct nfs_diskless *nd;
108 1.3 gwr struct proc *procp;
109 1.1 glass {
110 1.3 gwr struct ifreq ireq;
111 1.11 gwr struct in_addr my_ip, gw_ip;
112 1.3 gwr struct sockaddr_in bp_sin;
113 1.1 glass struct sockaddr_in *sin;
114 1.3 gwr struct ifnet *ifp;
115 1.3 gwr struct socket *so;
116 1.3 gwr int error, len;
117 1.3 gwr u_short port;
118 1.3 gwr
119 1.3 gwr #if 0
120 1.3 gwr /*
121 1.3 gwr * XXX time must be non-zero when we init the interface or else
122 1.3 gwr * the arp code will wedge... (Fixed in if_ether.c -gwr)
123 1.3 gwr */
124 1.3 gwr if (time.tv_sec == 0)
125 1.3 gwr time.tv_sec = 1;
126 1.3 gwr #endif
127 1.3 gwr
128 1.3 gwr /*
129 1.3 gwr * Find an interface, rarp for its ip address, stuff it, the
130 1.3 gwr * implied broadcast addr, and netmask into a nfs_diskless struct.
131 1.3 gwr *
132 1.3 gwr * This was moved here from nfs_vfsops.c because this procedure
133 1.3 gwr * would be quite different if someone decides to write (i.e.) a
134 1.3 gwr * BOOTP version of this file (might not use RARP, etc.) -gwr
135 1.3 gwr */
136 1.3 gwr
137 1.3 gwr /*
138 1.3 gwr * Find a network interface.
139 1.3 gwr */
140 1.13 pk if (nfsbootdevname)
141 1.13 pk ifp = ifunit(nfsbootdevname);
142 1.13 pk else
143 1.13 pk for (ifp = ifnet; ifp; ifp = ifp->if_next)
144 1.13 pk if ((ifp->if_flags &
145 1.13 pk (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
146 1.13 pk break;
147 1.3 gwr if (ifp == NULL)
148 1.3 gwr panic("nfs_boot: no suitable interface");
149 1.3 gwr sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
150 1.3 gwr printf("nfs_boot: using network interface '%s'\n",
151 1.6 deraadt ireq.ifr_name);
152 1.3 gwr
153 1.3 gwr /*
154 1.3 gwr * Bring up the interface.
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.3 gwr ireq.ifr_flags = IFF_UP;
159 1.3 gwr error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
160 1.6 deraadt if (error)
161 1.6 deraadt panic("nfs_boot: SIFFLAGS, error=%d", error);
162 1.3 gwr
163 1.3 gwr /*
164 1.3 gwr * Do RARP for the interface address. Also
165 1.3 gwr * save the server address for bootparam RPC.
166 1.3 gwr */
167 1.11 gwr if ((error = revarpwhoami(&my_ip, ifp)) != 0)
168 1.3 gwr panic("revarp failed, error=%d", error);
169 1.11 gwr printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
170 1.3 gwr
171 1.3 gwr /*
172 1.3 gwr * Do enough of ifconfig(8) so that the chosen interface can
173 1.3 gwr * talk to the server(s). (also get brcast addr and netmask)
174 1.3 gwr */
175 1.3 gwr /* Set interface address. */
176 1.3 gwr sin = (struct sockaddr_in *)&ireq.ifr_addr;
177 1.10 mycroft bzero((caddr_t)sin, sizeof(*sin));
178 1.3 gwr sin->sin_len = sizeof(*sin);
179 1.3 gwr sin->sin_family = AF_INET;
180 1.3 gwr sin->sin_addr.s_addr = my_ip.s_addr;
181 1.3 gwr error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
182 1.6 deraadt if (error)
183 1.6 deraadt panic("nfs_boot: set if addr, error=%d", error);
184 1.3 gwr
185 1.3 gwr soclose(so);
186 1.3 gwr
187 1.3 gwr /*
188 1.3 gwr * Get client name and gateway address.
189 1.3 gwr * RPC: bootparam/whoami
190 1.11 gwr * XXX - Using old broadcast addr. for WHOAMI,
191 1.11 gwr * then it is replaced with the BP server addr.
192 1.3 gwr */
193 1.10 mycroft bzero((caddr_t)&bp_sin, sizeof(bp_sin));
194 1.3 gwr bp_sin.sin_len = sizeof(bp_sin);
195 1.3 gwr bp_sin.sin_family = AF_INET;
196 1.11 gwr bp_sin.sin_addr.s_addr = ~0; /* XXX */
197 1.3 gwr hostnamelen = MAXHOSTNAMELEN;
198 1.6 deraadt
199 1.6 deraadt /* this returns gateway IP address */
200 1.6 deraadt error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
201 1.3 gwr if (error)
202 1.3 gwr panic("nfs_boot: bootparam whoami, error=%d", error);
203 1.11 gwr printf("nfs_boot: server_addr=0x%x\n",
204 1.11 gwr ntohl(bp_sin.sin_addr.s_addr));
205 1.3 gwr printf("nfs_boot: hostname=%s\n", hostname);
206 1.1 glass
207 1.3 gwr #ifdef NFS_BOOT_GATEWAY
208 1.1 glass /*
209 1.8 gwr * XXX - This code is conditionally compiled only because
210 1.8 gwr * many bootparam servers (in particular, SunOS 4.1.3)
211 1.8 gwr * always set the gateway address to their own address.
212 1.8 gwr * The bootparam server is not necessarily the gateway.
213 1.8 gwr * We could just believe the server, and at worst you would
214 1.8 gwr * need to delete the incorrect default route before adding
215 1.8 gwr * the correct one, but for simplicity, ignore the gateway.
216 1.3 gwr * If your server is OK, you can turn on this option.
217 1.3 gwr *
218 1.3 gwr * If the gateway address is set, add a default route.
219 1.3 gwr * (The mountd RPCs may go across a gateway.)
220 1.1 glass */
221 1.3 gwr if (gw_ip.s_addr) {
222 1.8 gwr struct sockaddr dst, gw, mask;
223 1.3 gwr /* Destination: (default) */
224 1.10 mycroft bzero((caddr_t)&dst, sizeof(dst));
225 1.8 gwr dst.sa_len = sizeof(dst);
226 1.8 gwr dst.sa_family = AF_INET;
227 1.3 gwr /* Gateway: */
228 1.10 mycroft bzero((caddr_t)&gw, sizeof(gw));
229 1.8 gwr sin = (struct sockaddr_in *)&gw;
230 1.8 gwr sin->sin_len = sizeof(gw);
231 1.8 gwr sin->sin_family = AF_INET;
232 1.8 gwr sin->sin_addr.s_addr = gw_ip.s_addr;
233 1.8 gwr /* Mask: (zero length) */
234 1.8 gwr bzero(&mask, sizeof(mask));
235 1.6 deraadt
236 1.8 gwr printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
237 1.3 gwr /* add, dest, gw, mask, flags, 0 */
238 1.8 gwr error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
239 1.8 gwr &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
240 1.3 gwr if (error)
241 1.3 gwr printf("nfs_boot: add route, error=%d\n", error);
242 1.3 gwr }
243 1.3 gwr #endif
244 1.3 gwr
245 1.3 gwr get_path_and_handle(&bp_sin, "root", &nd->nd_root);
246 1.3 gwr get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
247 1.1 glass
248 1.3 gwr return (0);
249 1.3 gwr }
250 1.1 glass
251 1.3 gwr static void
252 1.3 gwr get_path_and_handle(bpsin, key, ndmntp)
253 1.3 gwr struct sockaddr_in *bpsin; /* bootparam server */
254 1.6 deraadt char *key; /* root or swap */
255 1.6 deraadt struct nfs_dlmount *ndmntp; /* output */
256 1.3 gwr {
257 1.3 gwr char pathname[MAXPATHLEN];
258 1.4 pk char *sp, *dp, *endp;
259 1.3 gwr int error;
260 1.1 glass
261 1.3 gwr /*
262 1.3 gwr * Get server:pathname for "key" (root or swap)
263 1.3 gwr * using RPC to bootparam/getfile
264 1.3 gwr */
265 1.6 deraadt error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
266 1.6 deraadt ndmntp->ndm_host, pathname);
267 1.3 gwr if (error)
268 1.3 gwr panic("nfs_boot: bootparam get %s: %d", key, error);
269 1.3 gwr printf("%s on %s:%s\n", key, ndmntp->ndm_host, pathname);
270 1.1 glass
271 1.3 gwr /*
272 1.3 gwr * Get file handle for "key" (root or swap)
273 1.3 gwr * using RPC to mountd/mount
274 1.3 gwr */
275 1.6 deraadt error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
276 1.3 gwr if (error)
277 1.3 gwr panic("nfs_boot: mountd %s, error=%d", key, error);
278 1.4 pk
279 1.4 pk /* Construct remote path (for getmntinfo(3)) */
280 1.4 pk dp = ndmntp->ndm_host;
281 1.4 pk endp = dp + MNAMELEN - 1;
282 1.4 pk dp += strlen(dp);
283 1.4 pk *dp++ = ':';
284 1.4 pk for (sp = pathname; *sp && dp < endp;)
285 1.4 pk *dp++ = *sp++;
286 1.4 pk *dp = '\0';
287 1.6 deraadt
288 1.1 glass }
289 1.1 glass
290 1.3 gwr
291 1.1 glass /*
292 1.3 gwr * Get an mbuf with the given length, and
293 1.3 gwr * initialize the pkthdr length field.
294 1.1 glass */
295 1.3 gwr static struct mbuf *
296 1.3 gwr m_get_len(int msg_len)
297 1.1 glass {
298 1.1 glass struct mbuf *m;
299 1.3 gwr m = m_gethdr(M_WAIT, MT_DATA);
300 1.3 gwr if (m == NULL)
301 1.3 gwr return NULL;
302 1.3 gwr if (msg_len > MHLEN) {
303 1.3 gwr if (msg_len > MCLBYTES)
304 1.3 gwr panic("nfs_boot: msg_len > MCLBYTES");
305 1.3 gwr MCLGET(m, M_WAIT);
306 1.1 glass if (m == NULL)
307 1.3 gwr return NULL;
308 1.1 glass }
309 1.3 gwr m->m_len = msg_len;
310 1.3 gwr m->m_pkthdr.len = m->m_len;
311 1.3 gwr return (m);
312 1.1 glass }
313 1.1 glass
314 1.3 gwr
315 1.3 gwr /*
316 1.3 gwr * String representation for RPC.
317 1.3 gwr */
318 1.3 gwr struct rpc_string {
319 1.3 gwr u_long len; /* length without null or padding */
320 1.3 gwr u_char data[4]; /* data (longer, of course) */
321 1.3 gwr /* data is padded to a long-word boundary */
322 1.3 gwr };
323 1.3 gwr /* Compute space used given string length. */
324 1.3 gwr #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
325 1.3 gwr
326 1.3 gwr /*
327 1.3 gwr * Inet address in RPC messages
328 1.3 gwr * (Note, really four longs, NOT chars. Blech.)
329 1.3 gwr */
330 1.3 gwr struct bp_inaddr {
331 1.3 gwr u_long atype;
332 1.3 gwr long addr[4];
333 1.3 gwr };
334 1.3 gwr
335 1.3 gwr
336 1.1 glass /*
337 1.3 gwr * RPC: bootparam/whoami
338 1.3 gwr * Given client IP address, get:
339 1.3 gwr * client name (hostname)
340 1.3 gwr * domain name (domainname)
341 1.3 gwr * gateway address
342 1.3 gwr *
343 1.3 gwr * Setting the hostname and domainname here may be somewhat
344 1.3 gwr * controvercial, but it is so easy to do it here. -gwr
345 1.11 gwr *
346 1.11 gwr * Note - bpsin is initialized to the broadcast address,
347 1.11 gwr * and will be replaced with the bootparam server address
348 1.11 gwr * after this call is complete. Have to use PMAP_PROC_CALL
349 1.11 gwr * to make sure we get responses only from a servers that
350 1.11 gwr * know about us (don't want to broadcast a getport call).
351 1.3 gwr */
352 1.3 gwr static int
353 1.6 deraadt bp_whoami(bpsin, my_ip, gw_ip)
354 1.6 deraadt struct sockaddr_in *bpsin;
355 1.6 deraadt struct in_addr *my_ip;
356 1.6 deraadt struct in_addr *gw_ip;
357 1.1 glass {
358 1.11 gwr /* RPC structures for PMAPPROC_CALLIT */
359 1.11 gwr struct whoami_call {
360 1.11 gwr u_long call_prog;
361 1.11 gwr u_long call_vers;
362 1.11 gwr u_long call_proc;
363 1.11 gwr u_long call_arglen;
364 1.11 gwr struct bp_inaddr call_ia;
365 1.11 gwr } *call;
366 1.11 gwr
367 1.11 gwr struct rpc_string *str;
368 1.3 gwr struct bp_inaddr *bia;
369 1.11 gwr struct mbuf *m, *from;
370 1.3 gwr struct sockaddr_in *sin;
371 1.3 gwr int error, msg_len;
372 1.3 gwr int cn_len, dn_len;
373 1.3 gwr u_char *p;
374 1.11 gwr long *lp;
375 1.1 glass
376 1.3 gwr /*
377 1.3 gwr * Get message buffer of sufficient size.
378 1.3 gwr */
379 1.11 gwr msg_len = sizeof(*call);
380 1.3 gwr m = m_get_len(msg_len);
381 1.3 gwr if (m == NULL)
382 1.3 gwr return ENOBUFS;
383 1.1 glass
384 1.3 gwr /*
385 1.11 gwr * Build request message for PMAPPROC_CALLIT.
386 1.3 gwr */
387 1.11 gwr call = mtod(m, struct whoami_call *);
388 1.12 cgd call->call_prog = htonl(BOOTPARAM_PROG);
389 1.12 cgd call->call_vers = htonl(BOOTPARAM_VERS);
390 1.12 cgd call->call_proc = htonl(BOOTPARAM_WHOAMI);
391 1.12 cgd call->call_arglen = htonl(sizeof(struct bp_inaddr));
392 1.11 gwr
393 1.3 gwr /* client IP address */
394 1.11 gwr call->call_ia.atype = htonl(1);
395 1.11 gwr p = (u_char*)my_ip;
396 1.11 gwr lp = call->call_ia.addr;
397 1.11 gwr *lp++ = htonl(*p); p++;
398 1.11 gwr *lp++ = htonl(*p); p++;
399 1.11 gwr *lp++ = htonl(*p); p++;
400 1.11 gwr *lp++ = htonl(*p); p++;
401 1.11 gwr
402 1.11 gwr /* RPC: portmap/callit */
403 1.11 gwr bpsin->sin_port = htons(PMAPPORT);
404 1.11 gwr from = NULL;
405 1.11 gwr error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
406 1.11 gwr PMAPPROC_CALLIT, &m, &from);
407 1.1 glass if (error)
408 1.1 glass return error;
409 1.3 gwr
410 1.3 gwr /*
411 1.3 gwr * Parse result message.
412 1.3 gwr */
413 1.3 gwr msg_len = m->m_len;
414 1.11 gwr lp = mtod(m, long *);
415 1.11 gwr
416 1.11 gwr /* bootparam server port (also grab from address). */
417 1.11 gwr if (msg_len < sizeof(*lp))
418 1.11 gwr goto bad;
419 1.11 gwr msg_len -= sizeof(*lp);
420 1.12 cgd bpsin->sin_port = htons((short)ntohl(*lp++));
421 1.11 gwr sin = mtod(from, struct sockaddr_in *);
422 1.11 gwr bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
423 1.11 gwr
424 1.11 gwr /* length of encapsulated results */
425 1.12 cgd if (msg_len < (ntohl(*lp) + sizeof(*lp)))
426 1.11 gwr goto bad;
427 1.12 cgd msg_len = ntohl(*lp++);
428 1.11 gwr p = (char*)lp;
429 1.3 gwr
430 1.3 gwr /* client name */
431 1.3 gwr if (msg_len < sizeof(*str))
432 1.3 gwr goto bad;
433 1.3 gwr str = (struct rpc_string *)p;
434 1.3 gwr cn_len = ntohl(str->len);
435 1.3 gwr if (msg_len < cn_len)
436 1.3 gwr goto bad;
437 1.3 gwr if (cn_len >= MAXHOSTNAMELEN)
438 1.3 gwr goto bad;
439 1.3 gwr bcopy(str->data, hostname, cn_len);
440 1.3 gwr hostname[cn_len] = '\0';
441 1.3 gwr hostnamelen = cn_len;
442 1.3 gwr p += RPC_STR_SIZE(cn_len);
443 1.3 gwr msg_len -= RPC_STR_SIZE(cn_len);
444 1.3 gwr
445 1.3 gwr /* domain name */
446 1.3 gwr if (msg_len < sizeof(*str))
447 1.3 gwr goto bad;
448 1.3 gwr str = (struct rpc_string *)p;
449 1.3 gwr dn_len = ntohl(str->len);
450 1.3 gwr if (msg_len < dn_len)
451 1.3 gwr goto bad;
452 1.3 gwr if (dn_len >= MAXHOSTNAMELEN)
453 1.3 gwr goto bad;
454 1.3 gwr bcopy(str->data, domainname, dn_len);
455 1.3 gwr domainname[dn_len] = '\0';
456 1.3 gwr domainnamelen = dn_len;
457 1.3 gwr p += RPC_STR_SIZE(dn_len);
458 1.3 gwr msg_len -= RPC_STR_SIZE(dn_len);
459 1.3 gwr
460 1.3 gwr /* gateway address */
461 1.3 gwr if (msg_len < sizeof(*bia))
462 1.3 gwr goto bad;
463 1.3 gwr bia = (struct bp_inaddr *)p;
464 1.3 gwr if (bia->atype != htonl(1))
465 1.3 gwr goto bad;
466 1.3 gwr p = (u_char*)gw_ip;
467 1.3 gwr *p++ = ntohl(bia->addr[0]);
468 1.3 gwr *p++ = ntohl(bia->addr[1]);
469 1.3 gwr *p++ = ntohl(bia->addr[2]);
470 1.3 gwr *p++ = ntohl(bia->addr[3]);
471 1.3 gwr goto out;
472 1.3 gwr
473 1.6 deraadt bad:
474 1.3 gwr printf("nfs_boot: bootparam_whoami: bad reply\n");
475 1.3 gwr error = EBADRPC;
476 1.3 gwr
477 1.6 deraadt out:
478 1.11 gwr if (from)
479 1.11 gwr m_freem(from);
480 1.3 gwr m_freem(m);
481 1.3 gwr return(error);
482 1.1 glass }
483 1.1 glass
484 1.3 gwr
485 1.1 glass /*
486 1.3 gwr * RPC: bootparam/getfile
487 1.3 gwr * Given client name and file "key", get:
488 1.3 gwr * server name
489 1.3 gwr * server IP address
490 1.3 gwr * server pathname
491 1.1 glass */
492 1.3 gwr static int
493 1.6 deraadt bp_getfile(bpsin, key, md_sin, serv_name, pathname)
494 1.6 deraadt struct sockaddr_in *bpsin;
495 1.6 deraadt char *key;
496 1.6 deraadt struct sockaddr_in *md_sin;
497 1.6 deraadt char *serv_name;
498 1.6 deraadt char *pathname;
499 1.1 glass {
500 1.3 gwr struct rpc_string *str;
501 1.3 gwr struct mbuf *m;
502 1.3 gwr struct bp_inaddr *bia;
503 1.1 glass struct sockaddr_in *sin;
504 1.3 gwr u_char *p, *q;
505 1.3 gwr int error, msg_len;
506 1.3 gwr int cn_len, key_len, sn_len, path_len;
507 1.1 glass
508 1.3 gwr /*
509 1.3 gwr * Get message buffer of sufficient size.
510 1.3 gwr */
511 1.3 gwr cn_len = hostnamelen;
512 1.3 gwr key_len = strlen(key);
513 1.3 gwr msg_len = 0;
514 1.3 gwr msg_len += RPC_STR_SIZE(cn_len);
515 1.3 gwr msg_len += RPC_STR_SIZE(key_len);
516 1.3 gwr m = m_get_len(msg_len);
517 1.3 gwr if (m == NULL)
518 1.3 gwr return ENOBUFS;
519 1.1 glass
520 1.1 glass /*
521 1.3 gwr * Build request message.
522 1.1 glass */
523 1.3 gwr p = mtod(m, u_char *);
524 1.3 gwr bzero(p, msg_len);
525 1.3 gwr /* client name (hostname) */
526 1.3 gwr str = (struct rpc_string *)p;
527 1.3 gwr str->len = htonl(cn_len);
528 1.3 gwr bcopy(hostname, str->data, cn_len);
529 1.3 gwr p += RPC_STR_SIZE(cn_len);
530 1.3 gwr /* key name (root or swap) */
531 1.3 gwr str = (struct rpc_string *)p;
532 1.3 gwr str->len = htonl(key_len);
533 1.3 gwr bcopy(key, str->data, key_len);
534 1.3 gwr
535 1.3 gwr /* RPC: bootparam/getfile */
536 1.11 gwr error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
537 1.11 gwr BOOTPARAM_GETFILE, &m, NULL);
538 1.3 gwr if (error)
539 1.1 glass return error;
540 1.3 gwr
541 1.3 gwr /*
542 1.3 gwr * Parse result message.
543 1.3 gwr */
544 1.3 gwr p = mtod(m, u_char *);
545 1.3 gwr msg_len = m->m_len;
546 1.3 gwr
547 1.3 gwr /* server name */
548 1.3 gwr if (msg_len < sizeof(*str))
549 1.3 gwr goto bad;
550 1.3 gwr str = (struct rpc_string *)p;
551 1.3 gwr sn_len = ntohl(str->len);
552 1.3 gwr if (msg_len < sn_len)
553 1.3 gwr goto bad;
554 1.3 gwr if (sn_len >= MNAMELEN)
555 1.3 gwr goto bad;
556 1.3 gwr bcopy(str->data, serv_name, sn_len);
557 1.3 gwr serv_name[sn_len] = '\0';
558 1.3 gwr p += RPC_STR_SIZE(sn_len);
559 1.3 gwr msg_len -= RPC_STR_SIZE(sn_len);
560 1.3 gwr
561 1.3 gwr /* server IP address (mountd) */
562 1.3 gwr if (msg_len < sizeof(*bia))
563 1.3 gwr goto bad;
564 1.3 gwr bia = (struct bp_inaddr *)p;
565 1.3 gwr if (bia->atype != htonl(1))
566 1.3 gwr goto bad;
567 1.3 gwr sin = md_sin;
568 1.10 mycroft bzero((caddr_t)sin, sizeof(*sin));
569 1.3 gwr sin->sin_len = sizeof(*sin);
570 1.1 glass sin->sin_family = AF_INET;
571 1.3 gwr q = (u_char*) &sin->sin_addr;
572 1.3 gwr *q++ = ntohl(bia->addr[0]);
573 1.3 gwr *q++ = ntohl(bia->addr[1]);
574 1.3 gwr *q++ = ntohl(bia->addr[2]);
575 1.3 gwr *q++ = ntohl(bia->addr[3]);
576 1.3 gwr p += sizeof(*bia);
577 1.3 gwr msg_len -= sizeof(*bia);
578 1.3 gwr
579 1.3 gwr /* server pathname */
580 1.3 gwr if (msg_len < sizeof(*str))
581 1.3 gwr goto bad;
582 1.3 gwr str = (struct rpc_string *)p;
583 1.3 gwr path_len = ntohl(str->len);
584 1.3 gwr if (msg_len < path_len)
585 1.3 gwr goto bad;
586 1.3 gwr if (path_len >= MAXPATHLEN)
587 1.3 gwr goto bad;
588 1.3 gwr bcopy(str->data, pathname, path_len);
589 1.3 gwr pathname[path_len] = '\0';
590 1.3 gwr goto out;
591 1.3 gwr
592 1.6 deraadt bad:
593 1.3 gwr printf("nfs_boot: bootparam_getfile: bad reply\n");
594 1.3 gwr error = EBADRPC;
595 1.1 glass
596 1.6 deraadt out:
597 1.3 gwr m_freem(m);
598 1.3 gwr return(0);
599 1.3 gwr }
600 1.1 glass
601 1.1 glass
602 1.1 glass /*
603 1.3 gwr * RPC: mountd/mount
604 1.3 gwr * Given a server pathname, get an NFS file handle.
605 1.3 gwr * Also, sets sin->sin_port to the NFS service port.
606 1.1 glass */
607 1.3 gwr static int
608 1.3 gwr md_mount(mdsin, path, fhp)
609 1.3 gwr struct sockaddr_in *mdsin; /* mountd server address */
610 1.3 gwr char *path;
611 1.3 gwr u_char *fhp;
612 1.1 glass {
613 1.3 gwr /* The RPC structures */
614 1.3 gwr struct rpc_string *str;
615 1.3 gwr struct rdata {
616 1.3 gwr u_long errno;
617 1.3 gwr u_char fh[NFS_FHSIZE];
618 1.3 gwr } *rdata;
619 1.3 gwr struct mbuf *m;
620 1.3 gwr int error, mlen, slen;
621 1.3 gwr
622 1.11 gwr /* Get port number for MOUNTD. */
623 1.11 gwr error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
624 1.11 gwr &mdsin->sin_port);
625 1.11 gwr if (error) return error;
626 1.11 gwr
627 1.3 gwr slen = strlen(path);
628 1.3 gwr mlen = RPC_STR_SIZE(slen);
629 1.3 gwr
630 1.3 gwr m = m_get_len(mlen);
631 1.3 gwr if (m == NULL)
632 1.3 gwr return ENOBUFS;
633 1.3 gwr str = mtod(m, struct rpc_string *);
634 1.3 gwr str->len = htonl(slen);
635 1.3 gwr bcopy(path, str->data, slen);
636 1.3 gwr
637 1.3 gwr /* Do RPC to mountd. */
638 1.11 gwr error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
639 1.11 gwr RPCMNT_MOUNT, &m, NULL);
640 1.6 deraadt if (error)
641 1.3 gwr return error; /* message already freed */
642 1.1 glass
643 1.3 gwr mlen = m->m_len;
644 1.3 gwr if (mlen < sizeof(*rdata))
645 1.3 gwr goto bad;
646 1.3 gwr rdata = mtod(m, struct rdata *);
647 1.3 gwr error = ntohl(rdata->errno);
648 1.1 glass if (error)
649 1.3 gwr goto bad;
650 1.3 gwr bcopy(rdata->fh, fhp, NFS_FHSIZE);
651 1.1 glass
652 1.3 gwr /* Set port number for NFS use. */
653 1.11 gwr error = krpc_portmap(mdsin, NFS_PROG, NFS_VER2,
654 1.11 gwr &mdsin->sin_port);
655 1.3 gwr goto out;
656 1.1 glass
657 1.6 deraadt bad:
658 1.3 gwr error = EBADRPC;
659 1.1 glass
660 1.6 deraadt out:
661 1.3 gwr m_freem(m);
662 1.3 gwr return error;
663 1.7 paulus }
664 1.7 paulus
665 1.7 paulus #endif /* NETHER */
666