dev_net.c revision 1.10.2.1 1 1.1 cherry /*
2 1.10.2.1 pgoyette * $NetBSD: dev_net.c,v 1.10.2.1 2016/08/06 00:19:05 pgoyette Exp $
3 1.1 cherry */
4 1.1 cherry
5 1.1 cherry /*-
6 1.1 cherry * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 1.1 cherry * All rights reserved.
8 1.1 cherry *
9 1.1 cherry * This code is derived from software contributed to The NetBSD Foundation
10 1.1 cherry * by Gordon W. Ross.
11 1.1 cherry *
12 1.1 cherry * Redistribution and use in source and binary forms, with or without
13 1.1 cherry * modification, are permitted provided that the following conditions
14 1.1 cherry * are met:
15 1.1 cherry * 1. Redistributions of source code must retain the above copyright
16 1.1 cherry * notice, this list of conditions and the following disclaimer.
17 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 cherry * notice, this list of conditions and the following disclaimer in the
19 1.1 cherry * documentation and/or other materials provided with the distribution.
20 1.1 cherry *
21 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 cherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 cherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 cherry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 cherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 cherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 cherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 cherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 cherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 cherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 cherry * POSSIBILITY OF SUCH DAMAGE.
32 1.1 cherry */
33 1.1 cherry
34 1.1 cherry #include <sys/cdefs.h>
35 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/common/dev_net.c,v 1.15 2004/07/08 22:35:33 brian Exp $"); */
36 1.1 cherry
37 1.1 cherry /*-
38 1.1 cherry * This module implements a "raw device" interface suitable for
39 1.1 cherry * use by the stand-alone I/O library NFS code. This interface
40 1.1 cherry * does not support any "block" access, and exists only for the
41 1.1 cherry * purpose of initializing the network interface, getting boot
42 1.1 cherry * parameters, and performing the NFS mount.
43 1.1 cherry *
44 1.1 cherry * At open time, this does:
45 1.1 cherry *
46 1.1 cherry * find interface - netif_open()
47 1.1 cherry * RARP for IP address - rarp_getipaddress()
48 1.1 cherry * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
49 1.1 cherry * RPC/mountd - nfs_mount(sock, ip, path)
50 1.1 cherry *
51 1.1 cherry * the root file handle from mountd is saved in a global
52 1.1 cherry * for use by the NFS open code (NFS/lookup).
53 1.1 cherry */
54 1.1 cherry
55 1.1 cherry #include <sys/param.h>
56 1.10.2.1 pgoyette
57 1.10.2.1 pgoyette #include <lib/libsa/stand.h>
58 1.10.2.1 pgoyette #include <lib/libsa/net.h>
59 1.10.2.1 pgoyette #include <lib/libsa/bootparam.h>
60 1.10.2.1 pgoyette #include <lib/libsa/loadfile.h>
61 1.10.2.1 pgoyette #include <lib/libsa/netif.h>
62 1.10.2.1 pgoyette #include <lib/libsa/nfs.h>
63 1.10.2.1 pgoyette #include <lib/libsa/bootp.h>
64 1.10.2.1 pgoyette #include <lib/libkern/libkern.h>
65 1.1 cherry
66 1.1 cherry #include "dev_net.h"
67 1.1 cherry #include "bootstrap.h"
68 1.1 cherry
69 1.1 cherry int debug = 0;
70 1.1 cherry
71 1.1 cherry static int netdev_sock = -1;
72 1.1 cherry static int netdev_opens;
73 1.1 cherry
74 1.1 cherry static int net_getparams(int sock);
75 1.1 cherry
76 1.1 cherry /*
77 1.1 cherry * Called by devopen after it sets f->f_dev to our devsw entry.
78 1.1 cherry * This opens the low-level device and sets f->f_devdata.
79 1.1 cherry * This is declared with variable arguments...
80 1.1 cherry */
81 1.1 cherry int
82 1.1 cherry net_open(struct open_file *f, ...)
83 1.1 cherry {
84 1.1 cherry va_list args;
85 1.1 cherry char *devname; /* Device part of file name (or NULL). */
86 1.1 cherry int error = 0;
87 1.1 cherry
88 1.1 cherry va_start(args, f);
89 1.1 cherry devname = va_arg(args, char*);
90 1.1 cherry va_end(args);
91 1.1 cherry
92 1.1 cherry /* On first open, do netif open, mount, etc. */
93 1.1 cherry if (netdev_opens == 0) {
94 1.1 cherry /* Find network interface. */
95 1.1 cherry if (netdev_sock < 0) {
96 1.1 cherry netdev_sock = netif_open(devname);
97 1.1 cherry if (netdev_sock < 0) {
98 1.1 cherry printf("net_open: netif_open() failed\n");
99 1.1 cherry return (ENXIO);
100 1.1 cherry }
101 1.1 cherry if (debug)
102 1.1 cherry printf("net_open: netif_open() succeeded\n");
103 1.1 cherry }
104 1.1 cherry if (rootip.s_addr == 0) {
105 1.1 cherry /* Get root IP address, and path, etc. */
106 1.1 cherry error = net_getparams(netdev_sock);
107 1.1 cherry if (error) {
108 1.1 cherry /* getparams makes its own noise */
109 1.1 cherry netif_close(netdev_sock);
110 1.1 cherry netdev_sock = -1;
111 1.1 cherry return (error);
112 1.1 cherry }
113 1.1 cherry }
114 1.10.2.1 pgoyette if (debug)
115 1.10.2.1 pgoyette printf("net_open: got rootip %s\n", inet_ntoa(rootip));
116 1.10.2.1 pgoyette
117 1.10.2.1 pgoyette /*
118 1.10.2.1 pgoyette * Get the NFS file handle (mount).
119 1.10.2.1 pgoyette */
120 1.10.2.1 pgoyette error = nfs_mount(netdev_sock, rootip, rootpath);
121 1.10.2.1 pgoyette if (error) {
122 1.10.2.1 pgoyette netif_close(netdev_sock);
123 1.10.2.1 pgoyette netdev_sock = -1;
124 1.10.2.1 pgoyette printf("net_open: error with nfs mount 0x%x\n", error);
125 1.10.2.1 pgoyette return error;
126 1.10.2.1 pgoyette }
127 1.10.2.1 pgoyette
128 1.10.2.1 pgoyette if (debug)
129 1.10.2.1 pgoyette printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
130 1.10.2.1 pgoyette
131 1.1 cherry netdev_opens++;
132 1.1 cherry }
133 1.1 cherry netdev_opens++;
134 1.1 cherry f->f_devdata = &netdev_sock;
135 1.1 cherry return (error);
136 1.1 cherry }
137 1.1 cherry
138 1.1 cherry int
139 1.4 dsl net_close(struct open_file *f)
140 1.1 cherry {
141 1.1 cherry
142 1.1 cherry #ifdef NETIF_DEBUG
143 1.1 cherry if (debug)
144 1.1 cherry printf("net_close: opens=%d\n", netdev_opens);
145 1.1 cherry #endif
146 1.1 cherry
147 1.1 cherry /* On last close, do netif close, etc. */
148 1.1 cherry f->f_devdata = NULL;
149 1.1 cherry /* Extra close call? */
150 1.1 cherry if (netdev_opens <= 0)
151 1.1 cherry return (0);
152 1.1 cherry netdev_opens--;
153 1.1 cherry /* Not last close? */
154 1.1 cherry if (netdev_opens > 0)
155 1.1 cherry return(0);
156 1.1 cherry rootip.s_addr = 0;
157 1.1 cherry if (netdev_sock >= 0) {
158 1.1 cherry if (debug)
159 1.1 cherry printf("net_close: calling netif_close()\n");
160 1.1 cherry netif_close(netdev_sock);
161 1.1 cherry netdev_sock = -1;
162 1.1 cherry }
163 1.1 cherry return (0);
164 1.1 cherry }
165 1.1 cherry
166 1.1 cherry int
167 1.10.2.1 pgoyette net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
168 1.10.2.1 pgoyette {
169 1.10.2.1 pgoyette return EIO;
170 1.10.2.1 pgoyette }
171 1.10.2.1 pgoyette
172 1.10.2.1 pgoyette int
173 1.10.2.1 pgoyette net_ioctl(struct open_file *f, u_long cmd, void *data)
174 1.1 cherry {
175 1.1 cherry return EIO;
176 1.1 cherry }
177 1.1 cherry
178 1.1 cherry #define SUPPORT_BOOTP
179 1.1 cherry
180 1.1 cherry /*
181 1.1 cherry * Get info for NFS boot: our IP address, our hostname,
182 1.1 cherry * server IP address, and our root path on the server.
183 1.1 cherry * There are two ways to do this: The old, Sun way,
184 1.1 cherry * and the more modern, BOOTP way. (RFC951, RFC1048)
185 1.1 cherry *
186 1.1 cherry * The default is to use the Sun bootparams RPC
187 1.1 cherry * (because that is what the kernel will do).
188 1.1 cherry * MD code can make try_bootp initialied data,
189 1.1 cherry * which will override this common definition.
190 1.1 cherry */
191 1.1 cherry #ifdef SUPPORT_BOOTP
192 1.1 cherry int try_bootp = 1;
193 1.1 cherry #endif
194 1.1 cherry
195 1.1 cherry static int
196 1.4 dsl net_getparams(int sock)
197 1.1 cherry {
198 1.1 cherry char buf[MAXHOSTNAMELEN];
199 1.1 cherry char temp[FNAME_SIZE];
200 1.10.2.1 pgoyette char num[8];
201 1.10.2.1 pgoyette
202 1.1 cherry struct iodesc *d;
203 1.1 cherry int i;
204 1.1 cherry n_long smask;
205 1.1 cherry
206 1.1 cherry #ifdef SUPPORT_BOOTP
207 1.1 cherry /*
208 1.1 cherry * Try to get boot info using BOOTP. If we succeed, then
209 1.1 cherry * the server IP address, gateway, and root path will all
210 1.1 cherry * be initialized. If any remain uninitialized, we will
211 1.1 cherry * use RARP and RPC/bootparam (the Sun way) to get them.
212 1.1 cherry */
213 1.1 cherry if (try_bootp)
214 1.10.2.1 pgoyette bootp(sock);
215 1.1 cherry if (myip.s_addr != 0)
216 1.1 cherry goto exit;
217 1.1 cherry if (debug)
218 1.1 cherry printf("net_open: BOOTP failed, trying RARP/RPC...\n");
219 1.1 cherry #endif
220 1.1 cherry
221 1.1 cherry /*
222 1.1 cherry * Use RARP to get our IP address. This also sets our
223 1.1 cherry * netmask to the "natural" default for our address.
224 1.1 cherry */
225 1.1 cherry if (rarp_getipaddress(sock)) {
226 1.1 cherry printf("net_open: RARP failed\n");
227 1.1 cherry return (EIO);
228 1.1 cherry }
229 1.1 cherry printf("net_open: client addr: %s\n", inet_ntoa(myip));
230 1.1 cherry
231 1.1 cherry /* Get our hostname, server IP address, gateway. */
232 1.1 cherry if (bp_whoami(sock)) {
233 1.1 cherry printf("net_open: bootparam/whoami RPC failed\n");
234 1.1 cherry return (EIO);
235 1.1 cherry }
236 1.1 cherry printf("net_open: client name: %s\n", hostname);
237 1.1 cherry
238 1.1 cherry /*
239 1.1 cherry * Ignore the gateway from whoami (unreliable).
240 1.1 cherry * Use the "gateway" parameter instead.
241 1.1 cherry */
242 1.1 cherry smask = 0;
243 1.1 cherry gateip.s_addr = 0;
244 1.1 cherry if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
245 1.1 cherry /* Got it! Parse the netmask. */
246 1.10.2.1 pgoyette smask = inet_addr(buf);
247 1.1 cherry }
248 1.1 cherry if (smask) {
249 1.1 cherry netmask = smask;
250 1.10.2.1 pgoyette if (debug)
251 1.10.2.1 pgoyette printf("net_open: subnet mask: %s\n", intoa(netmask));
252 1.1 cherry }
253 1.1 cherry if (gateip.s_addr)
254 1.10.2.1 pgoyette if (debug)
255 1.10.2.1 pgoyette printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
256 1.1 cherry
257 1.1 cherry /* Get the root server and pathname. */
258 1.1 cherry if (bp_getfile(sock, "root", &rootip, rootpath)) {
259 1.1 cherry printf("net_open: bootparam/getfile RPC failed\n");
260 1.1 cherry return (EIO);
261 1.1 cherry }
262 1.1 cherry exit:
263 1.1 cherry /*
264 1.1 cherry * If present, strip the server's address off of the rootpath
265 1.1 cherry * before passing it along. This allows us to be compatible with
266 1.1 cherry * the kernel's diskless (BOOTP_NFSROOT) booting conventions
267 1.1 cherry */
268 1.10 christos for (i = 0; i < FNAME_SIZE && rootpath[i] != '\0'; i++)
269 1.1 cherry if (rootpath[i] == ':')
270 1.1 cherry break;
271 1.1 cherry if (i && i != FNAME_SIZE && rootpath[i] == ':') {
272 1.1 cherry rootpath[i++] = '\0';
273 1.1 cherry if (inet_addr(&rootpath[0]) != INADDR_NONE)
274 1.1 cherry rootip.s_addr = inet_addr(&rootpath[0]);
275 1.7 cegger memcpy(&temp[0], &rootpath[i], strlen(&rootpath[i])+1);
276 1.7 cegger memcpy(&rootpath[0], &temp[0], strlen(&rootpath[i])+1);
277 1.1 cherry }
278 1.1 cherry
279 1.10.2.1 pgoyette if (debug) {
280 1.10.2.1 pgoyette printf("net_open: server addr: %s\n", inet_ntoa(rootip));
281 1.10.2.1 pgoyette printf("net_open: server path: %s\n", rootpath);
282 1.10.2.1 pgoyette }
283 1.10.2.1 pgoyette
284 1.10.2.1 pgoyette /* do equivalent of
285 1.10.2.1 pgoyette * snprintf(temp, sizeof(temp), "%6D", d->myea, ":");
286 1.10.2.1 pgoyette * in lame way since snprintf seems to understand "%x", but not "%x:%x"
287 1.10.2.1 pgoyette */
288 1.1 cherry d = socktodesc(sock);
289 1.10.2.1 pgoyette memset(temp, '\0', sizeof(temp));
290 1.10.2.1 pgoyette
291 1.10.2.1 pgoyette for (i = 0; i < ETHER_ADDR_LEN; i++) {
292 1.10.2.1 pgoyette if (d->myea[i] < 0x10)
293 1.10.2.1 pgoyette strncat(temp, "0", 1);
294 1.10.2.1 pgoyette
295 1.10.2.1 pgoyette snprintf(num, sizeof(num), "%x", d->myea[i]);
296 1.10.2.1 pgoyette strncat(temp, num, 2);
297 1.10.2.1 pgoyette
298 1.10.2.1 pgoyette if (i < ETHER_ADDR_LEN-1)
299 1.10.2.1 pgoyette strncat(temp, ":", 1);
300 1.10.2.1 pgoyette }
301 1.10.2.1 pgoyette
302 1.1 cherry setenv("boot.netif.ip", inet_ntoa(myip), 1);
303 1.1 cherry setenv("boot.netif.netmask", intoa(netmask), 1);
304 1.1 cherry setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
305 1.1 cherry setenv("boot.netif.hwaddr", temp, 1);
306 1.1 cherry setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
307 1.1 cherry setenv("boot.nfsroot.path", rootpath, 1);
308 1.1 cherry
309 1.1 cherry return (0);
310 1.1 cherry }
311