nfs_boot.c revision 1.3 1 1.1 glass /*
2 1.1 glass * Copyright (c) 1994 Adam Glass, Gordon Ross
3 1.1 glass * All rights reserved.
4 1.1 glass *
5 1.1 glass * Redistribution and use in source and binary forms, with or without
6 1.1 glass * modification, are permitted provided that the following conditions
7 1.1 glass * are met:
8 1.1 glass * 1. Redistributions of source code must retain the above copyright
9 1.1 glass * notice, this list of conditions and the following disclaimer.
10 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 glass * notice, this list of conditions and the following disclaimer in the
12 1.1 glass * documentation and/or other materials provided with the distribution.
13 1.3 gwr * 3. The name of the authors may not be used to endorse or promote products
14 1.1 glass * derived from this software without specific prior written permission.
15 1.1 glass *
16 1.3 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 1.3 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.3 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.3 gwr * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.3 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.3 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.3 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.3 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.3 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.3 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 glass *
27 1.3 gwr * $Id: nfs_boot.c,v 1.3 1994/06/13 15:28:59 gwr Exp $
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.3 gwr int error;
229 1.1 glass
230 1.3 gwr /*
231 1.3 gwr * Get server:pathname for "key" (root or swap)
232 1.3 gwr * using RPC to bootparam/getfile
233 1.3 gwr */
234 1.3 gwr error = bp_getfile(bpsin, key,
235 1.3 gwr &ndmntp->ndm_saddr,
236 1.3 gwr ndmntp->ndm_host,
237 1.3 gwr pathname);
238 1.3 gwr if (error)
239 1.3 gwr panic("nfs_boot: bootparam get %s: %d", key, error);
240 1.3 gwr printf("%s on %s:%s\n", key, ndmntp->ndm_host, pathname);
241 1.1 glass
242 1.3 gwr /*
243 1.3 gwr * Get file handle for "key" (root or swap)
244 1.3 gwr * using RPC to mountd/mount
245 1.3 gwr */
246 1.3 gwr error = md_mount(&ndmntp->ndm_saddr,
247 1.3 gwr pathname,
248 1.3 gwr ndmntp->ndm_fh);
249 1.3 gwr if (error)
250 1.3 gwr panic("nfs_boot: mountd %s, error=%d", key, error);
251 1.1 glass }
252 1.1 glass
253 1.3 gwr
254 1.1 glass /*
255 1.3 gwr * Get an mbuf with the given length, and
256 1.3 gwr * initialize the pkthdr length field.
257 1.1 glass */
258 1.3 gwr static struct mbuf *
259 1.3 gwr m_get_len(int msg_len)
260 1.1 glass {
261 1.1 glass struct mbuf *m;
262 1.3 gwr m = m_gethdr(M_WAIT, MT_DATA);
263 1.3 gwr if (m == NULL)
264 1.3 gwr return NULL;
265 1.3 gwr if (msg_len > MHLEN) {
266 1.3 gwr if (msg_len > MCLBYTES)
267 1.3 gwr panic("nfs_boot: msg_len > MCLBYTES");
268 1.3 gwr MCLGET(m, M_WAIT);
269 1.1 glass if (m == NULL)
270 1.3 gwr return NULL;
271 1.1 glass }
272 1.3 gwr m->m_len = msg_len;
273 1.3 gwr m->m_pkthdr.len = m->m_len;
274 1.3 gwr return (m);
275 1.1 glass }
276 1.1 glass
277 1.3 gwr
278 1.3 gwr /*
279 1.3 gwr * String representation for RPC.
280 1.3 gwr */
281 1.3 gwr struct rpc_string {
282 1.3 gwr u_long len; /* length without null or padding */
283 1.3 gwr u_char data[4]; /* data (longer, of course) */
284 1.3 gwr /* data is padded to a long-word boundary */
285 1.3 gwr };
286 1.3 gwr /* Compute space used given string length. */
287 1.3 gwr #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
288 1.3 gwr
289 1.3 gwr /*
290 1.3 gwr * Inet address in RPC messages
291 1.3 gwr * (Note, really four longs, NOT chars. Blech.)
292 1.3 gwr */
293 1.3 gwr struct bp_inaddr {
294 1.3 gwr u_long atype;
295 1.3 gwr long addr[4];
296 1.3 gwr };
297 1.3 gwr
298 1.3 gwr
299 1.1 glass /*
300 1.3 gwr * RPC definitions for bootparamd
301 1.3 gwr * (XXX - move to a header file?)
302 1.1 glass */
303 1.3 gwr #define BOOTPARAM_PROG 100026
304 1.3 gwr #define BOOTPARAM_VERS 1
305 1.3 gwr #define BOOTPARAM_WHOAMI 1
306 1.3 gwr #define BOOTPARAM_GETFILE 2
307 1.3 gwr
308 1.3 gwr
309 1.3 gwr /*
310 1.3 gwr * RPC: bootparam/whoami
311 1.3 gwr * Given client IP address, get:
312 1.3 gwr * client name (hostname)
313 1.3 gwr * domain name (domainname)
314 1.3 gwr * gateway address
315 1.3 gwr *
316 1.3 gwr * Setting the hostname and domainname here may be somewhat
317 1.3 gwr * controvercial, but it is so easy to do it here. -gwr
318 1.3 gwr */
319 1.3 gwr static int
320 1.3 gwr bp_whoami(struct sockaddr_in *bpsin,
321 1.3 gwr struct in_addr *my_ip,
322 1.3 gwr struct in_addr *gw_ip)
323 1.1 glass {
324 1.3 gwr /* The RPC structures */
325 1.3 gwr struct bp_inaddr *bia;
326 1.3 gwr struct rpc_string *str;
327 1.1 glass struct mbuf *m;
328 1.3 gwr struct sockaddr_in *sin;
329 1.3 gwr int error, msg_len;
330 1.3 gwr int cn_len, dn_len;
331 1.3 gwr u_char *p;
332 1.1 glass
333 1.3 gwr /*
334 1.3 gwr * Get message buffer of sufficient size.
335 1.3 gwr */
336 1.3 gwr msg_len = sizeof(*bia);
337 1.3 gwr m = m_get_len(msg_len);
338 1.3 gwr if (m == NULL)
339 1.3 gwr return ENOBUFS;
340 1.1 glass
341 1.3 gwr /*
342 1.3 gwr * Build request message.
343 1.3 gwr */
344 1.3 gwr /* client IP address */
345 1.3 gwr bia = mtod(m, struct bp_inaddr *);
346 1.3 gwr bia->atype = htonl(1);
347 1.3 gwr p = (u_char*)my_ip; /* ugh! */
348 1.3 gwr bia->addr[0] = htonl(*p); p++;
349 1.3 gwr bia->addr[1] = htonl(*p); p++;
350 1.3 gwr bia->addr[2] = htonl(*p); p++;
351 1.3 gwr bia->addr[3] = htonl(*p); p++;
352 1.3 gwr
353 1.3 gwr /* RPC: bootparam/whoami */
354 1.3 gwr error = krpc_call((struct sockaddr *)bpsin,
355 1.3 gwr BOOTPARAM_PROG, BOOTPARAM_VERS,
356 1.3 gwr BOOTPARAM_WHOAMI, &m);
357 1.1 glass if (error)
358 1.1 glass return error;
359 1.3 gwr
360 1.3 gwr /*
361 1.3 gwr * Parse result message.
362 1.3 gwr */
363 1.3 gwr msg_len = m->m_len;
364 1.3 gwr p = mtod(m, char *);
365 1.3 gwr
366 1.3 gwr /* client name */
367 1.3 gwr if (msg_len < sizeof(*str))
368 1.3 gwr goto bad;
369 1.3 gwr str = (struct rpc_string *)p;
370 1.3 gwr cn_len = ntohl(str->len);
371 1.3 gwr if (msg_len < cn_len)
372 1.3 gwr goto bad;
373 1.3 gwr if (cn_len >= MAXHOSTNAMELEN)
374 1.3 gwr goto bad;
375 1.3 gwr bcopy(str->data, hostname, cn_len);
376 1.3 gwr hostname[cn_len] = '\0';
377 1.3 gwr hostnamelen = cn_len;
378 1.3 gwr p += RPC_STR_SIZE(cn_len);
379 1.3 gwr msg_len -= RPC_STR_SIZE(cn_len);
380 1.3 gwr
381 1.3 gwr /* domain name */
382 1.3 gwr if (msg_len < sizeof(*str))
383 1.3 gwr goto bad;
384 1.3 gwr str = (struct rpc_string *)p;
385 1.3 gwr dn_len = ntohl(str->len);
386 1.3 gwr if (msg_len < dn_len)
387 1.3 gwr goto bad;
388 1.3 gwr if (dn_len >= MAXHOSTNAMELEN)
389 1.3 gwr goto bad;
390 1.3 gwr bcopy(str->data, domainname, dn_len);
391 1.3 gwr domainname[dn_len] = '\0';
392 1.3 gwr domainnamelen = dn_len;
393 1.3 gwr p += RPC_STR_SIZE(dn_len);
394 1.3 gwr msg_len -= RPC_STR_SIZE(dn_len);
395 1.3 gwr
396 1.3 gwr /* gateway address */
397 1.3 gwr if (msg_len < sizeof(*bia))
398 1.3 gwr goto bad;
399 1.3 gwr bia = (struct bp_inaddr *)p;
400 1.3 gwr if (bia->atype != htonl(1))
401 1.3 gwr goto bad;
402 1.3 gwr p = (u_char*)gw_ip;
403 1.3 gwr *p++ = ntohl(bia->addr[0]);
404 1.3 gwr *p++ = ntohl(bia->addr[1]);
405 1.3 gwr *p++ = ntohl(bia->addr[2]);
406 1.3 gwr *p++ = ntohl(bia->addr[3]);
407 1.3 gwr goto out;
408 1.3 gwr
409 1.3 gwr bad:
410 1.3 gwr printf("nfs_boot: bootparam_whoami: bad reply\n");
411 1.3 gwr error = EBADRPC;
412 1.3 gwr
413 1.3 gwr out:
414 1.3 gwr m_freem(m);
415 1.3 gwr return(error);
416 1.1 glass }
417 1.1 glass
418 1.3 gwr
419 1.1 glass /*
420 1.3 gwr * RPC: bootparam/getfile
421 1.3 gwr * Given client name and file "key", get:
422 1.3 gwr * server name
423 1.3 gwr * server IP address
424 1.3 gwr * server pathname
425 1.1 glass */
426 1.3 gwr static int
427 1.3 gwr bp_getfile(struct sockaddr_in *bpsin, char *key,
428 1.3 gwr struct sockaddr_in *md_sin, char *serv_name, char *pathname)
429 1.1 glass {
430 1.3 gwr struct rpc_string *str;
431 1.3 gwr struct mbuf *m;
432 1.3 gwr struct bp_inaddr *bia;
433 1.1 glass struct sockaddr_in *sin;
434 1.3 gwr u_char *p, *q;
435 1.3 gwr int error, msg_len;
436 1.3 gwr int cn_len, key_len, sn_len, path_len;
437 1.1 glass
438 1.3 gwr /*
439 1.3 gwr * Get message buffer of sufficient size.
440 1.3 gwr */
441 1.3 gwr cn_len = hostnamelen;
442 1.3 gwr key_len = strlen(key);
443 1.3 gwr msg_len = 0;
444 1.3 gwr msg_len += RPC_STR_SIZE(cn_len);
445 1.3 gwr msg_len += RPC_STR_SIZE(key_len);
446 1.3 gwr m = m_get_len(msg_len);
447 1.3 gwr if (m == NULL)
448 1.3 gwr return ENOBUFS;
449 1.1 glass
450 1.1 glass /*
451 1.3 gwr * Build request message.
452 1.1 glass */
453 1.3 gwr p = mtod(m, u_char *);
454 1.3 gwr bzero(p, msg_len);
455 1.3 gwr /* client name (hostname) */
456 1.3 gwr str = (struct rpc_string *)p;
457 1.3 gwr str->len = htonl(cn_len);
458 1.3 gwr bcopy(hostname, str->data, cn_len);
459 1.3 gwr p += RPC_STR_SIZE(cn_len);
460 1.3 gwr /* key name (root or swap) */
461 1.3 gwr str = (struct rpc_string *)p;
462 1.3 gwr str->len = htonl(key_len);
463 1.3 gwr bcopy(key, str->data, key_len);
464 1.3 gwr
465 1.3 gwr /* RPC: bootparam/getfile */
466 1.3 gwr error = krpc_call((struct sockaddr *)bpsin,
467 1.3 gwr BOOTPARAM_PROG, BOOTPARAM_VERS,
468 1.3 gwr BOOTPARAM_GETFILE, &m);
469 1.3 gwr if (error)
470 1.1 glass return error;
471 1.3 gwr
472 1.3 gwr /*
473 1.3 gwr * Parse result message.
474 1.3 gwr */
475 1.3 gwr p = mtod(m, u_char *);
476 1.3 gwr msg_len = m->m_len;
477 1.3 gwr
478 1.3 gwr /* server name */
479 1.3 gwr if (msg_len < sizeof(*str))
480 1.3 gwr goto bad;
481 1.3 gwr str = (struct rpc_string *)p;
482 1.3 gwr sn_len = ntohl(str->len);
483 1.3 gwr if (msg_len < sn_len)
484 1.3 gwr goto bad;
485 1.3 gwr if (sn_len >= MNAMELEN)
486 1.3 gwr goto bad;
487 1.3 gwr bcopy(str->data, serv_name, sn_len);
488 1.3 gwr serv_name[sn_len] = '\0';
489 1.3 gwr p += RPC_STR_SIZE(sn_len);
490 1.3 gwr msg_len -= RPC_STR_SIZE(sn_len);
491 1.3 gwr
492 1.3 gwr /* server IP address (mountd) */
493 1.3 gwr if (msg_len < sizeof(*bia))
494 1.3 gwr goto bad;
495 1.3 gwr bia = (struct bp_inaddr *)p;
496 1.3 gwr if (bia->atype != htonl(1))
497 1.3 gwr goto bad;
498 1.3 gwr sin = md_sin;
499 1.3 gwr sin->sin_len = sizeof(*sin);
500 1.1 glass sin->sin_family = AF_INET;
501 1.3 gwr q = (u_char*) &sin->sin_addr;
502 1.3 gwr *q++ = ntohl(bia->addr[0]);
503 1.3 gwr *q++ = ntohl(bia->addr[1]);
504 1.3 gwr *q++ = ntohl(bia->addr[2]);
505 1.3 gwr *q++ = ntohl(bia->addr[3]);
506 1.3 gwr p += sizeof(*bia);
507 1.3 gwr msg_len -= sizeof(*bia);
508 1.3 gwr
509 1.3 gwr /* server pathname */
510 1.3 gwr if (msg_len < sizeof(*str))
511 1.3 gwr goto bad;
512 1.3 gwr str = (struct rpc_string *)p;
513 1.3 gwr path_len = ntohl(str->len);
514 1.3 gwr if (msg_len < path_len)
515 1.3 gwr goto bad;
516 1.3 gwr if (path_len >= MAXPATHLEN)
517 1.3 gwr goto bad;
518 1.3 gwr bcopy(str->data, pathname, path_len);
519 1.3 gwr pathname[path_len] = '\0';
520 1.3 gwr goto out;
521 1.3 gwr
522 1.3 gwr bad:
523 1.3 gwr printf("nfs_boot: bootparam_getfile: bad reply\n");
524 1.3 gwr error = EBADRPC;
525 1.1 glass
526 1.1 glass out:
527 1.3 gwr m_freem(m);
528 1.3 gwr return(0);
529 1.3 gwr }
530 1.1 glass
531 1.1 glass
532 1.1 glass /*
533 1.3 gwr * RPC: mountd/mount
534 1.3 gwr * Given a server pathname, get an NFS file handle.
535 1.3 gwr * Also, sets sin->sin_port to the NFS service port.
536 1.1 glass */
537 1.3 gwr static int
538 1.3 gwr md_mount(mdsin, path, fhp)
539 1.3 gwr struct sockaddr_in *mdsin; /* mountd server address */
540 1.3 gwr char *path;
541 1.3 gwr u_char *fhp;
542 1.1 glass {
543 1.3 gwr /* The RPC structures */
544 1.3 gwr struct rpc_string *str;
545 1.3 gwr struct rdata {
546 1.3 gwr u_long errno;
547 1.3 gwr u_char fh[NFS_FHSIZE];
548 1.3 gwr } *rdata;
549 1.3 gwr struct mbuf *m;
550 1.3 gwr int error, mlen, slen;
551 1.3 gwr
552 1.3 gwr slen = strlen(path);
553 1.3 gwr mlen = RPC_STR_SIZE(slen);
554 1.3 gwr
555 1.3 gwr m = m_get_len(mlen);
556 1.3 gwr if (m == NULL)
557 1.3 gwr return ENOBUFS;
558 1.3 gwr str = mtod(m, struct rpc_string *);
559 1.3 gwr str->len = htonl(slen);
560 1.3 gwr bcopy(path, str->data, slen);
561 1.3 gwr
562 1.3 gwr /* Do RPC to mountd. */
563 1.3 gwr error = krpc_call((struct sockaddr *)mdsin,
564 1.3 gwr RPCPROG_MNT, RPCMNT_VER1,
565 1.3 gwr RPCMNT_MOUNT, &m);
566 1.3 gwr if (error)
567 1.3 gwr return error; /* message already freed */
568 1.1 glass
569 1.3 gwr mlen = m->m_len;
570 1.3 gwr if (mlen < sizeof(*rdata))
571 1.3 gwr goto bad;
572 1.3 gwr rdata = mtod(m, struct rdata *);
573 1.3 gwr error = ntohl(rdata->errno);
574 1.1 glass if (error)
575 1.3 gwr goto bad;
576 1.3 gwr bcopy(rdata->fh, fhp, NFS_FHSIZE);
577 1.1 glass
578 1.3 gwr /* Set port number for NFS use. */
579 1.3 gwr error = krpc_portmap((struct sockaddr *)mdsin,
580 1.3 gwr NFS_PROG, NFS_VER2,
581 1.3 gwr &mdsin->sin_port);
582 1.3 gwr goto out;
583 1.1 glass
584 1.3 gwr bad:
585 1.3 gwr error = EBADRPC;
586 1.1 glass
587 1.3 gwr out:
588 1.3 gwr m_freem(m);
589 1.3 gwr return error;
590 1.1 glass }
591