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