1 /* 2 * Copyright 2001 Wasabi Systems, Inc. 3 * All rights reserved. 4 * 5 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project by 18 * Wasabi Systems, Inc. 19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 20 * or promote products derived from this software without specific prior 21 * written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 38 * All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by Jason L. Wright 51 * 4. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 64 * POSSIBILITY OF SUCH DAMAGE. 65 * 66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $ 67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ 68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $ 69 * $DragonFly: src/sys/net/bridge/if_bridge.c,v 1.60 2008/11/26 12:49:43 sephe Exp $ 70 */ 71 72 /* 73 * Network interface bridge support. 74 * 75 * TODO: 76 * 77 * - Currently only supports Ethernet-like interfaces (Ethernet, 78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way 79 * to bridge other types of interfaces (FDDI-FDDI, and maybe 80 * consider heterogenous bridges). 81 * 82 * 83 * Bridge's route information is duplicated to each CPUs: 84 * 85 * CPU0 CPU1 CPU2 CPU3 86 * +-----------+ +-----------+ +-----------+ +-----------+ 87 * | rtnode | | rtnode | | rtnode | | rtnode | 88 * | | | | | | | | 89 * | dst eaddr | | dst eaddr | | dst eaddr | | dst eaddr | 90 * +-----------+ +-----------+ +-----------+ +-----------+ 91 * | | | | 92 * | | | | 93 * | | +----------+ | | 94 * | | | rtinfo | | | 95 * | +---->| |<---+ | 96 * | | flags | | 97 * +-------------->| timeout |<-------------+ 98 * | dst_ifp | 99 * +----------+ 100 * 101 * We choose to put timeout and dst_ifp into shared part, so updating 102 * them will be cheaper than using message forwarding. Also there is 103 * not need to use spinlock to protect the updating: timeout and dst_ifp 104 * is not related and specific field's updating order has no importance. 105 * The cache pollution by the share part should not be heavy: in a stable 106 * setup, dst_ifp probably will be not changed in rtnode's life time, 107 * while timeout is refreshed once per second; most of the time, timeout 108 * and dst_ifp are read-only accessed. 109 * 110 * 111 * Bridge route information installation on bridge_input path: 112 * 113 * CPU0 CPU1 CPU2 CPU3 114 * 115 * tcp_thread2 116 * | 117 * alloc nmsg 118 * snd nmsg | 119 * w/o rtinfo | 120 * ifnet0<-----------------------+ 121 * | : 122 * lookup dst : 123 * rtnode exists?(Y)free nmsg : 124 * |(N) : 125 * | 126 * alloc rtinfo 127 * alloc rtnode 128 * install rtnode 129 * | 130 * +---------->ifnet1 131 * : fwd nmsg | 132 * : w/ rtinfo | 133 * : | 134 * : | 135 * alloc rtnode 136 * (w/ nmsg's rtinfo) 137 * install rtnode 138 * | 139 * +---------->ifnet2 140 * : fwd nmsg | 141 * : w/ rtinfo | 142 * : | 143 * : same as ifnet1 144 * | 145 * +---------->ifnet3 146 * : fwd nmsg | 147 * : w/ rtinfo | 148 * : | 149 * : same as ifnet1 150 * free nmsg 151 * : 152 * : 153 * 154 * The netmsgs forwarded between protocol threads and ifnet threads are 155 * allocated with (M_WAITOK|M_NULLOK), so it will not fail under most 156 * cases (route information is too precious to be not installed :). 157 * Since multiple threads may try to install route information for the 158 * same dst eaddr, we look up route information in ifnet0. However, this 159 * looking up only need to be performed on ifnet0, which is the start 160 * point of the route information installation process. 161 * 162 * 163 * Bridge route information deleting/flushing: 164 * 165 * CPU0 CPU1 CPU2 CPU3 166 * 167 * netisr0 168 * | 169 * find suitable rtnodes, 170 * mark their rtinfo dead 171 * | 172 * | domsg <------------------------------------------+ 173 * | | replymsg 174 * | | 175 * V fwdmsg fwdmsg fwdmsg | 176 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3 177 * delete rtnodes delete rtnodes delete rtnodes delete rtnodes 178 * w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo 179 * free dead rtinfos 180 * 181 * All deleting/flushing operations are serialized by netisr0, so each 182 * operation only reaps the route information marked dead by itself. 183 * 184 * 185 * Bridge route information adding/deleting/flushing: 186 * Since all operation is serialized by the fixed message flow between 187 * ifnet threads, it is not possible to create corrupted per-cpu route 188 * information. 189 * 190 * 191 * 192 * Percpu member interface list iteration with blocking operation: 193 * Since one bridge could only delete one member interface at a time and 194 * the deleted member interface is not freed after netmsg_service_sync(), 195 * following way is used to make sure that even if the certain member 196 * interface is ripped from the percpu list during the blocking operation, 197 * the iteration still could keep going: 198 * 199 * LIST_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) { 200 * blocking operation; 201 * blocking operation; 202 * ... 203 * ... 204 * if (nbif != NULL && !nbif->bif_onlist) { 205 * KKASSERT(bif->bif_onlist); 206 * nbif = LIST_NEXT(bif, bif_next); 207 * } 208 * } 209 * 210 * As mentioned above only one member interface could be unlinked from the 211 * percpu member interface list, so either bif or nbif may be not on the list, 212 * but _not_ both. To keep the list iteration, we don't care about bif, but 213 * only nbif. Since removed member interface will only be freed after we 214 * finish our work, it is safe to access any field in an unlinked bif (here 215 * bif_onlist). If nbif is no longer on the list, then bif must be on the 216 * list, so we change nbif to the next element of bif and keep going. 217 */ 218 219 #include "opt_inet.h" 220 #include "opt_inet6.h" 221 222 #include <sys/param.h> 223 #include <sys/mbuf.h> 224 #include <sys/malloc.h> 225 #include <sys/protosw.h> 226 #include <sys/systm.h> 227 #include <sys/time.h> 228 #include <sys/socket.h> /* for net/if.h */ 229 #include <sys/sockio.h> 230 #include <sys/ctype.h> /* string functions */ 231 #include <sys/kernel.h> 232 #include <sys/random.h> 233 #include <sys/sysctl.h> 234 #include <sys/module.h> 235 #include <sys/proc.h> 236 #include <sys/priv.h> 237 #include <sys/lock.h> 238 #include <sys/thread.h> 239 #include <sys/thread2.h> 240 #include <sys/mpipe.h> 241 242 #include <net/bpf.h> 243 #include <net/if.h> 244 #include <net/if_dl.h> 245 #include <net/if_types.h> 246 #include <net/if_var.h> 247 #include <net/pfil.h> 248 #include <net/ifq_var.h> 249 #include <net/if_clone.h> 250 251 #include <netinet/in.h> /* for struct arpcom */ 252 #include <netinet/in_systm.h> 253 #include <netinet/in_var.h> 254 #include <netinet/ip.h> 255 #include <netinet/ip_var.h> 256 #ifdef INET6 257 #include <netinet/ip6.h> 258 #include <netinet6/ip6_var.h> 259 #endif 260 #include <netinet/if_ether.h> /* for struct arpcom */ 261 #include <net/bridge/if_bridgevar.h> 262 #include <net/if_llc.h> 263 #include <net/netmsg2.h> 264 265 #include <net/route.h> 266 #include <sys/in_cksum.h> 267 268 /* 269 * Size of the route hash table. Must be a power of two. 270 */ 271 #ifndef BRIDGE_RTHASH_SIZE 272 #define BRIDGE_RTHASH_SIZE 1024 273 #endif 274 275 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1) 276 277 /* 278 * Maximum number of addresses to cache. 279 */ 280 #ifndef BRIDGE_RTABLE_MAX 281 #define BRIDGE_RTABLE_MAX 100 282 #endif 283 284 /* 285 * Spanning tree defaults. 286 */ 287 #define BSTP_DEFAULT_MAX_AGE (20 * 256) 288 #define BSTP_DEFAULT_HELLO_TIME (2 * 256) 289 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 290 #define BSTP_DEFAULT_HOLD_TIME (1 * 256) 291 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 292 #define BSTP_DEFAULT_PORT_PRIORITY 0x80 293 #define BSTP_DEFAULT_PATH_COST 55 294 295 /* 296 * Timeout (in seconds) for entries learned dynamically. 297 */ 298 #ifndef BRIDGE_RTABLE_TIMEOUT 299 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ 300 #endif 301 302 /* 303 * Number of seconds between walks of the route list. 304 */ 305 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD 306 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) 307 #endif 308 309 /* 310 * List of capabilities to mask on the member interface. 311 */ 312 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM 313 314 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *); 315 316 struct netmsg_brctl { 317 struct netmsg_base base; 318 bridge_ctl_t bc_func; 319 struct bridge_softc *bc_sc; 320 void *bc_arg; 321 }; 322 323 struct netmsg_brsaddr { 324 struct netmsg_base base; 325 struct bridge_softc *br_softc; 326 struct ifnet *br_dst_if; 327 struct bridge_rtinfo *br_rtinfo; 328 int br_setflags; 329 uint8_t br_dst[ETHER_ADDR_LEN]; 330 uint8_t br_flags; 331 }; 332 333 struct netmsg_braddbif { 334 struct netmsg_base base; 335 struct bridge_softc *br_softc; 336 struct bridge_ifinfo *br_bif_info; 337 struct ifnet *br_bif_ifp; 338 }; 339 340 struct netmsg_brdelbif { 341 struct netmsg_base base; 342 struct bridge_softc *br_softc; 343 struct bridge_ifinfo *br_bif_info; 344 struct bridge_iflist_head *br_bif_list; 345 }; 346 347 struct netmsg_brsflags { 348 struct netmsg_base base; 349 struct bridge_softc *br_softc; 350 struct bridge_ifinfo *br_bif_info; 351 uint32_t br_bif_flags; 352 }; 353 354 eventhandler_tag bridge_detach_cookie = NULL; 355 356 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 357 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *); 358 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *); 359 360 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; 361 362 static int bridge_clone_create(struct if_clone *, int, caddr_t); 363 static void bridge_clone_destroy(struct ifnet *); 364 365 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 366 static void bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int); 367 static void bridge_ifdetach(void *, struct ifnet *); 368 static void bridge_init(void *); 369 static void bridge_stop(struct ifnet *); 370 static void bridge_start(struct ifnet *); 371 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *); 372 static int bridge_output(struct ifnet *, struct mbuf *); 373 374 static void bridge_forward(struct bridge_softc *, struct mbuf *m); 375 376 static void bridge_timer_handler(netmsg_t); 377 static void bridge_timer(void *); 378 379 static void bridge_start_bcast(struct bridge_softc *, struct mbuf *); 380 static void bridge_broadcast(struct bridge_softc *, struct ifnet *, 381 struct mbuf *); 382 static void bridge_span(struct bridge_softc *, struct mbuf *); 383 384 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *, 385 struct ifnet *, uint8_t); 386 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *); 387 static void bridge_rtreap(struct bridge_softc *); 388 static void bridge_rtreap_async(struct bridge_softc *); 389 static void bridge_rttrim(struct bridge_softc *); 390 static int bridge_rtage_finddead(struct bridge_softc *); 391 static void bridge_rtage(struct bridge_softc *); 392 static void bridge_rtflush(struct bridge_softc *, int); 393 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *); 394 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *, 395 struct ifnet *, uint8_t); 396 static void bridge_rtmsg_sync(struct bridge_softc *sc); 397 static void bridge_rtreap_handler(netmsg_t); 398 static void bridge_rtinstall_handler(netmsg_t); 399 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *, 400 struct ifnet *, int, uint8_t, struct bridge_rtinfo **); 401 402 static void bridge_rtable_init(struct bridge_softc *); 403 static void bridge_rtable_fini(struct bridge_softc *); 404 405 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *); 406 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, 407 const uint8_t *); 408 static void bridge_rtnode_insert(struct bridge_softc *, 409 struct bridge_rtnode *); 410 static void bridge_rtnode_destroy(struct bridge_softc *, 411 struct bridge_rtnode *); 412 413 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, 414 const char *name); 415 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, 416 struct ifnet *ifp); 417 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *, 418 struct bridge_ifinfo *); 419 static void bridge_delete_member(struct bridge_softc *, 420 struct bridge_iflist *, int); 421 static void bridge_delete_span(struct bridge_softc *, 422 struct bridge_iflist *); 423 424 static int bridge_control(struct bridge_softc *, u_long, 425 bridge_ctl_t, void *); 426 static int bridge_ioctl_init(struct bridge_softc *, void *); 427 static int bridge_ioctl_stop(struct bridge_softc *, void *); 428 static int bridge_ioctl_add(struct bridge_softc *, void *); 429 static int bridge_ioctl_del(struct bridge_softc *, void *); 430 static int bridge_ioctl_gifflags(struct bridge_softc *, void *); 431 static int bridge_ioctl_sifflags(struct bridge_softc *, void *); 432 static int bridge_ioctl_scache(struct bridge_softc *, void *); 433 static int bridge_ioctl_gcache(struct bridge_softc *, void *); 434 static int bridge_ioctl_gifs(struct bridge_softc *, void *); 435 static int bridge_ioctl_rts(struct bridge_softc *, void *); 436 static int bridge_ioctl_saddr(struct bridge_softc *, void *); 437 static int bridge_ioctl_sto(struct bridge_softc *, void *); 438 static int bridge_ioctl_gto(struct bridge_softc *, void *); 439 static int bridge_ioctl_daddr(struct bridge_softc *, void *); 440 static int bridge_ioctl_flush(struct bridge_softc *, void *); 441 static int bridge_ioctl_gpri(struct bridge_softc *, void *); 442 static int bridge_ioctl_spri(struct bridge_softc *, void *); 443 static int bridge_ioctl_ght(struct bridge_softc *, void *); 444 static int bridge_ioctl_sht(struct bridge_softc *, void *); 445 static int bridge_ioctl_gfd(struct bridge_softc *, void *); 446 static int bridge_ioctl_sfd(struct bridge_softc *, void *); 447 static int bridge_ioctl_gma(struct bridge_softc *, void *); 448 static int bridge_ioctl_sma(struct bridge_softc *, void *); 449 static int bridge_ioctl_sifprio(struct bridge_softc *, void *); 450 static int bridge_ioctl_sifcost(struct bridge_softc *, void *); 451 static int bridge_ioctl_addspan(struct bridge_softc *, void *); 452 static int bridge_ioctl_delspan(struct bridge_softc *, void *); 453 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *, 454 int); 455 static int bridge_ip_checkbasic(struct mbuf **mp); 456 #ifdef INET6 457 static int bridge_ip6_checkbasic(struct mbuf **mp); 458 #endif /* INET6 */ 459 static int bridge_fragment(struct ifnet *, struct mbuf *, 460 struct ether_header *, int, struct llc *); 461 static void bridge_enqueue_handler(netmsg_t); 462 static void bridge_handoff(struct ifnet *, struct mbuf *); 463 464 static void bridge_del_bif_handler(netmsg_t); 465 static void bridge_add_bif_handler(netmsg_t); 466 static void bridge_set_bifflags_handler(netmsg_t); 467 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *, 468 struct bridge_iflist_head *); 469 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *, 470 struct ifnet *); 471 static void bridge_set_bifflags(struct bridge_softc *, 472 struct bridge_ifinfo *, uint32_t); 473 474 SYSCTL_DECL(_net_link); 475 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge"); 476 477 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */ 478 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */ 479 static int pfil_member = 1; /* run pfil hooks on the member interface */ 480 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW, 481 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled"); 482 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW, 483 &pfil_bridge, 0, "Packet filter on the bridge interface"); 484 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW, 485 &pfil_member, 0, "Packet filter on the member interface"); 486 487 struct bridge_control_arg { 488 union { 489 struct ifbreq ifbreq; 490 struct ifbifconf ifbifconf; 491 struct ifbareq ifbareq; 492 struct ifbaconf ifbaconf; 493 struct ifbrparam ifbrparam; 494 } bca_u; 495 int bca_len; 496 void *bca_uptr; 497 void *bca_kptr; 498 }; 499 500 struct bridge_control { 501 bridge_ctl_t bc_func; 502 int bc_argsize; 503 int bc_flags; 504 }; 505 506 #define BC_F_COPYIN 0x01 /* copy arguments in */ 507 #define BC_F_COPYOUT 0x02 /* copy arguments out */ 508 #define BC_F_SUSER 0x04 /* do super-user check */ 509 510 const struct bridge_control bridge_control_table[] = { 511 { bridge_ioctl_add, sizeof(struct ifbreq), 512 BC_F_COPYIN|BC_F_SUSER }, 513 { bridge_ioctl_del, sizeof(struct ifbreq), 514 BC_F_COPYIN|BC_F_SUSER }, 515 516 { bridge_ioctl_gifflags, sizeof(struct ifbreq), 517 BC_F_COPYIN|BC_F_COPYOUT }, 518 { bridge_ioctl_sifflags, sizeof(struct ifbreq), 519 BC_F_COPYIN|BC_F_SUSER }, 520 521 { bridge_ioctl_scache, sizeof(struct ifbrparam), 522 BC_F_COPYIN|BC_F_SUSER }, 523 { bridge_ioctl_gcache, sizeof(struct ifbrparam), 524 BC_F_COPYOUT }, 525 526 { bridge_ioctl_gifs, sizeof(struct ifbifconf), 527 BC_F_COPYIN|BC_F_COPYOUT }, 528 { bridge_ioctl_rts, sizeof(struct ifbaconf), 529 BC_F_COPYIN|BC_F_COPYOUT }, 530 531 { bridge_ioctl_saddr, sizeof(struct ifbareq), 532 BC_F_COPYIN|BC_F_SUSER }, 533 534 { bridge_ioctl_sto, sizeof(struct ifbrparam), 535 BC_F_COPYIN|BC_F_SUSER }, 536 { bridge_ioctl_gto, sizeof(struct ifbrparam), 537 BC_F_COPYOUT }, 538 539 { bridge_ioctl_daddr, sizeof(struct ifbareq), 540 BC_F_COPYIN|BC_F_SUSER }, 541 542 { bridge_ioctl_flush, sizeof(struct ifbreq), 543 BC_F_COPYIN|BC_F_SUSER }, 544 545 { bridge_ioctl_gpri, sizeof(struct ifbrparam), 546 BC_F_COPYOUT }, 547 { bridge_ioctl_spri, sizeof(struct ifbrparam), 548 BC_F_COPYIN|BC_F_SUSER }, 549 550 { bridge_ioctl_ght, sizeof(struct ifbrparam), 551 BC_F_COPYOUT }, 552 { bridge_ioctl_sht, sizeof(struct ifbrparam), 553 BC_F_COPYIN|BC_F_SUSER }, 554 555 { bridge_ioctl_gfd, sizeof(struct ifbrparam), 556 BC_F_COPYOUT }, 557 { bridge_ioctl_sfd, sizeof(struct ifbrparam), 558 BC_F_COPYIN|BC_F_SUSER }, 559 560 { bridge_ioctl_gma, sizeof(struct ifbrparam), 561 BC_F_COPYOUT }, 562 { bridge_ioctl_sma, sizeof(struct ifbrparam), 563 BC_F_COPYIN|BC_F_SUSER }, 564 565 { bridge_ioctl_sifprio, sizeof(struct ifbreq), 566 BC_F_COPYIN|BC_F_SUSER }, 567 568 { bridge_ioctl_sifcost, sizeof(struct ifbreq), 569 BC_F_COPYIN|BC_F_SUSER }, 570 571 { bridge_ioctl_addspan, sizeof(struct ifbreq), 572 BC_F_COPYIN|BC_F_SUSER }, 573 { bridge_ioctl_delspan, sizeof(struct ifbreq), 574 BC_F_COPYIN|BC_F_SUSER }, 575 }; 576 static const int bridge_control_table_size = 577 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]); 578 579 LIST_HEAD(, bridge_softc) bridge_list; 580 581 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge", 582 bridge_clone_create, 583 bridge_clone_destroy, 0, IF_MAXUNIT); 584 585 static int 586 bridge_modevent(module_t mod, int type, void *data) 587 { 588 switch (type) { 589 case MOD_LOAD: 590 LIST_INIT(&bridge_list); 591 if_clone_attach(&bridge_cloner); 592 bridge_input_p = bridge_input; 593 bridge_output_p = bridge_output; 594 bridge_detach_cookie = EVENTHANDLER_REGISTER( 595 ifnet_detach_event, bridge_ifdetach, NULL, 596 EVENTHANDLER_PRI_ANY); 597 #if notyet 598 bstp_linkstate_p = bstp_linkstate; 599 #endif 600 break; 601 case MOD_UNLOAD: 602 if (!LIST_EMPTY(&bridge_list)) 603 return (EBUSY); 604 EVENTHANDLER_DEREGISTER(ifnet_detach_event, 605 bridge_detach_cookie); 606 if_clone_detach(&bridge_cloner); 607 bridge_input_p = NULL; 608 bridge_output_p = NULL; 609 #if notyet 610 bstp_linkstate_p = NULL; 611 #endif 612 break; 613 default: 614 return (EOPNOTSUPP); 615 } 616 return (0); 617 } 618 619 static moduledata_t bridge_mod = { 620 "if_bridge", 621 bridge_modevent, 622 0 623 }; 624 625 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 626 627 628 /* 629 * bridge_clone_create: 630 * 631 * Create a new bridge instance. 632 */ 633 static int 634 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused) 635 { 636 struct bridge_softc *sc; 637 struct ifnet *ifp; 638 u_char eaddr[6]; 639 int cpu, rnd; 640 641 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 642 ifp = sc->sc_ifp = &sc->sc_if; 643 644 sc->sc_brtmax = BRIDGE_RTABLE_MAX; 645 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; 646 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE; 647 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME; 648 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY; 649 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY; 650 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME; 651 652 /* Initialize our routing table. */ 653 bridge_rtable_init(sc); 654 655 callout_init(&sc->sc_brcallout); 656 netmsg_init(&sc->sc_brtimemsg, NULL, &netisr_adone_rport, 657 MSGF_DROPABLE, bridge_timer_handler); 658 sc->sc_brtimemsg.lmsg.u.ms_resultp = sc; 659 660 callout_init(&sc->sc_bstpcallout); 661 netmsg_init(&sc->sc_bstptimemsg, NULL, &netisr_adone_rport, 662 MSGF_DROPABLE, bstp_tick_handler); 663 sc->sc_bstptimemsg.lmsg.u.ms_resultp = sc; 664 665 /* Initialize per-cpu member iface lists */ 666 sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus, 667 M_DEVBUF, M_WAITOK); 668 for (cpu = 0; cpu < ncpus; ++cpu) 669 LIST_INIT(&sc->sc_iflists[cpu]); 670 671 LIST_INIT(&sc->sc_spanlist); 672 673 ifp->if_softc = sc; 674 if_initname(ifp, ifc->ifc_name, unit); 675 ifp->if_mtu = ETHERMTU; 676 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST; 677 ifp->if_ioctl = bridge_ioctl; 678 ifp->if_start = bridge_start; 679 ifp->if_init = bridge_init; 680 ifp->if_type = IFT_BRIDGE; 681 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 682 ifq_set_ready(&ifp->if_snd); 683 ifp->if_hdrlen = ETHER_HDR_LEN; 684 685 /* 686 * Generate a random ethernet address and use the private AC:DE:48 687 * OUI code. 688 */ 689 rnd = karc4random(); 690 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */ 691 rnd = karc4random(); 692 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */ 693 694 eaddr[0] &= ~1; /* clear multicast bit */ 695 eaddr[0] |= 2; /* set the LAA bit */ 696 697 ether_ifattach(ifp, eaddr, NULL); 698 /* Now undo some of the damage... */ 699 ifp->if_baudrate = 0; 700 ifp->if_type = IFT_BRIDGE; 701 702 crit_enter(); /* XXX MP */ 703 LIST_INSERT_HEAD(&bridge_list, sc, sc_list); 704 crit_exit(); 705 706 return (0); 707 } 708 709 static void 710 bridge_delete_dispatch(netmsg_t msg) 711 { 712 struct bridge_softc *sc = msg->lmsg.u.ms_resultp; 713 struct ifnet *bifp = sc->sc_ifp; 714 struct bridge_iflist *bif; 715 716 ifnet_serialize_all(bifp); 717 718 while ((bif = LIST_FIRST(&sc->sc_iflists[mycpuid])) != NULL) 719 bridge_delete_member(sc, bif, 0); 720 721 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) 722 bridge_delete_span(sc, bif); 723 724 ifnet_deserialize_all(bifp); 725 726 lwkt_replymsg(&msg->lmsg, 0); 727 } 728 729 /* 730 * bridge_clone_destroy: 731 * 732 * Destroy a bridge instance. 733 */ 734 static void 735 bridge_clone_destroy(struct ifnet *ifp) 736 { 737 struct bridge_softc *sc = ifp->if_softc; 738 struct netmsg_base msg; 739 740 ifnet_serialize_all(ifp); 741 742 bridge_stop(ifp); 743 ifp->if_flags &= ~IFF_UP; 744 745 ifnet_deserialize_all(ifp); 746 747 netmsg_init(&msg, NULL, &curthread->td_msgport, 748 0, bridge_delete_dispatch); 749 msg.lmsg.u.ms_resultp = sc; 750 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0); 751 752 crit_enter(); /* XXX MP */ 753 LIST_REMOVE(sc, sc_list); 754 crit_exit(); 755 756 ether_ifdetach(ifp); 757 758 /* Tear down the routing table. */ 759 bridge_rtable_fini(sc); 760 761 /* Free per-cpu member iface lists */ 762 kfree(sc->sc_iflists, M_DEVBUF); 763 764 kfree(sc, M_DEVBUF); 765 } 766 767 /* 768 * bridge_ioctl: 769 * 770 * Handle a control request from the operator. 771 */ 772 static int 773 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 774 { 775 struct bridge_softc *sc = ifp->if_softc; 776 struct bridge_control_arg args; 777 struct ifdrv *ifd = (struct ifdrv *) data; 778 const struct bridge_control *bc; 779 int error = 0; 780 781 ASSERT_IFNET_SERIALIZED_ALL(ifp); 782 783 switch (cmd) { 784 case SIOCADDMULTI: 785 case SIOCDELMULTI: 786 break; 787 788 case SIOCGDRVSPEC: 789 case SIOCSDRVSPEC: 790 if (ifd->ifd_cmd >= bridge_control_table_size) { 791 error = EINVAL; 792 break; 793 } 794 bc = &bridge_control_table[ifd->ifd_cmd]; 795 796 if (cmd == SIOCGDRVSPEC && 797 (bc->bc_flags & BC_F_COPYOUT) == 0) { 798 error = EINVAL; 799 break; 800 } else if (cmd == SIOCSDRVSPEC && 801 (bc->bc_flags & BC_F_COPYOUT)) { 802 error = EINVAL; 803 break; 804 } 805 806 if (bc->bc_flags & BC_F_SUSER) { 807 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY); 808 if (error) 809 break; 810 } 811 812 if (ifd->ifd_len != bc->bc_argsize || 813 ifd->ifd_len > sizeof(args.bca_u)) { 814 error = EINVAL; 815 break; 816 } 817 818 memset(&args, 0, sizeof(args)); 819 if (bc->bc_flags & BC_F_COPYIN) { 820 error = copyin(ifd->ifd_data, &args.bca_u, 821 ifd->ifd_len); 822 if (error) 823 break; 824 } 825 826 error = bridge_control(sc, cmd, bc->bc_func, &args); 827 if (error) { 828 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL); 829 break; 830 } 831 832 if (bc->bc_flags & BC_F_COPYOUT) { 833 error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 834 if (args.bca_len != 0) { 835 KKASSERT(args.bca_kptr != NULL); 836 if (!error) { 837 error = copyout(args.bca_kptr, 838 args.bca_uptr, args.bca_len); 839 } 840 kfree(args.bca_kptr, M_TEMP); 841 } else { 842 KKASSERT(args.bca_kptr == NULL); 843 } 844 } else { 845 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL); 846 } 847 break; 848 849 case SIOCSIFFLAGS: 850 if (!(ifp->if_flags & IFF_UP) && 851 (ifp->if_flags & IFF_RUNNING)) { 852 /* 853 * If interface is marked down and it is running, 854 * then stop it. 855 */ 856 bridge_stop(ifp); 857 } else if ((ifp->if_flags & IFF_UP) && 858 !(ifp->if_flags & IFF_RUNNING)) { 859 /* 860 * If interface is marked up and it is stopped, then 861 * start it. 862 */ 863 ifp->if_init(sc); 864 } 865 break; 866 867 case SIOCSIFMTU: 868 /* Do not allow the MTU to be changed on the bridge */ 869 error = EINVAL; 870 break; 871 872 default: 873 error = ether_ioctl(ifp, cmd, data); 874 break; 875 } 876 return (error); 877 } 878 879 /* 880 * bridge_mutecaps: 881 * 882 * Clear or restore unwanted capabilities on the member interface 883 */ 884 static void 885 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute) 886 { 887 struct ifreq ifr; 888 int error; 889 890 if (ifp->if_ioctl == NULL) 891 return; 892 893 bzero(&ifr, sizeof(ifr)); 894 ifr.ifr_reqcap = ifp->if_capenable; 895 896 if (mute) { 897 /* mask off and save capabilities */ 898 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK; 899 if (bif_info->bifi_mutecap != 0) 900 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK; 901 } else { 902 /* restore muted capabilities */ 903 ifr.ifr_reqcap |= bif_info->bifi_mutecap; 904 } 905 906 if (bif_info->bifi_mutecap != 0) { 907 ifnet_serialize_all(ifp); 908 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL); 909 ifnet_deserialize_all(ifp); 910 } 911 } 912 913 /* 914 * bridge_lookup_member: 915 * 916 * Lookup a bridge member interface. 917 */ 918 static struct bridge_iflist * 919 bridge_lookup_member(struct bridge_softc *sc, const char *name) 920 { 921 struct bridge_iflist *bif; 922 923 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 924 if (strcmp(bif->bif_ifp->if_xname, name) == 0) 925 return (bif); 926 } 927 return (NULL); 928 } 929 930 /* 931 * bridge_lookup_member_if: 932 * 933 * Lookup a bridge member interface by ifnet*. 934 */ 935 static struct bridge_iflist * 936 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp) 937 { 938 struct bridge_iflist *bif; 939 940 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 941 if (bif->bif_ifp == member_ifp) 942 return (bif); 943 } 944 return (NULL); 945 } 946 947 /* 948 * bridge_lookup_member_ifinfo: 949 * 950 * Lookup a bridge member interface by bridge_ifinfo. 951 */ 952 static struct bridge_iflist * 953 bridge_lookup_member_ifinfo(struct bridge_softc *sc, 954 struct bridge_ifinfo *bif_info) 955 { 956 struct bridge_iflist *bif; 957 958 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 959 if (bif->bif_info == bif_info) 960 return (bif); 961 } 962 return (NULL); 963 } 964 965 /* 966 * bridge_delete_member: 967 * 968 * Delete the specified member interface. 969 */ 970 static void 971 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif, 972 int gone) 973 { 974 struct ifnet *ifs = bif->bif_ifp; 975 struct ifnet *bifp = sc->sc_ifp; 976 struct bridge_ifinfo *bif_info = bif->bif_info; 977 struct bridge_iflist_head saved_bifs; 978 979 ASSERT_IFNET_SERIALIZED_ALL(bifp); 980 KKASSERT(bif_info != NULL); 981 982 ifs->if_bridge = NULL; 983 984 /* 985 * Release bridge interface's serializer: 986 * - To avoid possible dead lock. 987 * - Various sync operation will block the current thread. 988 */ 989 ifnet_deserialize_all(bifp); 990 991 if (!gone) { 992 switch (ifs->if_type) { 993 case IFT_ETHER: 994 case IFT_L2VLAN: 995 /* 996 * Take the interface out of promiscuous mode. 997 */ 998 ifpromisc(ifs, 0); 999 bridge_mutecaps(bif_info, ifs, 0); 1000 break; 1001 1002 case IFT_GIF: 1003 break; 1004 1005 default: 1006 panic("bridge_delete_member: impossible"); 1007 break; 1008 } 1009 } 1010 1011 /* 1012 * Remove bifs from percpu linked list. 1013 * 1014 * Removed bifs are not freed immediately, instead, 1015 * they are saved in saved_bifs. They will be freed 1016 * after we make sure that no one is accessing them, 1017 * i.e. after following netmsg_service_sync() 1018 */ 1019 LIST_INIT(&saved_bifs); 1020 bridge_del_bif(sc, bif_info, &saved_bifs); 1021 1022 /* 1023 * Make sure that all protocol threads: 1024 * o see 'ifs' if_bridge is changed 1025 * o know that bif is removed from the percpu linked list 1026 */ 1027 netmsg_service_sync(); 1028 1029 /* 1030 * Free the removed bifs 1031 */ 1032 KKASSERT(!LIST_EMPTY(&saved_bifs)); 1033 while ((bif = LIST_FIRST(&saved_bifs)) != NULL) { 1034 LIST_REMOVE(bif, bif_next); 1035 kfree(bif, M_DEVBUF); 1036 } 1037 1038 /* See the comment in bridge_ioctl_stop() */ 1039 bridge_rtmsg_sync(sc); 1040 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL | IFBF_FLUSHSYNC); 1041 1042 ifnet_serialize_all(bifp); 1043 1044 if (bifp->if_flags & IFF_RUNNING) 1045 bstp_initialization(sc); 1046 1047 /* 1048 * Free the bif_info after bstp_initialization(), so that 1049 * bridge_softc.sc_root_port will not reference a dangling 1050 * pointer. 1051 */ 1052 kfree(bif_info, M_DEVBUF); 1053 } 1054 1055 /* 1056 * bridge_delete_span: 1057 * 1058 * Delete the specified span interface. 1059 */ 1060 static void 1061 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif) 1062 { 1063 KASSERT(bif->bif_ifp->if_bridge == NULL, 1064 ("%s: not a span interface", __func__)); 1065 1066 LIST_REMOVE(bif, bif_next); 1067 kfree(bif, M_DEVBUF); 1068 } 1069 1070 static int 1071 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused) 1072 { 1073 struct ifnet *ifp = sc->sc_ifp; 1074 1075 if (ifp->if_flags & IFF_RUNNING) 1076 return 0; 1077 1078 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz, 1079 bridge_timer, sc); 1080 1081 ifp->if_flags |= IFF_RUNNING; 1082 bstp_initialization(sc); 1083 return 0; 1084 } 1085 1086 static int 1087 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused) 1088 { 1089 struct ifnet *ifp = sc->sc_ifp; 1090 struct lwkt_msg *lmsg; 1091 1092 if ((ifp->if_flags & IFF_RUNNING) == 0) 1093 return 0; 1094 1095 callout_stop(&sc->sc_brcallout); 1096 1097 crit_enter(); 1098 lmsg = &sc->sc_brtimemsg.lmsg; 1099 if ((lmsg->ms_flags & MSGF_DONE) == 0) { 1100 /* Pending to be processed; drop it */ 1101 lwkt_dropmsg(lmsg); 1102 } 1103 crit_exit(); 1104 1105 bstp_stop(sc); 1106 1107 ifp->if_flags &= ~IFF_RUNNING; 1108 1109 ifnet_deserialize_all(ifp); 1110 1111 /* Let everyone know that we are stopped */ 1112 netmsg_service_sync(); 1113 1114 /* 1115 * Sync ifnetX msgports in the order we forward rtnode 1116 * installation message. This is used to make sure that 1117 * all rtnode installation messages sent by bridge_rtupdate() 1118 * during above netmsg_service_sync() are flushed. 1119 */ 1120 bridge_rtmsg_sync(sc); 1121 bridge_rtflush(sc, IFBF_FLUSHDYN | IFBF_FLUSHSYNC); 1122 1123 ifnet_serialize_all(ifp); 1124 return 0; 1125 } 1126 1127 static int 1128 bridge_ioctl_add(struct bridge_softc *sc, void *arg) 1129 { 1130 struct ifbreq *req = arg; 1131 struct bridge_iflist *bif; 1132 struct bridge_ifinfo *bif_info; 1133 struct ifnet *ifs, *bifp; 1134 int error = 0; 1135 1136 bifp = sc->sc_ifp; 1137 ASSERT_IFNET_SERIALIZED_ALL(bifp); 1138 1139 ifs = ifunit(req->ifbr_ifsname); 1140 if (ifs == NULL) 1141 return (ENOENT); 1142 1143 /* If it's in the span list, it can't be a member. */ 1144 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1145 if (ifs == bif->bif_ifp) 1146 return (EBUSY); 1147 1148 /* Allow the first Ethernet member to define the MTU */ 1149 if (ifs->if_type != IFT_GIF) { 1150 if (LIST_EMPTY(&sc->sc_iflists[mycpuid])) { 1151 bifp->if_mtu = ifs->if_mtu; 1152 } else if (bifp->if_mtu != ifs->if_mtu) { 1153 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname); 1154 return (EINVAL); 1155 } 1156 } 1157 1158 if (ifs->if_bridge == sc) 1159 return (EEXIST); 1160 1161 if (ifs->if_bridge != NULL) 1162 return (EBUSY); 1163 1164 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO); 1165 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY; 1166 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST; 1167 bif_info->bifi_ifp = ifs; 1168 1169 /* 1170 * Release bridge interface's serializer: 1171 * - To avoid possible dead lock. 1172 * - Various sync operation will block the current thread. 1173 */ 1174 ifnet_deserialize_all(bifp); 1175 1176 switch (ifs->if_type) { 1177 case IFT_ETHER: 1178 case IFT_L2VLAN: 1179 /* 1180 * Place the interface into promiscuous mode. 1181 */ 1182 error = ifpromisc(ifs, 1); 1183 if (error) { 1184 ifnet_serialize_all(bifp); 1185 goto out; 1186 } 1187 bridge_mutecaps(bif_info, ifs, 1); 1188 break; 1189 1190 case IFT_GIF: /* :^) */ 1191 break; 1192 1193 default: 1194 error = EINVAL; 1195 ifnet_serialize_all(bifp); 1196 goto out; 1197 } 1198 1199 /* 1200 * Add bifs to percpu linked lists 1201 */ 1202 bridge_add_bif(sc, bif_info, ifs); 1203 1204 ifnet_serialize_all(bifp); 1205 1206 if (bifp->if_flags & IFF_RUNNING) 1207 bstp_initialization(sc); 1208 else 1209 bstp_stop(sc); 1210 1211 /* 1212 * Everything has been setup, so let the member interface 1213 * deliver packets to this bridge on its input/output path. 1214 */ 1215 ifs->if_bridge = sc; 1216 out: 1217 if (error) { 1218 if (bif_info != NULL) 1219 kfree(bif_info, M_DEVBUF); 1220 } 1221 return (error); 1222 } 1223 1224 static int 1225 bridge_ioctl_del(struct bridge_softc *sc, void *arg) 1226 { 1227 struct ifbreq *req = arg; 1228 struct bridge_iflist *bif; 1229 1230 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1231 if (bif == NULL) 1232 return (ENOENT); 1233 1234 bridge_delete_member(sc, bif, 0); 1235 1236 return (0); 1237 } 1238 1239 static int 1240 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) 1241 { 1242 struct ifbreq *req = arg; 1243 struct bridge_iflist *bif; 1244 1245 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1246 if (bif == NULL) 1247 return (ENOENT); 1248 1249 req->ifbr_ifsflags = bif->bif_flags; 1250 req->ifbr_state = bif->bif_state; 1251 req->ifbr_priority = bif->bif_priority; 1252 req->ifbr_path_cost = bif->bif_path_cost; 1253 req->ifbr_portno = bif->bif_ifp->if_index & 0xff; 1254 1255 return (0); 1256 } 1257 1258 static int 1259 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) 1260 { 1261 struct ifbreq *req = arg; 1262 struct bridge_iflist *bif; 1263 struct ifnet *bifp = sc->sc_ifp; 1264 1265 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1266 if (bif == NULL) 1267 return (ENOENT); 1268 1269 if (req->ifbr_ifsflags & IFBIF_SPAN) { 1270 /* SPAN is readonly */ 1271 return (EINVAL); 1272 } 1273 1274 if (req->ifbr_ifsflags & IFBIF_STP) { 1275 switch (bif->bif_ifp->if_type) { 1276 case IFT_ETHER: 1277 /* These can do spanning tree. */ 1278 break; 1279 1280 default: 1281 /* Nothing else can. */ 1282 return (EINVAL); 1283 } 1284 } 1285 1286 ifnet_deserialize_all(bifp); 1287 bridge_set_bifflags(sc, bif->bif_info, req->ifbr_ifsflags); 1288 ifnet_serialize_all(bifp); 1289 1290 if (bifp->if_flags & IFF_RUNNING) 1291 bstp_initialization(sc); 1292 1293 return (0); 1294 } 1295 1296 static int 1297 bridge_ioctl_scache(struct bridge_softc *sc, void *arg) 1298 { 1299 struct ifbrparam *param = arg; 1300 struct ifnet *ifp = sc->sc_ifp; 1301 1302 sc->sc_brtmax = param->ifbrp_csize; 1303 1304 ifnet_deserialize_all(ifp); 1305 bridge_rttrim(sc); 1306 ifnet_serialize_all(ifp); 1307 1308 return (0); 1309 } 1310 1311 static int 1312 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) 1313 { 1314 struct ifbrparam *param = arg; 1315 1316 param->ifbrp_csize = sc->sc_brtmax; 1317 1318 return (0); 1319 } 1320 1321 static int 1322 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg) 1323 { 1324 struct bridge_control_arg *bc_arg = arg; 1325 struct ifbifconf *bifc = arg; 1326 struct bridge_iflist *bif; 1327 struct ifbreq *breq; 1328 int count, len; 1329 1330 count = 0; 1331 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) 1332 count++; 1333 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1334 count++; 1335 1336 if (bifc->ifbic_len == 0) { 1337 bifc->ifbic_len = sizeof(*breq) * count; 1338 return 0; 1339 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) { 1340 bifc->ifbic_len = 0; 1341 return 0; 1342 } 1343 1344 len = min(bifc->ifbic_len, sizeof(*breq) * count); 1345 KKASSERT(len >= sizeof(*breq)); 1346 1347 breq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO); 1348 if (breq == NULL) { 1349 bifc->ifbic_len = 0; 1350 return ENOMEM; 1351 } 1352 bc_arg->bca_kptr = breq; 1353 1354 count = 0; 1355 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 1356 if (len < sizeof(*breq)) 1357 break; 1358 1359 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname, 1360 sizeof(breq->ifbr_ifsname)); 1361 breq->ifbr_ifsflags = bif->bif_flags; 1362 breq->ifbr_state = bif->bif_state; 1363 breq->ifbr_priority = bif->bif_priority; 1364 breq->ifbr_path_cost = bif->bif_path_cost; 1365 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff; 1366 breq++; 1367 count++; 1368 len -= sizeof(*breq); 1369 } 1370 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 1371 if (len < sizeof(*breq)) 1372 break; 1373 1374 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname, 1375 sizeof(breq->ifbr_ifsname)); 1376 breq->ifbr_ifsflags = bif->bif_flags; 1377 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff; 1378 breq++; 1379 count++; 1380 len -= sizeof(*breq); 1381 } 1382 1383 bifc->ifbic_len = sizeof(*breq) * count; 1384 KKASSERT(bifc->ifbic_len > 0); 1385 1386 bc_arg->bca_len = bifc->ifbic_len; 1387 bc_arg->bca_uptr = bifc->ifbic_req; 1388 return 0; 1389 } 1390 1391 static int 1392 bridge_ioctl_rts(struct bridge_softc *sc, void *arg) 1393 { 1394 struct bridge_control_arg *bc_arg = arg; 1395 struct ifbaconf *bac = arg; 1396 struct bridge_rtnode *brt; 1397 struct ifbareq *bareq; 1398 int count, len; 1399 1400 count = 0; 1401 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) 1402 count++; 1403 1404 if (bac->ifbac_len == 0) { 1405 bac->ifbac_len = sizeof(*bareq) * count; 1406 return 0; 1407 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) { 1408 bac->ifbac_len = 0; 1409 return 0; 1410 } 1411 1412 len = min(bac->ifbac_len, sizeof(*bareq) * count); 1413 KKASSERT(len >= sizeof(*bareq)); 1414 1415 bareq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO); 1416 if (bareq == NULL) { 1417 bac->ifbac_len = 0; 1418 return ENOMEM; 1419 } 1420 bc_arg->bca_kptr = bareq; 1421 1422 count = 0; 1423 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) { 1424 struct bridge_rtinfo *bri = brt->brt_info; 1425 unsigned long expire; 1426 1427 if (len < sizeof(*bareq)) 1428 break; 1429 1430 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname, 1431 sizeof(bareq->ifba_ifsname)); 1432 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr)); 1433 expire = bri->bri_expire; 1434 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 1435 time_second < expire) 1436 bareq->ifba_expire = expire - time_second; 1437 else 1438 bareq->ifba_expire = 0; 1439 bareq->ifba_flags = bri->bri_flags; 1440 bareq++; 1441 count++; 1442 len -= sizeof(*bareq); 1443 } 1444 1445 bac->ifbac_len = sizeof(*bareq) * count; 1446 KKASSERT(bac->ifbac_len > 0); 1447 1448 bc_arg->bca_len = bac->ifbac_len; 1449 bc_arg->bca_uptr = bac->ifbac_req; 1450 return 0; 1451 } 1452 1453 static int 1454 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg) 1455 { 1456 struct ifbareq *req = arg; 1457 struct bridge_iflist *bif; 1458 struct ifnet *ifp = sc->sc_ifp; 1459 int error; 1460 1461 ASSERT_IFNET_SERIALIZED_ALL(ifp); 1462 1463 bif = bridge_lookup_member(sc, req->ifba_ifsname); 1464 if (bif == NULL) 1465 return (ENOENT); 1466 1467 ifnet_deserialize_all(ifp); 1468 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp, 1469 req->ifba_flags); 1470 ifnet_serialize_all(ifp); 1471 return (error); 1472 } 1473 1474 static int 1475 bridge_ioctl_sto(struct bridge_softc *sc, void *arg) 1476 { 1477 struct ifbrparam *param = arg; 1478 1479 sc->sc_brttimeout = param->ifbrp_ctime; 1480 1481 return (0); 1482 } 1483 1484 static int 1485 bridge_ioctl_gto(struct bridge_softc *sc, void *arg) 1486 { 1487 struct ifbrparam *param = arg; 1488 1489 param->ifbrp_ctime = sc->sc_brttimeout; 1490 1491 return (0); 1492 } 1493 1494 static int 1495 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg) 1496 { 1497 struct ifbareq *req = arg; 1498 struct ifnet *ifp = sc->sc_ifp; 1499 int error; 1500 1501 ifnet_deserialize_all(ifp); 1502 error = bridge_rtdaddr(sc, req->ifba_dst); 1503 ifnet_serialize_all(ifp); 1504 return error; 1505 } 1506 1507 static int 1508 bridge_ioctl_flush(struct bridge_softc *sc, void *arg) 1509 { 1510 struct ifbreq *req = arg; 1511 struct ifnet *ifp = sc->sc_ifp; 1512 1513 ifnet_deserialize_all(ifp); 1514 bridge_rtflush(sc, req->ifbr_ifsflags | IFBF_FLUSHSYNC); 1515 ifnet_serialize_all(ifp); 1516 1517 return (0); 1518 } 1519 1520 static int 1521 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) 1522 { 1523 struct ifbrparam *param = arg; 1524 1525 param->ifbrp_prio = sc->sc_bridge_priority; 1526 1527 return (0); 1528 } 1529 1530 static int 1531 bridge_ioctl_spri(struct bridge_softc *sc, void *arg) 1532 { 1533 struct ifbrparam *param = arg; 1534 1535 sc->sc_bridge_priority = param->ifbrp_prio; 1536 1537 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1538 bstp_initialization(sc); 1539 1540 return (0); 1541 } 1542 1543 static int 1544 bridge_ioctl_ght(struct bridge_softc *sc, void *arg) 1545 { 1546 struct ifbrparam *param = arg; 1547 1548 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8; 1549 1550 return (0); 1551 } 1552 1553 static int 1554 bridge_ioctl_sht(struct bridge_softc *sc, void *arg) 1555 { 1556 struct ifbrparam *param = arg; 1557 1558 if (param->ifbrp_hellotime == 0) 1559 return (EINVAL); 1560 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8; 1561 1562 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1563 bstp_initialization(sc); 1564 1565 return (0); 1566 } 1567 1568 static int 1569 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) 1570 { 1571 struct ifbrparam *param = arg; 1572 1573 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8; 1574 1575 return (0); 1576 } 1577 1578 static int 1579 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) 1580 { 1581 struct ifbrparam *param = arg; 1582 1583 if (param->ifbrp_fwddelay == 0) 1584 return (EINVAL); 1585 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8; 1586 1587 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1588 bstp_initialization(sc); 1589 1590 return (0); 1591 } 1592 1593 static int 1594 bridge_ioctl_gma(struct bridge_softc *sc, void *arg) 1595 { 1596 struct ifbrparam *param = arg; 1597 1598 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8; 1599 1600 return (0); 1601 } 1602 1603 static int 1604 bridge_ioctl_sma(struct bridge_softc *sc, void *arg) 1605 { 1606 struct ifbrparam *param = arg; 1607 1608 if (param->ifbrp_maxage == 0) 1609 return (EINVAL); 1610 sc->sc_bridge_max_age = param->ifbrp_maxage << 8; 1611 1612 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1613 bstp_initialization(sc); 1614 1615 return (0); 1616 } 1617 1618 static int 1619 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) 1620 { 1621 struct ifbreq *req = arg; 1622 struct bridge_iflist *bif; 1623 1624 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1625 if (bif == NULL) 1626 return (ENOENT); 1627 1628 bif->bif_priority = req->ifbr_priority; 1629 1630 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1631 bstp_initialization(sc); 1632 1633 return (0); 1634 } 1635 1636 static int 1637 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) 1638 { 1639 struct ifbreq *req = arg; 1640 struct bridge_iflist *bif; 1641 1642 bif = bridge_lookup_member(sc, req->ifbr_ifsname); 1643 if (bif == NULL) 1644 return (ENOENT); 1645 1646 bif->bif_path_cost = req->ifbr_path_cost; 1647 1648 if (sc->sc_ifp->if_flags & IFF_RUNNING) 1649 bstp_initialization(sc); 1650 1651 return (0); 1652 } 1653 1654 static int 1655 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg) 1656 { 1657 struct ifbreq *req = arg; 1658 struct bridge_iflist *bif; 1659 struct ifnet *ifs; 1660 1661 ifs = ifunit(req->ifbr_ifsname); 1662 if (ifs == NULL) 1663 return (ENOENT); 1664 1665 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1666 if (ifs == bif->bif_ifp) 1667 return (EBUSY); 1668 1669 if (ifs->if_bridge != NULL) 1670 return (EBUSY); 1671 1672 switch (ifs->if_type) { 1673 case IFT_ETHER: 1674 case IFT_GIF: 1675 case IFT_L2VLAN: 1676 break; 1677 1678 default: 1679 return (EINVAL); 1680 } 1681 1682 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO); 1683 bif->bif_ifp = ifs; 1684 bif->bif_flags = IFBIF_SPAN; 1685 /* NOTE: span bif does not need bridge_ifinfo */ 1686 1687 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next); 1688 1689 sc->sc_span = 1; 1690 1691 return (0); 1692 } 1693 1694 static int 1695 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg) 1696 { 1697 struct ifbreq *req = arg; 1698 struct bridge_iflist *bif; 1699 struct ifnet *ifs; 1700 1701 ifs = ifunit(req->ifbr_ifsname); 1702 if (ifs == NULL) 1703 return (ENOENT); 1704 1705 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1706 if (ifs == bif->bif_ifp) 1707 break; 1708 1709 if (bif == NULL) 1710 return (ENOENT); 1711 1712 bridge_delete_span(sc, bif); 1713 1714 if (LIST_EMPTY(&sc->sc_spanlist)) 1715 sc->sc_span = 0; 1716 1717 return (0); 1718 } 1719 1720 static void 1721 bridge_ifdetach_dispatch(netmsg_t msg) 1722 { 1723 struct ifnet *ifp, *bifp; 1724 struct bridge_softc *sc; 1725 struct bridge_iflist *bif; 1726 1727 ifp = msg->lmsg.u.ms_resultp; 1728 sc = ifp->if_bridge; 1729 1730 /* Check if the interface is a bridge member */ 1731 if (sc != NULL) { 1732 bifp = sc->sc_ifp; 1733 1734 ifnet_serialize_all(bifp); 1735 1736 bif = bridge_lookup_member_if(sc, ifp); 1737 if (bif != NULL) { 1738 bridge_delete_member(sc, bif, 1); 1739 } else { 1740 /* XXX Why bif will be NULL? */ 1741 } 1742 1743 ifnet_deserialize_all(bifp); 1744 goto reply; 1745 } 1746 1747 crit_enter(); /* XXX MP */ 1748 1749 /* Check if the interface is a span port */ 1750 LIST_FOREACH(sc, &bridge_list, sc_list) { 1751 bifp = sc->sc_ifp; 1752 1753 ifnet_serialize_all(bifp); 1754 1755 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) 1756 if (ifp == bif->bif_ifp) { 1757 bridge_delete_span(sc, bif); 1758 break; 1759 } 1760 1761 ifnet_deserialize_all(bifp); 1762 } 1763 1764 crit_exit(); 1765 1766 reply: 1767 lwkt_replymsg(&msg->lmsg, 0); 1768 } 1769 1770 /* 1771 * bridge_ifdetach: 1772 * 1773 * Detach an interface from a bridge. Called when a member 1774 * interface is detaching. 1775 */ 1776 static void 1777 bridge_ifdetach(void *arg __unused, struct ifnet *ifp) 1778 { 1779 struct netmsg_base msg; 1780 1781 netmsg_init(&msg, NULL, &curthread->td_msgport, 1782 0, bridge_ifdetach_dispatch); 1783 msg.lmsg.u.ms_resultp = ifp; 1784 1785 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0); 1786 } 1787 1788 /* 1789 * bridge_init: 1790 * 1791 * Initialize a bridge interface. 1792 */ 1793 static void 1794 bridge_init(void *xsc) 1795 { 1796 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL); 1797 } 1798 1799 /* 1800 * bridge_stop: 1801 * 1802 * Stop the bridge interface. 1803 */ 1804 static void 1805 bridge_stop(struct ifnet *ifp) 1806 { 1807 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL); 1808 } 1809 1810 /* 1811 * bridge_enqueue: 1812 * 1813 * Enqueue a packet on a bridge member interface. 1814 * 1815 */ 1816 void 1817 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m) 1818 { 1819 struct netmsg_packet *nmp; 1820 1821 nmp = &m->m_hdr.mh_netmsg; 1822 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport, 1823 0, bridge_enqueue_handler); 1824 nmp->nm_packet = m; 1825 nmp->base.lmsg.u.ms_resultp = dst_ifp; 1826 1827 lwkt_sendmsg(ifnet_portfn(mycpu->gd_cpuid), &nmp->base.lmsg); 1828 } 1829 1830 /* 1831 * bridge_output: 1832 * 1833 * Send output from a bridge member interface. This 1834 * performs the bridging function for locally originated 1835 * packets. 1836 * 1837 * The mbuf has the Ethernet header already attached. We must 1838 * enqueue or free the mbuf before returning. 1839 */ 1840 static int 1841 bridge_output(struct ifnet *ifp, struct mbuf *m) 1842 { 1843 struct bridge_softc *sc = ifp->if_bridge; 1844 struct ether_header *eh; 1845 struct ifnet *dst_if, *bifp; 1846 1847 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 1848 1849 /* 1850 * Make sure that we are still a member of a bridge interface. 1851 */ 1852 if (sc == NULL) { 1853 m_freem(m); 1854 return (0); 1855 } 1856 bifp = sc->sc_ifp; 1857 1858 if (m->m_len < ETHER_HDR_LEN) { 1859 m = m_pullup(m, ETHER_HDR_LEN); 1860 if (m == NULL) 1861 return (0); 1862 } 1863 eh = mtod(m, struct ether_header *); 1864 1865 /* 1866 * If bridge is down, but the original output interface is up, 1867 * go ahead and send out that interface. Otherwise, the packet 1868 * is dropped below. 1869 */ 1870 if ((bifp->if_flags & IFF_RUNNING) == 0) { 1871 dst_if = ifp; 1872 goto sendunicast; 1873 } 1874 1875 /* 1876 * If the packet is a multicast, or we don't know a better way to 1877 * get there, send to all interfaces. 1878 */ 1879 if (ETHER_IS_MULTICAST(eh->ether_dhost)) 1880 dst_if = NULL; 1881 else 1882 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1883 if (dst_if == NULL) { 1884 struct bridge_iflist *bif, *nbif; 1885 struct mbuf *mc; 1886 int used = 0; 1887 1888 if (sc->sc_span) 1889 bridge_span(sc, m); 1890 1891 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], 1892 bif_next, nbif) { 1893 dst_if = bif->bif_ifp; 1894 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1895 continue; 1896 1897 /* 1898 * If this is not the original output interface, 1899 * and the interface is participating in spanning 1900 * tree, make sure the port is in a state that 1901 * allows forwarding. 1902 */ 1903 if (dst_if != ifp && 1904 (bif->bif_flags & IFBIF_STP) != 0) { 1905 switch (bif->bif_state) { 1906 case BSTP_IFSTATE_BLOCKING: 1907 case BSTP_IFSTATE_LISTENING: 1908 case BSTP_IFSTATE_DISABLED: 1909 continue; 1910 } 1911 } 1912 1913 if (LIST_NEXT(bif, bif_next) == NULL) { 1914 used = 1; 1915 mc = m; 1916 } else { 1917 mc = m_copypacket(m, MB_DONTWAIT); 1918 if (mc == NULL) { 1919 bifp->if_oerrors++; 1920 continue; 1921 } 1922 } 1923 bridge_handoff(dst_if, mc); 1924 1925 if (nbif != NULL && !nbif->bif_onlist) { 1926 KKASSERT(bif->bif_onlist); 1927 nbif = LIST_NEXT(bif, bif_next); 1928 } 1929 } 1930 if (used == 0) 1931 m_freem(m); 1932 return (0); 1933 } 1934 1935 sendunicast: 1936 /* 1937 * XXX Spanning tree consideration here? 1938 */ 1939 if (sc->sc_span) 1940 bridge_span(sc, m); 1941 if ((dst_if->if_flags & IFF_RUNNING) == 0) 1942 m_freem(m); 1943 else 1944 bridge_handoff(dst_if, m); 1945 return (0); 1946 } 1947 1948 /* 1949 * bridge_start: 1950 * 1951 * Start output on a bridge. 1952 * 1953 */ 1954 static void 1955 bridge_start(struct ifnet *ifp) 1956 { 1957 struct bridge_softc *sc = ifp->if_softc; 1958 1959 ASSERT_IFNET_SERIALIZED_TX(ifp); 1960 1961 ifp->if_flags |= IFF_OACTIVE; 1962 for (;;) { 1963 struct ifnet *dst_if = NULL; 1964 struct ether_header *eh; 1965 struct mbuf *m; 1966 1967 m = ifq_dequeue(&ifp->if_snd, NULL); 1968 if (m == NULL) 1969 break; 1970 1971 if (m->m_len < sizeof(*eh)) { 1972 m = m_pullup(m, sizeof(*eh)); 1973 if (m == NULL) { 1974 ifp->if_oerrors++; 1975 continue; 1976 } 1977 } 1978 eh = mtod(m, struct ether_header *); 1979 1980 BPF_MTAP(ifp, m); 1981 ifp->if_opackets++; 1982 1983 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) 1984 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 1985 1986 if (dst_if == NULL) 1987 bridge_start_bcast(sc, m); 1988 else 1989 bridge_enqueue(dst_if, m); 1990 } 1991 ifp->if_flags &= ~IFF_OACTIVE; 1992 } 1993 1994 /* 1995 * bridge_forward: 1996 * 1997 * The forwarding function of the bridge. 1998 */ 1999 static void 2000 bridge_forward(struct bridge_softc *sc, struct mbuf *m) 2001 { 2002 struct bridge_iflist *bif; 2003 struct ifnet *src_if, *dst_if, *ifp; 2004 struct ether_header *eh; 2005 2006 src_if = m->m_pkthdr.rcvif; 2007 ifp = sc->sc_ifp; 2008 2009 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 2010 2011 ifp->if_ipackets++; 2012 ifp->if_ibytes += m->m_pkthdr.len; 2013 2014 /* 2015 * Look up the bridge_iflist. 2016 */ 2017 bif = bridge_lookup_member_if(sc, src_if); 2018 if (bif == NULL) { 2019 /* Interface is not a bridge member (anymore?) */ 2020 m_freem(m); 2021 return; 2022 } 2023 2024 if (bif->bif_flags & IFBIF_STP) { 2025 switch (bif->bif_state) { 2026 case BSTP_IFSTATE_BLOCKING: 2027 case BSTP_IFSTATE_LISTENING: 2028 case BSTP_IFSTATE_DISABLED: 2029 m_freem(m); 2030 return; 2031 } 2032 } 2033 2034 eh = mtod(m, struct ether_header *); 2035 2036 /* 2037 * If the interface is learning, and the source 2038 * address is valid and not multicast, record 2039 * the address. 2040 */ 2041 if ((bif->bif_flags & IFBIF_LEARNING) != 0 && 2042 ETHER_IS_MULTICAST(eh->ether_shost) == 0 && 2043 (eh->ether_shost[0] == 0 && 2044 eh->ether_shost[1] == 0 && 2045 eh->ether_shost[2] == 0 && 2046 eh->ether_shost[3] == 0 && 2047 eh->ether_shost[4] == 0 && 2048 eh->ether_shost[5] == 0) == 0) 2049 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC); 2050 2051 if ((bif->bif_flags & IFBIF_STP) != 0 && 2052 bif->bif_state == BSTP_IFSTATE_LEARNING) { 2053 m_freem(m); 2054 return; 2055 } 2056 2057 /* 2058 * At this point, the port either doesn't participate 2059 * in spanning tree or it is in the forwarding state. 2060 */ 2061 2062 /* 2063 * If the packet is unicast, destined for someone on 2064 * "this" side of the bridge, drop it. 2065 */ 2066 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) { 2067 dst_if = bridge_rtlookup(sc, eh->ether_dhost); 2068 if (src_if == dst_if) { 2069 m_freem(m); 2070 return; 2071 } 2072 } else { 2073 /* ...forward it to all interfaces. */ 2074 ifp->if_imcasts++; 2075 dst_if = NULL; 2076 } 2077 2078 if (dst_if == NULL) { 2079 bridge_broadcast(sc, src_if, m); 2080 return; 2081 } 2082 2083 /* 2084 * At this point, we're dealing with a unicast frame 2085 * going to a different interface. 2086 */ 2087 if ((dst_if->if_flags & IFF_RUNNING) == 0) { 2088 m_freem(m); 2089 return; 2090 } 2091 bif = bridge_lookup_member_if(sc, dst_if); 2092 if (bif == NULL) { 2093 /* Not a member of the bridge (anymore?) */ 2094 m_freem(m); 2095 return; 2096 } 2097 2098 if (bif->bif_flags & IFBIF_STP) { 2099 switch (bif->bif_state) { 2100 case BSTP_IFSTATE_DISABLED: 2101 case BSTP_IFSTATE_BLOCKING: 2102 m_freem(m); 2103 return; 2104 } 2105 } 2106 2107 if (inet_pfil_hook.ph_hashooks > 0 2108 #ifdef INET6 2109 || inet6_pfil_hook.ph_hashooks > 0 2110 #endif 2111 ) { 2112 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0) 2113 return; 2114 if (m == NULL) 2115 return; 2116 2117 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0) 2118 return; 2119 if (m == NULL) 2120 return; 2121 } 2122 bridge_handoff(dst_if, m); 2123 } 2124 2125 /* 2126 * bridge_input: 2127 * 2128 * Receive input from a member interface. Queue the packet for 2129 * bridging if it is not for us. 2130 */ 2131 static struct mbuf * 2132 bridge_input(struct ifnet *ifp, struct mbuf *m) 2133 { 2134 struct bridge_softc *sc = ifp->if_bridge; 2135 struct bridge_iflist *bif; 2136 struct ifnet *bifp, *new_ifp; 2137 struct ether_header *eh; 2138 struct mbuf *mc, *mc2; 2139 2140 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 2141 2142 /* 2143 * Make sure that we are still a member of a bridge interface. 2144 */ 2145 if (sc == NULL) 2146 return m; 2147 2148 new_ifp = NULL; 2149 bifp = sc->sc_ifp; 2150 2151 if ((bifp->if_flags & IFF_RUNNING) == 0) 2152 goto out; 2153 2154 /* 2155 * Implement support for bridge monitoring. If this flag has been 2156 * set on this interface, discard the packet once we push it through 2157 * the bpf(4) machinery, but before we do, increment various counters 2158 * associated with this bridge. 2159 */ 2160 if (bifp->if_flags & IFF_MONITOR) { 2161 /* Change input interface to this bridge */ 2162 m->m_pkthdr.rcvif = bifp; 2163 2164 BPF_MTAP(bifp, m); 2165 2166 /* Update bridge's ifnet statistics */ 2167 bifp->if_ipackets++; 2168 bifp->if_ibytes += m->m_pkthdr.len; 2169 if (m->m_flags & (M_MCAST | M_BCAST)) 2170 bifp->if_imcasts++; 2171 2172 m_freem(m); 2173 m = NULL; 2174 goto out; 2175 } 2176 2177 eh = mtod(m, struct ether_header *); 2178 2179 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) { 2180 /* 2181 * If the packet is for us, set the packets source as the 2182 * bridge, and return the packet back to ifnet.if_input for 2183 * local processing. 2184 */ 2185 KASSERT(bifp->if_bridge == NULL, 2186 ("loop created in bridge_input")); 2187 if (pfil_member != 0) { 2188 if (inet_pfil_hook.ph_hashooks > 0 2189 #ifdef INET6 2190 || inet6_pfil_hook.ph_hashooks > 0 2191 #endif 2192 ) { 2193 if (bridge_pfil(&m, NULL, ifp, PFIL_IN) != 0) 2194 goto out; 2195 if (m == NULL) 2196 goto out; 2197 } 2198 } 2199 new_ifp = bifp; 2200 goto out; 2201 } 2202 2203 /* 2204 * Tap all packets arriving on the bridge, no matter if 2205 * they are local destinations or not. In is in. 2206 */ 2207 BPF_MTAP(bifp, m); 2208 2209 bif = bridge_lookup_member_if(sc, ifp); 2210 if (bif == NULL) 2211 goto out; 2212 2213 if (sc->sc_span) 2214 bridge_span(sc, m); 2215 2216 if (m->m_flags & (M_BCAST | M_MCAST)) { 2217 /* Tap off 802.1D packets; they do not get forwarded. */ 2218 if (memcmp(eh->ether_dhost, bstp_etheraddr, 2219 ETHER_ADDR_LEN) == 0) { 2220 ifnet_serialize_all(bifp); 2221 bstp_input(sc, bif, m); 2222 ifnet_deserialize_all(bifp); 2223 2224 /* m is freed by bstp_input */ 2225 m = NULL; 2226 goto out; 2227 } 2228 2229 if (bif->bif_flags & IFBIF_STP) { 2230 switch (bif->bif_state) { 2231 case BSTP_IFSTATE_BLOCKING: 2232 case BSTP_IFSTATE_LISTENING: 2233 case BSTP_IFSTATE_DISABLED: 2234 goto out; 2235 } 2236 } 2237 2238 /* 2239 * Make a deep copy of the packet and enqueue the copy 2240 * for bridge processing; return the original packet for 2241 * local processing. 2242 */ 2243 mc = m_dup(m, MB_DONTWAIT); 2244 if (mc == NULL) 2245 goto out; 2246 2247 bridge_forward(sc, mc); 2248 2249 /* 2250 * Reinject the mbuf as arriving on the bridge so we have a 2251 * chance at claiming multicast packets. We can not loop back 2252 * here from ether_input as a bridge is never a member of a 2253 * bridge. 2254 */ 2255 KASSERT(bifp->if_bridge == NULL, 2256 ("loop created in bridge_input")); 2257 mc2 = m_dup(m, MB_DONTWAIT); 2258 #ifdef notyet 2259 if (mc2 != NULL) { 2260 /* Keep the layer3 header aligned */ 2261 int i = min(mc2->m_pkthdr.len, max_protohdr); 2262 mc2 = m_copyup(mc2, i, ETHER_ALIGN); 2263 } 2264 #endif 2265 if (mc2 != NULL) { 2266 /* 2267 * Don't tap to bpf(4) again; we have 2268 * already done the tapping. 2269 */ 2270 ether_reinput_oncpu(bifp, mc2, 0); 2271 } 2272 2273 /* Return the original packet for local processing. */ 2274 goto out; 2275 } 2276 2277 if (bif->bif_flags & IFBIF_STP) { 2278 switch (bif->bif_state) { 2279 case BSTP_IFSTATE_BLOCKING: 2280 case BSTP_IFSTATE_LISTENING: 2281 case BSTP_IFSTATE_DISABLED: 2282 goto out; 2283 } 2284 } 2285 2286 /* 2287 * Unicast. Make sure it's not for us. 2288 * 2289 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate) 2290 * is followed by breaking out of the loop. 2291 */ 2292 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 2293 if (bif->bif_ifp->if_type != IFT_ETHER) 2294 continue; 2295 2296 /* It is destined for us. */ 2297 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost, 2298 ETHER_ADDR_LEN) == 0) { 2299 if (bif->bif_ifp != ifp) { 2300 /* XXX loop prevention */ 2301 m->m_flags |= M_ETHER_BRIDGED; 2302 new_ifp = bif->bif_ifp; 2303 } 2304 if (bif->bif_flags & IFBIF_LEARNING) { 2305 bridge_rtupdate(sc, eh->ether_shost, 2306 ifp, IFBAF_DYNAMIC); 2307 } 2308 goto out; 2309 } 2310 2311 /* We just received a packet that we sent out. */ 2312 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost, 2313 ETHER_ADDR_LEN) == 0) { 2314 m_freem(m); 2315 m = NULL; 2316 goto out; 2317 } 2318 } 2319 2320 /* Perform the bridge forwarding function. */ 2321 bridge_forward(sc, m); 2322 m = NULL; 2323 out: 2324 if (new_ifp != NULL) { 2325 ether_reinput_oncpu(new_ifp, m, 1); 2326 m = NULL; 2327 } 2328 return (m); 2329 } 2330 2331 /* 2332 * bridge_start_bcast: 2333 * 2334 * Broadcast the packet sent from bridge to all member 2335 * interfaces. 2336 * This is a simplified version of bridge_broadcast(), however, 2337 * this function expects caller to hold bridge's serializer. 2338 */ 2339 static void 2340 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m) 2341 { 2342 struct bridge_iflist *bif; 2343 struct mbuf *mc; 2344 struct ifnet *dst_if, *bifp; 2345 int used = 0; 2346 2347 bifp = sc->sc_ifp; 2348 ASSERT_IFNET_SERIALIZED_ALL(bifp); 2349 2350 /* 2351 * Following loop is MPSAFE; nothing is blocking 2352 * in the loop body. 2353 */ 2354 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { 2355 dst_if = bif->bif_ifp; 2356 2357 if (bif->bif_flags & IFBIF_STP) { 2358 switch (bif->bif_state) { 2359 case BSTP_IFSTATE_BLOCKING: 2360 case BSTP_IFSTATE_DISABLED: 2361 continue; 2362 } 2363 } 2364 2365 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && 2366 (m->m_flags & (M_BCAST|M_MCAST)) == 0) 2367 continue; 2368 2369 if ((dst_if->if_flags & IFF_RUNNING) == 0) 2370 continue; 2371 2372 if (LIST_NEXT(bif, bif_next) == NULL) { 2373 mc = m; 2374 used = 1; 2375 } else { 2376 mc = m_copypacket(m, MB_DONTWAIT); 2377 if (mc == NULL) { 2378 bifp->if_oerrors++; 2379 continue; 2380 } 2381 } 2382 bridge_enqueue(dst_if, mc); 2383 } 2384 if (used == 0) 2385 m_freem(m); 2386 } 2387 2388 /* 2389 * bridge_broadcast: 2390 * 2391 * Send a frame to all interfaces that are members of 2392 * the bridge, except for the one on which the packet 2393 * arrived. 2394 */ 2395 static void 2396 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if, 2397 struct mbuf *m) 2398 { 2399 struct bridge_iflist *bif, *nbif; 2400 struct mbuf *mc; 2401 struct ifnet *dst_if, *bifp; 2402 int used = 0; 2403 2404 bifp = sc->sc_ifp; 2405 ASSERT_IFNET_NOT_SERIALIZED_ALL(bifp); 2406 2407 if (inet_pfil_hook.ph_hashooks > 0 2408 #ifdef INET6 2409 || inet6_pfil_hook.ph_hashooks > 0 2410 #endif 2411 ) { 2412 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0) 2413 return; 2414 if (m == NULL) 2415 return; 2416 2417 /* Filter on the bridge interface before broadcasting */ 2418 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0) 2419 return; 2420 if (m == NULL) 2421 return; 2422 } 2423 2424 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { 2425 dst_if = bif->bif_ifp; 2426 if (dst_if == src_if) 2427 continue; 2428 2429 if (bif->bif_flags & IFBIF_STP) { 2430 switch (bif->bif_state) { 2431 case BSTP_IFSTATE_BLOCKING: 2432 case BSTP_IFSTATE_DISABLED: 2433 continue; 2434 } 2435 } 2436 2437 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && 2438 (m->m_flags & (M_BCAST|M_MCAST)) == 0) 2439 continue; 2440 2441 if ((dst_if->if_flags & IFF_RUNNING) == 0) 2442 continue; 2443 2444 if (LIST_NEXT(bif, bif_next) == NULL) { 2445 mc = m; 2446 used = 1; 2447 } else { 2448 mc = m_copypacket(m, MB_DONTWAIT); 2449 if (mc == NULL) { 2450 sc->sc_ifp->if_oerrors++; 2451 continue; 2452 } 2453 } 2454 2455 /* 2456 * Filter on the output interface. Pass a NULL bridge 2457 * interface pointer so we do not redundantly filter on 2458 * the bridge for each interface we broadcast on. 2459 */ 2460 if (inet_pfil_hook.ph_hashooks > 0 2461 #ifdef INET6 2462 || inet6_pfil_hook.ph_hashooks > 0 2463 #endif 2464 ) { 2465 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0) 2466 continue; 2467 if (mc == NULL) 2468 continue; 2469 } 2470 bridge_handoff(dst_if, mc); 2471 2472 if (nbif != NULL && !nbif->bif_onlist) { 2473 KKASSERT(bif->bif_onlist); 2474 nbif = LIST_NEXT(bif, bif_next); 2475 } 2476 } 2477 if (used == 0) 2478 m_freem(m); 2479 } 2480 2481 /* 2482 * bridge_span: 2483 * 2484 * Duplicate a packet out one or more interfaces that are in span mode, 2485 * the original mbuf is unmodified. 2486 */ 2487 static void 2488 bridge_span(struct bridge_softc *sc, struct mbuf *m) 2489 { 2490 struct bridge_iflist *bif; 2491 struct ifnet *dst_if, *bifp; 2492 struct mbuf *mc; 2493 2494 bifp = sc->sc_ifp; 2495 ifnet_serialize_all(bifp); 2496 2497 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) { 2498 dst_if = bif->bif_ifp; 2499 2500 if ((dst_if->if_flags & IFF_RUNNING) == 0) 2501 continue; 2502 2503 mc = m_copypacket(m, MB_DONTWAIT); 2504 if (mc == NULL) { 2505 sc->sc_ifp->if_oerrors++; 2506 continue; 2507 } 2508 bridge_enqueue(dst_if, mc); 2509 } 2510 2511 ifnet_deserialize_all(bifp); 2512 } 2513 2514 static void 2515 bridge_rtmsg_sync_handler(netmsg_t msg) 2516 { 2517 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1); 2518 } 2519 2520 static void 2521 bridge_rtmsg_sync(struct bridge_softc *sc) 2522 { 2523 struct netmsg_base msg; 2524 2525 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2526 2527 netmsg_init(&msg, NULL, &curthread->td_msgport, 2528 0, bridge_rtmsg_sync_handler); 2529 ifnet_domsg(&msg.lmsg, 0); 2530 } 2531 2532 static __inline void 2533 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if, 2534 int setflags, uint8_t flags, uint32_t timeo) 2535 { 2536 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 2537 bri->bri_ifp != dst_if) 2538 bri->bri_ifp = dst_if; 2539 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 2540 bri->bri_expire != time_second + timeo) 2541 bri->bri_expire = time_second + timeo; 2542 if (setflags) 2543 bri->bri_flags = flags; 2544 } 2545 2546 static int 2547 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst, 2548 struct ifnet *dst_if, int setflags, uint8_t flags, 2549 struct bridge_rtinfo **bri0) 2550 { 2551 struct bridge_rtnode *brt; 2552 struct bridge_rtinfo *bri; 2553 2554 if (mycpuid == 0) { 2555 brt = bridge_rtnode_lookup(sc, dst); 2556 if (brt != NULL) { 2557 /* 2558 * rtnode for 'dst' already exists. We inform the 2559 * caller about this by leaving bri0 as NULL. The 2560 * caller will terminate the intallation upon getting 2561 * NULL bri0. However, we still need to update the 2562 * rtinfo. 2563 */ 2564 KKASSERT(*bri0 == NULL); 2565 2566 /* Update rtinfo */ 2567 bridge_rtinfo_update(brt->brt_info, dst_if, setflags, 2568 flags, sc->sc_brttimeout); 2569 return 0; 2570 } 2571 2572 /* 2573 * We only need to check brtcnt on CPU0, since if limit 2574 * is to be exceeded, ENOSPC is returned. Caller knows 2575 * this and will terminate the installation. 2576 */ 2577 if (sc->sc_brtcnt >= sc->sc_brtmax) 2578 return ENOSPC; 2579 2580 KKASSERT(*bri0 == NULL); 2581 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF, 2582 M_WAITOK | M_ZERO); 2583 *bri0 = bri; 2584 2585 /* Setup rtinfo */ 2586 bri->bri_flags = IFBAF_DYNAMIC; 2587 bridge_rtinfo_update(bri, dst_if, setflags, flags, 2588 sc->sc_brttimeout); 2589 } else { 2590 bri = *bri0; 2591 KKASSERT(bri != NULL); 2592 } 2593 2594 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF, 2595 M_WAITOK | M_ZERO); 2596 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); 2597 brt->brt_info = bri; 2598 2599 bridge_rtnode_insert(sc, brt); 2600 return 0; 2601 } 2602 2603 static void 2604 bridge_rtinstall_handler(netmsg_t msg) 2605 { 2606 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)msg; 2607 int error; 2608 2609 error = bridge_rtinstall_oncpu(brmsg->br_softc, 2610 brmsg->br_dst, brmsg->br_dst_if, 2611 brmsg->br_setflags, brmsg->br_flags, 2612 &brmsg->br_rtinfo); 2613 if (error) { 2614 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL); 2615 lwkt_replymsg(&brmsg->base.lmsg, error); 2616 return; 2617 } else if (brmsg->br_rtinfo == NULL) { 2618 /* rtnode already exists for 'dst' */ 2619 KKASSERT(mycpuid == 0); 2620 lwkt_replymsg(&brmsg->base.lmsg, 0); 2621 return; 2622 } 2623 ifnet_forwardmsg(&brmsg->base.lmsg, mycpuid + 1); 2624 } 2625 2626 /* 2627 * bridge_rtupdate: 2628 * 2629 * Add/Update a bridge routing entry. 2630 */ 2631 static int 2632 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, 2633 struct ifnet *dst_if, uint8_t flags) 2634 { 2635 struct bridge_rtnode *brt; 2636 2637 /* 2638 * A route for this destination might already exist. If so, 2639 * update it, otherwise create a new one. 2640 */ 2641 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) { 2642 struct netmsg_brsaddr *brmsg; 2643 2644 if (sc->sc_brtcnt >= sc->sc_brtmax) 2645 return ENOSPC; 2646 2647 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK); 2648 if (brmsg == NULL) 2649 return ENOMEM; 2650 2651 netmsg_init(&brmsg->base, NULL, &netisr_afree_rport, 2652 0, bridge_rtinstall_handler); 2653 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN); 2654 brmsg->br_dst_if = dst_if; 2655 brmsg->br_flags = flags; 2656 brmsg->br_setflags = 0; 2657 brmsg->br_softc = sc; 2658 brmsg->br_rtinfo = NULL; 2659 2660 ifnet_sendmsg(&brmsg->base.lmsg, 0); 2661 return 0; 2662 } 2663 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags, 2664 sc->sc_brttimeout); 2665 return 0; 2666 } 2667 2668 static int 2669 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst, 2670 struct ifnet *dst_if, uint8_t flags) 2671 { 2672 struct netmsg_brsaddr brmsg; 2673 2674 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2675 2676 netmsg_init(&brmsg.base, NULL, &curthread->td_msgport, 2677 0, bridge_rtinstall_handler); 2678 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN); 2679 brmsg.br_dst_if = dst_if; 2680 brmsg.br_flags = flags; 2681 brmsg.br_setflags = 1; 2682 brmsg.br_softc = sc; 2683 brmsg.br_rtinfo = NULL; 2684 2685 return ifnet_domsg(&brmsg.base.lmsg, 0); 2686 } 2687 2688 /* 2689 * bridge_rtlookup: 2690 * 2691 * Lookup the destination interface for an address. 2692 */ 2693 static struct ifnet * 2694 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr) 2695 { 2696 struct bridge_rtnode *brt; 2697 2698 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) 2699 return NULL; 2700 return brt->brt_info->bri_ifp; 2701 } 2702 2703 static void 2704 bridge_rtreap_handler(netmsg_t msg) 2705 { 2706 struct bridge_softc *sc = msg->lmsg.u.ms_resultp; 2707 struct bridge_rtnode *brt, *nbrt; 2708 2709 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) { 2710 if (brt->brt_info->bri_dead) 2711 bridge_rtnode_destroy(sc, brt); 2712 } 2713 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1); 2714 } 2715 2716 static void 2717 bridge_rtreap(struct bridge_softc *sc) 2718 { 2719 struct netmsg_base msg; 2720 2721 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2722 2723 netmsg_init(&msg, NULL, &curthread->td_msgport, 2724 0, bridge_rtreap_handler); 2725 msg.lmsg.u.ms_resultp = sc; 2726 2727 ifnet_domsg(&msg.lmsg, 0); 2728 } 2729 2730 static void 2731 bridge_rtreap_async(struct bridge_softc *sc) 2732 { 2733 struct netmsg_base *msg; 2734 2735 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK); 2736 2737 netmsg_init(msg, NULL, &netisr_afree_rport, 2738 0, bridge_rtreap_handler); 2739 msg->lmsg.u.ms_resultp = sc; 2740 2741 ifnet_sendmsg(&msg->lmsg, 0); 2742 } 2743 2744 /* 2745 * bridge_rttrim: 2746 * 2747 * Trim the routine table so that we have a number 2748 * of routing entries less than or equal to the 2749 * maximum number. 2750 */ 2751 static void 2752 bridge_rttrim(struct bridge_softc *sc) 2753 { 2754 struct bridge_rtnode *brt; 2755 int dead; 2756 2757 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2758 2759 /* Make sure we actually need to do this. */ 2760 if (sc->sc_brtcnt <= sc->sc_brtmax) 2761 return; 2762 2763 /* 2764 * Find out how many rtnodes are dead 2765 */ 2766 dead = bridge_rtage_finddead(sc); 2767 KKASSERT(dead <= sc->sc_brtcnt); 2768 2769 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) { 2770 /* Enough dead rtnodes are found */ 2771 bridge_rtreap(sc); 2772 return; 2773 } 2774 2775 /* 2776 * Kill some dynamic rtnodes to meet the brtmax 2777 */ 2778 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) { 2779 struct bridge_rtinfo *bri = brt->brt_info; 2780 2781 if (bri->bri_dead) { 2782 /* 2783 * We have counted this rtnode in 2784 * bridge_rtage_finddead() 2785 */ 2786 continue; 2787 } 2788 2789 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 2790 bri->bri_dead = 1; 2791 ++dead; 2792 KKASSERT(dead <= sc->sc_brtcnt); 2793 2794 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) { 2795 /* Enough rtnodes are collected */ 2796 break; 2797 } 2798 } 2799 } 2800 if (dead) 2801 bridge_rtreap(sc); 2802 } 2803 2804 /* 2805 * bridge_timer: 2806 * 2807 * Aging timer for the bridge. 2808 */ 2809 static void 2810 bridge_timer(void *arg) 2811 { 2812 struct bridge_softc *sc = arg; 2813 struct netmsg_base *msg; 2814 2815 KKASSERT(mycpuid == BRIDGE_CFGCPU); 2816 2817 crit_enter(); 2818 2819 if (callout_pending(&sc->sc_brcallout) || 2820 !callout_active(&sc->sc_brcallout)) { 2821 crit_exit(); 2822 return; 2823 } 2824 callout_deactivate(&sc->sc_brcallout); 2825 2826 msg = &sc->sc_brtimemsg; 2827 KKASSERT(msg->lmsg.ms_flags & MSGF_DONE); 2828 lwkt_sendmsg(BRIDGE_CFGPORT, &msg->lmsg); 2829 2830 crit_exit(); 2831 } 2832 2833 static void 2834 bridge_timer_handler(netmsg_t msg) 2835 { 2836 struct bridge_softc *sc = msg->lmsg.u.ms_resultp; 2837 2838 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); 2839 2840 crit_enter(); 2841 /* Reply ASAP */ 2842 lwkt_replymsg(&msg->lmsg, 0); 2843 crit_exit(); 2844 2845 bridge_rtage(sc); 2846 if (sc->sc_ifp->if_flags & IFF_RUNNING) { 2847 callout_reset(&sc->sc_brcallout, 2848 bridge_rtable_prune_period * hz, bridge_timer, sc); 2849 } 2850 } 2851 2852 static int 2853 bridge_rtage_finddead(struct bridge_softc *sc) 2854 { 2855 struct bridge_rtnode *brt; 2856 int dead = 0; 2857 2858 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) { 2859 struct bridge_rtinfo *bri = brt->brt_info; 2860 2861 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && 2862 time_second >= bri->bri_expire) { 2863 bri->bri_dead = 1; 2864 ++dead; 2865 KKASSERT(dead <= sc->sc_brtcnt); 2866 } 2867 } 2868 return dead; 2869 } 2870 2871 /* 2872 * bridge_rtage: 2873 * 2874 * Perform an aging cycle. 2875 */ 2876 static void 2877 bridge_rtage(struct bridge_softc *sc) 2878 { 2879 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2880 2881 if (bridge_rtage_finddead(sc)) 2882 bridge_rtreap(sc); 2883 } 2884 2885 /* 2886 * bridge_rtflush: 2887 * 2888 * Remove all dynamic addresses from the bridge. 2889 */ 2890 static void 2891 bridge_rtflush(struct bridge_softc *sc, int bf) 2892 { 2893 struct bridge_rtnode *brt; 2894 int reap; 2895 2896 reap = 0; 2897 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) { 2898 struct bridge_rtinfo *bri = brt->brt_info; 2899 2900 if ((bf & IFBF_FLUSHALL) || 2901 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { 2902 bri->bri_dead = 1; 2903 reap = 1; 2904 } 2905 } 2906 if (reap) { 2907 if (bf & IFBF_FLUSHSYNC) 2908 bridge_rtreap(sc); 2909 else 2910 bridge_rtreap_async(sc); 2911 } 2912 } 2913 2914 /* 2915 * bridge_rtdaddr: 2916 * 2917 * Remove an address from the table. 2918 */ 2919 static int 2920 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr) 2921 { 2922 struct bridge_rtnode *brt; 2923 2924 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 2925 2926 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) 2927 return (ENOENT); 2928 2929 /* TODO: add a cheaper delete operation */ 2930 brt->brt_info->bri_dead = 1; 2931 bridge_rtreap(sc); 2932 return (0); 2933 } 2934 2935 /* 2936 * bridge_rtdelete: 2937 * 2938 * Delete routes to a speicifc member interface. 2939 */ 2940 void 2941 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int bf) 2942 { 2943 struct bridge_rtnode *brt; 2944 int reap; 2945 2946 reap = 0; 2947 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) { 2948 struct bridge_rtinfo *bri = brt->brt_info; 2949 2950 if (bri->bri_ifp == ifp && 2951 ((bf & IFBF_FLUSHALL) || 2952 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) { 2953 bri->bri_dead = 1; 2954 reap = 1; 2955 } 2956 } 2957 if (reap) { 2958 if (bf & IFBF_FLUSHSYNC) 2959 bridge_rtreap(sc); 2960 else 2961 bridge_rtreap_async(sc); 2962 } 2963 } 2964 2965 /* 2966 * bridge_rtable_init: 2967 * 2968 * Initialize the route table for this bridge. 2969 */ 2970 static void 2971 bridge_rtable_init(struct bridge_softc *sc) 2972 { 2973 int cpu; 2974 2975 /* 2976 * Initialize per-cpu hash tables 2977 */ 2978 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus, 2979 M_DEVBUF, M_WAITOK); 2980 for (cpu = 0; cpu < ncpus; ++cpu) { 2981 int i; 2982 2983 sc->sc_rthashs[cpu] = 2984 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE, 2985 M_DEVBUF, M_WAITOK); 2986 2987 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++) 2988 LIST_INIT(&sc->sc_rthashs[cpu][i]); 2989 } 2990 sc->sc_rthash_key = karc4random(); 2991 2992 /* 2993 * Initialize per-cpu lists 2994 */ 2995 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus, 2996 M_DEVBUF, M_WAITOK); 2997 for (cpu = 0; cpu < ncpus; ++cpu) 2998 LIST_INIT(&sc->sc_rtlists[cpu]); 2999 } 3000 3001 /* 3002 * bridge_rtable_fini: 3003 * 3004 * Deconstruct the route table for this bridge. 3005 */ 3006 static void 3007 bridge_rtable_fini(struct bridge_softc *sc) 3008 { 3009 int cpu; 3010 3011 /* 3012 * Free per-cpu hash tables 3013 */ 3014 for (cpu = 0; cpu < ncpus; ++cpu) 3015 kfree(sc->sc_rthashs[cpu], M_DEVBUF); 3016 kfree(sc->sc_rthashs, M_DEVBUF); 3017 3018 /* 3019 * Free per-cpu lists 3020 */ 3021 kfree(sc->sc_rtlists, M_DEVBUF); 3022 } 3023 3024 /* 3025 * The following hash function is adapted from "Hash Functions" by Bob Jenkins 3026 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). 3027 */ 3028 #define mix(a, b, c) \ 3029 do { \ 3030 a -= b; a -= c; a ^= (c >> 13); \ 3031 b -= c; b -= a; b ^= (a << 8); \ 3032 c -= a; c -= b; c ^= (b >> 13); \ 3033 a -= b; a -= c; a ^= (c >> 12); \ 3034 b -= c; b -= a; b ^= (a << 16); \ 3035 c -= a; c -= b; c ^= (b >> 5); \ 3036 a -= b; a -= c; a ^= (c >> 3); \ 3037 b -= c; b -= a; b ^= (a << 10); \ 3038 c -= a; c -= b; c ^= (b >> 15); \ 3039 } while (/*CONSTCOND*/0) 3040 3041 static __inline uint32_t 3042 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) 3043 { 3044 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; 3045 3046 b += addr[5] << 8; 3047 b += addr[4]; 3048 a += addr[3] << 24; 3049 a += addr[2] << 16; 3050 a += addr[1] << 8; 3051 a += addr[0]; 3052 3053 mix(a, b, c); 3054 3055 return (c & BRIDGE_RTHASH_MASK); 3056 } 3057 3058 #undef mix 3059 3060 static int 3061 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b) 3062 { 3063 int i, d; 3064 3065 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { 3066 d = ((int)a[i]) - ((int)b[i]); 3067 } 3068 3069 return (d); 3070 } 3071 3072 /* 3073 * bridge_rtnode_lookup: 3074 * 3075 * Look up a bridge route node for the specified destination. 3076 */ 3077 static struct bridge_rtnode * 3078 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr) 3079 { 3080 struct bridge_rtnode *brt; 3081 uint32_t hash; 3082 int dir; 3083 3084 hash = bridge_rthash(sc, addr); 3085 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) { 3086 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr); 3087 if (dir == 0) 3088 return (brt); 3089 if (dir > 0) 3090 return (NULL); 3091 } 3092 3093 return (NULL); 3094 } 3095 3096 /* 3097 * bridge_rtnode_insert: 3098 * 3099 * Insert the specified bridge node into the route table. 3100 * Caller has to make sure that rtnode does not exist. 3101 */ 3102 static void 3103 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) 3104 { 3105 struct bridge_rtnode *lbrt; 3106 uint32_t hash; 3107 int dir; 3108 3109 hash = bridge_rthash(sc, brt->brt_addr); 3110 3111 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]); 3112 if (lbrt == NULL) { 3113 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash], brt, brt_hash); 3114 goto out; 3115 } 3116 3117 do { 3118 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr); 3119 KASSERT(dir != 0, ("rtnode already exist\n")); 3120 3121 if (dir > 0) { 3122 LIST_INSERT_BEFORE(lbrt, brt, brt_hash); 3123 goto out; 3124 } 3125 if (LIST_NEXT(lbrt, brt_hash) == NULL) { 3126 LIST_INSERT_AFTER(lbrt, brt, brt_hash); 3127 goto out; 3128 } 3129 lbrt = LIST_NEXT(lbrt, brt_hash); 3130 } while (lbrt != NULL); 3131 3132 panic("no suitable position found for rtnode\n"); 3133 out: 3134 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list); 3135 if (mycpuid == 0) { 3136 /* 3137 * Update the brtcnt. 3138 * We only need to do it once and we do it on CPU0. 3139 */ 3140 sc->sc_brtcnt++; 3141 } 3142 } 3143 3144 /* 3145 * bridge_rtnode_destroy: 3146 * 3147 * Destroy a bridge rtnode. 3148 */ 3149 static void 3150 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt) 3151 { 3152 LIST_REMOVE(brt, brt_hash); 3153 LIST_REMOVE(brt, brt_list); 3154 3155 if (mycpuid + 1 == ncpus) { 3156 /* Free rtinfo associated with rtnode on the last cpu */ 3157 kfree(brt->brt_info, M_DEVBUF); 3158 } 3159 kfree(brt, M_DEVBUF); 3160 3161 if (mycpuid == 0) { 3162 /* Update brtcnt only on CPU0 */ 3163 sc->sc_brtcnt--; 3164 } 3165 } 3166 3167 static __inline int 3168 bridge_post_pfil(struct mbuf *m) 3169 { 3170 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) 3171 return EOPNOTSUPP; 3172 3173 /* Not yet */ 3174 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) 3175 return EOPNOTSUPP; 3176 3177 return 0; 3178 } 3179 3180 /* 3181 * Send bridge packets through pfil if they are one of the types pfil can deal 3182 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without 3183 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for 3184 * that interface. 3185 */ 3186 static int 3187 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir) 3188 { 3189 int snap, error, i, hlen; 3190 struct ether_header *eh1, eh2; 3191 struct ip *ip; 3192 struct llc llc1; 3193 u_int16_t ether_type; 3194 3195 snap = 0; 3196 error = -1; /* Default error if not error == 0 */ 3197 3198 if (pfil_bridge == 0 && pfil_member == 0) 3199 return (0); /* filtering is disabled */ 3200 3201 i = min((*mp)->m_pkthdr.len, max_protohdr); 3202 if ((*mp)->m_len < i) { 3203 *mp = m_pullup(*mp, i); 3204 if (*mp == NULL) { 3205 kprintf("%s: m_pullup failed\n", __func__); 3206 return (-1); 3207 } 3208 } 3209 3210 eh1 = mtod(*mp, struct ether_header *); 3211 ether_type = ntohs(eh1->ether_type); 3212 3213 /* 3214 * Check for SNAP/LLC. 3215 */ 3216 if (ether_type < ETHERMTU) { 3217 struct llc *llc2 = (struct llc *)(eh1 + 1); 3218 3219 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && 3220 llc2->llc_dsap == LLC_SNAP_LSAP && 3221 llc2->llc_ssap == LLC_SNAP_LSAP && 3222 llc2->llc_control == LLC_UI) { 3223 ether_type = htons(llc2->llc_un.type_snap.ether_type); 3224 snap = 1; 3225 } 3226 } 3227 3228 /* 3229 * If we're trying to filter bridge traffic, don't look at anything 3230 * other than IP and ARP traffic. If the filter doesn't understand 3231 * IPv6, don't allow IPv6 through the bridge either. This is lame 3232 * since if we really wanted, say, an AppleTalk filter, we are hosed, 3233 * but of course we don't have an AppleTalk filter to begin with. 3234 * (Note that since pfil doesn't understand ARP it will pass *ALL* 3235 * ARP traffic.) 3236 */ 3237 switch (ether_type) { 3238 case ETHERTYPE_ARP: 3239 case ETHERTYPE_REVARP: 3240 return (0); /* Automatically pass */ 3241 3242 case ETHERTYPE_IP: 3243 #ifdef INET6 3244 case ETHERTYPE_IPV6: 3245 #endif /* INET6 */ 3246 break; 3247 3248 default: 3249 /* 3250 * Check to see if the user wants to pass non-ip 3251 * packets, these will not be checked by pfil(9) 3252 * and passed unconditionally so the default is to drop. 3253 */ 3254 if (pfil_onlyip) 3255 goto bad; 3256 } 3257 3258 /* Strip off the Ethernet header and keep a copy. */ 3259 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2); 3260 m_adj(*mp, ETHER_HDR_LEN); 3261 3262 /* Strip off snap header, if present */ 3263 if (snap) { 3264 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1); 3265 m_adj(*mp, sizeof(struct llc)); 3266 } 3267 3268 /* 3269 * Check the IP header for alignment and errors 3270 */ 3271 if (dir == PFIL_IN) { 3272 switch (ether_type) { 3273 case ETHERTYPE_IP: 3274 error = bridge_ip_checkbasic(mp); 3275 break; 3276 #ifdef INET6 3277 case ETHERTYPE_IPV6: 3278 error = bridge_ip6_checkbasic(mp); 3279 break; 3280 #endif /* INET6 */ 3281 default: 3282 error = 0; 3283 } 3284 if (error) 3285 goto bad; 3286 } 3287 3288 error = 0; 3289 3290 /* 3291 * Run the packet through pfil 3292 */ 3293 switch (ether_type) { 3294 case ETHERTYPE_IP: 3295 /* 3296 * before calling the firewall, swap fields the same as 3297 * IP does. here we assume the header is contiguous 3298 */ 3299 ip = mtod(*mp, struct ip *); 3300 3301 ip->ip_len = ntohs(ip->ip_len); 3302 ip->ip_off = ntohs(ip->ip_off); 3303 3304 /* 3305 * Run pfil on the member interface and the bridge, both can 3306 * be skipped by clearing pfil_member or pfil_bridge. 3307 * 3308 * Keep the order: 3309 * in_if -> bridge_if -> out_if 3310 */ 3311 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) { 3312 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir); 3313 if (*mp == NULL || error != 0) /* filter may consume */ 3314 break; 3315 error = bridge_post_pfil(*mp); 3316 if (error) 3317 break; 3318 } 3319 3320 if (pfil_member && ifp != NULL) { 3321 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir); 3322 if (*mp == NULL || error != 0) /* filter may consume */ 3323 break; 3324 error = bridge_post_pfil(*mp); 3325 if (error) 3326 break; 3327 } 3328 3329 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) { 3330 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir); 3331 if (*mp == NULL || error != 0) /* filter may consume */ 3332 break; 3333 error = bridge_post_pfil(*mp); 3334 if (error) 3335 break; 3336 } 3337 3338 /* check if we need to fragment the packet */ 3339 if (pfil_member && ifp != NULL && dir == PFIL_OUT) { 3340 i = (*mp)->m_pkthdr.len; 3341 if (i > ifp->if_mtu) { 3342 error = bridge_fragment(ifp, *mp, &eh2, snap, 3343 &llc1); 3344 return (error); 3345 } 3346 } 3347 3348 /* Recalculate the ip checksum and restore byte ordering */ 3349 ip = mtod(*mp, struct ip *); 3350 hlen = ip->ip_hl << 2; 3351 if (hlen < sizeof(struct ip)) 3352 goto bad; 3353 if (hlen > (*mp)->m_len) { 3354 if ((*mp = m_pullup(*mp, hlen)) == 0) 3355 goto bad; 3356 ip = mtod(*mp, struct ip *); 3357 if (ip == NULL) 3358 goto bad; 3359 } 3360 ip->ip_len = htons(ip->ip_len); 3361 ip->ip_off = htons(ip->ip_off); 3362 ip->ip_sum = 0; 3363 if (hlen == sizeof(struct ip)) 3364 ip->ip_sum = in_cksum_hdr(ip); 3365 else 3366 ip->ip_sum = in_cksum(*mp, hlen); 3367 3368 break; 3369 #ifdef INET6 3370 case ETHERTYPE_IPV6: 3371 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) 3372 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp, 3373 dir); 3374 3375 if (*mp == NULL || error != 0) /* filter may consume */ 3376 break; 3377 3378 if (pfil_member && ifp != NULL) 3379 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp, 3380 dir); 3381 3382 if (*mp == NULL || error != 0) /* filter may consume */ 3383 break; 3384 3385 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) 3386 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp, 3387 dir); 3388 break; 3389 #endif 3390 default: 3391 error = 0; 3392 break; 3393 } 3394 3395 if (*mp == NULL) 3396 return (error); 3397 if (error != 0) 3398 goto bad; 3399 3400 error = -1; 3401 3402 /* 3403 * Finally, put everything back the way it was and return 3404 */ 3405 if (snap) { 3406 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT); 3407 if (*mp == NULL) 3408 return (error); 3409 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc)); 3410 } 3411 3412 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT); 3413 if (*mp == NULL) 3414 return (error); 3415 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN); 3416 3417 return (0); 3418 3419 bad: 3420 m_freem(*mp); 3421 *mp = NULL; 3422 return (error); 3423 } 3424 3425 /* 3426 * Perform basic checks on header size since 3427 * pfil assumes ip_input has already processed 3428 * it for it. Cut-and-pasted from ip_input.c. 3429 * Given how simple the IPv6 version is, 3430 * does the IPv4 version really need to be 3431 * this complicated? 3432 * 3433 * XXX Should we update ipstat here, or not? 3434 * XXX Right now we update ipstat but not 3435 * XXX csum_counter. 3436 */ 3437 static int 3438 bridge_ip_checkbasic(struct mbuf **mp) 3439 { 3440 struct mbuf *m = *mp; 3441 struct ip *ip; 3442 int len, hlen; 3443 u_short sum; 3444 3445 if (*mp == NULL) 3446 return (-1); 3447 #if notyet 3448 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 3449 if ((m = m_copyup(m, sizeof(struct ip), 3450 (max_linkhdr + 3) & ~3)) == NULL) { 3451 /* XXXJRT new stat, please */ 3452 ipstat.ips_toosmall++; 3453 goto bad; 3454 } 3455 } else 3456 #endif 3457 #ifndef __predict_false 3458 #define __predict_false(x) x 3459 #endif 3460 if (__predict_false(m->m_len < sizeof (struct ip))) { 3461 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 3462 ipstat.ips_toosmall++; 3463 goto bad; 3464 } 3465 } 3466 ip = mtod(m, struct ip *); 3467 if (ip == NULL) goto bad; 3468 3469 if (ip->ip_v != IPVERSION) { 3470 ipstat.ips_badvers++; 3471 goto bad; 3472 } 3473 hlen = ip->ip_hl << 2; 3474 if (hlen < sizeof(struct ip)) { /* minimum header length */ 3475 ipstat.ips_badhlen++; 3476 goto bad; 3477 } 3478 if (hlen > m->m_len) { 3479 if ((m = m_pullup(m, hlen)) == 0) { 3480 ipstat.ips_badhlen++; 3481 goto bad; 3482 } 3483 ip = mtod(m, struct ip *); 3484 if (ip == NULL) goto bad; 3485 } 3486 3487 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 3488 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 3489 } else { 3490 if (hlen == sizeof(struct ip)) { 3491 sum = in_cksum_hdr(ip); 3492 } else { 3493 sum = in_cksum(m, hlen); 3494 } 3495 } 3496 if (sum) { 3497 ipstat.ips_badsum++; 3498 goto bad; 3499 } 3500 3501 /* Retrieve the packet length. */ 3502 len = ntohs(ip->ip_len); 3503 3504 /* 3505 * Check for additional length bogosity 3506 */ 3507 if (len < hlen) { 3508 ipstat.ips_badlen++; 3509 goto bad; 3510 } 3511 3512 /* 3513 * Check that the amount of data in the buffers 3514 * is as at least much as the IP header would have us expect. 3515 * Drop packet if shorter than we expect. 3516 */ 3517 if (m->m_pkthdr.len < len) { 3518 ipstat.ips_tooshort++; 3519 goto bad; 3520 } 3521 3522 /* Checks out, proceed */ 3523 *mp = m; 3524 return (0); 3525 3526 bad: 3527 *mp = m; 3528 return (-1); 3529 } 3530 3531 #ifdef INET6 3532 /* 3533 * Same as above, but for IPv6. 3534 * Cut-and-pasted from ip6_input.c. 3535 * XXX Should we update ip6stat, or not? 3536 */ 3537 static int 3538 bridge_ip6_checkbasic(struct mbuf **mp) 3539 { 3540 struct mbuf *m = *mp; 3541 struct ip6_hdr *ip6; 3542 3543 /* 3544 * If the IPv6 header is not aligned, slurp it up into a new 3545 * mbuf with space for link headers, in the event we forward 3546 * it. Otherwise, if it is aligned, make sure the entire base 3547 * IPv6 header is in the first mbuf of the chain. 3548 */ 3549 #if notyet 3550 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 3551 struct ifnet *inifp = m->m_pkthdr.rcvif; 3552 if ((m = m_copyup(m, sizeof(struct ip6_hdr), 3553 (max_linkhdr + 3) & ~3)) == NULL) { 3554 /* XXXJRT new stat, please */ 3555 ip6stat.ip6s_toosmall++; 3556 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 3557 goto bad; 3558 } 3559 } else 3560 #endif 3561 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) { 3562 struct ifnet *inifp = m->m_pkthdr.rcvif; 3563 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { 3564 ip6stat.ip6s_toosmall++; 3565 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 3566 goto bad; 3567 } 3568 } 3569 3570 ip6 = mtod(m, struct ip6_hdr *); 3571 3572 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 3573 ip6stat.ip6s_badvers++; 3574 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 3575 goto bad; 3576 } 3577 3578 /* Checks out, proceed */ 3579 *mp = m; 3580 return (0); 3581 3582 bad: 3583 *mp = m; 3584 return (-1); 3585 } 3586 #endif /* INET6 */ 3587 3588 /* 3589 * bridge_fragment: 3590 * 3591 * Return a fragmented mbuf chain. 3592 */ 3593 static int 3594 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh, 3595 int snap, struct llc *llc) 3596 { 3597 struct mbuf *m0; 3598 struct ip *ip; 3599 int error = -1; 3600 3601 if (m->m_len < sizeof(struct ip) && 3602 (m = m_pullup(m, sizeof(struct ip))) == NULL) 3603 goto out; 3604 ip = mtod(m, struct ip *); 3605 3606 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, 3607 CSUM_DELAY_IP); 3608 if (error) 3609 goto out; 3610 3611 /* walk the chain and re-add the Ethernet header */ 3612 for (m0 = m; m0; m0 = m0->m_nextpkt) { 3613 if (error == 0) { 3614 if (snap) { 3615 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT); 3616 if (m0 == NULL) { 3617 error = ENOBUFS; 3618 continue; 3619 } 3620 bcopy(llc, mtod(m0, caddr_t), 3621 sizeof(struct llc)); 3622 } 3623 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT); 3624 if (m0 == NULL) { 3625 error = ENOBUFS; 3626 continue; 3627 } 3628 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN); 3629 } else 3630 m_freem(m); 3631 } 3632 3633 if (error == 0) 3634 ipstat.ips_fragmented++; 3635 3636 return (error); 3637 3638 out: 3639 if (m != NULL) 3640 m_freem(m); 3641 return (error); 3642 } 3643 3644 static void 3645 bridge_enqueue_handler(netmsg_t msg) 3646 { 3647 struct netmsg_packet *nmp; 3648 struct ifnet *dst_ifp; 3649 struct mbuf *m; 3650 3651 nmp = &msg->packet; 3652 m = nmp->nm_packet; 3653 dst_ifp = nmp->base.lmsg.u.ms_resultp; 3654 3655 bridge_handoff(dst_ifp, m); 3656 } 3657 3658 static void 3659 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m) 3660 { 3661 struct mbuf *m0; 3662 3663 /* We may be sending a fragment so traverse the mbuf */ 3664 for (; m; m = m0) { 3665 struct altq_pktattr pktattr; 3666 3667 m0 = m->m_nextpkt; 3668 m->m_nextpkt = NULL; 3669 3670 if (ifq_is_enabled(&dst_ifp->if_snd)) 3671 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr); 3672 3673 ifq_dispatch(dst_ifp, m, &pktattr); 3674 } 3675 } 3676 3677 static void 3678 bridge_control_dispatch(netmsg_t msg) 3679 { 3680 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)msg; 3681 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp; 3682 int error; 3683 3684 ifnet_serialize_all(bifp); 3685 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg); 3686 ifnet_deserialize_all(bifp); 3687 3688 lwkt_replymsg(&bc_msg->base.lmsg, error); 3689 } 3690 3691 static int 3692 bridge_control(struct bridge_softc *sc, u_long cmd, 3693 bridge_ctl_t bc_func, void *bc_arg) 3694 { 3695 struct ifnet *bifp = sc->sc_ifp; 3696 struct netmsg_brctl bc_msg; 3697 int error; 3698 3699 ASSERT_IFNET_SERIALIZED_ALL(bifp); 3700 3701 bzero(&bc_msg, sizeof(bc_msg)); 3702 3703 netmsg_init(&bc_msg.base, NULL, &curthread->td_msgport, 3704 0, bridge_control_dispatch); 3705 bc_msg.bc_func = bc_func; 3706 bc_msg.bc_sc = sc; 3707 bc_msg.bc_arg = bc_arg; 3708 3709 ifnet_deserialize_all(bifp); 3710 error = lwkt_domsg(BRIDGE_CFGPORT, &bc_msg.base.lmsg, 0); 3711 ifnet_serialize_all(bifp); 3712 return error; 3713 } 3714 3715 static void 3716 bridge_add_bif_handler(netmsg_t msg) 3717 { 3718 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)msg; 3719 struct bridge_softc *sc; 3720 struct bridge_iflist *bif; 3721 3722 sc = amsg->br_softc; 3723 3724 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO); 3725 bif->bif_ifp = amsg->br_bif_ifp; 3726 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER; 3727 bif->bif_onlist = 1; 3728 bif->bif_info = amsg->br_bif_info; 3729 3730 LIST_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next); 3731 3732 ifnet_forwardmsg(&amsg->base.lmsg, mycpuid + 1); 3733 } 3734 3735 static void 3736 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info, 3737 struct ifnet *ifp) 3738 { 3739 struct netmsg_braddbif amsg; 3740 3741 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 3742 3743 netmsg_init(&amsg.base, NULL, &curthread->td_msgport, 3744 0, bridge_add_bif_handler); 3745 amsg.br_softc = sc; 3746 amsg.br_bif_info = bif_info; 3747 amsg.br_bif_ifp = ifp; 3748 3749 ifnet_domsg(&amsg.base.lmsg, 0); 3750 } 3751 3752 static void 3753 bridge_del_bif_handler(netmsg_t msg) 3754 { 3755 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)msg; 3756 struct bridge_softc *sc; 3757 struct bridge_iflist *bif; 3758 3759 sc = dmsg->br_softc; 3760 3761 /* 3762 * Locate the bif associated with the br_bif_info 3763 * on the current CPU 3764 */ 3765 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info); 3766 KKASSERT(bif != NULL && bif->bif_onlist); 3767 3768 /* Remove the bif from the current CPU's iflist */ 3769 bif->bif_onlist = 0; 3770 LIST_REMOVE(bif, bif_next); 3771 3772 /* Save the removed bif for later freeing */ 3773 LIST_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next); 3774 3775 ifnet_forwardmsg(&dmsg->base.lmsg, mycpuid + 1); 3776 } 3777 3778 static void 3779 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info, 3780 struct bridge_iflist_head *saved_bifs) 3781 { 3782 struct netmsg_brdelbif dmsg; 3783 3784 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 3785 3786 netmsg_init(&dmsg.base, NULL, &curthread->td_msgport, 3787 0, bridge_del_bif_handler); 3788 dmsg.br_softc = sc; 3789 dmsg.br_bif_info = bif_info; 3790 dmsg.br_bif_list = saved_bifs; 3791 3792 ifnet_domsg(&dmsg.base.lmsg, 0); 3793 } 3794 3795 static void 3796 bridge_set_bifflags_handler(netmsg_t msg) 3797 { 3798 struct netmsg_brsflags *smsg = (struct netmsg_brsflags *)msg; 3799 struct bridge_softc *sc; 3800 struct bridge_iflist *bif; 3801 3802 sc = smsg->br_softc; 3803 3804 /* 3805 * Locate the bif associated with the br_bif_info 3806 * on the current CPU 3807 */ 3808 bif = bridge_lookup_member_ifinfo(sc, smsg->br_bif_info); 3809 KKASSERT(bif != NULL && bif->bif_onlist); 3810 3811 bif->bif_flags = smsg->br_bif_flags; 3812 3813 ifnet_forwardmsg(&smsg->base.lmsg, mycpuid + 1); 3814 } 3815 3816 static void 3817 bridge_set_bifflags(struct bridge_softc *sc, struct bridge_ifinfo *bif_info, 3818 uint32_t bif_flags) 3819 { 3820 struct netmsg_brsflags smsg; 3821 3822 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp); 3823 3824 netmsg_init(&smsg.base, NULL, &curthread->td_msgport, 3825 0, bridge_set_bifflags_handler); 3826 smsg.br_softc = sc; 3827 smsg.br_bif_info = bif_info; 3828 smsg.br_bif_flags = bif_flags; 3829 3830 ifnet_domsg(&smsg.base.lmsg, 0); 3831 } 3832