1 /* $NetBSD: if_bridge.c,v 1.27 2004/12/04 18:31:43 peter Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by Jason L. Wright 53 * 4. The name of the author may not be used to endorse or promote products 54 * derived from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 59 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 60 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 61 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 62 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 65 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 * 68 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp 69 */ 70 71 /* 72 * Network interface bridge support. 73 * 74 * TODO: 75 * 76 * - Currently only supports Ethernet-like interfaces (Ethernet, 77 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way 78 * to bridge other types of interfaces (FDDI-FDDI, and maybe 79 * consider heterogenous bridges). 80 */ 81 82 #include <sys/cdefs.h> 83 __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.27 2004/12/04 18:31:43 peter Exp $"); 84 85 #include "opt_bridge_ipf.h" 86 #include "opt_inet.h" 87 #include "opt_pfil_hooks.h" 88 #include "bpfilter.h" 89 90 #include <sys/param.h> 91 #include <sys/kernel.h> 92 #include <sys/mbuf.h> 93 #include <sys/queue.h> 94 #include <sys/socket.h> 95 #include <sys/sockio.h> 96 #include <sys/systm.h> 97 #include <sys/proc.h> 98 #include <sys/pool.h> 99 100 #if NBPFILTER > 0 101 #include <net/bpf.h> 102 #endif 103 #include <net/if.h> 104 #include <net/if_dl.h> 105 #include <net/if_types.h> 106 #include <net/if_llc.h> 107 108 #include <net/if_ether.h> 109 #include <net/if_bridgevar.h> 110 111 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS) 112 /* Used for bridge_ip[6]_checkbasic */ 113 #include <netinet/in.h> 114 #include <netinet/in_systm.h> 115 #include <netinet/ip.h> 116 #include <netinet/ip_var.h> 117 118 #include <netinet/ip6.h> 119 #include <netinet6/in6_var.h> 120 #include <netinet6/ip6_var.h> 121 #endif /* BRIDGE_IPF && PFIL_HOOKS */ 122 123 /* 124 * Size of the route hash table. Must be a power of two. 125 */ 126 #ifndef BRIDGE_RTHASH_SIZE 127 #define BRIDGE_RTHASH_SIZE 1024 128 #endif 129 130 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1) 131 132 /* 133 * Maximum number of addresses to cache. 134 */ 135 #ifndef BRIDGE_RTABLE_MAX 136 #define BRIDGE_RTABLE_MAX 100 137 #endif 138 139 /* 140 * Spanning tree defaults. 141 */ 142 #define BSTP_DEFAULT_MAX_AGE (20 * 256) 143 #define BSTP_DEFAULT_HELLO_TIME (2 * 256) 144 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 145 #define BSTP_DEFAULT_HOLD_TIME (1 * 256) 146 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 147 #define BSTP_DEFAULT_PORT_PRIORITY 0x80 148 #define BSTP_DEFAULT_PATH_COST 55 149 150 /* 151 * Timeout (in seconds) for entries learned dynamically. 152 */ 153 #ifndef BRIDGE_RTABLE_TIMEOUT 154 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ 155 #endif 156 157 /* 158 * Number of seconds between walks of the route list. 159 */ 160 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD 161 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) 162 #endif 163 164 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; 165 166 struct pool bridge_rtnode_pool; 167 168 void bridgeattach(int); 169 170 int bridge_clone_create(struct if_clone *, int); 171 int bridge_clone_destroy(struct ifnet *); 172 173 int bridge_ioctl(struct ifnet *, u_long, caddr_t); 174 int bridge_init(struct ifnet *); 175 void bridge_stop(struct ifnet *, int); 176 void bridge_start(struct ifnet *); 177 178 void bridge_forward(struct bridge_softc *, struct mbuf *m); 179 180 void bridge_timer(void *); 181 182 void bridge_broadcast(struct bridge_softc *, struct ifnet *, struct mbuf *); 183 184 int bridge_rtupdate(struct bridge_softc *, const uint8_t *, 185 struct ifnet *, int, uint8_t); 186 struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *); 187 void bridge_rttrim(struct bridge_softc *); 188 void bridge_rtage(struct bridge_softc *); 189 void bridge_rtflush(struct bridge_softc *, int); 190 int bridge_rtdaddr(struct bridge_softc *, const uint8_t *); 191 void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp); 192 193 int bridge_rtable_init(struct bridge_softc *); 194 void bridge_rtable_fini(struct bridge_softc *); 195 196 struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, 197 const uint8_t *); 198 int bridge_rtnode_insert(struct bridge_softc *, struct bridge_rtnode *); 199 void bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *); 200 201 struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, 202 const char *name); 203 struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, 204 struct ifnet *ifp); 205 void bridge_delete_member(struct bridge_softc *, struct bridge_iflist *); 206 207 int bridge_ioctl_add(struct bridge_softc *, void *); 208 int bridge_ioctl_del(struct bridge_softc *, void *); 209 int bridge_ioctl_gifflags(struct bridge_softc *, void *); 210 int bridge_ioctl_sifflags(struct bridge_softc *, void *); 211 int bridge_ioctl_scache(struct bridge_softc *, void *); 212 int bridge_ioctl_gcache(struct bridge_softc *, void *); 213 int bridge_ioctl_gifs(struct bridge_softc *, void *); 214 int bridge_ioctl_rts(struct bridge_softc *, void *); 215 int bridge_ioctl_saddr(struct bridge_softc *, void *); 216 int bridge_ioctl_sto(struct bridge_softc *, void *); 217 int bridge_ioctl_gto(struct bridge_softc *, void *); 218 int bridge_ioctl_daddr(struct bridge_softc *, void *); 219 int bridge_ioctl_flush(struct bridge_softc *, void *); 220 int bridge_ioctl_gpri(struct bridge_softc *, void *); 221 int bridge_ioctl_spri(struct bridge_softc *, void *); 222 int bridge_ioctl_ght(struct bridge_softc *, void *); 223 int bridge_ioctl_sht(struct bridge_softc *, void *); 224 int bridge_ioctl_gfd(struct bridge_softc *, void *); 225 int bridge_ioctl_sfd(struct bridge_softc *, void *); 226 int bridge_ioctl_gma(struct bridge_softc *, void *); 227 int bridge_ioctl_sma(struct bridge_softc *, void *); 228 int bridge_ioctl_sifprio(struct bridge_softc *, void *); 229 int bridge_ioctl_sifcost(struct bridge_softc *, void *); 230 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS) 231 int bridge_ioctl_gfilt(struct bridge_softc *, void *); 232 int bridge_ioctl_sfilt(struct bridge_softc *, void *); 233 static int bridge_ipf(void *, struct mbuf **, struct ifnet *, int); 234 static int bridge_ip_checkbasic(struct mbuf **mp); 235 # ifdef INET6 236 static int bridge_ip6_checkbasic(struct mbuf **mp); 237 # endif /* INET6 */ 238 #endif /* BRIDGE_IPF && PFIL_HOOKS */ 239 240 struct bridge_control { 241 int (*bc_func)(struct bridge_softc *, void *); 242 int bc_argsize; 243 int bc_flags; 244 }; 245 246 #define BC_F_COPYIN 0x01 /* copy arguments in */ 247 #define BC_F_COPYOUT 0x02 /* copy arguments out */ 248 #define BC_F_SUSER 0x04 /* do super-user check */ 249 250 const struct bridge_control bridge_control_table[] = { 251 { bridge_ioctl_add, sizeof(struct ifbreq), 252 BC_F_COPYIN|BC_F_SUSER }, 253 { bridge_ioctl_del, sizeof(struct ifbreq), 254 BC_F_COPYIN|BC_F_SUSER }, 255 256 { bridge_ioctl_gifflags, sizeof(struct ifbreq), 257 BC_F_COPYIN|BC_F_COPYOUT }, 258 { bridge_ioctl_sifflags, sizeof(struct ifbreq), 259 BC_F_COPYIN|BC_F_SUSER }, 260 261 { bridge_ioctl_scache, sizeof(struct ifbrparam), 262 BC_F_COPYIN|BC_F_SUSER }, 263 { bridge_ioctl_gcache, sizeof(struct ifbrparam), 264 BC_F_COPYOUT }, 265 266 { bridge_ioctl_gifs, sizeof(struct ifbifconf), 267 BC_F_COPYIN|BC_F_COPYOUT }, 268 { bridge_ioctl_rts, sizeof(struct ifbaconf), 269 BC_F_COPYIN|BC_F_COPYOUT }, 270 271 { bridge_ioctl_saddr, sizeof(struct ifbareq), 272 BC_F_COPYIN|BC_F_SUSER }, 273 274 { bridge_ioctl_sto, sizeof(struct ifbrparam), 275 BC_F_COPYIN|BC_F_SUSER }, 276 { bridge_ioctl_gto, sizeof(struct ifbrparam), 277 BC_F_COPYOUT }, 278 279 { bridge_ioctl_daddr, sizeof(struct ifbareq), 280 BC_F_COPYIN|BC_F_SUSER }, 281 282 { bridge_ioctl_flush, sizeof(struct ifbreq), 283 BC_F_COPYIN|BC_F_SUSER }, 284 285 { bridge_ioctl_gpri, sizeof(struct ifbrparam), 286 BC_F_COPYOUT }, 287 { bridge_ioctl_spri, sizeof(struct ifbrparam), 288 BC_F_COPYIN|BC_F_SUSER }, 289 290 { bridge_ioctl_ght, sizeof(struct ifbrparam), 291 BC_F_COPYOUT }, 292 { bridge_ioctl_sht, sizeof(struct ifbrparam), 293 BC_F_COPYIN|BC_F_SUSER }, 294 295 { bridge_ioctl_gfd, sizeof(struct ifbrparam), 296 BC_F_COPYOUT }, 297 { bridge_ioctl_sfd, sizeof(struct ifbrparam), 298 BC_F_COPYIN|BC_F_SUSER }, 299 300 { bridge_ioctl_gma, sizeof(struct ifbrparam), 301 BC_F_COPYOUT }, 302 { bridge_ioctl_sma, sizeof(struct ifbrparam), 303 BC_F_COPYIN|BC_F_SUSER }, 304 305 { bridge_ioctl_sifprio, sizeof(struct ifbreq), 306 BC_F_COPYIN|BC_F_SUSER }, 307 308 { bridge_ioctl_sifcost, sizeof(struct ifbreq), 309 BC_F_COPYIN|BC_F_SUSER }, 310 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS) 311 { bridge_ioctl_gfilt, sizeof(struct ifbrparam), 312 BC_F_COPYOUT }, 313 { bridge_ioctl_sfilt, sizeof(struct ifbrparam), 314 BC_F_COPYIN|BC_F_SUSER }, 315 #endif /* BRIDGE_IPF && PFIL_HOOKS */ 316 }; 317 const int bridge_control_table_size = 318 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]); 319 320 LIST_HEAD(, bridge_softc) bridge_list; 321 322 struct if_clone bridge_cloner = 323 IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy); 324 325 /* 326 * bridgeattach: 327 * 328 * Pseudo-device attach routine. 329 */ 330 void 331 bridgeattach(int n) 332 { 333 334 pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode), 335 0, 0, 0, "brtpl", NULL); 336 337 LIST_INIT(&bridge_list); 338 if_clone_attach(&bridge_cloner); 339 } 340 341 /* 342 * bridge_clone_create: 343 * 344 * Create a new bridge instance. 345 */ 346 int 347 bridge_clone_create(struct if_clone *ifc, int unit) 348 { 349 struct bridge_softc *sc; 350 struct ifnet *ifp; 351 int s; 352 353 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK); 354 memset(sc, 0, sizeof(*sc)); 355 ifp = &sc->sc_if; 356 357 sc->sc_brtmax = BRIDGE_RTABLE_MAX; 358 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; 359 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE; 360 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME; 361 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY; 362 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY; 363 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME; 364 sc->sc_filter_flags = 0; 365 366 /* Initialize our routing table. */ 367 bridge_rtable_init(sc); 368 369 callout_init(&sc->sc_brcallout); 370 callout_init(&sc->sc_bstpcallout); 371 372 LIST_INIT(&sc->sc_iflist); 373 374 snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d", ifc->ifc_name, 375 unit); 376 ifp->if_softc = sc; 377 ifp->if_mtu = ETHERMTU; 378 ifp->if_ioctl = bridge_ioctl; 379 ifp->if_output = bridge_output; 380 ifp->if_start = bridge_start; 381 ifp->if_stop = bridge_stop; 382 ifp->if_init = bridge_init; 383 ifp->if_type = IFT_BRIDGE; 384 ifp->if_addrlen = 0; 385 ifp->if_dlt = DLT_EN10MB; 386 ifp->if_hdrlen = ETHER_HDR_LEN; 387 388 if_attach(ifp); 389 390 if_alloc_sadl(ifp); 391 392 s = splnet(); 393 LIST_INSERT_HEAD(&bridge_list, sc, sc_list); 394 splx(s); 395 396 return (0); 397 } 398 399 /* 400 * bridge_clone_destroy: 401 * 402 * Destroy a bridge instance. 403 */ 404 int 405 bridge_clone_destroy(struct ifnet *ifp) 406 { 407 struct bridge_softc *sc = ifp->if_softc; 408 struct bridge_iflist *bif; 409 int s; 410 411 s = splnet(); 412 413 bridge_stop(ifp, 1); 414 415 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL) 416 bridge_delete_member(sc, bif); 417 418 LIST_REMOVE(sc, sc_list); 419 420 splx(s); 421 422 if_detach(ifp); 423 424 /* Tear down the routing table. */ 425 bridge_rtable_fini(sc); 426 427 free(sc, M_DEVBUF); 428 429 return (0); 430 } 431 432 /* 433 * bridge_ioctl: 434 * 435 * Handle a control request from the operator. 436 */ 437 int 438 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 439 { 440 struct bridge_softc *sc = ifp->if_softc; 441 struct proc *p = curproc; /* XXX */ 442 union { 443 struct ifbreq ifbreq; 444 struct ifbifconf ifbifconf; 445 struct ifbareq ifbareq; 446 struct ifbaconf ifbaconf; 447 struct ifbrparam ifbrparam; 448 } args; 449 struct ifdrv *ifd = (struct ifdrv *) data; 450 const struct bridge_control *bc; 451 int s, error = 0; 452 453 s = splnet(); 454 455 switch (cmd) { 456 case SIOCGDRVSPEC: 457 case SIOCSDRVSPEC: 458 if (ifd->ifd_cmd >= bridge_control_table_size) { 459 error = EINVAL; 460 break; 461 } 462 bc = &bridge_control_table[ifd->ifd_cmd]; 463 464 if (cmd == SIOCGDRVSPEC && 465 (bc->bc_flags & BC_F_COPYOUT) == 0) { 466 error = EINVAL; 467 break; 468 } 469 else if (cmd == SIOCSDRVSPEC && 470 (bc->bc_flags & BC_F_COPYOUT) != 0) { 471 error = EINVAL; 472 break; 473 } 474 475 if (bc->bc_flags & BC_F_SUSER) { 476 error = suser(p->p_ucred, &p->p_acflag); 477 if (error) 478 break; 479 } 480 481 if (ifd->ifd_len != bc->bc_argsize || 482 ifd->ifd_len > sizeof(args)) { 483 error = EINVAL; 484 break; 485 } 486 487 if (bc->bc_flags & BC_F_COPYIN) { 488 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 489 if (error) 490 break; 491 } 492 493 error = (*bc->bc_func)(sc, &args); 494 if (error) 495 break; 496 497 if (bc->bc_flags & BC_F_COPYOUT) 498 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 499 500 break; 501 502 case SIOCSIFFLAGS: 503 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) { 504 /* 505 * If interface is marked down and it is running, 506 * then stop and disable it. 507 */ 508 (*ifp->if_stop)(ifp, 1); 509 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) { 510 /* 511 * If interface is marked up and it is stopped, then 512 * start it. 513 */ 514 error = (*ifp->if_init)(ifp); 515 } 516 break; 517 518 default: 519 error = ENOTTY; 520 break; 521 } 522 523 splx(s); 524 525 return (error); 526 } 527 528 /* 529 * bridge_lookup_member: 530 * 531 * Lookup a bridge member interface. Must be called at splnet(). 532 */ 533 struct bridge_iflist * 534 bridge_lookup_member(struct bridge_softc *sc, const char *name) 535 { 536 struct bridge_iflist *bif; 537 struct ifnet *ifp; 538 539 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 540 ifp = bif->bif_ifp; 541 if (strcmp(ifp->if_xname, name) == 0) 542 return (bif); 543 } 544 545 return (NULL); 546 } 547 548 /* 549 * bridge_lookup_member_if: 550 * 551 * Lookup a bridge member interface by ifnet*. Must be called at splnet(). 552 */ 553 struct bridge_iflist * 554 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp) 555 { 556 struct bridge_iflist *bif; 557 558 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 559 if (bif->bif_ifp == member_ifp) 560 return (bif); 561 } 562 563 return (NULL); 564 } 565 566 /* 567 * bridge_delete_member: 568 * 569 * Delete the specified member interface. 570 */ 571 void 572 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif) 573 { 574 struct ifnet *ifs = bif->bif_ifp; 575 576 switch (ifs->if_type) { 577 case IFT_ETHER: 578 /* 579 * Take the interface out of promiscuous mode. 580 */ 581 (void) ifpromisc(ifs, 0); 582 break; 583 584 default: 585 #ifdef DIAGNOSTIC 586 panic("bridge_delete_member: impossible"); 587 #endif 588 break; 589 } 590 591 ifs->if_bridge = NULL; 592 LIST_REMOVE(bif, bif_next); 593 594 bridge_rtdelete(sc, ifs); 595 596 free(bif, M_DEVBUF); 597 598 if (sc->sc_if.if_flags & IFF_RUNNING) 599 bstp_initialization(sc); 600 } 601 602 int 603 bridge_ioctl_add(struct bridge_softc *sc, void *arg) 604 { 605 struct ifbreq *req = arg; 606 struct bridge_iflist *bif = NULL; 607 struct ifnet *ifs; 608 int error = 0; 609 610 ifs = ifunit(req->ifbr_ifsname); 611 if (ifs == NULL) 612 return (ENOENT); 613 614 if (sc->sc_if.if_mtu != ifs->if_mtu) 615 return (EINVAL); 616 617 if (ifs->if_bridge == sc) 618 return (EEXIST); 619 620 if (ifs->if_bridge != NULL) 621 return (EBUSY); 622 623 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT); 624 if (bif == NULL) 625 return (ENOMEM); 626 627 switch (ifs->if_type) { 628 case IFT_ETHER: 629 /* 630 * Place the interface into promiscuous mode. 631 */ 632 error = ifpromisc(ifs, 1); 633 if (error) 634 goto out; 635 break; 636 637 default: 638 error = EINVAL; 639 goto out; 640 } 641 642 bif->bif_ifp = ifs; 643 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER; 644 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY; 645 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST; 646 647 ifs->if_bridge = sc; 648 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next); 649 650 if (sc->sc_if.if_flags & IFF_RUNNING) 651 bstp_initialization(sc); 652 else 653 bstp_stop(sc); 654 655 out: 656 if (error) { 657 if (bif != NULL) 658 free(bif, M_DEVBUF); 659 } 660 return (error); 661 } 662 663 int 664 bridge_ioctl_del(struct bridge_softc *sc, void *arg) 665 { 666 struct ifbreq *req = arg; 667 struct bridge_iflist *bif; 668 669 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 670 if (bif == NULL) 671 return (ENOENT); 672 673 bridge_delete_member(sc, bif); 674 675 return (0); 676 } 677 678 int 679 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) 680 { 681 struct ifbreq *req = arg; 682 struct bridge_iflist *bif; 683 684 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 685 if (bif == NULL) 686 return (ENOENT); 687 688 req->ifbr_ifsflags = bif->bif_flags; 689 req->ifbr_state = bif->bif_state; 690 req->ifbr_priority = bif->bif_priority; 691 req->ifbr_path_cost = bif->bif_path_cost; 692 req->ifbr_portno = bif->bif_ifp->if_index & 0xff; 693 694 return (0); 695 } 696 697 int 698 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) 699 { 700 struct ifbreq *req = arg; 701 struct bridge_iflist *bif; 702 703 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 704 if (bif == NULL) 705 return (ENOENT); 706 707 if (req->ifbr_ifsflags & IFBIF_STP) { 708 switch (bif->bif_ifp->if_type) { 709 case IFT_ETHER: 710 /* These can do spanning tree. */ 711 break; 712 713 default: 714 /* Nothing else can. */ 715 return (EINVAL); 716 } 717 } 718 719 bif->bif_flags = req->ifbr_ifsflags; 720 721 if (sc->sc_if.if_flags & IFF_RUNNING) 722 bstp_initialization(sc); 723 724 return (0); 725 } 726 727 int 728 bridge_ioctl_scache(struct bridge_softc *sc, void *arg) 729 { 730 struct ifbrparam *param = arg; 731 732 sc->sc_brtmax = param->ifbrp_csize; 733 bridge_rttrim(sc); 734 735 return (0); 736 } 737 738 int 739 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) 740 { 741 struct ifbrparam *param = arg; 742 743 param->ifbrp_csize = sc->sc_brtmax; 744 745 return (0); 746 } 747 748 int 749 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg) 750 { 751 struct ifbifconf *bifc = arg; 752 struct bridge_iflist *bif; 753 struct ifbreq breq; 754 int count, len, error = 0; 755 756 count = 0; 757 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) 758 count++; 759 760 if (bifc->ifbic_len == 0) { 761 bifc->ifbic_len = sizeof(breq) * count; 762 return (0); 763 } 764 765 count = 0; 766 len = bifc->ifbic_len; 767 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 768 if (len < sizeof(breq)) 769 break; 770 771 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, 772 sizeof(breq.ifbr_ifsname)); 773 breq.ifbr_ifsflags = bif->bif_flags; 774 breq.ifbr_state = bif->bif_state; 775 breq.ifbr_priority = bif->bif_priority; 776 breq.ifbr_path_cost = bif->bif_path_cost; 777 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff; 778 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq)); 779 if (error) 780 break; 781 count++; 782 len -= sizeof(breq); 783 } 784 785 bifc->ifbic_len = sizeof(breq) * count; 786 return (error); 787 } 788 789 int 790 bridge_ioctl_rts(struct bridge_softc *sc, void *arg) 791 { 792 struct ifbaconf *bac = arg; 793 struct bridge_rtnode *brt; 794 struct ifbareq bareq; 795 int count = 0, error = 0, len; 796 797 if (bac->ifbac_len == 0) 798 return (0); 799 800 len = bac->ifbac_len; 801 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { 802 if (len < sizeof(bareq)) 803 goto out; 804 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname, 805 sizeof(bareq.ifba_ifsname)); 806 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); 807 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 808 bareq.ifba_expire = brt->brt_expire - mono_time.tv_sec; 809 else 810 bareq.ifba_expire = 0; 811 bareq.ifba_flags = brt->brt_flags; 812 813 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq)); 814 if (error) 815 goto out; 816 count++; 817 len -= sizeof(bareq); 818 } 819 out: 820 bac->ifbac_len = sizeof(bareq) * count; 821 return (error); 822 } 823 824 int 825 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg) 826 { 827 struct ifbareq *req = arg; 828 struct bridge_iflist *bif; 829 int error; 830 831 bif = bridge_lookup_member(sc, req->ifba_ifsname); 832 if (bif == NULL) 833 return (ENOENT); 834 835 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1, 836 req->ifba_flags); 837 838 return (error); 839 } 840 841 int 842 bridge_ioctl_sto(struct bridge_softc *sc, void *arg) 843 { 844 struct ifbrparam *param = arg; 845 846 sc->sc_brttimeout = param->ifbrp_ctime; 847 848 return (0); 849 } 850 851 int 852 bridge_ioctl_gto(struct bridge_softc *sc, void *arg) 853 { 854 struct ifbrparam *param = arg; 855 856 param->ifbrp_ctime = sc->sc_brttimeout; 857 858 return (0); 859 } 860 861 int 862 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg) 863 { 864 struct ifbareq *req = arg; 865 866 return (bridge_rtdaddr(sc, req->ifba_dst)); 867 } 868 869 int 870 bridge_ioctl_flush(struct bridge_softc *sc, void *arg) 871 { 872 struct ifbreq *req = arg; 873 874 bridge_rtflush(sc, req->ifbr_ifsflags); 875 876 return (0); 877 } 878 879 int 880 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) 881 { 882 struct ifbrparam *param = arg; 883 884 param->ifbrp_prio = sc->sc_bridge_priority; 885 886 return (0); 887 } 888 889 int 890 bridge_ioctl_spri(struct bridge_softc *sc, void *arg) 891 { 892 struct ifbrparam *param = arg; 893 894 sc->sc_bridge_priority = param->ifbrp_prio; 895 896 if (sc->sc_if.if_flags & IFF_RUNNING) 897 bstp_initialization(sc); 898 899 return (0); 900 } 901 902 int 903 bridge_ioctl_ght(struct bridge_softc *sc, void *arg) 904 { 905 struct ifbrparam *param = arg; 906 907 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8; 908 909 return (0); 910 } 911 912 int 913 bridge_ioctl_sht(struct bridge_softc *sc, void *arg) 914 { 915 struct ifbrparam *param = arg; 916 917 if (param->ifbrp_hellotime == 0) 918 return (EINVAL); 919 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8; 920 921 if (sc->sc_if.if_flags & IFF_RUNNING) 922 bstp_initialization(sc); 923 924 return (0); 925 } 926 927 int 928 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) 929 { 930 struct ifbrparam *param = arg; 931 932 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8; 933 934 return (0); 935 } 936 937 int 938 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) 939 { 940 struct ifbrparam *param = arg; 941 942 if (param->ifbrp_fwddelay == 0) 943 return (EINVAL); 944 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8; 945 946 if (sc->sc_if.if_flags & IFF_RUNNING) 947 bstp_initialization(sc); 948 949 return (0); 950 } 951 952 int 953 bridge_ioctl_gma(struct bridge_softc *sc, void *arg) 954 { 955 struct ifbrparam *param = arg; 956 957 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8; 958 959 return (0); 960 } 961 962 int 963 bridge_ioctl_sma(struct bridge_softc *sc, void *arg) 964 { 965 struct ifbrparam *param = arg; 966 967 if (param->ifbrp_maxage == 0) 968 return (EINVAL); 969 sc->sc_bridge_max_age = param->ifbrp_maxage << 8; 970 971 if (sc->sc_if.if_flags & IFF_RUNNING) 972 bstp_initialization(sc); 973 974 return (0); 975 } 976 977 int 978 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) 979 { 980 struct ifbreq *req = arg; 981 struct bridge_iflist *bif; 982 983 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 984 if (bif == NULL) 985 return (ENOENT); 986 987 bif->bif_priority = req->ifbr_priority; 988 989 if (sc->sc_if.if_flags & IFF_RUNNING) 990 bstp_initialization(sc); 991 992 return (0); 993 } 994 995 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS) 996 int 997 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg) 998 { 999 struct ifbrparam *param = arg; 1000 1001 param->ifbrp_filter = sc->sc_filter_flags; 1002 1003 return (0); 1004 } 1005 1006 int 1007 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg) 1008 { 1009 struct ifbrparam *param = arg; 1010 uint32_t nflags, oflags; 1011 1012 if (param->ifbrp_filter & ~IFBF_FILT_MASK) 1013 return (EINVAL); 1014 1015 nflags = param->ifbrp_filter; 1016 oflags = sc->sc_filter_flags; 1017 1018 if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) { 1019 pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT, 1020 &sc->sc_if.if_pfil); 1021 } 1022 if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) { 1023 pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT, 1024 &sc->sc_if.if_pfil); 1025 } 1026 1027 sc->sc_filter_flags = nflags; 1028 1029 return (0); 1030 } 1031 #endif /* BRIDGE_IPF && PFIL_HOOKS */ 1032 1033 int 1034 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) 1035 { 1036 struct ifbreq *req = arg; 1037 struct bridge_iflist *bif; 1038 1039 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1040 if (bif == NULL) 1041 return (ENOENT); 1042 1043 bif->bif_path_cost = req->ifbr_path_cost; 1044 1045 if (sc->sc_if.if_flags & IFF_RUNNING) 1046 bstp_initialization(sc); 1047 1048 return (0); 1049 } 1050 1051 /* 1052 * bridge_ifdetach: 1053 * 1054 * Detach an interface from a bridge. Called when a member 1055 * interface is detaching. 1056 */ 1057 void 1058 bridge_ifdetach(struct ifnet *ifp) 1059 { 1060 struct bridge_softc *sc = ifp->if_bridge; 1061 struct ifbreq breq; 1062 1063 memset(&breq, 0, sizeof(breq)); 1064 snprintf(breq.ifbr_ifsname, sizeof(breq.ifbr_ifsname), ifp->if_xname); 1065 1066 (void) bridge_ioctl_del(sc, &breq); 1067 } 1068 1069 /* 1070 * bridge_init: 1071 * 1072 * Initialize a bridge interface. 1073 */ 1074 int 1075 bridge_init(struct ifnet *ifp) 1076 { 1077 struct bridge_softc *sc = ifp->if_softc; 1078 1079 if (ifp->if_flags & IFF_RUNNING) 1080 return (0); 1081 1082 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz, 1083 bridge_timer, sc); 1084 1085 ifp->if_flags |= IFF_RUNNING; 1086 bstp_initialization(sc); 1087 return (0); 1088 } 1089 1090 /* 1091 * bridge_stop: 1092 * 1093 * Stop the bridge interface. 1094 */ 1095 void 1096 bridge_stop(struct ifnet *ifp, int disable) 1097 { 1098 struct bridge_softc *sc = ifp->if_softc; 1099 1100 if ((ifp->if_flags & IFF_RUNNING) == 0) 1101 return; 1102 1103 callout_stop(&sc->sc_brcallout); 1104 bstp_stop(sc); 1105 1106 IF_PURGE(&ifp->if_snd); 1107 1108 bridge_rtflush(sc, IFBF_FLUSHDYN); 1109 1110 ifp->if_flags &= ~IFF_RUNNING; 1111 } 1112 1113 /* 1114 * bridge_enqueue: 1115 * 1116 * Enqueue a packet on a bridge member interface. 1117 * 1118 * NOTE: must be called at splnet(). 1119 */ 1120 __inline void 1121 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m, 1122 int runfilt) 1123 { 1124 ALTQ_DECL(struct altq_pktattr pktattr;) 1125 int len, error; 1126 short mflags; 1127 1128 /* 1129 * Clear any in-bound checksum flags for this packet. 1130 */ 1131 m->m_pkthdr.csum_flags = 0; 1132 1133 #ifdef PFIL_HOOKS 1134 if (runfilt) { 1135 if (pfil_run_hooks(&sc->sc_if.if_pfil, &m, 1136 dst_ifp, PFIL_OUT) != 0) { 1137 if (m != NULL) 1138 m_freem(m); 1139 return; 1140 } 1141 if (m == NULL) 1142 return; 1143 } 1144 #endif /* PFIL_HOOKS */ 1145 1146 #ifdef ALTQ 1147 /* 1148 * If ALTQ is enabled on the member interface, do 1149 * classification; the queueing discipline might 1150 * not require classification, but might require 1151 * the address family/header pointer in the pktattr. 1152 */ 1153 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) { 1154 /* XXX IFT_ETHER */ 1155 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr); 1156 } 1157 #endif /* ALTQ */ 1158 1159 len = m->m_pkthdr.len; 1160 mflags = m->m_flags; 1161 IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error); 1162 if (error) { 1163 /* mbuf is already freed */ 1164 sc->sc_if.if_oerrors++; 1165 return; 1166 } 1167 1168 sc->sc_if.if_opackets++; 1169 sc->sc_if.if_obytes += len; 1170 1171 dst_ifp->if_obytes += len; 1172 1173 if (mflags & M_MCAST) { 1174 sc->sc_if.if_omcasts++; 1175 dst_ifp->if_omcasts++; 1176 } 1177 1178 if ((dst_ifp->if_flags & IFF_OACTIVE) == 0) 1179 (*dst_ifp->if_start)(dst_ifp); 1180 } 1181 1182 /* 1183 * bridge_output: 1184 * 1185 * Send output from a bridge member interface. This 1186 * performs the bridging function for locally originated 1187 * packets. 1188 * 1189 * The mbuf has the Ethernet header already attached. We must 1190 * enqueue or free the mbuf before returning. 1191 */ 1192 int 1193 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa, 1194 struct rtentry *rt) 1195 { 1196 struct ether_header *eh; 1197 struct ifnet *dst_if; 1198 struct bridge_softc *sc; 1199 int s; 1200 1201 if (m->m_len < ETHER_HDR_LEN) { 1202 m = m_pullup(m, ETHER_HDR_LEN); 1203 if (m == NULL) 1204 return (0); 1205 } 1206 1207 eh = mtod(m, struct ether_header *); 1208 sc = ifp->if_bridge; 1209 1210 s = splnet(); 1211 1212 /* 1213 * If bridge is down, but the original output interface is up, 1214 * go ahead and send out that interface. Otherwise, the packet 1215 * is dropped below. 1216 */ 1217 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) { 1218 dst_if = ifp; 1219 goto sendunicast; 1220 } 1221 1222 /* 1223 * If the packet is a multicast, or we don't know a better way to 1224 * get there, send to all interfaces. 1225 */ 1226 if (ETHER_IS_MULTICAST(eh->ether_dhost)) 1227 dst_if = NULL; 1228 else 1229 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1230 if (dst_if == NULL) { 1231 struct bridge_iflist *bif; 1232 struct mbuf *mc; 1233 int used = 0; 1234 1235 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1236 dst_if = bif->bif_ifp; 1237 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1238 continue; 1239 1240 /* 1241 * If this is not the original output interface, 1242 * and the interface is participating in spanning 1243 * tree, make sure the port is in a state that 1244 * allows forwarding. 1245 */ 1246 if (dst_if != ifp && 1247 (bif->bif_flags & IFBIF_STP) != 0) { 1248 switch (bif->bif_state) { 1249 case BSTP_IFSTATE_BLOCKING: 1250 case BSTP_IFSTATE_LISTENING: 1251 case BSTP_IFSTATE_DISABLED: 1252 continue; 1253 } 1254 } 1255 1256 if (LIST_NEXT(bif, bif_next) == NULL) { 1257 used = 1; 1258 mc = m; 1259 } else { 1260 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT); 1261 if (mc == NULL) { 1262 sc->sc_if.if_oerrors++; 1263 continue; 1264 } 1265 } 1266 1267 bridge_enqueue(sc, dst_if, mc, 0); 1268 } 1269 if (used == 0) 1270 m_freem(m); 1271 splx(s); 1272 return (0); 1273 } 1274 1275 sendunicast: 1276 /* 1277 * XXX Spanning tree consideration here? 1278 */ 1279 1280 if ((dst_if->if_flags & IFF_RUNNING) == 0) { 1281 m_freem(m); 1282 splx(s); 1283 return (0); 1284 } 1285 1286 bridge_enqueue(sc, dst_if, m, 0); 1287 1288 splx(s); 1289 return (0); 1290 } 1291 1292 /* 1293 * bridge_start: 1294 * 1295 * Start output on a bridge. 1296 * 1297 * NOTE: This routine should never be called in this implementation. 1298 */ 1299 void 1300 bridge_start(struct ifnet *ifp) 1301 { 1302 1303 printf("%s: bridge_start() called\n", ifp->if_xname); 1304 } 1305 1306 /* 1307 * bridge_forward: 1308 * 1309 * The forwarding function of the bridge. 1310 */ 1311 void 1312 bridge_forward(struct bridge_softc *sc, struct mbuf *m) 1313 { 1314 struct bridge_iflist *bif; 1315 struct ifnet *src_if, *dst_if; 1316 struct ether_header *eh; 1317 1318 src_if = m->m_pkthdr.rcvif; 1319 1320 sc->sc_if.if_ipackets++; 1321 sc->sc_if.if_ibytes += m->m_pkthdr.len; 1322 1323 /* 1324 * Look up the bridge_iflist. 1325 */ 1326 bif = bridge_lookup_member_if(sc, src_if); 1327 if (bif == NULL) { 1328 /* Interface is not a bridge member (anymore?) */ 1329 m_freem(m); 1330 return; 1331 } 1332 1333 if (bif->bif_flags & IFBIF_STP) { 1334 switch (bif->bif_state) { 1335 case BSTP_IFSTATE_BLOCKING: 1336 case BSTP_IFSTATE_LISTENING: 1337 case BSTP_IFSTATE_DISABLED: 1338 m_freem(m); 1339 return; 1340 } 1341 } 1342 1343 eh = mtod(m, struct ether_header *); 1344 1345 /* 1346 * If the interface is learning, and the source 1347 * address is valid and not multicast, record 1348 * the address. 1349 */ 1350 if ((bif->bif_flags & IFBIF_LEARNING) != 0 && 1351 ETHER_IS_MULTICAST(eh->ether_shost) == 0 && 1352 (eh->ether_shost[0] == 0 && 1353 eh->ether_shost[1] == 0 && 1354 eh->ether_shost[2] == 0 && 1355 eh->ether_shost[3] == 0 && 1356 eh->ether_shost[4] == 0 && 1357 eh->ether_shost[5] == 0) == 0) { 1358 (void) bridge_rtupdate(sc, eh->ether_shost, 1359 src_if, 0, IFBAF_DYNAMIC); 1360 } 1361 1362 if ((bif->bif_flags & IFBIF_STP) != 0 && 1363 bif->bif_state == BSTP_IFSTATE_LEARNING) { 1364 m_freem(m); 1365 return; 1366 } 1367 1368 /* 1369 * At this point, the port either doesn't participate 1370 * in spanning tree or it is in the forwarding state. 1371 */ 1372 1373 /* 1374 * If the packet is unicast, destined for someone on 1375 * "this" side of the bridge, drop it. 1376 */ 1377 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) { 1378 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1379 if (src_if == dst_if) { 1380 m_freem(m); 1381 return; 1382 } 1383 } else { 1384 /* ...forward it to all interfaces. */ 1385 sc->sc_if.if_imcasts++; 1386 dst_if = NULL; 1387 } 1388 1389 #ifdef PFIL_HOOKS 1390 if (pfil_run_hooks(&sc->sc_if.if_pfil, &m, 1391 m->m_pkthdr.rcvif, PFIL_IN) != 0) { 1392 if (m != NULL) 1393 m_freem(m); 1394 return; 1395 } 1396 if (m == NULL) 1397 return; 1398 #endif /* PFIL_HOOKS */ 1399 1400 if (dst_if == NULL) { 1401 bridge_broadcast(sc, src_if, m); 1402 return; 1403 } 1404 1405 /* 1406 * At this point, we're dealing with a unicast frame 1407 * going to a different interface. 1408 */ 1409 if ((dst_if->if_flags & IFF_RUNNING) == 0) { 1410 m_freem(m); 1411 return; 1412 } 1413 bif = bridge_lookup_member_if(sc, dst_if); 1414 if (bif == NULL) { 1415 /* Not a member of the bridge (anymore?) */ 1416 m_freem(m); 1417 return; 1418 } 1419 1420 if (bif->bif_flags & IFBIF_STP) { 1421 switch (bif->bif_state) { 1422 case BSTP_IFSTATE_DISABLED: 1423 case BSTP_IFSTATE_BLOCKING: 1424 m_freem(m); 1425 return; 1426 } 1427 } 1428 1429 bridge_enqueue(sc, dst_if, m, 1); 1430 } 1431 1432 /* 1433 * bridge_input: 1434 * 1435 * Receive input from a member interface. Queue the packet for 1436 * bridging if it is not for us. 1437 */ 1438 struct mbuf * 1439 bridge_input(struct ifnet *ifp, struct mbuf *m) 1440 { 1441 struct bridge_softc *sc = ifp->if_bridge; 1442 struct bridge_iflist *bif; 1443 struct ether_header *eh; 1444 struct mbuf *mc; 1445 1446 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) 1447 return (m); 1448 1449 bif = bridge_lookup_member_if(sc, ifp); 1450 if (bif == NULL) 1451 return (m); 1452 1453 eh = mtod(m, struct ether_header *); 1454 1455 if (m->m_flags & (M_BCAST|M_MCAST)) { 1456 /* Tap off 802.1D packets; they do not get forwarded. */ 1457 if (memcmp(eh->ether_dhost, bstp_etheraddr, 1458 ETHER_ADDR_LEN) == 0) { 1459 m = bstp_input(ifp, m); 1460 if (m == NULL) 1461 return (NULL); 1462 } 1463 1464 if (bif->bif_flags & IFBIF_STP) { 1465 switch (bif->bif_state) { 1466 case BSTP_IFSTATE_BLOCKING: 1467 case BSTP_IFSTATE_LISTENING: 1468 case BSTP_IFSTATE_DISABLED: 1469 return (m); 1470 } 1471 } 1472 1473 /* 1474 * Make a deep copy of the packet and enqueue the copy 1475 * for bridge processing; return the original packet for 1476 * local processing. 1477 */ 1478 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT); 1479 if (mc == NULL) 1480 return (m); 1481 1482 /* Perform the bridge forwarding function with the copy. */ 1483 bridge_forward(sc, mc); 1484 1485 /* Return the original packet for local processing. */ 1486 return (m); 1487 } 1488 1489 if (bif->bif_flags & IFBIF_STP) { 1490 switch (bif->bif_state) { 1491 case BSTP_IFSTATE_BLOCKING: 1492 case BSTP_IFSTATE_LISTENING: 1493 case BSTP_IFSTATE_DISABLED: 1494 return (m); 1495 } 1496 } 1497 1498 /* 1499 * Unicast. Make sure it's not for us. 1500 */ 1501 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1502 /* It is destined for us. */ 1503 if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_dhost, 1504 ETHER_ADDR_LEN) == 0) { 1505 if (bif->bif_flags & IFBIF_LEARNING) 1506 (void) bridge_rtupdate(sc, 1507 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC); 1508 m->m_pkthdr.rcvif = bif->bif_ifp; 1509 return (m); 1510 } 1511 1512 /* We just received a packet that we sent out. */ 1513 if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_shost, 1514 ETHER_ADDR_LEN) == 0) { 1515 m_freem(m); 1516 return (NULL); 1517 } 1518 } 1519 1520 /* Perform the bridge forwarding function. */ 1521 bridge_forward(sc, m); 1522 1523 return (NULL); 1524 } 1525 1526 /* 1527 * bridge_broadcast: 1528 * 1529 * Send a frame to all interfaces that are members of 1530 * the bridge, except for the one on which the packet 1531 * arrived. 1532 */ 1533 void 1534 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if, 1535 struct mbuf *m) 1536 { 1537 struct bridge_iflist *bif; 1538 struct mbuf *mc; 1539 struct ifnet *dst_if; 1540 int used = 0; 1541 1542 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1543 dst_if = bif->bif_ifp; 1544 if (dst_if == src_if) 1545 continue; 1546 1547 if (bif->bif_flags & IFBIF_STP) { 1548 switch (bif->bif_state) { 1549 case BSTP_IFSTATE_BLOCKING: 1550 case BSTP_IFSTATE_DISABLED: 1551 continue; 1552 } 1553 } 1554 1555 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && 1556 (m->m_flags & (M_BCAST|M_MCAST)) == 0) 1557 continue; 1558 1559 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1560 continue; 1561 1562 if (LIST_NEXT(bif, bif_next) == NULL) { 1563 mc = m; 1564 used = 1; 1565 } else { 1566 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT); 1567 if (mc == NULL) { 1568 sc->sc_if.if_oerrors++; 1569 continue; 1570 } 1571 } 1572 1573 bridge_enqueue(sc, dst_if, mc, 1); 1574 } 1575 if (used == 0) 1576 m_freem(m); 1577 } 1578 1579 /* 1580 * bridge_rtupdate: 1581 * 1582 * Add a bridge routing entry. 1583 */ 1584 int 1585 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, 1586 struct ifnet *dst_if, int setflags, uint8_t flags) 1587 { 1588 struct bridge_rtnode *brt; 1589 int error; 1590 1591 /* 1592 * A route for this destination might already exist. If so, 1593 * update it, otherwise create a new one. 1594 */ 1595 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) { 1596 if (sc->sc_brtcnt >= sc->sc_brtmax) 1597 return (ENOSPC); 1598 1599 /* 1600 * Allocate a new bridge forwarding node, and 1601 * initialize the expiration time and Ethernet 1602 * address. 1603 */ 1604 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT); 1605 if (brt == NULL) 1606 return (ENOMEM); 1607 1608 memset(brt, 0, sizeof(*brt)); 1609 brt->brt_expire = mono_time.tv_sec + sc->sc_brttimeout; 1610 brt->brt_flags = IFBAF_DYNAMIC; 1611 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); 1612 1613 if ((error = bridge_rtnode_insert(sc, brt)) != 0) { 1614 pool_put(&bridge_rtnode_pool, brt); 1615 return (error); 1616 } 1617 } 1618 1619 brt->brt_ifp = dst_if; 1620 if (setflags) { 1621 brt->brt_flags = flags; 1622 brt->brt_expire = (flags & IFBAF_STATIC) ? 0 : 1623 mono_time.tv_sec + sc->sc_brttimeout; 1624 } 1625 1626 return (0); 1627 } 1628 1629 /* 1630 * bridge_rtlookup: 1631 * 1632 * Lookup the destination interface for an address. 1633 */ 1634 struct ifnet * 1635 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr) 1636 { 1637 struct bridge_rtnode *brt; 1638 1639 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) 1640 return (NULL); 1641 1642 return (brt->brt_ifp); 1643 } 1644 1645 /* 1646 * bridge_rttrim: 1647 * 1648 * Trim the routine table so that we have a number 1649 * of routing entries less than or equal to the 1650 * maximum number. 1651 */ 1652 void 1653 bridge_rttrim(struct bridge_softc *sc) 1654 { 1655 struct bridge_rtnode *brt, *nbrt; 1656 1657 /* Make sure we actually need to do this. */ 1658 if (sc->sc_brtcnt <= sc->sc_brtmax) 1659 return; 1660 1661 /* Force an aging cycle; this might trim enough addresses. */ 1662 bridge_rtage(sc); 1663 if (sc->sc_brtcnt <= sc->sc_brtmax) 1664 return; 1665 1666 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) { 1667 nbrt = LIST_NEXT(brt, brt_list); 1668 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 1669 bridge_rtnode_destroy(sc, brt); 1670 if (sc->sc_brtcnt <= sc->sc_brtmax) 1671 return; 1672 } 1673 } 1674 } 1675 1676 /* 1677 * bridge_timer: 1678 * 1679 * Aging timer for the bridge. 1680 */ 1681 void 1682 bridge_timer(void *arg) 1683 { 1684 struct bridge_softc *sc = arg; 1685 int s; 1686 1687 s = splnet(); 1688 bridge_rtage(sc); 1689 splx(s); 1690 1691 if (sc->sc_if.if_flags & IFF_RUNNING) 1692 callout_reset(&sc->sc_brcallout, 1693 bridge_rtable_prune_period * hz, bridge_timer, sc); 1694 } 1695 1696 /* 1697 * bridge_rtage: 1698 * 1699 * Perform an aging cycle. 1700 */ 1701 void 1702 bridge_rtage(struct bridge_softc *sc) 1703 { 1704 struct bridge_rtnode *brt, *nbrt; 1705 1706 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) { 1707 nbrt = LIST_NEXT(brt, brt_list); 1708 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 1709 if (mono_time.tv_sec >= brt->brt_expire) 1710 bridge_rtnode_destroy(sc, brt); 1711 } 1712 } 1713 } 1714 1715 /* 1716 * bridge_rtflush: 1717 * 1718 * Remove all dynamic addresses from the bridge. 1719 */ 1720 void 1721 bridge_rtflush(struct bridge_softc *sc, int full) 1722 { 1723 struct bridge_rtnode *brt, *nbrt; 1724 1725 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) { 1726 nbrt = LIST_NEXT(brt, brt_list); 1727 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 1728 bridge_rtnode_destroy(sc, brt); 1729 } 1730 } 1731 1732 /* 1733 * bridge_rtdaddr: 1734 * 1735 * Remove an address from the table. 1736 */ 1737 int 1738 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr) 1739 { 1740 struct bridge_rtnode *brt; 1741 1742 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) 1743 return (ENOENT); 1744 1745 bridge_rtnode_destroy(sc, brt); 1746 return (0); 1747 } 1748 1749 /* 1750 * bridge_rtdelete: 1751 * 1752 * Delete routes to a speicifc member interface. 1753 */ 1754 void 1755 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp) 1756 { 1757 struct bridge_rtnode *brt, *nbrt; 1758 1759 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) { 1760 nbrt = LIST_NEXT(brt, brt_list); 1761 if (brt->brt_ifp == ifp) 1762 bridge_rtnode_destroy(sc, brt); 1763 } 1764 } 1765 1766 /* 1767 * bridge_rtable_init: 1768 * 1769 * Initialize the route table for this bridge. 1770 */ 1771 int 1772 bridge_rtable_init(struct bridge_softc *sc) 1773 { 1774 int i; 1775 1776 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE, 1777 M_DEVBUF, M_NOWAIT); 1778 if (sc->sc_rthash == NULL) 1779 return (ENOMEM); 1780 1781 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++) 1782 LIST_INIT(&sc->sc_rthash[i]); 1783 1784 sc->sc_rthash_key = arc4random(); 1785 1786 LIST_INIT(&sc->sc_rtlist); 1787 1788 return (0); 1789 } 1790 1791 /* 1792 * bridge_rtable_fini: 1793 * 1794 * Deconstruct the route table for this bridge. 1795 */ 1796 void 1797 bridge_rtable_fini(struct bridge_softc *sc) 1798 { 1799 1800 free(sc->sc_rthash, M_DEVBUF); 1801 } 1802 1803 /* 1804 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 1805 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 1806 */ 1807 #define mix(a, b, c) \ 1808 do { \ 1809 a -= b; a -= c; a ^= (c >> 13); \ 1810 b -= c; b -= a; b ^= (a << 8); \ 1811 c -= a; c -= b; c ^= (b >> 13); \ 1812 a -= b; a -= c; a ^= (c >> 12); \ 1813 b -= c; b -= a; b ^= (a << 16); \ 1814 c -= a; c -= b; c ^= (b >> 5); \ 1815 a -= b; a -= c; a ^= (c >> 3); \ 1816 b -= c; b -= a; b ^= (a << 10); \ 1817 c -= a; c -= b; c ^= (b >> 15); \ 1818 } while (/*CONSTCOND*/0) 1819 1820 static __inline uint32_t 1821 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) 1822 { 1823 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; 1824 1825 b += addr[5] << 8; 1826 b += addr[4]; 1827 a += addr[3] << 24; 1828 a += addr[2] << 16; 1829 a += addr[1] << 8; 1830 a += addr[0]; 1831 1832 mix(a, b, c); 1833 1834 return (c & BRIDGE_RTHASH_MASK); 1835 } 1836 1837 #undef mix 1838 1839 /* 1840 * bridge_rtnode_lookup: 1841 * 1842 * Look up a bridge route node for the specified destination. 1843 */ 1844 struct bridge_rtnode * 1845 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr) 1846 { 1847 struct bridge_rtnode *brt; 1848 uint32_t hash; 1849 int dir; 1850 1851 hash = bridge_rthash(sc, addr); 1852 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) { 1853 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN); 1854 if (dir == 0) 1855 return (brt); 1856 if (dir > 0) 1857 return (NULL); 1858 } 1859 1860 return (NULL); 1861 } 1862 1863 /* 1864 * bridge_rtnode_insert: 1865 * 1866 * Insert the specified bridge node into the route table. We 1867 * assume the entry is not already in the table. 1868 */ 1869 int 1870 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) 1871 { 1872 struct bridge_rtnode *lbrt; 1873 uint32_t hash; 1874 int dir; 1875 1876 hash = bridge_rthash(sc, brt->brt_addr); 1877 1878 lbrt = LIST_FIRST(&sc->sc_rthash[hash]); 1879 if (lbrt == NULL) { 1880 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash); 1881 goto out; 1882 } 1883 1884 do { 1885 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN); 1886 if (dir == 0) 1887 return (EEXIST); 1888 if (dir > 0) { 1889 LIST_INSERT_BEFORE(lbrt, brt, brt_hash); 1890 goto out; 1891 } 1892 if (LIST_NEXT(lbrt, brt_hash) == NULL) { 1893 LIST_INSERT_AFTER(lbrt, brt, brt_hash); 1894 goto out; 1895 } 1896 lbrt = LIST_NEXT(lbrt, brt_hash); 1897 } while (lbrt != NULL); 1898 1899 #ifdef DIAGNOSTIC 1900 panic("bridge_rtnode_insert: impossible"); 1901 #endif 1902 1903 out: 1904 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list); 1905 sc->sc_brtcnt++; 1906 1907 return (0); 1908 } 1909 1910 /* 1911 * bridge_rtnode_destroy: 1912 * 1913 * Destroy a bridge rtnode. 1914 */ 1915 void 1916 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt) 1917 { 1918 1919 LIST_REMOVE(brt, brt_hash); 1920 1921 LIST_REMOVE(brt, brt_list); 1922 sc->sc_brtcnt--; 1923 pool_put(&bridge_rtnode_pool, brt); 1924 } 1925 1926 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS) 1927 extern struct pfil_head inet_pfil_hook; /* XXX */ 1928 extern struct pfil_head inet6_pfil_hook; /* XXX */ 1929 1930 /* 1931 * Send bridge packets through IPF if they are one of the types IPF can deal 1932 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without 1933 * question.) 1934 */ 1935 static int bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir) 1936 { 1937 int snap, error; 1938 struct ether_header *eh1, eh2; 1939 struct llc llc; 1940 u_int16_t ether_type; 1941 1942 snap = 0; 1943 error = -1; /* Default error if not error == 0 */ 1944 eh1 = mtod(*mp, struct ether_header *); 1945 ether_type = ntohs(eh1->ether_type); 1946 1947 /* 1948 * Check for SNAP/LLC. 1949 */ 1950 if (ether_type < ETHERMTU) { 1951 struct llc *llc = (struct llc *)(eh1 + 1); 1952 1953 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && 1954 llc->llc_dsap == LLC_SNAP_LSAP && 1955 llc->llc_ssap == LLC_SNAP_LSAP && 1956 llc->llc_control == LLC_UI) { 1957 ether_type = htons(llc->llc_un.type_snap.ether_type); 1958 snap = 1; 1959 } 1960 } 1961 1962 /* 1963 * If we're trying to filter bridge traffic, don't look at anything 1964 * other than IP and ARP traffic. If the filter doesn't understand 1965 * IPv6, don't allow IPv6 through the bridge either. This is lame 1966 * since if we really wanted, say, an AppleTalk filter, we are hosed, 1967 * but of course we don't have an AppleTalk filter to begin with. 1968 * (Note that since IPF doesn't understand ARP it will pass *ALL* 1969 * ARP traffic.) 1970 */ 1971 switch (ether_type) { 1972 case ETHERTYPE_ARP: 1973 case ETHERTYPE_REVARP: 1974 return 0; /* Automatically pass */ 1975 case ETHERTYPE_IP: 1976 # ifdef INET6 1977 case ETHERTYPE_IPV6: 1978 # endif /* INET6 */ 1979 break; 1980 default: 1981 goto bad; 1982 } 1983 1984 /* Strip off the Ethernet header and keep a copy. */ 1985 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2); 1986 m_adj(*mp, ETHER_HDR_LEN); 1987 1988 /* Strip off snap header, if present */ 1989 if (snap) { 1990 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc); 1991 m_adj(*mp, sizeof(struct llc)); 1992 } 1993 1994 /* 1995 * Check basic packet sanity and run IPF through pfil. 1996 */ 1997 switch (ether_type) 1998 { 1999 case ETHERTYPE_IP : 2000 error = (dir == PFIL_IN) ? bridge_ip_checkbasic(mp) : 0; 2001 if (error == 0) 2002 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir); 2003 break; 2004 # ifdef INET6 2005 case ETHERTYPE_IPV6 : 2006 error = (dir == PFIL_IN) ? bridge_ip6_checkbasic(mp) : 0; 2007 if (error == 0) 2008 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp, dir); 2009 break; 2010 # endif 2011 default : 2012 error = 0; 2013 break; 2014 } 2015 2016 if (*mp == NULL) 2017 return error; 2018 if (error != 0) 2019 goto bad; 2020 2021 error = -1; 2022 2023 /* 2024 * Finally, put everything back the way it was and return 2025 */ 2026 if (snap) { 2027 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT); 2028 if (*mp == NULL) 2029 return error; 2030 bcopy(&llc, mtod(*mp, caddr_t), sizeof(struct llc)); 2031 } 2032 2033 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT); 2034 if (*mp == NULL) 2035 return error; 2036 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN); 2037 2038 return 0; 2039 2040 bad: 2041 m_freem(*mp); 2042 *mp = NULL; 2043 return error; 2044 } 2045 2046 /* 2047 * Perform basic checks on header size since 2048 * IPF assumes ip_input has already processed 2049 * it for it. Cut-and-pasted from ip_input.c. 2050 * Given how simple the IPv6 version is, 2051 * does the IPv4 version really need to be 2052 * this complicated? 2053 * 2054 * XXX Should we update ipstat here, or not? 2055 * XXX Right now we update ipstat but not 2056 * XXX csum_counter. 2057 */ 2058 static int 2059 bridge_ip_checkbasic(struct mbuf **mp) 2060 { 2061 struct mbuf *m = *mp; 2062 struct ip *ip; 2063 int len, hlen; 2064 2065 if (*mp == NULL) 2066 return -1; 2067 2068 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 2069 if ((m = m_copyup(m, sizeof(struct ip), 2070 (max_linkhdr + 3) & ~3)) == NULL) { 2071 /* XXXJRT new stat, please */ 2072 ipstat.ips_toosmall++; 2073 goto bad; 2074 } 2075 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 2076 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 2077 ipstat.ips_toosmall++; 2078 goto bad; 2079 } 2080 } 2081 ip = mtod(m, struct ip *); 2082 if (ip == NULL) goto bad; 2083 2084 if (ip->ip_v != IPVERSION) { 2085 ipstat.ips_badvers++; 2086 goto bad; 2087 } 2088 hlen = ip->ip_hl << 2; 2089 if (hlen < sizeof(struct ip)) { /* minimum header length */ 2090 ipstat.ips_badhlen++; 2091 goto bad; 2092 } 2093 if (hlen > m->m_len) { 2094 if ((m = m_pullup(m, hlen)) == 0) { 2095 ipstat.ips_badhlen++; 2096 goto bad; 2097 } 2098 ip = mtod(m, struct ip *); 2099 if (ip == NULL) goto bad; 2100 } 2101 2102 switch (m->m_pkthdr.csum_flags & 2103 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) | 2104 M_CSUM_IPv4_BAD)) { 2105 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 2106 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */ 2107 goto bad; 2108 2109 case M_CSUM_IPv4: 2110 /* Checksum was okay. */ 2111 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */ 2112 break; 2113 2114 default: 2115 /* Must compute it ourselves. */ 2116 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */ 2117 if (in_cksum(m, hlen) != 0) 2118 goto bad; 2119 break; 2120 } 2121 2122 /* Retrieve the packet length. */ 2123 len = ntohs(ip->ip_len); 2124 2125 /* 2126 * Check for additional length bogosity 2127 */ 2128 if (len < hlen) { 2129 ipstat.ips_badlen++; 2130 goto bad; 2131 } 2132 2133 /* 2134 * Check that the amount of data in the buffers 2135 * is as at least much as the IP header would have us expect. 2136 * Drop packet if shorter than we expect. 2137 */ 2138 if (m->m_pkthdr.len < len) { 2139 ipstat.ips_tooshort++; 2140 goto bad; 2141 } 2142 2143 /* Checks out, proceed */ 2144 *mp = m; 2145 return 0; 2146 2147 bad: 2148 *mp = m; 2149 return -1; 2150 } 2151 2152 # ifdef INET6 2153 /* 2154 * Same as above, but for IPv6. 2155 * Cut-and-pasted from ip6_input.c. 2156 * XXX Should we update ip6stat, or not? 2157 */ 2158 static int 2159 bridge_ip6_checkbasic(struct mbuf **mp) 2160 { 2161 struct mbuf *m = *mp; 2162 struct ip6_hdr *ip6; 2163 2164 /* 2165 * If the IPv6 header is not aligned, slurp it up into a new 2166 * mbuf with space for link headers, in the event we forward 2167 * it. Otherwise, if it is aligned, make sure the entire base 2168 * IPv6 header is in the first mbuf of the chain. 2169 */ 2170 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 2171 struct ifnet *inifp = m->m_pkthdr.rcvif; 2172 if ((m = m_copyup(m, sizeof(struct ip6_hdr), 2173 (max_linkhdr + 3) & ~3)) == NULL) { 2174 /* XXXJRT new stat, please */ 2175 ip6stat.ip6s_toosmall++; 2176 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 2177 goto bad; 2178 } 2179 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { 2180 struct ifnet *inifp = m->m_pkthdr.rcvif; 2181 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 2182 ip6stat.ip6s_toosmall++; 2183 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 2184 goto bad; 2185 } 2186 } 2187 2188 ip6 = mtod(m, struct ip6_hdr *); 2189 2190 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 2191 ip6stat.ip6s_badvers++; 2192 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 2193 goto bad; 2194 } 2195 2196 /* Checks out, proceed */ 2197 *mp = m; 2198 return 0; 2199 2200 bad: 2201 *mp = m; 2202 return -1; 2203 } 2204 # endif /* INET6 */ 2205 #endif /* BRIDGE_IPF && PFIL_HOOKS */ 2206