dev_net.c revision 1.3 1 /* $NetBSD: dev_net.c,v 1.3 2003/03/11 16:30:02 drochner Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * This module implements a "raw device" interface suitable for
41 * use by the stand-alone I/O library NFS code. This interface
42 * does not support any "block" access, and exists only for the
43 * purpose of initializing the network interface, getting boot
44 * parameters, and performing the NFS mount.
45 *
46 * At open time, this does:
47 *
48 * find interface - netif_open()
49 * RARP for IP address - rarp_getipaddress()
50 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
51 * RPC/mountd - nfs_mount(sock, ip, path)
52 *
53 * the root file handle from mountd is saved in a global
54 * for use by the NFS open code (NFS/lookup).
55 */
56
57 #include <machine/stdarg.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <net/if.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63
64 #include <lib/libkern/libkern.h>
65
66 #include "stand.h"
67 #include "net.h"
68 #include "netif.h"
69 #include "nfs.h"
70 #include "bootparam.h"
71 #include "dev_net.h"
72
73 static int netdev_sock = -1;
74 static int netdev_opens;
75
76 static int net_getparams __P((int sock));
77
78 /*
79 * Called by devopen after it sets f->f_dev to our devsw entry.
80 * This opens the low-level device and sets f->f_devdata.
81 * This is declared with variable arguments...
82 */
83 int
84 net_open(struct open_file *f, ...)
85 {
86 va_list ap;
87 char *devname; /* Device part of file name (or NULL). */
88 int error = 0;
89
90 va_start(ap, f);
91 devname = va_arg(ap, char*);
92 va_end(ap);
93
94 #ifdef NETIF_DEBUG
95 if (debug)
96 printf("net_open: %s\n", devname);
97 #endif
98
99 /* On first open, do netif open, mount, etc. */
100 if (netdev_opens == 0) {
101 /* Find network interface. */
102 if (netdev_sock < 0) {
103 netdev_sock = netif_open(devname);
104 if (netdev_sock < 0) {
105 printf("net_open: netif_open() failed\n");
106 return (ENXIO);
107 }
108 if (debug)
109 printf("net_open: netif_open() succeeded\n");
110 }
111 if (rootip.s_addr == 0) {
112 /* Get root IP address, and path, etc. */
113 error = net_getparams(netdev_sock);
114 if (error) {
115 /* getparams makes its own noise */
116 netif_close(netdev_sock);
117 netdev_sock = -1;
118 return (error);
119 }
120 }
121 }
122 netdev_opens++;
123 f->f_devdata = &netdev_sock;
124 return (error);
125 }
126
127 int
128 net_close(f)
129 struct open_file *f;
130 {
131
132 #ifdef NETIF_DEBUG
133 if (debug)
134 printf("net_close: opens=%d\n", netdev_opens);
135 #endif
136
137 /* On last close, do netif close, etc. */
138 f->f_devdata = NULL;
139 /* Extra close call? */
140 if (netdev_opens <= 0)
141 return (0);
142 netdev_opens--;
143 /* Not last close? */
144 if (netdev_opens > 0)
145 return(0);
146 rootip.s_addr = 0;
147 if (netdev_sock >= 0) {
148 if (debug)
149 printf("net_close: calling netif_close()\n");
150 netif_close(netdev_sock);
151 netdev_sock = -1;
152 }
153 return (0);
154 }
155
156 int
157 net_ioctl(f, cmd, data)
158 struct open_file *f;
159 u_long cmd;
160 void *data;
161 {
162 return EIO;
163 }
164
165 int
166 net_strategy(devdata, rw, blk, size, buf, rsize)
167 void *devdata;
168 int rw;
169 daddr_t blk;
170 size_t size;
171 void *buf;
172 size_t *rsize;
173 {
174 return EIO;
175 }
176
177
178 /*
179 * Get info for NFS boot: our IP address, our hostname,
180 * server IP address, and our root path on the server.
181 */
182 #ifdef SUPPORT_BOOTP
183 int bootp __P((int sock));
184 #endif
185
186 static int
187 net_getparams(sock)
188 int sock;
189 {
190
191 #ifdef SUPPORT_BOOTP
192 /*
193 * Try to get boot info using BOOTP. If we succeed, then
194 * the server IP address, gateway, and root path will all
195 * be initialized. If any remain uninitialized, we will
196 * use RARP and RPC/bootparam (the Sun way) to get them.
197 */
198 bootp(sock);
199 if (myip.s_addr != 0)
200 return (0);
201 if (debug)
202 printf("net_open: BOOTP failed\n");
203 #endif
204
205 return (EIO);
206 }
207