9712 libstand: netif.c variable set but not used
[unleashed.git] / usr / src / boot / lib / libstand / netif.c
blob51bc5aa1259c8fbfdc09a94018441ca12b576c2d
1 /* $NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 drochner Exp $ */
3 /*
4 * Copyright (c) 1993 Adam Glass
5 * All rights reserved.
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass.
18 * 4. The name of the Author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/cdefs.h>
39 #include <sys/mount.h>
40 #include <string.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
45 #include "stand.h"
46 #include "net.h"
47 #include "netif.h"
49 struct iodesc sockets[SOPEN_MAX];
50 #ifdef NETIF_DEBUG
51 int netif_debug = 0;
52 #endif
55 * netif_init:
57 * initialize the generic network interface layer
60 void
61 netif_init(void)
63 struct netif_driver *drv;
64 int d, i;
66 #ifdef NETIF_DEBUG
67 if (netif_debug)
68 printf("netif_init: called\n");
69 #endif
70 for (d = 0; netif_drivers[d]; d++) {
71 drv = netif_drivers[d];
72 for (i = 0; i < drv->netif_nifs; i++)
73 drv->netif_ifs[i].dif_used = 0;
77 int
78 netif_match(struct netif *nif, void *machdep_hint)
80 struct netif_driver *drv = nif->nif_driver;
82 #if NETIF_DEBUG
83 if (netif_debug)
84 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
85 nif->nif_unit, nif->nif_sel);
86 #endif
87 return drv->netif_match(nif, machdep_hint);
90 struct netif *
91 netif_select(void *machdep_hint)
93 int d, u, s;
94 struct netif_driver *drv;
95 struct netif cur_if;
96 static struct netif best_if;
97 int best_val;
98 int val;
100 best_val = 0;
101 best_if.nif_driver = NULL;
103 for (d = 0; netif_drivers[d] != NULL; d++) {
104 cur_if.nif_driver = netif_drivers[d];
105 drv = cur_if.nif_driver;
107 for (u = 0; u < drv->netif_nifs; u++) {
108 cur_if.nif_unit = u;
109 #ifdef NETIF_DEBUG
110 if (netif_debug)
111 printf("\t%s%d:", drv->netif_bname,
112 cur_if.nif_unit);
113 #endif
115 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
116 cur_if.nif_sel = s;
118 if (drv->netif_ifs[u].dif_used & (1 << s)) {
119 #ifdef NETIF_DEBUG
120 if (netif_debug)
121 printf(" [%d used]", s);
122 #endif
123 continue;
126 val = netif_match(&cur_if, machdep_hint);
127 #ifdef NETIF_DEBUG
128 if (netif_debug)
129 printf(" [%d -> %d]", s, val);
130 #endif
131 if (val > best_val) {
132 best_val = val;
133 best_if = cur_if;
136 #ifdef NETIF_DEBUG
137 if (netif_debug)
138 printf("\n");
139 #endif
143 if (best_if.nif_driver == NULL)
144 return NULL;
146 best_if.nif_driver->
147 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
149 #ifdef NETIF_DEBUG
150 if (netif_debug)
151 printf("netif_select: %s%d(%d) wins\n",
152 best_if.nif_driver->netif_bname,
153 best_if.nif_unit, best_if.nif_sel);
154 #endif
155 return &best_if;
159 netif_probe(struct netif *nif, void *machdep_hint)
161 struct netif_driver *drv = nif->nif_driver;
163 #ifdef NETIF_DEBUG
164 if (netif_debug)
165 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
166 #endif
167 return drv->netif_probe(nif, machdep_hint);
170 void
171 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
173 struct netif_driver *drv = nif->nif_driver;
175 #ifdef NETIF_DEBUG
176 if (netif_debug)
177 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
178 #endif
179 desc->io_netif = nif;
180 #ifdef PARANOID
181 if (drv->netif_init == NULL)
182 panic("%s%d: no netif_init support\n", drv->netif_bname,
183 nif->nif_unit);
184 #endif
185 drv->netif_init(desc, machdep_hint);
186 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
187 sizeof(struct netif_stats));
190 void
191 netif_detach(struct netif *nif)
193 struct netif_driver *drv = nif->nif_driver;
195 #ifdef NETIF_DEBUG
196 if (netif_debug)
197 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
198 #endif
199 #ifdef PARANOID
200 if (drv->netif_end == NULL)
201 panic("%s%d: no netif_end support\n", drv->netif_bname,
202 nif->nif_unit);
203 #endif
204 drv->netif_end(nif);
207 ssize_t
208 netif_get(struct iodesc *desc, void **pkt, time_t timo)
210 #ifdef NETIF_DEBUG
211 struct netif *nif = desc->io_netif;
212 #endif
213 struct netif_driver *drv = desc->io_netif->nif_driver;
214 ssize_t rv;
216 #ifdef NETIF_DEBUG
217 if (netif_debug)
218 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
219 #endif
220 #ifdef PARANOID
221 if (drv->netif_get == NULL)
222 panic("%s%d: no netif_get support\n", drv->netif_bname,
223 nif->nif_unit);
224 #endif
225 rv = drv->netif_get(desc, pkt, timo);
226 #ifdef NETIF_DEBUG
227 if (netif_debug)
228 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
229 nif->nif_unit, (int)rv);
230 #endif
231 return (rv);
234 ssize_t
235 netif_put(struct iodesc *desc, void *pkt, size_t len)
237 #ifdef NETIF_DEBUG
238 struct netif *nif = desc->io_netif;
239 #endif
240 struct netif_driver *drv = desc->io_netif->nif_driver;
241 ssize_t rv;
243 #ifdef NETIF_DEBUG
244 if (netif_debug)
245 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
246 #endif
247 #ifdef PARANOID
248 if (drv->netif_put == NULL)
249 panic("%s%d: no netif_put support\n", drv->netif_bname,
250 nif->nif_unit);
251 #endif
252 rv = drv->netif_put(desc, pkt, len);
253 #ifdef NETIF_DEBUG
254 if (netif_debug)
255 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
256 nif->nif_unit, (int)rv);
257 #endif
258 return (rv);
261 struct iodesc *
262 socktodesc(int sock)
264 if (sock >= SOPEN_MAX) {
265 errno = EBADF;
266 return (NULL);
268 return (&sockets[sock]);
272 netif_open(void *machdep_hint)
274 int fd;
275 struct iodesc *s;
276 struct netif *nif;
278 /* find a free socket */
279 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
280 if (s->io_netif == (struct netif *)0)
281 goto fnd;
282 errno = EMFILE;
283 return (-1);
285 fnd:
286 bzero(s, sizeof(*s));
287 netif_init();
288 nif = netif_select(machdep_hint);
289 if (!nif)
290 panic("netboot: no interfaces left untried");
291 if (netif_probe(nif, machdep_hint)) {
292 printf("netboot: couldn't probe %s%d\n",
293 nif->nif_driver->netif_bname, nif->nif_unit);
294 errno = EINVAL;
295 return (-1);
297 netif_attach(nif, s, machdep_hint);
299 return (fd);
303 netif_close(int sock)
305 if (sock >= SOPEN_MAX) {
306 errno = EBADF;
307 return (-1);
309 netif_detach(sockets[sock].io_netif);
310 sockets[sock].io_netif = (struct netif *)0;
312 return (0);