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