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