dev_net.c revision 1.4 1 /* $NetBSD: dev_net.c,v 1.4 2003/03/12 13:15:08 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 and TFTP code. This interface
42 * does not support any "block" access, and exists only for the
43 * purpose of initializing the network interface and getting boot
44 * parameters.
45 *
46 * At open time, this does:
47 *
48 * find interface - netif_open()
49 * BOOTP for IP address - bootp()
50 */
51
52 #include <machine/stdarg.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <net/if.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58
59 #include <lib/libkern/libkern.h>
60
61 #include "stand.h"
62 #include "net.h"
63 #include "netif.h"
64 #include "nfs.h"
65 #include "dev_net.h"
66
67 static int netdev_sock = -1;
68 static int netdev_opens;
69
70 static int net_getparams __P((int sock));
71
72 /*
73 * Called by devopen after it sets f->f_dev to our devsw entry.
74 * This opens the low-level device and sets f->f_devdata.
75 * This is declared with variable arguments...
76 */
77 int
78 net_open(struct open_file *f, ...)
79 {
80 int error = 0;
81
82 #ifdef NETIF_DEBUG
83 if (debug)
84 printf("net_open\n");
85 #endif
86
87 /* On first open, do netif open, mount, etc. */
88 if (netdev_opens == 0) {
89 /* Find network interface. */
90 if (netdev_sock < 0) {
91 netdev_sock = netif_open(0);
92 if (netdev_sock < 0) {
93 printf("net_open: netif_open() failed\n");
94 return (ENXIO);
95 }
96 if (debug)
97 printf("net_open: netif_open() succeeded\n");
98 }
99 if (rootip.s_addr == 0) {
100 /* Get root IP address, and path, etc. */
101 error = net_getparams(netdev_sock);
102 if (error) {
103 /* getparams makes its own noise */
104 netif_close(netdev_sock);
105 netdev_sock = -1;
106 return (error);
107 }
108 }
109 }
110 netdev_opens++;
111 f->f_devdata = &netdev_sock;
112 return (error);
113 }
114
115 int
116 net_close(f)
117 struct open_file *f;
118 {
119
120 #ifdef NETIF_DEBUG
121 if (debug)
122 printf("net_close: opens=%d\n", netdev_opens);
123 #endif
124
125 /* On last close, do netif close, etc. */
126 f->f_devdata = NULL;
127 /* Extra close call? */
128 if (netdev_opens <= 0)
129 return (0);
130 netdev_opens--;
131 /* Not last close? */
132 if (netdev_opens > 0)
133 return(0);
134 rootip.s_addr = 0;
135 if (netdev_sock >= 0) {
136 if (debug)
137 printf("net_close: calling netif_close()\n");
138 netif_close(netdev_sock);
139 netdev_sock = -1;
140 }
141 return (0);
142 }
143
144 int
145 net_ioctl(f, cmd, data)
146 struct open_file *f;
147 u_long cmd;
148 void *data;
149 {
150 return EIO;
151 }
152
153 int
154 net_strategy(devdata, rw, blk, size, buf, rsize)
155 void *devdata;
156 int rw;
157 daddr_t blk;
158 size_t size;
159 void *buf;
160 size_t *rsize;
161 {
162 return EIO;
163 }
164
165
166 /*
167 * Get info for NFS boot: our IP address, our hostname,
168 * server IP address, and our root path on the server.
169 */
170 #ifdef SUPPORT_BOOTP
171 int bootp __P((int sock));
172 #endif
173
174 static int
175 net_getparams(sock)
176 int sock;
177 {
178
179 #ifdef SUPPORT_BOOTP
180 /*
181 * Try to get boot info using BOOTP. If we succeed, then
182 * the server IP address, gateway, and root path will all
183 * be initialized. If any remain uninitialized, we will
184 * use RARP and RPC/bootparam (the Sun way) to get them.
185 */
186 bootp(sock);
187 if (myip.s_addr != 0)
188 return (0);
189 if (debug)
190 printf("net_open: BOOTP failed\n");
191 #endif
192
193 return (EIO);
194 }
195