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