1 /* $NetBSD: netif.c,v 1.18 2003/03/12 14:49:19 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/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() 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(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", 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", 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 struct netif *nif = desc->io_netif; 233 struct netif_driver *drv = nif->nif_driver; 234 ssize_t rv; 235 236 #ifdef NETIF_DEBUG 237 if (netif_debug) 238 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit); 239 #endif 240 #ifdef PARANOID 241 if (drv->netif_get == NULL) 242 panic("%s%d: no netif_get support", drv->netif_bname, 243 nif->nif_unit); 244 #endif 245 rv = drv->netif_get(desc, pkt, len, timo); 246 #ifdef NETIF_DEBUG 247 if (netif_debug) 248 printf("%s%d: netif_get returning %d\n", drv->netif_bname, 249 nif->nif_unit, (int)rv); 250 #endif 251 return rv; 252 } 253 254 ssize_t 255 netif_put(desc, pkt, len) 256 struct iodesc *desc; 257 void *pkt; 258 size_t len; 259 { 260 struct netif *nif = desc->io_netif; 261 struct netif_driver *drv = nif->nif_driver; 262 ssize_t rv; 263 264 #ifdef NETIF_DEBUG 265 if (netif_debug) 266 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit); 267 #endif 268 #ifdef PARANOID 269 if (drv->netif_put == NULL) 270 panic("%s%d: no netif_put support", drv->netif_bname, 271 nif->nif_unit); 272 #endif 273 rv = drv->netif_put(desc, pkt, len); 274 #ifdef NETIF_DEBUG 275 if (netif_debug) 276 printf("%s%d: netif_put returning %d\n", drv->netif_bname, 277 nif->nif_unit, (int)rv); 278 #endif 279 return rv; 280 } 281 282 struct iodesc * 283 socktodesc(sock) 284 int sock; 285 { 286 #if !defined(LIBSA_NO_FD_CHECKING) 287 if (sock >= SOPEN_MAX) { 288 errno = EBADF; 289 return (NULL); 290 } 291 #endif 292 return (&sockets[sock]); 293 } 294 295 int 296 netif_open(machdep_hint) 297 void *machdep_hint; 298 { 299 int fd; 300 struct iodesc *s; 301 struct netif *nif; 302 303 /* find a free socket */ 304 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++) 305 if (s->io_netif == (struct netif *)0) 306 goto fnd; 307 errno = EMFILE; 308 return (-1); 309 310 fnd: 311 bzero(s, sizeof(*s)); 312 netif_init(); 313 nif = netif_select(machdep_hint); 314 if (!nif) 315 panic("netboot: no interfaces left untried"); 316 if (netif_probe(nif, machdep_hint)) { 317 printf("netboot: couldn't probe %s%d\n", 318 nif->nif_driver->netif_bname, nif->nif_unit); 319 errno = EINVAL; 320 return(-1); 321 } 322 netif_attach(nif, s, machdep_hint); 323 324 return(fd); 325 } 326 327 int 328 netif_close(sock) 329 int sock; 330 { 331 #if !defined(LIBSA_NO_FD_CHECKING) 332 if (sock >= SOPEN_MAX) { 333 errno = EBADF; 334 return(-1); 335 } 336 #endif 337 netif_detach(sockets[sock].io_netif); 338 sockets[sock].io_netif = (struct netif *)0; 339 340 return(0); 341 } 342