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