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