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