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