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: releng/5.4/sys/nfsclient/nfs_diskless.c 140738 2005-01-24 13:50:04Z brian $");
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
46 #include <sys/socket.h>
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 <netinet/in.h>
53 #include <rpc/rpcclnt.h>
54 #include <nfs/rpcv2.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);
62
63 /*
64 * Populate the essential fields in the nfsv3_diskless structure.
65 *
66 * The loader is expected to export the following environment variables:
67 *
68 * boot.netif.ip IP address on boot interface
69 * boot.netif.netmask netmask on boot interface
70 * boot.netif.gateway default gateway (optional)
71 * boot.netif.hwaddr hardware address of boot interface
72 * boot.nfsroot.server IP address of root filesystem server
73 * boot.nfsroot.path path of the root filesystem on server
74 * boot.nfsroot.nfshandle NFS handle for root filesystem on server
75 */
76 void
77 nfs_setup_diskless(void)
78 {
79 struct nfs_diskless *nd = &nfs_diskless;
80 struct ifnet *ifp;
81 struct ifaddr *ifa;
82 struct sockaddr_dl *sdl, ourdl;
83 struct sockaddr_in myaddr, netmask;
84 char *cp;
85
86 if (nfs_diskless_valid)
87 return;
88 /* set up interface */
89 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
90 return;
91 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
92 printf("nfs_diskless: no netmask\n");
93 return;
94 }
95 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
96 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
97 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
98 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
99 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
100
101 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
102 printf("nfs_diskless: no hardware address\n");
103 return;
104 }
105 ifa = NULL;
106 IFNET_RLOCK();
107 TAILQ_FOREACH(ifp, &ifnet, if_link) {
108 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
109 if ((ifa->ifa_addr->sa_family == AF_LINK) &&
110 (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
111 if ((sdl->sdl_type == ourdl.sdl_type) &&
112 (sdl->sdl_alen == ourdl.sdl_alen) &&
113 !bcmp(sdl->sdl_data + sdl->sdl_nlen,
114 ourdl.sdl_data + ourdl.sdl_nlen,
115 sdl->sdl_alen)) {
116 IFNET_RUNLOCK();
117 goto match_done;
118 }
119 }
120 }
121 }
122 IFNET_RUNLOCK();
123 printf("nfs_diskless: no interface\n");
124 return; /* no matching interface */
125 match_done:
126 strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
127
128 /* set up gateway */
129 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
130
131 /* set up root mount */
132 nd->root_args.rsize = 8192; /* XXX tunable? */
133 nd->root_args.wsize = 8192;
134 nd->root_args.sotype = SOCK_DGRAM;
135 nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
136 if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
137 printf("nfs_diskless: no server\n");
138 return;
139 }
140 nd->root_saddr.sin_port = htons(NFS_PORT);
141 if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
142 printf("nfs_diskless: no NFS handle\n");
143 return;
144 }
145 if ((cp = getenv("boot.nfsroot.path")) != NULL) {
146 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
147 freeenv(cp);
148 }
149
150 nfs_diskless_valid = 1;
151 }
152
153 static int
154 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
155 {
156 u_int32_t a[4];
157 char *cp;
158 int count;
159
160 bzero(sa, sizeof(*sa));
161 sa->sin_len = sizeof(*sa);
162 sa->sin_family = AF_INET;
163
164 if ((cp = getenv(ev)) == NULL)
165 return (1);
166 count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
167 freeenv(cp);
168 if (count != 4)
169 return (1);
170 sa->sin_addr.s_addr =
171 htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
172 return (0);
173 }
174
175 static int
176 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
177 {
178 char *cp;
179 u_int32_t a[6];
180 int count;
181
182 bzero(sa, sizeof(*sa));
183 sa->sdl_len = sizeof(*sa);
184 sa->sdl_family = AF_LINK;
185 sa->sdl_type = IFT_ETHER;
186 sa->sdl_alen = ETHER_ADDR_LEN;
187 if ((cp = getenv(ev)) == NULL)
188 return (1);
189 count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
190 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
191 freeenv(cp);
192 if (count != 6)
193 return (1);
194 sa->sdl_data[0] = a[0];
195 sa->sdl_data[1] = a[1];
196 sa->sdl_data[2] = a[2];
197 sa->sdl_data[3] = a[3];
198 sa->sdl_data[4] = a[4];
199 sa->sdl_data[5] = a[5];
200 return (0);
201 }
202
203 static int
204 decode_nfshandle(char *ev, u_char *fh)
205 {
206 u_char *cp, *ep;
207 int len, val;
208
209 ep = cp = getenv(ev);
210 if (cp == NULL)
211 return (0);
212 if ((strlen(cp) < 2) || (*cp != 'X')) {
213 freeenv(ep);
214 return (0);
215 }
216 len = 0;
217 cp++;
218 for (;;) {
219 if (*cp == 'X') {
220 freeenv(ep);
221 return (len);
222 }
223 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
224 freeenv(ep);
225 return (0);
226 }
227 *(fh++) = val;
228 len++;
229 cp += 2;
230 if (len > NFSX_V2FH) {
231 freeenv(ep);
232 return (0);
233 }
234 }
235 }
236
237 #if !defined(BOOTP_NFSROOT)
238 static void
239 nfs_rootconf(void)
240 {
241
242 nfs_setup_diskless();
243 if (nfs_diskless_valid)
244 rootdevnames[0] = "nfs:";
245 }
246
247 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL)
248 #endif
249
Cache object: d806479b42ce60be0be375e39c70a307
|