1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc 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 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 61 * POSSIBILITY OF SUCH DAMAGE. 62 * 63 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp 64 */ 65 66 /* 67 * Network interface bridge support. 68 * 69 * TODO: 70 * 71 * - Currently only supports Ethernet-like interfaces (Ethernet, 72 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way 73 * to bridge other types of interfaces (FDDI-FDDI, and maybe 74 * consider heterogenous bridges). 75 */ 76 77 #include <sys/cdefs.h> 78 __FBSDID("$FreeBSD$"); 79 80 #include "opt_inet.h" 81 #include "opt_inet6.h" 82 83 #include <sys/param.h> 84 #include <sys/eventhandler.h> 85 #include <sys/mbuf.h> 86 #include <sys/malloc.h> 87 #include <sys/protosw.h> 88 #include <sys/systm.h> 89 #include <sys/jail.h> 90 #include <sys/time.h> 91 #include <sys/socket.h> /* for net/if.h */ 92 #include <sys/sockio.h> 93 #include <sys/ctype.h> /* string functions */ 94 #include <sys/kernel.h> 95 #include <sys/random.h> 96 #include <sys/syslog.h> 97 #include <sys/sysctl.h> 98 #include <vm/uma.h> 99 #include <sys/module.h> 100 #include <sys/priv.h> 101 #include <sys/proc.h> 102 #include <sys/lock.h> 103 #include <sys/mutex.h> 104 105 #include <net/bpf.h> 106 #include <net/if.h> 107 #include <net/if_clone.h> 108 #include <net/if_dl.h> 109 #include <net/if_types.h> 110 #include <net/if_var.h> 111 #include <net/pfil.h> 112 #include <net/vnet.h> 113 114 #include <netinet/in.h> 115 #include <netinet/in_systm.h> 116 #include <netinet/in_var.h> 117 #include <netinet/ip.h> 118 #include <netinet/ip_var.h> 119 #ifdef INET6 120 #include <netinet/ip6.h> 121 #include <netinet6/ip6_var.h> 122 #include <netinet6/in6_ifattach.h> 123 #endif 124 #if defined(INET) || defined(INET6) 125 #include <netinet/ip_carp.h> 126 #endif 127 #include <machine/in_cksum.h> 128 #include <netinet/if_ether.h> 129 #include <net/bridgestp.h> 130 #include <net/if_bridgevar.h> 131 #include <net/if_llc.h> 132 #include <net/if_vlan_var.h> 133 134 #include <net/route.h> 135 136 /* 137 * Size of the route hash table. Must be a power of two. 138 */ 139 #ifndef BRIDGE_RTHASH_SIZE 140 #define BRIDGE_RTHASH_SIZE 1024 141 #endif 142 143 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1) 144 145 /* 146 * Default maximum number of addresses to cache. 147 */ 148 #ifndef BRIDGE_RTABLE_MAX 149 #define BRIDGE_RTABLE_MAX 2000 150 #endif 151 152 /* 153 * Timeout (in seconds) for entries learned dynamically. 154 */ 155 #ifndef BRIDGE_RTABLE_TIMEOUT 156 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ 157 #endif 158 159 /* 160 * Number of seconds between walks of the route list. 161 */ 162 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD 163 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) 164 #endif 165 166 /* 167 * List of capabilities to possibly mask on the member interface. 168 */ 169 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM) 170 171 /* 172 * List of capabilities to strip 173 */ 174 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO 175 176 /* 177 * Bridge interface list entry. 178 */ 179 struct bridge_iflist { 180 LIST_ENTRY(bridge_iflist) bif_next; 181 struct ifnet *bif_ifp; /* member if */ 182 struct bstp_port bif_stp; /* STP state */ 183 uint32_t bif_flags; /* member if flags */ 184 int bif_savedcaps; /* saved capabilities */ 185 uint32_t bif_addrmax; /* max # of addresses */ 186 uint32_t bif_addrcnt; /* cur. # of addresses */ 187 uint32_t bif_addrexceeded;/* # of address violations */ 188 }; 189 190 /* 191 * Bridge route node. 192 */ 193 struct bridge_rtnode { 194 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */ 195 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */ 196 struct bridge_iflist *brt_dst; /* destination if */ 197 unsigned long brt_expire; /* expiration time */ 198 uint8_t brt_flags; /* address flags */ 199 uint8_t brt_addr[ETHER_ADDR_LEN]; 200 uint16_t brt_vlan; /* vlan id */ 201 }; 202 #define brt_ifp brt_dst->bif_ifp 203 204 /* 205 * Software state for each bridge. 206 */ 207 struct bridge_softc { 208 struct ifnet *sc_ifp; /* make this an interface */ 209 LIST_ENTRY(bridge_softc) sc_list; 210 struct mtx sc_mtx; 211 struct cv sc_cv; 212 uint32_t sc_brtmax; /* max # of addresses */ 213 uint32_t sc_brtcnt; /* cur. # of addresses */ 214 uint32_t sc_brttimeout; /* rt timeout in seconds */ 215 struct callout sc_brcallout; /* bridge callout */ 216 uint32_t sc_iflist_ref; /* refcount for sc_iflist */ 217 uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */ 218 LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */ 219 LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */ 220 LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */ 221 uint32_t sc_rthash_key; /* key for hash */ 222 LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */ 223 struct bstp_state sc_stp; /* STP state */ 224 uint32_t sc_brtexceeded; /* # of cache drops */ 225 struct ifnet *sc_ifaddr; /* member mac copied from */ 226 u_char sc_defaddr[6]; /* Default MAC address */ 227 }; 228 229 static VNET_DEFINE(struct mtx, bridge_list_mtx); 230 #define V_bridge_list_mtx VNET(bridge_list_mtx) 231 eventhandler_tag bridge_detach_cookie = NULL; 232 233 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; 234 235 uma_zone_t bridge_rtnode_zone; 236 237 static int bridge_clone_create(struct if_clone *, int, caddr_t); 238 static void bridge_clone_destroy(struct ifnet *); 239 240 static int bridge_ioctl(struct ifnet *, u_long, caddr_t); 241 static void bridge_mutecaps(struct bridge_softc *); 242 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *, 243 int); 244 static void bridge_ifdetach(void *arg __unused, struct ifnet *); 245 static void bridge_init(void *); 246 static void bridge_dummynet(struct mbuf *, struct ifnet *); 247 static void bridge_stop(struct ifnet *, int); 248 static int bridge_transmit(struct ifnet *, struct mbuf *); 249 static void bridge_qflush(struct ifnet *); 250 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *); 251 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *, 252 struct rtentry *); 253 static int bridge_enqueue(struct bridge_softc *, struct ifnet *, 254 struct mbuf *); 255 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int); 256 257 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *, 258 struct mbuf *m); 259 260 static void bridge_timer(void *); 261 262 static void bridge_broadcast(struct bridge_softc *, struct ifnet *, 263 struct mbuf *, int); 264 static void bridge_span(struct bridge_softc *, struct mbuf *); 265 266 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *, 267 uint16_t, struct bridge_iflist *, int, uint8_t); 268 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *, 269 uint16_t); 270 static void bridge_rttrim(struct bridge_softc *); 271 static void bridge_rtage(struct bridge_softc *); 272 static void bridge_rtflush(struct bridge_softc *, int); 273 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *, 274 uint16_t); 275 276 static void bridge_rtable_init(struct bridge_softc *); 277 static void bridge_rtable_fini(struct bridge_softc *); 278 279 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *); 280 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, 281 const uint8_t *, uint16_t); 282 static int bridge_rtnode_insert(struct bridge_softc *, 283 struct bridge_rtnode *); 284 static void bridge_rtnode_destroy(struct bridge_softc *, 285 struct bridge_rtnode *); 286 static void bridge_rtable_expire(struct ifnet *, int); 287 static void bridge_state_change(struct ifnet *, int); 288 289 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, 290 const char *name); 291 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, 292 struct ifnet *ifp); 293 static void bridge_delete_member(struct bridge_softc *, 294 struct bridge_iflist *, int); 295 static void bridge_delete_span(struct bridge_softc *, 296 struct bridge_iflist *); 297 298 static int bridge_ioctl_add(struct bridge_softc *, void *); 299 static int bridge_ioctl_del(struct bridge_softc *, void *); 300 static int bridge_ioctl_gifflags(struct bridge_softc *, void *); 301 static int bridge_ioctl_sifflags(struct bridge_softc *, void *); 302 static int bridge_ioctl_scache(struct bridge_softc *, void *); 303 static int bridge_ioctl_gcache(struct bridge_softc *, void *); 304 static int bridge_ioctl_gifs(struct bridge_softc *, void *); 305 static int bridge_ioctl_rts(struct bridge_softc *, void *); 306 static int bridge_ioctl_saddr(struct bridge_softc *, void *); 307 static int bridge_ioctl_sto(struct bridge_softc *, void *); 308 static int bridge_ioctl_gto(struct bridge_softc *, void *); 309 static int bridge_ioctl_daddr(struct bridge_softc *, void *); 310 static int bridge_ioctl_flush(struct bridge_softc *, void *); 311 static int bridge_ioctl_gpri(struct bridge_softc *, void *); 312 static int bridge_ioctl_spri(struct bridge_softc *, void *); 313 static int bridge_ioctl_ght(struct bridge_softc *, void *); 314 static int bridge_ioctl_sht(struct bridge_softc *, void *); 315 static int bridge_ioctl_gfd(struct bridge_softc *, void *); 316 static int bridge_ioctl_sfd(struct bridge_softc *, void *); 317 static int bridge_ioctl_gma(struct bridge_softc *, void *); 318 static int bridge_ioctl_sma(struct bridge_softc *, void *); 319 static int bridge_ioctl_sifprio(struct bridge_softc *, void *); 320 static int bridge_ioctl_sifcost(struct bridge_softc *, void *); 321 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *); 322 static int bridge_ioctl_addspan(struct bridge_softc *, void *); 323 static int bridge_ioctl_delspan(struct bridge_softc *, void *); 324 static int bridge_ioctl_gbparam(struct bridge_softc *, void *); 325 static int bridge_ioctl_grte(struct bridge_softc *, void *); 326 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *); 327 static int bridge_ioctl_sproto(struct bridge_softc *, void *); 328 static int bridge_ioctl_stxhc(struct bridge_softc *, void *); 329 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *, 330 int); 331 static int bridge_ip_checkbasic(struct mbuf **mp); 332 #ifdef INET6 333 static int bridge_ip6_checkbasic(struct mbuf **mp); 334 #endif /* INET6 */ 335 static int bridge_fragment(struct ifnet *, struct mbuf *, 336 struct ether_header *, int, struct llc *); 337 static void bridge_linkstate(struct ifnet *ifp); 338 static void bridge_linkcheck(struct bridge_softc *sc); 339 340 extern void (*bridge_linkstate_p)(struct ifnet *ifp); 341 342 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */ 343 #define VLANTAGOF(_m) \ 344 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1 345 346 static struct bstp_cb_ops bridge_ops = { 347 .bcb_state = bridge_state_change, 348 .bcb_rtage = bridge_rtable_expire 349 }; 350 351 SYSCTL_DECL(_net_link); 352 static SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge"); 353 354 /* only pass IP[46] packets when pfil is enabled */ 355 static VNET_DEFINE(int, pfil_onlyip) = 1; 356 #define V_pfil_onlyip VNET(pfil_onlyip) 357 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, 358 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_onlyip), 0, 359 "Only pass IP packets when pfil is enabled"); 360 361 /* run pfil hooks on the bridge interface */ 362 static VNET_DEFINE(int, pfil_bridge) = 1; 363 #define V_pfil_bridge VNET(pfil_bridge) 364 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, 365 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_bridge), 0, 366 "Packet filter on the bridge interface"); 367 368 /* layer2 filter with ipfw */ 369 static VNET_DEFINE(int, pfil_ipfw); 370 #define V_pfil_ipfw VNET(pfil_ipfw) 371 372 /* layer2 ARP filter with ipfw */ 373 static VNET_DEFINE(int, pfil_ipfw_arp); 374 #define V_pfil_ipfw_arp VNET(pfil_ipfw_arp) 375 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, 376 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_ipfw_arp), 0, 377 "Filter ARP packets through IPFW layer2"); 378 379 /* run pfil hooks on the member interface */ 380 static VNET_DEFINE(int, pfil_member) = 1; 381 #define V_pfil_member VNET(pfil_member) 382 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, 383 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_member), 0, 384 "Packet filter on the member interface"); 385 386 /* run pfil hooks on the physical interface for locally destined packets */ 387 static VNET_DEFINE(int, pfil_local_phys); 388 #define V_pfil_local_phys VNET(pfil_local_phys) 389 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, 390 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_local_phys), 0, 391 "Packet filter on the physical interface for locally destined packets"); 392 393 /* log STP state changes */ 394 static VNET_DEFINE(int, log_stp); 395 #define V_log_stp VNET(log_stp) 396 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, 397 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(log_stp), 0, 398 "Log STP state changes"); 399 400 /* share MAC with first bridge member */ 401 static VNET_DEFINE(int, bridge_inherit_mac); 402 #define V_bridge_inherit_mac VNET(bridge_inherit_mac) 403 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, 404 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(bridge_inherit_mac), 0, 405 "Inherit MAC address from the first bridge member"); 406 407 static VNET_DEFINE(int, allow_llz_overlap) = 0; 408 #define V_allow_llz_overlap VNET(allow_llz_overlap) 409 SYSCTL_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap, 410 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(allow_llz_overlap), 0, 411 "Allow overlap of link-local scope " 412 "zones of a bridge interface and the member interfaces"); 413 414 struct bridge_control { 415 int (*bc_func)(struct bridge_softc *, void *); 416 int bc_argsize; 417 int bc_flags; 418 }; 419 420 #define BC_F_COPYIN 0x01 /* copy arguments in */ 421 #define BC_F_COPYOUT 0x02 /* copy arguments out */ 422 #define BC_F_SUSER 0x04 /* do super-user check */ 423 424 const struct bridge_control bridge_control_table[] = { 425 { bridge_ioctl_add, sizeof(struct ifbreq), 426 BC_F_COPYIN|BC_F_SUSER }, 427 { bridge_ioctl_del, sizeof(struct ifbreq), 428 BC_F_COPYIN|BC_F_SUSER }, 429 430 { bridge_ioctl_gifflags, sizeof(struct ifbreq), 431 BC_F_COPYIN|BC_F_COPYOUT }, 432 { bridge_ioctl_sifflags, sizeof(struct ifbreq), 433 BC_F_COPYIN|BC_F_SUSER }, 434 435 { bridge_ioctl_scache, sizeof(struct ifbrparam), 436 BC_F_COPYIN|BC_F_SUSER }, 437 { bridge_ioctl_gcache, sizeof(struct ifbrparam), 438 BC_F_COPYOUT }, 439 440 { bridge_ioctl_gifs, sizeof(struct ifbifconf), 441 BC_F_COPYIN|BC_F_COPYOUT }, 442 { bridge_ioctl_rts, sizeof(struct ifbaconf), 443 BC_F_COPYIN|BC_F_COPYOUT }, 444 445 { bridge_ioctl_saddr, sizeof(struct ifbareq), 446 BC_F_COPYIN|BC_F_SUSER }, 447 448 { bridge_ioctl_sto, sizeof(struct ifbrparam), 449 BC_F_COPYIN|BC_F_SUSER }, 450 { bridge_ioctl_gto, sizeof(struct ifbrparam), 451 BC_F_COPYOUT }, 452 453 { bridge_ioctl_daddr, sizeof(struct ifbareq), 454 BC_F_COPYIN|BC_F_SUSER }, 455 456 { bridge_ioctl_flush, sizeof(struct ifbreq), 457 BC_F_COPYIN|BC_F_SUSER }, 458 459 { bridge_ioctl_gpri, sizeof(struct ifbrparam), 460 BC_F_COPYOUT }, 461 { bridge_ioctl_spri, sizeof(struct ifbrparam), 462 BC_F_COPYIN|BC_F_SUSER }, 463 464 { bridge_ioctl_ght, sizeof(struct ifbrparam), 465 BC_F_COPYOUT }, 466 { bridge_ioctl_sht, sizeof(struct ifbrparam), 467 BC_F_COPYIN|BC_F_SUSER }, 468 469 { bridge_ioctl_gfd, sizeof(struct ifbrparam), 470 BC_F_COPYOUT }, 471 { bridge_ioctl_sfd, sizeof(struct ifbrparam), 472 BC_F_COPYIN|BC_F_SUSER }, 473 474 { bridge_ioctl_gma, sizeof(struct ifbrparam), 475 BC_F_COPYOUT }, 476 { bridge_ioctl_sma, sizeof(struct ifbrparam), 477 BC_F_COPYIN|BC_F_SUSER }, 478 479 { bridge_ioctl_sifprio, sizeof(struct ifbreq), 480 BC_F_COPYIN|BC_F_SUSER }, 481 482 { bridge_ioctl_sifcost, sizeof(struct ifbreq), 483 BC_F_COPYIN|BC_F_SUSER }, 484 485 { bridge_ioctl_addspan, sizeof(struct ifbreq), 486 BC_F_COPYIN|BC_F_SUSER }, 487 { bridge_ioctl_delspan, sizeof(struct ifbreq), 488 BC_F_COPYIN|BC_F_SUSER }, 489 490 { bridge_ioctl_gbparam, sizeof(struct ifbropreq), 491 BC_F_COPYOUT }, 492 493 { bridge_ioctl_grte, sizeof(struct ifbrparam), 494 BC_F_COPYOUT }, 495 496 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf), 497 BC_F_COPYIN|BC_F_COPYOUT }, 498 499 { bridge_ioctl_sproto, sizeof(struct ifbrparam), 500 BC_F_COPYIN|BC_F_SUSER }, 501 502 { bridge_ioctl_stxhc, sizeof(struct ifbrparam), 503 BC_F_COPYIN|BC_F_SUSER }, 504 505 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq), 506 BC_F_COPYIN|BC_F_SUSER }, 507 508 }; 509 const int bridge_control_table_size = nitems(bridge_control_table); 510 511 static VNET_DEFINE(LIST_HEAD(, bridge_softc), bridge_list); 512 #define V_bridge_list VNET(bridge_list) 513 #define BRIDGE_LIST_LOCK_INIT(x) mtx_init(&V_bridge_list_mtx, \ 514 "if_bridge list", NULL, MTX_DEF) 515 #define BRIDGE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_bridge_list_mtx) 516 #define BRIDGE_LIST_LOCK(x) mtx_lock(&V_bridge_list_mtx) 517 #define BRIDGE_LIST_UNLOCK(x) mtx_unlock(&V_bridge_list_mtx) 518 519 static VNET_DEFINE(struct if_clone *, bridge_cloner); 520 #define V_bridge_cloner VNET(bridge_cloner) 521 522 static const char bridge_name[] = "bridge"; 523 524 static void 525 vnet_bridge_init(const void *unused __unused) 526 { 527 528 BRIDGE_LIST_LOCK_INIT(); 529 LIST_INIT(&V_bridge_list); 530 V_bridge_cloner = if_clone_simple(bridge_name, 531 bridge_clone_create, bridge_clone_destroy, 0); 532 } 533 VNET_SYSINIT(vnet_bridge_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 534 vnet_bridge_init, NULL); 535 536 static void 537 vnet_bridge_uninit(const void *unused __unused) 538 { 539 540 if_clone_detach(V_bridge_cloner); 541 BRIDGE_LIST_LOCK_DESTROY(); 542 } 543 VNET_SYSUNINIT(vnet_bridge_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 544 vnet_bridge_uninit, NULL); 545 546 static int 547 bridge_modevent(module_t mod, int type, void *data) 548 { 549 550 switch (type) { 551 case MOD_LOAD: 552 bridge_rtnode_zone = uma_zcreate("bridge_rtnode", 553 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL, 554 UMA_ALIGN_PTR, 0); 555 bridge_input_p = bridge_input; 556 bridge_output_p = bridge_output; 557 bridge_dn_p = bridge_dummynet; 558 bridge_linkstate_p = bridge_linkstate; 559 bridge_detach_cookie = EVENTHANDLER_REGISTER( 560 ifnet_departure_event, bridge_ifdetach, NULL, 561 EVENTHANDLER_PRI_ANY); 562 break; 563 case MOD_UNLOAD: 564 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 565 bridge_detach_cookie); 566 uma_zdestroy(bridge_rtnode_zone); 567 bridge_input_p = NULL; 568 bridge_output_p = NULL; 569 bridge_dn_p = NULL; 570 bridge_linkstate_p = NULL; 571 break; 572 default: 573 return (EOPNOTSUPP); 574 } 575 return (0); 576 } 577 578 static moduledata_t bridge_mod = { 579 "if_bridge", 580 bridge_modevent, 581 0 582 }; 583 584 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 585 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1); 586 587 /* 588 * handler for net.link.bridge.ipfw 589 */ 590 static int 591 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS) 592 { 593 int enable = V_pfil_ipfw; 594 int error; 595 596 error = sysctl_handle_int(oidp, &enable, 0, req); 597 enable &= 1; 598 599 if (enable != V_pfil_ipfw) { 600 V_pfil_ipfw = enable; 601 602 /* 603 * Disable pfil so that ipfw doesnt run twice, if the user 604 * really wants both then they can re-enable pfil_bridge and/or 605 * pfil_member. Also allow non-ip packets as ipfw can filter by 606 * layer2 type. 607 */ 608 if (V_pfil_ipfw) { 609 V_pfil_onlyip = 0; 610 V_pfil_bridge = 0; 611 V_pfil_member = 0; 612 } 613 } 614 615 return (error); 616 } 617 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, 618 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET, 619 &VNET_NAME(pfil_ipfw), 0, &sysctl_pfil_ipfw, "I", 620 "Layer2 filter with IPFW"); 621 622 /* 623 * bridge_clone_create: 624 * 625 * Create a new bridge instance. 626 */ 627 static int 628 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params) 629 { 630 struct bridge_softc *sc, *sc2; 631 struct ifnet *bifp, *ifp; 632 int fb, retry; 633 unsigned long hostid; 634 635 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 636 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 637 if (ifp == NULL) { 638 free(sc, M_DEVBUF); 639 return (ENOSPC); 640 } 641 642 BRIDGE_LOCK_INIT(sc); 643 sc->sc_brtmax = BRIDGE_RTABLE_MAX; 644 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; 645 646 /* Initialize our routing table. */ 647 bridge_rtable_init(sc); 648 649 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0); 650 651 LIST_INIT(&sc->sc_iflist); 652 LIST_INIT(&sc->sc_spanlist); 653 654 ifp->if_softc = sc; 655 if_initname(ifp, bridge_name, unit); 656 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 657 ifp->if_ioctl = bridge_ioctl; 658 ifp->if_transmit = bridge_transmit; 659 ifp->if_qflush = bridge_qflush; 660 ifp->if_init = bridge_init; 661 ifp->if_type = IFT_BRIDGE; 662 663 /* 664 * Generate an ethernet address with a locally administered address. 665 * 666 * Since we are using random ethernet addresses for the bridge, it is 667 * possible that we might have address collisions, so make sure that 668 * this hardware address isn't already in use on another bridge. 669 * The first try uses the hostid and falls back to arc4rand(). 670 */ 671 fb = 0; 672 getcredhostid(curthread->td_ucred, &hostid); 673 do { 674 if (fb || hostid == 0) { 675 arc4rand(sc->sc_defaddr, ETHER_ADDR_LEN, 1); 676 sc->sc_defaddr[0] &= ~1;/* clear multicast bit */ 677 sc->sc_defaddr[0] |= 2; /* set the LAA bit */ 678 } else { 679 sc->sc_defaddr[0] = 0x2; 680 sc->sc_defaddr[1] = (hostid >> 24) & 0xff; 681 sc->sc_defaddr[2] = (hostid >> 16) & 0xff; 682 sc->sc_defaddr[3] = (hostid >> 8 ) & 0xff; 683 sc->sc_defaddr[4] = hostid & 0xff; 684 sc->sc_defaddr[5] = ifp->if_dunit & 0xff; 685 } 686 687 fb = 1; 688 retry = 0; 689 BRIDGE_LIST_LOCK(); 690 LIST_FOREACH(sc2, &V_bridge_list, sc_list) { 691 bifp = sc2->sc_ifp; 692 if (memcmp(sc->sc_defaddr, 693 IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) { 694 retry = 1; 695 break; 696 } 697 } 698 BRIDGE_LIST_UNLOCK(); 699 } while (retry == 1); 700 701 bstp_attach(&sc->sc_stp, &bridge_ops); 702 ether_ifattach(ifp, sc->sc_defaddr); 703 /* Now undo some of the damage... */ 704 ifp->if_baudrate = 0; 705 ifp->if_type = IFT_BRIDGE; 706 707 BRIDGE_LIST_LOCK(); 708 LIST_INSERT_HEAD(&V_bridge_list, sc, sc_list); 709 BRIDGE_LIST_UNLOCK(); 710 711 return (0); 712 } 713 714 /* 715 * bridge_clone_destroy: 716 * 717 * Destroy a bridge instance. 718 */ 719 static void 720 bridge_clone_destroy(struct ifnet *ifp) 721 { 722 struct bridge_softc *sc = ifp->if_softc; 723 struct bridge_iflist *bif; 724 725 BRIDGE_LOCK(sc); 726 727 bridge_stop(ifp, 1); 728 ifp->if_flags &= ~IFF_UP; 729 730 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL) 731 bridge_delete_member(sc, bif, 0); 732 733 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) { 734 bridge_delete_span(sc, bif); 735 } 736 737 BRIDGE_UNLOCK(sc); 738 739 callout_drain(&sc->sc_brcallout); 740 741 BRIDGE_LIST_LOCK(); 742 LIST_REMOVE(sc, sc_list); 743 BRIDGE_LIST_UNLOCK(); 744 745 bstp_detach(&sc->sc_stp); 746 ether_ifdetach(ifp); 747 if_free(ifp); 748 749 /* Tear down the routing table. */ 750 bridge_rtable_fini(sc); 751 752 BRIDGE_LOCK_DESTROY(sc); 753 free(sc, M_DEVBUF); 754 } 755 756 /* 757 * bridge_ioctl: 758 * 759 * Handle a control request from the operator. 760 */ 761 static int 762 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 763 { 764 struct bridge_softc *sc = ifp->if_softc; 765 struct ifreq *ifr = (struct ifreq *)data; 766 struct bridge_iflist *bif; 767 struct thread *td = curthread; 768 union { 769 struct ifbreq ifbreq; 770 struct ifbifconf ifbifconf; 771 struct ifbareq ifbareq; 772 struct ifbaconf ifbaconf; 773 struct ifbrparam ifbrparam; 774 struct ifbropreq ifbropreq; 775 } args; 776 struct ifdrv *ifd = (struct ifdrv *) data; 777 const struct bridge_control *bc; 778 int error = 0; 779 780 switch (cmd) { 781 782 case SIOCADDMULTI: 783 case SIOCDELMULTI: 784 break; 785 786 case SIOCGDRVSPEC: 787 case SIOCSDRVSPEC: 788 if (ifd->ifd_cmd >= bridge_control_table_size) { 789 error = EINVAL; 790 break; 791 } 792 bc = &bridge_control_table[ifd->ifd_cmd]; 793 794 if (cmd == SIOCGDRVSPEC && 795 (bc->bc_flags & BC_F_COPYOUT) == 0) { 796 error = EINVAL; 797 break; 798 } 799 else if (cmd == SIOCSDRVSPEC && 800 (bc->bc_flags & BC_F_COPYOUT) != 0) { 801 error = EINVAL; 802 break; 803 } 804 805 if (bc->bc_flags & BC_F_SUSER) { 806 error = priv_check(td, PRIV_NET_BRIDGE); 807 if (error) 808 break; 809 } 810 811 if (ifd->ifd_len != bc->bc_argsize || 812 ifd->ifd_len > sizeof(args)) { 813 error = EINVAL; 814 break; 815 } 816 817 bzero(&args, sizeof(args)); 818 if (bc->bc_flags & BC_F_COPYIN) { 819 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 820 if (error) 821 break; 822 } 823 824 BRIDGE_LOCK(sc); 825 error = (*bc->bc_func)(sc, &args); 826 BRIDGE_UNLOCK(sc); 827 if (error) 828 break; 829 830 if (bc->bc_flags & BC_F_COPYOUT) 831 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 832 833 break; 834 835 case SIOCSIFFLAGS: 836 if (!(ifp->if_flags & IFF_UP) && 837 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 838 /* 839 * If interface is marked down and it is running, 840 * then stop and disable it. 841 */ 842 BRIDGE_LOCK(sc); 843 bridge_stop(ifp, 1); 844 BRIDGE_UNLOCK(sc); 845 } else if ((ifp->if_flags & IFF_UP) && 846 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 847 /* 848 * If interface is marked up and it is stopped, then 849 * start it. 850 */ 851 (*ifp->if_init)(sc); 852 } 853 break; 854 855 case SIOCSIFMTU: 856 if (ifr->ifr_mtu < 576) { 857 error = EINVAL; 858 break; 859 } 860 if (LIST_EMPTY(&sc->sc_iflist)) { 861 sc->sc_ifp->if_mtu = ifr->ifr_mtu; 862 break; 863 } 864 BRIDGE_LOCK(sc); 865 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 866 if (bif->bif_ifp->if_mtu != ifr->ifr_mtu) { 867 log(LOG_NOTICE, "%s: invalid MTU: %u(%s)" 868 " != %d\n", sc->sc_ifp->if_xname, 869 bif->bif_ifp->if_mtu, 870 bif->bif_ifp->if_xname, ifr->ifr_mtu); 871 error = EINVAL; 872 break; 873 } 874 } 875 if (!error) 876 sc->sc_ifp->if_mtu = ifr->ifr_mtu; 877 BRIDGE_UNLOCK(sc); 878 break; 879 default: 880 /* 881 * drop the lock as ether_ioctl() will call bridge_start() and 882 * cause the lock to be recursed. 883 */ 884 error = ether_ioctl(ifp, cmd, data); 885 break; 886 } 887 888 return (error); 889 } 890 891 /* 892 * bridge_mutecaps: 893 * 894 * Clear or restore unwanted capabilities on the member interface 895 */ 896 static void 897 bridge_mutecaps(struct bridge_softc *sc) 898 { 899 struct bridge_iflist *bif; 900 int enabled, mask; 901 902 /* Initial bitmask of capabilities to test */ 903 mask = BRIDGE_IFCAPS_MASK; 904 905 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 906 /* Every member must support it or its disabled */ 907 mask &= bif->bif_savedcaps; 908 } 909 910 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 911 enabled = bif->bif_ifp->if_capenable; 912 enabled &= ~BRIDGE_IFCAPS_STRIP; 913 /* strip off mask bits and enable them again if allowed */ 914 enabled &= ~BRIDGE_IFCAPS_MASK; 915 enabled |= mask; 916 bridge_set_ifcap(sc, bif, enabled); 917 } 918 919 } 920 921 static void 922 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set) 923 { 924 struct ifnet *ifp = bif->bif_ifp; 925 struct ifreq ifr; 926 int error; 927 928 bzero(&ifr, sizeof(ifr)); 929 ifr.ifr_reqcap = set; 930 931 if (ifp->if_capenable != set) { 932 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); 933 if (error) 934 if_printf(sc->sc_ifp, 935 "error setting interface capabilities on %s\n", 936 ifp->if_xname); 937 } 938 } 939 940 /* 941 * bridge_lookup_member: 942 * 943 * Lookup a bridge member interface. 944 */ 945 static struct bridge_iflist * 946 bridge_lookup_member(struct bridge_softc *sc, const char *name) 947 { 948 struct bridge_iflist *bif; 949 struct ifnet *ifp; 950 951 BRIDGE_LOCK_ASSERT(sc); 952 953 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 954 ifp = bif->bif_ifp; 955 if (strcmp(ifp->if_xname, name) == 0) 956 return (bif); 957 } 958 959 return (NULL); 960 } 961 962 /* 963 * bridge_lookup_member_if: 964 * 965 * Lookup a bridge member interface by ifnet*. 966 */ 967 static struct bridge_iflist * 968 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp) 969 { 970 struct bridge_iflist *bif; 971 972 BRIDGE_LOCK_ASSERT(sc); 973 974 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 975 if (bif->bif_ifp == member_ifp) 976 return (bif); 977 } 978 979 return (NULL); 980 } 981 982 /* 983 * bridge_delete_member: 984 * 985 * Delete the specified member interface. 986 */ 987 static void 988 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif, 989 int gone) 990 { 991 struct ifnet *ifs = bif->bif_ifp; 992 struct ifnet *fif = NULL; 993 994 BRIDGE_LOCK_ASSERT(sc); 995 996 if (bif->bif_flags & IFBIF_STP) 997 bstp_disable(&bif->bif_stp); 998 999 ifs->if_bridge = NULL; 1000 BRIDGE_XLOCK(sc); 1001 LIST_REMOVE(bif, bif_next); 1002 BRIDGE_XDROP(sc); 1003 1004 /* 1005 * If removing the interface that gave the bridge its mac address, set 1006 * the mac address of the bridge to the address of the next member, or 1007 * to its default address if no members are left. 1008 */ 1009 if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) { 1010 if (LIST_EMPTY(&sc->sc_iflist)) { 1011 bcopy(sc->sc_defaddr, 1012 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1013 sc->sc_ifaddr = NULL; 1014 } else { 1015 fif = LIST_FIRST(&sc->sc_iflist)->bif_ifp; 1016 bcopy(IF_LLADDR(fif), 1017 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1018 sc->sc_ifaddr = fif; 1019 } 1020 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp); 1021 } 1022 1023 bridge_linkcheck(sc); 1024 bridge_mutecaps(sc); /* recalcuate now this interface is removed */ 1025 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL); 1026 KASSERT(bif->bif_addrcnt == 0, 1027 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt)); 1028 1029 BRIDGE_UNLOCK(sc); 1030 if (!gone) { 1031 switch (ifs->if_type) { 1032 case IFT_ETHER: 1033 case IFT_L2VLAN: 1034 /* 1035 * Take the interface out of promiscuous mode. 1036 */ 1037 (void) ifpromisc(ifs, 0); 1038 break; 1039 1040 case IFT_GIF: 1041 break; 1042 1043 default: 1044 #ifdef DIAGNOSTIC 1045 panic("bridge_delete_member: impossible"); 1046 #endif 1047 break; 1048 } 1049 /* reneable any interface capabilities */ 1050 bridge_set_ifcap(sc, bif, bif->bif_savedcaps); 1051 } 1052 bstp_destroy(&bif->bif_stp); /* prepare to free */ 1053 BRIDGE_LOCK(sc); 1054 free(bif, M_DEVBUF); 1055 } 1056 1057 /* 1058 * bridge_delete_span: 1059 * 1060 * Delete the specified span interface. 1061 */ 1062 static void 1063 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif) 1064 { 1065 BRIDGE_LOCK_ASSERT(sc); 1066 1067 KASSERT(bif->bif_ifp->if_bridge == NULL, 1068 ("%s: not a span interface", __func__)); 1069 1070 LIST_REMOVE(bif, bif_next); 1071 free(bif, M_DEVBUF); 1072 } 1073 1074 static int 1075 bridge_ioctl_add(struct bridge_softc *sc, void *arg) 1076 { 1077 struct ifbreq *req = arg; 1078 struct bridge_iflist *bif = NULL; 1079 struct ifnet *ifs; 1080 int error = 0; 1081 1082 ifs = ifunit(req->ifbr_ifsname); 1083 if (ifs == NULL) 1084 return (ENOENT); 1085 if (ifs->if_ioctl == NULL) /* must be supported */ 1086 return (EINVAL); 1087 1088 /* If it's in the span list, it can't be a member. */ 1089 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1090 if (ifs == bif->bif_ifp) 1091 return (EBUSY); 1092 1093 if (ifs->if_bridge == sc) 1094 return (EEXIST); 1095 1096 if (ifs->if_bridge != NULL) 1097 return (EBUSY); 1098 1099 switch (ifs->if_type) { 1100 case IFT_ETHER: 1101 case IFT_L2VLAN: 1102 case IFT_GIF: 1103 /* permitted interface types */ 1104 break; 1105 default: 1106 return (EINVAL); 1107 } 1108 1109 #ifdef INET6 1110 /* 1111 * Two valid inet6 addresses with link-local scope must not be 1112 * on the parent interface and the member interfaces at the 1113 * same time. This restriction is needed to prevent violation 1114 * of link-local scope zone. Attempts to add a member 1115 * interface which has inet6 addresses when the parent has 1116 * inet6 triggers removal of all inet6 addresses on the member 1117 * interface. 1118 */ 1119 1120 /* Check if the parent interface has a link-local scope addr. */ 1121 if (V_allow_llz_overlap == 0 && 1122 in6ifa_llaonifp(sc->sc_ifp) != NULL) { 1123 /* 1124 * If any, remove all inet6 addresses from the member 1125 * interfaces. 1126 */ 1127 BRIDGE_XLOCK(sc); 1128 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1129 if (in6ifa_llaonifp(bif->bif_ifp)) { 1130 BRIDGE_UNLOCK(sc); 1131 in6_ifdetach(bif->bif_ifp); 1132 BRIDGE_LOCK(sc); 1133 if_printf(sc->sc_ifp, 1134 "IPv6 addresses on %s have been removed " 1135 "before adding it as a member to prevent " 1136 "IPv6 address scope violation.\n", 1137 bif->bif_ifp->if_xname); 1138 } 1139 } 1140 BRIDGE_XDROP(sc); 1141 if (in6ifa_llaonifp(ifs)) { 1142 BRIDGE_UNLOCK(sc); 1143 in6_ifdetach(ifs); 1144 BRIDGE_LOCK(sc); 1145 if_printf(sc->sc_ifp, 1146 "IPv6 addresses on %s have been removed " 1147 "before adding it as a member to prevent " 1148 "IPv6 address scope violation.\n", 1149 ifs->if_xname); 1150 } 1151 } 1152 #endif 1153 /* Allow the first Ethernet member to define the MTU */ 1154 if (LIST_EMPTY(&sc->sc_iflist)) 1155 sc->sc_ifp->if_mtu = ifs->if_mtu; 1156 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) { 1157 if_printf(sc->sc_ifp, "invalid MTU: %u(%s) != %u\n", 1158 ifs->if_mtu, ifs->if_xname, sc->sc_ifp->if_mtu); 1159 return (EINVAL); 1160 } 1161 1162 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO); 1163 if (bif == NULL) 1164 return (ENOMEM); 1165 1166 bif->bif_ifp = ifs; 1167 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER; 1168 bif->bif_savedcaps = ifs->if_capenable; 1169 1170 /* 1171 * Assign the interface's MAC address to the bridge if it's the first 1172 * member and the MAC address of the bridge has not been changed from 1173 * the default randomly generated one. 1174 */ 1175 if (V_bridge_inherit_mac && LIST_EMPTY(&sc->sc_iflist) && 1176 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr, ETHER_ADDR_LEN)) { 1177 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1178 sc->sc_ifaddr = ifs; 1179 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp); 1180 } 1181 1182 ifs->if_bridge = sc; 1183 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp); 1184 /* 1185 * XXX: XLOCK HERE!?! 1186 * 1187 * NOTE: insert_***HEAD*** should be safe for the traversals. 1188 */ 1189 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next); 1190 1191 /* Set interface capabilities to the intersection set of all members */ 1192 bridge_mutecaps(sc); 1193 bridge_linkcheck(sc); 1194 1195 /* Place the interface into promiscuous mode */ 1196 switch (ifs->if_type) { 1197 case IFT_ETHER: 1198 case IFT_L2VLAN: 1199 BRIDGE_UNLOCK(sc); 1200 error = ifpromisc(ifs, 1); 1201 BRIDGE_LOCK(sc); 1202 break; 1203 } 1204 1205 if (error) { 1206 bridge_delete_member(sc, bif, 0); 1207 free(bif, M_DEVBUF); 1208 } 1209 return (error); 1210 } 1211 1212 static int 1213 bridge_ioctl_del(struct bridge_softc *sc, void *arg) 1214 { 1215 struct ifbreq *req = arg; 1216 struct bridge_iflist *bif; 1217 1218 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1219 if (bif == NULL) 1220 return (ENOENT); 1221 1222 bridge_delete_member(sc, bif, 0); 1223 1224 return (0); 1225 } 1226 1227 static int 1228 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) 1229 { 1230 struct ifbreq *req = arg; 1231 struct bridge_iflist *bif; 1232 struct bstp_port *bp; 1233 1234 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1235 if (bif == NULL) 1236 return (ENOENT); 1237 1238 bp = &bif->bif_stp; 1239 req->ifbr_ifsflags = bif->bif_flags; 1240 req->ifbr_state = bp->bp_state; 1241 req->ifbr_priority = bp->bp_priority; 1242 req->ifbr_path_cost = bp->bp_path_cost; 1243 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff; 1244 req->ifbr_proto = bp->bp_protover; 1245 req->ifbr_role = bp->bp_role; 1246 req->ifbr_stpflags = bp->bp_flags; 1247 req->ifbr_addrcnt = bif->bif_addrcnt; 1248 req->ifbr_addrmax = bif->bif_addrmax; 1249 req->ifbr_addrexceeded = bif->bif_addrexceeded; 1250 1251 /* Copy STP state options as flags */ 1252 if (bp->bp_operedge) 1253 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE; 1254 if (bp->bp_flags & BSTP_PORT_AUTOEDGE) 1255 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE; 1256 if (bp->bp_ptp_link) 1257 req->ifbr_ifsflags |= IFBIF_BSTP_PTP; 1258 if (bp->bp_flags & BSTP_PORT_AUTOPTP) 1259 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP; 1260 if (bp->bp_flags & BSTP_PORT_ADMEDGE) 1261 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE; 1262 if (bp->bp_flags & BSTP_PORT_ADMCOST) 1263 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST; 1264 return (0); 1265 } 1266 1267 static int 1268 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) 1269 { 1270 struct ifbreq *req = arg; 1271 struct bridge_iflist *bif; 1272 struct bstp_port *bp; 1273 int error; 1274 1275 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1276 if (bif == NULL) 1277 return (ENOENT); 1278 bp = &bif->bif_stp; 1279 1280 if (req->ifbr_ifsflags & IFBIF_SPAN) 1281 /* SPAN is readonly */ 1282 return (EINVAL); 1283 1284 if (req->ifbr_ifsflags & IFBIF_STP) { 1285 if ((bif->bif_flags & IFBIF_STP) == 0) { 1286 error = bstp_enable(&bif->bif_stp); 1287 if (error) 1288 return (error); 1289 } 1290 } else { 1291 if ((bif->bif_flags & IFBIF_STP) != 0) 1292 bstp_disable(&bif->bif_stp); 1293 } 1294 1295 /* Pass on STP flags */ 1296 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0); 1297 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0); 1298 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0); 1299 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0); 1300 1301 /* Save the bits relating to the bridge */ 1302 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK; 1303 1304 return (0); 1305 } 1306 1307 static int 1308 bridge_ioctl_scache(struct bridge_softc *sc, void *arg) 1309 { 1310 struct ifbrparam *param = arg; 1311 1312 sc->sc_brtmax = param->ifbrp_csize; 1313 bridge_rttrim(sc); 1314 1315 return (0); 1316 } 1317 1318 static int 1319 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) 1320 { 1321 struct ifbrparam *param = arg; 1322 1323 param->ifbrp_csize = sc->sc_brtmax; 1324 1325 return (0); 1326 } 1327 1328 static int 1329 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg) 1330 { 1331 struct ifbifconf *bifc = arg; 1332 struct bridge_iflist *bif; 1333 struct ifbreq breq; 1334 char *buf, *outbuf; 1335 int count, buflen, len, error = 0; 1336 1337 count = 0; 1338 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) 1339 count++; 1340 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1341 count++; 1342 1343 buflen = sizeof(breq) * count; 1344 if (bifc->ifbic_len == 0) { 1345 bifc->ifbic_len = buflen; 1346 return (0); 1347 } 1348 BRIDGE_UNLOCK(sc); 1349 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1350 BRIDGE_LOCK(sc); 1351 1352 count = 0; 1353 buf = outbuf; 1354 len = min(bifc->ifbic_len, buflen); 1355 bzero(&breq, sizeof(breq)); 1356 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1357 if (len < sizeof(breq)) 1358 break; 1359 1360 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, 1361 sizeof(breq.ifbr_ifsname)); 1362 /* Fill in the ifbreq structure */ 1363 error = bridge_ioctl_gifflags(sc, &breq); 1364 if (error) 1365 break; 1366 memcpy(buf, &breq, sizeof(breq)); 1367 count++; 1368 buf += sizeof(breq); 1369 len -= sizeof(breq); 1370 } 1371 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 1372 if (len < sizeof(breq)) 1373 break; 1374 1375 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, 1376 sizeof(breq.ifbr_ifsname)); 1377 breq.ifbr_ifsflags = bif->bif_flags; 1378 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff; 1379 memcpy(buf, &breq, sizeof(breq)); 1380 count++; 1381 buf += sizeof(breq); 1382 len -= sizeof(breq); 1383 } 1384 1385 BRIDGE_UNLOCK(sc); 1386 bifc->ifbic_len = sizeof(breq) * count; 1387 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len); 1388 BRIDGE_LOCK(sc); 1389 free(outbuf, M_TEMP); 1390 return (error); 1391 } 1392 1393 static int 1394 bridge_ioctl_rts(struct bridge_softc *sc, void *arg) 1395 { 1396 struct ifbaconf *bac = arg; 1397 struct bridge_rtnode *brt; 1398 struct ifbareq bareq; 1399 char *buf, *outbuf; 1400 int count, buflen, len, error = 0; 1401 1402 if (bac->ifbac_len == 0) 1403 return (0); 1404 1405 count = 0; 1406 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) 1407 count++; 1408 buflen = sizeof(bareq) * count; 1409 1410 BRIDGE_UNLOCK(sc); 1411 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1412 BRIDGE_LOCK(sc); 1413 1414 count = 0; 1415 buf = outbuf; 1416 len = min(bac->ifbac_len, buflen); 1417 bzero(&bareq, sizeof(bareq)); 1418 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { 1419 if (len < sizeof(bareq)) 1420 goto out; 1421 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname, 1422 sizeof(bareq.ifba_ifsname)); 1423 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); 1424 bareq.ifba_vlan = brt->brt_vlan; 1425 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 1426 time_uptime < brt->brt_expire) 1427 bareq.ifba_expire = brt->brt_expire - time_uptime; 1428 else 1429 bareq.ifba_expire = 0; 1430 bareq.ifba_flags = brt->brt_flags; 1431 1432 memcpy(buf, &bareq, sizeof(bareq)); 1433 count++; 1434 buf += sizeof(bareq); 1435 len -= sizeof(bareq); 1436 } 1437 out: 1438 BRIDGE_UNLOCK(sc); 1439 bac->ifbac_len = sizeof(bareq) * count; 1440 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len); 1441 BRIDGE_LOCK(sc); 1442 free(outbuf, M_TEMP); 1443 return (error); 1444 } 1445 1446 static int 1447 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg) 1448 { 1449 struct ifbareq *req = arg; 1450 struct bridge_iflist *bif; 1451 int error; 1452 1453 bif = bridge_lookup_member(sc, req->ifba_ifsname); 1454 if (bif == NULL) 1455 return (ENOENT); 1456 1457 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1, 1458 req->ifba_flags); 1459 1460 return (error); 1461 } 1462 1463 static int 1464 bridge_ioctl_sto(struct bridge_softc *sc, void *arg) 1465 { 1466 struct ifbrparam *param = arg; 1467 1468 sc->sc_brttimeout = param->ifbrp_ctime; 1469 return (0); 1470 } 1471 1472 static int 1473 bridge_ioctl_gto(struct bridge_softc *sc, void *arg) 1474 { 1475 struct ifbrparam *param = arg; 1476 1477 param->ifbrp_ctime = sc->sc_brttimeout; 1478 return (0); 1479 } 1480 1481 static int 1482 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg) 1483 { 1484 struct ifbareq *req = arg; 1485 1486 return (bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan)); 1487 } 1488 1489 static int 1490 bridge_ioctl_flush(struct bridge_softc *sc, void *arg) 1491 { 1492 struct ifbreq *req = arg; 1493 1494 bridge_rtflush(sc, req->ifbr_ifsflags); 1495 return (0); 1496 } 1497 1498 static int 1499 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) 1500 { 1501 struct ifbrparam *param = arg; 1502 struct bstp_state *bs = &sc->sc_stp; 1503 1504 param->ifbrp_prio = bs->bs_bridge_priority; 1505 return (0); 1506 } 1507 1508 static int 1509 bridge_ioctl_spri(struct bridge_softc *sc, void *arg) 1510 { 1511 struct ifbrparam *param = arg; 1512 1513 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio)); 1514 } 1515 1516 static int 1517 bridge_ioctl_ght(struct bridge_softc *sc, void *arg) 1518 { 1519 struct ifbrparam *param = arg; 1520 struct bstp_state *bs = &sc->sc_stp; 1521 1522 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8; 1523 return (0); 1524 } 1525 1526 static int 1527 bridge_ioctl_sht(struct bridge_softc *sc, void *arg) 1528 { 1529 struct ifbrparam *param = arg; 1530 1531 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime)); 1532 } 1533 1534 static int 1535 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) 1536 { 1537 struct ifbrparam *param = arg; 1538 struct bstp_state *bs = &sc->sc_stp; 1539 1540 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8; 1541 return (0); 1542 } 1543 1544 static int 1545 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) 1546 { 1547 struct ifbrparam *param = arg; 1548 1549 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay)); 1550 } 1551 1552 static int 1553 bridge_ioctl_gma(struct bridge_softc *sc, void *arg) 1554 { 1555 struct ifbrparam *param = arg; 1556 struct bstp_state *bs = &sc->sc_stp; 1557 1558 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8; 1559 return (0); 1560 } 1561 1562 static int 1563 bridge_ioctl_sma(struct bridge_softc *sc, void *arg) 1564 { 1565 struct ifbrparam *param = arg; 1566 1567 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage)); 1568 } 1569 1570 static int 1571 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) 1572 { 1573 struct ifbreq *req = arg; 1574 struct bridge_iflist *bif; 1575 1576 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1577 if (bif == NULL) 1578 return (ENOENT); 1579 1580 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority)); 1581 } 1582 1583 static int 1584 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) 1585 { 1586 struct ifbreq *req = arg; 1587 struct bridge_iflist *bif; 1588 1589 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1590 if (bif == NULL) 1591 return (ENOENT); 1592 1593 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost)); 1594 } 1595 1596 static int 1597 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg) 1598 { 1599 struct ifbreq *req = arg; 1600 struct bridge_iflist *bif; 1601 1602 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1603 if (bif == NULL) 1604 return (ENOENT); 1605 1606 bif->bif_addrmax = req->ifbr_addrmax; 1607 return (0); 1608 } 1609 1610 static int 1611 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg) 1612 { 1613 struct ifbreq *req = arg; 1614 struct bridge_iflist *bif = NULL; 1615 struct ifnet *ifs; 1616 1617 ifs = ifunit(req->ifbr_ifsname); 1618 if (ifs == NULL) 1619 return (ENOENT); 1620 1621 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1622 if (ifs == bif->bif_ifp) 1623 return (EBUSY); 1624 1625 if (ifs->if_bridge != NULL) 1626 return (EBUSY); 1627 1628 switch (ifs->if_type) { 1629 case IFT_ETHER: 1630 case IFT_GIF: 1631 case IFT_L2VLAN: 1632 break; 1633 default: 1634 return (EINVAL); 1635 } 1636 1637 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO); 1638 if (bif == NULL) 1639 return (ENOMEM); 1640 1641 bif->bif_ifp = ifs; 1642 bif->bif_flags = IFBIF_SPAN; 1643 1644 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next); 1645 1646 return (0); 1647 } 1648 1649 static int 1650 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg) 1651 { 1652 struct ifbreq *req = arg; 1653 struct bridge_iflist *bif; 1654 struct ifnet *ifs; 1655 1656 ifs = ifunit(req->ifbr_ifsname); 1657 if (ifs == NULL) 1658 return (ENOENT); 1659 1660 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1661 if (ifs == bif->bif_ifp) 1662 break; 1663 1664 if (bif == NULL) 1665 return (ENOENT); 1666 1667 bridge_delete_span(sc, bif); 1668 1669 return (0); 1670 } 1671 1672 static int 1673 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg) 1674 { 1675 struct ifbropreq *req = arg; 1676 struct bstp_state *bs = &sc->sc_stp; 1677 struct bstp_port *root_port; 1678 1679 req->ifbop_maxage = bs->bs_bridge_max_age >> 8; 1680 req->ifbop_hellotime = bs->bs_bridge_htime >> 8; 1681 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; 1682 1683 root_port = bs->bs_root_port; 1684 if (root_port == NULL) 1685 req->ifbop_root_port = 0; 1686 else 1687 req->ifbop_root_port = root_port->bp_ifp->if_index; 1688 1689 req->ifbop_holdcount = bs->bs_txholdcount; 1690 req->ifbop_priority = bs->bs_bridge_priority; 1691 req->ifbop_protocol = bs->bs_protover; 1692 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; 1693 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id; 1694 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; 1695 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id; 1696 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; 1697 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; 1698 1699 return (0); 1700 } 1701 1702 static int 1703 bridge_ioctl_grte(struct bridge_softc *sc, void *arg) 1704 { 1705 struct ifbrparam *param = arg; 1706 1707 param->ifbrp_cexceeded = sc->sc_brtexceeded; 1708 return (0); 1709 } 1710 1711 static int 1712 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg) 1713 { 1714 struct ifbpstpconf *bifstp = arg; 1715 struct bridge_iflist *bif; 1716 struct bstp_port *bp; 1717 struct ifbpstpreq bpreq; 1718 char *buf, *outbuf; 1719 int count, buflen, len, error = 0; 1720 1721 count = 0; 1722 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1723 if ((bif->bif_flags & IFBIF_STP) != 0) 1724 count++; 1725 } 1726 1727 buflen = sizeof(bpreq) * count; 1728 if (bifstp->ifbpstp_len == 0) { 1729 bifstp->ifbpstp_len = buflen; 1730 return (0); 1731 } 1732 1733 BRIDGE_UNLOCK(sc); 1734 outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 1735 BRIDGE_LOCK(sc); 1736 1737 count = 0; 1738 buf = outbuf; 1739 len = min(bifstp->ifbpstp_len, buflen); 1740 bzero(&bpreq, sizeof(bpreq)); 1741 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1742 if (len < sizeof(bpreq)) 1743 break; 1744 1745 if ((bif->bif_flags & IFBIF_STP) == 0) 1746 continue; 1747 1748 bp = &bif->bif_stp; 1749 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; 1750 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions; 1751 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost; 1752 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id; 1753 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; 1754 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id; 1755 1756 memcpy(buf, &bpreq, sizeof(bpreq)); 1757 count++; 1758 buf += sizeof(bpreq); 1759 len -= sizeof(bpreq); 1760 } 1761 1762 BRIDGE_UNLOCK(sc); 1763 bifstp->ifbpstp_len = sizeof(bpreq) * count; 1764 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); 1765 BRIDGE_LOCK(sc); 1766 free(outbuf, M_TEMP); 1767 return (error); 1768 } 1769 1770 static int 1771 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg) 1772 { 1773 struct ifbrparam *param = arg; 1774 1775 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto)); 1776 } 1777 1778 static int 1779 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg) 1780 { 1781 struct ifbrparam *param = arg; 1782 1783 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc)); 1784 } 1785 1786 /* 1787 * bridge_ifdetach: 1788 * 1789 * Detach an interface from a bridge. Called when a member 1790 * interface is detaching. 1791 */ 1792 static void 1793 bridge_ifdetach(void *arg __unused, struct ifnet *ifp) 1794 { 1795 struct bridge_softc *sc = ifp->if_bridge; 1796 struct bridge_iflist *bif; 1797 1798 if (ifp->if_flags & IFF_RENAMING) 1799 return; 1800 1801 /* Check if the interface is a bridge member */ 1802 if (sc != NULL) { 1803 BRIDGE_LOCK(sc); 1804 1805 bif = bridge_lookup_member_if(sc, ifp); 1806 if (bif != NULL) 1807 bridge_delete_member(sc, bif, 1); 1808 1809 BRIDGE_UNLOCK(sc); 1810 return; 1811 } 1812 1813 /* Check if the interface is a span port */ 1814 BRIDGE_LIST_LOCK(); 1815 LIST_FOREACH(sc, &V_bridge_list, sc_list) { 1816 BRIDGE_LOCK(sc); 1817 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1818 if (ifp == bif->bif_ifp) { 1819 bridge_delete_span(sc, bif); 1820 break; 1821 } 1822 1823 BRIDGE_UNLOCK(sc); 1824 } 1825 BRIDGE_LIST_UNLOCK(); 1826 } 1827 1828 /* 1829 * bridge_init: 1830 * 1831 * Initialize a bridge interface. 1832 */ 1833 static void 1834 bridge_init(void *xsc) 1835 { 1836 struct bridge_softc *sc = (struct bridge_softc *)xsc; 1837 struct ifnet *ifp = sc->sc_ifp; 1838 1839 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1840 return; 1841 1842 BRIDGE_LOCK(sc); 1843 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz, 1844 bridge_timer, sc); 1845 1846 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1847 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */ 1848 1849 BRIDGE_UNLOCK(sc); 1850 } 1851 1852 /* 1853 * bridge_stop: 1854 * 1855 * Stop the bridge interface. 1856 */ 1857 static void 1858 bridge_stop(struct ifnet *ifp, int disable) 1859 { 1860 struct bridge_softc *sc = ifp->if_softc; 1861 1862 BRIDGE_LOCK_ASSERT(sc); 1863 1864 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1865 return; 1866 1867 callout_stop(&sc->sc_brcallout); 1868 bstp_stop(&sc->sc_stp); 1869 1870 bridge_rtflush(sc, IFBF_FLUSHDYN); 1871 1872 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1873 } 1874 1875 /* 1876 * bridge_enqueue: 1877 * 1878 * Enqueue a packet on a bridge member interface. 1879 * 1880 */ 1881 static int 1882 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m) 1883 { 1884 int len, err = 0; 1885 short mflags; 1886 struct mbuf *m0; 1887 1888 /* We may be sending a fragment so traverse the mbuf */ 1889 for (; m; m = m0) { 1890 m0 = m->m_nextpkt; 1891 m->m_nextpkt = NULL; 1892 len = m->m_pkthdr.len; 1893 mflags = m->m_flags; 1894 1895 /* 1896 * If underlying interface can not do VLAN tag insertion itself 1897 * then attach a packet tag that holds it. 1898 */ 1899 if ((m->m_flags & M_VLANTAG) && 1900 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) { 1901 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); 1902 if (m == NULL) { 1903 if_printf(dst_ifp, 1904 "unable to prepend VLAN header\n"); 1905 if_inc_counter(dst_ifp, IFCOUNTER_OERRORS, 1); 1906 continue; 1907 } 1908 m->m_flags &= ~M_VLANTAG; 1909 } 1910 1911 if ((err = dst_ifp->if_transmit(dst_ifp, m))) { 1912 m_freem(m0); 1913 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 1914 break; 1915 } 1916 1917 if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1); 1918 if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, len); 1919 if (mflags & M_MCAST) 1920 if_inc_counter(sc->sc_ifp, IFCOUNTER_OMCASTS, 1); 1921 } 1922 1923 return (err); 1924 } 1925 1926 /* 1927 * bridge_dummynet: 1928 * 1929 * Receive a queued packet from dummynet and pass it on to the output 1930 * interface. 1931 * 1932 * The mbuf has the Ethernet header already attached. 1933 */ 1934 static void 1935 bridge_dummynet(struct mbuf *m, struct ifnet *ifp) 1936 { 1937 struct bridge_softc *sc; 1938 1939 sc = ifp->if_bridge; 1940 1941 /* 1942 * The packet didnt originate from a member interface. This should only 1943 * ever happen if a member interface is removed while packets are 1944 * queued for it. 1945 */ 1946 if (sc == NULL) { 1947 m_freem(m); 1948 return; 1949 } 1950 1951 if (PFIL_HOOKED(&V_inet_pfil_hook) 1952 #ifdef INET6 1953 || PFIL_HOOKED(&V_inet6_pfil_hook) 1954 #endif 1955 ) { 1956 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0) 1957 return; 1958 if (m == NULL) 1959 return; 1960 } 1961 1962 bridge_enqueue(sc, ifp, m); 1963 } 1964 1965 /* 1966 * bridge_output: 1967 * 1968 * Send output from a bridge member interface. This 1969 * performs the bridging function for locally originated 1970 * packets. 1971 * 1972 * The mbuf has the Ethernet header already attached. We must 1973 * enqueue or free the mbuf before returning. 1974 */ 1975 static int 1976 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa, 1977 struct rtentry *rt) 1978 { 1979 struct ether_header *eh; 1980 struct ifnet *dst_if; 1981 struct bridge_softc *sc; 1982 uint16_t vlan; 1983 1984 if (m->m_len < ETHER_HDR_LEN) { 1985 m = m_pullup(m, ETHER_HDR_LEN); 1986 if (m == NULL) 1987 return (0); 1988 } 1989 1990 eh = mtod(m, struct ether_header *); 1991 sc = ifp->if_bridge; 1992 vlan = VLANTAGOF(m); 1993 1994 BRIDGE_LOCK(sc); 1995 1996 /* 1997 * If bridge is down, but the original output interface is up, 1998 * go ahead and send out that interface. Otherwise, the packet 1999 * is dropped below. 2000 */ 2001 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2002 dst_if = ifp; 2003 goto sendunicast; 2004 } 2005 2006 /* 2007 * If the packet is a multicast, or we don't know a better way to 2008 * get there, send to all interfaces. 2009 */ 2010 if (ETHER_IS_MULTICAST(eh->ether_dhost)) 2011 dst_if = NULL; 2012 else 2013 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan); 2014 if (dst_if == NULL) { 2015 struct bridge_iflist *bif; 2016 struct mbuf *mc; 2017 int error = 0, used = 0; 2018 2019 bridge_span(sc, m); 2020 2021 BRIDGE_LOCK2REF(sc, error); 2022 if (error) { 2023 m_freem(m); 2024 return (0); 2025 } 2026 2027 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 2028 dst_if = bif->bif_ifp; 2029 2030 if (dst_if->if_type == IFT_GIF) 2031 continue; 2032 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2033 continue; 2034 2035 /* 2036 * If this is not the original output interface, 2037 * and the interface is participating in spanning 2038 * tree, make sure the port is in a state that 2039 * allows forwarding. 2040 */ 2041 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) && 2042 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2043 continue; 2044 2045 if (LIST_NEXT(bif, bif_next) == NULL) { 2046 used = 1; 2047 mc = m; 2048 } else { 2049 mc = m_copypacket(m, M_NOWAIT); 2050 if (mc == NULL) { 2051 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 2052 continue; 2053 } 2054 } 2055 2056 bridge_enqueue(sc, dst_if, mc); 2057 } 2058 if (used == 0) 2059 m_freem(m); 2060 BRIDGE_UNREF(sc); 2061 return (0); 2062 } 2063 2064 sendunicast: 2065 /* 2066 * XXX Spanning tree consideration here? 2067 */ 2068 2069 bridge_span(sc, m); 2070 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2071 m_freem(m); 2072 BRIDGE_UNLOCK(sc); 2073 return (0); 2074 } 2075 2076 BRIDGE_UNLOCK(sc); 2077 bridge_enqueue(sc, dst_if, m); 2078 return (0); 2079 } 2080 2081 /* 2082 * bridge_transmit: 2083 * 2084 * Do output on a bridge. 2085 * 2086 */ 2087 static int 2088 bridge_transmit(struct ifnet *ifp, struct mbuf *m) 2089 { 2090 struct bridge_softc *sc; 2091 struct ether_header *eh; 2092 struct ifnet *dst_if; 2093 int error = 0; 2094 2095 sc = ifp->if_softc; 2096 2097 ETHER_BPF_MTAP(ifp, m); 2098 2099 eh = mtod(m, struct ether_header *); 2100 2101 BRIDGE_LOCK(sc); 2102 if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) && 2103 (dst_if = bridge_rtlookup(sc, eh->ether_dhost, 1)) != NULL) { 2104 BRIDGE_UNLOCK(sc); 2105 error = bridge_enqueue(sc, dst_if, m); 2106 } else 2107 bridge_broadcast(sc, ifp, m, 0); 2108 2109 return (error); 2110 } 2111 2112 /* 2113 * The ifp->if_qflush entry point for if_bridge(4) is no-op. 2114 */ 2115 static void 2116 bridge_qflush(struct ifnet *ifp __unused) 2117 { 2118 } 2119 2120 /* 2121 * bridge_forward: 2122 * 2123 * The forwarding function of the bridge. 2124 * 2125 * NOTE: Releases the lock on return. 2126 */ 2127 static void 2128 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif, 2129 struct mbuf *m) 2130 { 2131 struct bridge_iflist *dbif; 2132 struct ifnet *src_if, *dst_if, *ifp; 2133 struct ether_header *eh; 2134 uint16_t vlan; 2135 uint8_t *dst; 2136 int error; 2137 2138 src_if = m->m_pkthdr.rcvif; 2139 ifp = sc->sc_ifp; 2140 2141 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 2142 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 2143 vlan = VLANTAGOF(m); 2144 2145 if ((sbif->bif_flags & IFBIF_STP) && 2146 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2147 goto drop; 2148 2149 eh = mtod(m, struct ether_header *); 2150 dst = eh->ether_dhost; 2151 2152 /* If the interface is learning, record the address. */ 2153 if (sbif->bif_flags & IFBIF_LEARNING) { 2154 error = bridge_rtupdate(sc, eh->ether_shost, vlan, 2155 sbif, 0, IFBAF_DYNAMIC); 2156 /* 2157 * If the interface has addresses limits then deny any source 2158 * that is not in the cache. 2159 */ 2160 if (error && sbif->bif_addrmax) 2161 goto drop; 2162 } 2163 2164 if ((sbif->bif_flags & IFBIF_STP) != 0 && 2165 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) 2166 goto drop; 2167 2168 /* 2169 * At this point, the port either doesn't participate 2170 * in spanning tree or it is in the forwarding state. 2171 */ 2172 2173 /* 2174 * If the packet is unicast, destined for someone on 2175 * "this" side of the bridge, drop it. 2176 */ 2177 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) { 2178 dst_if = bridge_rtlookup(sc, dst, vlan); 2179 if (src_if == dst_if) 2180 goto drop; 2181 } else { 2182 /* 2183 * Check if its a reserved multicast address, any address 2184 * listed in 802.1D section 7.12.6 may not be forwarded by the 2185 * bridge. 2186 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F 2187 */ 2188 if (dst[0] == 0x01 && dst[1] == 0x80 && 2189 dst[2] == 0xc2 && dst[3] == 0x00 && 2190 dst[4] == 0x00 && dst[5] <= 0x0f) 2191 goto drop; 2192 2193 /* ...forward it to all interfaces. */ 2194 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 2195 dst_if = NULL; 2196 } 2197 2198 /* 2199 * If we have a destination interface which is a member of our bridge, 2200 * OR this is a unicast packet, push it through the bpf(4) machinery. 2201 * For broadcast or multicast packets, don't bother because it will 2202 * be reinjected into ether_input. We do this before we pass the packets 2203 * through the pfil(9) framework, as it is possible that pfil(9) will 2204 * drop the packet, or possibly modify it, making it difficult to debug 2205 * firewall issues on the bridge. 2206 */ 2207 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0) 2208 ETHER_BPF_MTAP(ifp, m); 2209 2210 /* run the packet filter */ 2211 if (PFIL_HOOKED(&V_inet_pfil_hook) 2212 #ifdef INET6 2213 || PFIL_HOOKED(&V_inet6_pfil_hook) 2214 #endif 2215 ) { 2216 BRIDGE_UNLOCK(sc); 2217 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0) 2218 return; 2219 if (m == NULL) 2220 return; 2221 BRIDGE_LOCK(sc); 2222 } 2223 2224 if (dst_if == NULL) { 2225 bridge_broadcast(sc, src_if, m, 1); 2226 return; 2227 } 2228 2229 /* 2230 * At this point, we're dealing with a unicast frame 2231 * going to a different interface. 2232 */ 2233 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2234 goto drop; 2235 2236 dbif = bridge_lookup_member_if(sc, dst_if); 2237 if (dbif == NULL) 2238 /* Not a member of the bridge (anymore?) */ 2239 goto drop; 2240 2241 /* Private segments can not talk to each other */ 2242 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE) 2243 goto drop; 2244 2245 if ((dbif->bif_flags & IFBIF_STP) && 2246 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2247 goto drop; 2248 2249 BRIDGE_UNLOCK(sc); 2250 2251 if (PFIL_HOOKED(&V_inet_pfil_hook) 2252 #ifdef INET6 2253 || PFIL_HOOKED(&V_inet6_pfil_hook) 2254 #endif 2255 ) { 2256 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0) 2257 return; 2258 if (m == NULL) 2259 return; 2260 } 2261 2262 bridge_enqueue(sc, dst_if, m); 2263 return; 2264 2265 drop: 2266 BRIDGE_UNLOCK(sc); 2267 m_freem(m); 2268 } 2269 2270 /* 2271 * bridge_input: 2272 * 2273 * Receive input from a member interface. Queue the packet for 2274 * bridging if it is not for us. 2275 */ 2276 static struct mbuf * 2277 bridge_input(struct ifnet *ifp, struct mbuf *m) 2278 { 2279 struct bridge_softc *sc = ifp->if_bridge; 2280 struct bridge_iflist *bif, *bif2; 2281 struct ifnet *bifp; 2282 struct ether_header *eh; 2283 struct mbuf *mc, *mc2; 2284 uint16_t vlan; 2285 int error; 2286 2287 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2288 return (m); 2289 2290 bifp = sc->sc_ifp; 2291 vlan = VLANTAGOF(m); 2292 2293 /* 2294 * Implement support for bridge monitoring. If this flag has been 2295 * set on this interface, discard the packet once we push it through 2296 * the bpf(4) machinery, but before we do, increment the byte and 2297 * packet counters associated with this interface. 2298 */ 2299 if ((bifp->if_flags & IFF_MONITOR) != 0) { 2300 m->m_pkthdr.rcvif = bifp; 2301 ETHER_BPF_MTAP(bifp, m); 2302 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1); 2303 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 2304 m_freem(m); 2305 return (NULL); 2306 } 2307 BRIDGE_LOCK(sc); 2308 bif = bridge_lookup_member_if(sc, ifp); 2309 if (bif == NULL) { 2310 BRIDGE_UNLOCK(sc); 2311 return (m); 2312 } 2313 2314 eh = mtod(m, struct ether_header *); 2315 2316 bridge_span(sc, m); 2317 2318 if (m->m_flags & (M_BCAST|M_MCAST)) { 2319 /* Tap off 802.1D packets; they do not get forwarded. */ 2320 if (memcmp(eh->ether_dhost, bstp_etheraddr, 2321 ETHER_ADDR_LEN) == 0) { 2322 bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */ 2323 BRIDGE_UNLOCK(sc); 2324 return (NULL); 2325 } 2326 2327 if ((bif->bif_flags & IFBIF_STP) && 2328 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { 2329 BRIDGE_UNLOCK(sc); 2330 return (m); 2331 } 2332 2333 /* 2334 * Make a deep copy of the packet and enqueue the copy 2335 * for bridge processing; return the original packet for 2336 * local processing. 2337 */ 2338 mc = m_dup(m, M_NOWAIT); 2339 if (mc == NULL) { 2340 BRIDGE_UNLOCK(sc); 2341 return (m); 2342 } 2343 2344 /* Perform the bridge forwarding function with the copy. */ 2345 bridge_forward(sc, bif, mc); 2346 2347 /* 2348 * Reinject the mbuf as arriving on the bridge so we have a 2349 * chance at claiming multicast packets. We can not loop back 2350 * here from ether_input as a bridge is never a member of a 2351 * bridge. 2352 */ 2353 KASSERT(bifp->if_bridge == NULL, 2354 ("loop created in bridge_input")); 2355 mc2 = m_dup(m, M_NOWAIT); 2356 if (mc2 != NULL) { 2357 /* Keep the layer3 header aligned */ 2358 int i = min(mc2->m_pkthdr.len, max_protohdr); 2359 mc2 = m_copyup(mc2, i, ETHER_ALIGN); 2360 } 2361 if (mc2 != NULL) { 2362 mc2->m_pkthdr.rcvif = bifp; 2363 (*bifp->if_input)(bifp, mc2); 2364 } 2365 2366 /* Return the original packet for local processing. */ 2367 return (m); 2368 } 2369 2370 if ((bif->bif_flags & IFBIF_STP) && 2371 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { 2372 BRIDGE_UNLOCK(sc); 2373 return (m); 2374 } 2375 2376 #if (defined(INET) || defined(INET6)) 2377 # define OR_CARP_CHECK_WE_ARE_DST(iface) \ 2378 || ((iface)->if_carp \ 2379 && (*carp_forus_p)((iface), eh->ether_dhost)) 2380 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \ 2381 || ((iface)->if_carp \ 2382 && (*carp_forus_p)((iface), eh->ether_shost)) 2383 #else 2384 # define OR_CARP_CHECK_WE_ARE_DST(iface) 2385 # define OR_CARP_CHECK_WE_ARE_SRC(iface) 2386 #endif 2387 2388 #ifdef INET6 2389 # define OR_PFIL_HOOKED_INET6 \ 2390 || PFIL_HOOKED(&V_inet6_pfil_hook) 2391 #else 2392 # define OR_PFIL_HOOKED_INET6 2393 #endif 2394 2395 #define GRAB_OUR_PACKETS(iface) \ 2396 if ((iface)->if_type == IFT_GIF) \ 2397 continue; \ 2398 /* It is destined for us. */ \ 2399 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \ 2400 OR_CARP_CHECK_WE_ARE_DST((iface)) \ 2401 ) { \ 2402 if ((iface)->if_type == IFT_BRIDGE) { \ 2403 ETHER_BPF_MTAP(iface, m); \ 2404 if_inc_counter(iface, IFCOUNTER_IPACKETS, 1); \ 2405 if_inc_counter(iface, IFCOUNTER_IBYTES, m->m_pkthdr.len); \ 2406 /* Filter on the physical interface. */ \ 2407 if (V_pfil_local_phys && \ 2408 (PFIL_HOOKED(&V_inet_pfil_hook) \ 2409 OR_PFIL_HOOKED_INET6)) { \ 2410 if (bridge_pfil(&m, NULL, ifp, \ 2411 PFIL_IN) != 0 || m == NULL) { \ 2412 BRIDGE_UNLOCK(sc); \ 2413 return (NULL); \ 2414 } \ 2415 eh = mtod(m, struct ether_header *); \ 2416 } \ 2417 } \ 2418 if (bif->bif_flags & IFBIF_LEARNING) { \ 2419 error = bridge_rtupdate(sc, eh->ether_shost, \ 2420 vlan, bif, 0, IFBAF_DYNAMIC); \ 2421 if (error && bif->bif_addrmax) { \ 2422 BRIDGE_UNLOCK(sc); \ 2423 m_freem(m); \ 2424 return (NULL); \ 2425 } \ 2426 } \ 2427 m->m_pkthdr.rcvif = iface; \ 2428 BRIDGE_UNLOCK(sc); \ 2429 return (m); \ 2430 } \ 2431 \ 2432 /* We just received a packet that we sent out. */ \ 2433 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \ 2434 OR_CARP_CHECK_WE_ARE_SRC((iface)) \ 2435 ) { \ 2436 BRIDGE_UNLOCK(sc); \ 2437 m_freem(m); \ 2438 return (NULL); \ 2439 } 2440 2441 /* 2442 * Unicast. Make sure it's not for the bridge. 2443 */ 2444 do { GRAB_OUR_PACKETS(bifp) } while (0); 2445 2446 /* 2447 * Give a chance for ifp at first priority. This will help when the 2448 * packet comes through the interface like VLAN's with the same MACs 2449 * on several interfaces from the same bridge. This also will save 2450 * some CPU cycles in case the destination interface and the input 2451 * interface (eq ifp) are the same. 2452 */ 2453 do { GRAB_OUR_PACKETS(ifp) } while (0); 2454 2455 /* Now check the all bridge members. */ 2456 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) { 2457 GRAB_OUR_PACKETS(bif2->bif_ifp) 2458 } 2459 2460 #undef OR_CARP_CHECK_WE_ARE_DST 2461 #undef OR_CARP_CHECK_WE_ARE_SRC 2462 #undef OR_PFIL_HOOKED_INET6 2463 #undef GRAB_OUR_PACKETS 2464 2465 /* Perform the bridge forwarding function. */ 2466 bridge_forward(sc, bif, m); 2467 2468 return (NULL); 2469 } 2470 2471 /* 2472 * bridge_broadcast: 2473 * 2474 * Send a frame to all interfaces that are members of 2475 * the bridge, except for the one on which the packet 2476 * arrived. 2477 * 2478 * NOTE: Releases the lock on return. 2479 */ 2480 static void 2481 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if, 2482 struct mbuf *m, int runfilt) 2483 { 2484 struct bridge_iflist *dbif, *sbif; 2485 struct mbuf *mc; 2486 struct ifnet *dst_if; 2487 int error = 0, used = 0, i; 2488 2489 sbif = bridge_lookup_member_if(sc, src_if); 2490 2491 BRIDGE_LOCK2REF(sc, error); 2492 if (error) { 2493 m_freem(m); 2494 return; 2495 } 2496 2497 /* Filter on the bridge interface before broadcasting */ 2498 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook) 2499 #ifdef INET6 2500 || PFIL_HOOKED(&V_inet6_pfil_hook) 2501 #endif 2502 )) { 2503 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0) 2504 goto out; 2505 if (m == NULL) 2506 goto out; 2507 } 2508 2509 LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) { 2510 dst_if = dbif->bif_ifp; 2511 if (dst_if == src_if) 2512 continue; 2513 2514 /* Private segments can not talk to each other */ 2515 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)) 2516 continue; 2517 2518 if ((dbif->bif_flags & IFBIF_STP) && 2519 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2520 continue; 2521 2522 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 && 2523 (m->m_flags & (M_BCAST|M_MCAST)) == 0) 2524 continue; 2525 2526 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2527 continue; 2528 2529 if (LIST_NEXT(dbif, bif_next) == NULL) { 2530 mc = m; 2531 used = 1; 2532 } else { 2533 mc = m_dup(m, M_NOWAIT); 2534 if (mc == NULL) { 2535 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 2536 continue; 2537 } 2538 } 2539 2540 /* 2541 * Filter on the output interface. Pass a NULL bridge interface 2542 * pointer so we do not redundantly filter on the bridge for 2543 * each interface we broadcast on. 2544 */ 2545 if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook) 2546 #ifdef INET6 2547 || PFIL_HOOKED(&V_inet6_pfil_hook) 2548 #endif 2549 )) { 2550 if (used == 0) { 2551 /* Keep the layer3 header aligned */ 2552 i = min(mc->m_pkthdr.len, max_protohdr); 2553 mc = m_copyup(mc, i, ETHER_ALIGN); 2554 if (mc == NULL) { 2555 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 2556 continue; 2557 } 2558 } 2559 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0) 2560 continue; 2561 if (mc == NULL) 2562 continue; 2563 } 2564 2565 bridge_enqueue(sc, dst_if, mc); 2566 } 2567 if (used == 0) 2568 m_freem(m); 2569 2570 out: 2571 BRIDGE_UNREF(sc); 2572 } 2573 2574 /* 2575 * bridge_span: 2576 * 2577 * Duplicate a packet out one or more interfaces that are in span mode, 2578 * the original mbuf is unmodified. 2579 */ 2580 static void 2581 bridge_span(struct bridge_softc *sc, struct mbuf *m) 2582 { 2583 struct bridge_iflist *bif; 2584 struct ifnet *dst_if; 2585 struct mbuf *mc; 2586 2587 if (LIST_EMPTY(&sc->sc_spanlist)) 2588 return; 2589 2590 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 2591 dst_if = bif->bif_ifp; 2592 2593 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2594 continue; 2595 2596 mc = m_copypacket(m, M_NOWAIT); 2597 if (mc == NULL) { 2598 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 2599 continue; 2600 } 2601 2602 bridge_enqueue(sc, dst_if, mc); 2603 } 2604 } 2605 2606 /* 2607 * bridge_rtupdate: 2608 * 2609 * Add a bridge routing entry. 2610 */ 2611 static int 2612 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan, 2613 struct bridge_iflist *bif, int setflags, uint8_t flags) 2614 { 2615 struct bridge_rtnode *brt; 2616 int error; 2617 2618 BRIDGE_LOCK_ASSERT(sc); 2619 2620 /* Check the source address is valid and not multicast. */ 2621 if (ETHER_IS_MULTICAST(dst) || 2622 (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 && 2623 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0) 2624 return (EINVAL); 2625 2626 /* 802.1p frames map to vlan 1 */ 2627 if (vlan == 0) 2628 vlan = 1; 2629 2630 /* 2631 * A route for this destination might already exist. If so, 2632 * update it, otherwise create a new one. 2633 */ 2634 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) { 2635 if (sc->sc_brtcnt >= sc->sc_brtmax) { 2636 sc->sc_brtexceeded++; 2637 return (ENOSPC); 2638 } 2639 /* Check per interface address limits (if enabled) */ 2640 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) { 2641 bif->bif_addrexceeded++; 2642 return (ENOSPC); 2643 } 2644 2645 /* 2646 * Allocate a new bridge forwarding node, and 2647 * initialize the expiration time and Ethernet 2648 * address. 2649 */ 2650 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO); 2651 if (brt == NULL) 2652 return (ENOMEM); 2653 2654 if (bif->bif_flags & IFBIF_STICKY) 2655 brt->brt_flags = IFBAF_STICKY; 2656 else 2657 brt->brt_flags = IFBAF_DYNAMIC; 2658 2659 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); 2660 brt->brt_vlan = vlan; 2661 2662 if ((error = bridge_rtnode_insert(sc, brt)) != 0) { 2663 uma_zfree(bridge_rtnode_zone, brt); 2664 return (error); 2665 } 2666 brt->brt_dst = bif; 2667 bif->bif_addrcnt++; 2668 } 2669 2670 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 2671 brt->brt_dst != bif) { 2672 brt->brt_dst->bif_addrcnt--; 2673 brt->brt_dst = bif; 2674 brt->brt_dst->bif_addrcnt++; 2675 } 2676 2677 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 2678 brt->brt_expire = time_uptime + sc->sc_brttimeout; 2679 if (setflags) 2680 brt->brt_flags = flags; 2681 2682 return (0); 2683 } 2684 2685 /* 2686 * bridge_rtlookup: 2687 * 2688 * Lookup the destination interface for an address. 2689 */ 2690 static struct ifnet * 2691 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) 2692 { 2693 struct bridge_rtnode *brt; 2694 2695 BRIDGE_LOCK_ASSERT(sc); 2696 2697 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL) 2698 return (NULL); 2699 2700 return (brt->brt_ifp); 2701 } 2702 2703 /* 2704 * bridge_rttrim: 2705 * 2706 * Trim the routine table so that we have a number 2707 * of routing entries less than or equal to the 2708 * maximum number. 2709 */ 2710 static void 2711 bridge_rttrim(struct bridge_softc *sc) 2712 { 2713 struct bridge_rtnode *brt, *nbrt; 2714 2715 BRIDGE_LOCK_ASSERT(sc); 2716 2717 /* Make sure we actually need to do this. */ 2718 if (sc->sc_brtcnt <= sc->sc_brtmax) 2719 return; 2720 2721 /* Force an aging cycle; this might trim enough addresses. */ 2722 bridge_rtage(sc); 2723 if (sc->sc_brtcnt <= sc->sc_brtmax) 2724 return; 2725 2726 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 2727 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 2728 bridge_rtnode_destroy(sc, brt); 2729 if (sc->sc_brtcnt <= sc->sc_brtmax) 2730 return; 2731 } 2732 } 2733 } 2734 2735 /* 2736 * bridge_timer: 2737 * 2738 * Aging timer for the bridge. 2739 */ 2740 static void 2741 bridge_timer(void *arg) 2742 { 2743 struct bridge_softc *sc = arg; 2744 2745 BRIDGE_LOCK_ASSERT(sc); 2746 2747 bridge_rtage(sc); 2748 2749 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) 2750 callout_reset(&sc->sc_brcallout, 2751 bridge_rtable_prune_period * hz, bridge_timer, sc); 2752 } 2753 2754 /* 2755 * bridge_rtage: 2756 * 2757 * Perform an aging cycle. 2758 */ 2759 static void 2760 bridge_rtage(struct bridge_softc *sc) 2761 { 2762 struct bridge_rtnode *brt, *nbrt; 2763 2764 BRIDGE_LOCK_ASSERT(sc); 2765 2766 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 2767 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 2768 if (time_uptime >= brt->brt_expire) 2769 bridge_rtnode_destroy(sc, brt); 2770 } 2771 } 2772 } 2773 2774 /* 2775 * bridge_rtflush: 2776 * 2777 * Remove all dynamic addresses from the bridge. 2778 */ 2779 static void 2780 bridge_rtflush(struct bridge_softc *sc, int full) 2781 { 2782 struct bridge_rtnode *brt, *nbrt; 2783 2784 BRIDGE_LOCK_ASSERT(sc); 2785 2786 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 2787 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 2788 bridge_rtnode_destroy(sc, brt); 2789 } 2790 } 2791 2792 /* 2793 * bridge_rtdaddr: 2794 * 2795 * Remove an address from the table. 2796 */ 2797 static int 2798 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) 2799 { 2800 struct bridge_rtnode *brt; 2801 int found = 0; 2802 2803 BRIDGE_LOCK_ASSERT(sc); 2804 2805 /* 2806 * If vlan is zero then we want to delete for all vlans so the lookup 2807 * may return more than one. 2808 */ 2809 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) { 2810 bridge_rtnode_destroy(sc, brt); 2811 found = 1; 2812 } 2813 2814 return (found ? 0 : ENOENT); 2815 } 2816 2817 /* 2818 * bridge_rtdelete: 2819 * 2820 * Delete routes to a speicifc member interface. 2821 */ 2822 static void 2823 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full) 2824 { 2825 struct bridge_rtnode *brt, *nbrt; 2826 2827 BRIDGE_LOCK_ASSERT(sc); 2828 2829 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 2830 if (brt->brt_ifp == ifp && (full || 2831 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) 2832 bridge_rtnode_destroy(sc, brt); 2833 } 2834 } 2835 2836 /* 2837 * bridge_rtable_init: 2838 * 2839 * Initialize the route table for this bridge. 2840 */ 2841 static void 2842 bridge_rtable_init(struct bridge_softc *sc) 2843 { 2844 int i; 2845 2846 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE, 2847 M_DEVBUF, M_WAITOK); 2848 2849 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++) 2850 LIST_INIT(&sc->sc_rthash[i]); 2851 2852 sc->sc_rthash_key = arc4random(); 2853 LIST_INIT(&sc->sc_rtlist); 2854 } 2855 2856 /* 2857 * bridge_rtable_fini: 2858 * 2859 * Deconstruct the route table for this bridge. 2860 */ 2861 static void 2862 bridge_rtable_fini(struct bridge_softc *sc) 2863 { 2864 2865 KASSERT(sc->sc_brtcnt == 0, 2866 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt)); 2867 free(sc->sc_rthash, M_DEVBUF); 2868 } 2869 2870 /* 2871 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 2872 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 2873 */ 2874 #define mix(a, b, c) \ 2875 do { \ 2876 a -= b; a -= c; a ^= (c >> 13); \ 2877 b -= c; b -= a; b ^= (a << 8); \ 2878 c -= a; c -= b; c ^= (b >> 13); \ 2879 a -= b; a -= c; a ^= (c >> 12); \ 2880 b -= c; b -= a; b ^= (a << 16); \ 2881 c -= a; c -= b; c ^= (b >> 5); \ 2882 a -= b; a -= c; a ^= (c >> 3); \ 2883 b -= c; b -= a; b ^= (a << 10); \ 2884 c -= a; c -= b; c ^= (b >> 15); \ 2885 } while (/*CONSTCOND*/0) 2886 2887 static __inline uint32_t 2888 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) 2889 { 2890 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; 2891 2892 b += addr[5] << 8; 2893 b += addr[4]; 2894 a += addr[3] << 24; 2895 a += addr[2] << 16; 2896 a += addr[1] << 8; 2897 a += addr[0]; 2898 2899 mix(a, b, c); 2900 2901 return (c & BRIDGE_RTHASH_MASK); 2902 } 2903 2904 #undef mix 2905 2906 static int 2907 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b) 2908 { 2909 int i, d; 2910 2911 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { 2912 d = ((int)a[i]) - ((int)b[i]); 2913 } 2914 2915 return (d); 2916 } 2917 2918 /* 2919 * bridge_rtnode_lookup: 2920 * 2921 * Look up a bridge route node for the specified destination. Compare the 2922 * vlan id or if zero then just return the first match. 2923 */ 2924 static struct bridge_rtnode * 2925 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) 2926 { 2927 struct bridge_rtnode *brt; 2928 uint32_t hash; 2929 int dir; 2930 2931 BRIDGE_LOCK_ASSERT(sc); 2932 2933 hash = bridge_rthash(sc, addr); 2934 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) { 2935 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr); 2936 if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0)) 2937 return (brt); 2938 if (dir > 0) 2939 return (NULL); 2940 } 2941 2942 return (NULL); 2943 } 2944 2945 /* 2946 * bridge_rtnode_insert: 2947 * 2948 * Insert the specified bridge node into the route table. We 2949 * assume the entry is not already in the table. 2950 */ 2951 static int 2952 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) 2953 { 2954 struct bridge_rtnode *lbrt; 2955 uint32_t hash; 2956 int dir; 2957 2958 BRIDGE_LOCK_ASSERT(sc); 2959 2960 hash = bridge_rthash(sc, brt->brt_addr); 2961 2962 lbrt = LIST_FIRST(&sc->sc_rthash[hash]); 2963 if (lbrt == NULL) { 2964 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash); 2965 goto out; 2966 } 2967 2968 do { 2969 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr); 2970 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan) 2971 return (EEXIST); 2972 if (dir > 0) { 2973 LIST_INSERT_BEFORE(lbrt, brt, brt_hash); 2974 goto out; 2975 } 2976 if (LIST_NEXT(lbrt, brt_hash) == NULL) { 2977 LIST_INSERT_AFTER(lbrt, brt, brt_hash); 2978 goto out; 2979 } 2980 lbrt = LIST_NEXT(lbrt, brt_hash); 2981 } while (lbrt != NULL); 2982 2983 #ifdef DIAGNOSTIC 2984 panic("bridge_rtnode_insert: impossible"); 2985 #endif 2986 2987 out: 2988 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list); 2989 sc->sc_brtcnt++; 2990 2991 return (0); 2992 } 2993 2994 /* 2995 * bridge_rtnode_destroy: 2996 * 2997 * Destroy a bridge rtnode. 2998 */ 2999 static void 3000 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt) 3001 { 3002 BRIDGE_LOCK_ASSERT(sc); 3003 3004 LIST_REMOVE(brt, brt_hash); 3005 3006 LIST_REMOVE(brt, brt_list); 3007 sc->sc_brtcnt--; 3008 brt->brt_dst->bif_addrcnt--; 3009 uma_zfree(bridge_rtnode_zone, brt); 3010 } 3011 3012 /* 3013 * bridge_rtable_expire: 3014 * 3015 * Set the expiry time for all routes on an interface. 3016 */ 3017 static void 3018 bridge_rtable_expire(struct ifnet *ifp, int age) 3019 { 3020 struct bridge_softc *sc = ifp->if_bridge; 3021 struct bridge_rtnode *brt; 3022 3023 BRIDGE_LOCK(sc); 3024 3025 /* 3026 * If the age is zero then flush, otherwise set all the expiry times to 3027 * age for the interface 3028 */ 3029 if (age == 0) 3030 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN); 3031 else { 3032 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { 3033 /* Cap the expiry time to 'age' */ 3034 if (brt->brt_ifp == ifp && 3035 brt->brt_expire > time_uptime + age && 3036 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 3037 brt->brt_expire = time_uptime + age; 3038 } 3039 } 3040 BRIDGE_UNLOCK(sc); 3041 } 3042 3043 /* 3044 * bridge_state_change: 3045 * 3046 * Callback from the bridgestp code when a port changes states. 3047 */ 3048 static void 3049 bridge_state_change(struct ifnet *ifp, int state) 3050 { 3051 struct bridge_softc *sc = ifp->if_bridge; 3052 static const char *stpstates[] = { 3053 "disabled", 3054 "listening", 3055 "learning", 3056 "forwarding", 3057 "blocking", 3058 "discarding" 3059 }; 3060 3061 if (V_log_stp) 3062 log(LOG_NOTICE, "%s: state changed to %s on %s\n", 3063 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname); 3064 } 3065 3066 /* 3067 * Send bridge packets through pfil if they are one of the types pfil can deal 3068 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without 3069 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for 3070 * that interface. 3071 */ 3072 static int 3073 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir) 3074 { 3075 int snap, error, i, hlen; 3076 struct ether_header *eh1, eh2; 3077 struct ip *ip; 3078 struct llc llc1; 3079 u_int16_t ether_type; 3080 3081 snap = 0; 3082 error = -1; /* Default error if not error == 0 */ 3083 3084 #if 0 3085 /* we may return with the IP fields swapped, ensure its not shared */ 3086 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__)); 3087 #endif 3088 3089 if (V_pfil_bridge == 0 && V_pfil_member == 0 && V_pfil_ipfw == 0) 3090 return (0); /* filtering is disabled */ 3091 3092 i = min((*mp)->m_pkthdr.len, max_protohdr); 3093 if ((*mp)->m_len < i) { 3094 *mp = m_pullup(*mp, i); 3095 if (*mp == NULL) { 3096 printf("%s: m_pullup failed\n", __func__); 3097 return (-1); 3098 } 3099 } 3100 3101 eh1 = mtod(*mp, struct ether_header *); 3102 ether_type = ntohs(eh1->ether_type); 3103 3104 /* 3105 * Check for SNAP/LLC. 3106 */ 3107 if (ether_type < ETHERMTU) { 3108 struct llc *llc2 = (struct llc *)(eh1 + 1); 3109 3110 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && 3111 llc2->llc_dsap == LLC_SNAP_LSAP && 3112 llc2->llc_ssap == LLC_SNAP_LSAP && 3113 llc2->llc_control == LLC_UI) { 3114 ether_type = htons(llc2->llc_un.type_snap.ether_type); 3115 snap = 1; 3116 } 3117 } 3118 3119 /* 3120 * If we're trying to filter bridge traffic, don't look at anything 3121 * other than IP and ARP traffic. If the filter doesn't understand 3122 * IPv6, don't allow IPv6 through the bridge either. This is lame 3123 * since if we really wanted, say, an AppleTalk filter, we are hosed, 3124 * but of course we don't have an AppleTalk filter to begin with. 3125 * (Note that since pfil doesn't understand ARP it will pass *ALL* 3126 * ARP traffic.) 3127 */ 3128 switch (ether_type) { 3129 case ETHERTYPE_ARP: 3130 case ETHERTYPE_REVARP: 3131 if (V_pfil_ipfw_arp == 0) 3132 return (0); /* Automatically pass */ 3133 break; 3134 3135 case ETHERTYPE_IP: 3136 #ifdef INET6 3137 case ETHERTYPE_IPV6: 3138 #endif /* INET6 */ 3139 break; 3140 default: 3141 /* 3142 * Check to see if the user wants to pass non-ip 3143 * packets, these will not be checked by pfil(9) and 3144 * passed unconditionally so the default is to drop. 3145 */ 3146 if (V_pfil_onlyip) 3147 goto bad; 3148 } 3149 3150 /* Run the packet through pfil before stripping link headers */ 3151 if (PFIL_HOOKED(&V_link_pfil_hook) && V_pfil_ipfw != 0 && 3152 dir == PFIL_OUT && ifp != NULL) { 3153 3154 error = pfil_run_hooks(&V_link_pfil_hook, mp, ifp, dir, NULL); 3155 3156 if (*mp == NULL || error != 0) /* packet consumed by filter */ 3157 return (error); 3158 } 3159 3160 /* Strip off the Ethernet header and keep a copy. */ 3161 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2); 3162 m_adj(*mp, ETHER_HDR_LEN); 3163 3164 /* Strip off snap header, if present */ 3165 if (snap) { 3166 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1); 3167 m_adj(*mp, sizeof(struct llc)); 3168 } 3169 3170 /* 3171 * Check the IP header for alignment and errors 3172 */ 3173 if (dir == PFIL_IN) { 3174 switch (ether_type) { 3175 case ETHERTYPE_IP: 3176 error = bridge_ip_checkbasic(mp); 3177 break; 3178 #ifdef INET6 3179 case ETHERTYPE_IPV6: 3180 error = bridge_ip6_checkbasic(mp); 3181 break; 3182 #endif /* INET6 */ 3183 default: 3184 error = 0; 3185 } 3186 if (error) 3187 goto bad; 3188 } 3189 3190 error = 0; 3191 3192 /* 3193 * Run the packet through pfil 3194 */ 3195 switch (ether_type) { 3196 case ETHERTYPE_IP: 3197 /* 3198 * Run pfil on the member interface and the bridge, both can 3199 * be skipped by clearing pfil_member or pfil_bridge. 3200 * 3201 * Keep the order: 3202 * in_if -> bridge_if -> out_if 3203 */ 3204 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL) 3205 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp, 3206 dir, NULL); 3207 3208 if (*mp == NULL || error != 0) /* filter may consume */ 3209 break; 3210 3211 if (V_pfil_member && ifp != NULL) 3212 error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp, 3213 dir, NULL); 3214 3215 if (*mp == NULL || error != 0) /* filter may consume */ 3216 break; 3217 3218 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL) 3219 error = pfil_run_hooks(&V_inet_pfil_hook, mp, bifp, 3220 dir, NULL); 3221 3222 if (*mp == NULL || error != 0) /* filter may consume */ 3223 break; 3224 3225 /* check if we need to fragment the packet */ 3226 if (V_pfil_member && ifp != NULL && dir == PFIL_OUT) { 3227 i = (*mp)->m_pkthdr.len; 3228 if (i > ifp->if_mtu) { 3229 error = bridge_fragment(ifp, *mp, &eh2, snap, 3230 &llc1); 3231 return (error); 3232 } 3233 } 3234 3235 /* Recalculate the ip checksum. */ 3236 ip = mtod(*mp, struct ip *); 3237 hlen = ip->ip_hl << 2; 3238 if (hlen < sizeof(struct ip)) 3239 goto bad; 3240 if (hlen > (*mp)->m_len) { 3241 if ((*mp = m_pullup(*mp, hlen)) == 0) 3242 goto bad; 3243 ip = mtod(*mp, struct ip *); 3244 if (ip == NULL) 3245 goto bad; 3246 } 3247 ip->ip_sum = 0; 3248 if (hlen == sizeof(struct ip)) 3249 ip->ip_sum = in_cksum_hdr(ip); 3250 else 3251 ip->ip_sum = in_cksum(*mp, hlen); 3252 3253 break; 3254 #ifdef INET6 3255 case ETHERTYPE_IPV6: 3256 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL) 3257 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp, 3258 dir, NULL); 3259 3260 if (*mp == NULL || error != 0) /* filter may consume */ 3261 break; 3262 3263 if (V_pfil_member && ifp != NULL) 3264 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, ifp, 3265 dir, NULL); 3266 3267 if (*mp == NULL || error != 0) /* filter may consume */ 3268 break; 3269 3270 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL) 3271 error = pfil_run_hooks(&V_inet6_pfil_hook, mp, bifp, 3272 dir, NULL); 3273 break; 3274 #endif 3275 default: 3276 error = 0; 3277 break; 3278 } 3279 3280 if (*mp == NULL) 3281 return (error); 3282 if (error != 0) 3283 goto bad; 3284 3285 error = -1; 3286 3287 /* 3288 * Finally, put everything back the way it was and return 3289 */ 3290 if (snap) { 3291 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT); 3292 if (*mp == NULL) 3293 return (error); 3294 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc)); 3295 } 3296 3297 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT); 3298 if (*mp == NULL) 3299 return (error); 3300 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN); 3301 3302 return (0); 3303 3304 bad: 3305 m_freem(*mp); 3306 *mp = NULL; 3307 return (error); 3308 } 3309 3310 /* 3311 * Perform basic checks on header size since 3312 * pfil assumes ip_input has already processed 3313 * it for it. Cut-and-pasted from ip_input.c. 3314 * Given how simple the IPv6 version is, 3315 * does the IPv4 version really need to be 3316 * this complicated? 3317 * 3318 * XXX Should we update ipstat here, or not? 3319 * XXX Right now we update ipstat but not 3320 * XXX csum_counter. 3321 */ 3322 static int 3323 bridge_ip_checkbasic(struct mbuf **mp) 3324 { 3325 struct mbuf *m = *mp; 3326 struct ip *ip; 3327 int len, hlen; 3328 u_short sum; 3329 3330 if (*mp == NULL) 3331 return (-1); 3332 3333 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 3334 if ((m = m_copyup(m, sizeof(struct ip), 3335 (max_linkhdr + 3) & ~3)) == NULL) { 3336 /* XXXJRT new stat, please */ 3337 KMOD_IPSTAT_INC(ips_toosmall); 3338 goto bad; 3339 } 3340 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 3341 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 3342 KMOD_IPSTAT_INC(ips_toosmall); 3343 goto bad; 3344 } 3345 } 3346 ip = mtod(m, struct ip *); 3347 if (ip == NULL) goto bad; 3348 3349 if (ip->ip_v != IPVERSION) { 3350 KMOD_IPSTAT_INC(ips_badvers); 3351 goto bad; 3352 } 3353 hlen = ip->ip_hl << 2; 3354 if (hlen < sizeof(struct ip)) { /* minimum header length */ 3355 KMOD_IPSTAT_INC(ips_badhlen); 3356 goto bad; 3357 } 3358 if (hlen > m->m_len) { 3359 if ((m = m_pullup(m, hlen)) == 0) { 3360 KMOD_IPSTAT_INC(ips_badhlen); 3361 goto bad; 3362 } 3363 ip = mtod(m, struct ip *); 3364 if (ip == NULL) goto bad; 3365 } 3366 3367 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 3368 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 3369 } else { 3370 if (hlen == sizeof(struct ip)) { 3371 sum = in_cksum_hdr(ip); 3372 } else { 3373 sum = in_cksum(m, hlen); 3374 } 3375 } 3376 if (sum) { 3377 KMOD_IPSTAT_INC(ips_badsum); 3378 goto bad; 3379 } 3380 3381 /* Retrieve the packet length. */ 3382 len = ntohs(ip->ip_len); 3383 3384 /* 3385 * Check for additional length bogosity 3386 */ 3387 if (len < hlen) { 3388 KMOD_IPSTAT_INC(ips_badlen); 3389 goto bad; 3390 } 3391 3392 /* 3393 * Check that the amount of data in the buffers 3394 * is as at least much as the IP header would have us expect. 3395 * Drop packet if shorter than we expect. 3396 */ 3397 if (m->m_pkthdr.len < len) { 3398 KMOD_IPSTAT_INC(ips_tooshort); 3399 goto bad; 3400 } 3401 3402 /* Checks out, proceed */ 3403 *mp = m; 3404 return (0); 3405 3406 bad: 3407 *mp = m; 3408 return (-1); 3409 } 3410 3411 #ifdef INET6 3412 /* 3413 * Same as above, but for IPv6. 3414 * Cut-and-pasted from ip6_input.c. 3415 * XXX Should we update ip6stat, or not? 3416 */ 3417 static int 3418 bridge_ip6_checkbasic(struct mbuf **mp) 3419 { 3420 struct mbuf *m = *mp; 3421 struct ip6_hdr *ip6; 3422 3423 /* 3424 * If the IPv6 header is not aligned, slurp it up into a new 3425 * mbuf with space for link headers, in the event we forward 3426 * it. Otherwise, if it is aligned, make sure the entire base 3427 * IPv6 header is in the first mbuf of the chain. 3428 */ 3429 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 3430 struct ifnet *inifp = m->m_pkthdr.rcvif; 3431 if ((m = m_copyup(m, sizeof(struct ip6_hdr), 3432 (max_linkhdr + 3) & ~3)) == NULL) { 3433 /* XXXJRT new stat, please */ 3434 IP6STAT_INC(ip6s_toosmall); 3435 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 3436 goto bad; 3437 } 3438 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { 3439 struct ifnet *inifp = m->m_pkthdr.rcvif; 3440 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 3441 IP6STAT_INC(ip6s_toosmall); 3442 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 3443 goto bad; 3444 } 3445 } 3446 3447 ip6 = mtod(m, struct ip6_hdr *); 3448 3449 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 3450 IP6STAT_INC(ip6s_badvers); 3451 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 3452 goto bad; 3453 } 3454 3455 /* Checks out, proceed */ 3456 *mp = m; 3457 return (0); 3458 3459 bad: 3460 *mp = m; 3461 return (-1); 3462 } 3463 #endif /* INET6 */ 3464 3465 /* 3466 * bridge_fragment: 3467 * 3468 * Return a fragmented mbuf chain. 3469 */ 3470 static int 3471 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh, 3472 int snap, struct llc *llc) 3473 { 3474 struct mbuf *m0; 3475 struct ip *ip; 3476 int error = -1; 3477 3478 if (m->m_len < sizeof(struct ip) && 3479 (m = m_pullup(m, sizeof(struct ip))) == NULL) 3480 goto out; 3481 ip = mtod(m, struct ip *); 3482 3483 m->m_pkthdr.csum_flags |= CSUM_IP; 3484 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist); 3485 if (error) 3486 goto out; 3487 3488 /* walk the chain and re-add the Ethernet header */ 3489 for (m0 = m; m0; m0 = m0->m_nextpkt) { 3490 if (error == 0) { 3491 if (snap) { 3492 M_PREPEND(m0, sizeof(struct llc), M_NOWAIT); 3493 if (m0 == NULL) { 3494 error = ENOBUFS; 3495 continue; 3496 } 3497 bcopy(llc, mtod(m0, caddr_t), 3498 sizeof(struct llc)); 3499 } 3500 M_PREPEND(m0, ETHER_HDR_LEN, M_NOWAIT); 3501 if (m0 == NULL) { 3502 error = ENOBUFS; 3503 continue; 3504 } 3505 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN); 3506 } else 3507 m_freem(m); 3508 } 3509 3510 if (error == 0) 3511 KMOD_IPSTAT_INC(ips_fragmented); 3512 3513 return (error); 3514 3515 out: 3516 if (m != NULL) 3517 m_freem(m); 3518 return (error); 3519 } 3520 3521 static void 3522 bridge_linkstate(struct ifnet *ifp) 3523 { 3524 struct bridge_softc *sc = ifp->if_bridge; 3525 struct bridge_iflist *bif; 3526 3527 BRIDGE_LOCK(sc); 3528 bif = bridge_lookup_member_if(sc, ifp); 3529 if (bif == NULL) { 3530 BRIDGE_UNLOCK(sc); 3531 return; 3532 } 3533 bridge_linkcheck(sc); 3534 BRIDGE_UNLOCK(sc); 3535 3536 bstp_linkstate(&bif->bif_stp); 3537 } 3538 3539 static void 3540 bridge_linkcheck(struct bridge_softc *sc) 3541 { 3542 struct bridge_iflist *bif; 3543 int new_link, hasls; 3544 3545 BRIDGE_LOCK_ASSERT(sc); 3546 new_link = LINK_STATE_DOWN; 3547 hasls = 0; 3548 /* Our link is considered up if at least one of our ports is active */ 3549 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 3550 if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE) 3551 hasls++; 3552 if (bif->bif_ifp->if_link_state == LINK_STATE_UP) { 3553 new_link = LINK_STATE_UP; 3554 break; 3555 } 3556 } 3557 if (!LIST_EMPTY(&sc->sc_iflist) && !hasls) { 3558 /* If no interfaces support link-state then we default to up */ 3559 new_link = LINK_STATE_UP; 3560 } 3561 if_link_state_change(sc->sc_ifp, new_link); 3562 } 3563