1 /* $NetBSD: netif.c,v 1.10 1997/09/06 13:57:14 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/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/types.h> 39 #include <sys/cdefs.h> 40 #include <string.h> 41 42 #include <netinet/in.h> 43 #include <netinet/in_systm.h> 44 45 #include "stand.h" 46 #include "net.h" 47 #include "netif.h" 48 49 typedef TAILQ_HEAD(socket_list, iodesc) socket_list_t; 50 51 /* 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. 59 */ 60 static socket_list_t sockets = TAILQ_HEAD_INITIALIZER(sockets); 61 62 #ifdef NETIF_DEBUG 63 int netif_debug = 0; 64 #endif 65 66 /* 67 * netif_init: 68 * 69 * initialize the generic network interface layer 70 */ 71 72 void 73 netif_init(void) 74 { 75 struct netif_driver *drv; 76 int d, i; 77 78 #ifdef NETIF_DEBUG 79 if (netif_debug) 80 printf("netif_init: called\n"); 81 #endif 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; 86 } 87 } 88 89 int 90 netif_match(struct netif *nif, void *machdep_hint) 91 { 92 struct netif_driver *drv = nif->nif_driver; 93 94 #if NETIF_DEBUG 95 if (netif_debug) 96 printf("%s%d: netif_match (%d)\n", drv->netif_bname, 97 nif->nif_unit, nif->nif_sel); 98 #endif 99 return drv->netif_match(nif, machdep_hint); 100 } 101 102 struct netif * 103 netif_select(void *machdep_hint) 104 { 105 int d, u, s; 106 struct netif_driver *drv; 107 struct netif cur_if; 108 static struct netif best_if; 109 int best_val; 110 int val; 111 112 best_val = 0; 113 best_if.nif_driver = NULL; 114 115 for (d = 0; netif_drivers[d] != NULL; d++) { 116 cur_if.nif_driver = netif_drivers[d]; 117 drv = cur_if.nif_driver; 118 119 for (u = 0; u < drv->netif_nifs; u++) { 120 cur_if.nif_unit = u; 121 122 #ifdef NETIF_DEBUG 123 if (netif_debug) 124 printf("\t%s%d:", drv->netif_bname, 125 cur_if.nif_unit); 126 #endif 127 128 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) { 129 cur_if.nif_sel = s; 130 131 if (drv->netif_ifs[u].dif_used & (1 << s)) { 132 #ifdef NETIF_DEBUG 133 if (netif_debug) 134 printf(" [%d used]", s); 135 #endif 136 continue; 137 } 138 139 val = netif_match(&cur_if, machdep_hint); 140 #ifdef NETIF_DEBUG 141 if (netif_debug) 142 printf(" [%d -> %d]", s, val); 143 #endif 144 if (val > best_val) { 145 best_val = val; 146 best_if = cur_if; 147 } 148 } 149 #ifdef NETIF_DEBUG 150 if (netif_debug) 151 printf("\n"); 152 #endif 153 } 154 } 155 156 if (best_if.nif_driver == NULL) 157 return NULL; 158 159 best_if.nif_driver-> 160 netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel); 161 162 #ifdef NETIF_DEBUG 163 if (netif_debug) 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); 167 #endif 168 return &best_if; 169 } 170 171 int 172 netif_probe(struct netif *nif, void *machdep_hint) 173 { 174 struct netif_driver *drv = nif->nif_driver; 175 176 #ifdef NETIF_DEBUG 177 if (netif_debug) 178 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit); 179 #endif 180 return drv->netif_probe(nif, machdep_hint); 181 } 182 183 void 184 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint) 185 { 186 struct netif_driver *drv = nif->nif_driver; 187 188 #ifdef NETIF_DEBUG 189 if (netif_debug) 190 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit); 191 #endif 192 desc->io_netif = nif; 193 #ifdef PARANOID 194 if (drv->netif_init == NULL) 195 panic("%s%d: no netif_init support", drv->netif_bname, 196 nif->nif_unit); 197 #endif 198 drv->netif_init(desc, machdep_hint); 199 bzero(drv->netif_ifs[nif->nif_unit].dif_stats, 200 sizeof(struct netif_stats)); 201 } 202 203 void 204 netif_detach(struct netif *nif) 205 { 206 struct netif_driver *drv = nif->nif_driver; 207 208 #ifdef NETIF_DEBUG 209 if (netif_debug) 210 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit); 211 #endif 212 #ifdef PARANOID 213 if (drv->netif_end == NULL) 214 panic("%s%d: no netif_end support", drv->netif_bname, 215 nif->nif_unit); 216 #endif 217 drv->netif_end(nif); 218 } 219 220 ssize_t 221 netif_get(struct iodesc *desc, void **pkt, time_t timo) 222 { 223 #ifdef NETIF_DEBUG 224 struct netif *nif = desc->io_netif; 225 #endif 226 struct netif_driver *drv = desc->io_netif->nif_driver; 227 ssize_t rv; 228 229 #ifdef NETIF_DEBUG 230 if (netif_debug) 231 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit); 232 #endif 233 #ifdef PARANOID 234 if (drv->netif_get == NULL) 235 panic("%s%d: no netif_get support", drv->netif_bname, 236 nif->nif_unit); 237 #endif 238 rv = drv->netif_get(desc, pkt, timo); 239 #ifdef NETIF_DEBUG 240 if (netif_debug) 241 printf("%s%d: netif_get returning %d\n", drv->netif_bname, 242 nif->nif_unit, (int)rv); 243 #endif 244 return (rv); 245 } 246 247 ssize_t 248 netif_put(struct iodesc *desc, void *pkt, size_t len) 249 { 250 #ifdef NETIF_DEBUG 251 struct netif *nif = desc->io_netif; 252 #endif 253 struct netif_driver *drv = desc->io_netif->nif_driver; 254 ssize_t rv; 255 256 #ifdef NETIF_DEBUG 257 if (netif_debug) 258 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit); 259 #endif 260 #ifdef PARANOID 261 if (drv->netif_put == NULL) 262 panic("%s%d: no netif_put support", drv->netif_bname, 263 nif->nif_unit); 264 #endif 265 rv = drv->netif_put(desc, pkt, len); 266 #ifdef NETIF_DEBUG 267 if (netif_debug) 268 printf("%s%d: netif_put returning %d\n", drv->netif_bname, 269 nif->nif_unit, (int)rv); 270 #endif 271 return (rv); 272 } 273 274 /* 275 * socktodesc_impl: 276 * 277 * Walk socket list and return pointer to iodesc structure. 278 * if id is < 0, return first unused iodesc. 279 */ 280 static struct iodesc * 281 socktodesc_impl(int socket) 282 { 283 struct iodesc *s; 284 285 TAILQ_FOREACH(s, &sockets, io_link) { 286 /* search by socket id */ 287 if (socket >= 0) { 288 if (s->io_id == socket) 289 break; 290 continue; 291 } 292 /* search for first unused entry */ 293 if (s->io_netif == NULL) 294 break; 295 } 296 return (s); 297 } 298 299 struct iodesc * 300 socktodesc(int sock) 301 { 302 struct iodesc *desc; 303 304 if (sock < 0) 305 desc = NULL; 306 else 307 desc = socktodesc_impl(sock); 308 309 if (desc == NULL) 310 errno = EBADF; 311 312 return (desc); 313 } 314 315 int 316 netif_open(void *machdep_hint) 317 { 318 struct iodesc *s; 319 struct netif *nif; 320 321 /* find a free socket */ 322 s = socktodesc_impl(-1); 323 if (s == NULL) { 324 struct iodesc *last; 325 326 s = calloc(1, sizeof (*s)); 327 if (s == NULL) 328 return (-1); 329 330 last = TAILQ_LAST(&sockets, socket_list); 331 if (last != NULL) 332 s->io_id = last->io_id + 1; 333 TAILQ_INSERT_TAIL(&sockets, s, io_link); 334 } 335 336 netif_init(); 337 nif = netif_select(machdep_hint); 338 if (!nif) 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); 343 errno = EINVAL; 344 return (-1); 345 } 346 netif_attach(nif, s, machdep_hint); 347 348 return (s->io_id); 349 } 350 351 int 352 netif_close(int sock) 353 { 354 struct iodesc *s, *last; 355 int err; 356 357 err = 0; 358 s = socktodesc_impl(sock); 359 if (s == NULL || sock < 0) { 360 err = EBADF; 361 return (-1); 362 } 363 netif_detach(s->io_netif); 364 bzero(&s->destip, sizeof (s->destip)); 365 bzero(&s->myip, sizeof (s->myip)); 366 s->destport = 0; 367 s->myport = 0; 368 s->xid = 0; 369 bzero(s->myea, sizeof (s->myea)); 370 s->io_netif = NULL; 371 372 /* free unused entries from tail. */ 373 TAILQ_FOREACH_REVERSE_SAFE(last, &sockets, socket_list, io_link, s) { 374 if (last->io_netif != NULL) 375 break; 376 TAILQ_REMOVE(&sockets, last, io_link); 377 free(last); 378 } 379 380 if (err) { 381 errno = err; 382 return (-1); 383 } 384 385 return (0); 386 } 387