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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)autoconf.c 7.1 (Berkeley) 5/9/91
37 * $FreeBSD: releng/5.0/sys/nfsclient/nfs_diskless.c 103770 2002-09-22 00:59:02Z jake $
38 */
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 <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfsclient/nfs.h>
56 #include <nfsclient/nfsdiskless.h>
57
58 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
59 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
60 static int decode_nfshandle(char *ev, u_char *fh);
61
62 /*
63 * Populate the essential fields in the nfsv3_diskless structure.
64 *
65 * The loader is expected to export the following environment variables:
66 *
67 * boot.netif.ip IP address on boot interface
68 * boot.netif.netmask netmask on boot interface
69 * boot.netif.gateway default gateway (optional)
70 * boot.netif.hwaddr hardware address of boot interface
71 * boot.nfsroot.server IP address of root filesystem server
72 * boot.nfsroot.path path of the root filesystem on server
73 * boot.nfsroot.nfshandle NFS handle for root filesystem on server
74 */
75 void
76 nfs_setup_diskless(void)
77 {
78 struct nfs_diskless *nd = &nfs_diskless;
79 struct ifnet *ifp;
80 struct ifaddr *ifa;
81 struct sockaddr_dl *sdl, ourdl;
82 struct sockaddr_in myaddr, netmask;
83 char *cp;
84
85 if (nfs_diskless_valid)
86 return;
87 /* set up interface */
88 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
89 return;
90 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
91 printf("nfs_diskless: no netmask\n");
92 return;
93 }
94 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
95 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
96 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
97 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
98 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
99
100 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
101 printf("nfs_diskless: no hardware address\n");
102 return;
103 }
104 ifa = NULL;
105 TAILQ_FOREACH(ifp, &ifnet, if_link) {
106 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
107 if ((ifa->ifa_addr->sa_family == AF_LINK) &&
108 (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
109 if ((sdl->sdl_type == ourdl.sdl_type) &&
110 (sdl->sdl_alen == ourdl.sdl_alen) &&
111 !bcmp(sdl->sdl_data + sdl->sdl_nlen,
112 ourdl.sdl_data + ourdl.sdl_nlen,
113 sdl->sdl_alen))
114 goto match_done;
115 }
116 }
117 }
118 printf("nfs_diskless: no interface\n");
119 return; /* no matching interface */
120 match_done:
121 sprintf(nd->myif.ifra_name, "%s%d", ifp->if_name, ifp->if_unit);
122
123 /* set up gateway */
124 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
125
126 /* XXX set up swap? */
127
128 /* set up root mount */
129 nd->root_args.rsize = 8192; /* XXX tunable? */
130 nd->root_args.wsize = 8192;
131 nd->root_args.sotype = SOCK_DGRAM;
132 nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
133 if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
134 printf("nfs_diskless: no server\n");
135 return;
136 }
137 nd->root_saddr.sin_port = htons(NFS_PORT);
138 if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
139 printf("nfs_diskless: no NFS handle\n");
140 return;
141 }
142 if ((cp = getenv("boot.nfsroot.path")) != NULL) {
143 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
144 freeenv(cp);
145 }
146
147 nfs_diskless_valid = 1;
148 }
149
150 static int
151 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
152 {
153 u_int32_t a[4];
154 char *cp;
155 int count;
156
157 bzero(sa, sizeof(*sa));
158 sa->sin_len = sizeof(*sa);
159 sa->sin_family = AF_INET;
160
161 if ((cp = getenv(ev)) == NULL)
162 return (1);
163 count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
164 freeenv(cp);
165 if (count != 4)
166 return (1);
167 sa->sin_addr.s_addr =
168 htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
169 return (0);
170 }
171
172 static int
173 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
174 {
175 char *cp;
176 u_int32_t a[6];
177 int count;
178
179 bzero(sa, sizeof(*sa));
180 sa->sdl_len = sizeof(*sa);
181 sa->sdl_family = AF_LINK;
182 sa->sdl_type = IFT_ETHER;
183 sa->sdl_alen = ETHER_ADDR_LEN;
184 if ((cp = getenv(ev)) == NULL)
185 return (1);
186 count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
187 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
188 freeenv(cp);
189 if (count != 6)
190 return (1);
191 sa->sdl_data[0] = a[0];
192 sa->sdl_data[1] = a[1];
193 sa->sdl_data[2] = a[2];
194 sa->sdl_data[3] = a[3];
195 sa->sdl_data[4] = a[4];
196 sa->sdl_data[5] = a[5];
197 return (0);
198 }
199
200 static int
201 decode_nfshandle(char *ev, u_char *fh)
202 {
203 u_char *cp, *ep;
204 int len, val;
205
206 ep = cp = getenv(ev);
207 if (cp == NULL)
208 return (0);
209 if ((strlen(cp) < 2) || (*cp != 'X')) {
210 freeenv(ep);
211 return (0);
212 }
213 len = 0;
214 cp++;
215 for (;;) {
216 if (*cp == 'X') {
217 freeenv(ep);
218 return (len);
219 }
220 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
221 freeenv(ep);
222 return (0);
223 }
224 *(fh++) = val;
225 len++;
226 cp += 2;
227 if (len > NFSX_V2FH) {
228 freeenv(ep);
229 return (0);
230 }
231 }
232 }
Cache object: 5c6f84cc9d1071b99f75d85bc90ab777
|