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