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