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 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/cdefs.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
47 typedef TAILQ_HEAD(socket_list, iodesc) socket_list_t;
50 * Open socket list. The current implementation and assumption is,
51 * we only remove entries from tail and we only add new entries to tail.
52 * This decision is to keep iodesc id management simple - we get list
53 * entries ordered by continiously growing io_id field.
54 * If we do have multiple sockets open and we do close socket not from tail,
55 * this entry will be marked unused. netif_open() will reuse unused entry, or
56 * netif_close() will free all unused tail entries.
58 static socket_list_t sockets = TAILQ_HEAD_INITIALIZER(sockets);
67 * initialize the generic network interface layer
73 struct netif_driver *drv;
78 printf("netif_init: called\n");
80 for (d = 0; netif_drivers[d]; d++) {
81 drv = netif_drivers[d];
82 for (i = 0; i < drv->netif_nifs; i++)
83 drv->netif_ifs[i].dif_used = 0;
88 netif_match(struct netif *nif, void *machdep_hint)
90 struct netif_driver *drv = nif->nif_driver;
94 printf("%s%d: netif_match (%d)\n", drv->netif_bname,
95 nif->nif_unit, nif->nif_sel);
97 return drv->netif_match(nif, machdep_hint);
101 netif_select(void *machdep_hint)
104 struct netif_driver *drv;
106 static struct netif best_if;
111 best_if.nif_driver = NULL;
113 for (d = 0; netif_drivers[d] != NULL; d++) {
114 cur_if.nif_driver = netif_drivers[d];
115 drv = cur_if.nif_driver;
117 for (u = 0; u < drv->netif_nifs; u++) {
122 printf("\t%s%d:", drv->netif_bname,
126 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
129 if (drv->netif_ifs[u].dif_used & (1 << s)) {
132 printf(" [%d used]", s);
137 val = netif_match(&cur_if, machdep_hint);
140 printf(" [%d -> %d]", s, val);
142 if (val > best_val) {
154 if (best_if.nif_driver == NULL)
158 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
162 printf("netif_select: %s%d(%d) wins\n",
163 best_if.nif_driver->netif_bname,
164 best_if.nif_unit, best_if.nif_sel);
170 netif_probe(struct netif *nif, void *machdep_hint)
172 struct netif_driver *drv = nif->nif_driver;
176 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
178 return drv->netif_probe(nif, machdep_hint);
182 netif_attach(struct netif *nif, struct iodesc *desc, void *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", 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));
202 netif_detach(struct netif *nif)
204 struct netif_driver *drv = nif->nif_driver;
208 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
211 if (drv->netif_end == NULL)
212 panic("%s%d: no netif_end support", drv->netif_bname,
219 netif_get(struct iodesc *desc, void **pkt, time_t timo)
222 struct netif *nif = desc->io_netif;
224 struct netif_driver *drv = desc->io_netif->nif_driver;
229 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
232 if (drv->netif_get == NULL)
233 panic("%s%d: no netif_get support", drv->netif_bname,
236 rv = drv->netif_get(desc, pkt, timo);
239 printf("%s%d: netif_get returning %d\n", drv->netif_bname,
240 nif->nif_unit, (int)rv);
246 netif_put(struct iodesc *desc, void *pkt, size_t len)
249 struct netif *nif = desc->io_netif;
251 struct netif_driver *drv = desc->io_netif->nif_driver;
256 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
259 if (drv->netif_put == NULL)
260 panic("%s%d: no netif_put support", drv->netif_bname,
263 rv = drv->netif_put(desc, pkt, len);
266 printf("%s%d: netif_put returning %d\n", drv->netif_bname,
267 nif->nif_unit, (int)rv);
275 * Walk socket list and return pointer to iodesc structure.
276 * if id is < 0, return first unused iodesc.
278 static struct iodesc *
279 socktodesc_impl(int socket)
283 TAILQ_FOREACH(s, &sockets, io_link) {
284 /* search by socket id */
286 if (s->io_id == socket)
290 /* search for first unused entry */
291 if (s->io_netif == NULL)
305 desc = socktodesc_impl(sock);
314 netif_open(void *machdep_hint)
319 /* find a free socket */
320 s = socktodesc_impl(-1);
324 s = calloc(1, sizeof (*s));
328 last = TAILQ_LAST(&sockets, socket_list);
330 s->io_id = last->io_id + 1;
331 TAILQ_INSERT_TAIL(&sockets, s, io_link);
335 nif = netif_select(machdep_hint);
337 panic("netboot: no interfaces left untried");
338 if (netif_probe(nif, machdep_hint)) {
339 printf("netboot: couldn't probe %s%d\n",
340 nif->nif_driver->netif_bname, nif->nif_unit);
344 netif_attach(nif, s, machdep_hint);
350 netif_close(int sock)
352 struct iodesc *s, *last;
356 s = socktodesc_impl(sock);
357 if (s == NULL || sock < 0) {
361 netif_detach(s->io_netif);
362 bzero(&s->destip, sizeof (s->destip));
363 bzero(&s->myip, sizeof (s->myip));
367 bzero(s->myea, sizeof (s->myea));
370 /* free unused entries from tail. */
371 TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) {
372 if (last->io_netif != NULL)
374 TAILQ_REMOVE(&sockets, last, io_link);