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