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