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