1 /* $OpenBSD: netif.c,v 1.10 2014/07/13 15:31:20 mpi Exp $ */ 2 /* $NetBSD: netif.c,v 1.7 1996/10/13 02:29:03 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1993 Adam Glass 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Adam Glass. 19 * 4. The name of the Author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/mount.h> 38 39 #include <netinet/in.h> 40 41 #include "stand.h" 42 #include "net.h" 43 #include "netif.h" 44 45 struct iodesc sockets[SOPEN_MAX]; 46 #ifdef NETIF_DEBUG 47 int netif_debug = 0; 48 #endif 49 50 /* 51 * netif_init: 52 * 53 * initialize the generic network interface layer 54 */ 55 56 void 57 netif_init(void) 58 { 59 struct netif_driver *drv; 60 int d, i; 61 62 #ifdef NETIF_DEBUG 63 if (netif_debug) 64 printf("netif_init: called\n"); 65 #endif 66 for (d = 0; d < n_netif_drivers; d++) { 67 drv = netif_drivers[d]; 68 for (i = 0; i < drv->netif_nifs; i++) 69 drv->netif_ifs[i].dif_used = 0; 70 } 71 } 72 73 static int 74 netif_match(struct netif *nif, void *machdep_hint) 75 { 76 struct netif_driver *drv = nif->nif_driver; 77 78 #if 0 79 if (netif_debug) 80 printf("%s%d: netif_match (%d)\n", drv->netif_bname, 81 nif->nif_unit, nif->nif_sel); 82 #endif 83 return drv->netif_match(nif, machdep_hint); 84 } 85 86 struct netif * 87 netif_select(void *machdep_hint) 88 { 89 int d, u, unit_done, s; 90 struct netif_driver *drv; 91 struct netif cur_if; 92 static struct netif best_if; 93 int best_val; 94 int val; 95 96 best_val = 0; 97 best_if.nif_driver = NULL; 98 99 #ifdef NETIF_DEBUG 100 if (netif_debug) 101 printf("netif_select: %d interfaces\n", n_netif_drivers); 102 #endif 103 104 for (d = 0; d < n_netif_drivers; d++) { 105 cur_if.nif_driver = netif_drivers[d]; 106 drv = cur_if.nif_driver; 107 108 for (u = 0; u < drv->netif_nifs; u++) { 109 cur_if.nif_unit = u; 110 unit_done = 0; 111 112 #ifdef NETIF_DEBUG 113 if (netif_debug) 114 printf("\t%s%d:", drv->netif_bname, 115 cur_if.nif_unit); 116 #endif 117 118 for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) { 119 cur_if.nif_sel = s; 120 121 if (drv->netif_ifs[u].dif_used & (1 << s)) { 122 #ifdef NETIF_DEBUG 123 if (netif_debug) 124 printf(" [%d used]", s); 125 #endif 126 continue; 127 } 128 129 val = netif_match(&cur_if, machdep_hint); 130 #ifdef NETIF_DEBUG 131 if (netif_debug) 132 printf(" [%d -> %d]", s, val); 133 #endif 134 if (val > best_val) { 135 best_val = val; 136 best_if = cur_if; 137 } 138 } 139 #ifdef NETIF_DEBUG 140 if (netif_debug) 141 printf("\n"); 142 #endif 143 } 144 } 145 146 if (best_if.nif_driver == NULL) 147 return NULL; 148 149 best_if.nif_driver->netif_ifs[best_if.nif_unit].dif_used |= 150 (1 << best_if.nif_sel); 151 152 #ifdef NETIF_DEBUG 153 if (netif_debug) 154 printf("netif_select: %s%d(%d) wins\n", 155 best_if.nif_driver->netif_bname, 156 best_if.nif_unit, best_if.nif_sel); 157 #endif 158 return &best_if; 159 } 160 161 int 162 netif_probe(struct netif *nif, void *machdep_hint) 163 { 164 struct netif_driver *drv = nif->nif_driver; 165 166 #ifdef NETIF_DEBUG 167 if (netif_debug) 168 printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit); 169 #endif 170 return drv->netif_probe(nif, machdep_hint); 171 } 172 173 void 174 netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint) 175 { 176 struct netif_driver *drv = nif->nif_driver; 177 178 #ifdef NETIF_DEBUG 179 if (netif_debug) 180 printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit); 181 #endif 182 desc->io_netif = nif; 183 #ifdef PARANOID 184 if (drv->netif_init == NULL) 185 panic("%s%d: no netif_init support", drv->netif_bname, 186 nif->nif_unit); 187 #endif 188 drv->netif_init(desc, machdep_hint); 189 bzero(drv->netif_ifs[nif->nif_unit].dif_stats, 190 sizeof(struct netif_stats)); 191 } 192 193 void 194 netif_detach(struct netif *nif) 195 { 196 struct netif_driver *drv = nif->nif_driver; 197 198 #ifdef NETIF_DEBUG 199 if (netif_debug) 200 printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit); 201 #endif 202 #ifdef PARANOID 203 if (drv->netif_end == NULL) 204 panic("%s%d: no netif_end support", drv->netif_bname, 205 nif->nif_unit); 206 #endif 207 drv->netif_end(nif); 208 } 209 210 ssize_t 211 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo) 212 { 213 #ifdef NETIF_DEBUG 214 struct netif *nif = desc->io_netif; 215 #endif 216 struct netif_driver *drv = desc->io_netif->nif_driver; 217 ssize_t rv; 218 219 #ifdef NETIF_DEBUG 220 if (netif_debug) 221 printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit); 222 #endif 223 #ifdef PARANOID 224 if (drv->netif_get == NULL) 225 panic("%s%d: no netif_get support", drv->netif_bname, 226 nif->nif_unit); 227 #endif 228 rv = drv->netif_get(desc, pkt, len, timo); 229 #ifdef NETIF_DEBUG 230 if (netif_debug) 231 printf("%s%d: netif_get returning %d\n", drv->netif_bname, 232 nif->nif_unit, rv); 233 #endif 234 return rv; 235 } 236 237 ssize_t 238 netif_put(struct iodesc *desc, void *pkt, size_t len) 239 { 240 #ifdef NETIF_DEBUG 241 struct netif *nif = desc->io_netif; 242 #endif 243 struct netif_driver *drv = desc->io_netif->nif_driver; 244 ssize_t rv; 245 246 #ifdef NETIF_DEBUG 247 if (netif_debug) 248 printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit); 249 #endif 250 #ifdef PARANOID 251 if (drv->netif_put == NULL) 252 panic("%s%d: no netif_put support", drv->netif_bname, 253 nif->nif_unit); 254 #endif 255 rv = drv->netif_put(desc, pkt, len); 256 #ifdef NETIF_DEBUG 257 if (netif_debug) 258 printf("%s%d: netif_put returning %d\n", drv->netif_bname, 259 nif->nif_unit, rv); 260 #endif 261 return rv; 262 } 263 264 struct iodesc * 265 socktodesc(sock) 266 int sock; 267 { 268 if (sock >= SOPEN_MAX) { 269 errno = EBADF; 270 return (NULL); 271 } 272 return (&sockets[sock]); 273 } 274 275 int 276 netif_open(void *machdep_hint) 277 { 278 int fd; 279 struct iodesc *s; 280 struct netif *nif; 281 282 /* find a free socket */ 283 for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++) 284 if (s->io_netif == (struct netif *)0) 285 goto fnd; 286 errno = EMFILE; 287 return (-1); 288 289 fnd: 290 bzero(s, sizeof(*s)); 291 netif_init(); 292 nif = netif_select(machdep_hint); 293 if (!nif) 294 panic("netboot: no interfaces left untried"); 295 if (netif_probe(nif, machdep_hint)) { 296 printf("netboot: couldn't probe %s%d\n", 297 nif->nif_driver->netif_bname, nif->nif_unit); 298 errno = EINVAL; 299 return(-1); 300 } 301 netif_attach(nif, s, machdep_hint); 302 303 return(fd); 304 } 305 306 int 307 netif_close(int sock) 308 { 309 if (sock >= SOPEN_MAX) { 310 errno = EBADF; 311 return(-1); 312 } 313 netif_detach(sockets[sock].io_netif); 314 sockets[sock].io_netif = (struct netif *)0; 315 316 return(0); 317 } 318