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