if_bridge.c revision 1.160
1/* $NetBSD: if_bridge.c,v 1.160 2018/11/09 06:44:31 ozaki-r 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.160 2018/11/09 06:44:31 ozaki-r Exp $"); 84 85#ifdef _KERNEL_OPT 86#include "opt_bridge_ipf.h" 87#include "opt_inet.h" 88#include "opt_net_mpsafe.h" 89#endif /* _KERNEL_OPT */ 90 91#include <sys/param.h> 92#include <sys/kernel.h> 93#include <sys/mbuf.h> 94#include <sys/queue.h> 95#include <sys/socket.h> 96#include <sys/socketvar.h> /* for softnet_lock */ 97#include <sys/sockio.h> 98#include <sys/systm.h> 99#include <sys/proc.h> 100#include <sys/pool.h> 101#include <sys/kauth.h> 102#include <sys/cpu.h> 103#include <sys/cprng.h> 104#include <sys/mutex.h> 105#include <sys/kmem.h> 106 107#include <net/bpf.h> 108#include <net/if.h> 109#include <net/if_dl.h> 110#include <net/if_types.h> 111#include <net/if_llc.h> 112 113#include <net/if_ether.h> 114#include <net/if_bridgevar.h> 115 116#if defined(BRIDGE_IPF) 117/* Used for bridge_ip[6]_checkbasic */ 118#include <netinet/in.h> 119#include <netinet/in_systm.h> 120#include <netinet/ip.h> 121#include <netinet/ip_var.h> 122#include <netinet/ip_private.h> /* XXX */ 123 124#include <netinet/ip6.h> 125#include <netinet6/in6_var.h> 126#include <netinet6/ip6_var.h> 127#include <netinet6/ip6_private.h> /* XXX */ 128#endif /* BRIDGE_IPF */ 129 130/* 131 * Size of the route hash table. Must be a power of two. 132 */ 133#ifndef BRIDGE_RTHASH_SIZE 134#define BRIDGE_RTHASH_SIZE 1024 135#endif 136 137#define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1) 138 139#include "carp.h" 140#if NCARP > 0 141#include <netinet/in.h> 142#include <netinet/in_var.h> 143#include <netinet/ip_carp.h> 144#endif 145 146#include "ioconf.h" 147 148__CTASSERT(sizeof(struct ifbifconf) == sizeof(struct ifbaconf)); 149__CTASSERT(offsetof(struct ifbifconf, ifbic_len) == offsetof(struct ifbaconf, ifbac_len)); 150__CTASSERT(offsetof(struct ifbifconf, ifbic_buf) == offsetof(struct ifbaconf, ifbac_buf)); 151 152/* 153 * Maximum number of addresses to cache. 154 */ 155#ifndef BRIDGE_RTABLE_MAX 156#define BRIDGE_RTABLE_MAX 100 157#endif 158 159/* 160 * Spanning tree defaults. 161 */ 162#define BSTP_DEFAULT_MAX_AGE (20 * 256) 163#define BSTP_DEFAULT_HELLO_TIME (2 * 256) 164#define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 165#define BSTP_DEFAULT_HOLD_TIME (1 * 256) 166#define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 167#define BSTP_DEFAULT_PORT_PRIORITY 0x80 168#define BSTP_DEFAULT_PATH_COST 55 169 170/* 171 * Timeout (in seconds) for entries learned dynamically. 172 */ 173#ifndef BRIDGE_RTABLE_TIMEOUT 174#define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ 175#endif 176 177/* 178 * Number of seconds between walks of the route list. 179 */ 180#ifndef BRIDGE_RTABLE_PRUNE_PERIOD 181#define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) 182#endif 183 184#define BRIDGE_RT_LOCK(_sc) mutex_enter((_sc)->sc_rtlist_lock) 185#define BRIDGE_RT_UNLOCK(_sc) mutex_exit((_sc)->sc_rtlist_lock) 186#define BRIDGE_RT_LOCKED(_sc) mutex_owned((_sc)->sc_rtlist_lock) 187 188#define BRIDGE_RT_PSZ_PERFORM(_sc) \ 189 pserialize_perform((_sc)->sc_rtlist_psz) 190 191#define BRIDGE_RT_RENTER(__s) do { __s = pserialize_read_enter(); } while (0) 192#define BRIDGE_RT_REXIT(__s) do { pserialize_read_exit(__s); } while (0) 193 194#define BRIDGE_RTLIST_READER_FOREACH(_brt, _sc) \ 195 PSLIST_READER_FOREACH((_brt), &((_sc)->sc_rtlist), \ 196 struct bridge_rtnode, brt_list) 197#define BRIDGE_RTLIST_WRITER_FOREACH(_brt, _sc) \ 198 PSLIST_WRITER_FOREACH((_brt), &((_sc)->sc_rtlist), \ 199 struct bridge_rtnode, brt_list) 200#define BRIDGE_RTLIST_WRITER_INSERT_HEAD(_sc, _brt) \ 201 PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rtlist, brt, brt_list) 202#define BRIDGE_RTLIST_WRITER_REMOVE(_brt) \ 203 PSLIST_WRITER_REMOVE((_brt), brt_list) 204 205#define BRIDGE_RTHASH_READER_FOREACH(_brt, _sc, _hash) \ 206 PSLIST_READER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)], \ 207 struct bridge_rtnode, brt_hash) 208#define BRIDGE_RTHASH_WRITER_FOREACH(_brt, _sc, _hash) \ 209 PSLIST_WRITER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)], \ 210 struct bridge_rtnode, brt_hash) 211#define BRIDGE_RTHASH_WRITER_INSERT_HEAD(_sc, _hash, _brt) \ 212 PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rthash[(_hash)], brt, brt_hash) 213#define BRIDGE_RTHASH_WRITER_INSERT_AFTER(_brt, _new) \ 214 PSLIST_WRITER_INSERT_AFTER((_brt), (_new), brt_hash) 215#define BRIDGE_RTHASH_WRITER_REMOVE(_brt) \ 216 PSLIST_WRITER_REMOVE((_brt), brt_hash) 217 218#ifdef NET_MPSAFE 219#define DECLARE_LOCK_VARIABLE 220#define ACQUIRE_GLOBAL_LOCKS() do { } while (0) 221#define RELEASE_GLOBAL_LOCKS() do { } while (0) 222#else 223#define DECLARE_LOCK_VARIABLE int __s 224#define ACQUIRE_GLOBAL_LOCKS() do { \ 225 KERNEL_LOCK(1, NULL); \ 226 mutex_enter(softnet_lock); \ 227 __s = splsoftnet(); \ 228 } while (0) 229#define RELEASE_GLOBAL_LOCKS() do { \ 230 splx(__s); \ 231 mutex_exit(softnet_lock); \ 232 KERNEL_UNLOCK_ONE(NULL); \ 233 } while (0) 234#endif 235 236struct psref_class *bridge_psref_class __read_mostly; 237 238int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; 239 240static struct pool bridge_rtnode_pool; 241 242static int bridge_clone_create(struct if_clone *, int); 243static int bridge_clone_destroy(struct ifnet *); 244 245static int bridge_ioctl(struct ifnet *, u_long, void *); 246static int bridge_init(struct ifnet *); 247static void bridge_stop(struct ifnet *, int); 248static void bridge_start(struct ifnet *); 249 250static void bridge_input(struct ifnet *, struct mbuf *); 251static void bridge_forward(struct bridge_softc *, struct mbuf *); 252 253static void bridge_timer(void *); 254 255static void bridge_broadcast(struct bridge_softc *, struct ifnet *, 256 struct mbuf *); 257 258static int bridge_rtupdate(struct bridge_softc *, const uint8_t *, 259 struct ifnet *, int, uint8_t); 260static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *); 261static void bridge_rttrim(struct bridge_softc *); 262static void bridge_rtage(struct bridge_softc *); 263static void bridge_rtage_work(struct work *, void *); 264static void bridge_rtflush(struct bridge_softc *, int); 265static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *); 266static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp); 267 268static void bridge_rtable_init(struct bridge_softc *); 269static void bridge_rtable_fini(struct bridge_softc *); 270 271static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, 272 const uint8_t *); 273static int bridge_rtnode_insert(struct bridge_softc *, 274 struct bridge_rtnode *); 275static void bridge_rtnode_remove(struct bridge_softc *, 276 struct bridge_rtnode *); 277static void bridge_rtnode_destroy(struct bridge_rtnode *); 278 279static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, 280 const char *name, 281 struct psref *); 282static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, 283 struct ifnet *ifp, 284 struct psref *); 285static void bridge_release_member(struct bridge_softc *, struct bridge_iflist *, 286 struct psref *); 287static void bridge_delete_member(struct bridge_softc *, 288 struct bridge_iflist *); 289static void bridge_acquire_member(struct bridge_softc *sc, 290 struct bridge_iflist *, 291 struct psref *); 292 293static int bridge_ioctl_add(struct bridge_softc *, void *); 294static int bridge_ioctl_del(struct bridge_softc *, void *); 295static int bridge_ioctl_gifflags(struct bridge_softc *, void *); 296static int bridge_ioctl_sifflags(struct bridge_softc *, void *); 297static int bridge_ioctl_scache(struct bridge_softc *, void *); 298static int bridge_ioctl_gcache(struct bridge_softc *, void *); 299static int bridge_ioctl_gifs(struct bridge_softc *, void *); 300static int bridge_ioctl_rts(struct bridge_softc *, void *); 301static int bridge_ioctl_saddr(struct bridge_softc *, void *); 302static int bridge_ioctl_sto(struct bridge_softc *, void *); 303static int bridge_ioctl_gto(struct bridge_softc *, void *); 304static int bridge_ioctl_daddr(struct bridge_softc *, void *); 305static int bridge_ioctl_flush(struct bridge_softc *, void *); 306static int bridge_ioctl_gpri(struct bridge_softc *, void *); 307static int bridge_ioctl_spri(struct bridge_softc *, void *); 308static int bridge_ioctl_ght(struct bridge_softc *, void *); 309static int bridge_ioctl_sht(struct bridge_softc *, void *); 310static int bridge_ioctl_gfd(struct bridge_softc *, void *); 311static int bridge_ioctl_sfd(struct bridge_softc *, void *); 312static int bridge_ioctl_gma(struct bridge_softc *, void *); 313static int bridge_ioctl_sma(struct bridge_softc *, void *); 314static int bridge_ioctl_sifprio(struct bridge_softc *, void *); 315static int bridge_ioctl_sifcost(struct bridge_softc *, void *); 316#if defined(BRIDGE_IPF) 317static int bridge_ioctl_gfilt(struct bridge_softc *, void *); 318static int bridge_ioctl_sfilt(struct bridge_softc *, void *); 319static int bridge_ipf(void *, struct mbuf **, struct ifnet *, int); 320static int bridge_ip_checkbasic(struct mbuf **mp); 321# ifdef INET6 322static int bridge_ip6_checkbasic(struct mbuf **mp); 323# endif /* INET6 */ 324#endif /* BRIDGE_IPF */ 325 326struct bridge_control { 327 int (*bc_func)(struct bridge_softc *, void *); 328 int bc_argsize; 329 int bc_flags; 330}; 331 332#define BC_F_COPYIN 0x01 /* copy arguments in */ 333#define BC_F_COPYOUT 0x02 /* copy arguments out */ 334#define BC_F_SUSER 0x04 /* do super-user check */ 335#define BC_F_XLATEIN 0x08 /* xlate arguments in */ 336#define BC_F_XLATEOUT 0x10 /* xlate arguments out */ 337 338static const struct bridge_control bridge_control_table[] = { 339[BRDGADD] = {bridge_ioctl_add, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 340[BRDGDEL] = {bridge_ioctl_del, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 341 342[BRDGGIFFLGS] = {bridge_ioctl_gifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_COPYOUT}, 343[BRDGSIFFLGS] = {bridge_ioctl_sifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 344 345[BRDGSCACHE] = {bridge_ioctl_scache, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 346[BRDGGCACHE] = {bridge_ioctl_gcache, sizeof(struct ifbrparam), BC_F_COPYOUT}, 347 348[OBRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_COPYIN|BC_F_COPYOUT}, 349[OBRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_COPYIN|BC_F_COPYOUT}, 350 351[BRDGSADDR] = {bridge_ioctl_saddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER}, 352 353[BRDGSTO] = {bridge_ioctl_sto, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 354[BRDGGTO] = {bridge_ioctl_gto, sizeof(struct ifbrparam), BC_F_COPYOUT}, 355 356[BRDGDADDR] = {bridge_ioctl_daddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER}, 357 358[BRDGFLUSH] = {bridge_ioctl_flush, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 359 360[BRDGGPRI] = {bridge_ioctl_gpri, sizeof(struct ifbrparam), BC_F_COPYOUT}, 361[BRDGSPRI] = {bridge_ioctl_spri, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 362 363[BRDGGHT] = {bridge_ioctl_ght, sizeof(struct ifbrparam), BC_F_COPYOUT}, 364[BRDGSHT] = {bridge_ioctl_sht, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 365 366[BRDGGFD] = {bridge_ioctl_gfd, sizeof(struct ifbrparam), BC_F_COPYOUT}, 367[BRDGSFD] = {bridge_ioctl_sfd, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 368 369[BRDGGMA] = {bridge_ioctl_gma, sizeof(struct ifbrparam), BC_F_COPYOUT}, 370[BRDGSMA] = {bridge_ioctl_sma, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 371 372[BRDGSIFPRIO] = {bridge_ioctl_sifprio, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 373 374[BRDGSIFCOST] = {bridge_ioctl_sifcost, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER}, 375#if defined(BRIDGE_IPF) 376[BRDGGFILT] = {bridge_ioctl_gfilt, sizeof(struct ifbrparam), BC_F_COPYOUT}, 377[BRDGSFILT] = {bridge_ioctl_sfilt, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER}, 378#endif /* BRIDGE_IPF */ 379[BRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_XLATEIN|BC_F_XLATEOUT}, 380[BRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_XLATEIN|BC_F_XLATEOUT}, 381}; 382 383static const int bridge_control_table_size = __arraycount(bridge_control_table); 384 385static struct if_clone bridge_cloner = 386 IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy); 387 388/* 389 * bridgeattach: 390 * 391 * Pseudo-device attach routine. 392 */ 393void 394bridgeattach(int n) 395{ 396 397 pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode), 398 0, 0, 0, "brtpl", NULL, IPL_NET); 399 400 bridge_psref_class = psref_class_create("bridge", IPL_SOFTNET); 401 402 if_clone_attach(&bridge_cloner); 403} 404 405/* 406 * bridge_clone_create: 407 * 408 * Create a new bridge instance. 409 */ 410static int 411bridge_clone_create(struct if_clone *ifc, int unit) 412{ 413 struct bridge_softc *sc; 414 struct ifnet *ifp; 415 int error; 416 417 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 418 ifp = &sc->sc_if; 419 420 sc->sc_brtmax = BRIDGE_RTABLE_MAX; 421 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; 422 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE; 423 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME; 424 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY; 425 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY; 426 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME; 427 sc->sc_filter_flags = 0; 428 429 /* Initialize our routing table. */ 430 bridge_rtable_init(sc); 431 432 error = workqueue_create(&sc->sc_rtage_wq, "bridge_rtage", 433 bridge_rtage_work, sc, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE); 434 if (error) 435 panic("%s: workqueue_create %d\n", __func__, error); 436 437 callout_init(&sc->sc_brcallout, CALLOUT_MPSAFE); 438 callout_init(&sc->sc_bstpcallout, CALLOUT_MPSAFE); 439 440 mutex_init(&sc->sc_iflist_psref.bip_lock, MUTEX_DEFAULT, IPL_NONE); 441 PSLIST_INIT(&sc->sc_iflist_psref.bip_iflist); 442 sc->sc_iflist_psref.bip_psz = pserialize_create(); 443 444 if_initname(ifp, ifc->ifc_name, unit); 445 ifp->if_softc = sc; 446 ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE; 447#ifdef NET_MPSAFE 448 ifp->if_extflags |= IFEF_MPSAFE; 449#endif 450 ifp->if_mtu = ETHERMTU; 451 ifp->if_ioctl = bridge_ioctl; 452 ifp->if_output = bridge_output; 453 ifp->if_start = bridge_start; 454 ifp->if_stop = bridge_stop; 455 ifp->if_init = bridge_init; 456 ifp->if_type = IFT_BRIDGE; 457 ifp->if_addrlen = 0; 458 ifp->if_dlt = DLT_EN10MB; 459 ifp->if_hdrlen = ETHER_HDR_LEN; 460 461 error = if_initialize(ifp); 462 if (error != 0) { 463 pserialize_destroy(sc->sc_iflist_psref.bip_psz); 464 mutex_destroy(&sc->sc_iflist_psref.bip_lock); 465 callout_destroy(&sc->sc_brcallout); 466 callout_destroy(&sc->sc_bstpcallout); 467 workqueue_destroy(sc->sc_rtage_wq); 468 bridge_rtable_fini(sc); 469 kmem_free(sc, sizeof(*sc)); 470 471 return error; 472 } 473 if_alloc_sadl(ifp); 474 if_register(ifp); 475 476 return 0; 477} 478 479/* 480 * bridge_clone_destroy: 481 * 482 * Destroy a bridge instance. 483 */ 484static int 485bridge_clone_destroy(struct ifnet *ifp) 486{ 487 struct bridge_softc *sc = ifp->if_softc; 488 struct bridge_iflist *bif; 489 490 if ((ifp->if_flags & IFF_RUNNING) != 0) 491 bridge_stop(ifp, 1); 492 493 BRIDGE_LOCK(sc); 494 for (;;) { 495 bif = PSLIST_WRITER_FIRST(&sc->sc_iflist_psref.bip_iflist, struct bridge_iflist, 496 bif_next); 497 if (bif == NULL) 498 break; 499 bridge_delete_member(sc, bif); 500 } 501 PSLIST_DESTROY(&sc->sc_iflist_psref.bip_iflist); 502 BRIDGE_UNLOCK(sc); 503 504 if_detach(ifp); 505 506 /* Tear down the routing table. */ 507 bridge_rtable_fini(sc); 508 509 pserialize_destroy(sc->sc_iflist_psref.bip_psz); 510 mutex_destroy(&sc->sc_iflist_psref.bip_lock); 511 callout_destroy(&sc->sc_brcallout); 512 callout_destroy(&sc->sc_bstpcallout); 513 workqueue_destroy(sc->sc_rtage_wq); 514 kmem_free(sc, sizeof(*sc)); 515 516 return 0; 517} 518 519/* 520 * bridge_ioctl: 521 * 522 * Handle a control request from the operator. 523 */ 524static int 525bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data) 526{ 527 struct bridge_softc *sc = ifp->if_softc; 528 struct lwp *l = curlwp; /* XXX */ 529 union { 530 struct ifbreq ifbreq; 531 struct ifbifconf ifbifconf; 532 struct ifbareq ifbareq; 533 struct ifbaconf ifbaconf; 534 struct ifbrparam ifbrparam; 535 } args; 536 struct ifdrv *ifd = (struct ifdrv *) data; 537 const struct bridge_control *bc = NULL; /* XXXGCC */ 538 int s, error = 0; 539 540 /* Authorize command before calling splsoftnet(). */ 541 switch (cmd) { 542 case SIOCGDRVSPEC: 543 case SIOCSDRVSPEC: 544 if (ifd->ifd_cmd >= bridge_control_table_size 545 || (bc = &bridge_control_table[ifd->ifd_cmd]) == NULL) { 546 error = EINVAL; 547 return error; 548 } 549 550 /* We only care about BC_F_SUSER at this point. */ 551 if ((bc->bc_flags & BC_F_SUSER) == 0) 552 break; 553 554 error = kauth_authorize_network(l->l_cred, 555 KAUTH_NETWORK_INTERFACE_BRIDGE, 556 cmd == SIOCGDRVSPEC ? 557 KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV : 558 KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV, 559 ifd, NULL, NULL); 560 if (error) 561 return error; 562 563 break; 564 } 565 566 s = splsoftnet(); 567 568 switch (cmd) { 569 case SIOCGDRVSPEC: 570 case SIOCSDRVSPEC: 571 KASSERT(bc != NULL); 572 if (cmd == SIOCGDRVSPEC && 573 (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) == 0) { 574 error = EINVAL; 575 break; 576 } 577 else if (cmd == SIOCSDRVSPEC && 578 (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) != 0) { 579 error = EINVAL; 580 break; 581 } 582 583 /* BC_F_SUSER is checked above, before splsoftnet(). */ 584 585 if ((bc->bc_flags & (BC_F_XLATEIN|BC_F_XLATEOUT)) == 0 586 && (ifd->ifd_len != bc->bc_argsize 587 || ifd->ifd_len > sizeof(args))) { 588 error = EINVAL; 589 break; 590 } 591 592 memset(&args, 0, sizeof(args)); 593 if (bc->bc_flags & BC_F_COPYIN) { 594 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 595 if (error) 596 break; 597 } else if (bc->bc_flags & BC_F_XLATEIN) { 598 args.ifbifconf.ifbic_len = ifd->ifd_len; 599 args.ifbifconf.ifbic_buf = ifd->ifd_data; 600 } 601 602 error = (*bc->bc_func)(sc, &args); 603 if (error) 604 break; 605 606 if (bc->bc_flags & BC_F_COPYOUT) { 607 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 608 } else if (bc->bc_flags & BC_F_XLATEOUT) { 609 ifd->ifd_len = args.ifbifconf.ifbic_len; 610 ifd->ifd_data = args.ifbifconf.ifbic_buf; 611 } 612 break; 613 614 case SIOCSIFFLAGS: 615 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 616 break; 617 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 618 case IFF_RUNNING: 619 /* 620 * If interface is marked down and it is running, 621 * then stop and disable it. 622 */ 623 (*ifp->if_stop)(ifp, 1); 624 break; 625 case IFF_UP: 626 /* 627 * If interface is marked up and it is stopped, then 628 * start it. 629 */ 630 error = (*ifp->if_init)(ifp); 631 break; 632 default: 633 break; 634 } 635 break; 636 637 case SIOCSIFMTU: 638 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) 639 error = 0; 640 break; 641 642 default: 643 error = ifioctl_common(ifp, cmd, data); 644 break; 645 } 646 647 splx(s); 648 649 return error; 650} 651 652/* 653 * bridge_lookup_member: 654 * 655 * Lookup a bridge member interface. 656 */ 657static struct bridge_iflist * 658bridge_lookup_member(struct bridge_softc *sc, const char *name, struct psref *psref) 659{ 660 struct bridge_iflist *bif; 661 struct ifnet *ifp; 662 int s; 663 664 BRIDGE_PSZ_RENTER(s); 665 666 BRIDGE_IFLIST_READER_FOREACH(bif, sc) { 667 ifp = bif->bif_ifp; 668 if (strcmp(ifp->if_xname, name) == 0) 669 break; 670 } 671 if (bif != NULL) 672 bridge_acquire_member(sc, bif, psref); 673 674 BRIDGE_PSZ_REXIT(s); 675 676 return bif; 677} 678 679/* 680 * bridge_lookup_member_if: 681 * 682 * Lookup a bridge member interface by ifnet*. 683 */ 684static struct bridge_iflist * 685bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp, 686 struct psref *psref) 687{ 688 struct bridge_iflist *bif; 689 int s; 690 691 BRIDGE_PSZ_RENTER(s); 692 693 bif = member_ifp->if_bridgeif; 694 if (bif != NULL) { 695 psref_acquire(psref, &bif->bif_psref, 696 bridge_psref_class); 697 } 698 699 BRIDGE_PSZ_REXIT(s); 700 701 return bif; 702} 703 704static void 705bridge_acquire_member(struct bridge_softc *sc, struct bridge_iflist *bif, 706 struct psref *psref) 707{ 708 709 psref_acquire(psref, &bif->bif_psref, bridge_psref_class); 710} 711 712/* 713 * bridge_release_member: 714 * 715 * Release the specified member interface. 716 */ 717static void 718bridge_release_member(struct bridge_softc *sc, struct bridge_iflist *bif, 719 struct psref *psref) 720{ 721 722 psref_release(psref, &bif->bif_psref, bridge_psref_class); 723} 724 725/* 726 * bridge_delete_member: 727 * 728 * Delete the specified member interface. 729 */ 730static void 731bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif) 732{ 733 struct ifnet *ifs = bif->bif_ifp; 734 735 KASSERT(BRIDGE_LOCKED(sc)); 736 737 ifs->_if_input = ether_input; 738 ifs->if_bridge = NULL; 739 ifs->if_bridgeif = NULL; 740 741 PSLIST_WRITER_REMOVE(bif, bif_next); 742 BRIDGE_PSZ_PERFORM(sc); 743 BRIDGE_UNLOCK(sc); 744 745 psref_target_destroy(&bif->bif_psref, bridge_psref_class); 746 747 PSLIST_ENTRY_DESTROY(bif, bif_next); 748 kmem_free(bif, sizeof(*bif)); 749 750 BRIDGE_LOCK(sc); 751} 752 753static int 754bridge_ioctl_add(struct bridge_softc *sc, void *arg) 755{ 756 struct ifbreq *req = arg; 757 struct bridge_iflist *bif = NULL; 758 struct ifnet *ifs; 759 int error = 0; 760 struct psref psref; 761 762 ifs = if_get(req->ifbr_ifsname, &psref); 763 if (ifs == NULL) 764 return ENOENT; 765 766 if (ifs->if_bridge == sc) { 767 error = EEXIST; 768 goto out; 769 } 770 771 if (ifs->if_bridge != NULL) { 772 error = EBUSY; 773 goto out; 774 } 775 776 if (ifs->_if_input != ether_input) { 777 error = EINVAL; 778 goto out; 779 } 780 781 /* FIXME: doesn't work with non-IFF_SIMPLEX interfaces */ 782 if ((ifs->if_flags & IFF_SIMPLEX) == 0) { 783 error = EINVAL; 784 goto out; 785 } 786 787 bif = kmem_alloc(sizeof(*bif), KM_SLEEP); 788 789 switch (ifs->if_type) { 790 case IFT_ETHER: 791 if (sc->sc_if.if_mtu != ifs->if_mtu) { 792 error = EINVAL; 793 goto out; 794 } 795 /* FALLTHROUGH */ 796 case IFT_L2TP: 797 IFNET_LOCK(ifs); 798 error = ether_enable_vlan_mtu(ifs); 799 IFNET_UNLOCK(ifs); 800 if (error > 0) 801 goto out; 802 /* 803 * Place the interface into promiscuous mode. 804 */ 805 error = ifpromisc(ifs, 1); 806 if (error) 807 goto out; 808 break; 809 default: 810 error = EINVAL; 811 goto out; 812 } 813 814 bif->bif_ifp = ifs; 815 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER; 816 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY; 817 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST; 818 PSLIST_ENTRY_INIT(bif, bif_next); 819 psref_target_init(&bif->bif_psref, bridge_psref_class); 820 821 BRIDGE_LOCK(sc); 822 823 ifs->if_bridge = sc; 824 ifs->if_bridgeif = bif; 825 PSLIST_WRITER_INSERT_HEAD(&sc->sc_iflist_psref.bip_iflist, bif, bif_next); 826 ifs->_if_input = bridge_input; 827 828 BRIDGE_UNLOCK(sc); 829 830 if (sc->sc_if.if_flags & IFF_RUNNING) 831 bstp_initialization(sc); 832 else 833 bstp_stop(sc); 834 835 out: 836 if_put(ifs, &psref); 837 if (error) { 838 if (bif != NULL) 839 kmem_free(bif, sizeof(*bif)); 840 } 841 return error; 842} 843 844static int 845bridge_ioctl_del(struct bridge_softc *sc, void *arg) 846{ 847 struct ifbreq *req = arg; 848 const char *name = req->ifbr_ifsname; 849 struct bridge_iflist *bif; 850 struct ifnet *ifs; 851 852 BRIDGE_LOCK(sc); 853 854 /* 855 * Don't use bridge_lookup_member. We want to get a member 856 * with bif_refs == 0. 857 */ 858 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) { 859 ifs = bif->bif_ifp; 860 if (strcmp(ifs->if_xname, name) == 0) 861 break; 862 } 863 864 if (bif == NULL) { 865 BRIDGE_UNLOCK(sc); 866 return ENOENT; 867 } 868 869 bridge_delete_member(sc, bif); 870 871 BRIDGE_UNLOCK(sc); 872 873 switch (ifs->if_type) { 874 case IFT_ETHER: 875 case IFT_L2TP: 876 /* 877 * Take the interface out of promiscuous mode. 878 * Don't call it with holding a spin lock. 879 */ 880 (void) ifpromisc(ifs, 0); 881 IFNET_LOCK(ifs); 882 (void) ether_disable_vlan_mtu(ifs); 883 IFNET_UNLOCK(ifs); 884 break; 885 default: 886#ifdef DIAGNOSTIC 887 panic("bridge_delete_member: impossible"); 888#endif 889 break; 890 } 891 892 bridge_rtdelete(sc, ifs); 893 894 if (sc->sc_if.if_flags & IFF_RUNNING) 895 bstp_initialization(sc); 896 897 return 0; 898} 899 900static int 901bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) 902{ 903 struct ifbreq *req = arg; 904 struct bridge_iflist *bif; 905 struct psref psref; 906 907 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref); 908 if (bif == NULL) 909 return ENOENT; 910 911 req->ifbr_ifsflags = bif->bif_flags; 912 req->ifbr_state = bif->bif_state; 913 req->ifbr_priority = bif->bif_priority; 914 req->ifbr_path_cost = bif->bif_path_cost; 915 req->ifbr_portno = bif->bif_ifp->if_index & 0xff; 916 917 bridge_release_member(sc, bif, &psref); 918 919 return 0; 920} 921 922static int 923bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) 924{ 925 struct ifbreq *req = arg; 926 struct bridge_iflist *bif; 927 struct psref psref; 928 929 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref); 930 if (bif == NULL) 931 return ENOENT; 932 933 if (req->ifbr_ifsflags & IFBIF_STP) { 934 switch (bif->bif_ifp->if_type) { 935 case IFT_ETHER: 936 case IFT_L2TP: 937 /* These can do spanning tree. */ 938 break; 939 940 default: 941 /* Nothing else can. */ 942 bridge_release_member(sc, bif, &psref); 943 return EINVAL; 944 } 945 } 946 947 bif->bif_flags = req->ifbr_ifsflags; 948 949 bridge_release_member(sc, bif, &psref); 950 951 if (sc->sc_if.if_flags & IFF_RUNNING) 952 bstp_initialization(sc); 953 954 return 0; 955} 956 957static int 958bridge_ioctl_scache(struct bridge_softc *sc, void *arg) 959{ 960 struct ifbrparam *param = arg; 961 962 sc->sc_brtmax = param->ifbrp_csize; 963 bridge_rttrim(sc); 964 965 return 0; 966} 967 968static int 969bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) 970{ 971 struct ifbrparam *param = arg; 972 973 param->ifbrp_csize = sc->sc_brtmax; 974 975 return 0; 976} 977 978static int 979bridge_ioctl_gifs(struct bridge_softc *sc, void *arg) 980{ 981 struct ifbifconf *bifc = arg; 982 struct bridge_iflist *bif; 983 struct ifbreq *breqs; 984 int i, count, error = 0; 985 986retry: 987 BRIDGE_LOCK(sc); 988 count = 0; 989 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) 990 count++; 991 BRIDGE_UNLOCK(sc); 992 993 if (count == 0) { 994 bifc->ifbic_len = 0; 995 return 0; 996 } 997 998 if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) { 999 /* Tell that a larger buffer is needed */ 1000 bifc->ifbic_len = sizeof(*breqs) * count; 1001 return 0; 1002 } 1003 1004 breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP); 1005 1006 BRIDGE_LOCK(sc); 1007 1008 i = 0; 1009 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) 1010 i++; 1011 if (i > count) { 1012 /* 1013 * The number of members has been increased. 1014 * We need more memory! 1015 */ 1016 BRIDGE_UNLOCK(sc); 1017 kmem_free(breqs, sizeof(*breqs) * count); 1018 goto retry; 1019 } 1020 1021 i = 0; 1022 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) { 1023 struct ifbreq *breq = &breqs[i++]; 1024 memset(breq, 0, sizeof(*breq)); 1025 1026 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname, 1027 sizeof(breq->ifbr_ifsname)); 1028 breq->ifbr_ifsflags = bif->bif_flags; 1029 breq->ifbr_state = bif->bif_state; 1030 breq->ifbr_priority = bif->bif_priority; 1031 breq->ifbr_path_cost = bif->bif_path_cost; 1032 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff; 1033 } 1034 1035 /* Don't call copyout with holding the mutex */ 1036 BRIDGE_UNLOCK(sc); 1037 1038 for (i = 0; i < count; i++) { 1039 error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs)); 1040 if (error) 1041 break; 1042 } 1043 bifc->ifbic_len = sizeof(*breqs) * i; 1044 1045 kmem_free(breqs, sizeof(*breqs) * count); 1046 1047 return error; 1048} 1049 1050static int 1051bridge_ioctl_rts(struct bridge_softc *sc, void *arg) 1052{ 1053 struct ifbaconf *bac = arg; 1054 struct bridge_rtnode *brt; 1055 struct ifbareq bareq; 1056 int count = 0, error = 0, len; 1057 1058 if (bac->ifbac_len == 0) 1059 return 0; 1060 1061 BRIDGE_RT_LOCK(sc); 1062 1063 /* The passed buffer is not enough, tell a required size. */ 1064 if (bac->ifbac_len < (sizeof(bareq) * sc->sc_brtcnt)) { 1065 count = sc->sc_brtcnt; 1066 goto out; 1067 } 1068 1069 len = bac->ifbac_len; 1070 BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) { 1071 if (len < sizeof(bareq)) 1072 goto out; 1073 memset(&bareq, 0, sizeof(bareq)); 1074 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname, 1075 sizeof(bareq.ifba_ifsname)); 1076 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); 1077 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 1078 bareq.ifba_expire = brt->brt_expire - time_uptime; 1079 } else 1080 bareq.ifba_expire = 0; 1081 bareq.ifba_flags = brt->brt_flags; 1082 1083 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq)); 1084 if (error) 1085 goto out; 1086 count++; 1087 len -= sizeof(bareq); 1088 } 1089 out: 1090 BRIDGE_RT_UNLOCK(sc); 1091 1092 bac->ifbac_len = sizeof(bareq) * count; 1093 return error; 1094} 1095 1096static int 1097bridge_ioctl_saddr(struct bridge_softc *sc, void *arg) 1098{ 1099 struct ifbareq *req = arg; 1100 struct bridge_iflist *bif; 1101 int error; 1102 struct psref psref; 1103 1104 bif = bridge_lookup_member(sc, req->ifba_ifsname, &psref); 1105 if (bif == NULL) 1106 return ENOENT; 1107 1108 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1, 1109 req->ifba_flags); 1110 1111 bridge_release_member(sc, bif, &psref); 1112 1113 return error; 1114} 1115 1116static int 1117bridge_ioctl_sto(struct bridge_softc *sc, void *arg) 1118{ 1119 struct ifbrparam *param = arg; 1120 1121 sc->sc_brttimeout = param->ifbrp_ctime; 1122 1123 return 0; 1124} 1125 1126static int 1127bridge_ioctl_gto(struct bridge_softc *sc, void *arg) 1128{ 1129 struct ifbrparam *param = arg; 1130 1131 param->ifbrp_ctime = sc->sc_brttimeout; 1132 1133 return 0; 1134} 1135 1136static int 1137bridge_ioctl_daddr(struct bridge_softc *sc, void *arg) 1138{ 1139 struct ifbareq *req = arg; 1140 1141 return (bridge_rtdaddr(sc, req->ifba_dst)); 1142} 1143 1144static int 1145bridge_ioctl_flush(struct bridge_softc *sc, void *arg) 1146{ 1147 struct ifbreq *req = arg; 1148 1149 bridge_rtflush(sc, req->ifbr_ifsflags); 1150 1151 return 0; 1152} 1153 1154static int 1155bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) 1156{ 1157 struct ifbrparam *param = arg; 1158 1159 param->ifbrp_prio = sc->sc_bridge_priority; 1160 1161 return 0; 1162} 1163 1164static int 1165bridge_ioctl_spri(struct bridge_softc *sc, void *arg) 1166{ 1167 struct ifbrparam *param = arg; 1168 1169 sc->sc_bridge_priority = param->ifbrp_prio; 1170 1171 if (sc->sc_if.if_flags & IFF_RUNNING) 1172 bstp_initialization(sc); 1173 1174 return 0; 1175} 1176 1177static int 1178bridge_ioctl_ght(struct bridge_softc *sc, void *arg) 1179{ 1180 struct ifbrparam *param = arg; 1181 1182 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8; 1183 1184 return 0; 1185} 1186 1187static int 1188bridge_ioctl_sht(struct bridge_softc *sc, void *arg) 1189{ 1190 struct ifbrparam *param = arg; 1191 1192 if (param->ifbrp_hellotime == 0) 1193 return EINVAL; 1194 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8; 1195 1196 if (sc->sc_if.if_flags & IFF_RUNNING) 1197 bstp_initialization(sc); 1198 1199 return 0; 1200} 1201 1202static int 1203bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) 1204{ 1205 struct ifbrparam *param = arg; 1206 1207 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8; 1208 1209 return 0; 1210} 1211 1212static int 1213bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) 1214{ 1215 struct ifbrparam *param = arg; 1216 1217 if (param->ifbrp_fwddelay == 0) 1218 return EINVAL; 1219 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8; 1220 1221 if (sc->sc_if.if_flags & IFF_RUNNING) 1222 bstp_initialization(sc); 1223 1224 return 0; 1225} 1226 1227static int 1228bridge_ioctl_gma(struct bridge_softc *sc, void *arg) 1229{ 1230 struct ifbrparam *param = arg; 1231 1232 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8; 1233 1234 return 0; 1235} 1236 1237static int 1238bridge_ioctl_sma(struct bridge_softc *sc, void *arg) 1239{ 1240 struct ifbrparam *param = arg; 1241 1242 if (param->ifbrp_maxage == 0) 1243 return EINVAL; 1244 sc->sc_bridge_max_age = param->ifbrp_maxage << 8; 1245 1246 if (sc->sc_if.if_flags & IFF_RUNNING) 1247 bstp_initialization(sc); 1248 1249 return 0; 1250} 1251 1252static int 1253bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) 1254{ 1255 struct ifbreq *req = arg; 1256 struct bridge_iflist *bif; 1257 struct psref psref; 1258 1259 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref); 1260 if (bif == NULL) 1261 return ENOENT; 1262 1263 bif->bif_priority = req->ifbr_priority; 1264 1265 if (sc->sc_if.if_flags & IFF_RUNNING) 1266 bstp_initialization(sc); 1267 1268 bridge_release_member(sc, bif, &psref); 1269 1270 return 0; 1271} 1272 1273#if defined(BRIDGE_IPF) 1274static int 1275bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg) 1276{ 1277 struct ifbrparam *param = arg; 1278 1279 param->ifbrp_filter = sc->sc_filter_flags; 1280 1281 return 0; 1282} 1283 1284static int 1285bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg) 1286{ 1287 struct ifbrparam *param = arg; 1288 uint32_t nflags, oflags; 1289 1290 if (param->ifbrp_filter & ~IFBF_FILT_MASK) 1291 return EINVAL; 1292 1293 nflags = param->ifbrp_filter; 1294 oflags = sc->sc_filter_flags; 1295 1296 if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) { 1297 pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT, 1298 sc->sc_if.if_pfil); 1299 } 1300 if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) { 1301 pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT, 1302 sc->sc_if.if_pfil); 1303 } 1304 1305 sc->sc_filter_flags = nflags; 1306 1307 return 0; 1308} 1309#endif /* BRIDGE_IPF */ 1310 1311static int 1312bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) 1313{ 1314 struct ifbreq *req = arg; 1315 struct bridge_iflist *bif; 1316 struct psref psref; 1317 1318 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref); 1319 if (bif == NULL) 1320 return ENOENT; 1321 1322 bif->bif_path_cost = req->ifbr_path_cost; 1323 1324 if (sc->sc_if.if_flags & IFF_RUNNING) 1325 bstp_initialization(sc); 1326 1327 bridge_release_member(sc, bif, &psref); 1328 1329 return 0; 1330} 1331 1332/* 1333 * bridge_ifdetach: 1334 * 1335 * Detach an interface from a bridge. Called when a member 1336 * interface is detaching. 1337 */ 1338void 1339bridge_ifdetach(struct ifnet *ifp) 1340{ 1341 struct bridge_softc *sc = ifp->if_bridge; 1342 struct ifbreq breq; 1343 1344 /* ioctl_lock should prevent this from happening */ 1345 KASSERT(sc != NULL); 1346 1347 memset(&breq, 0, sizeof(breq)); 1348 strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname)); 1349 1350 (void) bridge_ioctl_del(sc, &breq); 1351} 1352 1353/* 1354 * bridge_init: 1355 * 1356 * Initialize a bridge interface. 1357 */ 1358static int 1359bridge_init(struct ifnet *ifp) 1360{ 1361 struct bridge_softc *sc = ifp->if_softc; 1362 1363 KASSERT((ifp->if_flags & IFF_RUNNING) == 0); 1364 1365 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz, 1366 bridge_timer, sc); 1367 bstp_initialization(sc); 1368 1369 ifp->if_flags |= IFF_RUNNING; 1370 return 0; 1371} 1372 1373/* 1374 * bridge_stop: 1375 * 1376 * Stop the bridge interface. 1377 */ 1378static void 1379bridge_stop(struct ifnet *ifp, int disable) 1380{ 1381 struct bridge_softc *sc = ifp->if_softc; 1382 1383 KASSERT((ifp->if_flags & IFF_RUNNING) != 0); 1384 ifp->if_flags &= ~IFF_RUNNING; 1385 1386 callout_halt(&sc->sc_brcallout, NULL); 1387 workqueue_wait(sc->sc_rtage_wq, &sc->sc_rtage_wk); 1388 bstp_stop(sc); 1389 bridge_rtflush(sc, IFBF_FLUSHDYN); 1390} 1391 1392/* 1393 * bridge_enqueue: 1394 * 1395 * Enqueue a packet on a bridge member interface. 1396 */ 1397void 1398bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m, 1399 int runfilt) 1400{ 1401 int len, error; 1402 short mflags; 1403 1404 if (runfilt) { 1405 if (pfil_run_hooks(sc->sc_if.if_pfil, &m, 1406 dst_ifp, PFIL_OUT) != 0) { 1407 if (m != NULL) 1408 m_freem(m); 1409 return; 1410 } 1411 if (m == NULL) 1412 return; 1413 } 1414 1415#ifdef ALTQ 1416 KERNEL_LOCK(1, NULL); 1417 /* 1418 * If ALTQ is enabled on the member interface, do 1419 * classification; the queueing discipline might 1420 * not require classification, but might require 1421 * the address family/header pointer in the pktattr. 1422 */ 1423 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) { 1424 /* XXX IFT_ETHER */ 1425 altq_etherclassify(&dst_ifp->if_snd, m); 1426 } 1427 KERNEL_UNLOCK_ONE(NULL); 1428#endif /* ALTQ */ 1429 1430 len = m->m_pkthdr.len; 1431 mflags = m->m_flags; 1432 1433 error = if_transmit_lock(dst_ifp, m); 1434 if (error) { 1435 /* mbuf is already freed */ 1436 sc->sc_if.if_oerrors++; 1437 return; 1438 } 1439 1440 sc->sc_if.if_opackets++; 1441 sc->sc_if.if_obytes += len; 1442 if (mflags & M_MCAST) 1443 sc->sc_if.if_omcasts++; 1444} 1445 1446/* 1447 * bridge_output: 1448 * 1449 * Send output from a bridge member interface. This 1450 * performs the bridging function for locally originated 1451 * packets. 1452 * 1453 * The mbuf has the Ethernet header already attached. We must 1454 * enqueue or free the mbuf before returning. 1455 */ 1456int 1457bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa, 1458 const struct rtentry *rt) 1459{ 1460 struct ether_header *eh; 1461 struct ifnet *dst_if; 1462 struct bridge_softc *sc; 1463 int s; 1464 1465 /* 1466 * bridge_output() is called from ether_output(), furthermore 1467 * ifp argument doesn't point to bridge(4). So, don't assert 1468 * IFEF_MPSAFE here. 1469 */ 1470 1471 if (m->m_len < ETHER_HDR_LEN) { 1472 m = m_pullup(m, ETHER_HDR_LEN); 1473 if (m == NULL) 1474 return 0; 1475 } 1476 1477 eh = mtod(m, struct ether_header *); 1478 sc = ifp->if_bridge; 1479 1480 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 1481 if (memcmp(etherbroadcastaddr, 1482 eh->ether_dhost, ETHER_ADDR_LEN) == 0) 1483 m->m_flags |= M_BCAST; 1484 else 1485 m->m_flags |= M_MCAST; 1486 } 1487 1488 /* 1489 * If bridge is down, but the original output interface is up, 1490 * go ahead and send out that interface. Otherwise, the packet 1491 * is dropped below. 1492 */ 1493 if (__predict_false(sc == NULL) || 1494 (sc->sc_if.if_flags & IFF_RUNNING) == 0) { 1495 dst_if = ifp; 1496 goto sendunicast; 1497 } 1498 1499 /* 1500 * If the packet is a multicast, or we don't know a better way to 1501 * get there, send to all interfaces. 1502 */ 1503 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0) 1504 dst_if = NULL; 1505 else 1506 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1507 if (dst_if == NULL) { 1508 /* XXX Should call bridge_broadcast, but there are locking 1509 * issues which need resolving first. */ 1510 struct bridge_iflist *bif; 1511 struct mbuf *mc; 1512 bool used = false; 1513 1514 BRIDGE_PSZ_RENTER(s); 1515 BRIDGE_IFLIST_READER_FOREACH(bif, sc) { 1516 struct psref psref; 1517 1518 bridge_acquire_member(sc, bif, &psref); 1519 BRIDGE_PSZ_REXIT(s); 1520 1521 dst_if = bif->bif_ifp; 1522 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1523 goto next; 1524 1525 /* 1526 * If this is not the original output interface, 1527 * and the interface is participating in spanning 1528 * tree, make sure the port is in a state that 1529 * allows forwarding. 1530 */ 1531 if (dst_if != ifp && 1532 (bif->bif_flags & IFBIF_STP) != 0) { 1533 switch (bif->bif_state) { 1534 case BSTP_IFSTATE_BLOCKING: 1535 case BSTP_IFSTATE_LISTENING: 1536 case BSTP_IFSTATE_DISABLED: 1537 goto next; 1538 } 1539 } 1540 1541 if (PSLIST_READER_NEXT(bif, struct bridge_iflist, 1542 bif_next) == NULL && 1543 ((m->m_flags & (M_MCAST | M_BCAST)) == 0 || 1544 dst_if == ifp)) 1545 { 1546 used = true; 1547 mc = m; 1548 } else { 1549 mc = m_copypacket(m, M_DONTWAIT); 1550 if (mc == NULL) { 1551 sc->sc_if.if_oerrors++; 1552 goto next; 1553 } 1554 } 1555 1556 bridge_enqueue(sc, dst_if, mc, 0); 1557 1558 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 && 1559 dst_if != ifp) 1560 { 1561 if (PSLIST_READER_NEXT(bif, 1562 struct bridge_iflist, bif_next) == NULL) 1563 { 1564 used = true; 1565 mc = m; 1566 } else { 1567 mc = m_copypacket(m, M_DONTWAIT); 1568 if (mc == NULL) { 1569 sc->sc_if.if_oerrors++; 1570 goto next; 1571 } 1572 } 1573 1574 m_set_rcvif(mc, dst_if); 1575 mc->m_flags &= ~M_PROMISC; 1576 1577 s = splsoftnet(); 1578 KERNEL_LOCK_UNLESS_IFP_MPSAFE(dst_if); 1579 ether_input(dst_if, mc); 1580 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(dst_if); 1581 splx(s); 1582 } 1583 1584next: 1585 BRIDGE_PSZ_RENTER(s); 1586 bridge_release_member(sc, bif, &psref); 1587 1588 /* Guarantee we don't re-enter the loop as we already 1589 * decided we're at the end. */ 1590 if (used) 1591 break; 1592 } 1593 BRIDGE_PSZ_REXIT(s); 1594 1595 if (!used) 1596 m_freem(m); 1597 return 0; 1598 } 1599 1600 sendunicast: 1601 /* 1602 * XXX Spanning tree consideration here? 1603 */ 1604 1605 if ((dst_if->if_flags & IFF_RUNNING) == 0) { 1606 m_freem(m); 1607 return 0; 1608 } 1609 1610 bridge_enqueue(sc, dst_if, m, 0); 1611 1612 return 0; 1613} 1614 1615/* 1616 * bridge_start: 1617 * 1618 * Start output on a bridge. 1619 * 1620 * NOTE: This routine should never be called in this implementation. 1621 */ 1622static void 1623bridge_start(struct ifnet *ifp) 1624{ 1625 1626 printf("%s: bridge_start() called\n", ifp->if_xname); 1627} 1628 1629/* 1630 * bridge_forward: 1631 * 1632 * The forwarding function of the bridge. 1633 */ 1634static void 1635bridge_forward(struct bridge_softc *sc, struct mbuf *m) 1636{ 1637 struct bridge_iflist *bif; 1638 struct ifnet *src_if, *dst_if; 1639 struct ether_header *eh; 1640 struct psref psref; 1641 struct psref psref_src; 1642 DECLARE_LOCK_VARIABLE; 1643 1644 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) 1645 return; 1646 1647 src_if = m_get_rcvif_psref(m, &psref_src); 1648 if (src_if == NULL) { 1649 /* Interface is being destroyed? */ 1650 m_freem(m); 1651 goto out; 1652 } 1653 1654 sc->sc_if.if_ipackets++; 1655 sc->sc_if.if_ibytes += m->m_pkthdr.len; 1656 1657 /* 1658 * Look up the bridge_iflist. 1659 */ 1660 bif = bridge_lookup_member_if(sc, src_if, &psref); 1661 if (bif == NULL) { 1662 /* Interface is not a bridge member (anymore?) */ 1663 m_freem(m); 1664 goto out; 1665 } 1666 1667 if (bif->bif_flags & IFBIF_STP) { 1668 switch (bif->bif_state) { 1669 case BSTP_IFSTATE_BLOCKING: 1670 case BSTP_IFSTATE_LISTENING: 1671 case BSTP_IFSTATE_DISABLED: 1672 m_freem(m); 1673 bridge_release_member(sc, bif, &psref); 1674 goto out; 1675 } 1676 } 1677 1678 eh = mtod(m, struct ether_header *); 1679 1680 /* 1681 * If the interface is learning, and the source 1682 * address is valid and not multicast, record 1683 * the address. 1684 */ 1685 if ((bif->bif_flags & IFBIF_LEARNING) != 0 && 1686 ETHER_IS_MULTICAST(eh->ether_shost) == 0 && 1687 (eh->ether_shost[0] == 0 && 1688 eh->ether_shost[1] == 0 && 1689 eh->ether_shost[2] == 0 && 1690 eh->ether_shost[3] == 0 && 1691 eh->ether_shost[4] == 0 && 1692 eh->ether_shost[5] == 0) == 0) { 1693 (void) bridge_rtupdate(sc, eh->ether_shost, 1694 src_if, 0, IFBAF_DYNAMIC); 1695 } 1696 1697 if ((bif->bif_flags & IFBIF_STP) != 0 && 1698 bif->bif_state == BSTP_IFSTATE_LEARNING) { 1699 m_freem(m); 1700 bridge_release_member(sc, bif, &psref); 1701 goto out; 1702 } 1703 1704 bridge_release_member(sc, bif, &psref); 1705 1706 /* 1707 * At this point, the port either doesn't participate 1708 * in spanning tree or it is in the forwarding state. 1709 */ 1710 1711 /* 1712 * If the packet is unicast, destined for someone on 1713 * "this" side of the bridge, drop it. 1714 */ 1715 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) { 1716 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1717 if (src_if == dst_if) { 1718 m_freem(m); 1719 goto out; 1720 } 1721 } else { 1722 /* ...forward it to all interfaces. */ 1723 sc->sc_if.if_imcasts++; 1724 dst_if = NULL; 1725 } 1726 1727 if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) { 1728 if (m != NULL) 1729 m_freem(m); 1730 goto out; 1731 } 1732 if (m == NULL) 1733 goto out; 1734 1735 if (dst_if == NULL) { 1736 bridge_broadcast(sc, src_if, m); 1737 goto out; 1738 } 1739 1740 m_put_rcvif_psref(src_if, &psref_src); 1741 src_if = NULL; 1742 1743 /* 1744 * At this point, we're dealing with a unicast frame 1745 * going to a different interface. 1746 */ 1747 if ((dst_if->if_flags & IFF_RUNNING) == 0) { 1748 m_freem(m); 1749 goto out; 1750 } 1751 1752 bif = bridge_lookup_member_if(sc, dst_if, &psref); 1753 if (bif == NULL) { 1754 /* Not a member of the bridge (anymore?) */ 1755 m_freem(m); 1756 goto out; 1757 } 1758 1759 if (bif->bif_flags & IFBIF_STP) { 1760 switch (bif->bif_state) { 1761 case BSTP_IFSTATE_DISABLED: 1762 case BSTP_IFSTATE_BLOCKING: 1763 m_freem(m); 1764 bridge_release_member(sc, bif, &psref); 1765 goto out; 1766 } 1767 } 1768 1769 bridge_release_member(sc, bif, &psref); 1770 1771 /* 1772 * Before enqueueing this packet to the destination interface, 1773 * clear any in-bound checksum flags to prevent them from being 1774 * misused as out-bound flags. 1775 */ 1776 m->m_pkthdr.csum_flags = 0; 1777 1778 ACQUIRE_GLOBAL_LOCKS(); 1779 bridge_enqueue(sc, dst_if, m, 1); 1780 RELEASE_GLOBAL_LOCKS(); 1781out: 1782 if (src_if != NULL) 1783 m_put_rcvif_psref(src_if, &psref_src); 1784 return; 1785} 1786 1787static bool 1788bstp_state_before_learning(struct bridge_iflist *bif) 1789{ 1790 if (bif->bif_flags & IFBIF_STP) { 1791 switch (bif->bif_state) { 1792 case BSTP_IFSTATE_BLOCKING: 1793 case BSTP_IFSTATE_LISTENING: 1794 case BSTP_IFSTATE_DISABLED: 1795 return true; 1796 } 1797 } 1798 return false; 1799} 1800 1801static bool 1802bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src) 1803{ 1804 uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost; 1805 1806 if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0 1807#if NCARP > 0 1808 || (bif->bif_ifp->if_carp && 1809 carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL) 1810#endif /* NCARP > 0 */ 1811 ) 1812 return true; 1813 1814 return false; 1815} 1816 1817/* 1818 * bridge_input: 1819 * 1820 * Receive input from a member interface. Queue the packet for 1821 * bridging if it is not for us. 1822 */ 1823static void 1824bridge_input(struct ifnet *ifp, struct mbuf *m) 1825{ 1826 struct bridge_softc *sc = ifp->if_bridge; 1827 struct bridge_iflist *bif; 1828 struct ether_header *eh; 1829 struct psref psref; 1830 int bound; 1831 DECLARE_LOCK_VARIABLE; 1832 1833 KASSERT(!cpu_intr_p()); 1834 1835 if (__predict_false(sc == NULL) || 1836 (sc->sc_if.if_flags & IFF_RUNNING) == 0) { 1837 ACQUIRE_GLOBAL_LOCKS(); 1838 ether_input(ifp, m); 1839 RELEASE_GLOBAL_LOCKS(); 1840 return; 1841 } 1842 1843 bound = curlwp_bind(); 1844 bif = bridge_lookup_member_if(sc, ifp, &psref); 1845 if (bif == NULL) { 1846 curlwp_bindx(bound); 1847 ACQUIRE_GLOBAL_LOCKS(); 1848 ether_input(ifp, m); 1849 RELEASE_GLOBAL_LOCKS(); 1850 return; 1851 } 1852 1853 eh = mtod(m, struct ether_header *); 1854 1855 if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 1856 if (memcmp(etherbroadcastaddr, 1857 eh->ether_dhost, ETHER_ADDR_LEN) == 0) 1858 m->m_flags |= M_BCAST; 1859 else 1860 m->m_flags |= M_MCAST; 1861 } 1862 1863 /* 1864 * A 'fast' path for packets addressed to interfaces that are 1865 * part of this bridge. 1866 */ 1867 if (!(m->m_flags & (M_BCAST|M_MCAST)) && 1868 !bstp_state_before_learning(bif)) { 1869 struct bridge_iflist *_bif; 1870 struct ifnet *_ifp = NULL; 1871 int s; 1872 struct psref _psref; 1873 1874 BRIDGE_PSZ_RENTER(s); 1875 BRIDGE_IFLIST_READER_FOREACH(_bif, sc) { 1876 /* It is destined for us. */ 1877 if (bridge_ourether(_bif, eh, 0)) { 1878 bridge_acquire_member(sc, _bif, &_psref); 1879 BRIDGE_PSZ_REXIT(s); 1880 if (_bif->bif_flags & IFBIF_LEARNING) 1881 (void) bridge_rtupdate(sc, 1882 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC); 1883 m_set_rcvif(m, _bif->bif_ifp); 1884 _ifp = _bif->bif_ifp; 1885 bridge_release_member(sc, _bif, &_psref); 1886 goto out; 1887 } 1888 1889 /* We just received a packet that we sent out. */ 1890 if (bridge_ourether(_bif, eh, 1)) 1891 break; 1892 } 1893 BRIDGE_PSZ_REXIT(s); 1894out: 1895 1896 if (_bif != NULL) { 1897 bridge_release_member(sc, bif, &psref); 1898 curlwp_bindx(bound); 1899 if (_ifp != NULL) { 1900 m->m_flags &= ~M_PROMISC; 1901 ACQUIRE_GLOBAL_LOCKS(); 1902 ether_input(_ifp, m); 1903 RELEASE_GLOBAL_LOCKS(); 1904 } else 1905 m_freem(m); 1906 return; 1907 } 1908 } 1909 1910 /* Tap off 802.1D packets; they do not get forwarded. */ 1911 if (bif->bif_flags & IFBIF_STP && 1912 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) { 1913 bstp_input(sc, bif, m); 1914 bridge_release_member(sc, bif, &psref); 1915 curlwp_bindx(bound); 1916 return; 1917 } 1918 1919 /* 1920 * A normal switch would discard the packet here, but that's not what 1921 * we've done historically. This also prevents some obnoxious behaviour. 1922 */ 1923 if (bstp_state_before_learning(bif)) { 1924 bridge_release_member(sc, bif, &psref); 1925 curlwp_bindx(bound); 1926 ACQUIRE_GLOBAL_LOCKS(); 1927 ether_input(ifp, m); 1928 RELEASE_GLOBAL_LOCKS(); 1929 return; 1930 } 1931 1932 bridge_release_member(sc, bif, &psref); 1933 1934 bridge_forward(sc, m); 1935 1936 curlwp_bindx(bound); 1937} 1938 1939/* 1940 * bridge_broadcast: 1941 * 1942 * Send a frame to all interfaces that are members of 1943 * the bridge, except for the one on which the packet 1944 * arrived. 1945 */ 1946static void 1947bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if, 1948 struct mbuf *m) 1949{ 1950 struct bridge_iflist *bif; 1951 struct mbuf *mc; 1952 struct ifnet *dst_if; 1953 bool bmcast; 1954 int s; 1955 DECLARE_LOCK_VARIABLE; 1956 1957 bmcast = m->m_flags & (M_BCAST|M_MCAST); 1958 1959 BRIDGE_PSZ_RENTER(s); 1960 BRIDGE_IFLIST_READER_FOREACH(bif, sc) { 1961 struct psref psref; 1962 1963 bridge_acquire_member(sc, bif, &psref); 1964 BRIDGE_PSZ_REXIT(s); 1965 1966 dst_if = bif->bif_ifp; 1967 1968 if (bif->bif_flags & IFBIF_STP) { 1969 switch (bif->bif_state) { 1970 case BSTP_IFSTATE_BLOCKING: 1971 case BSTP_IFSTATE_DISABLED: 1972 goto next; 1973 } 1974 } 1975 1976 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast) 1977 goto next; 1978 1979 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1980 goto next; 1981 1982 if (dst_if != src_if) { 1983 mc = m_copypacket(m, M_DONTWAIT); 1984 if (mc == NULL) { 1985 sc->sc_if.if_oerrors++; 1986 goto next; 1987 } 1988 /* 1989 * Before enqueueing this packet to the destination 1990 * interface, clear any in-bound checksum flags to 1991 * prevent them from being misused as out-bound flags. 1992 */ 1993 mc->m_pkthdr.csum_flags = 0; 1994 1995 ACQUIRE_GLOBAL_LOCKS(); 1996 bridge_enqueue(sc, dst_if, mc, 1); 1997 RELEASE_GLOBAL_LOCKS(); 1998 } 1999 2000 if (bmcast) { 2001 mc = m_copypacket(m, M_DONTWAIT); 2002 if (mc == NULL) { 2003 sc->sc_if.if_oerrors++; 2004 goto next; 2005 } 2006 2007 m_set_rcvif(mc, dst_if); 2008 mc->m_flags &= ~M_PROMISC; 2009 2010 ACQUIRE_GLOBAL_LOCKS(); 2011 ether_input(dst_if, mc); 2012 RELEASE_GLOBAL_LOCKS(); 2013 } 2014next: 2015 BRIDGE_PSZ_RENTER(s); 2016 bridge_release_member(sc, bif, &psref); 2017 } 2018 BRIDGE_PSZ_REXIT(s); 2019 2020 m_freem(m); 2021} 2022 2023static int 2024bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst, 2025 struct bridge_rtnode **brtp) 2026{ 2027 struct bridge_rtnode *brt; 2028 int error; 2029 2030 if (sc->sc_brtcnt >= sc->sc_brtmax) 2031 return ENOSPC; 2032 2033 /* 2034 * Allocate a new bridge forwarding node, and 2035 * initialize the expiration time and Ethernet 2036 * address. 2037 */ 2038 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT); 2039 if (brt == NULL) 2040 return ENOMEM; 2041 2042 memset(brt, 0, sizeof(*brt)); 2043 brt->brt_expire = time_uptime + sc->sc_brttimeout; 2044 brt->brt_flags = IFBAF_DYNAMIC; 2045 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); 2046 PSLIST_ENTRY_INIT(brt, brt_list); 2047 PSLIST_ENTRY_INIT(brt, brt_hash); 2048 2049 BRIDGE_RT_LOCK(sc); 2050 error = bridge_rtnode_insert(sc, brt); 2051 BRIDGE_RT_UNLOCK(sc); 2052 2053 if (error != 0) { 2054 pool_put(&bridge_rtnode_pool, brt); 2055 return error; 2056 } 2057 2058 *brtp = brt; 2059 return 0; 2060} 2061 2062/* 2063 * bridge_rtupdate: 2064 * 2065 * Add a bridge routing entry. 2066 */ 2067static int 2068bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, 2069 struct ifnet *dst_if, int setflags, uint8_t flags) 2070{ 2071 struct bridge_rtnode *brt; 2072 int s; 2073 2074again: 2075 /* 2076 * A route for this destination might already exist. If so, 2077 * update it, otherwise create a new one. 2078 */ 2079 BRIDGE_RT_RENTER(s); 2080 brt = bridge_rtnode_lookup(sc, dst); 2081 2082 if (brt != NULL) { 2083 brt->brt_ifp = dst_if; 2084 if (setflags) { 2085 brt->brt_flags = flags; 2086 if (flags & IFBAF_STATIC) 2087 brt->brt_expire = 0; 2088 else 2089 brt->brt_expire = time_uptime + sc->sc_brttimeout; 2090 } else { 2091 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 2092 brt->brt_expire = time_uptime + sc->sc_brttimeout; 2093 } 2094 } 2095 BRIDGE_RT_REXIT(s); 2096 2097 if (brt == NULL) { 2098 int r; 2099 2100 r = bridge_rtalloc(sc, dst, &brt); 2101 if (r != 0) 2102 return r; 2103 goto again; 2104 } 2105 2106 return 0; 2107} 2108 2109/* 2110 * bridge_rtlookup: 2111 * 2112 * Lookup the destination interface for an address. 2113 */ 2114static struct ifnet * 2115bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr) 2116{ 2117 struct bridge_rtnode *brt; 2118 struct ifnet *ifs = NULL; 2119 int s; 2120 2121 BRIDGE_RT_RENTER(s); 2122 brt = bridge_rtnode_lookup(sc, addr); 2123 if (brt != NULL) 2124 ifs = brt->brt_ifp; 2125 BRIDGE_RT_REXIT(s); 2126 2127 return ifs; 2128} 2129 2130typedef bool (*bridge_iterate_cb_t) 2131 (struct bridge_softc *, struct bridge_rtnode *, bool *, void *); 2132 2133/* 2134 * bridge_rtlist_iterate_remove: 2135 * 2136 * It iterates on sc->sc_rtlist and removes rtnodes of it which func 2137 * callback judges to remove. Removals of rtnodes are done in a manner 2138 * of pserialize. To this end, all kmem_* operations are placed out of 2139 * mutexes. 2140 */ 2141static void 2142bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg) 2143{ 2144 struct bridge_rtnode *brt; 2145 struct bridge_rtnode **brt_list; 2146 int i, count; 2147 2148retry: 2149 count = sc->sc_brtcnt; 2150 if (count == 0) 2151 return; 2152 brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP); 2153 2154 BRIDGE_RT_LOCK(sc); 2155 if (__predict_false(sc->sc_brtcnt > count)) { 2156 /* The rtnodes increased, we need more memory */ 2157 BRIDGE_RT_UNLOCK(sc); 2158 kmem_free(brt_list, sizeof(*brt_list) * count); 2159 goto retry; 2160 } 2161 2162 i = 0; 2163 /* 2164 * We don't need to use a _SAFE variant here because we know 2165 * that a removed item keeps its next pointer as-is thanks to 2166 * pslist(9) and isn't freed in the loop. 2167 */ 2168 BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) { 2169 bool need_break = false; 2170 if (func(sc, brt, &need_break, arg)) { 2171 bridge_rtnode_remove(sc, brt); 2172 brt_list[i++] = brt; 2173 } 2174 if (need_break) 2175 break; 2176 } 2177 2178 if (i > 0) 2179 BRIDGE_RT_PSZ_PERFORM(sc); 2180 BRIDGE_RT_UNLOCK(sc); 2181 2182 while (--i >= 0) 2183 bridge_rtnode_destroy(brt_list[i]); 2184 2185 kmem_free(brt_list, sizeof(*brt_list) * count); 2186} 2187 2188static bool 2189bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt, 2190 bool *need_break, void *arg) 2191{ 2192 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 2193 /* Take into account of the subsequent removal */ 2194 if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax) 2195 *need_break = true; 2196 return true; 2197 } else 2198 return false; 2199} 2200 2201static void 2202bridge_rttrim0(struct bridge_softc *sc) 2203{ 2204 bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL); 2205} 2206 2207/* 2208 * bridge_rttrim: 2209 * 2210 * Trim the routine table so that we have a number 2211 * of routing entries less than or equal to the 2212 * maximum number. 2213 */ 2214static void 2215bridge_rttrim(struct bridge_softc *sc) 2216{ 2217 2218 /* Make sure we actually need to do this. */ 2219 if (sc->sc_brtcnt <= sc->sc_brtmax) 2220 return; 2221 2222 /* Force an aging cycle; this might trim enough addresses. */ 2223 bridge_rtage(sc); 2224 if (sc->sc_brtcnt <= sc->sc_brtmax) 2225 return; 2226 2227 bridge_rttrim0(sc); 2228 2229 return; 2230} 2231 2232/* 2233 * bridge_timer: 2234 * 2235 * Aging timer for the bridge. 2236 */ 2237static void 2238bridge_timer(void *arg) 2239{ 2240 struct bridge_softc *sc = arg; 2241 2242 workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL); 2243} 2244 2245static void 2246bridge_rtage_work(struct work *wk, void *arg) 2247{ 2248 struct bridge_softc *sc = arg; 2249 2250 KASSERT(wk == &sc->sc_rtage_wk); 2251 2252 bridge_rtage(sc); 2253 2254 if (sc->sc_if.if_flags & IFF_RUNNING) 2255 callout_reset(&sc->sc_brcallout, 2256 bridge_rtable_prune_period * hz, bridge_timer, sc); 2257} 2258 2259static bool 2260bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt, 2261 bool *need_break, void *arg) 2262{ 2263 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 2264 time_uptime >= brt->brt_expire) 2265 return true; 2266 else 2267 return false; 2268} 2269 2270/* 2271 * bridge_rtage: 2272 * 2273 * Perform an aging cycle. 2274 */ 2275static void 2276bridge_rtage(struct bridge_softc *sc) 2277{ 2278 bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL); 2279} 2280 2281 2282static bool 2283bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt, 2284 bool *need_break, void *arg) 2285{ 2286 int full = *(int*)arg; 2287 2288 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 2289 return true; 2290 else 2291 return false; 2292} 2293 2294/* 2295 * bridge_rtflush: 2296 * 2297 * Remove all dynamic addresses from the bridge. 2298 */ 2299static void 2300bridge_rtflush(struct bridge_softc *sc, int full) 2301{ 2302 bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full); 2303} 2304 2305/* 2306 * bridge_rtdaddr: 2307 * 2308 * Remove an address from the table. 2309 */ 2310static int 2311bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr) 2312{ 2313 struct bridge_rtnode *brt; 2314 2315 BRIDGE_RT_LOCK(sc); 2316 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) { 2317 BRIDGE_RT_UNLOCK(sc); 2318 return ENOENT; 2319 } 2320 bridge_rtnode_remove(sc, brt); 2321 BRIDGE_RT_PSZ_PERFORM(sc); 2322 BRIDGE_RT_UNLOCK(sc); 2323 2324 bridge_rtnode_destroy(brt); 2325 2326 return 0; 2327} 2328 2329/* 2330 * bridge_rtdelete: 2331 * 2332 * Delete routes to a speicifc member interface. 2333 */ 2334static void 2335bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp) 2336{ 2337 struct bridge_rtnode *brt; 2338 2339 /* XXX pserialize_perform for each entry is slow */ 2340again: 2341 BRIDGE_RT_LOCK(sc); 2342 BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) { 2343 if (brt->brt_ifp == ifp) 2344 break; 2345 } 2346 if (brt == NULL) { 2347 BRIDGE_RT_UNLOCK(sc); 2348 return; 2349 } 2350 bridge_rtnode_remove(sc, brt); 2351 BRIDGE_RT_PSZ_PERFORM(sc); 2352 BRIDGE_RT_UNLOCK(sc); 2353 2354 bridge_rtnode_destroy(brt); 2355 2356 goto again; 2357} 2358 2359/* 2360 * bridge_rtable_init: 2361 * 2362 * Initialize the route table for this bridge. 2363 */ 2364static void 2365bridge_rtable_init(struct bridge_softc *sc) 2366{ 2367 int i; 2368 2369 sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE, 2370 KM_SLEEP); 2371 2372 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++) 2373 PSLIST_INIT(&sc->sc_rthash[i]); 2374 2375 sc->sc_rthash_key = cprng_fast32(); 2376 2377 PSLIST_INIT(&sc->sc_rtlist); 2378 2379 sc->sc_rtlist_psz = pserialize_create(); 2380 sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET); 2381} 2382 2383/* 2384 * bridge_rtable_fini: 2385 * 2386 * Deconstruct the route table for this bridge. 2387 */ 2388static void 2389bridge_rtable_fini(struct bridge_softc *sc) 2390{ 2391 2392 kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE); 2393 mutex_obj_free(sc->sc_rtlist_lock); 2394 pserialize_destroy(sc->sc_rtlist_psz); 2395} 2396 2397/* 2398 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2399 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2400 */ 2401#define mix(a, b, c) \ 2402do { \ 2403 a -= b; a -= c; a ^= (c >> 13); \ 2404 b -= c; b -= a; b ^= (a << 8); \ 2405 c -= a; c -= b; c ^= (b >> 13); \ 2406 a -= b; a -= c; a ^= (c >> 12); \ 2407 b -= c; b -= a; b ^= (a << 16); \ 2408 c -= a; c -= b; c ^= (b >> 5); \ 2409 a -= b; a -= c; a ^= (c >> 3); \ 2410 b -= c; b -= a; b ^= (a << 10); \ 2411 c -= a; c -= b; c ^= (b >> 15); \ 2412} while (/*CONSTCOND*/0) 2413 2414static inline uint32_t 2415bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) 2416{ 2417 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; 2418 2419 b += addr[5] << 8; 2420 b += addr[4]; 2421 a += addr[3] << 24; 2422 a += addr[2] << 16; 2423 a += addr[1] << 8; 2424 a += addr[0]; 2425 2426 mix(a, b, c); 2427 2428 return (c & BRIDGE_RTHASH_MASK); 2429} 2430 2431#undef mix 2432 2433/* 2434 * bridge_rtnode_lookup: 2435 * 2436 * Look up a bridge route node for the specified destination. 2437 */ 2438static struct bridge_rtnode * 2439bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr) 2440{ 2441 struct bridge_rtnode *brt; 2442 uint32_t hash; 2443 int dir; 2444 2445 hash = bridge_rthash(sc, addr); 2446 BRIDGE_RTHASH_READER_FOREACH(brt, sc, hash) { 2447 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN); 2448 if (dir == 0) 2449 return brt; 2450 if (dir > 0) 2451 return NULL; 2452 } 2453 2454 return NULL; 2455} 2456 2457/* 2458 * bridge_rtnode_insert: 2459 * 2460 * Insert the specified bridge node into the route table. We 2461 * assume the entry is not already in the table. 2462 */ 2463static int 2464bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) 2465{ 2466 struct bridge_rtnode *lbrt, *prev = NULL; 2467 uint32_t hash; 2468 2469 KASSERT(BRIDGE_RT_LOCKED(sc)); 2470 2471 hash = bridge_rthash(sc, brt->brt_addr); 2472 BRIDGE_RTHASH_WRITER_FOREACH(lbrt, sc, hash) { 2473 int dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN); 2474 if (dir == 0) 2475 return EEXIST; 2476 if (dir > 0) 2477 break; 2478 prev = lbrt; 2479 } 2480 if (prev == NULL) 2481 BRIDGE_RTHASH_WRITER_INSERT_HEAD(sc, hash, brt); 2482 else 2483 BRIDGE_RTHASH_WRITER_INSERT_AFTER(prev, brt); 2484 2485 BRIDGE_RTLIST_WRITER_INSERT_HEAD(sc, brt); 2486 sc->sc_brtcnt++; 2487 2488 return 0; 2489} 2490 2491/* 2492 * bridge_rtnode_remove: 2493 * 2494 * Remove a bridge rtnode from the rthash and the rtlist of a bridge. 2495 */ 2496static void 2497bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt) 2498{ 2499 2500 KASSERT(BRIDGE_RT_LOCKED(sc)); 2501 2502 BRIDGE_RTHASH_WRITER_REMOVE(brt); 2503 BRIDGE_RTLIST_WRITER_REMOVE(brt); 2504 sc->sc_brtcnt--; 2505} 2506 2507/* 2508 * bridge_rtnode_destroy: 2509 * 2510 * Destroy a bridge rtnode. 2511 */ 2512static void 2513bridge_rtnode_destroy(struct bridge_rtnode *brt) 2514{ 2515 2516 PSLIST_ENTRY_DESTROY(brt, brt_list); 2517 PSLIST_ENTRY_DESTROY(brt, brt_hash); 2518 pool_put(&bridge_rtnode_pool, brt); 2519} 2520 2521#if defined(BRIDGE_IPF) 2522extern pfil_head_t *inet_pfil_hook; /* XXX */ 2523extern pfil_head_t *inet6_pfil_hook; /* XXX */ 2524 2525/* 2526 * Send bridge packets through IPF if they are one of the types IPF can deal 2527 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without 2528 * question.) 2529 */ 2530static int 2531bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir) 2532{ 2533 int snap, error; 2534 struct ether_header *eh1, eh2; 2535 struct llc llc1; 2536 uint16_t ether_type; 2537 2538 snap = 0; 2539 error = -1; /* Default error if not error == 0 */ 2540 eh1 = mtod(*mp, struct ether_header *); 2541 ether_type = ntohs(eh1->ether_type); 2542 2543 /* 2544 * Check for SNAP/LLC. 2545 */ 2546 if (ether_type < ETHERMTU) { 2547 struct llc *llc2 = (struct llc *)(eh1 + 1); 2548 2549 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && 2550 llc2->llc_dsap == LLC_SNAP_LSAP && 2551 llc2->llc_ssap == LLC_SNAP_LSAP && 2552 llc2->llc_control == LLC_UI) { 2553 ether_type = htons(llc2->llc_un.type_snap.ether_type); 2554 snap = 1; 2555 } 2556 } 2557 2558 /* 2559 * If we're trying to filter bridge traffic, don't look at anything 2560 * other than IP and ARP traffic. If the filter doesn't understand 2561 * IPv6, don't allow IPv6 through the bridge either. This is lame 2562 * since if we really wanted, say, an AppleTalk filter, we are hosed, 2563 * but of course we don't have an AppleTalk filter to begin with. 2564 * (Note that since IPF doesn't understand ARP it will pass *ALL* 2565 * ARP traffic.) 2566 */ 2567 switch (ether_type) { 2568 case ETHERTYPE_ARP: 2569 case ETHERTYPE_REVARP: 2570 return 0; /* Automatically pass */ 2571 case ETHERTYPE_IP: 2572# ifdef INET6 2573 case ETHERTYPE_IPV6: 2574# endif /* INET6 */ 2575 break; 2576 default: 2577 goto bad; 2578 } 2579 2580 /* Strip off the Ethernet header and keep a copy. */ 2581 m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2); 2582 m_adj(*mp, ETHER_HDR_LEN); 2583 2584 /* Strip off snap header, if present */ 2585 if (snap) { 2586 m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1); 2587 m_adj(*mp, sizeof(struct llc)); 2588 } 2589 2590 /* 2591 * Check basic packet sanity and run IPF through pfil. 2592 */ 2593 KASSERT(!cpu_intr_p()); 2594 switch (ether_type) 2595 { 2596 case ETHERTYPE_IP : 2597 error = bridge_ip_checkbasic(mp); 2598 if (error == 0) 2599 error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir); 2600 break; 2601# ifdef INET6 2602 case ETHERTYPE_IPV6 : 2603 error = bridge_ip6_checkbasic(mp); 2604 if (error == 0) 2605 error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir); 2606 break; 2607# endif 2608 default : 2609 error = 0; 2610 break; 2611 } 2612 2613 if (*mp == NULL) 2614 return error; 2615 if (error != 0) 2616 goto bad; 2617 2618 error = -1; 2619 2620 /* 2621 * Finally, put everything back the way it was and return 2622 */ 2623 if (snap) { 2624 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT); 2625 if (*mp == NULL) 2626 return error; 2627 bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc)); 2628 } 2629 2630 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT); 2631 if (*mp == NULL) 2632 return error; 2633 bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN); 2634 2635 return 0; 2636 2637 bad: 2638 m_freem(*mp); 2639 *mp = NULL; 2640 return error; 2641} 2642 2643/* 2644 * Perform basic checks on header size since 2645 * IPF assumes ip_input has already processed 2646 * it for it. Cut-and-pasted from ip_input.c. 2647 * Given how simple the IPv6 version is, 2648 * does the IPv4 version really need to be 2649 * this complicated? 2650 * 2651 * XXX Should we update ipstat here, or not? 2652 * XXX Right now we update ipstat but not 2653 * XXX csum_counter. 2654 */ 2655static int 2656bridge_ip_checkbasic(struct mbuf **mp) 2657{ 2658 struct mbuf *m = *mp; 2659 struct ip *ip; 2660 int len, hlen; 2661 2662 if (*mp == NULL) 2663 return -1; 2664 2665 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { 2666 if ((m = m_copyup(m, sizeof(struct ip), 2667 (max_linkhdr + 3) & ~3)) == NULL) { 2668 /* XXXJRT new stat, please */ 2669 ip_statinc(IP_STAT_TOOSMALL); 2670 goto bad; 2671 } 2672 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 2673 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 2674 ip_statinc(IP_STAT_TOOSMALL); 2675 goto bad; 2676 } 2677 } 2678 ip = mtod(m, struct ip *); 2679 if (ip == NULL) goto bad; 2680 2681 if (ip->ip_v != IPVERSION) { 2682 ip_statinc(IP_STAT_BADVERS); 2683 goto bad; 2684 } 2685 hlen = ip->ip_hl << 2; 2686 if (hlen < sizeof(struct ip)) { /* minimum header length */ 2687 ip_statinc(IP_STAT_BADHLEN); 2688 goto bad; 2689 } 2690 if (hlen > m->m_len) { 2691 if ((m = m_pullup(m, hlen)) == 0) { 2692 ip_statinc(IP_STAT_BADHLEN); 2693 goto bad; 2694 } 2695 ip = mtod(m, struct ip *); 2696 if (ip == NULL) goto bad; 2697 } 2698 2699 switch (m->m_pkthdr.csum_flags & 2700 ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) | 2701 M_CSUM_IPv4_BAD)) { 2702 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 2703 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */ 2704 goto bad; 2705 2706 case M_CSUM_IPv4: 2707 /* Checksum was okay. */ 2708 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */ 2709 break; 2710 2711 default: 2712 /* Must compute it ourselves. */ 2713 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */ 2714 if (in_cksum(m, hlen) != 0) 2715 goto bad; 2716 break; 2717 } 2718 2719 /* Retrieve the packet length. */ 2720 len = ntohs(ip->ip_len); 2721 2722 /* 2723 * Check for additional length bogosity 2724 */ 2725 if (len < hlen) { 2726 ip_statinc(IP_STAT_BADLEN); 2727 goto bad; 2728 } 2729 2730 /* 2731 * Check that the amount of data in the buffers 2732 * is as at least much as the IP header would have us expect. 2733 * Drop packet if shorter than we expect. 2734 */ 2735 if (m->m_pkthdr.len < len) { 2736 ip_statinc(IP_STAT_TOOSHORT); 2737 goto bad; 2738 } 2739 2740 /* Checks out, proceed */ 2741 *mp = m; 2742 return 0; 2743 2744 bad: 2745 *mp = m; 2746 return -1; 2747} 2748 2749# ifdef INET6 2750/* 2751 * Same as above, but for IPv6. 2752 * Cut-and-pasted from ip6_input.c. 2753 * XXX Should we update ip6stat, or not? 2754 */ 2755static int 2756bridge_ip6_checkbasic(struct mbuf **mp) 2757{ 2758 struct mbuf *m = *mp; 2759 struct ip6_hdr *ip6; 2760 2761 /* 2762 * If the IPv6 header is not aligned, slurp it up into a new 2763 * mbuf with space for link headers, in the event we forward 2764 * it. Otherwise, if it is aligned, make sure the entire base 2765 * IPv6 header is in the first mbuf of the chain. 2766 */ 2767 if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) { 2768 struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m); 2769 if ((m = m_copyup(m, sizeof(struct ip6_hdr), 2770 (max_linkhdr + 3) & ~3)) == NULL) { 2771 /* XXXJRT new stat, please */ 2772 ip6_statinc(IP6_STAT_TOOSMALL); 2773 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 2774 goto bad; 2775 } 2776 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { 2777 struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m); 2778 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 2779 ip6_statinc(IP6_STAT_TOOSMALL); 2780 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 2781 goto bad; 2782 } 2783 } 2784 2785 ip6 = mtod(m, struct ip6_hdr *); 2786 2787 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 2788 ip6_statinc(IP6_STAT_BADVERS); 2789 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr); 2790 goto bad; 2791 } 2792 2793 /* Checks out, proceed */ 2794 *mp = m; 2795 return 0; 2796 2797 bad: 2798 *mp = m; 2799 return -1; 2800} 2801# endif /* INET6 */ 2802#endif /* BRIDGE_IPF */ 2803