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