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