2 * $NetBSD: dev_net.c,v 1.12 1997/12/10 20:38:37 gwr 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.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 * $FreeBSD: src/sys/boot/common/dev_net.c,v 1.13 2003/11/03 19:45:05 iedowse Exp $
44 * This module implements a "raw device" interface suitable for
45 * use by the stand-alone I/O library NFS code. This interface
46 * does not support any "block" access, and exists only for the
47 * purpose of initializing the network interface, getting boot
48 * parameters, and performing the NFS mount.
50 * At open time, this does:
52 * find interface - netif_open()
53 * RARP for IP address - rarp_getipaddress()
54 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
55 * RPC/mountd - nfs_mount(sock, ip, path)
57 * the root file handle from mountd is saved in a global
58 * for use by the NFS open code (NFS/lookup).
61 #include <machine/stdarg.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
73 #include <bootparam.h>
76 #include "bootstrap.h"
80 static int netdev_sock
= -1;
81 static int netdev_opens
;
83 static int net_init(void);
84 static int net_open(struct open_file
*, ...);
85 static int net_close(struct open_file
*);
86 static int net_strategy(void *, int, daddr_t
, size_t, char *, size_t *);
87 static void net_print(int);
89 static int net_getparams(int sock
);
91 struct devsw netdev
= {
109 * Called by devopen after it sets f->f_dev to our devsw entry.
110 * This opens the low-level device and sets f->f_devdata.
111 * This is declared with variable arguments...
114 net_open(struct open_file
*f
, ...)
117 char *devname
; /* Device part of file name (or NULL). */
121 devname
= va_arg(args
, char*);
124 /* On first open, do netif open, mount, etc. */
125 if (netdev_opens
== 0) {
126 /* Find network interface. */
127 if (netdev_sock
< 0) {
128 netdev_sock
= netif_open(devname
);
129 if (netdev_sock
< 0) {
130 printf("net_open: netif_open() failed\n");
134 printf("net_open: netif_open() succeeded\n");
136 if (rootip
.s_addr
== 0) {
137 /* Get root IP address, and path, etc. */
138 error
= net_getparams(netdev_sock
);
140 /* getparams makes its own noise */
141 netif_close(netdev_sock
);
149 devreplace(f
, &netdev_sock
);
154 net_close(struct open_file
*f
)
159 printf("net_close: opens=%d\n", netdev_opens
);
162 /* On last close, do netif close, etc. */
164 /* Extra close call? */
165 if (netdev_opens
<= 0)
168 /* Not last close? */
169 if (netdev_opens
> 0)
172 if (netdev_sock
>= 0) {
174 printf("net_close: calling netif_close()\n");
175 netif_close(netdev_sock
);
182 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, char *buf
,
188 #define SUPPORT_BOOTP
191 * Get info for NFS boot: our IP address, our hostname,
192 * server IP address, and our root path on the server.
193 * There are two ways to do this: The old, Sun way,
194 * and the more modern, BOOTP way. (RFC951, RFC1048)
196 * The default is to use the Sun bootparams RPC
197 * (because that is what the kernel will do).
198 * MD code can make try_bootp initialied data,
199 * which will override this common definition.
205 extern n_long
ip_convertaddr(char *p
);
208 net_getparams(int sock
)
210 char buf
[MAXHOSTNAMELEN
];
211 char temp
[FNAME_SIZE
];
218 * Try to get boot info using BOOTP. If we succeed, then
219 * the server IP address, gateway, and root path will all
220 * be initialized. If any remain uninitialized, we will
221 * use RARP and RPC/bootparam (the Sun way) to get them.
224 bootp(sock
, BOOTP_NONE
);
225 if (myip
.s_addr
!= 0)
228 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
232 * Use RARP to get our IP address. This also sets our
233 * netmask to the "natural" default for our address.
235 if (rarp_getipaddress(sock
)) {
236 printf("net_open: RARP failed\n");
239 printf("net_open: client addr: %s\n", inet_ntoa(myip
));
241 /* Get our hostname, server IP address, gateway. */
242 if (bp_whoami(sock
)) {
243 printf("net_open: bootparam/whoami RPC failed\n");
246 printf("net_open: client name: %s\n", hostname
);
249 * Ignore the gateway from whoami (unreliable).
250 * Use the "gateway" parameter instead.
254 if (bp_getfile(sock
, "gateway", &gateip
, buf
) == 0) {
255 /* Got it! Parse the netmask. */
256 smask
= ip_convertaddr(buf
);
260 printf("net_open: subnet mask: %s\n", intoa(netmask
));
263 printf("net_open: net gateway: %s\n", inet_ntoa(gateip
));
265 /* Get the root server and pathname. */
266 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
267 printf("net_open: bootparam/getfile RPC failed\n");
272 * If present, strip the server's address off of the rootpath
273 * before passing it along. This allows us to be compatible with
274 * the kernel's diskless (BOOTP_NFSROOT) booting conventions
276 for (i
= 0; i
< FNAME_SIZE
&& rootpath
[i
] != '\0'; i
++)
277 if (rootpath
[i
] == ':')
279 if (i
&& i
!= FNAME_SIZE
&& rootpath
[i
] == ':') {
280 rootpath
[i
++] = '\0';
281 if (inet_addr(&rootpath
[0]) != INADDR_NONE
)
282 rootip
.s_addr
= inet_addr(&rootpath
[0]);
283 bcopy(&rootpath
[i
], &temp
[0], strlen(&rootpath
[i
])+1);
284 bcopy(&temp
[0], &rootpath
[0], strlen(&rootpath
[i
])+1);
286 printf("net_open: server addr: %s\n", inet_ntoa(rootip
));
287 printf("net_open: server path: %s\n", rootpath
);
289 d
= socktodesc(sock
);
290 setenv("boot.netif.ip", inet_ntoa(myip
), 1);
291 setenv("boot.netif.netmask", intoa(netmask
), 1);
292 setenv("boot.netif.gateway", inet_ntoa(gateip
), 1);
293 setenv("boot.netif.hwaddr", ether_sprintf(d
->myea
), 1);
294 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
295 setenv("boot.nfsroot.path", rootpath
, 1);
301 net_print(int verbose
)