2 * $NetBSD: dev_net.c,v 1.6 2009/03/18 17:06:45 cegger Exp $
6 * Copyright (c) 1997 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 /* __FBSDID("$FreeBSD: src/sys/boot/common/dev_net.c,v 1.15 2004/07/08 22:35:33 brian Exp $"); */
38 * This module implements a "raw device" interface suitable for
39 * use by the stand-alone I/O library NFS code. This interface
40 * does not support any "block" access, and exists only for the
41 * purpose of initializing the network interface, getting boot
42 * parameters, and performing the NFS mount.
44 * At open time, this does:
46 * find interface - netif_open()
47 * RARP for IP address - rarp_getipaddress()
48 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
49 * RPC/mountd - nfs_mount(sock, ip, path)
51 * the root file handle from mountd is saved in a global
52 * for use by the NFS open code (NFS/lookup).
55 #include <machine/stdarg.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
67 #include <bootparam.h>
70 #include "bootstrap.h"
74 static int netdev_sock
= -1;
75 static int netdev_opens
;
77 static int net_init(void);
78 static int net_open(struct open_file
*, ...);
79 static int net_close(struct open_file
*);
80 static int net_strategy();
81 static void net_print(int);
83 static int net_getparams(int sock
);
85 struct devsw netdev
= {
103 * Called by devopen after it sets f->f_dev to our devsw entry.
104 * This opens the low-level device and sets f->f_devdata.
105 * This is declared with variable arguments...
108 net_open(struct open_file
*f
, ...)
111 char *devname
; /* Device part of file name (or NULL). */
115 devname
= va_arg(args
, char*);
118 /* On first open, do netif open, mount, etc. */
119 if (netdev_opens
== 0) {
120 /* Find network interface. */
121 if (netdev_sock
< 0) {
122 netdev_sock
= netif_open(devname
);
123 if (netdev_sock
< 0) {
124 printf("net_open: netif_open() failed\n");
128 printf("net_open: netif_open() succeeded\n");
130 if (rootip
.s_addr
== 0) {
131 /* Get root IP address, and path, etc. */
132 error
= net_getparams(netdev_sock
);
134 /* getparams makes its own noise */
135 netif_close(netdev_sock
);
143 f
->f_devdata
= &netdev_sock
;
148 net_close(struct open_file
*f
)
153 printf("net_close: opens=%d\n", netdev_opens
);
156 /* On last close, do netif close, etc. */
158 /* Extra close call? */
159 if (netdev_opens
<= 0)
162 /* Not last close? */
163 if (netdev_opens
> 0)
166 if (netdev_sock
>= 0) {
168 printf("net_close: calling netif_close()\n");
169 netif_close(netdev_sock
);
181 #define SUPPORT_BOOTP
184 * Get info for NFS boot: our IP address, our hostname,
185 * server IP address, and our root path on the server.
186 * There are two ways to do this: The old, Sun way,
187 * and the more modern, BOOTP way. (RFC951, RFC1048)
189 * The default is to use the Sun bootparams RPC
190 * (because that is what the kernel will do).
191 * MD code can make try_bootp initialied data,
192 * which will override this common definition.
198 extern n_long
ip_convertaddr(char *p
);
201 net_getparams(int sock
)
203 char buf
[MAXHOSTNAMELEN
];
204 char temp
[FNAME_SIZE
];
211 * Try to get boot info using BOOTP. If we succeed, then
212 * the server IP address, gateway, and root path will all
213 * be initialized. If any remain uninitialized, we will
214 * use RARP and RPC/bootparam (the Sun way) to get them.
217 bootp(sock
, BOOTP_NONE
);
218 if (myip
.s_addr
!= 0)
221 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
225 * Use RARP to get our IP address. This also sets our
226 * netmask to the "natural" default for our address.
228 if (rarp_getipaddress(sock
)) {
229 printf("net_open: RARP failed\n");
232 printf("net_open: client addr: %s\n", inet_ntoa(myip
));
234 /* Get our hostname, server IP address, gateway. */
235 if (bp_whoami(sock
)) {
236 printf("net_open: bootparam/whoami RPC failed\n");
239 printf("net_open: client name: %s\n", hostname
);
242 * Ignore the gateway from whoami (unreliable).
243 * Use the "gateway" parameter instead.
247 if (bp_getfile(sock
, "gateway", &gateip
, buf
) == 0) {
248 /* Got it! Parse the netmask. */
249 smask
= ip_convertaddr(buf
);
253 printf("net_open: subnet mask: %s\n", intoa(netmask
));
256 printf("net_open: net gateway: %s\n", inet_ntoa(gateip
));
258 /* Get the root server and pathname. */
259 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
260 printf("net_open: bootparam/getfile RPC failed\n");
265 * If present, strip the server's address off of the rootpath
266 * before passing it along. This allows us to be compatible with
267 * the kernel's diskless (BOOTP_NFSROOT) booting conventions
269 for (i
= 0; rootpath
[i
] != '\0' && i
< FNAME_SIZE
; i
++)
270 if (rootpath
[i
] == ':')
272 if (i
&& i
!= FNAME_SIZE
&& rootpath
[i
] == ':') {
273 rootpath
[i
++] = '\0';
274 if (inet_addr(&rootpath
[0]) != INADDR_NONE
)
275 rootip
.s_addr
= inet_addr(&rootpath
[0]);
276 memcpy(&temp
[0], &rootpath
[i
], strlen(&rootpath
[i
])+1);
277 memcpy(&rootpath
[0], &temp
[0], strlen(&rootpath
[i
])+1);
279 printf("net_open: server addr: %s\n", inet_ntoa(rootip
));
280 printf("net_open: server path: %s\n", rootpath
);
282 d
= socktodesc(sock
);
283 sprintf(temp
, "%6D", d
->myea
, ":");
284 setenv("boot.netif.ip", inet_ntoa(myip
), 1);
285 setenv("boot.netif.netmask", intoa(netmask
), 1);
286 setenv("boot.netif.gateway", inet_ntoa(gateip
), 1);
287 setenv("boot.netif.hwaddr", temp
, 1);
288 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
289 setenv("boot.nfsroot.path", rootpath
, 1);
295 net_print(int verbose
)