xref: /netbsd-src/sys/arch/sbmips/stand/netboot/dev_net.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: dev_net.c,v 1.2 2003/03/19 17:21:42 drochner 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 <lib/libsa/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 char hostname[FNAME_SIZE];
91 
92 /*
93  * Local things...
94  */
95 static int netdev_sock = -1;
96 static int netdev_opens;
97 
98 int net_getparams __P((int));
99 
100 /*
101  * Called by devopen after it sets f->f_dev to our devsw entry.
102  * This opens the low-level device and sets f->f_devdata.
103  * This is declared with variable arguments...
104  */
105 int
106 net_open(struct open_file *f, ...)
107 {
108 	va_list ap;
109 	char *devname;		/* Device part of file name (or NULL). */
110 	int error = 0;
111 
112 	va_start(ap, f);
113 	devname = va_arg(ap, char*);
114 	va_end(ap);
115 
116 #ifdef	NETIF_DEBUG
117 	if (debug)
118 		printf("net_open: %s\n", devname);
119 #endif
120 
121 	/* On first open, do netif open, mount, etc. */
122 	if (netdev_opens == 0) {
123 		/* Find network interface. */
124 		if (netdev_sock < 0) {
125 			netdev_sock = netif_open(devname);
126 			if (netdev_sock < 0) {
127 				printf("net_open: netif_open() failed\n");
128 				return (ENXIO);
129 			}
130 			if (debug)
131 				printf("net_open: netif_open() succeeded\n");
132 		}
133 		if (rootip.s_addr == 0) {
134 			/* Get root IP address, and path, etc. */
135 			error = net_getparams(netdev_sock);
136 			if (error) {
137 				/* getparams makes its own noise */
138 				goto fail;
139 			}
140 			/* Get the NFS file handle (mountd). */
141 			error = nfs_mount(netdev_sock, rootip, rootpath);
142 			if (error) {
143 				error = errno;
144 				printf("net_open: NFS mount error=%d\n", error);
145 				rootip.s_addr = 0;
146 			fail:
147 				netif_close(netdev_sock);
148 				netdev_sock = -1;
149 				return (error);
150 			}
151 			if (debug)
152 				printf("net_open: NFS mount succeeded\n");
153 		}
154 	}
155 	netdev_opens++;
156 	f->f_devdata = nfs_root_node;
157 	return (error);
158 }
159 
160 int
161 net_close(f)
162 	struct open_file *f;
163 {
164 
165 #ifdef	NETIF_DEBUG
166 	if (debug)
167 		printf("net_close: opens=%d\n", netdev_opens);
168 #endif
169 
170 	/* On last close, do netif close, etc. */
171 	f->f_devdata = NULL;
172 	/* Extra close call? */
173 	if (netdev_opens <= 0)
174 		return (0);
175 	netdev_opens--;
176 	/* Not last close? */
177 	if (netdev_opens > 0)
178 		return(0);
179 	rootip.s_addr = 0;
180 	if (netdev_sock >= 0) {
181 		if (debug)
182 			printf("net_close: calling netif_close()\n");
183 		netif_close(netdev_sock);
184 		netdev_sock = -1;
185 	}
186 	return (0);
187 }
188 
189 int
190 net_ioctl(struct open_file *a, u_long b, void *c)
191 {
192 	return EIO;
193 }
194 
195 int
196 net_strategy(void *a, int b, daddr_t c, size_t d, void *e, size_t *f)
197 {
198 	return EIO;
199 }
200 
201 int
202 net_getparams(sock)
203 	int sock;
204 {
205 	/*
206 	 * Get info for NFS boot: our IP address, our hostname,
207 	 * server IP address, and our root path on the server.
208 	 * There are two ways to do this:  The old, Sun way,
209 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
210 	 */
211 
212 #ifdef	SUN_BOOTPARAMS
213 	/* Get our IP address.  (rarp.c) */
214 	if (rarp_getipaddress(sock)) {
215 		printf("net_open: RARP failed\n");
216 		return (EIO);
217 	}
218 #else	/* BOOTPARAMS */
219 	/*
220 	 * Get boot info using BOOTP. (RFC951, RFC1048)
221 	 * This also gets the server IP address, gateway,
222 	 * root path, etc.
223 	 */
224 	bootp(sock);
225 	if (myip.s_addr == 0) {
226 		printf("net_open: BOOTP failed\n");
227 		return (EIO);
228 	}
229 #endif	/* BOOTPARAMS */
230 
231 	printf("boot: client addr: %s\n", inet_ntoa(myip));
232 
233 #ifdef	SUN_BOOTPARAMS
234 	/* Get our hostname, server IP address, gateway. */
235 	if (bp_whoami(sock)) {
236 		printf("net_open: bootparam/whoami RPC failed\n");
237 		return (EIO);
238 	}
239 #endif	/* BOOTPARAMS */
240 
241 	printf("boot: client name: %s\n", hostname);
242 	if (gateip.s_addr) {
243 		printf("boot: subnet mask: %s\n", intoa(netmask));
244 		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
245 	}
246 
247 #ifdef	SUN_BOOTPARAMS
248 	/* Get the root pathname. */
249 	if (bp_getfile(sock, "root", &rootip, rootpath)) {
250 		printf("net_open: bootparam/getfile RPC failed\n");
251 		return (EIO);
252 	}
253 #endif	/* BOOTPARAMS */
254 
255 	printf("boot: server addr: %s\n", inet_ntoa(rootip));
256 	printf("boot: server path: %s\n", rootpath);
257 
258 	return (0);
259 }
260