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