Home | History | Annotate | Line # | Download | only in libsa
netif.c revision 1.20.44.1
      1 /*	$NetBSD: netif.c,v 1.20.44.1 2007/11/27 19:38:35 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Adam Glass
      5  * All rights reserved.
      6  *
      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.
     20  *
     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.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/cdefs.h>
     36 #include <sys/mount.h>
     37 #ifdef _STANDALONE
     38 #include <lib/libkern/libkern.h>
     39 #else
     40 #include <string.h>
     41 #endif
     42 
     43 #include <netinet/in.h>
     44 #include <netinet/in_systm.h>
     45 
     46 #include "stand.h"
     47 #include "netif.h"
     48 
     49 struct iodesc sockets[SOPEN_MAX];
     50 #ifdef NETIF_DEBUG
     51 int netif_debug = 0;
     52 #endif
     53 
     54 /*
     55  * netif_init:
     56  *
     57  * initialize the generic network interface layer
     58  */
     59 
     60 void
     61 netif_init(void)
     62 {
     63 	struct netif_driver *drv;
     64 	int d, i;
     65 
     66 #ifdef NETIF_DEBUG
     67 	if (netif_debug)
     68 		printf("netif_init: called\n");
     69 #endif
     70 	for (d = 0; d < n_netif_drivers; d++) {
     71 		drv = netif_drivers[d];
     72 		for (i = 0; i < drv->netif_nifs; i++)
     73 			drv->netif_ifs[i].dif_used = 0;
     74 	}
     75 }
     76 
     77 int	netif_match __P((struct netif *, void *));
     78 
     79 int
     80 netif_match(struct netif *nif, void *machdep_hint)
     81 {
     82 	struct netif_driver *drv = nif->nif_driver;
     83 
     84 #if 0
     85 	if (netif_debug)
     86 		printf("%s%d: netif_match (%d)\n", drv->netif_bname,
     87 		    nif->nif_unit, nif->nif_sel);
     88 #endif
     89 	return drv->netif_match(nif, machdep_hint);
     90 }
     91 
     92 struct netif *
     93 netif_select(void *machdep_hint)
     94 {
     95 	int d, u, unit_done, s;
     96 	struct netif_driver *drv;
     97 	struct netif cur_if;
     98 	static struct netif best_if;
     99 	int best_val;
    100 	int val;
    101 
    102 	best_val = 0;
    103 	best_if.nif_driver = NULL;
    104 
    105 #ifdef NETIF_DEBUG
    106 	if (netif_debug)
    107 		printf("netif_select: %d interfaces\n", n_netif_drivers);
    108 #endif
    109 
    110 	for (d = 0; d < n_netif_drivers; d++) {
    111 		cur_if.nif_driver = netif_drivers[d];
    112 		drv = cur_if.nif_driver;
    113 
    114 		for (u = 0; u < drv->netif_nifs; u++) {
    115 			cur_if.nif_unit = u;
    116 			unit_done = 0;
    117 
    118 #ifdef NETIF_DEBUG
    119 			if (netif_debug)
    120 				printf("\t%s%d:", drv->netif_bname,
    121 				    cur_if.nif_unit);
    122 #endif
    123 
    124 			for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
    125 				cur_if.nif_sel = s;
    126 
    127 				if (drv->netif_ifs[u].dif_used & (1 << s)) {
    128 #ifdef NETIF_DEBUG
    129 					if (netif_debug)
    130 						printf(" [%d used]", s);
    131 #endif
    132 					continue;
    133 				}
    134 
    135 				val = netif_match(&cur_if, machdep_hint);
    136 #ifdef NETIF_DEBUG
    137 				if (netif_debug)
    138 					printf(" [%d -> %d]", s, val);
    139 #endif
    140 				if (val > best_val) {
    141 					best_val = val;
    142 					best_if = cur_if;
    143 				}
    144 			}
    145 #ifdef NETIF_DEBUG
    146 			if (netif_debug)
    147 				printf("\n");
    148 #endif
    149 		}
    150 	}
    151 
    152 	if (best_if.nif_driver == NULL)
    153 		return NULL;
    154 
    155 	best_if.nif_driver->
    156 	    netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
    157 
    158 #ifdef NETIF_DEBUG
    159 	if (netif_debug)
    160 		printf("netif_select: %s%d(%d) wins\n",
    161 			best_if.nif_driver->netif_bname,
    162 			best_if.nif_unit, best_if.nif_sel);
    163 #endif
    164 	return &best_if;
    165 }
    166 
    167 int
    168 netif_probe(struct netif *nif, void *machdep_hint)
    169 {
    170 	struct netif_driver *drv = nif->nif_driver;
    171 
    172 #ifdef NETIF_DEBUG
    173 	if (netif_debug)
    174 		printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
    175 #endif
    176 	return drv->netif_probe(nif, machdep_hint);
    177 }
    178 
    179 void
    180 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
    181 {
    182 	struct netif_driver *drv = nif->nif_driver;
    183 
    184 #ifdef NETIF_DEBUG
    185 	if (netif_debug)
    186 		printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
    187 #endif
    188 	desc->io_netif = nif;
    189 #ifdef PARANOID
    190 	if (drv->netif_init == NULL)
    191 		panic("%s%d: no netif_init support", drv->netif_bname,
    192 		    nif->nif_unit);
    193 #endif
    194 	drv->netif_init(desc, machdep_hint);
    195 	bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
    196 	    sizeof(struct netif_stats));
    197 }
    198 
    199 void
    200 netif_detach(struct netif *nif)
    201 {
    202 	struct netif_driver *drv = nif->nif_driver;
    203 
    204 #ifdef NETIF_DEBUG
    205 	if (netif_debug)
    206 		printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
    207 #endif
    208 #ifdef PARANOID
    209 	if (drv->netif_end == NULL)
    210 		panic("%s%d: no netif_end support", drv->netif_bname,
    211 		    nif->nif_unit);
    212 #endif
    213 	drv->netif_end(nif);
    214 }
    215 
    216 ssize_t
    217 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
    218 {
    219 	struct netif *nif = desc->io_netif;
    220 	struct netif_driver *drv = nif->nif_driver;
    221 	ssize_t rv;
    222 
    223 #ifdef NETIF_DEBUG
    224 	if (netif_debug)
    225 		printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
    226 #endif
    227 #ifdef PARANOID
    228 	if (drv->netif_get == NULL)
    229 		panic("%s%d: no netif_get support", drv->netif_bname,
    230 		    nif->nif_unit);
    231 #endif
    232 	rv = drv->netif_get(desc, pkt, len, timo);
    233 #ifdef NETIF_DEBUG
    234 	if (netif_debug)
    235 		printf("%s%d: netif_get returning %d\n", drv->netif_bname,
    236 		    nif->nif_unit, (int)rv);
    237 #endif
    238 	return rv;
    239 }
    240 
    241 ssize_t
    242 netif_put(struct iodesc *desc, void *pkt, size_t len)
    243 {
    244 	struct netif *nif = desc->io_netif;
    245 	struct netif_driver *drv = nif->nif_driver;
    246 	ssize_t rv;
    247 
    248 #ifdef NETIF_DEBUG
    249 	if (netif_debug)
    250 		printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
    251 #endif
    252 #ifdef PARANOID
    253 	if (drv->netif_put == NULL)
    254 		panic("%s%d: no netif_put support", drv->netif_bname,
    255 		    nif->nif_unit);
    256 #endif
    257 	rv = drv->netif_put(desc, pkt, len);
    258 #ifdef NETIF_DEBUG
    259 	if (netif_debug)
    260 		printf("%s%d: netif_put returning %d\n", drv->netif_bname,
    261 		    nif->nif_unit, (int)rv);
    262 #endif
    263 	return rv;
    264 }
    265 
    266 struct iodesc *
    267 socktodesc(int sock)
    268 {
    269 #if !defined(LIBSA_NO_FD_CHECKING)
    270 	if (sock >= SOPEN_MAX) {
    271 		errno = EBADF;
    272 		return NULL;
    273 	}
    274 #endif
    275 	return &sockets[sock];
    276 }
    277 
    278 int
    279 netif_open(void *machdep_hint)
    280 {
    281 	int fd;
    282 	struct iodesc *s;
    283 	struct netif *nif;
    284 
    285 	/* find a free socket */
    286 	for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
    287 		if (s->io_netif == (struct netif *)0)
    288 			goto fnd;
    289 	errno = EMFILE;
    290 	return -1;
    291 
    292 fnd:
    293 	bzero(s, sizeof(*s));
    294 	netif_init();
    295 	nif = netif_select(machdep_hint);
    296 	if (!nif)
    297 		panic("netboot: no interfaces left untried");
    298 	if (netif_probe(nif, machdep_hint)) {
    299 		printf("netboot: couldn't probe %s%d\n",
    300 		    nif->nif_driver->netif_bname, nif->nif_unit);
    301 		errno = EINVAL;
    302 		return -1;
    303 	}
    304 	netif_attach(nif, s, machdep_hint);
    305 
    306 	return fd;
    307 }
    308 
    309 int
    310 netif_close(int sock)
    311 {
    312 #if !defined(LIBSA_NO_FD_CHECKING)
    313 	if (sock >= SOPEN_MAX) {
    314 		errno = EBADF;
    315 		return -1;
    316 	}
    317 #endif
    318 	netif_detach(sockets[sock].io_netif);
    319 	sockets[sock].io_netif = (struct netif *)0;
    320 
    321 	return 0;
    322 }
    323