[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/nfsclient/nfs_diskless.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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: src/sys/nfsclient/nfs_diskless.c,v 1.22 2008/10/02 15:37:58 zec Exp $");
 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 #include <sys/vimage.h>
 47 
 48 #include <net/if.h>
 49 #include <net/if_dl.h>
 50 #include <net/if_types.h>
 51 #include <net/if_var.h>
 52 #include <net/ethernet.h>
 53 #include <netinet/in.h>
 54 #include <rpc/rpcclnt.h>
 55 #include <nfs/rpcv2.h>
 56 #include <nfs/nfsproto.h>
 57 #include <nfsclient/nfs.h>
 58 #include <nfsclient/nfsdiskless.h>
 59 
 60 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
 61 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
 62 static int decode_nfshandle(char *ev, u_char *fh);
 63 
 64 /*
 65  * Validate/sanity check a rsize/wsize parameter.
 66  */
 67 static int
 68 checkrwsize(unsigned long v, const char *name)
 69 {
 70         /*
 71          * 32K is used as an upper bound because most servers
 72          * limit block size to satisfy IPv4's limit of
 73          * 64K/reassembled packet.  The lower bound is pretty
 74          * much arbitrary.
 75          */
 76         if (!(4 <= v && v <= 32*1024)) {
 77                 printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
 78                 return 0;
 79         } else
 80                 return 1;
 81 }
 82 
 83 /*
 84  * Parse mount options and apply them to the supplied
 85  * nfs_diskless state.  Used also by bootp/dhcp support.
 86  */
 87 void
 88 nfs_parse_options(const char *envopts, struct nfs_args *nd)
 89 {
 90         char *opts, *o, *otmp;
 91         unsigned long v;
 92 
 93         opts = strdup(envopts, M_TEMP);
 94         otmp = opts;
 95         while ((o = strsep(&otmp, ":;, ")) != NULL) {
 96                 if (*o == '\0')
 97                         ; /* Skip empty options. */
 98                 else if (strcmp(o, "soft") == 0)
 99                         nd->flags |= NFSMNT_SOFT;
100                 else if (strcmp(o, "intr") == 0)
101                         nd->flags |= NFSMNT_INT;
102                 else if (strcmp(o, "conn") == 0)
103                         nd->flags |= NFSMNT_NOCONN;
104                 else if (strcmp(o, "nolockd") == 0)
105                         nd->flags |= NFSMNT_NOLOCKD;
106                 else if (strcmp(o, "nfsv2") == 0)
107                         nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4);
108                 else if (strcmp(o, "nfsv3") == 0) {
109                         nd->flags &= ~NFSMNT_NFSV4;
110                         nd->flags |= NFSMNT_NFSV3;
111                 } else if (strcmp(o, "tcp") == 0)
112                         nd->sotype = SOCK_STREAM;
113                 else if (strcmp(o, "udp") == 0)
114                         nd->sotype = SOCK_DGRAM;
115                 else if (strncmp(o, "rsize=", 6) == 0) {
116                         v = strtoul(o+6, NULL, 10);
117                         if (checkrwsize(v, "rsize")) {
118                                 nd->rsize = (int) v;
119                                 nd->flags |= NFSMNT_RSIZE;
120                         }
121                 } else if (strncmp(o, "wsize=", 6) == 0) {
122                         v = strtoul(o+6, NULL, 10);
123                         if (checkrwsize(v, "wsize")) {
124                                 nd->wsize = (int) v;
125                                 nd->flags |= NFSMNT_WSIZE;
126                         }
127                 } else
128                         printf("%s: skipping unknown option \"%s\"\n",
129                             __func__, o);
130         }
131         free(opts, M_TEMP);
132 }
133 
134 /*
135  * Populate the essential fields in the nfsv3_diskless structure.
136  *
137  * The loader is expected to export the following environment variables:
138  *
139  * boot.netif.name              name of boot interface
140  * boot.netif.ip                IP address on boot interface
141  * boot.netif.netmask           netmask on boot interface
142  * boot.netif.gateway           default gateway (optional)
143  * boot.netif.hwaddr            hardware address of boot interface
144  * boot.nfsroot.server          IP address of root filesystem server
145  * boot.nfsroot.path            path of the root filesystem on server
146  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
147  * boot.nfsroot.options         NFS options for the root filesystem
148  */
149 void
150 nfs_setup_diskless(void)
151 {
152         INIT_VNET_NET(curvnet);
153         struct nfs_diskless *nd = &nfs_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 
160         if (nfs_diskless_valid)
161                 return;
162         /* set up interface */
163         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
164                 return;
165         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
166                 printf("nfs_diskless: no netmask\n");
167                 return;
168         }
169         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
170         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
171         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
172                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
173         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
174 
175         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
176                 printf("nfs_diskless: no hardware address\n");
177                 return;
178         }
179         ifa = NULL;
180         IFNET_RLOCK();
181         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
182                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
183                         if (ifa->ifa_addr->sa_family == AF_LINK) {
184                                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
185                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
186                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
187                                     !bcmp(LLADDR(sdl),
188                                           LLADDR(&ourdl),
189                                           sdl->sdl_alen)) {
190                                     IFNET_RUNLOCK();
191                                     goto match_done;
192                                 }
193                         }
194                 }
195         }
196         IFNET_RUNLOCK();
197         printf("nfs_diskless: no interface\n");
198         return; /* no matching interface */
199 match_done:
200         setenv("boot.netif.name", ifp->if_xname);
201         strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
202         
203         /* set up gateway */
204         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
205 
206         /* set up root mount */
207         nd->root_args.rsize = 8192;             /* XXX tunable? */
208         nd->root_args.wsize = 8192;
209         nd->root_args.sotype = SOCK_STREAM;
210         nd->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
211         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
212                 printf("nfs_diskless: no server\n");
213                 return;
214         }
215         nd->root_saddr.sin_port = htons(NFS_PORT);
216         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
217                 printf("nfs_diskless: no NFS handle\n");
218                 return;
219         }
220         if ((cp = getenv("boot.nfsroot.path")) != NULL) {
221                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
222                 freeenv(cp);
223         }
224         if ((cp = getenv("boot.nfsroot.options")) != NULL) {
225                 struct nfs_args args;
226 
227                 /* XXX yech, convert between old and current arg format */
228                 args.flags = nd->root_args.flags;
229                 args.sotype = nd->root_args.sotype;
230                 args.rsize = nd->root_args.rsize;
231                 args.wsize = nd->root_args.wsize;
232                 nfs_parse_options(cp, &args);
233                 nd->root_args.flags = args.flags;
234                 nd->root_args.sotype = args.sotype;
235                 nd->root_args.rsize = args.rsize;
236                 nd->root_args.wsize = args.wsize;
237                 freeenv(cp);
238         }
239 
240         nfs_diskless_valid = 1;
241 }
242 
243 static int
244 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
245 {
246         u_int32_t a[4];
247         char *cp;
248         int count;
249 
250         bzero(sa, sizeof(*sa));
251         sa->sin_len = sizeof(*sa);
252         sa->sin_family = AF_INET;
253 
254         if ((cp = getenv(ev)) == NULL)
255                 return (1);
256         count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
257         freeenv(cp);
258         if (count != 4)
259                 return (1);
260         sa->sin_addr.s_addr =
261             htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
262         return (0);
263 }
264 
265 static int
266 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
267 {
268         char *cp;
269         u_int32_t a[6];
270         int count;
271 
272         bzero(sa, sizeof(*sa));
273         sa->sdl_len = sizeof(*sa);
274         sa->sdl_family = AF_LINK;
275         sa->sdl_type = IFT_ETHER;
276         sa->sdl_alen = ETHER_ADDR_LEN;
277         if ((cp = getenv(ev)) == NULL)
278                 return (1);
279         count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
280             &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
281         freeenv(cp);
282         if (count != 6)
283                 return (1);
284         sa->sdl_data[0] = a[0];
285         sa->sdl_data[1] = a[1];
286         sa->sdl_data[2] = a[2];
287         sa->sdl_data[3] = a[3];
288         sa->sdl_data[4] = a[4];
289         sa->sdl_data[5] = a[5];
290         return (0);
291 }
292 
293 static int
294 decode_nfshandle(char *ev, u_char *fh) 
295 {
296         u_char *cp, *ep;
297         int len, val;
298 
299         ep = cp = getenv(ev);
300         if (cp == NULL)
301                 return (0);
302         if ((strlen(cp) < 2) || (*cp != 'X')) {
303                 freeenv(ep);
304                 return (0);
305         }
306         len = 0;
307         cp++;
308         for (;;) {
309                 if (*cp == 'X') {
310                         freeenv(ep);
311                         return (len);
312                 }
313                 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
314                         freeenv(ep);
315                         return (0);
316                 }
317                 *(fh++) = val;
318                 len++;
319                 cp += 2;
320                 if (len > NFSX_V2FH) {
321                     freeenv(ep);
322                     return (0);
323                 }
324         }
325 }
326 
327 #if !defined(BOOTP_NFSROOT)
328 static void
329 nfs_rootconf(void)
330 {
331 
332         nfs_setup_diskless();
333         if (nfs_diskless_valid)
334                 rootdevnames[0] = "nfs:";
335 }
336 
337 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);
338 #endif
339 
340 

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.