dev_net.c revision 46356
1/*
2 * $Id: dev_net.c,v 1.3 1998/09/20 21:46:19 dfr Exp $
3 * From: $NetBSD: dev_net.c,v 1.12 1997/12/10 20:38:37 gwr Exp $
4 */
5
6/*-
7 * Copyright (c) 1997 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Gordon W. Ross.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *        This product includes software developed by the NetBSD
24 *        Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 *    contributors may be used to endorse or promote products derived
27 *    from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42/*
43 * This module implements a "raw device" interface suitable for
44 * use by the stand-alone I/O library NFS code.  This interface
45 * does not support any "block" access, and exists only for the
46 * purpose of initializing the network interface, getting boot
47 * parameters, and performing the NFS mount.
48 *
49 * At open time, this does:
50 *
51 * find interface      - netif_open()
52 * RARP for IP address - rarp_getipaddress()
53 * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
54 * RPC/mountd          - nfs_mount(sock, ip, path)
55 *
56 * the root file handle from mountd is saved in a global
57 * for use by the NFS open code (NFS/lookup).
58 */
59
60#include <machine/stdarg.h>
61#include <sys/param.h>
62#include <sys/socket.h>
63#include <net/if.h>
64#include <netinet/in.h>
65#include <netinet/in_systm.h>
66
67#include <stand.h>
68#include <string.h>
69#include <net.h>
70#include <netif.h>
71#include <bootparam.h>
72
73#include "dev_net.h"
74#include "bootstrap.h"
75
76int debug = 0;
77
78static int netdev_sock = -1;
79static int netdev_opens;
80
81static int	net_init(void);
82static int	net_open(struct open_file *, void *vdev);
83static int	net_close(struct open_file *);
84static int	net_ioctl();
85static int	net_strategy();
86
87static int net_getparams(int sock);
88
89struct devsw netdev = {
90    "net",
91    DEVT_NET,
92    net_init,
93    net_strategy,
94    net_open,
95    net_close,
96    noioctl
97};
98
99int
100net_init(void)
101{
102    return 0;
103}
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 */
110int
111net_open(struct open_file *f, void *vdev)
112{
113    char *devname;		/* Device part of file name (or NULL). */
114    int error = 0;
115
116    devname = vdev;
117
118    /* On first open, do netif open, mount, etc. */
119    if (netdev_opens == 0) {
120	/* Find network interface. */
121	if (netdev_sock < 0) {
122	    netdev_sock = netif_open(devname);
123	    if (netdev_sock < 0) {
124		printf("net_open: netif_open() failed\n");
125		return (ENXIO);
126	    }
127	    if (debug)
128		printf("net_open: netif_open() succeeded\n");
129	}
130	if (rootip.s_addr == 0) {
131	    /* Get root IP address, and path, etc. */
132	    error = net_getparams(netdev_sock);
133	    if (error) {
134				/* getparams makes its own noise */
135		netif_close(netdev_sock);
136		netdev_sock = -1;
137		return (error);
138	    }
139	}
140    }
141    netdev_opens++;
142    f->f_devdata = &netdev_sock;
143    return (error);
144}
145
146int
147net_close(f)
148    struct open_file *f;
149{
150
151#ifdef	NETIF_DEBUG
152    if (debug)
153	printf("net_close: opens=%d\n", netdev_opens);
154#endif
155
156    /* On last close, do netif close, etc. */
157    f->f_devdata = NULL;
158    /* Extra close call? */
159    if (netdev_opens <= 0)
160	return (0);
161    netdev_opens--;
162    /* Not last close? */
163    if (netdev_opens > 0)
164	return(0);
165    rootip.s_addr = 0;
166    if (netdev_sock >= 0) {
167	if (debug)
168	    printf("net_close: calling netif_close()\n");
169	netif_close(netdev_sock);
170	netdev_sock = -1;
171    }
172    return (0);
173}
174
175int
176net_ioctl()
177{
178    return EIO;
179}
180
181int
182net_strategy()
183{
184    return EIO;
185}
186
187#define SUPPORT_BOOTP
188
189/*
190 * Get info for NFS boot: our IP address, our hostname,
191 * server IP address, and our root path on the server.
192 * There are two ways to do this:  The old, Sun way,
193 * and the more modern, BOOTP way. (RFC951, RFC1048)
194 *
195 * The default is to use the Sun bootparams RPC
196 * (because that is what the kernel will do).
197 * MD code can make try_bootp initialied data,
198 * which will override this common definition.
199 */
200#ifdef	SUPPORT_BOOTP
201int try_bootp = 1;
202int bootp(int sock);
203#endif
204
205static int
206net_getparams(sock)
207    int sock;
208{
209    char buf[MAXHOSTNAMELEN];
210    char temp[FNAME_SIZE];
211    int i;
212    n_long smask;
213
214#ifdef	SUPPORT_BOOTP
215    /*
216     * Try to get boot info using BOOTP.  If we succeed, then
217     * the server IP address, gateway, and root path will all
218     * be initialized.  If any remain uninitialized, we will
219     * use RARP and RPC/bootparam (the Sun way) to get them.
220     */
221    if (try_bootp)
222	bootp(sock);
223    if (myip.s_addr != 0)
224	goto exit;
225    if (debug)
226	printf("net_open: BOOTP failed, trying RARP/RPC...\n");
227#endif
228
229    /*
230     * Use RARP to get our IP address.  This also sets our
231     * netmask to the "natural" default for our address.
232     */
233    if (rarp_getipaddress(sock)) {
234	printf("net_open: RARP failed\n");
235	return (EIO);
236    }
237    printf("net_open: client addr: %s\n", inet_ntoa(myip));
238
239    /* Get our hostname, server IP address, gateway. */
240    if (bp_whoami(sock)) {
241	printf("net_open: bootparam/whoami RPC failed\n");
242	return (EIO);
243    }
244    printf("net_open: client name: %s\n", hostname);
245
246    /*
247     * Ignore the gateway from whoami (unreliable).
248     * Use the "gateway" parameter instead.
249     */
250    smask = 0;
251    gateip.s_addr = 0;
252    if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
253	/* Got it!  Parse the netmask. */
254	smask = ip_convertaddr(buf);
255    }
256    if (smask) {
257	netmask = smask;
258	printf("net_open: subnet mask: %s\n", intoa(netmask));
259    }
260    if (gateip.s_addr)
261	printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
262
263    /* Get the root server and pathname. */
264    if (bp_getfile(sock, "root", &rootip, rootpath)) {
265	printf("net_open: bootparam/getfile RPC failed\n");
266	return (EIO);
267    }
268 exit:
269    printf("net_open: server addr: %s\n", inet_ntoa(rootip));
270
271    /*
272     * If present, strip the server's address off of the rootpath
273     * before passing it along.  This allows us to be compatible with
274     * the kernel's diskless (BOOTP_NFSROOT) booting conventions
275     */
276
277    for(i=0; i<FNAME_SIZE; i++)
278	    if(rootpath[i] == ':')
279		    break;
280    if(i && i != FNAME_SIZE) {
281	    i++;
282	    bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
283	    bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
284    }
285    printf("net_open: server path: %s\n", rootpath);
286    return (0);
287}
288