Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / ia64 / stand / common / dev_net.c
blob5b7c6c0ccd58208ee69bd3ca3aacd0dcc9b714cb
1 /*
2 * $NetBSD: dev_net.c,v 1.6 2009/03/18 17:06:45 cegger Exp $
3 */
5 /*-
6 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Gordon W. Ross.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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 $"); */
37 /*-
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>
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
62 #include <stand.h>
63 #include <string.h>
64 #include <net.h>
65 #include <netif.h>
66 #include <bootp.h>
67 #include <bootparam.h>
69 #include "dev_net.h"
70 #include "bootstrap.h"
72 int debug = 0;
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 = {
86 "net",
87 DEVT_NET,
88 net_init,
89 net_strategy,
90 net_open,
91 net_close,
92 noioctl,
93 net_print
96 int
97 net_init(void)
99 return 0;
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, ...)
110 va_list args;
111 char *devname; /* Device part of file name (or NULL). */
112 int error = 0;
114 va_start(args, f);
115 devname = va_arg(args, char*);
116 va_end(args);
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");
125 return (ENXIO);
127 if (debug)
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);
133 if (error) {
134 /* getparams makes its own noise */
135 netif_close(netdev_sock);
136 netdev_sock = -1;
137 return (error);
140 netdev_opens++;
142 netdev_opens++;
143 f->f_devdata = &netdev_sock;
144 return (error);
148 net_close(struct open_file *f)
151 #ifdef NETIF_DEBUG
152 if (debug)
153 printf("net_close: opens=%d\n", netdev_opens);
154 #endif
156 /* On last close, do netif close, etc. */
157 f->f_devdata = NULL;
158 /* Extra close call? */
159 if (netdev_opens <= 0)
160 return (0);
161 netdev_opens--;
162 /* Not last close? */
163 if (netdev_opens > 0)
164 return(0);
165 rootip.s_addr = 0;
166 if (netdev_sock >= 0) {
167 if (debug)
168 printf("net_close: calling netif_close()\n");
169 netif_close(netdev_sock);
170 netdev_sock = -1;
172 return (0);
176 net_strategy(void)
178 return EIO;
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.
194 #ifdef SUPPORT_BOOTP
195 int try_bootp = 1;
196 #endif
198 extern n_long ip_convertaddr(char *p);
200 static int
201 net_getparams(int sock)
203 char buf[MAXHOSTNAMELEN];
204 char temp[FNAME_SIZE];
205 struct iodesc *d;
206 int i;
207 n_long smask;
209 #ifdef SUPPORT_BOOTP
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.
216 if (try_bootp)
217 bootp(sock, BOOTP_NONE);
218 if (myip.s_addr != 0)
219 goto exit;
220 if (debug)
221 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
222 #endif
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");
230 return (EIO);
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");
237 return (EIO);
239 printf("net_open: client name: %s\n", hostname);
242 * Ignore the gateway from whoami (unreliable).
243 * Use the "gateway" parameter instead.
245 smask = 0;
246 gateip.s_addr = 0;
247 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
248 /* Got it! Parse the netmask. */
249 smask = ip_convertaddr(buf);
251 if (smask) {
252 netmask = smask;
253 printf("net_open: subnet mask: %s\n", intoa(netmask));
255 if (gateip.s_addr)
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");
261 return (EIO);
263 exit:
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] == ':')
271 break;
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);
291 return (0);
294 static void
295 net_print(int verbose)
297 return;