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