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