1 /* $NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 drochner Exp $ */
4 * Copyright (c) 1993 Adam Glass
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/cdefs.h>
40 #include <sys/mount.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
50 struct iodesc sockets[SOPEN_MAX];
58 * initialize the generic network interface layer
64 struct netif_driver *drv;
69 printf("netif_init: called\n");
71 for (d = 0; netif_drivers[d]; d++) {
72 drv = netif_drivers[d];
73 for (i = 0; i < drv->netif_nifs; i++)
74 drv->netif_ifs[i].dif_used = 0;
79 netif_match(nif, machdep_hint)
83 struct netif_driver *drv = nif->nif_driver;
87 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
88 nif->nif_unit, nif->nif_sel);
90 return drv->netif_match(nif, machdep_hint);
94 netif_select(machdep_hint)
97 int d, u, unit_done, s;
98 struct netif_driver *drv;
100 static struct netif best_if;
105 best_if.nif_driver = NULL;
107 for (d = 0; netif_drivers[d] != NULL; d++) {
108 cur_if.nif_driver = netif_drivers[d];
109 drv = cur_if.nif_driver;
111 for (u = 0; u < drv->netif_nifs; u++) {
117 printf("\t%s%d:", drv->netif_bname,
121 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
124 if (drv->netif_ifs[u].dif_used & (1 << s)) {
127 printf(" [%d used]", s);
132 val = netif_match(&cur_if, machdep_hint);
135 printf(" [%d -> %d]", s, val);
137 if (val > best_val) {
149 if (best_if.nif_driver == NULL)
153 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
157 printf("netif_select: %s%d(%d) wins\n",
158 best_if.nif_driver->netif_bname,
159 best_if.nif_unit, best_if.nif_sel);
165 netif_probe(nif, machdep_hint)
169 struct netif_driver *drv = nif->nif_driver;
173 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
175 return drv->netif_probe(nif, machdep_hint);
179 netif_attach(nif, desc, machdep_hint)
184 struct netif_driver *drv = nif->nif_driver;
188 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
190 desc->io_netif = nif;
192 if (drv->netif_init == NULL)
193 panic("%s%d: no netif_init support\n", drv->netif_bname,
196 drv->netif_init(desc, machdep_hint);
197 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
198 sizeof(struct netif_stats));
205 struct netif_driver *drv = nif->nif_driver;
209 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
212 if (drv->netif_end == NULL)
213 panic("%s%d: no netif_end support\n", drv->netif_bname,
220 netif_get(desc, pkt, len, timo)
227 struct netif *nif = desc->io_netif;
229 struct netif_driver *drv = desc->io_netif->nif_driver;
234 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
237 if (drv->netif_get == NULL)
238 panic("%s%d: no netif_get support\n", drv->netif_bname,
241 rv = drv->netif_get(desc, pkt, len, timo);
244 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
245 nif->nif_unit, (int)rv);
251 netif_put(desc, pkt, len)
257 struct netif *nif = desc->io_netif;
259 struct netif_driver *drv = desc->io_netif->nif_driver;
264 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
267 if (drv->netif_put == NULL)
268 panic("%s%d: no netif_put support\n", drv->netif_bname,
271 rv = drv->netif_put(desc, pkt, len);
274 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
275 nif->nif_unit, (int)rv);
284 if (sock >= SOPEN_MAX) {
288 return (&sockets[sock]);
292 netif_open(machdep_hint)
299 /* find a free socket */
300 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
301 if (s->io_netif == (struct netif *)0)
307 bzero(s, sizeof(*s));
309 nif = netif_select(machdep_hint);
311 panic("netboot: no interfaces left untried");
312 if (netif_probe(nif, machdep_hint)) {
313 printf("netboot: couldn't probe %s%d\n",
314 nif->nif_driver->netif_bname, nif->nif_unit);
318 netif_attach(nif, s, machdep_hint);
327 if (sock >= SOPEN_MAX) {
331 netif_detach(sockets[sock].io_netif);
332 sockets[sock].io_netif = (struct netif *)0;