xref: /netbsd/sys/arch/alpha/stand/netboot/dev_net.c (revision c4a72b64)
1 /* $NetBSD: dev_net.c,v 1.11 2002/11/09 06:34:38 thorpej 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 <machine/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/in_systm.h>
57 
58 #include <lib/libsa/stand.h>
59 #include <lib/libsa/net.h>
60 #include <lib/libsa/netif.h>
61 #include <lib/libsa/bootparam.h>
62 #include <lib/libsa/nfs.h>
63 
64 #include <lib/libkern/libkern.h>
65 
66 #include "dev_net.h"
67 
68 #ifndef SUN_BOOTPARAMS
69 void bootp      __P((int));
70 #endif
71 
72 extern int debug;
73 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
74 
75 /*
76  * Various globals needed by the network code:
77  */
78 
79 #if 0
80 /* for arp.c, rarp.c */
81 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
82 #endif
83 
84 struct	in_addr myip;		/* my ip address */
85 struct	in_addr rootip;		/* root ip address */
86 struct	in_addr gateip;		/* swap ip address */
87 n_long	netmask;		/* subnet or net mask */
88 
89 char rootpath[FNAME_SIZE];
90 
91 int hostnamelen;
92 char hostname[FNAME_SIZE];
93 
94 int domainnamelen;
95 char domainname[FNAME_SIZE];
96 
97 /*
98  * Local things...
99  */
100 static int netdev_sock = -1;
101 static int netdev_opens;
102 
103 int net_getparams __P((int));
104 
105 /*
106  * Called by devopen after it sets f->f_dev to our devsw entry.
107  * This opens the low-level device and sets f->f_devdata.
108  * This is declared with variable arguments...
109  */
110 int
111 net_open(struct open_file *f, ...)
112 {
113 	va_list ap;
114 	char *devname;		/* Device part of file name (or NULL). */
115 	int error = 0;
116 
117 	va_start(ap, f);
118 	devname = va_arg(ap, char*);
119 	va_end(ap);
120 
121 #ifdef	NETIF_DEBUG
122 	if (debug)
123 		printf("net_open: %s\n", devname);
124 #endif
125 
126 	/* On first open, do netif open, mount, etc. */
127 	if (netdev_opens == 0) {
128 		/* Find network interface. */
129 		if (netdev_sock < 0) {
130 			netdev_sock = netif_open(devname);
131 			if (netdev_sock < 0) {
132 				printf("net_open: netif_open() failed\n");
133 				return (ENXIO);
134 			}
135 			if (debug)
136 				printf("net_open: netif_open() succeeded\n");
137 		}
138 		if (rootip.s_addr == 0) {
139 			/* Get root IP address, and path, etc. */
140 			error = net_getparams(netdev_sock);
141 			if (error) {
142 				/* getparams makes its own noise */
143 				goto fail;
144 			}
145 			/* Get the NFS file handle (mountd). */
146 			error = nfs_mount(netdev_sock, rootip, rootpath);
147 			if (error) {
148 				error = errno;
149 				printf("net_open: NFS mount error=%d\n", error);
150 				rootip.s_addr = 0;
151 			fail:
152 				netif_close(netdev_sock);
153 				netdev_sock = -1;
154 				return (error);
155 			}
156 			if (debug)
157 				printf("net_open: NFS mount succeeded\n");
158 		}
159 	}
160 	netdev_opens++;
161 	f->f_devdata = nfs_root_node;
162 	return (error);
163 }
164 
165 int
166 net_close(f)
167 	struct open_file *f;
168 {
169 
170 #ifdef	NETIF_DEBUG
171 	if (debug)
172 		printf("net_close: opens=%d\n", netdev_opens);
173 #endif
174 
175 	/* On last close, do netif close, etc. */
176 	f->f_devdata = NULL;
177 	/* Extra close call? */
178 	if (netdev_opens <= 0)
179 		return (0);
180 	netdev_opens--;
181 	/* Not last close? */
182 	if (netdev_opens > 0)
183 		return(0);
184 	rootip.s_addr = 0;
185 	if (netdev_sock >= 0) {
186 		if (debug)
187 			printf("net_close: calling netif_close()\n");
188 		netif_close(netdev_sock);
189 		netdev_sock = -1;
190 	}
191 	return (0);
192 }
193 
194 int
195 net_ioctl(f, cmd, data)
196 	struct open_file *f;
197 	u_long cmd;
198 	void *data;
199 {
200 	return EIO;
201 }
202 
203 int
204 net_strategy(devdata, rw, blk, size, buf, rsize)
205 	void *devdata;
206 	int rw;
207 	daddr_t blk;
208 	size_t size;
209 	void *buf;
210 	size_t *rsize;
211 {
212 	return EIO;
213 }
214 
215 int
216 net_getparams(sock)
217 	int sock;
218 {
219 	/*
220 	 * Get info for NFS boot: our IP address, our hostname,
221 	 * server IP address, and our root path on the server.
222 	 * There are two ways to do this:  The old, Sun way,
223 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
224 	 */
225 
226 #ifdef	SUN_BOOTPARAMS
227 	/* Get our IP address.  (rarp.c) */
228 	if (rarp_getipaddress(sock)) {
229 		printf("net_open: RARP failed\n");
230 		return (EIO);
231 	}
232 #else	/* BOOTPARAMS */
233 	/*
234 	 * Get boot info using BOOTP. (RFC951, RFC1048)
235 	 * This also gets the server IP address, gateway,
236 	 * root path, etc.
237 	 */
238 	bootp(sock);
239 	if (myip.s_addr == 0) {
240 		printf("net_open: BOOTP failed\n");
241 		return (EIO);
242 	}
243 #endif	/* BOOTPARAMS */
244 
245 	printf("boot: client addr: %s\n", inet_ntoa(myip));
246 
247 #ifdef	SUN_BOOTPARAMS
248 	/* Get our hostname, server IP address, gateway. */
249 	if (bp_whoami(sock)) {
250 		printf("net_open: bootparam/whoami RPC failed\n");
251 		return (EIO);
252 	}
253 #endif	/* BOOTPARAMS */
254 
255 	printf("boot: client name: %s\n", hostname);
256 	if (gateip.s_addr) {
257 		printf("boot: subnet mask: %s\n", intoa(netmask));
258 		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
259 	}
260 
261 #ifdef	SUN_BOOTPARAMS
262 	/* Get the root pathname. */
263 	if (bp_getfile(sock, "root", &rootip, rootpath)) {
264 		printf("net_open: bootparam/getfile RPC failed\n");
265 		return (EIO);
266 	}
267 #endif	/* BOOTPARAMS */
268 
269 	printf("boot: server addr: %s\n", inet_ntoa(rootip));
270 	printf("boot: server path: %s\n", rootpath);
271 
272 	return (0);
273 }
274