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