xref: /netbsd-src/sys/lib/libsa/dev_net.c (revision 9573504567626934c7ee01c7dce0c4bb1dfe7403)
1 /*	$NetBSD: dev_net.c,v 1.3 1995/09/23 03:42:51 gwr 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 <sys/param.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #include <netinet/in_systm.h>
57 
58 #include "stand.h"
59 #include "net.h"
60 #include "netif.h"
61 #include "bootparam.h"
62 
63 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
64 
65 /*
66  * Various globals needed by the network code:
67  */
68 
69 /* for arp.c, rarp.c */
70 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
71 
72 struct	in_addr myip;		/* my ip address */
73 struct	in_addr rootip;		/* root ip address */
74 struct	in_addr gateip;		/* swap ip address */
75 n_long	netmask;		/* subnet or net mask */
76 
77 char rootpath[FNAME_SIZE];
78 
79 int hostnamelen;
80 char hostname[FNAME_SIZE];
81 
82 int domainnamelen;
83 char domainname[FNAME_SIZE];
84 
85 /*
86  * Local things...
87  */
88 static int netdev_sock = -1;
89 static int open_count;
90 
91 /*
92  * Called by devopen after it sets f->f_dev to our devsw entry.
93  * This opens the low-level device and sets f->f_devdata.
94  */
95 int
96 net_open(f, devname)
97 	struct open_file *f;
98 	char *devname;		/* Device part of file name (or NULL). */
99 {
100 	int error = 0;
101 
102 	/* On first open, do netif open, mount, etc. */
103 	if (open_count == 0) {
104 		/* Find network interface. */
105 		if ((netdev_sock = netif_open(devname)) < 0)
106 			return (error=ENXIO);
107 		if ((error = net_mountroot(f, devname)) != 0)
108 			return (error);
109 	}
110 	open_count++;
111 	f->f_devdata = nfs_root_node;
112 	return (error);
113 }
114 
115 int
116 net_close(f)
117 	struct open_file *f;
118 {
119 	/* On last close, do netif close, etc. */
120 	if (open_count > 0)
121 		if (--open_count == 0)
122 			netif_close(netdev_sock);
123 	f->f_devdata = NULL;
124 }
125 
126 int
127 net_ioctl()
128 {
129 	return EIO;
130 }
131 
132 int
133 net_strategy()
134 {
135 	return EIO;
136 }
137 
138 int
139 net_mountroot(f, devname)
140 	struct open_file *f;
141 	char *devname;		/* Device part of file name (or NULL). */
142 {
143 	int error;
144 
145 #ifdef DEBUG
146 	printf("net_mountroot: %s\n", devname);
147 #endif
148 
149 	/*
150 	 * Get info for NFS boot: our IP address, our hostname,
151 	 * server IP address, and our root path on the server.
152 	 * There are two ways to do this:  The old, Sun way,
153 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
154 	 */
155 
156 #ifdef	SUN_BOOTPARAMS
157 	/* Get our IP address.  (rarp.c) */
158 	if (rarp_getipaddress(netdev_sock))
159 		return (EIO);
160 #else	/* BOOTPARAMS */
161 	/*
162 	 * Get boot info using BOOTP. (RFC951, RFC1048)
163 	 * This also gets the server IP address, gateway,
164 	 * root path, etc.
165 	 */
166 	bootp(netdev_sock);	/* XXX - Error return? */
167 #endif	/* BOOTPARAMS */
168 
169 	printf("boot: client addr: %s\n", inet_ntoa(myip));
170 
171 #ifdef	SUN_BOOTPARAMS
172 	/* Get our hostname, server IP address, gateway. */
173 	if (bp_whoami(netdev_sock))
174 		return (EIO);
175 #endif	/* BOOTPARAMS */
176 
177 	printf("boot: client name: %s\n", hostname);
178 	if (gateip.s_addr) {
179 		printf("boot: subnet mask: %s\n", intoa(netmask));
180 		printf("boot: net gateway: %s\n", inet_ntoa(gateip));
181 	}
182 
183 #ifdef	SUN_BOOTPARAMS
184 	/* Get the root pathname. */
185 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
186 		return (EIO);
187 #endif	/* BOOTPARAMS */
188 
189 	printf("boot: server addr: %s\n", inet_ntoa(rootip));
190 	printf("boot: server path: %s\n", rootpath);
191 
192 	/* Get the NFS file handle (mount). */
193 	error = nfs_mount(netdev_sock, rootip, rootpath);
194 
195 	return (error);
196 }
197