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