1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright 2001 Wasabi Systems, Inc. 7 * All rights reserved. 8 * 9 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed for the NetBSD Project by 22 * Wasabi Systems, Inc. 23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 24 * or promote products derived from this software without specific prior 25 * written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 42 * All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 55 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 56 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 61 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 62 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 * POSSIBILITY OF SUCH DAMAGE. 64 * 65 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp 66 */ 67 68 /* 69 * Network interface bridge support. 70 * 71 * TODO: 72 * 73 * - Currently only supports Ethernet-like interfaces (Ethernet, 74 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way 75 * to bridge other types of interfaces (maybe consider 76 * heterogeneous bridges). 77 */ 78 79 #include "opt_inet.h" 80 #include "opt_inet6.h" 81 82 #define EXTERR_CATEGORY EXTERR_CAT_BRIDGE 83 84 #include <sys/param.h> 85 #include <sys/ctype.h> /* string functions */ 86 #include <sys/eventhandler.h> 87 #include <sys/exterrvar.h> 88 #include <sys/jail.h> 89 #include <sys/kernel.h> 90 #include <sys/lock.h> 91 #include <sys/malloc.h> 92 #include <sys/mbuf.h> 93 #include <sys/module.h> 94 #include <sys/mutex.h> 95 #include <sys/priv.h> 96 #include <sys/proc.h> 97 #include <sys/protosw.h> 98 #include <sys/random.h> 99 #include <sys/systm.h> 100 #include <sys/socket.h> /* for net/if.h */ 101 #include <sys/sockio.h> 102 #include <sys/syslog.h> 103 #include <sys/sysctl.h> 104 #include <sys/time.h> 105 106 #include <vm/uma.h> 107 108 #include <net/bpf.h> 109 #include <net/if.h> 110 #include <net/if_clone.h> 111 #include <net/if_dl.h> 112 #include <net/if_types.h> 113 #include <net/if_var.h> 114 #include <net/if_private.h> 115 #include <net/pfil.h> 116 #include <net/vnet.h> 117 118 #include <netinet/in.h> 119 #include <netinet/in_systm.h> 120 #include <netinet/in_var.h> 121 #include <netinet/ip.h> 122 #include <netinet/ip_var.h> 123 #ifdef INET6 124 #include <netinet/ip6.h> 125 #include <netinet6/ip6_var.h> 126 #include <netinet6/in6_ifattach.h> 127 #endif 128 #if defined(INET) || defined(INET6) 129 #include <netinet/ip_carp.h> 130 #endif 131 #include <machine/in_cksum.h> 132 #include <netinet/if_ether.h> 133 #include <net/bridgestp.h> 134 #include <net/if_bridgevar.h> 135 #include <net/if_llc.h> 136 #include <net/if_vlan_var.h> 137 138 #include <net/route.h> 139 140 /* 141 * At various points in the code we need to know if we're hooked into the INET 142 * and/or INET6 pfil. Define some macros to do that based on which IP versions 143 * are enabled in the kernel. This avoids littering the rest of the code with 144 * #ifnet INET6 to avoid referencing V_inet6_pfil_head. 145 */ 146 #ifdef INET6 147 #define PFIL_HOOKED_IN_INET6 PFIL_HOOKED_IN(V_inet6_pfil_head) 148 #define PFIL_HOOKED_OUT_INET6 PFIL_HOOKED_OUT(V_inet6_pfil_head) 149 #else 150 #define PFIL_HOOKED_IN_INET6 false 151 #define PFIL_HOOKED_OUT_INET6 false 152 #endif 153 154 #ifdef INET 155 #define PFIL_HOOKED_IN_INET PFIL_HOOKED_IN(V_inet_pfil_head) 156 #define PFIL_HOOKED_OUT_INET PFIL_HOOKED_OUT(V_inet_pfil_head) 157 #else 158 #define PFIL_HOOKED_IN_INET false 159 #define PFIL_HOOKED_OUT_INET false 160 #endif 161 162 #define PFIL_HOOKED_IN_46 (PFIL_HOOKED_IN_INET6 || PFIL_HOOKED_IN_INET) 163 #define PFIL_HOOKED_OUT_46 (PFIL_HOOKED_OUT_INET6 || PFIL_HOOKED_OUT_INET) 164 165 /* 166 * Size of the route hash table. Must be a power of two. 167 */ 168 #ifndef BRIDGE_RTHASH_SIZE 169 #define BRIDGE_RTHASH_SIZE 1024 170 #endif 171 172 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1) 173 174 /* 175 * Default maximum number of addresses to cache. 176 */ 177 #ifndef BRIDGE_RTABLE_MAX 178 #define BRIDGE_RTABLE_MAX 2000 179 #endif 180 181 /* 182 * Timeout (in seconds) for entries learned dynamically. 183 */ 184 #ifndef BRIDGE_RTABLE_TIMEOUT 185 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ 186 #endif 187 188 /* 189 * Number of seconds between walks of the route list. 190 */ 191 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD 192 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) 193 #endif 194 195 /* 196 * List of capabilities to possibly mask on the member interface. 197 */ 198 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM|\ 199 IFCAP_TXCSUM_IPV6|IFCAP_MEXTPG) 200 201 /* 202 * List of capabilities to strip 203 */ 204 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO 205 206 /* 207 * Bridge locking 208 * 209 * The bridge relies heavily on the epoch(9) system to protect its data 210 * structures. This means we can safely use CK_LISTs while in NET_EPOCH, but we 211 * must ensure there is only one writer at a time. 212 * 213 * That is: for read accesses we only need to be in NET_EPOCH, but for write 214 * accesses we must hold: 215 * 216 * - BRIDGE_RT_LOCK, for any change to bridge_rtnodes 217 * - BRIDGE_LOCK, for any other change 218 * 219 * The BRIDGE_LOCK is a sleepable lock, because it is held across ioctl() 220 * calls to bridge member interfaces and these ioctl()s can sleep. 221 * The BRIDGE_RT_LOCK is a non-sleepable mutex, because it is sometimes 222 * required while we're in NET_EPOCH and then we're not allowed to sleep. 223 */ 224 #define BRIDGE_LOCK_INIT(_sc) do { \ 225 sx_init(&(_sc)->sc_sx, "if_bridge"); \ 226 mtx_init(&(_sc)->sc_rt_mtx, "if_bridge rt", NULL, MTX_DEF); \ 227 } while (0) 228 #define BRIDGE_LOCK_DESTROY(_sc) do { \ 229 sx_destroy(&(_sc)->sc_sx); \ 230 mtx_destroy(&(_sc)->sc_rt_mtx); \ 231 } while (0) 232 #define BRIDGE_LOCK(_sc) sx_xlock(&(_sc)->sc_sx) 233 #define BRIDGE_UNLOCK(_sc) sx_xunlock(&(_sc)->sc_sx) 234 #define BRIDGE_LOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SX_XLOCKED) 235 #define BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(_sc) \ 236 MPASS(in_epoch(net_epoch_preempt) || sx_xlocked(&(_sc)->sc_sx)) 237 #define BRIDGE_UNLOCK_ASSERT(_sc) sx_assert(&(_sc)->sc_sx, SX_UNLOCKED) 238 #define BRIDGE_RT_LOCK(_sc) mtx_lock(&(_sc)->sc_rt_mtx) 239 #define BRIDGE_RT_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_rt_mtx) 240 #define BRIDGE_RT_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_rt_mtx, MA_OWNED) 241 #define BRIDGE_RT_LOCK_OR_NET_EPOCH_ASSERT(_sc) \ 242 MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(_sc)->sc_rt_mtx)) 243 244 struct bridge_softc; 245 246 /* 247 * Bridge interface list entry. 248 */ 249 struct bridge_iflist { 250 CK_LIST_ENTRY(bridge_iflist) bif_next; 251 struct ifnet *bif_ifp; /* member if */ 252 struct bridge_softc *bif_sc; /* parent bridge */ 253 struct bstp_port bif_stp; /* STP state */ 254 uint32_t bif_flags; /* member if flags */ 255 int bif_savedcaps; /* saved capabilities */ 256 uint32_t bif_addrmax; /* max # of addresses */ 257 uint32_t bif_addrcnt; /* cur. # of addresses */ 258 uint32_t bif_addrexceeded;/* # of address violations */ 259 struct epoch_context bif_epoch_ctx; 260 ether_vlanid_t bif_untagged; /* untagged vlan id */ 261 ifbvlan_set_t bif_vlan_set; /* allowed tagged vlans */ 262 }; 263 264 /* 265 * Bridge route node. 266 */ 267 struct bridge_rtnode { 268 CK_LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */ 269 CK_LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */ 270 struct bridge_iflist *brt_dst; /* destination if */ 271 unsigned long brt_expire; /* expiration time */ 272 uint8_t brt_flags; /* address flags */ 273 uint8_t brt_addr[ETHER_ADDR_LEN]; 274 ether_vlanid_t brt_vlan; /* vlan id */ 275 struct vnet *brt_vnet; 276 struct epoch_context brt_epoch_ctx; 277 }; 278 #define brt_ifp brt_dst->bif_ifp 279 280 /* 281 * Software state for each bridge. 282 */ 283 struct bridge_softc { 284 struct ifnet *sc_ifp; /* make this an interface */ 285 LIST_ENTRY(bridge_softc) sc_list; 286 struct sx sc_sx; 287 struct mtx sc_rt_mtx; 288 uint32_t sc_brtmax; /* max # of addresses */ 289 uint32_t sc_brtcnt; /* cur. # of addresses */ 290 uint32_t sc_brttimeout; /* rt timeout in seconds */ 291 struct callout sc_brcallout; /* bridge callout */ 292 CK_LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */ 293 CK_LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */ 294 CK_LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */ 295 uint32_t sc_rthash_key; /* key for hash */ 296 CK_LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */ 297 struct bstp_state sc_stp; /* STP state */ 298 uint32_t sc_brtexceeded; /* # of cache drops */ 299 struct ifnet *sc_ifaddr; /* member mac copied from */ 300 struct ether_addr sc_defaddr; /* Default MAC address */ 301 if_input_fn_t sc_if_input; /* Saved copy of if_input */ 302 struct epoch_context sc_epoch_ctx; 303 }; 304 305 VNET_DEFINE_STATIC(struct sx, bridge_list_sx); 306 #define V_bridge_list_sx VNET(bridge_list_sx) 307 static eventhandler_tag bridge_detach_cookie; 308 309 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; 310 311 VNET_DEFINE_STATIC(uma_zone_t, bridge_rtnode_zone); 312 #define V_bridge_rtnode_zone VNET(bridge_rtnode_zone) 313 314 static int bridge_clone_create(struct if_clone *, char *, size_t, 315 struct ifc_data *, struct ifnet **); 316 static int bridge_clone_destroy(struct if_clone *, struct ifnet *, uint32_t); 317 318 static int bridge_ioctl(struct ifnet *, u_long, caddr_t); 319 static void bridge_mutecaps(struct bridge_softc *); 320 static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *, 321 int); 322 static void bridge_ifdetach(void *arg __unused, struct ifnet *); 323 static void bridge_init(void *); 324 static void bridge_dummynet(struct mbuf *, struct ifnet *); 325 static bool bridge_same(const void *, const void *); 326 static void *bridge_get_softc(struct ifnet *); 327 static void bridge_stop(struct ifnet *, int); 328 static int bridge_transmit(struct ifnet *, struct mbuf *); 329 #ifdef ALTQ 330 static void bridge_altq_start(if_t); 331 static int bridge_altq_transmit(if_t, struct mbuf *); 332 #endif 333 static void bridge_qflush(struct ifnet *); 334 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *); 335 static void bridge_inject(struct ifnet *, struct mbuf *); 336 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *, 337 struct rtentry *); 338 static int bridge_enqueue(struct bridge_softc *, struct ifnet *, 339 struct mbuf *, struct bridge_iflist *); 340 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int); 341 342 static void bridge_forward(struct bridge_softc *, struct bridge_iflist *, 343 struct mbuf *m); 344 static bool bridge_member_ifaddrs(void); 345 static void bridge_timer(void *); 346 347 static void bridge_broadcast(struct bridge_softc *, struct ifnet *, 348 struct mbuf *, int); 349 static void bridge_span(struct bridge_softc *, struct mbuf *); 350 351 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *, 352 ether_vlanid_t, struct bridge_iflist *, int, uint8_t); 353 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *, 354 ether_vlanid_t); 355 static void bridge_rttrim(struct bridge_softc *); 356 static void bridge_rtage(struct bridge_softc *); 357 static void bridge_rtflush(struct bridge_softc *, int); 358 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *, 359 ether_vlanid_t); 360 static bool bridge_vfilter_in(const struct bridge_iflist *, struct mbuf *); 361 static bool bridge_vfilter_out(const struct bridge_iflist *, 362 const struct mbuf *); 363 364 static void bridge_rtable_init(struct bridge_softc *); 365 static void bridge_rtable_fini(struct bridge_softc *); 366 367 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *); 368 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, 369 const uint8_t *, ether_vlanid_t); 370 static int bridge_rtnode_insert(struct bridge_softc *, 371 struct bridge_rtnode *); 372 static void bridge_rtnode_destroy(struct bridge_softc *, 373 struct bridge_rtnode *); 374 static void bridge_rtable_expire(struct ifnet *, int); 375 static void bridge_state_change(struct ifnet *, int); 376 377 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, 378 const char *name); 379 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, 380 struct ifnet *ifp); 381 static void bridge_delete_member(struct bridge_softc *, 382 struct bridge_iflist *, int); 383 static void bridge_delete_span(struct bridge_softc *, 384 struct bridge_iflist *); 385 386 static int bridge_ioctl_add(struct bridge_softc *, void *); 387 static int bridge_ioctl_del(struct bridge_softc *, void *); 388 static int bridge_ioctl_gifflags(struct bridge_softc *, void *); 389 static int bridge_ioctl_sifflags(struct bridge_softc *, void *); 390 static int bridge_ioctl_scache(struct bridge_softc *, void *); 391 static int bridge_ioctl_gcache(struct bridge_softc *, void *); 392 static int bridge_ioctl_gifs(struct bridge_softc *, void *); 393 static int bridge_ioctl_rts(struct bridge_softc *, void *); 394 static int bridge_ioctl_saddr(struct bridge_softc *, void *); 395 static int bridge_ioctl_sto(struct bridge_softc *, void *); 396 static int bridge_ioctl_gto(struct bridge_softc *, void *); 397 static int bridge_ioctl_daddr(struct bridge_softc *, void *); 398 static int bridge_ioctl_flush(struct bridge_softc *, void *); 399 static int bridge_ioctl_gpri(struct bridge_softc *, void *); 400 static int bridge_ioctl_spri(struct bridge_softc *, void *); 401 static int bridge_ioctl_ght(struct bridge_softc *, void *); 402 static int bridge_ioctl_sht(struct bridge_softc *, void *); 403 static int bridge_ioctl_gfd(struct bridge_softc *, void *); 404 static int bridge_ioctl_sfd(struct bridge_softc *, void *); 405 static int bridge_ioctl_gma(struct bridge_softc *, void *); 406 static int bridge_ioctl_sma(struct bridge_softc *, void *); 407 static int bridge_ioctl_sifprio(struct bridge_softc *, void *); 408 static int bridge_ioctl_sifcost(struct bridge_softc *, void *); 409 static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *); 410 static int bridge_ioctl_sifuntagged(struct bridge_softc *, void *); 411 static int bridge_ioctl_sifvlanset(struct bridge_softc *, void *); 412 static int bridge_ioctl_gifvlanset(struct bridge_softc *, void *); 413 static int bridge_ioctl_addspan(struct bridge_softc *, void *); 414 static int bridge_ioctl_delspan(struct bridge_softc *, void *); 415 static int bridge_ioctl_gbparam(struct bridge_softc *, void *); 416 static int bridge_ioctl_grte(struct bridge_softc *, void *); 417 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *); 418 static int bridge_ioctl_sproto(struct bridge_softc *, void *); 419 static int bridge_ioctl_stxhc(struct bridge_softc *, void *); 420 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *, 421 int); 422 #ifdef INET 423 static int bridge_ip_checkbasic(struct mbuf **mp); 424 static int bridge_fragment(struct ifnet *, struct mbuf **mp, 425 struct ether_header *, int, struct llc *); 426 #endif /* INET */ 427 #ifdef INET6 428 static int bridge_ip6_checkbasic(struct mbuf **mp); 429 #endif /* INET6 */ 430 static void bridge_linkstate(struct ifnet *ifp); 431 static void bridge_linkcheck(struct bridge_softc *sc); 432 433 /* 434 * Use the "null" value from IEEE 802.1Q-2014 Table 9-2 435 * to indicate untagged frames. 436 */ 437 #define VLANTAGOF(_m) \ 438 ((_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : DOT1Q_VID_NULL) 439 440 static struct bstp_cb_ops bridge_ops = { 441 .bcb_state = bridge_state_change, 442 .bcb_rtage = bridge_rtable_expire 443 }; 444 445 SYSCTL_DECL(_net_link); 446 static SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 447 "Bridge"); 448 449 /* only pass IP[46] packets when pfil is enabled */ 450 VNET_DEFINE_STATIC(int, pfil_onlyip) = 1; 451 #define V_pfil_onlyip VNET(pfil_onlyip) 452 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, 453 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_onlyip), 0, 454 "Only pass IP packets when pfil is enabled"); 455 456 /* run pfil hooks on the bridge interface */ 457 VNET_DEFINE_STATIC(int, pfil_bridge) = 0; 458 #define V_pfil_bridge VNET(pfil_bridge) 459 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, 460 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_bridge), 0, 461 "Packet filter on the bridge interface"); 462 463 /* layer2 filter with ipfw */ 464 VNET_DEFINE_STATIC(int, pfil_ipfw); 465 #define V_pfil_ipfw VNET(pfil_ipfw) 466 467 /* layer2 ARP filter with ipfw */ 468 VNET_DEFINE_STATIC(int, pfil_ipfw_arp); 469 #define V_pfil_ipfw_arp VNET(pfil_ipfw_arp) 470 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, 471 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_ipfw_arp), 0, 472 "Filter ARP packets through IPFW layer2"); 473 474 /* run pfil hooks on the member interface */ 475 VNET_DEFINE_STATIC(int, pfil_member) = 0; 476 #define V_pfil_member VNET(pfil_member) 477 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, 478 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_member), 0, 479 "Packet filter on the member interface"); 480 481 /* run pfil hooks on the physical interface for locally destined packets */ 482 VNET_DEFINE_STATIC(int, pfil_local_phys); 483 #define V_pfil_local_phys VNET(pfil_local_phys) 484 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, 485 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(pfil_local_phys), 0, 486 "Packet filter on the physical interface for locally destined packets"); 487 488 /* log STP state changes */ 489 VNET_DEFINE_STATIC(int, log_stp); 490 #define V_log_stp VNET(log_stp) 491 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, 492 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(log_stp), 0, 493 "Log STP state changes"); 494 495 /* share MAC with first bridge member */ 496 VNET_DEFINE_STATIC(int, bridge_inherit_mac); 497 #define V_bridge_inherit_mac VNET(bridge_inherit_mac) 498 SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, 499 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(bridge_inherit_mac), 0, 500 "Inherit MAC address from the first bridge member"); 501 502 VNET_DEFINE_STATIC(int, allow_llz_overlap) = 0; 503 #define V_allow_llz_overlap VNET(allow_llz_overlap) 504 SYSCTL_INT(_net_link_bridge, OID_AUTO, allow_llz_overlap, 505 CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(allow_llz_overlap), 0, 506 "Allow overlap of link-local scope " 507 "zones of a bridge interface and the member interfaces"); 508 509 /* log MAC address port flapping */ 510 VNET_DEFINE_STATIC(bool, log_mac_flap) = true; 511 #define V_log_mac_flap VNET(log_mac_flap) 512 SYSCTL_BOOL(_net_link_bridge, OID_AUTO, log_mac_flap, 513 CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(log_mac_flap), true, 514 "Log MAC address port flapping"); 515 516 /* allow IP addresses on bridge members */ 517 VNET_DEFINE_STATIC(bool, member_ifaddrs) = false; 518 #define V_member_ifaddrs VNET(member_ifaddrs) 519 SYSCTL_BOOL(_net_link_bridge, OID_AUTO, member_ifaddrs, 520 CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(member_ifaddrs), false, 521 "Allow layer 3 addresses on bridge members"); 522 523 static bool 524 bridge_member_ifaddrs(void) 525 { 526 return (V_member_ifaddrs); 527 } 528 529 VNET_DEFINE_STATIC(int, log_interval) = 5; 530 VNET_DEFINE_STATIC(int, log_count) = 0; 531 VNET_DEFINE_STATIC(struct timeval, log_last) = { 0 }; 532 533 #define V_log_interval VNET(log_interval) 534 #define V_log_count VNET(log_count) 535 #define V_log_last VNET(log_last) 536 537 struct bridge_control { 538 int (*bc_func)(struct bridge_softc *, void *); 539 int bc_argsize; 540 int bc_flags; 541 }; 542 543 #define BC_F_COPYIN 0x01 /* copy arguments in */ 544 #define BC_F_COPYOUT 0x02 /* copy arguments out */ 545 #define BC_F_SUSER 0x04 /* do super-user check */ 546 547 static const struct bridge_control bridge_control_table[] = { 548 { bridge_ioctl_add, sizeof(struct ifbreq), 549 BC_F_COPYIN|BC_F_SUSER }, 550 { bridge_ioctl_del, sizeof(struct ifbreq), 551 BC_F_COPYIN|BC_F_SUSER }, 552 553 { bridge_ioctl_gifflags, sizeof(struct ifbreq), 554 BC_F_COPYIN|BC_F_COPYOUT }, 555 { bridge_ioctl_sifflags, sizeof(struct ifbreq), 556 BC_F_COPYIN|BC_F_SUSER }, 557 558 { bridge_ioctl_scache, sizeof(struct ifbrparam), 559 BC_F_COPYIN|BC_F_SUSER }, 560 { bridge_ioctl_gcache, sizeof(struct ifbrparam), 561 BC_F_COPYOUT }, 562 563 { bridge_ioctl_gifs, sizeof(struct ifbifconf), 564 BC_F_COPYIN|BC_F_COPYOUT }, 565 { bridge_ioctl_rts, sizeof(struct ifbaconf), 566 BC_F_COPYIN|BC_F_COPYOUT }, 567 568 { bridge_ioctl_saddr, sizeof(struct ifbareq), 569 BC_F_COPYIN|BC_F_SUSER }, 570 571 { bridge_ioctl_sto, sizeof(struct ifbrparam), 572 BC_F_COPYIN|BC_F_SUSER }, 573 { bridge_ioctl_gto, sizeof(struct ifbrparam), 574 BC_F_COPYOUT }, 575 576 { bridge_ioctl_daddr, sizeof(struct ifbareq), 577 BC_F_COPYIN|BC_F_SUSER }, 578 579 { bridge_ioctl_flush, sizeof(struct ifbreq), 580 BC_F_COPYIN|BC_F_SUSER }, 581 582 { bridge_ioctl_gpri, sizeof(struct ifbrparam), 583 BC_F_COPYOUT }, 584 { bridge_ioctl_spri, sizeof(struct ifbrparam), 585 BC_F_COPYIN|BC_F_SUSER }, 586 587 { bridge_ioctl_ght, sizeof(struct ifbrparam), 588 BC_F_COPYOUT }, 589 { bridge_ioctl_sht, sizeof(struct ifbrparam), 590 BC_F_COPYIN|BC_F_SUSER }, 591 592 { bridge_ioctl_gfd, sizeof(struct ifbrparam), 593 BC_F_COPYOUT }, 594 { bridge_ioctl_sfd, sizeof(struct ifbrparam), 595 BC_F_COPYIN|BC_F_SUSER }, 596 597 { bridge_ioctl_gma, sizeof(struct ifbrparam), 598 BC_F_COPYOUT }, 599 { bridge_ioctl_sma, sizeof(struct ifbrparam), 600 BC_F_COPYIN|BC_F_SUSER }, 601 602 { bridge_ioctl_sifprio, sizeof(struct ifbreq), 603 BC_F_COPYIN|BC_F_SUSER }, 604 605 { bridge_ioctl_sifcost, sizeof(struct ifbreq), 606 BC_F_COPYIN|BC_F_SUSER }, 607 608 { bridge_ioctl_addspan, sizeof(struct ifbreq), 609 BC_F_COPYIN|BC_F_SUSER }, 610 { bridge_ioctl_delspan, sizeof(struct ifbreq), 611 BC_F_COPYIN|BC_F_SUSER }, 612 613 { bridge_ioctl_gbparam, sizeof(struct ifbropreq), 614 BC_F_COPYOUT }, 615 616 { bridge_ioctl_grte, sizeof(struct ifbrparam), 617 BC_F_COPYOUT }, 618 619 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf), 620 BC_F_COPYIN|BC_F_COPYOUT }, 621 622 { bridge_ioctl_sproto, sizeof(struct ifbrparam), 623 BC_F_COPYIN|BC_F_SUSER }, 624 625 { bridge_ioctl_stxhc, sizeof(struct ifbrparam), 626 BC_F_COPYIN|BC_F_SUSER }, 627 628 { bridge_ioctl_sifmaxaddr, sizeof(struct ifbreq), 629 BC_F_COPYIN|BC_F_SUSER }, 630 631 { bridge_ioctl_sifuntagged, sizeof(struct ifbreq), 632 BC_F_COPYIN|BC_F_SUSER }, 633 634 { bridge_ioctl_sifvlanset, sizeof(struct ifbif_vlan_req), 635 BC_F_COPYIN|BC_F_SUSER }, 636 637 { bridge_ioctl_gifvlanset, sizeof(struct ifbif_vlan_req), 638 BC_F_COPYIN|BC_F_COPYOUT }, 639 }; 640 static const int bridge_control_table_size = nitems(bridge_control_table); 641 642 VNET_DEFINE_STATIC(LIST_HEAD(, bridge_softc), bridge_list) = 643 LIST_HEAD_INITIALIZER(); 644 #define V_bridge_list VNET(bridge_list) 645 #define BRIDGE_LIST_LOCK_INIT(x) sx_init(&V_bridge_list_sx, \ 646 "if_bridge list") 647 #define BRIDGE_LIST_LOCK_DESTROY(x) sx_destroy(&V_bridge_list_sx) 648 #define BRIDGE_LIST_LOCK(x) sx_xlock(&V_bridge_list_sx) 649 #define BRIDGE_LIST_UNLOCK(x) sx_xunlock(&V_bridge_list_sx) 650 651 VNET_DEFINE_STATIC(struct if_clone *, bridge_cloner); 652 #define V_bridge_cloner VNET(bridge_cloner) 653 654 static const char bridge_name[] = "bridge"; 655 656 static void 657 vnet_bridge_init(const void *unused __unused) 658 { 659 660 V_bridge_rtnode_zone = uma_zcreate("bridge_rtnode", 661 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL, 662 UMA_ALIGN_PTR, 0); 663 BRIDGE_LIST_LOCK_INIT(); 664 665 struct if_clone_addreq req = { 666 .create_f = bridge_clone_create, 667 .destroy_f = bridge_clone_destroy, 668 .flags = IFC_F_AUTOUNIT, 669 }; 670 V_bridge_cloner = ifc_attach_cloner(bridge_name, &req); 671 } 672 VNET_SYSINIT(vnet_bridge_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 673 vnet_bridge_init, NULL); 674 675 static void 676 vnet_bridge_uninit(const void *unused __unused) 677 { 678 679 ifc_detach_cloner(V_bridge_cloner); 680 V_bridge_cloner = NULL; 681 BRIDGE_LIST_LOCK_DESTROY(); 682 683 /* Callbacks may use the UMA zone. */ 684 NET_EPOCH_DRAIN_CALLBACKS(); 685 686 uma_zdestroy(V_bridge_rtnode_zone); 687 } 688 VNET_SYSUNINIT(vnet_bridge_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, 689 vnet_bridge_uninit, NULL); 690 691 static int 692 bridge_modevent(module_t mod, int type, void *data) 693 { 694 695 switch (type) { 696 case MOD_LOAD: 697 bridge_dn_p = bridge_dummynet; 698 bridge_same_p = bridge_same; 699 bridge_get_softc_p = bridge_get_softc; 700 bridge_member_ifaddrs_p = bridge_member_ifaddrs; 701 bridge_detach_cookie = EVENTHANDLER_REGISTER( 702 ifnet_departure_event, bridge_ifdetach, NULL, 703 EVENTHANDLER_PRI_ANY); 704 break; 705 case MOD_UNLOAD: 706 EVENTHANDLER_DEREGISTER(ifnet_departure_event, 707 bridge_detach_cookie); 708 bridge_dn_p = NULL; 709 bridge_same_p = NULL; 710 bridge_get_softc_p = NULL; 711 bridge_member_ifaddrs_p = NULL; 712 break; 713 default: 714 return (EOPNOTSUPP); 715 } 716 return (0); 717 } 718 719 static moduledata_t bridge_mod = { 720 "if_bridge", 721 bridge_modevent, 722 0 723 }; 724 725 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 726 MODULE_VERSION(if_bridge, 1); 727 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1); 728 729 /* 730 * handler for net.link.bridge.ipfw 731 */ 732 static int 733 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS) 734 { 735 int enable = V_pfil_ipfw; 736 int error; 737 738 error = sysctl_handle_int(oidp, &enable, 0, req); 739 enable &= 1; 740 741 if (enable != V_pfil_ipfw) { 742 V_pfil_ipfw = enable; 743 744 /* 745 * Disable pfil so that ipfw doesnt run twice, if the user 746 * really wants both then they can re-enable pfil_bridge and/or 747 * pfil_member. Also allow non-ip packets as ipfw can filter by 748 * layer2 type. 749 */ 750 if (V_pfil_ipfw) { 751 V_pfil_onlyip = 0; 752 V_pfil_bridge = 0; 753 V_pfil_member = 0; 754 } 755 } 756 757 return (error); 758 } 759 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, 760 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET | CTLFLAG_NEEDGIANT, 761 &VNET_NAME(pfil_ipfw), 0, &sysctl_pfil_ipfw, "I", 762 "Layer2 filter with IPFW"); 763 764 #ifdef VIMAGE 765 static void 766 bridge_reassign(struct ifnet *ifp, struct vnet *newvnet, char *arg) 767 { 768 struct bridge_softc *sc = ifp->if_softc; 769 struct bridge_iflist *bif; 770 771 BRIDGE_LOCK(sc); 772 773 while ((bif = CK_LIST_FIRST(&sc->sc_iflist)) != NULL) 774 bridge_delete_member(sc, bif, 0); 775 776 while ((bif = CK_LIST_FIRST(&sc->sc_spanlist)) != NULL) { 777 bridge_delete_span(sc, bif); 778 } 779 780 BRIDGE_UNLOCK(sc); 781 782 ether_reassign(ifp, newvnet, arg); 783 } 784 #endif 785 786 /* 787 * bridge_get_softc: 788 * 789 * Return the bridge softc for an ifnet. 790 */ 791 static void * 792 bridge_get_softc(struct ifnet *ifp) 793 { 794 struct bridge_iflist *bif; 795 796 NET_EPOCH_ASSERT(); 797 798 bif = ifp->if_bridge; 799 if (bif == NULL) 800 return (NULL); 801 return (bif->bif_sc); 802 } 803 804 /* 805 * bridge_same: 806 * 807 * Return true if two interfaces are in the same bridge. This is only used by 808 * bridgestp via bridge_same_p. 809 */ 810 static bool 811 bridge_same(const void *bifap, const void *bifbp) 812 { 813 const struct bridge_iflist *bifa = bifap, *bifb = bifbp; 814 815 NET_EPOCH_ASSERT(); 816 817 if (bifa == NULL || bifb == NULL) 818 return (false); 819 820 return (bifa->bif_sc == bifb->bif_sc); 821 } 822 823 /* 824 * bridge_clone_create: 825 * 826 * Create a new bridge instance. 827 */ 828 static int 829 bridge_clone_create(struct if_clone *ifc, char *name, size_t len, 830 struct ifc_data *ifd, struct ifnet **ifpp) 831 { 832 struct bridge_softc *sc; 833 struct ifnet *ifp; 834 835 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO); 836 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 837 838 BRIDGE_LOCK_INIT(sc); 839 sc->sc_brtmax = BRIDGE_RTABLE_MAX; 840 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; 841 842 /* Initialize our routing table. */ 843 bridge_rtable_init(sc); 844 845 callout_init_mtx(&sc->sc_brcallout, &sc->sc_rt_mtx, 0); 846 847 CK_LIST_INIT(&sc->sc_iflist); 848 CK_LIST_INIT(&sc->sc_spanlist); 849 850 ifp->if_softc = sc; 851 if_initname(ifp, bridge_name, ifd->unit); 852 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 853 ifp->if_capabilities = ifp->if_capenable = IFCAP_VLAN_HWTAGGING; 854 ifp->if_ioctl = bridge_ioctl; 855 #ifdef ALTQ 856 ifp->if_start = bridge_altq_start; 857 ifp->if_transmit = bridge_altq_transmit; 858 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 859 ifp->if_snd.ifq_drv_maxlen = 0; 860 IFQ_SET_READY(&ifp->if_snd); 861 #else 862 ifp->if_transmit = bridge_transmit; 863 #endif 864 ifp->if_qflush = bridge_qflush; 865 ifp->if_init = bridge_init; 866 ifp->if_type = IFT_BRIDGE; 867 868 ether_gen_addr(ifp, &sc->sc_defaddr); 869 870 bstp_attach(&sc->sc_stp, &bridge_ops); 871 ether_ifattach(ifp, sc->sc_defaddr.octet); 872 /* Now undo some of the damage... */ 873 ifp->if_baudrate = 0; 874 #ifdef VIMAGE 875 ifp->if_reassign = bridge_reassign; 876 #endif 877 sc->sc_if_input = ifp->if_input; /* ether_input */ 878 ifp->if_input = bridge_inject; 879 880 /* 881 * Allow BRIDGE_INPUT() to pass in packets originating from the bridge 882 * itself via bridge_inject(). This is required for netmap but 883 * otherwise has no effect. 884 */ 885 ifp->if_bridge_input = bridge_input; 886 887 BRIDGE_LIST_LOCK(); 888 LIST_INSERT_HEAD(&V_bridge_list, sc, sc_list); 889 BRIDGE_LIST_UNLOCK(); 890 *ifpp = ifp; 891 892 return (0); 893 } 894 895 static void 896 bridge_clone_destroy_cb(struct epoch_context *ctx) 897 { 898 struct bridge_softc *sc; 899 900 sc = __containerof(ctx, struct bridge_softc, sc_epoch_ctx); 901 902 BRIDGE_LOCK_DESTROY(sc); 903 free(sc, M_DEVBUF); 904 } 905 906 /* 907 * bridge_clone_destroy: 908 * 909 * Destroy a bridge instance. 910 */ 911 static int 912 bridge_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 913 { 914 struct bridge_softc *sc = ifp->if_softc; 915 struct bridge_iflist *bif; 916 struct epoch_tracker et; 917 918 BRIDGE_LOCK(sc); 919 920 bridge_stop(ifp, 1); 921 ifp->if_flags &= ~IFF_UP; 922 923 while ((bif = CK_LIST_FIRST(&sc->sc_iflist)) != NULL) 924 bridge_delete_member(sc, bif, 0); 925 926 while ((bif = CK_LIST_FIRST(&sc->sc_spanlist)) != NULL) { 927 bridge_delete_span(sc, bif); 928 } 929 930 /* Tear down the routing table. */ 931 bridge_rtable_fini(sc); 932 933 BRIDGE_UNLOCK(sc); 934 935 NET_EPOCH_ENTER(et); 936 937 callout_drain(&sc->sc_brcallout); 938 939 BRIDGE_LIST_LOCK(); 940 LIST_REMOVE(sc, sc_list); 941 BRIDGE_LIST_UNLOCK(); 942 943 bstp_detach(&sc->sc_stp); 944 #ifdef ALTQ 945 IFQ_PURGE(&ifp->if_snd); 946 #endif 947 NET_EPOCH_EXIT(et); 948 949 ether_ifdetach(ifp); 950 if_free(ifp); 951 952 NET_EPOCH_CALL(bridge_clone_destroy_cb, &sc->sc_epoch_ctx); 953 954 return (0); 955 } 956 957 /* 958 * bridge_ioctl: 959 * 960 * Handle a control request from the operator. 961 */ 962 static int 963 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 964 { 965 struct bridge_softc *sc = ifp->if_softc; 966 struct ifreq *ifr = (struct ifreq *)data; 967 struct bridge_iflist *bif; 968 struct thread *td = curthread; 969 union { 970 struct ifbreq ifbreq; 971 struct ifbifconf ifbifconf; 972 struct ifbareq ifbareq; 973 struct ifbaconf ifbaconf; 974 struct ifbrparam ifbrparam; 975 struct ifbropreq ifbropreq; 976 struct ifbif_vlan_req ifvlanreq; 977 } args; 978 struct ifdrv *ifd = (struct ifdrv *) data; 979 const struct bridge_control *bc; 980 int error = 0, oldmtu; 981 982 BRIDGE_LOCK(sc); 983 984 switch (cmd) { 985 case SIOCADDMULTI: 986 case SIOCDELMULTI: 987 break; 988 989 case SIOCGDRVSPEC: 990 case SIOCSDRVSPEC: 991 if (ifd->ifd_cmd >= bridge_control_table_size) { 992 error = EXTERROR(EINVAL, "Invalid control command"); 993 break; 994 } 995 bc = &bridge_control_table[ifd->ifd_cmd]; 996 997 if (cmd == SIOCGDRVSPEC && 998 (bc->bc_flags & BC_F_COPYOUT) == 0) { 999 error = EXTERROR(EINVAL, 1000 "Inappropriate ioctl for command " 1001 "(expected SIOCSDRVSPEC)"); 1002 break; 1003 } 1004 else if (cmd == SIOCSDRVSPEC && 1005 (bc->bc_flags & BC_F_COPYOUT) != 0) { 1006 error = EXTERROR(EINVAL, 1007 "Inappropriate ioctl for command " 1008 "(expected SIOCGDRVSPEC)"); 1009 break; 1010 } 1011 1012 if (bc->bc_flags & BC_F_SUSER) { 1013 error = priv_check(td, PRIV_NET_BRIDGE); 1014 if (error) { 1015 EXTERROR(error, "PRIV_NET_BRIDGE required"); 1016 break; 1017 } 1018 } 1019 1020 if (ifd->ifd_len != bc->bc_argsize || 1021 ifd->ifd_len > sizeof(args)) { 1022 error = EXTERROR(EINVAL, "Invalid argument size"); 1023 break; 1024 } 1025 1026 bzero(&args, sizeof(args)); 1027 if (bc->bc_flags & BC_F_COPYIN) { 1028 error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 1029 if (error) 1030 break; 1031 } 1032 1033 oldmtu = ifp->if_mtu; 1034 error = (*bc->bc_func)(sc, &args); 1035 if (error) 1036 break; 1037 1038 /* 1039 * Bridge MTU may change during addition of the first port. 1040 * If it did, do network layer specific procedure. 1041 */ 1042 if (ifp->if_mtu != oldmtu) 1043 if_notifymtu(ifp); 1044 1045 if (bc->bc_flags & BC_F_COPYOUT) 1046 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 1047 1048 break; 1049 1050 case SIOCSIFFLAGS: 1051 if (!(ifp->if_flags & IFF_UP) && 1052 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1053 /* 1054 * If interface is marked down and it is running, 1055 * then stop and disable it. 1056 */ 1057 bridge_stop(ifp, 1); 1058 } else if ((ifp->if_flags & IFF_UP) && 1059 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1060 /* 1061 * If interface is marked up and it is stopped, then 1062 * start it. 1063 */ 1064 BRIDGE_UNLOCK(sc); 1065 (*ifp->if_init)(sc); 1066 BRIDGE_LOCK(sc); 1067 } 1068 break; 1069 1070 case SIOCSIFMTU: 1071 oldmtu = sc->sc_ifp->if_mtu; 1072 1073 if (ifr->ifr_mtu < IF_MINMTU) { 1074 error = EXTERROR(EINVAL, 1075 "Requested MTU is lower than IF_MINMTU"); 1076 break; 1077 } 1078 if (CK_LIST_EMPTY(&sc->sc_iflist)) { 1079 sc->sc_ifp->if_mtu = ifr->ifr_mtu; 1080 break; 1081 } 1082 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1083 error = (*bif->bif_ifp->if_ioctl)(bif->bif_ifp, 1084 SIOCSIFMTU, (caddr_t)ifr); 1085 if (error != 0) { 1086 log(LOG_NOTICE, "%s: invalid MTU: %u for" 1087 " member %s\n", sc->sc_ifp->if_xname, 1088 ifr->ifr_mtu, 1089 bif->bif_ifp->if_xname); 1090 error = EINVAL; 1091 break; 1092 } 1093 } 1094 if (error) { 1095 /* Restore the previous MTU on all member interfaces. */ 1096 ifr->ifr_mtu = oldmtu; 1097 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1098 (*bif->bif_ifp->if_ioctl)(bif->bif_ifp, 1099 SIOCSIFMTU, (caddr_t)ifr); 1100 } 1101 EXTERROR(error, 1102 "Failed to set MTU on member interface"); 1103 } else { 1104 sc->sc_ifp->if_mtu = ifr->ifr_mtu; 1105 } 1106 break; 1107 default: 1108 /* 1109 * drop the lock as ether_ioctl() will call bridge_start() and 1110 * cause the lock to be recursed. 1111 */ 1112 BRIDGE_UNLOCK(sc); 1113 error = ether_ioctl(ifp, cmd, data); 1114 BRIDGE_LOCK(sc); 1115 break; 1116 } 1117 1118 BRIDGE_UNLOCK(sc); 1119 1120 return (error); 1121 } 1122 1123 /* 1124 * bridge_mutecaps: 1125 * 1126 * Clear or restore unwanted capabilities on the member interface 1127 */ 1128 static void 1129 bridge_mutecaps(struct bridge_softc *sc) 1130 { 1131 struct bridge_iflist *bif; 1132 int enabled, mask; 1133 1134 BRIDGE_LOCK_ASSERT(sc); 1135 1136 /* Initial bitmask of capabilities to test */ 1137 mask = BRIDGE_IFCAPS_MASK; 1138 1139 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1140 /* Every member must support it or it's disabled */ 1141 mask &= bif->bif_savedcaps; 1142 } 1143 1144 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1145 enabled = bif->bif_ifp->if_capenable; 1146 enabled &= ~BRIDGE_IFCAPS_STRIP; 1147 /* Strip off mask bits and enable them again if allowed */ 1148 enabled &= ~BRIDGE_IFCAPS_MASK; 1149 enabled |= mask; 1150 bridge_set_ifcap(sc, bif, enabled); 1151 } 1152 } 1153 1154 static void 1155 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set) 1156 { 1157 struct ifnet *ifp = bif->bif_ifp; 1158 struct ifreq ifr; 1159 int error, mask, stuck; 1160 1161 bzero(&ifr, sizeof(ifr)); 1162 ifr.ifr_reqcap = set; 1163 1164 if (ifp->if_capenable != set) { 1165 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); 1166 if (error) 1167 if_printf(sc->sc_ifp, 1168 "error setting capabilities on %s: %d\n", 1169 ifp->if_xname, error); 1170 mask = BRIDGE_IFCAPS_MASK | BRIDGE_IFCAPS_STRIP; 1171 stuck = ifp->if_capenable & mask & ~set; 1172 if (stuck != 0) 1173 if_printf(sc->sc_ifp, 1174 "can't disable some capabilities on %s: 0x%x\n", 1175 ifp->if_xname, stuck); 1176 } 1177 } 1178 1179 /* 1180 * bridge_lookup_member: 1181 * 1182 * Lookup a bridge member interface. 1183 */ 1184 static struct bridge_iflist * 1185 bridge_lookup_member(struct bridge_softc *sc, const char *name) 1186 { 1187 struct bridge_iflist *bif; 1188 struct ifnet *ifp; 1189 1190 BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc); 1191 1192 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1193 ifp = bif->bif_ifp; 1194 if (strcmp(ifp->if_xname, name) == 0) 1195 return (bif); 1196 } 1197 1198 return (NULL); 1199 } 1200 1201 /* 1202 * bridge_lookup_member_if: 1203 * 1204 * Lookup a bridge member interface by ifnet*. 1205 */ 1206 static struct bridge_iflist * 1207 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp) 1208 { 1209 BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc); 1210 return (member_ifp->if_bridge); 1211 } 1212 1213 static void 1214 bridge_delete_member_cb(struct epoch_context *ctx) 1215 { 1216 struct bridge_iflist *bif; 1217 1218 bif = __containerof(ctx, struct bridge_iflist, bif_epoch_ctx); 1219 1220 free(bif, M_DEVBUF); 1221 } 1222 1223 /* 1224 * bridge_delete_member: 1225 * 1226 * Delete the specified member interface. 1227 */ 1228 static void 1229 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif, 1230 int gone) 1231 { 1232 struct ifnet *ifs = bif->bif_ifp; 1233 struct ifnet *fif = NULL; 1234 struct bridge_iflist *bifl; 1235 1236 BRIDGE_LOCK_ASSERT(sc); 1237 1238 if (bif->bif_flags & IFBIF_STP) 1239 bstp_disable(&bif->bif_stp); 1240 1241 ifs->if_bridge = NULL; 1242 CK_LIST_REMOVE(bif, bif_next); 1243 1244 /* 1245 * If removing the interface that gave the bridge its mac address, set 1246 * the mac address of the bridge to the address of the next member, or 1247 * to its default address if no members are left. 1248 */ 1249 if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) { 1250 if (CK_LIST_EMPTY(&sc->sc_iflist)) { 1251 bcopy(&sc->sc_defaddr, 1252 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1253 sc->sc_ifaddr = NULL; 1254 } else { 1255 bifl = CK_LIST_FIRST(&sc->sc_iflist); 1256 fif = bifl->bif_ifp; 1257 bcopy(IF_LLADDR(fif), 1258 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1259 sc->sc_ifaddr = fif; 1260 } 1261 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp); 1262 } 1263 1264 bridge_linkcheck(sc); 1265 bridge_mutecaps(sc); /* recalcuate now this interface is removed */ 1266 BRIDGE_RT_LOCK(sc); 1267 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL); 1268 BRIDGE_RT_UNLOCK(sc); 1269 KASSERT(bif->bif_addrcnt == 0, 1270 ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt)); 1271 1272 ifs->if_bridge_output = NULL; 1273 ifs->if_bridge_input = NULL; 1274 ifs->if_bridge_linkstate = NULL; 1275 if (!gone) { 1276 switch (ifs->if_type) { 1277 case IFT_ETHER: 1278 case IFT_L2VLAN: 1279 /* 1280 * Take the interface out of promiscuous mode, but only 1281 * if it was promiscuous in the first place. It might 1282 * not be if we're in the bridge_ioctl_add() error path. 1283 */ 1284 if (ifs->if_flags & IFF_PROMISC) 1285 (void) ifpromisc(ifs, 0); 1286 break; 1287 1288 case IFT_GIF: 1289 break; 1290 1291 default: 1292 #ifdef DIAGNOSTIC 1293 panic("bridge_delete_member: impossible"); 1294 #endif 1295 break; 1296 } 1297 /* Re-enable any interface capabilities */ 1298 bridge_set_ifcap(sc, bif, bif->bif_savedcaps); 1299 } 1300 bstp_destroy(&bif->bif_stp); /* prepare to free */ 1301 1302 NET_EPOCH_CALL(bridge_delete_member_cb, &bif->bif_epoch_ctx); 1303 } 1304 1305 /* 1306 * bridge_delete_span: 1307 * 1308 * Delete the specified span interface. 1309 */ 1310 static void 1311 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif) 1312 { 1313 BRIDGE_LOCK_ASSERT(sc); 1314 1315 KASSERT(bif->bif_ifp->if_bridge == NULL, 1316 ("%s: not a span interface", __func__)); 1317 1318 CK_LIST_REMOVE(bif, bif_next); 1319 1320 NET_EPOCH_CALL(bridge_delete_member_cb, &bif->bif_epoch_ctx); 1321 } 1322 1323 static int 1324 bridge_ioctl_add(struct bridge_softc *sc, void *arg) 1325 { 1326 struct ifbreq *req = arg; 1327 struct bridge_iflist *bif = NULL; 1328 struct ifnet *ifs; 1329 int error = 0; 1330 1331 ifs = ifunit(req->ifbr_ifsname); 1332 if (ifs == NULL) 1333 return (EXTERROR(ENOENT, "No such interface", 1334 req->ifbr_ifsname)); 1335 if (ifs->if_ioctl == NULL) /* must be supported */ 1336 return (EXTERROR(EINVAL, "Interface must support ioctl(2)")); 1337 1338 /* If it's in the span list, it can't be a member. */ 1339 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1340 if (ifs == bif->bif_ifp) 1341 return (EXTERROR(EBUSY, 1342 "Span interface cannot be a member")); 1343 1344 if (ifs->if_bridge) { 1345 struct bridge_iflist *sbif = ifs->if_bridge; 1346 if (sbif->bif_sc == sc) 1347 return (EXTERROR(EEXIST, 1348 "Interface is already a member of this bridge")); 1349 1350 return (EXTERROR(EBUSY, 1351 "Interface is already a member of another bridge")); 1352 } 1353 1354 switch (ifs->if_type) { 1355 case IFT_ETHER: 1356 case IFT_L2VLAN: 1357 case IFT_GIF: 1358 /* permitted interface types */ 1359 break; 1360 default: 1361 return (EXTERROR(EINVAL, "Unsupported interface type")); 1362 } 1363 1364 #ifdef INET6 1365 /* 1366 * Two valid inet6 addresses with link-local scope must not be 1367 * on the parent interface and the member interfaces at the 1368 * same time. This restriction is needed to prevent violation 1369 * of link-local scope zone. Attempts to add a member 1370 * interface which has inet6 addresses when the parent has 1371 * inet6 triggers removal of all inet6 addresses on the member 1372 * interface. 1373 */ 1374 1375 /* Check if the parent interface has a link-local scope addr. */ 1376 if (V_allow_llz_overlap == 0 && 1377 in6ifa_llaonifp(sc->sc_ifp) != NULL) { 1378 /* 1379 * If any, remove all inet6 addresses from the member 1380 * interfaces. 1381 */ 1382 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1383 if (in6ifa_llaonifp(bif->bif_ifp)) { 1384 in6_ifdetach(bif->bif_ifp); 1385 if_printf(sc->sc_ifp, 1386 "IPv6 addresses on %s have been removed " 1387 "before adding it as a member to prevent " 1388 "IPv6 address scope violation.\n", 1389 bif->bif_ifp->if_xname); 1390 } 1391 } 1392 if (in6ifa_llaonifp(ifs)) { 1393 in6_ifdetach(ifs); 1394 if_printf(sc->sc_ifp, 1395 "IPv6 addresses on %s have been removed " 1396 "before adding it as a member to prevent " 1397 "IPv6 address scope violation.\n", 1398 ifs->if_xname); 1399 } 1400 } 1401 #endif 1402 1403 /* 1404 * If member_ifaddrs is disabled, do not allow an interface with 1405 * assigned IP addresses to be added to a bridge. 1406 */ 1407 if (!V_member_ifaddrs) { 1408 struct ifaddr *ifa; 1409 1410 CK_STAILQ_FOREACH(ifa, &ifs->if_addrhead, ifa_link) { 1411 #ifdef INET 1412 if (ifa->ifa_addr->sa_family == AF_INET) 1413 return (EXTERROR(EINVAL, 1414 "Member interface may not have " 1415 "an IPv4 address configured")); 1416 #endif 1417 #ifdef INET6 1418 if (ifa->ifa_addr->sa_family == AF_INET6) 1419 return (EXTERROR(EINVAL, 1420 "Member interface may not have " 1421 "an IPv6 address configured")); 1422 #endif 1423 } 1424 } 1425 1426 /* Allow the first Ethernet member to define the MTU */ 1427 if (CK_LIST_EMPTY(&sc->sc_iflist)) 1428 sc->sc_ifp->if_mtu = ifs->if_mtu; 1429 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) { 1430 struct ifreq ifr; 1431 1432 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", 1433 ifs->if_xname); 1434 ifr.ifr_mtu = sc->sc_ifp->if_mtu; 1435 1436 error = (*ifs->if_ioctl)(ifs, 1437 SIOCSIFMTU, (caddr_t)&ifr); 1438 if (error != 0) { 1439 log(LOG_NOTICE, "%s: invalid MTU: %u for" 1440 " new member %s\n", sc->sc_ifp->if_xname, 1441 ifr.ifr_mtu, 1442 ifs->if_xname); 1443 return (EXTERROR(EINVAL, 1444 "Failed to set MTU on new member")); 1445 } 1446 } 1447 1448 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO); 1449 if (bif == NULL) 1450 return (ENOMEM); 1451 1452 bif->bif_sc = sc; 1453 bif->bif_ifp = ifs; 1454 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER; 1455 bif->bif_savedcaps = ifs->if_capenable; 1456 1457 /* 1458 * Assign the interface's MAC address to the bridge if it's the first 1459 * member and the MAC address of the bridge has not been changed from 1460 * the default randomly generated one. 1461 */ 1462 if (V_bridge_inherit_mac && CK_LIST_EMPTY(&sc->sc_iflist) && 1463 !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr.octet, ETHER_ADDR_LEN)) { 1464 bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); 1465 sc->sc_ifaddr = ifs; 1466 EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp); 1467 } 1468 1469 ifs->if_bridge = bif; 1470 ifs->if_bridge_output = bridge_output; 1471 ifs->if_bridge_input = bridge_input; 1472 ifs->if_bridge_linkstate = bridge_linkstate; 1473 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp); 1474 /* 1475 * XXX: XLOCK HERE!?! 1476 * 1477 * NOTE: insert_***HEAD*** should be safe for the traversals. 1478 */ 1479 CK_LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next); 1480 1481 /* Set interface capabilities to the intersection set of all members */ 1482 bridge_mutecaps(sc); 1483 bridge_linkcheck(sc); 1484 1485 /* Place the interface into promiscuous mode */ 1486 switch (ifs->if_type) { 1487 case IFT_ETHER: 1488 case IFT_L2VLAN: 1489 error = ifpromisc(ifs, 1); 1490 break; 1491 } 1492 1493 if (error) 1494 bridge_delete_member(sc, bif, 0); 1495 return (error); 1496 } 1497 1498 static int 1499 bridge_ioctl_del(struct bridge_softc *sc, void *arg) 1500 { 1501 struct ifbreq *req = arg; 1502 struct bridge_iflist *bif; 1503 1504 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1505 if (bif == NULL) 1506 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1507 1508 bridge_delete_member(sc, bif, 0); 1509 1510 return (0); 1511 } 1512 1513 static int 1514 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) 1515 { 1516 struct ifbreq *req = arg; 1517 struct bridge_iflist *bif; 1518 struct bstp_port *bp; 1519 1520 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1521 if (bif == NULL) 1522 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1523 1524 bp = &bif->bif_stp; 1525 req->ifbr_ifsflags = bif->bif_flags; 1526 req->ifbr_state = bp->bp_state; 1527 req->ifbr_priority = bp->bp_priority; 1528 req->ifbr_path_cost = bp->bp_path_cost; 1529 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff; 1530 req->ifbr_proto = bp->bp_protover; 1531 req->ifbr_role = bp->bp_role; 1532 req->ifbr_stpflags = bp->bp_flags; 1533 req->ifbr_addrcnt = bif->bif_addrcnt; 1534 req->ifbr_addrmax = bif->bif_addrmax; 1535 req->ifbr_addrexceeded = bif->bif_addrexceeded; 1536 req->ifbr_untagged = bif->bif_untagged; 1537 1538 /* Copy STP state options as flags */ 1539 if (bp->bp_operedge) 1540 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE; 1541 if (bp->bp_flags & BSTP_PORT_AUTOEDGE) 1542 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE; 1543 if (bp->bp_ptp_link) 1544 req->ifbr_ifsflags |= IFBIF_BSTP_PTP; 1545 if (bp->bp_flags & BSTP_PORT_AUTOPTP) 1546 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP; 1547 if (bp->bp_flags & BSTP_PORT_ADMEDGE) 1548 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE; 1549 if (bp->bp_flags & BSTP_PORT_ADMCOST) 1550 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST; 1551 return (0); 1552 } 1553 1554 static int 1555 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) 1556 { 1557 struct epoch_tracker et; 1558 struct ifbreq *req = arg; 1559 struct bridge_iflist *bif; 1560 struct bstp_port *bp; 1561 int error; 1562 1563 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1564 if (bif == NULL) 1565 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1566 bp = &bif->bif_stp; 1567 1568 if (req->ifbr_ifsflags & IFBIF_SPAN) 1569 /* SPAN is readonly */ 1570 return (EXTERROR(EINVAL, "Span interface cannot be modified")); 1571 1572 NET_EPOCH_ENTER(et); 1573 1574 if (req->ifbr_ifsflags & IFBIF_STP) { 1575 if ((bif->bif_flags & IFBIF_STP) == 0) { 1576 error = bstp_enable(&bif->bif_stp); 1577 if (error) { 1578 NET_EPOCH_EXIT(et); 1579 return (EXTERROR(error, 1580 "Failed to enable STP")); 1581 } 1582 } 1583 } else { 1584 if ((bif->bif_flags & IFBIF_STP) != 0) 1585 bstp_disable(&bif->bif_stp); 1586 } 1587 1588 /* Pass on STP flags */ 1589 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0); 1590 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0); 1591 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0); 1592 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0); 1593 1594 /* Save the bits relating to the bridge */ 1595 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK; 1596 1597 NET_EPOCH_EXIT(et); 1598 1599 return (0); 1600 } 1601 1602 static int 1603 bridge_ioctl_scache(struct bridge_softc *sc, void *arg) 1604 { 1605 struct ifbrparam *param = arg; 1606 1607 sc->sc_brtmax = param->ifbrp_csize; 1608 bridge_rttrim(sc); 1609 1610 return (0); 1611 } 1612 1613 static int 1614 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) 1615 { 1616 struct ifbrparam *param = arg; 1617 1618 param->ifbrp_csize = sc->sc_brtmax; 1619 1620 return (0); 1621 } 1622 1623 static int 1624 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg) 1625 { 1626 struct ifbifconf *bifc = arg; 1627 struct bridge_iflist *bif; 1628 struct ifbreq breq; 1629 char *buf, *outbuf; 1630 int count, buflen, len, error = 0; 1631 1632 count = 0; 1633 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) 1634 count++; 1635 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1636 count++; 1637 1638 buflen = sizeof(breq) * count; 1639 if (bifc->ifbic_len == 0) { 1640 bifc->ifbic_len = buflen; 1641 return (0); 1642 } 1643 outbuf = malloc(buflen, M_TEMP, M_NOWAIT | M_ZERO); 1644 if (outbuf == NULL) 1645 return (ENOMEM); 1646 1647 count = 0; 1648 buf = outbuf; 1649 len = min(bifc->ifbic_len, buflen); 1650 bzero(&breq, sizeof(breq)); 1651 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 1652 if (len < sizeof(breq)) 1653 break; 1654 1655 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, 1656 sizeof(breq.ifbr_ifsname)); 1657 /* Fill in the ifbreq structure */ 1658 error = bridge_ioctl_gifflags(sc, &breq); 1659 if (error) 1660 break; 1661 memcpy(buf, &breq, sizeof(breq)); 1662 count++; 1663 buf += sizeof(breq); 1664 len -= sizeof(breq); 1665 } 1666 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 1667 if (len < sizeof(breq)) 1668 break; 1669 1670 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, 1671 sizeof(breq.ifbr_ifsname)); 1672 breq.ifbr_ifsflags = bif->bif_flags; 1673 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff; 1674 memcpy(buf, &breq, sizeof(breq)); 1675 count++; 1676 buf += sizeof(breq); 1677 len -= sizeof(breq); 1678 } 1679 1680 bifc->ifbic_len = sizeof(breq) * count; 1681 error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len); 1682 free(outbuf, M_TEMP); 1683 return (error); 1684 } 1685 1686 static int 1687 bridge_ioctl_rts(struct bridge_softc *sc, void *arg) 1688 { 1689 struct ifbaconf *bac = arg; 1690 struct bridge_rtnode *brt; 1691 struct ifbareq bareq; 1692 char *buf, *outbuf; 1693 int count, buflen, len, error = 0; 1694 1695 if (bac->ifbac_len == 0) 1696 return (0); 1697 1698 count = 0; 1699 CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) 1700 count++; 1701 buflen = sizeof(bareq) * count; 1702 1703 outbuf = malloc(buflen, M_TEMP, M_NOWAIT | M_ZERO); 1704 if (outbuf == NULL) 1705 return (ENOMEM); 1706 1707 count = 0; 1708 buf = outbuf; 1709 len = min(bac->ifbac_len, buflen); 1710 bzero(&bareq, sizeof(bareq)); 1711 CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { 1712 if (len < sizeof(bareq)) 1713 goto out; 1714 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname, 1715 sizeof(bareq.ifba_ifsname)); 1716 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); 1717 bareq.ifba_vlan = brt->brt_vlan; 1718 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 1719 time_uptime < brt->brt_expire) 1720 bareq.ifba_expire = brt->brt_expire - time_uptime; 1721 else 1722 bareq.ifba_expire = 0; 1723 bareq.ifba_flags = brt->brt_flags; 1724 1725 memcpy(buf, &bareq, sizeof(bareq)); 1726 count++; 1727 buf += sizeof(bareq); 1728 len -= sizeof(bareq); 1729 } 1730 out: 1731 bac->ifbac_len = sizeof(bareq) * count; 1732 error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len); 1733 free(outbuf, M_TEMP); 1734 return (error); 1735 } 1736 1737 static int 1738 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg) 1739 { 1740 struct ifbareq *req = arg; 1741 struct bridge_iflist *bif; 1742 struct epoch_tracker et; 1743 int error; 1744 1745 NET_EPOCH_ENTER(et); 1746 bif = bridge_lookup_member(sc, req->ifba_ifsname); 1747 if (bif == NULL) { 1748 NET_EPOCH_EXIT(et); 1749 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1750 } 1751 1752 /* bridge_rtupdate() may acquire the lock. */ 1753 error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1, 1754 req->ifba_flags); 1755 NET_EPOCH_EXIT(et); 1756 1757 return (error); 1758 } 1759 1760 static int 1761 bridge_ioctl_sto(struct bridge_softc *sc, void *arg) 1762 { 1763 struct ifbrparam *param = arg; 1764 1765 sc->sc_brttimeout = param->ifbrp_ctime; 1766 return (0); 1767 } 1768 1769 static int 1770 bridge_ioctl_gto(struct bridge_softc *sc, void *arg) 1771 { 1772 struct ifbrparam *param = arg; 1773 1774 param->ifbrp_ctime = sc->sc_brttimeout; 1775 return (0); 1776 } 1777 1778 static int 1779 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg) 1780 { 1781 struct ifbareq *req = arg; 1782 int vlan = req->ifba_vlan; 1783 1784 /* Userspace uses '0' to mean 'any vlan' */ 1785 if (vlan == 0) 1786 vlan = DOT1Q_VID_RSVD_IMPL; 1787 1788 return (bridge_rtdaddr(sc, req->ifba_dst, vlan)); 1789 } 1790 1791 static int 1792 bridge_ioctl_flush(struct bridge_softc *sc, void *arg) 1793 { 1794 struct ifbreq *req = arg; 1795 1796 BRIDGE_RT_LOCK(sc); 1797 bridge_rtflush(sc, req->ifbr_ifsflags); 1798 BRIDGE_RT_UNLOCK(sc); 1799 1800 return (0); 1801 } 1802 1803 static int 1804 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) 1805 { 1806 struct ifbrparam *param = arg; 1807 struct bstp_state *bs = &sc->sc_stp; 1808 1809 param->ifbrp_prio = bs->bs_bridge_priority; 1810 return (0); 1811 } 1812 1813 static int 1814 bridge_ioctl_spri(struct bridge_softc *sc, void *arg) 1815 { 1816 struct ifbrparam *param = arg; 1817 1818 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio)); 1819 } 1820 1821 static int 1822 bridge_ioctl_ght(struct bridge_softc *sc, void *arg) 1823 { 1824 struct ifbrparam *param = arg; 1825 struct bstp_state *bs = &sc->sc_stp; 1826 1827 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8; 1828 return (0); 1829 } 1830 1831 static int 1832 bridge_ioctl_sht(struct bridge_softc *sc, void *arg) 1833 { 1834 struct ifbrparam *param = arg; 1835 1836 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime)); 1837 } 1838 1839 static int 1840 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) 1841 { 1842 struct ifbrparam *param = arg; 1843 struct bstp_state *bs = &sc->sc_stp; 1844 1845 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8; 1846 return (0); 1847 } 1848 1849 static int 1850 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) 1851 { 1852 struct ifbrparam *param = arg; 1853 1854 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay)); 1855 } 1856 1857 static int 1858 bridge_ioctl_gma(struct bridge_softc *sc, void *arg) 1859 { 1860 struct ifbrparam *param = arg; 1861 struct bstp_state *bs = &sc->sc_stp; 1862 1863 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8; 1864 return (0); 1865 } 1866 1867 static int 1868 bridge_ioctl_sma(struct bridge_softc *sc, void *arg) 1869 { 1870 struct ifbrparam *param = arg; 1871 1872 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage)); 1873 } 1874 1875 static int 1876 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) 1877 { 1878 struct ifbreq *req = arg; 1879 struct bridge_iflist *bif; 1880 1881 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1882 if (bif == NULL) 1883 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1884 1885 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority)); 1886 } 1887 1888 static int 1889 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) 1890 { 1891 struct ifbreq *req = arg; 1892 struct bridge_iflist *bif; 1893 1894 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1895 if (bif == NULL) 1896 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1897 1898 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost)); 1899 } 1900 1901 static int 1902 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg) 1903 { 1904 struct ifbreq *req = arg; 1905 struct bridge_iflist *bif; 1906 1907 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1908 if (bif == NULL) 1909 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1910 1911 bif->bif_addrmax = req->ifbr_addrmax; 1912 return (0); 1913 } 1914 1915 static int 1916 bridge_ioctl_sifuntagged(struct bridge_softc *sc, void *arg) 1917 { 1918 struct ifbreq *req = arg; 1919 struct bridge_iflist *bif; 1920 1921 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1922 if (bif == NULL) 1923 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1924 1925 if (req->ifbr_untagged > DOT1Q_VID_MAX) 1926 return (EXTERROR(EINVAL, "Invalid VLAN ID")); 1927 1928 if (req->ifbr_untagged != DOT1Q_VID_NULL) 1929 bif->bif_flags |= IFBIF_VLANFILTER; 1930 bif->bif_untagged = req->ifbr_untagged; 1931 return (0); 1932 } 1933 1934 static int 1935 bridge_ioctl_sifvlanset(struct bridge_softc *sc, void *arg) 1936 { 1937 struct ifbif_vlan_req *req = arg; 1938 struct bridge_iflist *bif; 1939 1940 bif = bridge_lookup_member(sc, req->bv_ifname); 1941 if (bif == NULL) 1942 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1943 1944 /* Reject invalid VIDs. */ 1945 if (BRVLAN_TEST(&req->bv_set, DOT1Q_VID_NULL) || 1946 BRVLAN_TEST(&req->bv_set, DOT1Q_VID_RSVD_IMPL)) 1947 return (EXTERROR(EINVAL, "Invalid VLAN ID in set")); 1948 1949 switch (req->bv_op) { 1950 /* Replace the existing vlan set with the new set */ 1951 case BRDG_VLAN_OP_SET: 1952 BIT_COPY(BRVLAN_SETSIZE, &req->bv_set, &bif->bif_vlan_set); 1953 break; 1954 1955 /* Modify the existing vlan set to add the given vlans */ 1956 case BRDG_VLAN_OP_ADD: 1957 BIT_OR(BRVLAN_SETSIZE, &bif->bif_vlan_set, &req->bv_set); 1958 break; 1959 1960 /* Modify the existing vlan set to remove the given vlans */ 1961 case BRDG_VLAN_OP_DEL: 1962 BIT_ANDNOT(BRVLAN_SETSIZE, &bif->bif_vlan_set, &req->bv_set); 1963 break; 1964 1965 /* Invalid or unknown operation */ 1966 default: 1967 return (EXTERROR(EINVAL, 1968 "Unsupported BRDGSIFVLANSET operation")); 1969 } 1970 1971 /* 1972 * The only reason to modify the VLAN access list is to use VLAN 1973 * filtering on this interface, so enable it automatically. 1974 */ 1975 bif->bif_flags |= IFBIF_VLANFILTER; 1976 1977 return (0); 1978 } 1979 1980 static int 1981 bridge_ioctl_gifvlanset(struct bridge_softc *sc, void *arg) 1982 { 1983 struct ifbif_vlan_req *req = arg; 1984 struct bridge_iflist *bif; 1985 1986 bif = bridge_lookup_member(sc, req->bv_ifname); 1987 if (bif == NULL) 1988 return (EXTERROR(ENOENT, "Interface is not a bridge member")); 1989 1990 BIT_COPY(BRVLAN_SETSIZE, &bif->bif_vlan_set, &req->bv_set); 1991 return (0); 1992 } 1993 1994 static int 1995 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg) 1996 { 1997 struct ifbreq *req = arg; 1998 struct bridge_iflist *bif = NULL; 1999 struct ifnet *ifs; 2000 2001 ifs = ifunit(req->ifbr_ifsname); 2002 if (ifs == NULL) 2003 return (EXTERROR(ENOENT, "No such interface")); 2004 2005 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 2006 if (ifs == bif->bif_ifp) 2007 return (EXTERROR(EBUSY, 2008 "Interface is already a span port")); 2009 2010 if (ifs->if_bridge != NULL) 2011 return (EXTERROR(EEXIST, 2012 "Interface is already a bridge member")); 2013 2014 switch (ifs->if_type) { 2015 case IFT_ETHER: 2016 case IFT_GIF: 2017 case IFT_L2VLAN: 2018 break; 2019 default: 2020 return (EXTERROR(EINVAL, "Unsupported interface type")); 2021 } 2022 2023 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO); 2024 if (bif == NULL) 2025 return (ENOMEM); 2026 2027 bif->bif_ifp = ifs; 2028 bif->bif_flags = IFBIF_SPAN; 2029 2030 CK_LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next); 2031 2032 return (0); 2033 } 2034 2035 static int 2036 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg) 2037 { 2038 struct ifbreq *req = arg; 2039 struct bridge_iflist *bif; 2040 struct ifnet *ifs; 2041 2042 ifs = ifunit(req->ifbr_ifsname); 2043 if (ifs == NULL) 2044 return (EXTERROR(ENOENT, "No such interface")); 2045 2046 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 2047 if (ifs == bif->bif_ifp) 2048 break; 2049 2050 if (bif == NULL) 2051 return (EXTERROR(ENOENT, "Interface is not a span port")); 2052 2053 bridge_delete_span(sc, bif); 2054 2055 return (0); 2056 } 2057 2058 static int 2059 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg) 2060 { 2061 struct ifbropreq *req = arg; 2062 struct bstp_state *bs = &sc->sc_stp; 2063 struct bstp_port *root_port; 2064 2065 req->ifbop_maxage = bs->bs_bridge_max_age >> 8; 2066 req->ifbop_hellotime = bs->bs_bridge_htime >> 8; 2067 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; 2068 2069 root_port = bs->bs_root_port; 2070 if (root_port == NULL) 2071 req->ifbop_root_port = 0; 2072 else 2073 req->ifbop_root_port = root_port->bp_ifp->if_index; 2074 2075 req->ifbop_holdcount = bs->bs_txholdcount; 2076 req->ifbop_priority = bs->bs_bridge_priority; 2077 req->ifbop_protocol = bs->bs_protover; 2078 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; 2079 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id; 2080 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; 2081 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id; 2082 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; 2083 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; 2084 2085 return (0); 2086 } 2087 2088 static int 2089 bridge_ioctl_grte(struct bridge_softc *sc, void *arg) 2090 { 2091 struct ifbrparam *param = arg; 2092 2093 param->ifbrp_cexceeded = sc->sc_brtexceeded; 2094 return (0); 2095 } 2096 2097 static int 2098 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg) 2099 { 2100 struct ifbpstpconf *bifstp = arg; 2101 struct bridge_iflist *bif; 2102 struct bstp_port *bp; 2103 struct ifbpstpreq bpreq; 2104 char *buf, *outbuf; 2105 int count, buflen, len, error = 0; 2106 2107 count = 0; 2108 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 2109 if ((bif->bif_flags & IFBIF_STP) != 0) 2110 count++; 2111 } 2112 2113 buflen = sizeof(bpreq) * count; 2114 if (bifstp->ifbpstp_len == 0) { 2115 bifstp->ifbpstp_len = buflen; 2116 return (0); 2117 } 2118 2119 outbuf = malloc(buflen, M_TEMP, M_NOWAIT | M_ZERO); 2120 if (outbuf == NULL) 2121 return (ENOMEM); 2122 2123 count = 0; 2124 buf = outbuf; 2125 len = min(bifstp->ifbpstp_len, buflen); 2126 bzero(&bpreq, sizeof(bpreq)); 2127 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 2128 if (len < sizeof(bpreq)) 2129 break; 2130 2131 if ((bif->bif_flags & IFBIF_STP) == 0) 2132 continue; 2133 2134 bp = &bif->bif_stp; 2135 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; 2136 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions; 2137 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost; 2138 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id; 2139 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; 2140 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id; 2141 2142 memcpy(buf, &bpreq, sizeof(bpreq)); 2143 count++; 2144 buf += sizeof(bpreq); 2145 len -= sizeof(bpreq); 2146 } 2147 2148 bifstp->ifbpstp_len = sizeof(bpreq) * count; 2149 error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); 2150 free(outbuf, M_TEMP); 2151 return (error); 2152 } 2153 2154 static int 2155 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg) 2156 { 2157 struct ifbrparam *param = arg; 2158 2159 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto)); 2160 } 2161 2162 static int 2163 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg) 2164 { 2165 struct ifbrparam *param = arg; 2166 2167 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc)); 2168 } 2169 2170 /* 2171 * bridge_ifdetach: 2172 * 2173 * Detach an interface from a bridge. Called when a member 2174 * interface is detaching. 2175 */ 2176 static void 2177 bridge_ifdetach(void *arg __unused, struct ifnet *ifp) 2178 { 2179 struct bridge_iflist *bif = ifp->if_bridge; 2180 struct bridge_softc *sc = NULL; 2181 2182 if (bif) 2183 sc = bif->bif_sc; 2184 2185 if (ifp->if_flags & IFF_RENAMING) 2186 return; 2187 if (V_bridge_cloner == NULL) { 2188 /* 2189 * This detach handler can be called after 2190 * vnet_bridge_uninit(). Just return in that case. 2191 */ 2192 return; 2193 } 2194 /* Check if the interface is a bridge member */ 2195 if (sc != NULL) { 2196 BRIDGE_LOCK(sc); 2197 bridge_delete_member(sc, bif, 1); 2198 BRIDGE_UNLOCK(sc); 2199 return; 2200 } 2201 2202 /* Check if the interface is a span port */ 2203 BRIDGE_LIST_LOCK(); 2204 LIST_FOREACH(sc, &V_bridge_list, sc_list) { 2205 BRIDGE_LOCK(sc); 2206 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 2207 if (ifp == bif->bif_ifp) { 2208 bridge_delete_span(sc, bif); 2209 break; 2210 } 2211 2212 BRIDGE_UNLOCK(sc); 2213 } 2214 BRIDGE_LIST_UNLOCK(); 2215 } 2216 2217 /* 2218 * bridge_init: 2219 * 2220 * Initialize a bridge interface. 2221 */ 2222 static void 2223 bridge_init(void *xsc) 2224 { 2225 struct bridge_softc *sc = (struct bridge_softc *)xsc; 2226 struct ifnet *ifp = sc->sc_ifp; 2227 2228 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2229 return; 2230 2231 BRIDGE_LOCK(sc); 2232 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz, 2233 bridge_timer, sc); 2234 2235 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2236 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */ 2237 2238 BRIDGE_UNLOCK(sc); 2239 } 2240 2241 /* 2242 * bridge_stop: 2243 * 2244 * Stop the bridge interface. 2245 */ 2246 static void 2247 bridge_stop(struct ifnet *ifp, int disable) 2248 { 2249 struct bridge_softc *sc = ifp->if_softc; 2250 2251 BRIDGE_LOCK_ASSERT(sc); 2252 2253 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2254 return; 2255 2256 BRIDGE_RT_LOCK(sc); 2257 callout_stop(&sc->sc_brcallout); 2258 2259 bstp_stop(&sc->sc_stp); 2260 2261 bridge_rtflush(sc, IFBF_FLUSHDYN); 2262 BRIDGE_RT_UNLOCK(sc); 2263 2264 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2265 } 2266 2267 /* 2268 * bridge_enqueue: 2269 * 2270 * Enqueue a packet on a bridge member interface. 2271 * 2272 */ 2273 static int 2274 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m, 2275 struct bridge_iflist *bif) 2276 { 2277 int len, err = 0; 2278 short mflags; 2279 struct mbuf *m0; 2280 2281 /* 2282 * Find the bridge member port this packet is being sent on, if the 2283 * caller didn't already provide it. 2284 */ 2285 if (bif == NULL) 2286 bif = bridge_lookup_member_if(sc, dst_ifp); 2287 if (bif == NULL) { 2288 /* Perhaps the interface was removed from the bridge */ 2289 m_freem(m); 2290 return (EINVAL); 2291 } 2292 2293 /* We may be sending a fragment so traverse the mbuf */ 2294 for (; m; m = m0) { 2295 m0 = m->m_nextpkt; 2296 m->m_nextpkt = NULL; 2297 len = m->m_pkthdr.len; 2298 mflags = m->m_flags; 2299 2300 /* 2301 * If VLAN filtering is enabled, and the native VLAN ID of the 2302 * outgoing interface matches the VLAN ID of the frame, remove 2303 * the VLAN header. 2304 */ 2305 if ((bif->bif_flags & IFBIF_VLANFILTER) && 2306 bif->bif_untagged != DOT1Q_VID_NULL && 2307 VLANTAGOF(m) == bif->bif_untagged) { 2308 m->m_flags &= ~M_VLANTAG; 2309 m->m_pkthdr.ether_vtag = 0; 2310 } 2311 2312 /* 2313 * If underlying interface can not do VLAN tag insertion itself 2314 * then attach a packet tag that holds it. 2315 */ 2316 if ((m->m_flags & M_VLANTAG) && 2317 (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) { 2318 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); 2319 if (m == NULL) { 2320 if_printf(dst_ifp, 2321 "unable to prepend VLAN header\n"); 2322 if_inc_counter(dst_ifp, IFCOUNTER_OERRORS, 1); 2323 continue; 2324 } 2325 m->m_flags &= ~M_VLANTAG; 2326 } 2327 2328 M_ASSERTPKTHDR(m); /* We shouldn't transmit mbuf without pkthdr */ 2329 if ((err = dst_ifp->if_transmit(dst_ifp, m))) { 2330 int n; 2331 2332 for (m = m0, n = 1; m != NULL; m = m0, n++) { 2333 m0 = m->m_nextpkt; 2334 m_freem(m); 2335 } 2336 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, n); 2337 break; 2338 } 2339 2340 if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1); 2341 if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, len); 2342 if (mflags & M_MCAST) 2343 if_inc_counter(sc->sc_ifp, IFCOUNTER_OMCASTS, 1); 2344 } 2345 2346 return (err); 2347 } 2348 2349 /* 2350 * bridge_dummynet: 2351 * 2352 * Receive a queued packet from dummynet and pass it on to the output 2353 * interface. 2354 * 2355 * The mbuf has the Ethernet header already attached. 2356 */ 2357 static void 2358 bridge_dummynet(struct mbuf *m, struct ifnet *ifp) 2359 { 2360 struct bridge_iflist *bif = ifp->if_bridge; 2361 struct bridge_softc *sc = NULL; 2362 2363 if (bif) 2364 sc = bif->bif_sc; 2365 2366 /* 2367 * The packet didnt originate from a member interface. This should only 2368 * ever happen if a member interface is removed while packets are 2369 * queued for it. 2370 */ 2371 if (sc == NULL) { 2372 m_freem(m); 2373 return; 2374 } 2375 2376 if (PFIL_HOOKED_OUT_46) { 2377 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0) 2378 return; 2379 if (m == NULL) 2380 return; 2381 } 2382 2383 bridge_enqueue(sc, ifp, m, NULL); 2384 } 2385 2386 /* 2387 * bridge_output: 2388 * 2389 * Send output from a bridge member interface. This 2390 * performs the bridging function for locally originated 2391 * packets. 2392 * 2393 * The mbuf has the Ethernet header already attached. We must 2394 * enqueue or free the mbuf before returning. 2395 */ 2396 static int 2397 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa, 2398 struct rtentry *rt) 2399 { 2400 struct ether_header *eh; 2401 struct bridge_iflist *sbif; 2402 struct ifnet *bifp, *dst_if; 2403 struct bridge_softc *sc; 2404 ether_vlanid_t vlan; 2405 2406 NET_EPOCH_ASSERT(); 2407 2408 if (m->m_len < ETHER_HDR_LEN) { 2409 m = m_pullup(m, ETHER_HDR_LEN); 2410 if (m == NULL) 2411 return (0); 2412 } 2413 2414 sbif = ifp->if_bridge; 2415 sc = sbif->bif_sc; 2416 bifp = sc->sc_ifp; 2417 2418 eh = mtod(m, struct ether_header *); 2419 vlan = VLANTAGOF(m); 2420 2421 /* 2422 * If bridge is down, but the original output interface is up, 2423 * go ahead and send out that interface. Otherwise, the packet 2424 * is dropped below. 2425 */ 2426 if ((bifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2427 dst_if = ifp; 2428 goto sendunicast; 2429 } 2430 2431 /* 2432 * If the packet is a multicast, or we don't know a better way to 2433 * get there, send to all interfaces. 2434 */ 2435 if (ETHER_IS_MULTICAST(eh->ether_dhost)) 2436 dst_if = NULL; 2437 else 2438 dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan); 2439 /* Tap any traffic not passing back out the originating interface */ 2440 if (dst_if != ifp) 2441 ETHER_BPF_MTAP(bifp, m); 2442 if (dst_if == NULL) { 2443 struct bridge_iflist *bif; 2444 struct mbuf *mc; 2445 int used = 0; 2446 2447 bridge_span(sc, m); 2448 2449 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 2450 dst_if = bif->bif_ifp; 2451 2452 if (dst_if->if_type == IFT_GIF) 2453 continue; 2454 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2455 continue; 2456 2457 /* 2458 * If this is not the original output interface, 2459 * and the interface is participating in spanning 2460 * tree, make sure the port is in a state that 2461 * allows forwarding. 2462 */ 2463 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) && 2464 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2465 continue; 2466 2467 if (CK_LIST_NEXT(bif, bif_next) == NULL) { 2468 used = 1; 2469 mc = m; 2470 } else { 2471 mc = m_dup(m, M_NOWAIT); 2472 if (mc == NULL) { 2473 if_inc_counter(bifp, IFCOUNTER_OERRORS, 1); 2474 continue; 2475 } 2476 } 2477 2478 bridge_enqueue(sc, dst_if, mc, bif); 2479 } 2480 if (used == 0) 2481 m_freem(m); 2482 return (0); 2483 } 2484 2485 sendunicast: 2486 /* 2487 * XXX Spanning tree consideration here? 2488 */ 2489 2490 bridge_span(sc, m); 2491 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2492 m_freem(m); 2493 return (0); 2494 } 2495 2496 bridge_enqueue(sc, dst_if, m, NULL); 2497 return (0); 2498 } 2499 2500 /* 2501 * bridge_transmit: 2502 * 2503 * Do output on a bridge. 2504 * 2505 */ 2506 static int 2507 bridge_transmit(struct ifnet *ifp, struct mbuf *m) 2508 { 2509 struct bridge_softc *sc; 2510 struct ether_header *eh; 2511 struct ifnet *dst_if; 2512 int error = 0; 2513 ether_vlanid_t vlan; 2514 2515 sc = ifp->if_softc; 2516 2517 ETHER_BPF_MTAP(ifp, m); 2518 2519 eh = mtod(m, struct ether_header *); 2520 vlan = VLANTAGOF(m); 2521 2522 if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) && 2523 (dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan)) != NULL) { 2524 error = bridge_enqueue(sc, dst_if, m, NULL); 2525 } else 2526 bridge_broadcast(sc, ifp, m, 0); 2527 2528 return (error); 2529 } 2530 2531 #ifdef ALTQ 2532 static void 2533 bridge_altq_start(if_t ifp) 2534 { 2535 struct ifaltq *ifq = &ifp->if_snd; 2536 struct mbuf *m; 2537 2538 IFQ_LOCK(ifq); 2539 IFQ_DEQUEUE_NOLOCK(ifq, m); 2540 while (m != NULL) { 2541 bridge_transmit(ifp, m); 2542 IFQ_DEQUEUE_NOLOCK(ifq, m); 2543 } 2544 IFQ_UNLOCK(ifq); 2545 } 2546 2547 static int 2548 bridge_altq_transmit(if_t ifp, struct mbuf *m) 2549 { 2550 int err; 2551 2552 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 2553 IFQ_ENQUEUE(&ifp->if_snd, m, err); 2554 if (err == 0) 2555 bridge_altq_start(ifp); 2556 } else 2557 err = bridge_transmit(ifp, m); 2558 2559 return (err); 2560 } 2561 #endif /* ALTQ */ 2562 2563 /* 2564 * The ifp->if_qflush entry point for if_bridge(4) is no-op. 2565 */ 2566 static void 2567 bridge_qflush(struct ifnet *ifp __unused) 2568 { 2569 } 2570 2571 /* 2572 * bridge_forward: 2573 * 2574 * The forwarding function of the bridge. 2575 * 2576 * NOTE: Releases the lock on return. 2577 */ 2578 static void 2579 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif, 2580 struct mbuf *m) 2581 { 2582 struct bridge_iflist *dbif; 2583 struct ifnet *src_if, *dst_if, *ifp; 2584 struct ether_header *eh; 2585 uint8_t *dst; 2586 int error; 2587 ether_vlanid_t vlan; 2588 2589 NET_EPOCH_ASSERT(); 2590 2591 src_if = m->m_pkthdr.rcvif; 2592 ifp = sc->sc_ifp; 2593 vlan = VLANTAGOF(m); 2594 2595 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 2596 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 2597 2598 if ((sbif->bif_flags & IFBIF_STP) && 2599 sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2600 goto drop; 2601 2602 eh = mtod(m, struct ether_header *); 2603 dst = eh->ether_dhost; 2604 2605 /* If the interface is learning, record the address. */ 2606 if (sbif->bif_flags & IFBIF_LEARNING) { 2607 error = bridge_rtupdate(sc, eh->ether_shost, vlan, 2608 sbif, 0, IFBAF_DYNAMIC); 2609 /* 2610 * If the interface has addresses limits then deny any source 2611 * that is not in the cache. 2612 */ 2613 if (error && sbif->bif_addrmax) 2614 goto drop; 2615 } 2616 2617 if ((sbif->bif_flags & IFBIF_STP) != 0 && 2618 sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) 2619 goto drop; 2620 2621 #ifdef DEV_NETMAP 2622 /* 2623 * Hand the packet to netmap only if it wasn't injected by netmap 2624 * itself. 2625 */ 2626 if ((m->m_flags & M_BRIDGE_INJECT) == 0 && 2627 (if_getcapenable(ifp) & IFCAP_NETMAP) != 0) { 2628 ifp->if_input(ifp, m); 2629 return; 2630 } 2631 m->m_flags &= ~M_BRIDGE_INJECT; 2632 #endif 2633 2634 /* 2635 * At this point, the port either doesn't participate 2636 * in spanning tree or it is in the forwarding state. 2637 */ 2638 2639 /* 2640 * If the packet is unicast, destined for someone on 2641 * "this" side of the bridge, drop it. 2642 */ 2643 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) { 2644 dst_if = bridge_rtlookup(sc, dst, vlan); 2645 if (src_if == dst_if) 2646 goto drop; 2647 } else { 2648 /* 2649 * Check if its a reserved multicast address, any address 2650 * listed in 802.1D section 7.12.6 may not be forwarded by the 2651 * bridge. 2652 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F 2653 */ 2654 if (dst[0] == 0x01 && dst[1] == 0x80 && 2655 dst[2] == 0xc2 && dst[3] == 0x00 && 2656 dst[4] == 0x00 && dst[5] <= 0x0f) 2657 goto drop; 2658 2659 /* ...forward it to all interfaces. */ 2660 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 2661 dst_if = NULL; 2662 } 2663 2664 /* 2665 * If we have a destination interface which is a member of our bridge, 2666 * OR this is a unicast packet, push it through the bpf(4) machinery. 2667 * For broadcast or multicast packets, don't bother because it will 2668 * be reinjected into ether_input. We do this before we pass the packets 2669 * through the pfil(9) framework, as it is possible that pfil(9) will 2670 * drop the packet, or possibly modify it, making it difficult to debug 2671 * firewall issues on the bridge. 2672 */ 2673 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0) 2674 ETHER_BPF_MTAP(ifp, m); 2675 2676 /* run the packet filter */ 2677 if (PFIL_HOOKED_IN_46) { 2678 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0) 2679 return; 2680 if (m == NULL) 2681 return; 2682 } 2683 2684 if (dst_if == NULL) { 2685 bridge_broadcast(sc, src_if, m, 1); 2686 return; 2687 } 2688 2689 /* 2690 * At this point, we're dealing with a unicast frame 2691 * going to a different interface. 2692 */ 2693 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 2694 goto drop; 2695 2696 dbif = bridge_lookup_member_if(sc, dst_if); 2697 if (dbif == NULL) 2698 /* Not a member of the bridge (anymore?) */ 2699 goto drop; 2700 2701 /* Private segments can not talk to each other */ 2702 if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE) 2703 goto drop; 2704 2705 /* Do VLAN filtering. */ 2706 if (!bridge_vfilter_out(dbif, m)) 2707 goto drop; 2708 2709 if ((dbif->bif_flags & IFBIF_STP) && 2710 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 2711 goto drop; 2712 2713 if (PFIL_HOOKED_OUT_46) { 2714 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0) 2715 return; 2716 if (m == NULL) 2717 return; 2718 } 2719 2720 bridge_enqueue(sc, dst_if, m, dbif); 2721 return; 2722 2723 drop: 2724 m_freem(m); 2725 } 2726 2727 /* 2728 * bridge_input: 2729 * 2730 * Receive input from a member interface. Queue the packet for 2731 * bridging if it is not for us. 2732 */ 2733 static struct mbuf * 2734 bridge_input(struct ifnet *ifp, struct mbuf *m) 2735 { 2736 struct bridge_softc *sc = NULL; 2737 struct bridge_iflist *bif, *bif2; 2738 struct ifnet *bifp; 2739 struct ether_header *eh; 2740 struct mbuf *mc, *mc2; 2741 ether_vlanid_t vlan; 2742 int error; 2743 2744 NET_EPOCH_ASSERT(); 2745 2746 eh = mtod(m, struct ether_header *); 2747 vlan = VLANTAGOF(m); 2748 2749 bif = ifp->if_bridge; 2750 if (bif) 2751 sc = bif->bif_sc; 2752 2753 if (sc == NULL) { 2754 /* 2755 * This packet originated from the bridge itself, so it must 2756 * have been transmitted by netmap. Derive the "source" 2757 * interface from the source address and drop the packet if the 2758 * source address isn't known. 2759 */ 2760 KASSERT((m->m_flags & M_BRIDGE_INJECT) != 0, 2761 ("%s: ifnet %p missing a bridge softc", __func__, ifp)); 2762 sc = if_getsoftc(ifp); 2763 ifp = bridge_rtlookup(sc, eh->ether_shost, vlan); 2764 if (ifp == NULL) { 2765 if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1); 2766 m_freem(m); 2767 return (NULL); 2768 } 2769 m->m_pkthdr.rcvif = ifp; 2770 } 2771 bifp = sc->sc_ifp; 2772 if ((bifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2773 return (m); 2774 2775 /* 2776 * Implement support for bridge monitoring. If this flag has been 2777 * set on this interface, discard the packet once we push it through 2778 * the bpf(4) machinery, but before we do, increment the byte and 2779 * packet counters associated with this interface. 2780 */ 2781 if ((bifp->if_flags & IFF_MONITOR) != 0) { 2782 m->m_pkthdr.rcvif = bifp; 2783 ETHER_BPF_MTAP(bifp, m); 2784 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1); 2785 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 2786 m_freem(m); 2787 return (NULL); 2788 } 2789 2790 /* Do VLAN filtering. */ 2791 if (!bridge_vfilter_in(bif, m)) { 2792 if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1); 2793 m_freem(m); 2794 return (NULL); 2795 } 2796 /* bridge_vfilter_in() may add a tag */ 2797 vlan = VLANTAGOF(m); 2798 2799 bridge_span(sc, m); 2800 2801 if (m->m_flags & (M_BCAST|M_MCAST)) { 2802 /* Tap off 802.1D packets; they do not get forwarded. */ 2803 if (memcmp(eh->ether_dhost, bstp_etheraddr, 2804 ETHER_ADDR_LEN) == 0) { 2805 bstp_input(&bif->bif_stp, ifp, m); /* consumes mbuf */ 2806 return (NULL); 2807 } 2808 2809 if ((bif->bif_flags & IFBIF_STP) && 2810 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { 2811 return (m); 2812 } 2813 2814 /* 2815 * Make a deep copy of the packet and enqueue the copy 2816 * for bridge processing; return the original packet for 2817 * local processing. 2818 */ 2819 mc = m_dup(m, M_NOWAIT); 2820 if (mc == NULL) { 2821 return (m); 2822 } 2823 2824 /* Perform the bridge forwarding function with the copy. */ 2825 bridge_forward(sc, bif, mc); 2826 2827 #ifdef DEV_NETMAP 2828 /* 2829 * If netmap is enabled and has not already seen this packet, 2830 * then it will be consumed by bridge_forward(). 2831 */ 2832 if ((if_getcapenable(bifp) & IFCAP_NETMAP) != 0 && 2833 (m->m_flags & M_BRIDGE_INJECT) == 0) { 2834 m_freem(m); 2835 return (NULL); 2836 } 2837 #endif 2838 2839 /* 2840 * Reinject the mbuf as arriving on the bridge so we have a 2841 * chance at claiming multicast packets. We can not loop back 2842 * here from ether_input as a bridge is never a member of a 2843 * bridge. 2844 */ 2845 KASSERT(bifp->if_bridge == NULL, 2846 ("loop created in bridge_input")); 2847 mc2 = m_dup(m, M_NOWAIT); 2848 if (mc2 != NULL) { 2849 /* Keep the layer3 header aligned */ 2850 int i = min(mc2->m_pkthdr.len, max_protohdr); 2851 mc2 = m_copyup(mc2, i, ETHER_ALIGN); 2852 } 2853 if (mc2 != NULL) { 2854 mc2->m_pkthdr.rcvif = bifp; 2855 mc2->m_flags &= ~M_BRIDGE_INJECT; 2856 sc->sc_if_input(bifp, mc2); 2857 } 2858 2859 /* Return the original packet for local processing. */ 2860 return (m); 2861 } 2862 2863 if ((bif->bif_flags & IFBIF_STP) && 2864 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { 2865 return (m); 2866 } 2867 2868 #if defined(INET) || defined(INET6) 2869 #define CARP_CHECK_WE_ARE_DST(iface) \ 2870 ((iface)->if_carp && (*carp_forus_p)((iface), eh->ether_dhost)) 2871 #define CARP_CHECK_WE_ARE_SRC(iface) \ 2872 ((iface)->if_carp && (*carp_forus_p)((iface), eh->ether_shost)) 2873 #else 2874 #define CARP_CHECK_WE_ARE_DST(iface) false 2875 #define CARP_CHECK_WE_ARE_SRC(iface) false 2876 #endif 2877 2878 #ifdef DEV_NETMAP 2879 #define GRAB_FOR_NETMAP(ifp, m) do { \ 2880 if ((if_getcapenable(ifp) & IFCAP_NETMAP) != 0 && \ 2881 ((m)->m_flags & M_BRIDGE_INJECT) == 0) { \ 2882 (ifp)->if_input(ifp, m); \ 2883 return (NULL); \ 2884 } \ 2885 } while (0) 2886 #else 2887 #define GRAB_FOR_NETMAP(ifp, m) 2888 #endif 2889 2890 #define GRAB_OUR_PACKETS(iface) \ 2891 if ((iface)->if_type == IFT_GIF) \ 2892 continue; \ 2893 /* It is destined for us. */ \ 2894 if (memcmp(IF_LLADDR(iface), eh->ether_dhost, ETHER_ADDR_LEN) == 0 || \ 2895 CARP_CHECK_WE_ARE_DST(iface)) { \ 2896 if (bif->bif_flags & IFBIF_LEARNING) { \ 2897 error = bridge_rtupdate(sc, eh->ether_shost, \ 2898 vlan, bif, 0, IFBAF_DYNAMIC); \ 2899 if (error && bif->bif_addrmax) { \ 2900 m_freem(m); \ 2901 return (NULL); \ 2902 } \ 2903 } \ 2904 m->m_pkthdr.rcvif = iface; \ 2905 if ((iface) == ifp) { \ 2906 /* Skip bridge processing... src == dest */ \ 2907 return (m); \ 2908 } \ 2909 /* It's passing over or to the bridge, locally. */ \ 2910 ETHER_BPF_MTAP(bifp, m); \ 2911 if_inc_counter(bifp, IFCOUNTER_IPACKETS, 1); \ 2912 if_inc_counter(bifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);\ 2913 /* Hand the packet over to netmap if necessary. */ \ 2914 GRAB_FOR_NETMAP(bifp, m); \ 2915 /* Filter on the physical interface. */ \ 2916 if (V_pfil_local_phys && PFIL_HOOKED_IN_46) { \ 2917 if (bridge_pfil(&m, NULL, ifp, \ 2918 PFIL_IN) != 0 || m == NULL) { \ 2919 return (NULL); \ 2920 } \ 2921 } \ 2922 if ((iface) != bifp) \ 2923 ETHER_BPF_MTAP(iface, m); \ 2924 /* Pass tagged packets to if_vlan, if it's loaded */ \ 2925 if (VLANTAGOF(m) != 0) { \ 2926 if (bifp->if_vlantrunk == NULL) { \ 2927 m_freem(m); \ 2928 return (NULL); \ 2929 } \ 2930 (*vlan_input_p)(bifp, m); \ 2931 return (NULL); \ 2932 } \ 2933 return (m); \ 2934 } \ 2935 \ 2936 /* We just received a packet that we sent out. */ \ 2937 if (memcmp(IF_LLADDR(iface), eh->ether_shost, ETHER_ADDR_LEN) == 0 || \ 2938 CARP_CHECK_WE_ARE_SRC(iface)) { \ 2939 m_freem(m); \ 2940 return (NULL); \ 2941 } 2942 2943 /* 2944 * Unicast. Make sure it's not for the bridge. 2945 */ 2946 do { GRAB_OUR_PACKETS(bifp) } while (0); 2947 2948 /* 2949 * Check the interface the packet arrived on. For tagged frames, 2950 * we need to do this even if member_ifaddrs is disabled because 2951 * vlan(4) might need to handle the traffic. 2952 */ 2953 if (V_member_ifaddrs || (vlan && ifp->if_vlantrunk)) 2954 do { GRAB_OUR_PACKETS(ifp) } while (0); 2955 2956 /* 2957 * We only need to check other members interface if member_ifaddrs 2958 * is enabled; otherwise we should have never traffic destined for 2959 * a member's lladdr. 2960 */ 2961 if (V_member_ifaddrs) { 2962 CK_LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) { 2963 GRAB_OUR_PACKETS(bif2->bif_ifp) 2964 } 2965 } 2966 2967 #undef CARP_CHECK_WE_ARE_DST 2968 #undef CARP_CHECK_WE_ARE_SRC 2969 #undef GRAB_FOR_NETMAP 2970 #undef GRAB_OUR_PACKETS 2971 2972 /* Perform the bridge forwarding function. */ 2973 bridge_forward(sc, bif, m); 2974 2975 return (NULL); 2976 } 2977 2978 /* 2979 * Inject a packet back into the host ethernet stack. This will generally only 2980 * be used by netmap when an application writes to the host TX ring. The 2981 * M_BRIDGE_INJECT flag ensures that the packet is re-routed to the bridge 2982 * interface after ethernet processing. 2983 */ 2984 static void 2985 bridge_inject(struct ifnet *ifp, struct mbuf *m) 2986 { 2987 struct bridge_softc *sc; 2988 2989 if (ifp->if_type == IFT_L2VLAN) { 2990 /* 2991 * vlan(4) gives us the vlan ifnet, so we need to get the 2992 * bridge softc to get a pointer to ether_input to send the 2993 * packet to. 2994 */ 2995 struct ifnet *bifp = NULL; 2996 2997 if (vlan_trunkdev_p == NULL) { 2998 m_freem(m); 2999 return; 3000 } 3001 3002 bifp = vlan_trunkdev_p(ifp); 3003 if (bifp == NULL) { 3004 m_freem(m); 3005 return; 3006 } 3007 3008 sc = if_getsoftc(bifp); 3009 sc->sc_if_input(ifp, m); 3010 return; 3011 } 3012 3013 KASSERT((if_getcapenable(ifp) & IFCAP_NETMAP) != 0, 3014 ("%s: iface %s is not running in netmap mode", 3015 __func__, if_name(ifp))); 3016 KASSERT((m->m_flags & M_BRIDGE_INJECT) == 0, 3017 ("%s: mbuf %p has M_BRIDGE_INJECT set", __func__, m)); 3018 3019 m->m_flags |= M_BRIDGE_INJECT; 3020 sc = if_getsoftc(ifp); 3021 sc->sc_if_input(ifp, m); 3022 } 3023 3024 /* 3025 * bridge_broadcast: 3026 * 3027 * Send a frame to all interfaces that are members of 3028 * the bridge, except for the one on which the packet 3029 * arrived. 3030 * 3031 * NOTE: Releases the lock on return. 3032 */ 3033 static void 3034 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if, 3035 struct mbuf *m, int runfilt) 3036 { 3037 struct bridge_iflist *dbif, *sbif; 3038 struct mbuf *mc; 3039 struct ifnet *dst_if; 3040 int used = 0, i; 3041 3042 NET_EPOCH_ASSERT(); 3043 3044 sbif = bridge_lookup_member_if(sc, src_if); 3045 3046 /* Filter on the bridge interface before broadcasting */ 3047 if (runfilt && PFIL_HOOKED_OUT_46) { 3048 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0) 3049 return; 3050 if (m == NULL) 3051 return; 3052 } 3053 3054 CK_LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) { 3055 dst_if = dbif->bif_ifp; 3056 if (dst_if == src_if) 3057 continue; 3058 3059 /* Private segments can not talk to each other */ 3060 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)) 3061 continue; 3062 3063 /* Do VLAN filtering. */ 3064 if (!bridge_vfilter_out(dbif, m)) 3065 continue; 3066 3067 if ((dbif->bif_flags & IFBIF_STP) && 3068 dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) 3069 continue; 3070 3071 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 && 3072 (m->m_flags & (M_BCAST|M_MCAST)) == 0) 3073 continue; 3074 3075 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 3076 continue; 3077 3078 if (CK_LIST_NEXT(dbif, bif_next) == NULL) { 3079 mc = m; 3080 used = 1; 3081 } else { 3082 mc = m_dup(m, M_NOWAIT); 3083 if (mc == NULL) { 3084 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 3085 continue; 3086 } 3087 } 3088 3089 /* 3090 * Filter on the output interface. Pass a NULL bridge interface 3091 * pointer so we do not redundantly filter on the bridge for 3092 * each interface we broadcast on. 3093 */ 3094 if (runfilt && PFIL_HOOKED_OUT_46) { 3095 if (used == 0) { 3096 /* Keep the layer3 header aligned */ 3097 i = min(mc->m_pkthdr.len, max_protohdr); 3098 mc = m_copyup(mc, i, ETHER_ALIGN); 3099 if (mc == NULL) { 3100 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 3101 continue; 3102 } 3103 } 3104 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0) 3105 continue; 3106 if (mc == NULL) 3107 continue; 3108 } 3109 3110 bridge_enqueue(sc, dst_if, mc, dbif); 3111 } 3112 if (used == 0) 3113 m_freem(m); 3114 } 3115 3116 /* 3117 * bridge_span: 3118 * 3119 * Duplicate a packet out one or more interfaces that are in span mode, 3120 * the original mbuf is unmodified. 3121 */ 3122 static void 3123 bridge_span(struct bridge_softc *sc, struct mbuf *m) 3124 { 3125 struct bridge_iflist *bif; 3126 struct ifnet *dst_if; 3127 struct mbuf *mc; 3128 3129 NET_EPOCH_ASSERT(); 3130 3131 if (CK_LIST_EMPTY(&sc->sc_spanlist)) 3132 return; 3133 3134 CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 3135 dst_if = bif->bif_ifp; 3136 3137 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) 3138 continue; 3139 3140 mc = m_dup(m, M_NOWAIT); 3141 if (mc == NULL) { 3142 if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1); 3143 continue; 3144 } 3145 3146 bridge_enqueue(sc, dst_if, mc, bif); 3147 } 3148 } 3149 3150 /* 3151 * Incoming VLAN filtering. Given a frame and the member interface it was 3152 * received on, decide whether the port configuration allows it. 3153 */ 3154 static bool 3155 bridge_vfilter_in(const struct bridge_iflist *sbif, struct mbuf *m) 3156 { 3157 ether_vlanid_t vlan; 3158 3159 vlan = VLANTAGOF(m); 3160 /* Make sure the vlan id is reasonable. */ 3161 if (vlan > DOT1Q_VID_MAX) 3162 return (false); 3163 3164 /* If VLAN filtering isn't enabled, pass everything. */ 3165 if ((sbif->bif_flags & IFBIF_VLANFILTER) == 0) 3166 return (true); 3167 3168 if (vlan == DOT1Q_VID_NULL) { 3169 /* 3170 * The frame doesn't have a tag. If the interface does not 3171 * have an untagged vlan configured, drop the frame. 3172 */ 3173 if (sbif->bif_untagged == DOT1Q_VID_NULL) 3174 return (false); 3175 3176 /* 3177 * Otherwise, insert a new tag based on the interface's 3178 * untagged vlan id. 3179 */ 3180 m->m_pkthdr.ether_vtag = sbif->bif_untagged; 3181 m->m_flags |= M_VLANTAG; 3182 } else { 3183 /* 3184 * The frame has a tag, so check it matches the interface's 3185 * vlan access list. We explicitly do not accept tagged 3186 * frames for the untagged vlan id here (unless it's also 3187 * in the access list). 3188 */ 3189 if (!BRVLAN_TEST(&sbif->bif_vlan_set, vlan)) 3190 return (false); 3191 } 3192 3193 /* Accept the frame. */ 3194 return (true); 3195 } 3196 3197 /* 3198 * Outgoing VLAN filtering. Given a frame, its vlan, and the member interface 3199 * we intend to send it to, decide whether the port configuration allows it to 3200 * be sent. 3201 */ 3202 static bool 3203 bridge_vfilter_out(const struct bridge_iflist *dbif, const struct mbuf *m) 3204 { 3205 struct ether_header *eh; 3206 ether_vlanid_t vlan; 3207 3208 NET_EPOCH_ASSERT(); 3209 3210 /* If VLAN filtering isn't enabled, pass everything. */ 3211 if ((dbif->bif_flags & IFBIF_VLANFILTER) == 0) 3212 return (true); 3213 3214 vlan = VLANTAGOF(m); 3215 3216 /* 3217 * Always allow untagged 802.1D STP frames, even if they would 3218 * otherwise be dropped. This is required for STP to work on 3219 * a filtering bridge. 3220 * 3221 * Tagged STP (Cisco PVST+) is a non-standard extension, so 3222 * handle those frames via the normal filtering path. 3223 */ 3224 eh = mtod(m, struct ether_header *); 3225 if (vlan == DOT1Q_VID_NULL && 3226 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) 3227 return (true); 3228 3229 /* 3230 * If the frame wasn't assigned to a vlan at ingress, drop it. 3231 * We can't forward these frames to filtering ports because we 3232 * don't know what VLAN they're supposed to be in. 3233 */ 3234 if (vlan == DOT1Q_VID_NULL) 3235 return (false); 3236 3237 /* 3238 * If the frame's vlan matches the interfaces's untagged vlan, 3239 * allow it. 3240 */ 3241 if (vlan == dbif->bif_untagged) 3242 return (true); 3243 3244 /* 3245 * If the frame's vlan is on the interface's tagged access list, 3246 * allow it. 3247 */ 3248 if (BRVLAN_TEST(&dbif->bif_vlan_set, vlan)) 3249 return (true); 3250 3251 /* The frame was not permitted, so drop it. */ 3252 return (false); 3253 } 3254 3255 /* 3256 * bridge_rtupdate: 3257 * 3258 * Add a bridge routing entry. 3259 */ 3260 static int 3261 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, 3262 ether_vlanid_t vlan, struct bridge_iflist *bif, 3263 int setflags, uint8_t flags) 3264 { 3265 struct bridge_rtnode *brt; 3266 struct bridge_iflist *obif; 3267 int error; 3268 3269 BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc); 3270 3271 /* Check the source address is valid and not multicast. */ 3272 if (ETHER_IS_MULTICAST(dst)) 3273 return (EXTERROR(EINVAL, "Multicast address not permitted")); 3274 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 && 3275 dst[3] == 0 && dst[4] == 0 && dst[5] == 0) 3276 return (EXTERROR(EINVAL, "Zero address not permitted")); 3277 3278 /* 3279 * A route for this destination might already exist. If so, 3280 * update it, otherwise create a new one. 3281 */ 3282 if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) { 3283 BRIDGE_RT_LOCK(sc); 3284 3285 /* Check again, now that we have the lock. There could have 3286 * been a race and we only want to insert this once. */ 3287 if (bridge_rtnode_lookup(sc, dst, vlan) != NULL) { 3288 BRIDGE_RT_UNLOCK(sc); 3289 return (0); 3290 } 3291 3292 if (sc->sc_brtcnt >= sc->sc_brtmax) { 3293 sc->sc_brtexceeded++; 3294 BRIDGE_RT_UNLOCK(sc); 3295 return (EXTERROR(ENOSPC, "Address table is full")); 3296 } 3297 /* Check per interface address limits (if enabled) */ 3298 if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) { 3299 bif->bif_addrexceeded++; 3300 BRIDGE_RT_UNLOCK(sc); 3301 return (EXTERROR(ENOSPC, 3302 "Interface address limit exceeded")); 3303 } 3304 3305 /* 3306 * Allocate a new bridge forwarding node, and 3307 * initialize the expiration time and Ethernet 3308 * address. 3309 */ 3310 brt = uma_zalloc(V_bridge_rtnode_zone, M_NOWAIT | M_ZERO); 3311 if (brt == NULL) { 3312 BRIDGE_RT_UNLOCK(sc); 3313 return (EXTERROR(ENOMEM, 3314 "Cannot allocate address node")); 3315 } 3316 brt->brt_vnet = curvnet; 3317 3318 if (bif->bif_flags & IFBIF_STICKY) 3319 brt->brt_flags = IFBAF_STICKY; 3320 else 3321 brt->brt_flags = IFBAF_DYNAMIC; 3322 3323 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); 3324 brt->brt_vlan = vlan; 3325 3326 brt->brt_dst = bif; 3327 if ((error = bridge_rtnode_insert(sc, brt)) != 0) { 3328 uma_zfree(V_bridge_rtnode_zone, brt); 3329 BRIDGE_RT_UNLOCK(sc); 3330 return (error); 3331 } 3332 bif->bif_addrcnt++; 3333 3334 BRIDGE_RT_UNLOCK(sc); 3335 } 3336 3337 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 3338 (obif = brt->brt_dst) != bif) { 3339 MPASS(obif != NULL); 3340 3341 BRIDGE_RT_LOCK(sc); 3342 brt->brt_dst->bif_addrcnt--; 3343 brt->brt_dst = bif; 3344 brt->brt_dst->bif_addrcnt++; 3345 BRIDGE_RT_UNLOCK(sc); 3346 3347 if (V_log_mac_flap && 3348 ppsratecheck(&V_log_last, &V_log_count, V_log_interval)) { 3349 log(LOG_NOTICE, 3350 "%s: mac address %6D vlan %d moved from %s to %s\n", 3351 sc->sc_ifp->if_xname, 3352 &brt->brt_addr[0], ":", 3353 brt->brt_vlan, 3354 obif->bif_ifp->if_xname, 3355 bif->bif_ifp->if_xname); 3356 } 3357 } 3358 3359 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 3360 brt->brt_expire = time_uptime + sc->sc_brttimeout; 3361 if (setflags) 3362 brt->brt_flags = flags; 3363 3364 return (0); 3365 } 3366 3367 /* 3368 * bridge_rtlookup: 3369 * 3370 * Lookup the destination interface for an address. 3371 */ 3372 static struct ifnet * 3373 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, 3374 ether_vlanid_t vlan) 3375 { 3376 struct bridge_rtnode *brt; 3377 3378 NET_EPOCH_ASSERT(); 3379 3380 if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL) 3381 return (NULL); 3382 3383 return (brt->brt_ifp); 3384 } 3385 3386 /* 3387 * bridge_rttrim: 3388 * 3389 * Trim the routine table so that we have a number 3390 * of routing entries less than or equal to the 3391 * maximum number. 3392 */ 3393 static void 3394 bridge_rttrim(struct bridge_softc *sc) 3395 { 3396 struct bridge_rtnode *brt, *nbrt; 3397 3398 NET_EPOCH_ASSERT(); 3399 BRIDGE_RT_LOCK_ASSERT(sc); 3400 3401 /* Make sure we actually need to do this. */ 3402 if (sc->sc_brtcnt <= sc->sc_brtmax) 3403 return; 3404 3405 /* Force an aging cycle; this might trim enough addresses. */ 3406 bridge_rtage(sc); 3407 if (sc->sc_brtcnt <= sc->sc_brtmax) 3408 return; 3409 3410 CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 3411 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 3412 bridge_rtnode_destroy(sc, brt); 3413 if (sc->sc_brtcnt <= sc->sc_brtmax) 3414 return; 3415 } 3416 } 3417 } 3418 3419 /* 3420 * bridge_timer: 3421 * 3422 * Aging timer for the bridge. 3423 */ 3424 static void 3425 bridge_timer(void *arg) 3426 { 3427 struct bridge_softc *sc = arg; 3428 3429 BRIDGE_RT_LOCK_ASSERT(sc); 3430 3431 /* Destruction of rtnodes requires a proper vnet context */ 3432 CURVNET_SET(sc->sc_ifp->if_vnet); 3433 bridge_rtage(sc); 3434 3435 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) 3436 callout_reset(&sc->sc_brcallout, 3437 bridge_rtable_prune_period * hz, bridge_timer, sc); 3438 CURVNET_RESTORE(); 3439 } 3440 3441 /* 3442 * bridge_rtage: 3443 * 3444 * Perform an aging cycle. 3445 */ 3446 static void 3447 bridge_rtage(struct bridge_softc *sc) 3448 { 3449 struct bridge_rtnode *brt, *nbrt; 3450 3451 BRIDGE_RT_LOCK_ASSERT(sc); 3452 3453 CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 3454 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 3455 if (time_uptime >= brt->brt_expire) 3456 bridge_rtnode_destroy(sc, brt); 3457 } 3458 } 3459 } 3460 3461 /* 3462 * bridge_rtflush: 3463 * 3464 * Remove all dynamic addresses from the bridge. 3465 */ 3466 static void 3467 bridge_rtflush(struct bridge_softc *sc, int full) 3468 { 3469 struct bridge_rtnode *brt, *nbrt; 3470 3471 BRIDGE_RT_LOCK_ASSERT(sc); 3472 3473 CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 3474 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 3475 bridge_rtnode_destroy(sc, brt); 3476 } 3477 } 3478 3479 /* 3480 * bridge_rtdaddr: 3481 * 3482 * Remove an address from the table. 3483 */ 3484 static int 3485 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, 3486 ether_vlanid_t vlan) 3487 { 3488 struct bridge_rtnode *brt; 3489 int found = 0; 3490 3491 BRIDGE_RT_LOCK(sc); 3492 3493 /* 3494 * If vlan is DOT1Q_VID_RSVD_IMPL then we want to delete for all vlans 3495 * so the lookup may return more than one. 3496 */ 3497 while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) { 3498 bridge_rtnode_destroy(sc, brt); 3499 found = 1; 3500 } 3501 3502 BRIDGE_RT_UNLOCK(sc); 3503 3504 return (found ? 0 : ENOENT); 3505 } 3506 3507 /* 3508 * bridge_rtdelete: 3509 * 3510 * Delete routes to a speicifc member interface. 3511 */ 3512 static void 3513 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full) 3514 { 3515 struct bridge_rtnode *brt, *nbrt; 3516 3517 BRIDGE_RT_LOCK_ASSERT(sc); 3518 3519 CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { 3520 if (brt->brt_ifp == ifp && (full || 3521 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) 3522 bridge_rtnode_destroy(sc, brt); 3523 } 3524 } 3525 3526 /* 3527 * bridge_rtable_init: 3528 * 3529 * Initialize the route table for this bridge. 3530 */ 3531 static void 3532 bridge_rtable_init(struct bridge_softc *sc) 3533 { 3534 int i; 3535 3536 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE, 3537 M_DEVBUF, M_WAITOK); 3538 3539 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++) 3540 CK_LIST_INIT(&sc->sc_rthash[i]); 3541 3542 sc->sc_rthash_key = arc4random(); 3543 CK_LIST_INIT(&sc->sc_rtlist); 3544 } 3545 3546 /* 3547 * bridge_rtable_fini: 3548 * 3549 * Deconstruct the route table for this bridge. 3550 */ 3551 static void 3552 bridge_rtable_fini(struct bridge_softc *sc) 3553 { 3554 3555 KASSERT(sc->sc_brtcnt == 0, 3556 ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt)); 3557 free(sc->sc_rthash, M_DEVBUF); 3558 } 3559 3560 /* 3561 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 3562 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 3563 */ 3564 #define mix(a, b, c) \ 3565 do { \ 3566 a -= b; a -= c; a ^= (c >> 13); \ 3567 b -= c; b -= a; b ^= (a << 8); \ 3568 c -= a; c -= b; c ^= (b >> 13); \ 3569 a -= b; a -= c; a ^= (c >> 12); \ 3570 b -= c; b -= a; b ^= (a << 16); \ 3571 c -= a; c -= b; c ^= (b >> 5); \ 3572 a -= b; a -= c; a ^= (c >> 3); \ 3573 b -= c; b -= a; b ^= (a << 10); \ 3574 c -= a; c -= b; c ^= (b >> 15); \ 3575 } while (/*CONSTCOND*/0) 3576 3577 static __inline uint32_t 3578 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) 3579 { 3580 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; 3581 3582 b += addr[5] << 8; 3583 b += addr[4]; 3584 a += addr[3] << 24; 3585 a += addr[2] << 16; 3586 a += addr[1] << 8; 3587 a += addr[0]; 3588 3589 mix(a, b, c); 3590 3591 return (c & BRIDGE_RTHASH_MASK); 3592 } 3593 3594 #undef mix 3595 3596 static int 3597 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b) 3598 { 3599 int i, d; 3600 3601 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { 3602 d = ((int)a[i]) - ((int)b[i]); 3603 } 3604 3605 return (d); 3606 } 3607 3608 /* 3609 * bridge_rtnode_lookup: 3610 * 3611 * Look up a bridge route node for the specified destination. Compare the 3612 * vlan id or if zero then just return the first match. 3613 */ 3614 static struct bridge_rtnode * 3615 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, 3616 ether_vlanid_t vlan) 3617 { 3618 struct bridge_rtnode *brt; 3619 uint32_t hash; 3620 int dir; 3621 3622 BRIDGE_RT_LOCK_OR_NET_EPOCH_ASSERT(sc); 3623 3624 hash = bridge_rthash(sc, addr); 3625 CK_LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) { 3626 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr); 3627 if (dir == 0 && (brt->brt_vlan == vlan || vlan == DOT1Q_VID_RSVD_IMPL)) 3628 return (brt); 3629 if (dir > 0) 3630 return (NULL); 3631 } 3632 3633 return (NULL); 3634 } 3635 3636 /* 3637 * bridge_rtnode_insert: 3638 * 3639 * Insert the specified bridge node into the route table. We 3640 * assume the entry is not already in the table. 3641 */ 3642 static int 3643 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) 3644 { 3645 struct bridge_rtnode *lbrt; 3646 uint32_t hash; 3647 int dir; 3648 3649 BRIDGE_RT_LOCK_ASSERT(sc); 3650 3651 hash = bridge_rthash(sc, brt->brt_addr); 3652 3653 lbrt = CK_LIST_FIRST(&sc->sc_rthash[hash]); 3654 if (lbrt == NULL) { 3655 CK_LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash); 3656 goto out; 3657 } 3658 3659 do { 3660 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr); 3661 if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan) 3662 return (EXTERROR(EEXIST, "Address already exists")); 3663 if (dir > 0) { 3664 CK_LIST_INSERT_BEFORE(lbrt, brt, brt_hash); 3665 goto out; 3666 } 3667 if (CK_LIST_NEXT(lbrt, brt_hash) == NULL) { 3668 CK_LIST_INSERT_AFTER(lbrt, brt, brt_hash); 3669 goto out; 3670 } 3671 lbrt = CK_LIST_NEXT(lbrt, brt_hash); 3672 } while (lbrt != NULL); 3673 3674 #ifdef DIAGNOSTIC 3675 panic("bridge_rtnode_insert: impossible"); 3676 #endif 3677 3678 out: 3679 CK_LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list); 3680 sc->sc_brtcnt++; 3681 3682 return (0); 3683 } 3684 3685 static void 3686 bridge_rtnode_destroy_cb(struct epoch_context *ctx) 3687 { 3688 struct bridge_rtnode *brt; 3689 3690 brt = __containerof(ctx, struct bridge_rtnode, brt_epoch_ctx); 3691 3692 CURVNET_SET(brt->brt_vnet); 3693 uma_zfree(V_bridge_rtnode_zone, brt); 3694 CURVNET_RESTORE(); 3695 } 3696 3697 /* 3698 * bridge_rtnode_destroy: 3699 * 3700 * Destroy a bridge rtnode. 3701 */ 3702 static void 3703 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt) 3704 { 3705 BRIDGE_RT_LOCK_ASSERT(sc); 3706 3707 CK_LIST_REMOVE(brt, brt_hash); 3708 3709 CK_LIST_REMOVE(brt, brt_list); 3710 sc->sc_brtcnt--; 3711 brt->brt_dst->bif_addrcnt--; 3712 3713 NET_EPOCH_CALL(bridge_rtnode_destroy_cb, &brt->brt_epoch_ctx); 3714 } 3715 3716 /* 3717 * bridge_rtable_expire: 3718 * 3719 * Set the expiry time for all routes on an interface. 3720 */ 3721 static void 3722 bridge_rtable_expire(struct ifnet *ifp, int age) 3723 { 3724 struct bridge_iflist *bif = NULL; 3725 struct bridge_softc *sc = NULL; 3726 struct bridge_rtnode *brt; 3727 3728 CURVNET_SET(ifp->if_vnet); 3729 3730 bif = ifp->if_bridge; 3731 if (bif) 3732 sc = bif->bif_sc; 3733 MPASS(sc != NULL); 3734 BRIDGE_RT_LOCK(sc); 3735 3736 /* 3737 * If the age is zero then flush, otherwise set all the expiry times to 3738 * age for the interface 3739 */ 3740 if (age == 0) 3741 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN); 3742 else { 3743 CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { 3744 /* Cap the expiry time to 'age' */ 3745 if (brt->brt_ifp == ifp && 3746 brt->brt_expire > time_uptime + age && 3747 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) 3748 brt->brt_expire = time_uptime + age; 3749 } 3750 } 3751 BRIDGE_RT_UNLOCK(sc); 3752 CURVNET_RESTORE(); 3753 } 3754 3755 /* 3756 * bridge_state_change: 3757 * 3758 * Callback from the bridgestp code when a port changes states. 3759 */ 3760 static void 3761 bridge_state_change(struct ifnet *ifp, int state) 3762 { 3763 struct bridge_iflist *bif = ifp->if_bridge; 3764 struct bridge_softc *sc = bif->bif_sc; 3765 static const char *stpstates[] = { 3766 "disabled", 3767 "listening", 3768 "learning", 3769 "forwarding", 3770 "blocking", 3771 "discarding" 3772 }; 3773 3774 CURVNET_SET(ifp->if_vnet); 3775 if (V_log_stp) 3776 log(LOG_NOTICE, "%s: state changed to %s on %s\n", 3777 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname); 3778 CURVNET_RESTORE(); 3779 } 3780 3781 /* 3782 * Send bridge packets through pfil if they are one of the types pfil can deal 3783 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without 3784 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for 3785 * that interface. 3786 */ 3787 static int 3788 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir) 3789 { 3790 int snap, error, i; 3791 struct ether_header *eh1, eh2; 3792 struct llc llc1; 3793 u_int16_t ether_type; 3794 pfil_return_t rv; 3795 #ifdef INET 3796 struct ip *ip = NULL; 3797 int hlen = 0; 3798 #endif 3799 3800 snap = 0; 3801 error = -1; /* Default error if not error == 0 */ 3802 3803 #if 0 3804 /* we may return with the IP fields swapped, ensure its not shared */ 3805 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__)); 3806 #endif 3807 3808 if (V_pfil_bridge == 0 && V_pfil_member == 0 && V_pfil_ipfw == 0) 3809 return (0); /* filtering is disabled */ 3810 3811 i = min((*mp)->m_pkthdr.len, max_protohdr); 3812 if ((*mp)->m_len < i) { 3813 *mp = m_pullup(*mp, i); 3814 if (*mp == NULL) { 3815 printf("%s: m_pullup failed\n", __func__); 3816 return (-1); 3817 } 3818 } 3819 3820 eh1 = mtod(*mp, struct ether_header *); 3821 ether_type = ntohs(eh1->ether_type); 3822 3823 /* 3824 * Check for SNAP/LLC. 3825 */ 3826 if (ether_type < ETHERMTU) { 3827 struct llc *llc2 = (struct llc *)(eh1 + 1); 3828 3829 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && 3830 llc2->llc_dsap == LLC_SNAP_LSAP && 3831 llc2->llc_ssap == LLC_SNAP_LSAP && 3832 llc2->llc_control == LLC_UI) { 3833 ether_type = htons(llc2->llc_un.type_snap.ether_type); 3834 snap = 1; 3835 } 3836 } 3837 3838 /* 3839 * If we're trying to filter bridge traffic, only look at traffic for 3840 * protocols available in the kernel (IPv4 and/or IPv6) to avoid 3841 * passing traffic for an unsupported protocol to the filter. This is 3842 * lame since if we really wanted, say, an AppleTalk filter, we are 3843 * hosed, but of course we don't have an AppleTalk filter to begin 3844 * with. (Note that since pfil doesn't understand ARP it will pass 3845 * *ALL* ARP traffic.) 3846 */ 3847 switch (ether_type) { 3848 #ifdef INET 3849 case ETHERTYPE_ARP: 3850 case ETHERTYPE_REVARP: 3851 if (V_pfil_ipfw_arp == 0) 3852 return (0); /* Automatically pass */ 3853 3854 /* FALLTHROUGH */ 3855 case ETHERTYPE_IP: 3856 #endif 3857 #ifdef INET6 3858 case ETHERTYPE_IPV6: 3859 #endif /* INET6 */ 3860 break; 3861 3862 default: 3863 /* 3864 * We get here if the packet isn't from a supported 3865 * protocol. Check to see if the user wants to pass 3866 * non-IP packets, these will not be checked by pfil(9) 3867 * and passed unconditionally so the default is to 3868 * drop. 3869 */ 3870 if (V_pfil_onlyip) 3871 goto bad; 3872 } 3873 3874 /* Run the packet through pfil before stripping link headers */ 3875 if (PFIL_HOOKED_OUT(V_link_pfil_head) && V_pfil_ipfw != 0 && 3876 dir == PFIL_OUT && ifp != NULL) { 3877 switch (pfil_mbuf_out(V_link_pfil_head, mp, ifp, NULL)) { 3878 case PFIL_DROPPED: 3879 return (EACCES); 3880 case PFIL_CONSUMED: 3881 return (0); 3882 } 3883 } 3884 3885 /* Strip off the Ethernet header and keep a copy. */ 3886 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2); 3887 m_adj(*mp, ETHER_HDR_LEN); 3888 3889 /* Strip off snap header, if present */ 3890 if (snap) { 3891 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1); 3892 m_adj(*mp, sizeof(struct llc)); 3893 } 3894 3895 /* 3896 * Check the IP header for alignment and errors 3897 */ 3898 if (dir == PFIL_IN) { 3899 switch (ether_type) { 3900 #ifdef INET 3901 case ETHERTYPE_IP: 3902 error = bridge_ip_checkbasic(mp); 3903 break; 3904 #endif 3905 #ifdef INET6 3906 case ETHERTYPE_IPV6: 3907 error = bridge_ip6_checkbasic(mp); 3908 break; 3909 #endif /* INET6 */ 3910 default: 3911 error = 0; 3912 } 3913 if (error) 3914 goto bad; 3915 } 3916 3917 error = 0; 3918 3919 /* 3920 * Run the packet through pfil 3921 */ 3922 rv = PFIL_PASS; 3923 switch (ether_type) { 3924 #ifdef INET 3925 case ETHERTYPE_IP: 3926 /* 3927 * Run pfil on the member interface and the bridge, both can 3928 * be skipped by clearing pfil_member or pfil_bridge. 3929 * 3930 * Keep the order: 3931 * in_if -> bridge_if -> out_if 3932 */ 3933 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv = 3934 pfil_mbuf_out(V_inet_pfil_head, mp, bifp, NULL)) != 3935 PFIL_PASS) 3936 break; 3937 3938 if (V_pfil_member && ifp != NULL) { 3939 rv = (dir == PFIL_OUT) ? 3940 pfil_mbuf_out(V_inet_pfil_head, mp, ifp, NULL) : 3941 pfil_mbuf_in(V_inet_pfil_head, mp, ifp, NULL); 3942 if (rv != PFIL_PASS) 3943 break; 3944 } 3945 3946 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv = 3947 pfil_mbuf_in(V_inet_pfil_head, mp, bifp, NULL)) != 3948 PFIL_PASS) 3949 break; 3950 3951 /* check if we need to fragment the packet */ 3952 /* bridge_fragment generates a mbuf chain of packets */ 3953 /* that already include eth headers */ 3954 if (V_pfil_member && ifp != NULL && dir == PFIL_OUT) { 3955 i = (*mp)->m_pkthdr.len; 3956 if (i > ifp->if_mtu) { 3957 error = bridge_fragment(ifp, mp, &eh2, snap, 3958 &llc1); 3959 return (error); 3960 } 3961 } 3962 3963 /* Recalculate the ip checksum. */ 3964 ip = mtod(*mp, struct ip *); 3965 hlen = ip->ip_hl << 2; 3966 if (hlen < sizeof(struct ip)) 3967 goto bad; 3968 if (hlen > (*mp)->m_len) { 3969 if ((*mp = m_pullup(*mp, hlen)) == NULL) 3970 goto bad; 3971 ip = mtod(*mp, struct ip *); 3972 if (ip == NULL) 3973 goto bad; 3974 } 3975 ip->ip_sum = 0; 3976 if (hlen == sizeof(struct ip)) 3977 ip->ip_sum = in_cksum_hdr(ip); 3978 else 3979 ip->ip_sum = in_cksum(*mp, hlen); 3980 3981 break; 3982 #endif /* INET */ 3983 #ifdef INET6 3984 case ETHERTYPE_IPV6: 3985 if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv = 3986 pfil_mbuf_out(V_inet6_pfil_head, mp, bifp, NULL)) != 3987 PFIL_PASS) 3988 break; 3989 3990 if (V_pfil_member && ifp != NULL) { 3991 rv = (dir == PFIL_OUT) ? 3992 pfil_mbuf_out(V_inet6_pfil_head, mp, ifp, NULL) : 3993 pfil_mbuf_in(V_inet6_pfil_head, mp, ifp, NULL); 3994 if (rv != PFIL_PASS) 3995 break; 3996 } 3997 3998 if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv = 3999 pfil_mbuf_in(V_inet6_pfil_head, mp, bifp, NULL)) != 4000 PFIL_PASS) 4001 break; 4002 break; 4003 #endif 4004 } 4005 4006 switch (rv) { 4007 case PFIL_CONSUMED: 4008 return (0); 4009 case PFIL_DROPPED: 4010 return (EACCES); 4011 default: 4012 break; 4013 } 4014 4015 error = -1; 4016 4017 /* 4018 * Finally, put everything back the way it was and return 4019 */ 4020 if (snap) { 4021 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT); 4022 if (*mp == NULL) 4023 return (error); 4024 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc)); 4025 } 4026 4027 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT); 4028 if (*mp == NULL) 4029 return (error); 4030 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN); 4031 4032 return (0); 4033 4034 bad: 4035 m_freem(*mp); 4036 *mp = NULL; 4037 return (error); 4038 } 4039 4040 #ifdef INET 4041 /* 4042 * Perform basic checks on header size since 4043 * pfil assumes ip_input has already processed 4044 * it for it. Cut-and-pasted from ip_input.c. 4045 * Given how simple the IPv6 version is, 4046 * does the IPv4 version really need to be 4047 * this complicated? 4048 * 4049 * XXX Should we update ipstat here, or not? 4050 * XXX Right now we update ipstat but not 4051 * XXX csum_counter. 4052 */ 4053 static int 4054 bridge_ip_checkbasic(struct mbuf **mp) 4055 { 4056 struct mbuf *m = *mp; 4057 struct ip *ip; 4058 int len, hlen; 4059 u_short sum; 4060 4061 if (*mp == NULL) 4062 return (-1); 4063 4064 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 4065 if ((m = m_copyup(m, sizeof(struct ip), 4066 (max_linkhdr + 3) & ~3)) == NULL) { 4067 /* XXXJRT new stat, please */ 4068 KMOD_IPSTAT_INC(ips_toosmall); 4069 goto bad; 4070 } 4071 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 4072 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 4073 KMOD_IPSTAT_INC(ips_toosmall); 4074 goto bad; 4075 } 4076 } 4077 ip = mtod(m, struct ip *); 4078 if (ip == NULL) goto bad; 4079 4080 if (ip->ip_v != IPVERSION) { 4081 KMOD_IPSTAT_INC(ips_badvers); 4082 goto bad; 4083 } 4084 hlen = ip->ip_hl << 2; 4085 if (hlen < sizeof(struct ip)) { /* minimum header length */ 4086 KMOD_IPSTAT_INC(ips_badhlen); 4087 goto bad; 4088 } 4089 if (hlen > m->m_len) { 4090 if ((m = m_pullup(m, hlen)) == NULL) { 4091 KMOD_IPSTAT_INC(ips_badhlen); 4092 goto bad; 4093 } 4094 ip = mtod(m, struct ip *); 4095 if (ip == NULL) goto bad; 4096 } 4097 4098 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 4099 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 4100 } else { 4101 if (hlen == sizeof(struct ip)) { 4102 sum = in_cksum_hdr(ip); 4103 } else { 4104 sum = in_cksum(m, hlen); 4105 } 4106 } 4107 if (sum) { 4108 KMOD_IPSTAT_INC(ips_badsum); 4109 goto bad; 4110 } 4111 4112 /* Retrieve the packet length. */ 4113 len = ntohs(ip->ip_len); 4114 4115 /* 4116 * Check for additional length bogosity 4117 */ 4118 if (len < hlen) { 4119 KMOD_IPSTAT_INC(ips_badlen); 4120 goto bad; 4121 } 4122 4123 /* 4124 * Check that the amount of data in the buffers 4125 * is as at least much as the IP header would have us expect. 4126 * Drop packet if shorter than we expect. 4127 */ 4128 if (m->m_pkthdr.len < len) { 4129 KMOD_IPSTAT_INC(ips_tooshort); 4130 goto bad; 4131 } 4132 4133 /* Checks out, proceed */ 4134 *mp = m; 4135 return (0); 4136 4137 bad: 4138 *mp = m; 4139 return (-1); 4140 } 4141 #endif /* INET */ 4142 4143 #ifdef INET6 4144 /* 4145 * Same as above, but for IPv6. 4146 * Cut-and-pasted from ip6_input.c. 4147 * XXX Should we update ip6stat, or not? 4148 */ 4149 static int 4150 bridge_ip6_checkbasic(struct mbuf **mp) 4151 { 4152 struct mbuf *m = *mp; 4153 struct ip6_hdr *ip6; 4154 4155 /* 4156 * If the IPv6 header is not aligned, slurp it up into a new 4157 * mbuf with space for link headers, in the event we forward 4158 * it. Otherwise, if it is aligned, make sure the entire base 4159 * IPv6 header is in the first mbuf of the chain. 4160 */ 4161 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 4162 struct ifnet *inifp = m->m_pkthdr.rcvif; 4163 if ((m = m_copyup(m, sizeof(struct ip6_hdr), 4164 (max_linkhdr + 3) & ~3)) == NULL) { 4165 /* XXXJRT new stat, please */ 4166 IP6STAT_INC(ip6s_toosmall); 4167 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 4168 goto bad; 4169 } 4170 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { 4171 struct ifnet *inifp = m->m_pkthdr.rcvif; 4172 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 4173 IP6STAT_INC(ip6s_toosmall); 4174 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 4175 goto bad; 4176 } 4177 } 4178 4179 ip6 = mtod(m, struct ip6_hdr *); 4180 4181 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 4182 IP6STAT_INC(ip6s_badvers); 4183 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 4184 goto bad; 4185 } 4186 4187 /* Checks out, proceed */ 4188 *mp = m; 4189 return (0); 4190 4191 bad: 4192 *mp = m; 4193 return (-1); 4194 } 4195 #endif /* INET6 */ 4196 4197 #ifdef INET 4198 /* 4199 * bridge_fragment: 4200 * 4201 * Fragment mbuf chain in multiple packets and prepend ethernet header. 4202 */ 4203 static int 4204 bridge_fragment(struct ifnet *ifp, struct mbuf **mp, struct ether_header *eh, 4205 int snap, struct llc *llc) 4206 { 4207 struct mbuf *m = *mp, *nextpkt = NULL, *mprev = NULL, *mcur = NULL; 4208 struct ip *ip; 4209 int error = -1; 4210 4211 if (m->m_len < sizeof(struct ip) && 4212 (m = m_pullup(m, sizeof(struct ip))) == NULL) 4213 goto dropit; 4214 ip = mtod(m, struct ip *); 4215 4216 m->m_pkthdr.csum_flags |= CSUM_IP; 4217 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist); 4218 if (error) 4219 goto dropit; 4220 4221 /* 4222 * Walk the chain and re-add the Ethernet header for 4223 * each mbuf packet. 4224 */ 4225 for (mcur = m; mcur; mcur = mcur->m_nextpkt) { 4226 nextpkt = mcur->m_nextpkt; 4227 mcur->m_nextpkt = NULL; 4228 if (snap) { 4229 M_PREPEND(mcur, sizeof(struct llc), M_NOWAIT); 4230 if (mcur == NULL) { 4231 error = ENOBUFS; 4232 if (mprev != NULL) 4233 mprev->m_nextpkt = nextpkt; 4234 goto dropit; 4235 } 4236 bcopy(llc, mtod(mcur, caddr_t),sizeof(struct llc)); 4237 } 4238 4239 M_PREPEND(mcur, ETHER_HDR_LEN, M_NOWAIT); 4240 if (mcur == NULL) { 4241 error = ENOBUFS; 4242 if (mprev != NULL) 4243 mprev->m_nextpkt = nextpkt; 4244 goto dropit; 4245 } 4246 bcopy(eh, mtod(mcur, caddr_t), ETHER_HDR_LEN); 4247 4248 /* 4249 * The previous two M_PREPEND could have inserted one or two 4250 * mbufs in front so we have to update the previous packet's 4251 * m_nextpkt. 4252 */ 4253 mcur->m_nextpkt = nextpkt; 4254 if (mprev != NULL) 4255 mprev->m_nextpkt = mcur; 4256 else { 4257 /* The first mbuf in the original chain needs to be 4258 * updated. */ 4259 *mp = mcur; 4260 } 4261 mprev = mcur; 4262 } 4263 4264 KMOD_IPSTAT_INC(ips_fragmented); 4265 return (error); 4266 4267 dropit: 4268 for (mcur = *mp; mcur; mcur = m) { /* droping the full packet chain */ 4269 m = mcur->m_nextpkt; 4270 m_freem(mcur); 4271 } 4272 return (error); 4273 } 4274 #endif /* INET */ 4275 4276 static void 4277 bridge_linkstate(struct ifnet *ifp) 4278 { 4279 struct bridge_softc *sc = NULL; 4280 struct bridge_iflist *bif; 4281 struct epoch_tracker et; 4282 4283 NET_EPOCH_ENTER(et); 4284 4285 bif = ifp->if_bridge; 4286 if (bif) 4287 sc = bif->bif_sc; 4288 4289 if (sc != NULL) { 4290 bridge_linkcheck(sc); 4291 bstp_linkstate(&bif->bif_stp); 4292 } 4293 4294 NET_EPOCH_EXIT(et); 4295 } 4296 4297 static void 4298 bridge_linkcheck(struct bridge_softc *sc) 4299 { 4300 struct bridge_iflist *bif; 4301 int new_link, hasls; 4302 4303 BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc); 4304 4305 new_link = LINK_STATE_DOWN; 4306 hasls = 0; 4307 /* Our link is considered up if at least one of our ports is active */ 4308 CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) { 4309 if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE) 4310 hasls++; 4311 if (bif->bif_ifp->if_link_state == LINK_STATE_UP) { 4312 new_link = LINK_STATE_UP; 4313 break; 4314 } 4315 } 4316 if (!CK_LIST_EMPTY(&sc->sc_iflist) && !hasls) { 4317 /* If no interfaces support link-state then we default to up */ 4318 new_link = LINK_STATE_UP; 4319 } 4320 if_link_state_change(sc->sc_ifp, new_link); 4321 } 4322