]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/nfsclient/nfs_diskless.c
MFC 216679:
[FreeBSD/releng/8.2.git] / sys / nfsclient / nfs_diskless.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)autoconf.c    7.1 (Berkeley) 5/9/91
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_bootp.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/socket.h>
46
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/if_var.h>
51 #include <net/ethernet.h>
52 #include <net/vnet.h>
53
54 #include <netinet/in.h>
55 #include <nfs/nfsproto.h>
56 #include <nfsclient/nfs.h>
57 #include <nfsclient/nfsdiskless.h>
58
59 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
60 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
61 static int decode_nfshandle(char *ev, u_char *fh, int maxfh);
62
63 /*
64  * Validate/sanity check a rsize/wsize parameter.
65  */
66 static int
67 checkrwsize(unsigned long v, const char *name)
68 {
69         /*
70          * 32K is used as an upper bound because most servers
71          * limit block size to satisfy IPv4's limit of
72          * 64K/reassembled packet.  The lower bound is pretty
73          * much arbitrary.
74          */
75         if (!(4 <= v && v <= 32*1024)) {
76                 printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
77                 return 0;
78         } else
79                 return 1;
80 }
81
82 /*
83  * Parse mount options and apply them to the supplied
84  * nfs_diskless state.  Used also by bootp/dhcp support.
85  */
86 void
87 nfs_parse_options(const char *envopts, struct nfs_args *nd)
88 {
89         char *opts, *o, *otmp;
90         unsigned long v;
91
92         opts = strdup(envopts, M_TEMP);
93         otmp = opts;
94         while ((o = strsep(&otmp, ":;, ")) != NULL) {
95                 if (*o == '\0')
96                         ; /* Skip empty options. */
97                 else if (strcmp(o, "soft") == 0)
98                         nd->flags |= NFSMNT_SOFT;
99                 else if (strcmp(o, "intr") == 0)
100                         nd->flags |= NFSMNT_INT;
101                 else if (strcmp(o, "conn") == 0)
102                         nd->flags |= NFSMNT_NOCONN;
103                 else if (strcmp(o, "nolockd") == 0)
104                         nd->flags |= NFSMNT_NOLOCKD;
105                 else if (strcmp(o, "nfsv2") == 0)
106                         nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4);
107                 else if (strcmp(o, "nfsv3") == 0) {
108                         nd->flags &= ~NFSMNT_NFSV4;
109                         nd->flags |= NFSMNT_NFSV3;
110                 } else if (strcmp(o, "tcp") == 0)
111                         nd->sotype = SOCK_STREAM;
112                 else if (strcmp(o, "udp") == 0)
113                         nd->sotype = SOCK_DGRAM;
114                 else if (strncmp(o, "rsize=", 6) == 0) {
115                         v = strtoul(o+6, NULL, 10);
116                         if (checkrwsize(v, "rsize")) {
117                                 nd->rsize = (int) v;
118                                 nd->flags |= NFSMNT_RSIZE;
119                         }
120                 } else if (strncmp(o, "wsize=", 6) == 0) {
121                         v = strtoul(o+6, NULL, 10);
122                         if (checkrwsize(v, "wsize")) {
123                                 nd->wsize = (int) v;
124                                 nd->flags |= NFSMNT_WSIZE;
125                         }
126                 } else
127                         printf("%s: skipping unknown option \"%s\"\n",
128                             __func__, o);
129         }
130         free(opts, M_TEMP);
131 }
132
133 /*
134  * Populate the essential fields in the nfsv3_diskless structure.
135  *
136  * The loader is expected to export the following environment variables:
137  *
138  * boot.netif.name              name of boot interface
139  * boot.netif.ip                IP address on boot interface
140  * boot.netif.netmask           netmask on boot interface
141  * boot.netif.gateway           default gateway (optional)
142  * boot.netif.hwaddr            hardware address of boot interface
143  * boot.nfsroot.server          IP address of root filesystem server
144  * boot.nfsroot.path            path of the root filesystem on server
145  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
146  * boot.nfsroot.nfshandlelen    and length of this handle (for NFSv3 only)
147  * boot.nfsroot.options         NFS options for the root filesystem
148  */
149 void
150 nfs_setup_diskless(void)
151 {
152         struct nfs_diskless *nd = &nfs_diskless;
153         struct nfsv3_diskless *nd3 = &nfsv3_diskless;
154         struct ifnet *ifp;
155         struct ifaddr *ifa;
156         struct sockaddr_dl *sdl, ourdl;
157         struct sockaddr_in myaddr, netmask;
158         char *cp;
159         int cnt, fhlen, is_nfsv3;
160         uint32_t len;
161
162         if (nfs_diskless_valid != 0)
163                 return;
164
165         /* get handle size. If this succeeds, it's an NFSv3 setup. */
166         if ((cp = getenv("boot.nfsroot.nfshandlelen")) != NULL) {
167                 cnt = sscanf(cp, "%d", &len);
168                 freeenv(cp);
169                 if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) {
170                         printf("nfs_diskless: bad NFS handle len\n");
171                         return;
172                 }
173                 nd3->root_fhsize = len;
174                 is_nfsv3 = 1;
175         } else
176                 is_nfsv3 = 0;
177         /* set up interface */
178         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
179                 return;
180         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
181                 printf("nfs_diskless: no netmask\n");
182                 return;
183         }
184         if (is_nfsv3 != 0) {
185                 bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr));
186                 bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr));
187                 ((struct sockaddr_in *) 
188                    &nd3->myif.ifra_broadaddr)->sin_addr.s_addr =
189                     myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
190                 bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask));
191         } else {
192                 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
193                 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
194                 ((struct sockaddr_in *) 
195                    &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
196                     myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
197                 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
198         }
199
200         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
201                 printf("nfs_diskless: no hardware address\n");
202                 return;
203         }
204         ifa = NULL;
205         IFNET_RLOCK();
206         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
207                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
208                         if (ifa->ifa_addr->sa_family == AF_LINK) {
209                                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
210                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
211                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
212                                     !bcmp(LLADDR(sdl),
213                                           LLADDR(&ourdl),
214                                           sdl->sdl_alen)) {
215                                     IFNET_RUNLOCK();
216                                     goto match_done;
217                                 }
218                         }
219                 }
220         }
221         IFNET_RUNLOCK();
222         printf("nfs_diskless: no interface\n");
223         return; /* no matching interface */
224 match_done:
225         setenv("boot.netif.name", ifp->if_xname);
226         if (is_nfsv3 != 0) {
227                 strlcpy(nd3->myif.ifra_name, ifp->if_xname,
228                     sizeof(nd3->myif.ifra_name));
229         
230                 /* set up gateway */
231                 inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway);
232
233                 /* set up root mount */
234                 nd3->root_args.rsize = 32768;           /* XXX tunable? */
235                 nd3->root_args.wsize = 32768;
236                 nd3->root_args.sotype = SOCK_STREAM;
237                 nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE |
238                     NFSMNT_RSIZE | NFSMNT_RESVPORT);
239                 if (inaddr_to_sockaddr("boot.nfsroot.server",
240                     &nd3->root_saddr)) {
241                         printf("nfs_diskless: no server\n");
242                         return;
243                 }
244                 nd3->root_saddr.sin_port = htons(NFS_PORT);
245                 fhlen = decode_nfshandle("boot.nfsroot.nfshandle",
246                     &nd3->root_fh[0], NFSX_V3FHMAX);
247                 if (fhlen == 0) {
248                         printf("nfs_diskless: no NFS handle\n");
249                         return;
250                 }
251                 if (fhlen != nd3->root_fhsize) {
252                         printf("nfs_diskless: bad NFS handle len=%d\n", fhlen);
253                         return;
254                 }
255                 if ((cp = getenv("boot.nfsroot.path")) != NULL) {
256                         strncpy(nd3->root_hostnam, cp, MNAMELEN - 1);
257                         freeenv(cp);
258                 }
259                 if ((cp = getenv("boot.nfsroot.options")) != NULL) {
260                         nfs_parse_options(cp, &nd3->root_args);
261                         freeenv(cp);
262                 }
263         
264                 nfs_diskless_valid = 3;
265         } else {
266                 strlcpy(nd->myif.ifra_name, ifp->if_xname,
267                     sizeof(nd->myif.ifra_name));
268         
269                 /* set up gateway */
270                 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
271
272                 /* set up root mount */
273                 nd->root_args.rsize = 8192;             /* XXX tunable? */
274                 nd->root_args.wsize = 8192;
275                 nd->root_args.sotype = SOCK_STREAM;
276                 nd->root_args.flags = (NFSMNT_WSIZE |
277                     NFSMNT_RSIZE | NFSMNT_RESVPORT);
278                 if (inaddr_to_sockaddr("boot.nfsroot.server",
279                     &nd->root_saddr)) {
280                         printf("nfs_diskless: no server\n");
281                         return;
282                 }
283                 nd->root_saddr.sin_port = htons(NFS_PORT);
284                 if (decode_nfshandle("boot.nfsroot.nfshandle",
285                     &nd->root_fh[0], NFSX_V2FH) == 0) {
286                         printf("nfs_diskless: no NFS handle\n");
287                         return;
288                 }
289                 if ((cp = getenv("boot.nfsroot.path")) != NULL) {
290                         strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
291                         freeenv(cp);
292                 }
293                 if ((cp = getenv("boot.nfsroot.options")) != NULL) {
294                         struct nfs_args args;
295         
296                         /*
297                          * XXX yech, convert between old and current
298                          * arg format
299                          */
300                         args.flags = nd->root_args.flags;
301                         args.sotype = nd->root_args.sotype;
302                         args.rsize = nd->root_args.rsize;
303                         args.wsize = nd->root_args.wsize;
304                         nfs_parse_options(cp, &args);
305                         nd->root_args.flags = args.flags;
306                         nd->root_args.sotype = args.sotype;
307                         nd->root_args.rsize = args.rsize;
308                         nd->root_args.wsize = args.wsize;
309                         freeenv(cp);
310                 }
311         
312                 nfs_diskless_valid = 1;
313         }
314 }
315
316 static int
317 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
318 {
319         u_int32_t a[4];
320         char *cp;
321         int count;
322
323         bzero(sa, sizeof(*sa));
324         sa->sin_len = sizeof(*sa);
325         sa->sin_family = AF_INET;
326
327         if ((cp = getenv(ev)) == NULL)
328                 return (1);
329         count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
330         freeenv(cp);
331         if (count != 4)
332                 return (1);
333         sa->sin_addr.s_addr =
334             htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
335         return (0);
336 }
337
338 static int
339 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
340 {
341         char *cp;
342         u_int32_t a[6];
343         int count;
344
345         bzero(sa, sizeof(*sa));
346         sa->sdl_len = sizeof(*sa);
347         sa->sdl_family = AF_LINK;
348         sa->sdl_type = IFT_ETHER;
349         sa->sdl_alen = ETHER_ADDR_LEN;
350         if ((cp = getenv(ev)) == NULL)
351                 return (1);
352         count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
353             &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
354         freeenv(cp);
355         if (count != 6)
356                 return (1);
357         sa->sdl_data[0] = a[0];
358         sa->sdl_data[1] = a[1];
359         sa->sdl_data[2] = a[2];
360         sa->sdl_data[3] = a[3];
361         sa->sdl_data[4] = a[4];
362         sa->sdl_data[5] = a[5];
363         return (0);
364 }
365
366 static int
367 decode_nfshandle(char *ev, u_char *fh, int maxfh) 
368 {
369         u_char *cp, *ep;
370         int len, val;
371
372         ep = cp = getenv(ev);
373         if (cp == NULL)
374                 return (0);
375         if ((strlen(cp) < 2) || (*cp != 'X')) {
376                 freeenv(ep);
377                 return (0);
378         }
379         len = 0;
380         cp++;
381         for (;;) {
382                 if (*cp == 'X') {
383                         freeenv(ep);
384                         return (len);
385                 }
386                 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
387                         freeenv(ep);
388                         return (0);
389                 }
390                 *(fh++) = val;
391                 len++;
392                 cp += 2;
393                 if (len > maxfh) {
394                     freeenv(ep);
395                     return (0);
396                 }
397         }
398 }
399
400 #if !defined(BOOTP_NFSROOT)
401 static void
402 nfs_rootconf(void)
403 {
404
405         nfs_setup_diskless();
406         if (nfs_diskless_valid)
407                 rootdevnames[0] = "nfs:";
408 }
409
410 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);
411 #endif
412