1 /* $NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 drochner Exp $ */
2 /* $DragonFly: src/lib/libstand/netif.c,v 1.2 2004/10/25 19:38:45 drhodus Exp $ */
5 * Copyright (c) 1993 Adam Glass
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Adam Glass.
19 * 4. The name of the Author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
38 #include <sys/mount.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
48 struct iodesc sockets[SOPEN_MAX];
56 * initialize the generic network interface layer
62 struct netif_driver *drv;
67 printf("netif_init: called\n");
69 for (d = 0; netif_drivers[d]; d++) {
70 drv = netif_drivers[d];
71 for (i = 0; i < drv->netif_nifs; i++)
72 drv->netif_ifs[i].dif_used = 0;
77 netif_match(nif, machdep_hint)
81 struct netif_driver *drv = nif->nif_driver;
85 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
86 nif->nif_unit, nif->nif_sel);
88 return drv->netif_match(nif, machdep_hint);
92 netif_select(machdep_hint)
95 int d, u, unit_done, s;
96 struct netif_driver *drv;
98 static struct netif best_if;
103 best_if.nif_driver = NULL;
105 for (d = 0; netif_drivers[d] != NULL; d++) {
106 cur_if.nif_driver = netif_drivers[d];
107 drv = cur_if.nif_driver;
109 for (u = 0; u < drv->netif_nifs; u++) {
115 printf("\t%s%d:", drv->netif_bname,
119 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
122 if (drv->netif_ifs[u].dif_used & (1 << s)) {
125 printf(" [%d used]", s);
130 val = netif_match(&cur_if, machdep_hint);
133 printf(" [%d -> %d]", s, val);
135 if (val > best_val) {
147 if (best_if.nif_driver == NULL)
151 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
155 printf("netif_select: %s%d(%d) wins\n",
156 best_if.nif_driver->netif_bname,
157 best_if.nif_unit, best_if.nif_sel);
163 netif_probe(nif, machdep_hint)
167 struct netif_driver *drv = nif->nif_driver;
171 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
173 return drv->netif_probe(nif, machdep_hint);
177 netif_attach(nif, desc, machdep_hint)
182 struct netif_driver *drv = nif->nif_driver;
186 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
188 desc->io_netif = nif;
190 if (drv->netif_init == NULL)
191 panic("%s%d: no netif_init support\n", drv->netif_bname,
194 drv->netif_init(desc, machdep_hint);
195 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
196 sizeof(struct netif_stats));
203 struct netif_driver *drv = nif->nif_driver;
207 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
210 if (drv->netif_end == NULL)
211 panic("%s%d: no netif_end support\n", drv->netif_bname,
218 netif_get(desc, pkt, len, timo)
225 struct netif *nif = desc->io_netif;
227 struct netif_driver *drv = desc->io_netif->nif_driver;
232 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
235 if (drv->netif_get == NULL)
236 panic("%s%d: no netif_get support\n", drv->netif_bname,
239 rv = drv->netif_get(desc, pkt, len, timo);
242 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
243 nif->nif_unit, (int)rv);
249 netif_put(desc, pkt, len)
255 struct netif *nif = desc->io_netif;
257 struct netif_driver *drv = desc->io_netif->nif_driver;
262 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
265 if (drv->netif_put == NULL)
266 panic("%s%d: no netif_put support\n", drv->netif_bname,
269 rv = drv->netif_put(desc, pkt, len);
272 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
273 nif->nif_unit, (int)rv);
282 if (sock >= SOPEN_MAX) {
286 return (&sockets[sock]);
290 netif_open(machdep_hint)
297 /* find a free socket */
298 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
299 if (s->io_netif == (struct netif *)0)
305 bzero(s, sizeof(*s));
307 nif = netif_select(machdep_hint);
309 panic("netboot: no interfaces left untried");
310 if (netif_probe(nif, machdep_hint)) {
311 printf("netboot: couldn't probe %s%d\n",
312 nif->nif_driver->netif_bname, nif->nif_unit);
316 netif_attach(nif, s, machdep_hint);
325 if (sock >= SOPEN_MAX) {
329 netif_detach(sockets[sock].io_netif);
330 sockets[sock].io_netif = (struct netif *)0;