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