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>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
49 typedef TAILQ_HEAD(socket_list, iodesc) socket_list_t;
52 * Open socket list. The current implementation and assumption is,
53 * we only remove entries from tail and we only add new entries to tail.
54 * This decision is to keep iodesc id management simple - we get list
55 * entries ordered by continiously growing io_id field.
56 * If we do have multiple sockets open and we do close socket not from tail,
57 * this entry will be marked unused. netif_open() will reuse unused entry, or
58 * netif_close() will free all unused tail entries.
60 static socket_list_t sockets = TAILQ_HEAD_INITIALIZER(sockets);
69 * initialize the generic network interface layer
75 struct netif_driver *drv;
80 printf("netif_init: called\n");
82 for (d = 0; netif_drivers[d]; d++) {
83 drv = netif_drivers[d];
84 for (i = 0; i < drv->netif_nifs; i++)
85 drv->netif_ifs[i].dif_used = 0;
90 netif_match(struct netif *nif, void *machdep_hint)
92 struct netif_driver *drv = nif->nif_driver;
96 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
97 nif->nif_unit, nif->nif_sel);
99 return drv->netif_match(nif, machdep_hint);
103 netif_select(void *machdep_hint)
106 struct netif_driver *drv;
108 static struct netif best_if;
113 best_if.nif_driver = NULL;
115 for (d = 0; netif_drivers[d] != NULL; d++) {
116 cur_if.nif_driver = netif_drivers[d];
117 drv = cur_if.nif_driver;
119 for (u = 0; u < drv->netif_nifs; u++) {
124 printf("\t%s%d:", drv->netif_bname,
128 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
131 if (drv->netif_ifs[u].dif_used & (1 << s)) {
134 printf(" [%d used]", s);
139 val = netif_match(&cur_if, machdep_hint);
142 printf(" [%d -> %d]", s, val);
144 if (val > best_val) {
156 if (best_if.nif_driver == NULL)
160 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
164 printf("netif_select: %s%d(%d) wins\n",
165 best_if.nif_driver->netif_bname,
166 best_if.nif_unit, best_if.nif_sel);
172 netif_probe(struct netif *nif, void *machdep_hint)
174 struct netif_driver *drv = nif->nif_driver;
178 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
180 return drv->netif_probe(nif, machdep_hint);
184 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
186 struct netif_driver *drv = nif->nif_driver;
190 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
192 desc->io_netif = nif;
194 if (drv->netif_init == NULL)
195 panic("%s%d: no netif_init support", drv->netif_bname,
198 drv->netif_init(desc, machdep_hint);
199 bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
200 sizeof(struct netif_stats));
204 netif_detach(struct netif *nif)
206 struct netif_driver *drv = nif->nif_driver;
210 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
213 if (drv->netif_end == NULL)
214 panic("%s%d: no netif_end support", drv->netif_bname,
221 netif_get(struct iodesc *desc, void **pkt, time_t timo)
224 struct netif *nif = desc->io_netif;
226 struct netif_driver *drv = desc->io_netif->nif_driver;
231 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
234 if (drv->netif_get == NULL)
235 panic("%s%d: no netif_get support", drv->netif_bname,
238 rv = drv->netif_get(desc, pkt, timo);
241 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
242 nif->nif_unit, (int)rv);
248 netif_put(struct iodesc *desc, void *pkt, size_t len)
251 struct netif *nif = desc->io_netif;
253 struct netif_driver *drv = desc->io_netif->nif_driver;
258 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
261 if (drv->netif_put == NULL)
262 panic("%s%d: no netif_put support", drv->netif_bname,
265 rv = drv->netif_put(desc, pkt, len);
268 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
269 nif->nif_unit, (int)rv);
277 * Walk socket list and return pointer to iodesc structure.
278 * if id is < 0, return first unused iodesc.
280 static struct iodesc *
281 socktodesc_impl(int socket)
285 TAILQ_FOREACH(s, &sockets, io_link) {
286 /* search by socket id */
288 if (s->io_id == socket)
292 /* search for first unused entry */
293 if (s->io_netif == NULL)
307 desc = socktodesc_impl(sock);
316 netif_open(void *machdep_hint)
321 /* find a free socket */
322 s = socktodesc_impl(-1);
326 s = calloc(1, sizeof (*s));
330 last = TAILQ_LAST(&sockets, socket_list);
332 s->io_id = last->io_id + 1;
333 TAILQ_INSERT_TAIL(&sockets, s, io_link);
337 nif = netif_select(machdep_hint);
339 panic("netboot: no interfaces left untried");
340 if (netif_probe(nif, machdep_hint)) {
341 printf("netboot: couldn't probe %s%d\n",
342 nif->nif_driver->netif_bname, nif->nif_unit);
346 netif_attach(nif, s, machdep_hint);
352 netif_close(int sock)
354 struct iodesc *s, *last;
358 s = socktodesc_impl(sock);
359 if (s == NULL || sock < 0) {
363 netif_detach(s->io_netif);
364 bzero(&s->destip, sizeof (s->destip));
365 bzero(&s->myip, sizeof (s->myip));
369 bzero(s->myea, sizeof (s->myea));
372 /* free unused entries from tail. */
373 TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) {
374 if (last->io_netif != NULL)
376 TAILQ_REMOVE(&sockets, last, io_link);