dev_net.c revision 60506
1/*
2 * $FreeBSD: head/sys/boot/common/dev_net.c 60506 2000-05-13 15:40:46Z dfr $
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 *, ...);
83static int	net_close(struct open_file *);
84static int	net_strategy();
85
86static int net_getparams(int sock);
87
88struct devsw netdev = {
89    "net",
90    DEVT_NET,
91    net_init,
92    net_strategy,
93    net_open,
94    net_close,
95    noioctl
96};
97
98int
99net_init(void)
100{
101    return 0;
102}
103
104/*
105 * Called by devopen after it sets f->f_dev to our devsw entry.
106 * This opens the low-level device and sets f->f_devdata.
107 * This is declared with variable arguments...
108 */
109int
110net_open(struct open_file *f, ...)
111{
112    va_list args;
113    char *devname;		/* Device part of file name (or NULL). */
114    int error = 0;
115
116    va_start(args, f);
117    devname = va_arg(args, char*);
118    va_end(args);
119
120    /* On first open, do netif open, mount, etc. */
121    if (netdev_opens == 0) {
122	/* Find network interface. */
123	if (netdev_sock < 0) {
124	    netdev_sock = netif_open(devname);
125	    if (netdev_sock < 0) {
126		printf("net_open: netif_open() failed\n");
127		return (ENXIO);
128	    }
129	    if (debug)
130		printf("net_open: netif_open() succeeded\n");
131	}
132	if (rootip.s_addr == 0) {
133	    /* Get root IP address, and path, etc. */
134	    error = net_getparams(netdev_sock);
135	    if (error) {
136				/* getparams makes its own noise */
137		netif_close(netdev_sock);
138		netdev_sock = -1;
139		return (error);
140	    }
141	}
142	netdev_opens++;
143    }
144    netdev_opens++;
145    f->f_devdata = &netdev_sock;
146    return (error);
147}
148
149int
150net_close(f)
151    struct open_file *f;
152{
153
154#ifdef	NETIF_DEBUG
155    if (debug)
156	printf("net_close: opens=%d\n", netdev_opens);
157#endif
158
159    /* On last close, do netif close, etc. */
160    f->f_devdata = NULL;
161    /* Extra close call? */
162    if (netdev_opens <= 0)
163	return (0);
164    netdev_opens--;
165    /* Not last close? */
166    if (netdev_opens > 0)
167	return(0);
168    rootip.s_addr = 0;
169    if (netdev_sock >= 0) {
170	if (debug)
171	    printf("net_close: calling netif_close()\n");
172	netif_close(netdev_sock);
173	netdev_sock = -1;
174    }
175    return (0);
176}
177
178int
179net_strategy()
180{
181    return EIO;
182}
183
184#define SUPPORT_BOOTP
185
186/*
187 * Get info for NFS boot: our IP address, our hostname,
188 * server IP address, and our root path on the server.
189 * There are two ways to do this:  The old, Sun way,
190 * and the more modern, BOOTP way. (RFC951, RFC1048)
191 *
192 * The default is to use the Sun bootparams RPC
193 * (because that is what the kernel will do).
194 * MD code can make try_bootp initialied data,
195 * which will override this common definition.
196 */
197#ifdef	SUPPORT_BOOTP
198int try_bootp = 1;
199int bootp(int sock);
200#endif
201
202extern n_long ip_convertaddr(char *p);
203
204static int
205net_getparams(sock)
206    int sock;
207{
208    char buf[MAXHOSTNAMELEN];
209    char temp[FNAME_SIZE];
210    int i;
211    n_long smask;
212
213#ifdef	SUPPORT_BOOTP
214    /*
215     * Try to get boot info using BOOTP.  If we succeed, then
216     * the server IP address, gateway, and root path will all
217     * be initialized.  If any remain uninitialized, we will
218     * use RARP and RPC/bootparam (the Sun way) to get them.
219     */
220    if (try_bootp)
221	bootp(sock);
222    if (myip.s_addr != 0)
223	goto exit;
224    if (debug)
225	printf("net_open: BOOTP failed, trying RARP/RPC...\n");
226#endif
227
228    /*
229     * Use RARP to get our IP address.  This also sets our
230     * netmask to the "natural" default for our address.
231     */
232    if (rarp_getipaddress(sock)) {
233	printf("net_open: RARP failed\n");
234	return (EIO);
235    }
236    printf("net_open: client addr: %s\n", inet_ntoa(myip));
237
238    /* Get our hostname, server IP address, gateway. */
239    if (bp_whoami(sock)) {
240	printf("net_open: bootparam/whoami RPC failed\n");
241	return (EIO);
242    }
243    printf("net_open: client name: %s\n", hostname);
244
245    /*
246     * Ignore the gateway from whoami (unreliable).
247     * Use the "gateway" parameter instead.
248     */
249    smask = 0;
250    gateip.s_addr = 0;
251    if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
252	/* Got it!  Parse the netmask. */
253	smask = ip_convertaddr(buf);
254    }
255    if (smask) {
256	netmask = smask;
257	printf("net_open: subnet mask: %s\n", intoa(netmask));
258    }
259    if (gateip.s_addr)
260	printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
261
262    /* Get the root server and pathname. */
263    if (bp_getfile(sock, "root", &rootip, rootpath)) {
264	printf("net_open: bootparam/getfile RPC failed\n");
265	return (EIO);
266    }
267 exit:
268    printf("net_open: server addr: %s\n", inet_ntoa(rootip));
269
270    /*
271     * If present, strip the server's address off of the rootpath
272     * before passing it along.  This allows us to be compatible with
273     * the kernel's diskless (BOOTP_NFSROOT) booting conventions
274     */
275
276    for(i=0; i<FNAME_SIZE; i++)
277	    if(rootpath[i] == ':')
278		    break;
279    if(i && i != FNAME_SIZE) {
280	    i++;
281	    bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
282	    bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
283    }
284    printf("net_open: server path: %s\n", rootpath);
285    return (0);
286}
287