1 /* $NetBSD: dev_net.c,v 1.14 2009/03/14 15:36:00 dsl Exp $ */
4 * Copyright (c) 1995 Gordon W. Ross
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This module implements a "raw device" interface suitable for
30 * use by the stand-alone I/O library NFS code. This interface
31 * does not support any "block" access, and exists only for the
32 * purpose of initializing the network interface, getting boot
33 * parameters, and performing the NFS mount.
35 * At open time, this does:
37 * find interface - netif_open()
38 * RARP for IP address - rarp_getipaddress()
39 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
40 * RPC/mountd - nfs_mount(sock, ip, path)
42 * the root file handle from mountd is saved in a global
43 * for use by the NFS open code (NFS/lookup).
46 #include <machine/stdarg.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
53 #include <lib/libsa/stand.h>
54 #include <lib/libsa/net.h>
55 #include <lib/libsa/netif.h>
56 #include <lib/libsa/bootparam.h>
57 #include <lib/libsa/nfs.h>
59 #include <lib/libkern/libkern.h>
63 #ifndef SUN_BOOTPARAMS
68 extern int nfs_root_node
[]; /* XXX - get from nfs_mount() */
71 * Various globals needed by the network code:
75 /* for arp.c, rarp.c */
76 u_char bcea
[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
79 struct in_addr myip
; /* my ip address */
80 struct in_addr rootip
; /* root ip address */
81 struct in_addr gateip
; /* swap ip address */
82 n_long netmask
; /* subnet or net mask */
84 char rootpath
[FNAME_SIZE
];
85 char hostname
[FNAME_SIZE
];
90 static int netdev_sock
= -1;
91 static int netdev_opens
;
93 int net_getparams(int);
96 * Called by devopen after it sets f->f_dev to our devsw entry.
97 * This opens the low-level device and sets f->f_devdata.
98 * This is declared with variable arguments...
101 net_open(struct open_file
*f
, ...)
104 char *devname
; /* Device part of file name (or NULL). */
108 devname
= va_arg(ap
, char*);
113 printf("net_open: %s\n", devname
);
116 /* On first open, do netif open, mount, etc. */
117 if (netdev_opens
== 0) {
118 /* Find network interface. */
119 if (netdev_sock
< 0) {
120 netdev_sock
= netif_open(devname
);
121 if (netdev_sock
< 0) {
122 printf("net_open: netif_open() failed\n");
126 printf("net_open: netif_open() succeeded\n");
128 if (rootip
.s_addr
== 0) {
129 /* Get root IP address, and path, etc. */
130 error
= net_getparams(netdev_sock
);
132 /* getparams makes its own noise */
135 /* Get the NFS file handle (mountd). */
136 error
= nfs_mount(netdev_sock
, rootip
, rootpath
);
139 printf("net_open: NFS mount error=%d\n", error
);
142 netif_close(netdev_sock
);
147 printf("net_open: NFS mount succeeded\n");
151 f
->f_devdata
= nfs_root_node
;
156 net_close(struct open_file
*f
)
161 printf("net_close: opens=%d\n", netdev_opens
);
164 /* On last close, do netif close, etc. */
166 /* Extra close call? */
167 if (netdev_opens
<= 0)
170 /* Not last close? */
171 if (netdev_opens
> 0)
174 if (netdev_sock
>= 0) {
176 printf("net_close: calling netif_close()\n");
177 netif_close(netdev_sock
);
184 net_ioctl(struct open_file
*f
, u_long cmd
, void *data
)
190 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, void *buf
, size_t *rsize
)
196 net_getparams(int sock
)
199 * Get info for NFS boot: our IP address, our hostname,
200 * server IP address, and our root path on the server.
201 * There are two ways to do this: The old, Sun way,
202 * and the more modern, BOOTP way. (RFC951, RFC1048)
205 #ifdef SUN_BOOTPARAMS
206 /* Get our IP address. (rarp.c) */
207 if (rarp_getipaddress(sock
)) {
208 printf("net_open: RARP failed\n");
211 #else /* BOOTPARAMS */
213 * Get boot info using BOOTP. (RFC951, RFC1048)
214 * This also gets the server IP address, gateway,
218 if (myip
.s_addr
== 0) {
219 printf("net_open: BOOTP failed\n");
222 #endif /* BOOTPARAMS */
224 printf("boot: client addr: %s\n", inet_ntoa(myip
));
226 #ifdef SUN_BOOTPARAMS
227 /* Get our hostname, server IP address, gateway. */
228 if (bp_whoami(sock
)) {
229 printf("net_open: bootparam/whoami RPC failed\n");
232 #endif /* BOOTPARAMS */
234 printf("boot: client name: %s\n", hostname
);
236 printf("boot: subnet mask: %s\n", intoa(netmask
));
237 printf("boot: net gateway: %s\n", inet_ntoa(gateip
));
240 #ifdef SUN_BOOTPARAMS
241 /* Get the root pathname. */
242 if (bp_getfile(sock
, "root", &rootip
, rootpath
)) {
243 printf("net_open: bootparam/getfile RPC failed\n");
246 #endif /* BOOTPARAMS */
248 printf("boot: server addr: %s\n", inet_ntoa(rootip
));
249 printf("boot: server path: %s\n", rootpath
);