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