1/* $NetBSD: dev_net.c,v 1.2 2021/04/12 03:55:40 mrg 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * This module implements a "raw device" interface suitable for
30 * use by the stand-alone I/O library NFS code.  This interface
31 * does not support any "block" access, and exists only for the
32 * purpose of initializing the network interface, getting boot
33 * parameters, and performing the NFS mount.
34 *
35 * At open time, this does:
36 *
37 * find interface      - netif_open()
38 * RARP for IP address - rarp_getipaddress()
39 * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
40 * RPC/mountd          - nfs_mount(sock, ip, path)
41 *
42 * the root file handle from mountd is saved in a global
43 * for use by the NFS open code (NFS/lookup).
44 */
45
46#include <sys/param.h>
47#include <sys/socket.h>
48#include <net/if.h>
49#include <netinet/in.h>
50#include <netinet/in_systm.h>
51
52#include <lib/libsa/stand.h>
53#include <lib/libsa/net.h>
54#include <lib/libsa/netif.h>
55#include <lib/libsa/bootparam.h>
56#include <lib/libsa/nfs.h>
57
58#include <lib/libkern/libkern.h>
59
60#include <lib/libsa/dev_net.h>
61
62#ifndef SUN_BOOTPARAMS
63void bootp(int);
64#endif
65
66extern int debug;
67extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
68
69/*
70 * Local things...
71 */
72static int netdev_sock = -1;
73static int netdev_opens;
74
75int net_getparams(int);
76
77/*
78 * Called by devopen after it sets f->f_dev to our devsw entry.
79 * This opens the low-level device and sets f->f_devdata.
80 * This is declared with variable arguments...
81 */
82int
83net_open(struct open_file *f, ...)
84{
85	va_list ap;
86	char *devname;		/* Device part of file name (or NULL). */
87	int error = 0;
88
89	va_start(ap, f);
90	devname = va_arg(ap, char*);
91	va_end(ap);
92
93#ifdef	NETIF_DEBUG
94	if (debug)
95		printf("net_open: %s\n", devname);
96#endif
97
98	/* On first open, do netif open, mount, etc. */
99	if (netdev_opens == 0) {
100		/* Find network interface. */
101		if (netdev_sock < 0) {
102			netdev_sock = netif_open(devname);
103			if (netdev_sock < 0) {
104				printf("net_open: netif_open() failed\n");
105				return (ENXIO);
106			}
107			if (debug)
108				printf("net_open: netif_open() succeeded\n");
109		}
110		if (rootip.s_addr == 0) {
111			/* Get root IP address, and path, etc. */
112			error = net_getparams(netdev_sock);
113			if (error) {
114				/* getparams makes its own noise */
115				goto fail;
116			}
117			/* Get the NFS file handle (mountd). */
118			error = nfs_mount(netdev_sock, rootip, rootpath);
119			if (error) {
120				error = errno;
121				printf("net_open: NFS mount error=%d\n", error);
122				rootip.s_addr = 0;
123			fail:
124				netif_close(netdev_sock);
125				netdev_sock = -1;
126				return (error);
127			}
128			if (debug)
129				printf("net_open: NFS mount succeeded\n");
130		}
131	}
132	netdev_opens++;
133	f->f_devdata = nfs_root_node;
134	return (error);
135}
136
137int
138net_close(struct open_file *f)
139{
140
141#ifdef	NETIF_DEBUG
142	if (debug)
143		printf("net_close: opens=%d\n", netdev_opens);
144#endif
145
146	/* On last close, do netif close, etc. */
147	f->f_devdata = NULL;
148	/* Extra close call? */
149	if (netdev_opens <= 0)
150		return (0);
151	netdev_opens--;
152	/* Not last close? */
153	if (netdev_opens > 0)
154		return(0);
155	rootip.s_addr = 0;
156	if (netdev_sock >= 0) {
157		if (debug)
158			printf("net_close: calling netif_close()\n");
159		netif_close(netdev_sock);
160		netdev_sock = -1;
161	}
162	return (0);
163}
164
165int
166net_ioctl(struct open_file *a, u_long b, void *c)
167{
168	return EIO;
169}
170
171int
172net_strategy(void *a, int b, daddr_t c, size_t d, void *e, size_t *f)
173{
174	return EIO;
175}
176
177int
178net_getparams(int sock)
179{
180	/*
181	 * Get info for NFS boot: our IP address, our hostname,
182	 * server IP address, and our root path on the server.
183	 * There are two ways to do this:  The old, Sun way,
184	 * and the more modern, BOOTP way. (RFC951, RFC1048)
185	 */
186
187#ifdef	SUN_BOOTPARAMS
188	/* Get our IP address.  (rarp.c) */
189	if (rarp_getipaddress(sock)) {
190		printf("net_open: RARP failed\n");
191		return (EIO);
192	}
193#else	/* BOOTPARAMS */
194	/*
195	 * Get boot info using BOOTP. (RFC951, RFC1048)
196	 * This also gets the server IP address, gateway,
197	 * root path, etc.
198	 */
199	bootp(sock);
200	if (myip.s_addr == 0) {
201		printf("net_open: BOOTP failed\n");
202		return (EIO);
203	}
204#endif	/* BOOTPARAMS */
205
206	printf("boot: client addr: %s\n", inet_ntoa(myip));
207
208#ifdef	SUN_BOOTPARAMS
209	/* Get our hostname, server IP address, gateway. */
210	if (bp_whoami(sock)) {
211		printf("net_open: bootparam/whoami RPC failed\n");
212		return (EIO);
213	}
214#endif	/* BOOTPARAMS */
215
216	printf("boot: client name: %s\n", hostname);
217	if (gateip.s_addr) {
218		printf("boot: subnet mask: %s\n", intoa(netmask));
219		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
220	}
221
222#ifdef	SUN_BOOTPARAMS
223	/* Get the root pathname. */
224	if (bp_getfile(sock, "root", &rootip, rootpath)) {
225		printf("net_open: bootparam/getfile RPC failed\n");
226		return (EIO);
227	}
228#endif	/* BOOTPARAMS */
229
230	printf("boot: server addr: %s\n", inet_ntoa(rootip));
231	printf("boot: server path: %s\n", rootpath);
232
233	return (0);
234}
235