dev_net.c revision 1.1.16.2 1 1.1.16.2 jdolecek /* $NetBSD: dev_net.c,v 1.1.16.2 2002/06/23 17:37:35 jdolecek Exp $ */
2 1.1.16.2 jdolecek
3 1.1.16.2 jdolecek /*-
4 1.1.16.2 jdolecek * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1.16.2 jdolecek * All rights reserved.
6 1.1.16.2 jdolecek *
7 1.1.16.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.1.16.2 jdolecek * by Gordon W. Ross.
9 1.1.16.2 jdolecek *
10 1.1.16.2 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.1.16.2 jdolecek * modification, are permitted provided that the following conditions
12 1.1.16.2 jdolecek * are met:
13 1.1.16.2 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.1.16.2 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.1.16.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.16.2 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.1.16.2 jdolecek * documentation and/or other materials provided with the distribution.
18 1.1.16.2 jdolecek * 3. All advertising materials mentioning features or use of this software
19 1.1.16.2 jdolecek * must display the following acknowledgement:
20 1.1.16.2 jdolecek * This product includes software developed by the NetBSD
21 1.1.16.2 jdolecek * Foundation, Inc. and its contributors.
22 1.1.16.2 jdolecek * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.16.2 jdolecek * contributors may be used to endorse or promote products derived
24 1.1.16.2 jdolecek * from this software without specific prior written permission.
25 1.1.16.2 jdolecek *
26 1.1.16.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.16.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.16.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.16.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.16.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.16.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.16.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.16.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.16.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.16.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.16.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
37 1.1.16.2 jdolecek */
38 1.1.16.2 jdolecek
39 1.1.16.2 jdolecek /*
40 1.1.16.2 jdolecek * This module implements a "raw device" interface suitable for
41 1.1.16.2 jdolecek * use by the stand-alone I/O library NFS code. This interface
42 1.1.16.2 jdolecek * does not support any "block" access, and exists only for the
43 1.1.16.2 jdolecek * purpose of initializing the network interface, getting boot
44 1.1.16.2 jdolecek * parameters, and performing the NFS mount.
45 1.1.16.2 jdolecek *
46 1.1.16.2 jdolecek * At open time, this does:
47 1.1.16.2 jdolecek *
48 1.1.16.2 jdolecek * find interface - netif_open()
49 1.1.16.2 jdolecek * RARP for IP address - rarp_getipaddress()
50 1.1.16.2 jdolecek * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
51 1.1.16.2 jdolecek * RPC/mountd - nfs_mount(sock, ip, path)
52 1.1.16.2 jdolecek *
53 1.1.16.2 jdolecek * the root file handle from mountd is saved in a global
54 1.1.16.2 jdolecek * for use by the NFS open code (NFS/lookup).
55 1.1.16.2 jdolecek */
56 1.1.16.2 jdolecek
57 1.1.16.2 jdolecek #include <machine/stdarg.h>
58 1.1.16.2 jdolecek #include <sys/param.h>
59 1.1.16.2 jdolecek #include <sys/socket.h>
60 1.1.16.2 jdolecek #include <net/if.h>
61 1.1.16.2 jdolecek #include <netinet/in.h>
62 1.1.16.2 jdolecek #include <netinet/in_systm.h>
63 1.1.16.2 jdolecek
64 1.1.16.2 jdolecek #include <lib/libkern/libkern.h>
65 1.1.16.2 jdolecek
66 1.1.16.2 jdolecek #include "stand.h"
67 1.1.16.2 jdolecek #include "net.h"
68 1.1.16.2 jdolecek #include "netif.h"
69 1.1.16.2 jdolecek #include "nfs.h"
70 1.1.16.2 jdolecek #include "bootparam.h"
71 1.1.16.2 jdolecek #include "dev_net.h"
72 1.1.16.2 jdolecek
73 1.1.16.2 jdolecek extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
74 1.1.16.2 jdolecek
75 1.1.16.2 jdolecek static int netdev_sock = -1;
76 1.1.16.2 jdolecek static int netdev_opens;
77 1.1.16.2 jdolecek
78 1.1.16.2 jdolecek static int net_getparams __P((int sock));
79 1.1.16.2 jdolecek
80 1.1.16.2 jdolecek /*
81 1.1.16.2 jdolecek * Called by devopen after it sets f->f_dev to our devsw entry.
82 1.1.16.2 jdolecek * This opens the low-level device and sets f->f_devdata.
83 1.1.16.2 jdolecek * This is declared with variable arguments...
84 1.1.16.2 jdolecek */
85 1.1.16.2 jdolecek int
86 1.1.16.2 jdolecek net_open(struct open_file *f, ...)
87 1.1.16.2 jdolecek {
88 1.1.16.2 jdolecek va_list ap;
89 1.1.16.2 jdolecek char *devname; /* Device part of file name (or NULL). */
90 1.1.16.2 jdolecek int error = 0;
91 1.1.16.2 jdolecek
92 1.1.16.2 jdolecek va_start(ap, f);
93 1.1.16.2 jdolecek devname = va_arg(ap, char*);
94 1.1.16.2 jdolecek va_end(ap);
95 1.1.16.2 jdolecek
96 1.1.16.2 jdolecek #ifdef NETIF_DEBUG
97 1.1.16.2 jdolecek if (debug)
98 1.1.16.2 jdolecek printf("net_open: %s\n", devname);
99 1.1.16.2 jdolecek #endif
100 1.1.16.2 jdolecek
101 1.1.16.2 jdolecek /* On first open, do netif open, mount, etc. */
102 1.1.16.2 jdolecek if (netdev_opens == 0) {
103 1.1.16.2 jdolecek /* Find network interface. */
104 1.1.16.2 jdolecek if (netdev_sock < 0) {
105 1.1.16.2 jdolecek netdev_sock = netif_open(devname);
106 1.1.16.2 jdolecek if (netdev_sock < 0) {
107 1.1.16.2 jdolecek printf("net_open: netif_open() failed\n");
108 1.1.16.2 jdolecek return (ENXIO);
109 1.1.16.2 jdolecek }
110 1.1.16.2 jdolecek if (debug)
111 1.1.16.2 jdolecek printf("net_open: netif_open() succeeded\n");
112 1.1.16.2 jdolecek }
113 1.1.16.2 jdolecek if (rootip.s_addr == 0) {
114 1.1.16.2 jdolecek /* Get root IP address, and path, etc. */
115 1.1.16.2 jdolecek error = net_getparams(netdev_sock);
116 1.1.16.2 jdolecek if (error) {
117 1.1.16.2 jdolecek /* getparams makes its own noise */
118 1.1.16.2 jdolecek goto fail;
119 1.1.16.2 jdolecek }
120 1.1.16.2 jdolecek /* Get the NFS file handle (mountd). */
121 1.1.16.2 jdolecek error = nfs_mount(netdev_sock, rootip, rootpath);
122 1.1.16.2 jdolecek if (error) {
123 1.1.16.2 jdolecek printf("net_open: NFS mount error=%d\n", error);
124 1.1.16.2 jdolecek rootip.s_addr = 0;
125 1.1.16.2 jdolecek fail:
126 1.1.16.2 jdolecek netif_close(netdev_sock);
127 1.1.16.2 jdolecek netdev_sock = -1;
128 1.1.16.2 jdolecek return (error);
129 1.1.16.2 jdolecek }
130 1.1.16.2 jdolecek if (debug)
131 1.1.16.2 jdolecek printf("net_open: NFS mount succeeded\n");
132 1.1.16.2 jdolecek }
133 1.1.16.2 jdolecek }
134 1.1.16.2 jdolecek netdev_opens++;
135 1.1.16.2 jdolecek f->f_devdata = nfs_root_node;
136 1.1.16.2 jdolecek return (error);
137 1.1.16.2 jdolecek }
138 1.1.16.2 jdolecek
139 1.1.16.2 jdolecek int
140 1.1.16.2 jdolecek net_close(f)
141 1.1.16.2 jdolecek struct open_file *f;
142 1.1.16.2 jdolecek {
143 1.1.16.2 jdolecek
144 1.1.16.2 jdolecek #ifdef NETIF_DEBUG
145 1.1.16.2 jdolecek if (debug)
146 1.1.16.2 jdolecek printf("net_close: opens=%d\n", netdev_opens);
147 1.1.16.2 jdolecek #endif
148 1.1.16.2 jdolecek
149 1.1.16.2 jdolecek /* On last close, do netif close, etc. */
150 1.1.16.2 jdolecek f->f_devdata = NULL;
151 1.1.16.2 jdolecek /* Extra close call? */
152 1.1.16.2 jdolecek if (netdev_opens <= 0)
153 1.1.16.2 jdolecek return (0);
154 1.1.16.2 jdolecek netdev_opens--;
155 1.1.16.2 jdolecek /* Not last close? */
156 1.1.16.2 jdolecek if (netdev_opens > 0)
157 1.1.16.2 jdolecek return(0);
158 1.1.16.2 jdolecek rootip.s_addr = 0;
159 1.1.16.2 jdolecek if (netdev_sock >= 0) {
160 1.1.16.2 jdolecek if (debug)
161 1.1.16.2 jdolecek printf("net_close: calling netif_close()\n");
162 1.1.16.2 jdolecek netif_close(netdev_sock);
163 1.1.16.2 jdolecek netdev_sock = -1;
164 1.1.16.2 jdolecek }
165 1.1.16.2 jdolecek return (0);
166 1.1.16.2 jdolecek }
167 1.1.16.2 jdolecek
168 1.1.16.2 jdolecek int
169 1.1.16.2 jdolecek net_ioctl(f, cmd, data)
170 1.1.16.2 jdolecek struct open_file *f;
171 1.1.16.2 jdolecek u_long cmd;
172 1.1.16.2 jdolecek void *data;
173 1.1.16.2 jdolecek {
174 1.1.16.2 jdolecek return EIO;
175 1.1.16.2 jdolecek }
176 1.1.16.2 jdolecek
177 1.1.16.2 jdolecek int
178 1.1.16.2 jdolecek net_strategy(devdata, rw, blk, size, buf, rsize)
179 1.1.16.2 jdolecek void *devdata;
180 1.1.16.2 jdolecek int rw;
181 1.1.16.2 jdolecek daddr_t blk;
182 1.1.16.2 jdolecek size_t size;
183 1.1.16.2 jdolecek void *buf;
184 1.1.16.2 jdolecek size_t *rsize;
185 1.1.16.2 jdolecek {
186 1.1.16.2 jdolecek return EIO;
187 1.1.16.2 jdolecek }
188 1.1.16.2 jdolecek
189 1.1.16.2 jdolecek
190 1.1.16.2 jdolecek /*
191 1.1.16.2 jdolecek * Get info for NFS boot: our IP address, our hostname,
192 1.1.16.2 jdolecek * server IP address, and our root path on the server.
193 1.1.16.2 jdolecek * There are two ways to do this: The old, Sun way,
194 1.1.16.2 jdolecek * and the more modern, BOOTP way. (RFC951, RFC1048)
195 1.1.16.2 jdolecek *
196 1.1.16.2 jdolecek * The default is to use the Sun bootparams RPC
197 1.1.16.2 jdolecek * (because that is what the kernel will do).
198 1.1.16.2 jdolecek * MD code can make try_bootp initialied data,
199 1.1.16.2 jdolecek * which will override this common definition.
200 1.1.16.2 jdolecek */
201 1.1.16.2 jdolecek #ifdef SUPPORT_BOOTP
202 1.1.16.2 jdolecek int try_bootp;
203 1.1.16.2 jdolecek int bootp __P((int sock));
204 1.1.16.2 jdolecek #endif
205 1.1.16.2 jdolecek
206 1.1.16.2 jdolecek static int
207 1.1.16.2 jdolecek net_getparams(sock)
208 1.1.16.2 jdolecek int sock;
209 1.1.16.2 jdolecek {
210 1.1.16.2 jdolecek char buf[MAXHOSTNAMELEN];
211 1.1.16.2 jdolecek n_long smask;
212 1.1.16.2 jdolecek #ifdef SUPPORT_BOOTP
213 1.1.16.2 jdolecek /*
214 1.1.16.2 jdolecek * Try to get boot info using BOOTP. If we succeed, then
215 1.1.16.2 jdolecek * the server IP address, gateway, and root path will all
216 1.1.16.2 jdolecek * be initialized. If any remain uninitialized, we will
217 1.1.16.2 jdolecek * use RARP and RPC/bootparam (the Sun way) to get them.
218 1.1.16.2 jdolecek */
219 1.1.16.2 jdolecek if (try_bootp)
220 1.1.16.2 jdolecek bootp(sock);
221 1.1.16.2 jdolecek if (myip.s_addr != 0)
222 1.1.16.2 jdolecek return (0);
223 1.1.16.2 jdolecek if (debug)
224 1.1.16.2 jdolecek printf("net_open: BOOTP failed, trying RARP/RPC...\n");
225 1.1.16.2 jdolecek #endif
226 1.1.16.2 jdolecek
227 1.1.16.2 jdolecek /*
228 1.1.16.2 jdolecek * Use RARP to get our IP address. This also sets our
229 1.1.16.2 jdolecek * netmask to the "natural" default for our address.
230 1.1.16.2 jdolecek */
231 1.1.16.2 jdolecek if (rarp_getipaddress(sock)) {
232 1.1.16.2 jdolecek printf("net_open: RARP failed\n");
233 1.1.16.2 jdolecek return (EIO);
234 1.1.16.2 jdolecek }
235 1.1.16.2 jdolecek printf("net_open: client addr: %s\n", inet_ntoa(myip));
236 1.1.16.2 jdolecek
237 1.1.16.2 jdolecek /* Get our hostname, server IP address, gateway. */
238 1.1.16.2 jdolecek if (bp_whoami(sock)) {
239 1.1.16.2 jdolecek printf("net_open: bootparam/whoami RPC failed\n");
240 1.1.16.2 jdolecek return (EIO);
241 1.1.16.2 jdolecek }
242 1.1.16.2 jdolecek printf("net_open: client name: %s\n", hostname);
243 1.1.16.2 jdolecek
244 1.1.16.2 jdolecek /*
245 1.1.16.2 jdolecek * Ignore the gateway from whoami (unreliable).
246 1.1.16.2 jdolecek * Use the "gateway" parameter instead.
247 1.1.16.2 jdolecek */
248 1.1.16.2 jdolecek smask = 0;
249 1.1.16.2 jdolecek gateip.s_addr = 0;
250 1.1.16.2 jdolecek if (bp_getfile(sock, "gateway", &gateip, buf))
251 1.1.16.2 jdolecek printf("nfs_open: gateway bootparam missing\n");
252 1.1.16.2 jdolecek else {
253 1.1.16.2 jdolecek /* Got it! Parse the netmask. */
254 1.1.16.2 jdolecek smask = inet_addr(buf);
255 1.1.16.2 jdolecek }
256 1.1.16.2 jdolecek if (smask) {
257 1.1.16.2 jdolecek netmask = smask;
258 1.1.16.2 jdolecek printf("net_open: subnet mask: %s\n", intoa(netmask));
259 1.1.16.2 jdolecek }
260 1.1.16.2 jdolecek if (gateip.s_addr)
261 1.1.16.2 jdolecek printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
262 1.1.16.2 jdolecek
263 1.1.16.2 jdolecek /* Get the root server and pathname. */
264 1.1.16.2 jdolecek if (bp_getfile(sock, "root", &rootip, rootpath)) {
265 1.1.16.2 jdolecek printf("net_open: bootparam/getfile RPC failed\n");
266 1.1.16.2 jdolecek return (EIO);
267 1.1.16.2 jdolecek }
268 1.1.16.2 jdolecek
269 1.1.16.2 jdolecek printf("net_open: server addr: %s\n", inet_ntoa(rootip));
270 1.1.16.2 jdolecek printf("net_open: server path: %s\n", rootpath);
271 1.1.16.2 jdolecek
272 1.1.16.2 jdolecek return (0);
273 1.1.16.2 jdolecek }
274