]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/dev_net.c
MFC r325834,r325997,326502: Move sys/boot to stand/
[FreeBSD/FreeBSD.git] / stand / 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 <stddef.h>
62 #include <string.h>
63 #include <net.h>
64 #include <netif.h>
65 #include <bootp.h>
66 #include <bootparam.h>
67
68 #include "dev_net.h"
69 #include "bootstrap.h"
70
71 #ifdef  NETIF_DEBUG
72 int debug = 0;
73 #endif
74
75 static char *netdev_name;
76 static int netdev_sock = -1;
77 static int netdev_opens;
78
79 static int      net_init(void);
80 static int      net_open(struct open_file *, ...);
81 static int      net_close(struct open_file *);
82 static void     net_cleanup(void);
83 static int      net_strategy(void *, int, daddr_t, size_t, char *, size_t *);
84 static int      net_print(int);
85
86 static int net_getparams(int sock);
87
88 struct devsw netdev = {
89         "net",
90         DEVT_NET,
91         net_init,
92         net_strategy,
93         net_open,
94         net_close,
95         noioctl,
96         net_print,
97         net_cleanup
98 };
99
100 static struct uri_scheme {
101         const char *scheme;
102         int proto;
103 } uri_schemes[] = {
104         { "tftp:/", NET_TFTP },
105         { "nfs:/", NET_NFS },
106 };
107
108 static int
109 net_init(void)
110 {
111
112         return (0);
113 }
114
115 /*
116  * Called by devopen after it sets f->f_dev to our devsw entry.
117  * This opens the low-level device and sets f->f_devdata.
118  * This is declared with variable arguments...
119  */
120 static int
121 net_open(struct open_file *f, ...)
122 {
123         struct iodesc *d;
124         va_list args;
125         char *devname;          /* Device part of file name (or NULL). */
126         int error = 0;
127
128         va_start(args, f);
129         devname = va_arg(args, char*);
130         va_end(args);
131
132         /* Before opening another interface, close the previous one first. */
133         if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
134                 net_cleanup();
135
136         /* On first open, do netif open, mount, etc. */
137         if (netdev_opens == 0) {
138                 /* Find network interface. */
139                 if (netdev_sock < 0) {
140                         netdev_sock = netif_open(devname);
141                         if (netdev_sock < 0) {
142                                 printf("net_open: netif_open() failed\n");
143                                 return (ENXIO);
144                         }
145                         netdev_name = strdup(devname);
146 #ifdef  NETIF_DEBUG
147                         if (debug)
148                                 printf("net_open: netif_open() succeeded\n");
149 #endif
150                 }
151                 /*
152                  * If network params were not set by netif_open(), try to get
153                  * them via bootp, rarp, etc.
154                  */
155                 if (rootip.s_addr == 0) {
156                         /* Get root IP address, and path, etc. */
157                         error = net_getparams(netdev_sock);
158                         if (error) {
159                                 /* getparams makes its own noise */
160                                 free(netdev_name);
161                                 netif_close(netdev_sock);
162                                 netdev_sock = -1;
163                                 return (error);
164                         }
165                 }
166                 /*
167                  * Set the variables required by the kernel's nfs_diskless
168                  * mechanism.  This is the minimum set of variables required to
169                  * mount a root filesystem without needing to obtain additional
170                  * info from bootp or other sources.
171                  */
172                 d = socktodesc(netdev_sock);
173                 setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
174                 setenv("boot.netif.ip", inet_ntoa(myip), 1);
175                 setenv("boot.netif.netmask", intoa(netmask), 1);
176                 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
177                 setenv("boot.netif.server", inet_ntoa(rootip), 1);
178                 if (netproto == NET_TFTP) {
179                         setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
180                         setenv("boot.tftproot.path", rootpath, 1);
181                 } else if (netproto == NET_NFS) {
182                         setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
183                         setenv("boot.nfsroot.path", rootpath, 1);
184                 }
185                 if (intf_mtu != 0) {
186                         char mtu[16];
187                         snprintf(mtu, sizeof(mtu), "%u", intf_mtu);
188                         setenv("boot.netif.mtu", mtu, 1);
189                 }
190
191         }
192         netdev_opens++;
193         f->f_devdata = &netdev_sock;
194         return (error);
195 }
196
197 static int
198 net_close(struct open_file *f)
199 {
200
201 #ifdef  NETIF_DEBUG
202         if (debug)
203                 printf("net_close: opens=%d\n", netdev_opens);
204 #endif
205
206         f->f_devdata = NULL;
207
208         return (0);
209 }
210
211 static void
212 net_cleanup(void)
213 {
214
215         if (netdev_sock >= 0) {
216 #ifdef  NETIF_DEBUG
217                 if (debug)
218                         printf("net_cleanup: calling netif_close()\n");
219 #endif
220                 rootip.s_addr = 0;
221                 free(netdev_name);
222                 netif_close(netdev_sock);
223                 netdev_sock = -1;
224         }
225 }
226
227 static int
228 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
229     size_t *rsize)
230 {
231
232         return (EIO);
233 }
234
235 #define SUPPORT_BOOTP
236
237 /*
238  * Get info for NFS boot: our IP address, our hostname,
239  * server IP address, and our root path on the server.
240  * There are two ways to do this:  The old, Sun way,
241  * and the more modern, BOOTP way. (RFC951, RFC1048)
242  *
243  * The default is to use the Sun bootparams RPC
244  * (because that is what the kernel will do).
245  * MD code can make try_bootp initialied data,
246  * which will override this common definition.
247  */
248 #ifdef  SUPPORT_BOOTP
249 int try_bootp = 1;
250 #endif
251
252 extern n_long ip_convertaddr(char *p);
253
254 static int
255 net_getparams(int sock)
256 {
257         char buf[MAXHOSTNAMELEN];
258         n_long rootaddr, smask;
259
260 #ifdef  SUPPORT_BOOTP
261         /*
262          * Try to get boot info using BOOTP.  If we succeed, then
263          * the server IP address, gateway, and root path will all
264          * be initialized.  If any remain uninitialized, we will
265          * use RARP and RPC/bootparam (the Sun way) to get them.
266          */
267         if (try_bootp)
268                 bootp(sock);
269         if (myip.s_addr != 0)
270                 goto exit;
271 #ifdef  NETIF_DEBUG
272         if (debug)
273                 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
274 #endif
275 #endif
276
277         /*
278          * Use RARP to get our IP address.  This also sets our
279          * netmask to the "natural" default for our address.
280          */
281         if (rarp_getipaddress(sock)) {
282                 printf("net_open: RARP failed\n");
283                 return (EIO);
284         }
285         printf("net_open: client addr: %s\n", inet_ntoa(myip));
286
287         /* Get our hostname, server IP address, gateway. */
288         if (bp_whoami(sock)) {
289                 printf("net_open: bootparam/whoami RPC failed\n");
290                 return (EIO);
291         }
292 #ifdef  NETIF_DEBUG
293         if (debug)
294                 printf("net_open: client name: %s\n", hostname);
295 #endif
296
297         /*
298          * Ignore the gateway from whoami (unreliable).
299          * Use the "gateway" parameter instead.
300          */
301         smask = 0;
302         gateip.s_addr = 0;
303         if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
304                 /* Got it!  Parse the netmask. */
305                 smask = ip_convertaddr(buf);
306         }
307         if (smask) {
308                 netmask = smask;
309 #ifdef  NETIF_DEBUG
310                 if (debug)
311                         printf("net_open: subnet mask: %s\n", intoa(netmask));
312 #endif
313         }
314 #ifdef  NETIF_DEBUG
315         if (gateip.s_addr && debug)
316                 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
317 #endif
318
319         /* Get the root server and pathname. */
320         if (bp_getfile(sock, "root", &rootip, rootpath)) {
321                 printf("net_open: bootparam/getfile RPC failed\n");
322                 return (EIO);
323         }
324 exit:
325         if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
326                 rootip.s_addr = rootaddr;
327
328 #ifdef  NETIF_DEBUG
329         if (debug) {
330                 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
331                 printf("net_open: server path: %s\n", rootpath);
332         }
333 #endif
334
335         return (0);
336 }
337
338 static int
339 net_print(int verbose)
340 {
341         struct netif_driver *drv;
342         int i, d, cnt;
343         int ret = 0;
344
345         if (netif_drivers[0] == NULL)
346                 return (ret);
347
348         printf("%s devices:", netdev.dv_name);
349         if ((ret = pager_output("\n")) != 0)
350                 return (ret);
351
352         cnt = 0;
353         for (d = 0; netif_drivers[d]; d++) {
354                 drv = netif_drivers[d];
355                 for (i = 0; i < drv->netif_nifs; i++) {
356                         printf("\t%s%d:", netdev.dv_name, cnt++);
357                         if (verbose) {
358                                 printf(" (%s%d)", drv->netif_bname,
359                                     drv->netif_ifs[i].dif_unit);
360                         }
361                         if ((ret = pager_output("\n")) != 0)
362                                 return (ret);
363                 }
364         }
365         return (ret);
366 }
367
368 /*
369  * Parses the rootpath if present
370  *
371  * The rootpath format can be in the form
372  * <scheme>://ip/path
373  * <scheme>:/path
374  *
375  * For compatibility with previous behaviour it also accepts as an NFS scheme
376  * ip:/path
377  * /path
378  *
379  * If an ip is set it returns it in network byte order.
380  * The default scheme defined in the global netproto, if not set it defaults to
381  * NFS.
382  * It leaves just the pathname in the global rootpath.
383  */
384 uint32_t
385 net_parse_rootpath()
386 {
387         n_long addr = htonl(INADDR_NONE);
388         size_t i;
389         char ip[FNAME_SIZE];
390         char *ptr, *val;
391
392         netproto = NET_NONE;
393
394         for (i = 0; i < nitems(uri_schemes); i++) {
395                 if (strncmp(rootpath, uri_schemes[i].scheme,
396                     strlen(uri_schemes[i].scheme)) != 0)
397                         continue;
398
399                 netproto = uri_schemes[i].proto;
400                 break;
401         }
402         ptr = rootpath;
403         /* Fallback for compatibility mode */
404         if (netproto == NET_NONE) {
405                 netproto = NET_NFS;
406                 (void)strsep(&ptr, ":");
407                 if (ptr != NULL) {
408                         addr = inet_addr(rootpath);
409                         bcopy(ptr, rootpath, strlen(ptr) + 1);
410                 }
411         } else {
412                 ptr += strlen(uri_schemes[i].scheme);
413                 if (*ptr == '/') {
414                         /* we are in the form <scheme>://, we do expect an ip */
415                         ptr++;
416                         /*
417                          * XXX when http will be there we will need to check for
418                          * a port, but right now we do not need it yet
419                          */
420                         val = strchr(ptr, '/');
421                         if (val != NULL) {
422                                 snprintf(ip, sizeof(ip), "%.*s",
423                                     (int)((uintptr_t)val - (uintptr_t)ptr),
424                                     ptr);
425                                 addr = inet_addr(ip);
426                                 bcopy(val, rootpath, strlen(val) + 1);
427                         }
428                 } else {
429                         ptr--;
430                         bcopy(ptr, rootpath, strlen(ptr) + 1);
431                 }
432         }
433
434         return (addr);
435 }