dev_net.c revision 1.5 1 /* $NetBSD: dev_net.c,v 1.5 2003/03/12 17:35:57 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 <lib/libsa/stand.h>
62 #include <lib/libsa/net.h>
63 #include "pxe_netif.h"
64 #include "dev_net.h"
65
66 static int netdev_sock = -1;
67 static int netdev_opens;
68
69 static int net_getparams __P((int sock));
70
71 /*
72 * Called by devopen after it sets f->f_dev to our devsw entry.
73 * This opens the low-level device and sets f->f_devdata.
74 * This is declared with variable arguments...
75 */
76 int
77 net_open(struct open_file *f, ...)
78 {
79 int error = 0;
80
81 #ifdef NETIF_DEBUG
82 if (debug)
83 printf("net_open\n");
84 #endif
85
86 /* On first open, do netif open, mount, etc. */
87 if (netdev_opens == 0) {
88 /* Find network interface. */
89 if (netdev_sock < 0) {
90 netdev_sock = pxe_netif_open();
91 if (netdev_sock < 0) {
92 printf("net_open: netif_open() failed\n");
93 return (ENXIO);
94 }
95 if (debug)
96 printf("net_open: netif_open() succeeded\n");
97 }
98 if (rootip.s_addr == 0) {
99 /* Get root IP address, and path, etc. */
100 error = net_getparams(netdev_sock);
101 if (error) {
102 /* getparams makes its own noise */
103 pxe_netif_close(netdev_sock);
104 netdev_sock = -1;
105 return (error);
106 }
107 }
108 }
109 netdev_opens++;
110 f->f_devdata = &netdev_sock;
111 return (error);
112 }
113
114 int
115 net_close(f)
116 struct open_file *f;
117 {
118
119 #ifdef NETIF_DEBUG
120 if (debug)
121 printf("net_close: opens=%d\n", netdev_opens);
122 #endif
123
124 /* On last close, do netif close, etc. */
125 f->f_devdata = NULL;
126 /* Extra close call? */
127 if (netdev_opens <= 0)
128 return (0);
129 netdev_opens--;
130 /* Not last close? */
131 if (netdev_opens > 0)
132 return(0);
133 rootip.s_addr = 0;
134 if (netdev_sock >= 0) {
135 if (debug)
136 printf("net_close: calling netif_close()\n");
137 pxe_netif_close(netdev_sock);
138 pxe_netif_shutdown(); /* XXX shouldn't be done here */
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 network 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