1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
63 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
67 * Network interface bridge support.
71 * - Currently only supports Ethernet-like interfaces (Ethernet,
72 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
73 * to bridge other types of interfaces (FDDI-FDDI, and maybe
74 * consider heterogeneous bridges).
77 #include <sys/cdefs.h>
78 __FBSDID("$FreeBSD$");
81 #include "opt_inet6.h"
83 #include <sys/param.h>
84 #include <sys/eventhandler.h>
86 #include <sys/malloc.h>
87 #include <sys/protosw.h>
88 #include <sys/systm.h>
91 #include <sys/socket.h> /* for net/if.h */
92 #include <sys/sockio.h>
93 #include <sys/ctype.h> /* string functions */
94 #include <sys/kernel.h>
95 #include <sys/random.h>
96 #include <sys/syslog.h>
97 #include <sys/sysctl.h>
99 #include <sys/module.h>
100 #include <sys/priv.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/mutex.h>
107 #include <net/if_clone.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/if_var.h>
111 #include <net/pfil.h>
112 #include <net/vnet.h>
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/in_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip_var.h>
120 #include <netinet/ip6.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet6/in6_ifattach.h>
124 #if defined(INET) || defined(INET6)
125 #include <netinet/ip_carp.h>
127 #include <machine/in_cksum.h>
128 #include <netinet/if_ether.h>
129 #include <net/bridgestp.h>
130 #include <net/if_bridgevar.h>
131 #include <net/if_llc.h>
132 #include <net/if_vlan_var.h>
134 #include <net/route.h>
137 * Size of the route hash table. Must be a power of two.
139 #ifndef BRIDGE_RTHASH_SIZE
140 #define BRIDGE_RTHASH_SIZE 1024
143 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
146 * Default maximum number of addresses to cache.
148 #ifndef BRIDGE_RTABLE_MAX
149 #define BRIDGE_RTABLE_MAX 2000
153 * Timeout (in seconds) for entries learned dynamically.
155 #ifndef BRIDGE_RTABLE_TIMEOUT
156 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
160 * Number of seconds between walks of the route list.
162 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
163 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
167 * List of capabilities to possibly mask on the member interface.
169 #define BRIDGE_IFCAPS_MASK (IFCAP_TOE|IFCAP_TSO|IFCAP_TXCSUM)
172 * List of capabilities to strip
174 #define BRIDGE_IFCAPS_STRIP IFCAP_LRO
177 * Bridge interface list entry.
179 struct bridge_iflist
{
180 LIST_ENTRY(bridge_iflist
) bif_next
;
181 struct ifnet
*bif_ifp
; /* member if */
182 struct bstp_port bif_stp
; /* STP state */
183 uint32_t bif_flags
; /* member if flags */
184 int bif_savedcaps
; /* saved capabilities */
185 uint32_t bif_addrmax
; /* max # of addresses */
186 uint32_t bif_addrcnt
; /* cur. # of addresses */
187 uint32_t bif_addrexceeded
;/* # of address violations */
193 struct bridge_rtnode
{
194 LIST_ENTRY(bridge_rtnode
) brt_hash
; /* hash table linkage */
195 LIST_ENTRY(bridge_rtnode
) brt_list
; /* list linkage */
196 struct bridge_iflist
*brt_dst
; /* destination if */
197 unsigned long brt_expire
; /* expiration time */
198 uint8_t brt_flags
; /* address flags */
199 uint8_t brt_addr
[ETHER_ADDR_LEN
];
200 uint16_t brt_vlan
; /* vlan id */
202 #define brt_ifp brt_dst->bif_ifp
205 * Software state for each bridge.
207 struct bridge_softc
{
208 struct ifnet
*sc_ifp
; /* make this an interface */
209 LIST_ENTRY(bridge_softc
) sc_list
;
212 uint32_t sc_brtmax
; /* max # of addresses */
213 uint32_t sc_brtcnt
; /* cur. # of addresses */
214 uint32_t sc_brttimeout
; /* rt timeout in seconds */
215 struct callout sc_brcallout
; /* bridge callout */
216 uint32_t sc_iflist_ref
; /* refcount for sc_iflist */
217 uint32_t sc_iflist_xcnt
; /* refcount for sc_iflist */
218 LIST_HEAD(, bridge_iflist
) sc_iflist
; /* member interface list */
219 LIST_HEAD(, bridge_rtnode
) *sc_rthash
; /* our forwarding table */
220 LIST_HEAD(, bridge_rtnode
) sc_rtlist
; /* list version of above */
221 uint32_t sc_rthash_key
; /* key for hash */
222 LIST_HEAD(, bridge_iflist
) sc_spanlist
; /* span ports list */
223 struct bstp_state sc_stp
; /* STP state */
224 uint32_t sc_brtexceeded
; /* # of cache drops */
225 struct ifnet
*sc_ifaddr
; /* member mac copied from */
226 u_char sc_defaddr
[6]; /* Default MAC address */
229 static VNET_DEFINE(struct mtx
, bridge_list_mtx
);
230 #define V_bridge_list_mtx VNET(bridge_list_mtx)
231 static eventhandler_tag bridge_detach_cookie
;
233 int bridge_rtable_prune_period
= BRIDGE_RTABLE_PRUNE_PERIOD
;
235 uma_zone_t bridge_rtnode_zone
;
237 static int bridge_clone_create(struct if_clone
*, int, caddr_t
);
238 static void bridge_clone_destroy(struct ifnet
*);
240 static int bridge_ioctl(struct ifnet
*, u_long
, caddr_t
);
241 static void bridge_mutecaps(struct bridge_softc
*);
242 static void bridge_set_ifcap(struct bridge_softc
*, struct bridge_iflist
*,
244 static void bridge_ifdetach(void *arg __unused
, struct ifnet
*);
245 static void bridge_init(void *);
246 static void bridge_dummynet(struct mbuf
*, struct ifnet
*);
247 static void bridge_stop(struct ifnet
*, int);
248 static int bridge_transmit(struct ifnet
*, struct mbuf
*);
249 static void bridge_qflush(struct ifnet
*);
250 static struct mbuf
*bridge_input(struct ifnet
*, struct mbuf
*);
251 static int bridge_output(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
253 static int bridge_enqueue(struct bridge_softc
*, struct ifnet
*,
255 static void bridge_rtdelete(struct bridge_softc
*, struct ifnet
*ifp
, int);
257 static void bridge_forward(struct bridge_softc
*, struct bridge_iflist
*,
260 static void bridge_timer(void *);
262 static void bridge_broadcast(struct bridge_softc
*, struct ifnet
*,
264 static void bridge_span(struct bridge_softc
*, struct mbuf
*);
266 static int bridge_rtupdate(struct bridge_softc
*, const uint8_t *,
267 uint16_t, struct bridge_iflist
*, int, uint8_t);
268 static struct ifnet
*bridge_rtlookup(struct bridge_softc
*, const uint8_t *,
270 static void bridge_rttrim(struct bridge_softc
*);
271 static void bridge_rtage(struct bridge_softc
*);
272 static void bridge_rtflush(struct bridge_softc
*, int);
273 static int bridge_rtdaddr(struct bridge_softc
*, const uint8_t *,
276 static void bridge_rtable_init(struct bridge_softc
*);
277 static void bridge_rtable_fini(struct bridge_softc
*);
279 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
280 static struct bridge_rtnode
*bridge_rtnode_lookup(struct bridge_softc
*,
281 const uint8_t *, uint16_t);
282 static int bridge_rtnode_insert(struct bridge_softc
*,
283 struct bridge_rtnode
*);
284 static void bridge_rtnode_destroy(struct bridge_softc
*,
285 struct bridge_rtnode
*);
286 static void bridge_rtable_expire(struct ifnet
*, int);
287 static void bridge_state_change(struct ifnet
*, int);
289 static struct bridge_iflist
*bridge_lookup_member(struct bridge_softc
*,
291 static struct bridge_iflist
*bridge_lookup_member_if(struct bridge_softc
*,
293 static void bridge_delete_member(struct bridge_softc
*,
294 struct bridge_iflist
*, int);
295 static void bridge_delete_span(struct bridge_softc
*,
296 struct bridge_iflist
*);
298 static int bridge_ioctl_add(struct bridge_softc
*, void *);
299 static int bridge_ioctl_del(struct bridge_softc
*, void *);
300 static int bridge_ioctl_gifflags(struct bridge_softc
*, void *);
301 static int bridge_ioctl_sifflags(struct bridge_softc
*, void *);
302 static int bridge_ioctl_scache(struct bridge_softc
*, void *);
303 static int bridge_ioctl_gcache(struct bridge_softc
*, void *);
304 static int bridge_ioctl_gifs(struct bridge_softc
*, void *);
305 static int bridge_ioctl_rts(struct bridge_softc
*, void *);
306 static int bridge_ioctl_saddr(struct bridge_softc
*, void *);
307 static int bridge_ioctl_sto(struct bridge_softc
*, void *);
308 static int bridge_ioctl_gto(struct bridge_softc
*, void *);
309 static int bridge_ioctl_daddr(struct bridge_softc
*, void *);
310 static int bridge_ioctl_flush(struct bridge_softc
*, void *);
311 static int bridge_ioctl_gpri(struct bridge_softc
*, void *);
312 static int bridge_ioctl_spri(struct bridge_softc
*, void *);
313 static int bridge_ioctl_ght(struct bridge_softc
*, void *);
314 static int bridge_ioctl_sht(struct bridge_softc
*, void *);
315 static int bridge_ioctl_gfd(struct bridge_softc
*, void *);
316 static int bridge_ioctl_sfd(struct bridge_softc
*, void *);
317 static int bridge_ioctl_gma(struct bridge_softc
*, void *);
318 static int bridge_ioctl_sma(struct bridge_softc
*, void *);
319 static int bridge_ioctl_sifprio(struct bridge_softc
*, void *);
320 static int bridge_ioctl_sifcost(struct bridge_softc
*, void *);
321 static int bridge_ioctl_sifmaxaddr(struct bridge_softc
*, void *);
322 static int bridge_ioctl_addspan(struct bridge_softc
*, void *);
323 static int bridge_ioctl_delspan(struct bridge_softc
*, void *);
324 static int bridge_ioctl_gbparam(struct bridge_softc
*, void *);
325 static int bridge_ioctl_grte(struct bridge_softc
*, void *);
326 static int bridge_ioctl_gifsstp(struct bridge_softc
*, void *);
327 static int bridge_ioctl_sproto(struct bridge_softc
*, void *);
328 static int bridge_ioctl_stxhc(struct bridge_softc
*, void *);
329 static int bridge_pfil(struct mbuf
**, struct ifnet
*, struct ifnet
*,
331 static int bridge_ip_checkbasic(struct mbuf
**mp
);
333 static int bridge_ip6_checkbasic(struct mbuf
**mp
);
335 static int bridge_fragment(struct ifnet
*, struct mbuf
*,
336 struct ether_header
*, int, struct llc
*);
337 static void bridge_linkstate(struct ifnet
*ifp
);
338 static void bridge_linkcheck(struct bridge_softc
*sc
);
340 extern void (*bridge_linkstate_p
)(struct ifnet
*ifp
);
342 /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */
343 #define VLANTAGOF(_m) \
344 (_m->m_flags & M_VLANTAG) ? EVL_VLANOFTAG(_m->m_pkthdr.ether_vtag) : 1
346 static struct bstp_cb_ops bridge_ops
= {
347 .bcb_state
= bridge_state_change
,
348 .bcb_rtage
= bridge_rtable_expire
351 SYSCTL_DECL(_net_link
);
352 static SYSCTL_NODE(_net_link
, IFT_BRIDGE
, bridge
, CTLFLAG_RW
, 0, "Bridge");
354 /* only pass IP[46] packets when pfil is enabled */
355 static VNET_DEFINE(int, pfil_onlyip
) = 1;
356 #define V_pfil_onlyip VNET(pfil_onlyip)
357 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_onlyip
,
358 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(pfil_onlyip
), 0,
359 "Only pass IP packets when pfil is enabled");
361 /* run pfil hooks on the bridge interface */
362 static VNET_DEFINE(int, pfil_bridge
) = 1;
363 #define V_pfil_bridge VNET(pfil_bridge)
364 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_bridge
,
365 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(pfil_bridge
), 0,
366 "Packet filter on the bridge interface");
368 /* layer2 filter with ipfw */
369 static VNET_DEFINE(int, pfil_ipfw
);
370 #define V_pfil_ipfw VNET(pfil_ipfw)
372 /* layer2 ARP filter with ipfw */
373 static VNET_DEFINE(int, pfil_ipfw_arp
);
374 #define V_pfil_ipfw_arp VNET(pfil_ipfw_arp)
375 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, ipfw_arp
,
376 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(pfil_ipfw_arp
), 0,
377 "Filter ARP packets through IPFW layer2");
379 /* run pfil hooks on the member interface */
380 static VNET_DEFINE(int, pfil_member
) = 1;
381 #define V_pfil_member VNET(pfil_member)
382 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_member
,
383 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(pfil_member
), 0,
384 "Packet filter on the member interface");
386 /* run pfil hooks on the physical interface for locally destined packets */
387 static VNET_DEFINE(int, pfil_local_phys
);
388 #define V_pfil_local_phys VNET(pfil_local_phys)
389 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, pfil_local_phys
,
390 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(pfil_local_phys
), 0,
391 "Packet filter on the physical interface for locally destined packets");
393 /* log STP state changes */
394 static VNET_DEFINE(int, log_stp
);
395 #define V_log_stp VNET(log_stp)
396 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, log_stp
,
397 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(log_stp
), 0,
398 "Log STP state changes");
400 /* share MAC with first bridge member */
401 static VNET_DEFINE(int, bridge_inherit_mac
);
402 #define V_bridge_inherit_mac VNET(bridge_inherit_mac)
403 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, inherit_mac
,
404 CTLFLAG_RWTUN
| CTLFLAG_VNET
, &VNET_NAME(bridge_inherit_mac
), 0,
405 "Inherit MAC address from the first bridge member");
407 static VNET_DEFINE(int, allow_llz_overlap
) = 0;
408 #define V_allow_llz_overlap VNET(allow_llz_overlap)
409 SYSCTL_INT(_net_link_bridge
, OID_AUTO
, allow_llz_overlap
,
410 CTLFLAG_VNET
| CTLFLAG_RW
| CTLFLAG_VNET
, &VNET_NAME(allow_llz_overlap
), 0,
411 "Allow overlap of link-local scope "
412 "zones of a bridge interface and the member interfaces");
414 struct bridge_control
{
415 int (*bc_func
)(struct bridge_softc
*, void *);
420 #define BC_F_COPYIN 0x01 /* copy arguments in */
421 #define BC_F_COPYOUT 0x02 /* copy arguments out */
422 #define BC_F_SUSER 0x04 /* do super-user check */
424 const struct bridge_control bridge_control_table
[] = {
425 { bridge_ioctl_add
, sizeof(struct ifbreq
),
426 BC_F_COPYIN
|BC_F_SUSER
},
427 { bridge_ioctl_del
, sizeof(struct ifbreq
),
428 BC_F_COPYIN
|BC_F_SUSER
},
430 { bridge_ioctl_gifflags
, sizeof(struct ifbreq
),
431 BC_F_COPYIN
|BC_F_COPYOUT
},
432 { bridge_ioctl_sifflags
, sizeof(struct ifbreq
),
433 BC_F_COPYIN
|BC_F_SUSER
},
435 { bridge_ioctl_scache
, sizeof(struct ifbrparam
),
436 BC_F_COPYIN
|BC_F_SUSER
},
437 { bridge_ioctl_gcache
, sizeof(struct ifbrparam
),
440 { bridge_ioctl_gifs
, sizeof(struct ifbifconf
),
441 BC_F_COPYIN
|BC_F_COPYOUT
},
442 { bridge_ioctl_rts
, sizeof(struct ifbaconf
),
443 BC_F_COPYIN
|BC_F_COPYOUT
},
445 { bridge_ioctl_saddr
, sizeof(struct ifbareq
),
446 BC_F_COPYIN
|BC_F_SUSER
},
448 { bridge_ioctl_sto
, sizeof(struct ifbrparam
),
449 BC_F_COPYIN
|BC_F_SUSER
},
450 { bridge_ioctl_gto
, sizeof(struct ifbrparam
),
453 { bridge_ioctl_daddr
, sizeof(struct ifbareq
),
454 BC_F_COPYIN
|BC_F_SUSER
},
456 { bridge_ioctl_flush
, sizeof(struct ifbreq
),
457 BC_F_COPYIN
|BC_F_SUSER
},
459 { bridge_ioctl_gpri
, sizeof(struct ifbrparam
),
461 { bridge_ioctl_spri
, sizeof(struct ifbrparam
),
462 BC_F_COPYIN
|BC_F_SUSER
},
464 { bridge_ioctl_ght
, sizeof(struct ifbrparam
),
466 { bridge_ioctl_sht
, sizeof(struct ifbrparam
),
467 BC_F_COPYIN
|BC_F_SUSER
},
469 { bridge_ioctl_gfd
, sizeof(struct ifbrparam
),
471 { bridge_ioctl_sfd
, sizeof(struct ifbrparam
),
472 BC_F_COPYIN
|BC_F_SUSER
},
474 { bridge_ioctl_gma
, sizeof(struct ifbrparam
),
476 { bridge_ioctl_sma
, sizeof(struct ifbrparam
),
477 BC_F_COPYIN
|BC_F_SUSER
},
479 { bridge_ioctl_sifprio
, sizeof(struct ifbreq
),
480 BC_F_COPYIN
|BC_F_SUSER
},
482 { bridge_ioctl_sifcost
, sizeof(struct ifbreq
),
483 BC_F_COPYIN
|BC_F_SUSER
},
485 { bridge_ioctl_addspan
, sizeof(struct ifbreq
),
486 BC_F_COPYIN
|BC_F_SUSER
},
487 { bridge_ioctl_delspan
, sizeof(struct ifbreq
),
488 BC_F_COPYIN
|BC_F_SUSER
},
490 { bridge_ioctl_gbparam
, sizeof(struct ifbropreq
),
493 { bridge_ioctl_grte
, sizeof(struct ifbrparam
),
496 { bridge_ioctl_gifsstp
, sizeof(struct ifbpstpconf
),
497 BC_F_COPYIN
|BC_F_COPYOUT
},
499 { bridge_ioctl_sproto
, sizeof(struct ifbrparam
),
500 BC_F_COPYIN
|BC_F_SUSER
},
502 { bridge_ioctl_stxhc
, sizeof(struct ifbrparam
),
503 BC_F_COPYIN
|BC_F_SUSER
},
505 { bridge_ioctl_sifmaxaddr
, sizeof(struct ifbreq
),
506 BC_F_COPYIN
|BC_F_SUSER
},
509 const int bridge_control_table_size
= nitems(bridge_control_table
);
511 static VNET_DEFINE(LIST_HEAD(, bridge_softc
), bridge_list
);
512 #define V_bridge_list VNET(bridge_list)
513 #define BRIDGE_LIST_LOCK_INIT(x) mtx_init(&V_bridge_list_mtx, \
514 "if_bridge list", NULL, MTX_DEF)
515 #define BRIDGE_LIST_LOCK_DESTROY(x) mtx_destroy(&V_bridge_list_mtx)
516 #define BRIDGE_LIST_LOCK(x) mtx_lock(&V_bridge_list_mtx)
517 #define BRIDGE_LIST_UNLOCK(x) mtx_unlock(&V_bridge_list_mtx)
519 static VNET_DEFINE(struct if_clone
*, bridge_cloner
);
520 #define V_bridge_cloner VNET(bridge_cloner)
522 static const char bridge_name
[] = "bridge";
525 vnet_bridge_init(const void *unused __unused
)
528 BRIDGE_LIST_LOCK_INIT();
529 LIST_INIT(&V_bridge_list
);
530 V_bridge_cloner
= if_clone_simple(bridge_name
,
531 bridge_clone_create
, bridge_clone_destroy
, 0);
533 VNET_SYSINIT(vnet_bridge_init
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_ANY
,
534 vnet_bridge_init
, NULL
);
537 vnet_bridge_uninit(const void *unused __unused
)
540 if_clone_detach(V_bridge_cloner
);
541 V_bridge_cloner
= NULL
;
542 BRIDGE_LIST_LOCK_DESTROY();
544 VNET_SYSUNINIT(vnet_bridge_uninit
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_ANY
,
545 vnet_bridge_uninit
, NULL
);
548 bridge_modevent(module_t mod
, int type
, void *data
)
553 bridge_rtnode_zone
= uma_zcreate("bridge_rtnode",
554 sizeof(struct bridge_rtnode
), NULL
, NULL
, NULL
, NULL
,
556 bridge_input_p
= bridge_input
;
557 bridge_output_p
= bridge_output
;
558 bridge_dn_p
= bridge_dummynet
;
559 bridge_linkstate_p
= bridge_linkstate
;
560 bridge_detach_cookie
= EVENTHANDLER_REGISTER(
561 ifnet_departure_event
, bridge_ifdetach
, NULL
,
562 EVENTHANDLER_PRI_ANY
);
565 EVENTHANDLER_DEREGISTER(ifnet_departure_event
,
566 bridge_detach_cookie
);
567 uma_zdestroy(bridge_rtnode_zone
);
568 bridge_input_p
= NULL
;
569 bridge_output_p
= NULL
;
571 bridge_linkstate_p
= NULL
;
579 static moduledata_t bridge_mod
= {
585 DECLARE_MODULE(if_bridge
, bridge_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);
586 MODULE_DEPEND(if_bridge
, bridgestp
, 1, 1, 1);
589 * handler for net.link.bridge.ipfw
592 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS
)
594 int enable
= V_pfil_ipfw
;
597 error
= sysctl_handle_int(oidp
, &enable
, 0, req
);
600 if (enable
!= V_pfil_ipfw
) {
601 V_pfil_ipfw
= enable
;
604 * Disable pfil so that ipfw doesnt run twice, if the user
605 * really wants both then they can re-enable pfil_bridge and/or
606 * pfil_member. Also allow non-ip packets as ipfw can filter by
618 SYSCTL_PROC(_net_link_bridge
, OID_AUTO
, ipfw
,
619 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_VNET
,
620 &VNET_NAME(pfil_ipfw
), 0, &sysctl_pfil_ipfw
, "I",
621 "Layer2 filter with IPFW");
624 * bridge_clone_create:
626 * Create a new bridge instance.
629 bridge_clone_create(struct if_clone
*ifc
, int unit
, caddr_t params
)
631 struct bridge_softc
*sc
, *sc2
;
632 struct ifnet
*bifp
, *ifp
;
634 unsigned long hostid
;
636 sc
= malloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
637 ifp
= sc
->sc_ifp
= if_alloc(IFT_ETHER
);
643 BRIDGE_LOCK_INIT(sc
);
644 sc
->sc_brtmax
= BRIDGE_RTABLE_MAX
;
645 sc
->sc_brttimeout
= BRIDGE_RTABLE_TIMEOUT
;
647 /* Initialize our routing table. */
648 bridge_rtable_init(sc
);
650 callout_init_mtx(&sc
->sc_brcallout
, &sc
->sc_mtx
, 0);
652 LIST_INIT(&sc
->sc_iflist
);
653 LIST_INIT(&sc
->sc_spanlist
);
656 if_initname(ifp
, bridge_name
, unit
);
657 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
658 ifp
->if_ioctl
= bridge_ioctl
;
659 ifp
->if_transmit
= bridge_transmit
;
660 ifp
->if_qflush
= bridge_qflush
;
661 ifp
->if_init
= bridge_init
;
662 ifp
->if_type
= IFT_BRIDGE
;
665 * Generate an ethernet address with a locally administered address.
667 * Since we are using random ethernet addresses for the bridge, it is
668 * possible that we might have address collisions, so make sure that
669 * this hardware address isn't already in use on another bridge.
670 * The first try uses the hostid and falls back to arc4rand().
673 getcredhostid(curthread
->td_ucred
, &hostid
);
675 if (fb
|| hostid
== 0) {
676 arc4rand(sc
->sc_defaddr
, ETHER_ADDR_LEN
, 1);
677 sc
->sc_defaddr
[0] &= ~1;/* clear multicast bit */
678 sc
->sc_defaddr
[0] |= 2; /* set the LAA bit */
680 sc
->sc_defaddr
[0] = 0x2;
681 sc
->sc_defaddr
[1] = (hostid
>> 24) & 0xff;
682 sc
->sc_defaddr
[2] = (hostid
>> 16) & 0xff;
683 sc
->sc_defaddr
[3] = (hostid
>> 8 ) & 0xff;
684 sc
->sc_defaddr
[4] = hostid
& 0xff;
685 sc
->sc_defaddr
[5] = ifp
->if_dunit
& 0xff;
691 LIST_FOREACH(sc2
, &V_bridge_list
, sc_list
) {
693 if (memcmp(sc
->sc_defaddr
,
694 IF_LLADDR(bifp
), ETHER_ADDR_LEN
) == 0) {
699 BRIDGE_LIST_UNLOCK();
700 } while (retry
== 1);
702 bstp_attach(&sc
->sc_stp
, &bridge_ops
);
703 ether_ifattach(ifp
, sc
->sc_defaddr
);
704 /* Now undo some of the damage... */
705 ifp
->if_baudrate
= 0;
706 ifp
->if_type
= IFT_BRIDGE
;
709 LIST_INSERT_HEAD(&V_bridge_list
, sc
, sc_list
);
710 BRIDGE_LIST_UNLOCK();
716 * bridge_clone_destroy:
718 * Destroy a bridge instance.
721 bridge_clone_destroy(struct ifnet
*ifp
)
723 struct bridge_softc
*sc
= ifp
->if_softc
;
724 struct bridge_iflist
*bif
;
729 ifp
->if_flags
&= ~IFF_UP
;
731 while ((bif
= LIST_FIRST(&sc
->sc_iflist
)) != NULL
)
732 bridge_delete_member(sc
, bif
, 0);
734 while ((bif
= LIST_FIRST(&sc
->sc_spanlist
)) != NULL
) {
735 bridge_delete_span(sc
, bif
);
740 callout_drain(&sc
->sc_brcallout
);
743 LIST_REMOVE(sc
, sc_list
);
744 BRIDGE_LIST_UNLOCK();
746 bstp_detach(&sc
->sc_stp
);
750 /* Tear down the routing table. */
751 bridge_rtable_fini(sc
);
753 BRIDGE_LOCK_DESTROY(sc
);
760 * Handle a control request from the operator.
763 bridge_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
765 struct bridge_softc
*sc
= ifp
->if_softc
;
766 struct ifreq
*ifr
= (struct ifreq
*)data
;
767 struct bridge_iflist
*bif
;
768 struct thread
*td
= curthread
;
770 struct ifbreq ifbreq
;
771 struct ifbifconf ifbifconf
;
772 struct ifbareq ifbareq
;
773 struct ifbaconf ifbaconf
;
774 struct ifbrparam ifbrparam
;
775 struct ifbropreq ifbropreq
;
777 struct ifdrv
*ifd
= (struct ifdrv
*) data
;
778 const struct bridge_control
*bc
;
789 if (ifd
->ifd_cmd
>= bridge_control_table_size
) {
793 bc
= &bridge_control_table
[ifd
->ifd_cmd
];
795 if (cmd
== SIOCGDRVSPEC
&&
796 (bc
->bc_flags
& BC_F_COPYOUT
) == 0) {
800 else if (cmd
== SIOCSDRVSPEC
&&
801 (bc
->bc_flags
& BC_F_COPYOUT
) != 0) {
806 if (bc
->bc_flags
& BC_F_SUSER
) {
807 error
= priv_check(td
, PRIV_NET_BRIDGE
);
812 if (ifd
->ifd_len
!= bc
->bc_argsize
||
813 ifd
->ifd_len
> sizeof(args
)) {
818 bzero(&args
, sizeof(args
));
819 if (bc
->bc_flags
& BC_F_COPYIN
) {
820 error
= copyin(ifd
->ifd_data
, &args
, ifd
->ifd_len
);
826 error
= (*bc
->bc_func
)(sc
, &args
);
831 if (bc
->bc_flags
& BC_F_COPYOUT
)
832 error
= copyout(&args
, ifd
->ifd_data
, ifd
->ifd_len
);
837 if (!(ifp
->if_flags
& IFF_UP
) &&
838 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)) {
840 * If interface is marked down and it is running,
841 * then stop and disable it.
846 } else if ((ifp
->if_flags
& IFF_UP
) &&
847 !(ifp
->if_drv_flags
& IFF_DRV_RUNNING
)) {
849 * If interface is marked up and it is stopped, then
857 if (ifr
->ifr_mtu
< 576) {
861 if (LIST_EMPTY(&sc
->sc_iflist
)) {
862 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
866 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
867 if (bif
->bif_ifp
->if_mtu
!= ifr
->ifr_mtu
) {
868 log(LOG_NOTICE
, "%s: invalid MTU: %u(%s)"
869 " != %d\n", sc
->sc_ifp
->if_xname
,
870 bif
->bif_ifp
->if_mtu
,
871 bif
->bif_ifp
->if_xname
, ifr
->ifr_mtu
);
877 sc
->sc_ifp
->if_mtu
= ifr
->ifr_mtu
;
882 * drop the lock as ether_ioctl() will call bridge_start() and
883 * cause the lock to be recursed.
885 error
= ether_ioctl(ifp
, cmd
, data
);
895 * Clear or restore unwanted capabilities on the member interface
898 bridge_mutecaps(struct bridge_softc
*sc
)
900 struct bridge_iflist
*bif
;
903 /* Initial bitmask of capabilities to test */
904 mask
= BRIDGE_IFCAPS_MASK
;
906 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
907 /* Every member must support it or its disabled */
908 mask
&= bif
->bif_savedcaps
;
911 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
912 enabled
= bif
->bif_ifp
->if_capenable
;
913 enabled
&= ~BRIDGE_IFCAPS_STRIP
;
914 /* strip off mask bits and enable them again if allowed */
915 enabled
&= ~BRIDGE_IFCAPS_MASK
;
917 bridge_set_ifcap(sc
, bif
, enabled
);
923 bridge_set_ifcap(struct bridge_softc
*sc
, struct bridge_iflist
*bif
, int set
)
925 struct ifnet
*ifp
= bif
->bif_ifp
;
929 bzero(&ifr
, sizeof(ifr
));
930 ifr
.ifr_reqcap
= set
;
932 if (ifp
->if_capenable
!= set
) {
933 error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFCAP
, (caddr_t
)&ifr
);
935 if_printf(sc
->sc_ifp
,
936 "error setting interface capabilities on %s\n",
942 * bridge_lookup_member:
944 * Lookup a bridge member interface.
946 static struct bridge_iflist
*
947 bridge_lookup_member(struct bridge_softc
*sc
, const char *name
)
949 struct bridge_iflist
*bif
;
952 BRIDGE_LOCK_ASSERT(sc
);
954 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
956 if (strcmp(ifp
->if_xname
, name
) == 0)
964 * bridge_lookup_member_if:
966 * Lookup a bridge member interface by ifnet*.
968 static struct bridge_iflist
*
969 bridge_lookup_member_if(struct bridge_softc
*sc
, struct ifnet
*member_ifp
)
971 struct bridge_iflist
*bif
;
973 BRIDGE_LOCK_ASSERT(sc
);
975 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
976 if (bif
->bif_ifp
== member_ifp
)
984 * bridge_delete_member:
986 * Delete the specified member interface.
989 bridge_delete_member(struct bridge_softc
*sc
, struct bridge_iflist
*bif
,
992 struct ifnet
*ifs
= bif
->bif_ifp
;
993 struct ifnet
*fif
= NULL
;
995 BRIDGE_LOCK_ASSERT(sc
);
997 if (bif
->bif_flags
& IFBIF_STP
)
998 bstp_disable(&bif
->bif_stp
);
1000 ifs
->if_bridge
= NULL
;
1002 LIST_REMOVE(bif
, bif_next
);
1006 * If removing the interface that gave the bridge its mac address, set
1007 * the mac address of the bridge to the address of the next member, or
1008 * to its default address if no members are left.
1010 if (V_bridge_inherit_mac
&& sc
->sc_ifaddr
== ifs
) {
1011 if (LIST_EMPTY(&sc
->sc_iflist
)) {
1012 bcopy(sc
->sc_defaddr
,
1013 IF_LLADDR(sc
->sc_ifp
), ETHER_ADDR_LEN
);
1014 sc
->sc_ifaddr
= NULL
;
1016 fif
= LIST_FIRST(&sc
->sc_iflist
)->bif_ifp
;
1017 bcopy(IF_LLADDR(fif
),
1018 IF_LLADDR(sc
->sc_ifp
), ETHER_ADDR_LEN
);
1019 sc
->sc_ifaddr
= fif
;
1021 EVENTHANDLER_INVOKE(iflladdr_event
, sc
->sc_ifp
);
1024 bridge_linkcheck(sc
);
1025 bridge_mutecaps(sc
); /* recalcuate now this interface is removed */
1026 bridge_rtdelete(sc
, ifs
, IFBF_FLUSHALL
);
1027 KASSERT(bif
->bif_addrcnt
== 0,
1028 ("%s: %d bridge routes referenced", __func__
, bif
->bif_addrcnt
));
1032 switch (ifs
->if_type
) {
1036 * Take the interface out of promiscuous mode, but only
1037 * if it was promiscuous in the first place. It might
1038 * not be if we're in the bridge_ioctl_add() error path.
1040 if (ifs
->if_flags
& IFF_PROMISC
)
1041 (void) ifpromisc(ifs
, 0);
1049 panic("bridge_delete_member: impossible");
1053 /* reneable any interface capabilities */
1054 bridge_set_ifcap(sc
, bif
, bif
->bif_savedcaps
);
1056 bstp_destroy(&bif
->bif_stp
); /* prepare to free */
1058 free(bif
, M_DEVBUF
);
1062 * bridge_delete_span:
1064 * Delete the specified span interface.
1067 bridge_delete_span(struct bridge_softc
*sc
, struct bridge_iflist
*bif
)
1069 BRIDGE_LOCK_ASSERT(sc
);
1071 KASSERT(bif
->bif_ifp
->if_bridge
== NULL
,
1072 ("%s: not a span interface", __func__
));
1074 LIST_REMOVE(bif
, bif_next
);
1075 free(bif
, M_DEVBUF
);
1079 bridge_ioctl_add(struct bridge_softc
*sc
, void *arg
)
1081 struct ifbreq
*req
= arg
;
1082 struct bridge_iflist
*bif
= NULL
;
1086 ifs
= ifunit(req
->ifbr_ifsname
);
1089 if (ifs
->if_ioctl
== NULL
) /* must be supported */
1092 /* If it's in the span list, it can't be a member. */
1093 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1094 if (ifs
== bif
->bif_ifp
)
1097 if (ifs
->if_bridge
== sc
)
1100 if (ifs
->if_bridge
!= NULL
)
1103 switch (ifs
->if_type
) {
1107 /* permitted interface types */
1115 * Two valid inet6 addresses with link-local scope must not be
1116 * on the parent interface and the member interfaces at the
1117 * same time. This restriction is needed to prevent violation
1118 * of link-local scope zone. Attempts to add a member
1119 * interface which has inet6 addresses when the parent has
1120 * inet6 triggers removal of all inet6 addresses on the member
1124 /* Check if the parent interface has a link-local scope addr. */
1125 if (V_allow_llz_overlap
== 0 &&
1126 in6ifa_llaonifp(sc
->sc_ifp
) != NULL
) {
1128 * If any, remove all inet6 addresses from the member
1132 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1133 if (in6ifa_llaonifp(bif
->bif_ifp
)) {
1135 in6_ifdetach(bif
->bif_ifp
);
1137 if_printf(sc
->sc_ifp
,
1138 "IPv6 addresses on %s have been removed "
1139 "before adding it as a member to prevent "
1140 "IPv6 address scope violation.\n",
1141 bif
->bif_ifp
->if_xname
);
1145 if (in6ifa_llaonifp(ifs
)) {
1149 if_printf(sc
->sc_ifp
,
1150 "IPv6 addresses on %s have been removed "
1151 "before adding it as a member to prevent "
1152 "IPv6 address scope violation.\n",
1157 /* Allow the first Ethernet member to define the MTU */
1158 if (LIST_EMPTY(&sc
->sc_iflist
))
1159 sc
->sc_ifp
->if_mtu
= ifs
->if_mtu
;
1160 else if (sc
->sc_ifp
->if_mtu
!= ifs
->if_mtu
) {
1161 if_printf(sc
->sc_ifp
, "invalid MTU: %u(%s) != %u\n",
1162 ifs
->if_mtu
, ifs
->if_xname
, sc
->sc_ifp
->if_mtu
);
1166 bif
= malloc(sizeof(*bif
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
1171 bif
->bif_flags
= IFBIF_LEARNING
| IFBIF_DISCOVER
;
1172 bif
->bif_savedcaps
= ifs
->if_capenable
;
1175 * Assign the interface's MAC address to the bridge if it's the first
1176 * member and the MAC address of the bridge has not been changed from
1177 * the default randomly generated one.
1179 if (V_bridge_inherit_mac
&& LIST_EMPTY(&sc
->sc_iflist
) &&
1180 !memcmp(IF_LLADDR(sc
->sc_ifp
), sc
->sc_defaddr
, ETHER_ADDR_LEN
)) {
1181 bcopy(IF_LLADDR(ifs
), IF_LLADDR(sc
->sc_ifp
), ETHER_ADDR_LEN
);
1182 sc
->sc_ifaddr
= ifs
;
1183 EVENTHANDLER_INVOKE(iflladdr_event
, sc
->sc_ifp
);
1186 ifs
->if_bridge
= sc
;
1187 bstp_create(&sc
->sc_stp
, &bif
->bif_stp
, bif
->bif_ifp
);
1189 * XXX: XLOCK HERE!?!
1191 * NOTE: insert_***HEAD*** should be safe for the traversals.
1193 LIST_INSERT_HEAD(&sc
->sc_iflist
, bif
, bif_next
);
1195 /* Set interface capabilities to the intersection set of all members */
1196 bridge_mutecaps(sc
);
1197 bridge_linkcheck(sc
);
1199 /* Place the interface into promiscuous mode */
1200 switch (ifs
->if_type
) {
1204 error
= ifpromisc(ifs
, 1);
1210 bridge_delete_member(sc
, bif
, 0);
1215 bridge_ioctl_del(struct bridge_softc
*sc
, void *arg
)
1217 struct ifbreq
*req
= arg
;
1218 struct bridge_iflist
*bif
;
1220 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1224 bridge_delete_member(sc
, bif
, 0);
1230 bridge_ioctl_gifflags(struct bridge_softc
*sc
, void *arg
)
1232 struct ifbreq
*req
= arg
;
1233 struct bridge_iflist
*bif
;
1234 struct bstp_port
*bp
;
1236 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1241 req
->ifbr_ifsflags
= bif
->bif_flags
;
1242 req
->ifbr_state
= bp
->bp_state
;
1243 req
->ifbr_priority
= bp
->bp_priority
;
1244 req
->ifbr_path_cost
= bp
->bp_path_cost
;
1245 req
->ifbr_portno
= bif
->bif_ifp
->if_index
& 0xfff;
1246 req
->ifbr_proto
= bp
->bp_protover
;
1247 req
->ifbr_role
= bp
->bp_role
;
1248 req
->ifbr_stpflags
= bp
->bp_flags
;
1249 req
->ifbr_addrcnt
= bif
->bif_addrcnt
;
1250 req
->ifbr_addrmax
= bif
->bif_addrmax
;
1251 req
->ifbr_addrexceeded
= bif
->bif_addrexceeded
;
1253 /* Copy STP state options as flags */
1254 if (bp
->bp_operedge
)
1255 req
->ifbr_ifsflags
|= IFBIF_BSTP_EDGE
;
1256 if (bp
->bp_flags
& BSTP_PORT_AUTOEDGE
)
1257 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOEDGE
;
1258 if (bp
->bp_ptp_link
)
1259 req
->ifbr_ifsflags
|= IFBIF_BSTP_PTP
;
1260 if (bp
->bp_flags
& BSTP_PORT_AUTOPTP
)
1261 req
->ifbr_ifsflags
|= IFBIF_BSTP_AUTOPTP
;
1262 if (bp
->bp_flags
& BSTP_PORT_ADMEDGE
)
1263 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMEDGE
;
1264 if (bp
->bp_flags
& BSTP_PORT_ADMCOST
)
1265 req
->ifbr_ifsflags
|= IFBIF_BSTP_ADMCOST
;
1270 bridge_ioctl_sifflags(struct bridge_softc
*sc
, void *arg
)
1272 struct ifbreq
*req
= arg
;
1273 struct bridge_iflist
*bif
;
1274 struct bstp_port
*bp
;
1277 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1282 if (req
->ifbr_ifsflags
& IFBIF_SPAN
)
1283 /* SPAN is readonly */
1286 if (req
->ifbr_ifsflags
& IFBIF_STP
) {
1287 if ((bif
->bif_flags
& IFBIF_STP
) == 0) {
1288 error
= bstp_enable(&bif
->bif_stp
);
1293 if ((bif
->bif_flags
& IFBIF_STP
) != 0)
1294 bstp_disable(&bif
->bif_stp
);
1297 /* Pass on STP flags */
1298 bstp_set_edge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_EDGE
? 1 : 0);
1299 bstp_set_autoedge(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOEDGE
? 1 : 0);
1300 bstp_set_ptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_PTP
? 1 : 0);
1301 bstp_set_autoptp(bp
, req
->ifbr_ifsflags
& IFBIF_BSTP_AUTOPTP
? 1 : 0);
1303 /* Save the bits relating to the bridge */
1304 bif
->bif_flags
= req
->ifbr_ifsflags
& IFBIFMASK
;
1310 bridge_ioctl_scache(struct bridge_softc
*sc
, void *arg
)
1312 struct ifbrparam
*param
= arg
;
1314 sc
->sc_brtmax
= param
->ifbrp_csize
;
1321 bridge_ioctl_gcache(struct bridge_softc
*sc
, void *arg
)
1323 struct ifbrparam
*param
= arg
;
1325 param
->ifbrp_csize
= sc
->sc_brtmax
;
1331 bridge_ioctl_gifs(struct bridge_softc
*sc
, void *arg
)
1333 struct ifbifconf
*bifc
= arg
;
1334 struct bridge_iflist
*bif
;
1337 int count
, buflen
, len
, error
= 0;
1340 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
)
1342 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1345 buflen
= sizeof(breq
) * count
;
1346 if (bifc
->ifbic_len
== 0) {
1347 bifc
->ifbic_len
= buflen
;
1351 outbuf
= malloc(buflen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1356 len
= min(bifc
->ifbic_len
, buflen
);
1357 bzero(&breq
, sizeof(breq
));
1358 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1359 if (len
< sizeof(breq
))
1362 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
1363 sizeof(breq
.ifbr_ifsname
));
1364 /* Fill in the ifbreq structure */
1365 error
= bridge_ioctl_gifflags(sc
, &breq
);
1368 memcpy(buf
, &breq
, sizeof(breq
));
1370 buf
+= sizeof(breq
);
1371 len
-= sizeof(breq
);
1373 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
1374 if (len
< sizeof(breq
))
1377 strlcpy(breq
.ifbr_ifsname
, bif
->bif_ifp
->if_xname
,
1378 sizeof(breq
.ifbr_ifsname
));
1379 breq
.ifbr_ifsflags
= bif
->bif_flags
;
1380 breq
.ifbr_portno
= bif
->bif_ifp
->if_index
& 0xfff;
1381 memcpy(buf
, &breq
, sizeof(breq
));
1383 buf
+= sizeof(breq
);
1384 len
-= sizeof(breq
);
1388 bifc
->ifbic_len
= sizeof(breq
) * count
;
1389 error
= copyout(outbuf
, bifc
->ifbic_req
, bifc
->ifbic_len
);
1391 free(outbuf
, M_TEMP
);
1396 bridge_ioctl_rts(struct bridge_softc
*sc
, void *arg
)
1398 struct ifbaconf
*bac
= arg
;
1399 struct bridge_rtnode
*brt
;
1400 struct ifbareq bareq
;
1402 int count
, buflen
, len
, error
= 0;
1404 if (bac
->ifbac_len
== 0)
1408 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
)
1410 buflen
= sizeof(bareq
) * count
;
1413 outbuf
= malloc(buflen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1418 len
= min(bac
->ifbac_len
, buflen
);
1419 bzero(&bareq
, sizeof(bareq
));
1420 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
1421 if (len
< sizeof(bareq
))
1423 strlcpy(bareq
.ifba_ifsname
, brt
->brt_ifp
->if_xname
,
1424 sizeof(bareq
.ifba_ifsname
));
1425 memcpy(bareq
.ifba_dst
, brt
->brt_addr
, sizeof(brt
->brt_addr
));
1426 bareq
.ifba_vlan
= brt
->brt_vlan
;
1427 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
1428 time_uptime
< brt
->brt_expire
)
1429 bareq
.ifba_expire
= brt
->brt_expire
- time_uptime
;
1431 bareq
.ifba_expire
= 0;
1432 bareq
.ifba_flags
= brt
->brt_flags
;
1434 memcpy(buf
, &bareq
, sizeof(bareq
));
1436 buf
+= sizeof(bareq
);
1437 len
-= sizeof(bareq
);
1441 bac
->ifbac_len
= sizeof(bareq
) * count
;
1442 error
= copyout(outbuf
, bac
->ifbac_req
, bac
->ifbac_len
);
1444 free(outbuf
, M_TEMP
);
1449 bridge_ioctl_saddr(struct bridge_softc
*sc
, void *arg
)
1451 struct ifbareq
*req
= arg
;
1452 struct bridge_iflist
*bif
;
1455 bif
= bridge_lookup_member(sc
, req
->ifba_ifsname
);
1459 error
= bridge_rtupdate(sc
, req
->ifba_dst
, req
->ifba_vlan
, bif
, 1,
1466 bridge_ioctl_sto(struct bridge_softc
*sc
, void *arg
)
1468 struct ifbrparam
*param
= arg
;
1470 sc
->sc_brttimeout
= param
->ifbrp_ctime
;
1475 bridge_ioctl_gto(struct bridge_softc
*sc
, void *arg
)
1477 struct ifbrparam
*param
= arg
;
1479 param
->ifbrp_ctime
= sc
->sc_brttimeout
;
1484 bridge_ioctl_daddr(struct bridge_softc
*sc
, void *arg
)
1486 struct ifbareq
*req
= arg
;
1488 return (bridge_rtdaddr(sc
, req
->ifba_dst
, req
->ifba_vlan
));
1492 bridge_ioctl_flush(struct bridge_softc
*sc
, void *arg
)
1494 struct ifbreq
*req
= arg
;
1496 bridge_rtflush(sc
, req
->ifbr_ifsflags
);
1501 bridge_ioctl_gpri(struct bridge_softc
*sc
, void *arg
)
1503 struct ifbrparam
*param
= arg
;
1504 struct bstp_state
*bs
= &sc
->sc_stp
;
1506 param
->ifbrp_prio
= bs
->bs_bridge_priority
;
1511 bridge_ioctl_spri(struct bridge_softc
*sc
, void *arg
)
1513 struct ifbrparam
*param
= arg
;
1515 return (bstp_set_priority(&sc
->sc_stp
, param
->ifbrp_prio
));
1519 bridge_ioctl_ght(struct bridge_softc
*sc
, void *arg
)
1521 struct ifbrparam
*param
= arg
;
1522 struct bstp_state
*bs
= &sc
->sc_stp
;
1524 param
->ifbrp_hellotime
= bs
->bs_bridge_htime
>> 8;
1529 bridge_ioctl_sht(struct bridge_softc
*sc
, void *arg
)
1531 struct ifbrparam
*param
= arg
;
1533 return (bstp_set_htime(&sc
->sc_stp
, param
->ifbrp_hellotime
));
1537 bridge_ioctl_gfd(struct bridge_softc
*sc
, void *arg
)
1539 struct ifbrparam
*param
= arg
;
1540 struct bstp_state
*bs
= &sc
->sc_stp
;
1542 param
->ifbrp_fwddelay
= bs
->bs_bridge_fdelay
>> 8;
1547 bridge_ioctl_sfd(struct bridge_softc
*sc
, void *arg
)
1549 struct ifbrparam
*param
= arg
;
1551 return (bstp_set_fdelay(&sc
->sc_stp
, param
->ifbrp_fwddelay
));
1555 bridge_ioctl_gma(struct bridge_softc
*sc
, void *arg
)
1557 struct ifbrparam
*param
= arg
;
1558 struct bstp_state
*bs
= &sc
->sc_stp
;
1560 param
->ifbrp_maxage
= bs
->bs_bridge_max_age
>> 8;
1565 bridge_ioctl_sma(struct bridge_softc
*sc
, void *arg
)
1567 struct ifbrparam
*param
= arg
;
1569 return (bstp_set_maxage(&sc
->sc_stp
, param
->ifbrp_maxage
));
1573 bridge_ioctl_sifprio(struct bridge_softc
*sc
, void *arg
)
1575 struct ifbreq
*req
= arg
;
1576 struct bridge_iflist
*bif
;
1578 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1582 return (bstp_set_port_priority(&bif
->bif_stp
, req
->ifbr_priority
));
1586 bridge_ioctl_sifcost(struct bridge_softc
*sc
, void *arg
)
1588 struct ifbreq
*req
= arg
;
1589 struct bridge_iflist
*bif
;
1591 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1595 return (bstp_set_path_cost(&bif
->bif_stp
, req
->ifbr_path_cost
));
1599 bridge_ioctl_sifmaxaddr(struct bridge_softc
*sc
, void *arg
)
1601 struct ifbreq
*req
= arg
;
1602 struct bridge_iflist
*bif
;
1604 bif
= bridge_lookup_member(sc
, req
->ifbr_ifsname
);
1608 bif
->bif_addrmax
= req
->ifbr_addrmax
;
1613 bridge_ioctl_addspan(struct bridge_softc
*sc
, void *arg
)
1615 struct ifbreq
*req
= arg
;
1616 struct bridge_iflist
*bif
= NULL
;
1619 ifs
= ifunit(req
->ifbr_ifsname
);
1623 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1624 if (ifs
== bif
->bif_ifp
)
1627 if (ifs
->if_bridge
!= NULL
)
1630 switch (ifs
->if_type
) {
1639 bif
= malloc(sizeof(*bif
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
1644 bif
->bif_flags
= IFBIF_SPAN
;
1646 LIST_INSERT_HEAD(&sc
->sc_spanlist
, bif
, bif_next
);
1652 bridge_ioctl_delspan(struct bridge_softc
*sc
, void *arg
)
1654 struct ifbreq
*req
= arg
;
1655 struct bridge_iflist
*bif
;
1658 ifs
= ifunit(req
->ifbr_ifsname
);
1662 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1663 if (ifs
== bif
->bif_ifp
)
1669 bridge_delete_span(sc
, bif
);
1675 bridge_ioctl_gbparam(struct bridge_softc
*sc
, void *arg
)
1677 struct ifbropreq
*req
= arg
;
1678 struct bstp_state
*bs
= &sc
->sc_stp
;
1679 struct bstp_port
*root_port
;
1681 req
->ifbop_maxage
= bs
->bs_bridge_max_age
>> 8;
1682 req
->ifbop_hellotime
= bs
->bs_bridge_htime
>> 8;
1683 req
->ifbop_fwddelay
= bs
->bs_bridge_fdelay
>> 8;
1685 root_port
= bs
->bs_root_port
;
1686 if (root_port
== NULL
)
1687 req
->ifbop_root_port
= 0;
1689 req
->ifbop_root_port
= root_port
->bp_ifp
->if_index
;
1691 req
->ifbop_holdcount
= bs
->bs_txholdcount
;
1692 req
->ifbop_priority
= bs
->bs_bridge_priority
;
1693 req
->ifbop_protocol
= bs
->bs_protover
;
1694 req
->ifbop_root_path_cost
= bs
->bs_root_pv
.pv_cost
;
1695 req
->ifbop_bridgeid
= bs
->bs_bridge_pv
.pv_dbridge_id
;
1696 req
->ifbop_designated_root
= bs
->bs_root_pv
.pv_root_id
;
1697 req
->ifbop_designated_bridge
= bs
->bs_root_pv
.pv_dbridge_id
;
1698 req
->ifbop_last_tc_time
.tv_sec
= bs
->bs_last_tc_time
.tv_sec
;
1699 req
->ifbop_last_tc_time
.tv_usec
= bs
->bs_last_tc_time
.tv_usec
;
1705 bridge_ioctl_grte(struct bridge_softc
*sc
, void *arg
)
1707 struct ifbrparam
*param
= arg
;
1709 param
->ifbrp_cexceeded
= sc
->sc_brtexceeded
;
1714 bridge_ioctl_gifsstp(struct bridge_softc
*sc
, void *arg
)
1716 struct ifbpstpconf
*bifstp
= arg
;
1717 struct bridge_iflist
*bif
;
1718 struct bstp_port
*bp
;
1719 struct ifbpstpreq bpreq
;
1721 int count
, buflen
, len
, error
= 0;
1724 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1725 if ((bif
->bif_flags
& IFBIF_STP
) != 0)
1729 buflen
= sizeof(bpreq
) * count
;
1730 if (bifstp
->ifbpstp_len
== 0) {
1731 bifstp
->ifbpstp_len
= buflen
;
1736 outbuf
= malloc(buflen
, M_TEMP
, M_WAITOK
| M_ZERO
);
1741 len
= min(bifstp
->ifbpstp_len
, buflen
);
1742 bzero(&bpreq
, sizeof(bpreq
));
1743 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
1744 if (len
< sizeof(bpreq
))
1747 if ((bif
->bif_flags
& IFBIF_STP
) == 0)
1751 bpreq
.ifbp_portno
= bif
->bif_ifp
->if_index
& 0xfff;
1752 bpreq
.ifbp_fwd_trans
= bp
->bp_forward_transitions
;
1753 bpreq
.ifbp_design_cost
= bp
->bp_desg_pv
.pv_cost
;
1754 bpreq
.ifbp_design_port
= bp
->bp_desg_pv
.pv_port_id
;
1755 bpreq
.ifbp_design_bridge
= bp
->bp_desg_pv
.pv_dbridge_id
;
1756 bpreq
.ifbp_design_root
= bp
->bp_desg_pv
.pv_root_id
;
1758 memcpy(buf
, &bpreq
, sizeof(bpreq
));
1760 buf
+= sizeof(bpreq
);
1761 len
-= sizeof(bpreq
);
1765 bifstp
->ifbpstp_len
= sizeof(bpreq
) * count
;
1766 error
= copyout(outbuf
, bifstp
->ifbpstp_req
, bifstp
->ifbpstp_len
);
1768 free(outbuf
, M_TEMP
);
1773 bridge_ioctl_sproto(struct bridge_softc
*sc
, void *arg
)
1775 struct ifbrparam
*param
= arg
;
1777 return (bstp_set_protocol(&sc
->sc_stp
, param
->ifbrp_proto
));
1781 bridge_ioctl_stxhc(struct bridge_softc
*sc
, void *arg
)
1783 struct ifbrparam
*param
= arg
;
1785 return (bstp_set_holdcount(&sc
->sc_stp
, param
->ifbrp_txhc
));
1791 * Detach an interface from a bridge. Called when a member
1792 * interface is detaching.
1795 bridge_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
1797 struct bridge_softc
*sc
= ifp
->if_bridge
;
1798 struct bridge_iflist
*bif
;
1800 if (ifp
->if_flags
& IFF_RENAMING
)
1802 if (V_bridge_cloner
== NULL
) {
1804 * This detach handler can be called after
1805 * vnet_bridge_uninit(). Just return in that case.
1809 /* Check if the interface is a bridge member */
1813 bif
= bridge_lookup_member_if(sc
, ifp
);
1815 bridge_delete_member(sc
, bif
, 1);
1821 /* Check if the interface is a span port */
1823 LIST_FOREACH(sc
, &V_bridge_list
, sc_list
) {
1825 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
)
1826 if (ifp
== bif
->bif_ifp
) {
1827 bridge_delete_span(sc
, bif
);
1833 BRIDGE_LIST_UNLOCK();
1839 * Initialize a bridge interface.
1842 bridge_init(void *xsc
)
1844 struct bridge_softc
*sc
= (struct bridge_softc
*)xsc
;
1845 struct ifnet
*ifp
= sc
->sc_ifp
;
1847 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1851 callout_reset(&sc
->sc_brcallout
, bridge_rtable_prune_period
* hz
,
1854 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1855 bstp_init(&sc
->sc_stp
); /* Initialize Spanning Tree */
1863 * Stop the bridge interface.
1866 bridge_stop(struct ifnet
*ifp
, int disable
)
1868 struct bridge_softc
*sc
= ifp
->if_softc
;
1870 BRIDGE_LOCK_ASSERT(sc
);
1872 if ((ifp
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
1875 callout_stop(&sc
->sc_brcallout
);
1876 bstp_stop(&sc
->sc_stp
);
1878 bridge_rtflush(sc
, IFBF_FLUSHDYN
);
1880 ifp
->if_drv_flags
&= ~IFF_DRV_RUNNING
;
1886 * Enqueue a packet on a bridge member interface.
1890 bridge_enqueue(struct bridge_softc
*sc
, struct ifnet
*dst_ifp
, struct mbuf
*m
)
1896 /* We may be sending a fragment so traverse the mbuf */
1899 m
->m_nextpkt
= NULL
;
1900 len
= m
->m_pkthdr
.len
;
1901 mflags
= m
->m_flags
;
1904 * If underlying interface can not do VLAN tag insertion itself
1905 * then attach a packet tag that holds it.
1907 if ((m
->m_flags
& M_VLANTAG
) &&
1908 (dst_ifp
->if_capenable
& IFCAP_VLAN_HWTAGGING
) == 0) {
1909 m
= ether_vlanencap(m
, m
->m_pkthdr
.ether_vtag
);
1912 "unable to prepend VLAN header\n");
1913 if_inc_counter(dst_ifp
, IFCOUNTER_OERRORS
, 1);
1916 m
->m_flags
&= ~M_VLANTAG
;
1919 if ((err
= dst_ifp
->if_transmit(dst_ifp
, m
))) {
1921 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OERRORS
, 1);
1925 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OPACKETS
, 1);
1926 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OBYTES
, len
);
1927 if (mflags
& M_MCAST
)
1928 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OMCASTS
, 1);
1937 * Receive a queued packet from dummynet and pass it on to the output
1940 * The mbuf has the Ethernet header already attached.
1943 bridge_dummynet(struct mbuf
*m
, struct ifnet
*ifp
)
1945 struct bridge_softc
*sc
;
1947 sc
= ifp
->if_bridge
;
1950 * The packet didnt originate from a member interface. This should only
1951 * ever happen if a member interface is removed while packets are
1959 if (PFIL_HOOKED(&V_inet_pfil_hook
)
1961 || PFIL_HOOKED(&V_inet6_pfil_hook
)
1964 if (bridge_pfil(&m
, sc
->sc_ifp
, ifp
, PFIL_OUT
) != 0)
1970 bridge_enqueue(sc
, ifp
, m
);
1976 * Send output from a bridge member interface. This
1977 * performs the bridging function for locally originated
1980 * The mbuf has the Ethernet header already attached. We must
1981 * enqueue or free the mbuf before returning.
1984 bridge_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*sa
,
1987 struct ether_header
*eh
;
1988 struct ifnet
*dst_if
;
1989 struct bridge_softc
*sc
;
1992 if (m
->m_len
< ETHER_HDR_LEN
) {
1993 m
= m_pullup(m
, ETHER_HDR_LEN
);
1998 eh
= mtod(m
, struct ether_header
*);
1999 sc
= ifp
->if_bridge
;
2000 vlan
= VLANTAGOF(m
);
2005 * If bridge is down, but the original output interface is up,
2006 * go ahead and send out that interface. Otherwise, the packet
2009 if ((sc
->sc_ifp
->if_drv_flags
& IFF_DRV_RUNNING
) == 0) {
2015 * If the packet is a multicast, or we don't know a better way to
2016 * get there, send to all interfaces.
2018 if (ETHER_IS_MULTICAST(eh
->ether_dhost
))
2021 dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, vlan
);
2022 if (dst_if
== NULL
) {
2023 struct bridge_iflist
*bif
;
2025 int error
= 0, used
= 0;
2029 BRIDGE_LOCK2REF(sc
, error
);
2035 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
2036 dst_if
= bif
->bif_ifp
;
2038 if (dst_if
->if_type
== IFT_GIF
)
2040 if ((dst_if
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
2044 * If this is not the original output interface,
2045 * and the interface is participating in spanning
2046 * tree, make sure the port is in a state that
2047 * allows forwarding.
2049 if (dst_if
!= ifp
&& (bif
->bif_flags
& IFBIF_STP
) &&
2050 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
2053 if (LIST_NEXT(bif
, bif_next
) == NULL
) {
2057 mc
= m_copypacket(m
, M_NOWAIT
);
2059 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OERRORS
, 1);
2064 bridge_enqueue(sc
, dst_if
, mc
);
2074 * XXX Spanning tree consideration here?
2078 if ((dst_if
->if_drv_flags
& IFF_DRV_RUNNING
) == 0) {
2085 bridge_enqueue(sc
, dst_if
, m
);
2092 * Do output on a bridge.
2096 bridge_transmit(struct ifnet
*ifp
, struct mbuf
*m
)
2098 struct bridge_softc
*sc
;
2099 struct ether_header
*eh
;
2100 struct ifnet
*dst_if
;
2105 ETHER_BPF_MTAP(ifp
, m
);
2107 eh
= mtod(m
, struct ether_header
*);
2110 if (((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) &&
2111 (dst_if
= bridge_rtlookup(sc
, eh
->ether_dhost
, 1)) != NULL
) {
2113 error
= bridge_enqueue(sc
, dst_if
, m
);
2115 bridge_broadcast(sc
, ifp
, m
, 0);
2121 * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2124 bridge_qflush(struct ifnet
*ifp __unused
)
2131 * The forwarding function of the bridge.
2133 * NOTE: Releases the lock on return.
2136 bridge_forward(struct bridge_softc
*sc
, struct bridge_iflist
*sbif
,
2139 struct bridge_iflist
*dbif
;
2140 struct ifnet
*src_if
, *dst_if
, *ifp
;
2141 struct ether_header
*eh
;
2146 src_if
= m
->m_pkthdr
.rcvif
;
2149 if_inc_counter(ifp
, IFCOUNTER_IPACKETS
, 1);
2150 if_inc_counter(ifp
, IFCOUNTER_IBYTES
, m
->m_pkthdr
.len
);
2151 vlan
= VLANTAGOF(m
);
2153 if ((sbif
->bif_flags
& IFBIF_STP
) &&
2154 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
2157 eh
= mtod(m
, struct ether_header
*);
2158 dst
= eh
->ether_dhost
;
2160 /* If the interface is learning, record the address. */
2161 if (sbif
->bif_flags
& IFBIF_LEARNING
) {
2162 error
= bridge_rtupdate(sc
, eh
->ether_shost
, vlan
,
2163 sbif
, 0, IFBAF_DYNAMIC
);
2165 * If the interface has addresses limits then deny any source
2166 * that is not in the cache.
2168 if (error
&& sbif
->bif_addrmax
)
2172 if ((sbif
->bif_flags
& IFBIF_STP
) != 0 &&
2173 sbif
->bif_stp
.bp_state
== BSTP_IFSTATE_LEARNING
)
2177 * At this point, the port either doesn't participate
2178 * in spanning tree or it is in the forwarding state.
2182 * If the packet is unicast, destined for someone on
2183 * "this" side of the bridge, drop it.
2185 if ((m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0) {
2186 dst_if
= bridge_rtlookup(sc
, dst
, vlan
);
2187 if (src_if
== dst_if
)
2191 * Check if its a reserved multicast address, any address
2192 * listed in 802.1D section 7.12.6 may not be forwarded by the
2194 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2196 if (dst
[0] == 0x01 && dst
[1] == 0x80 &&
2197 dst
[2] == 0xc2 && dst
[3] == 0x00 &&
2198 dst
[4] == 0x00 && dst
[5] <= 0x0f)
2201 /* ...forward it to all interfaces. */
2202 if_inc_counter(ifp
, IFCOUNTER_IMCASTS
, 1);
2207 * If we have a destination interface which is a member of our bridge,
2208 * OR this is a unicast packet, push it through the bpf(4) machinery.
2209 * For broadcast or multicast packets, don't bother because it will
2210 * be reinjected into ether_input. We do this before we pass the packets
2211 * through the pfil(9) framework, as it is possible that pfil(9) will
2212 * drop the packet, or possibly modify it, making it difficult to debug
2213 * firewall issues on the bridge.
2215 if (dst_if
!= NULL
|| (m
->m_flags
& (M_BCAST
| M_MCAST
)) == 0)
2216 ETHER_BPF_MTAP(ifp
, m
);
2218 /* run the packet filter */
2219 if (PFIL_HOOKED(&V_inet_pfil_hook
)
2221 || PFIL_HOOKED(&V_inet6_pfil_hook
)
2225 if (bridge_pfil(&m
, ifp
, src_if
, PFIL_IN
) != 0)
2232 if (dst_if
== NULL
) {
2233 bridge_broadcast(sc
, src_if
, m
, 1);
2238 * At this point, we're dealing with a unicast frame
2239 * going to a different interface.
2241 if ((dst_if
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
2244 dbif
= bridge_lookup_member_if(sc
, dst_if
);
2246 /* Not a member of the bridge (anymore?) */
2249 /* Private segments can not talk to each other */
2250 if (sbif
->bif_flags
& dbif
->bif_flags
& IFBIF_PRIVATE
)
2253 if ((dbif
->bif_flags
& IFBIF_STP
) &&
2254 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
2259 if (PFIL_HOOKED(&V_inet_pfil_hook
)
2261 || PFIL_HOOKED(&V_inet6_pfil_hook
)
2264 if (bridge_pfil(&m
, ifp
, dst_if
, PFIL_OUT
) != 0)
2270 bridge_enqueue(sc
, dst_if
, m
);
2281 * Receive input from a member interface. Queue the packet for
2282 * bridging if it is not for us.
2284 static struct mbuf
*
2285 bridge_input(struct ifnet
*ifp
, struct mbuf
*m
)
2287 struct bridge_softc
*sc
= ifp
->if_bridge
;
2288 struct bridge_iflist
*bif
, *bif2
;
2290 struct ether_header
*eh
;
2291 struct mbuf
*mc
, *mc2
;
2295 if ((sc
->sc_ifp
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
2299 vlan
= VLANTAGOF(m
);
2302 * Implement support for bridge monitoring. If this flag has been
2303 * set on this interface, discard the packet once we push it through
2304 * the bpf(4) machinery, but before we do, increment the byte and
2305 * packet counters associated with this interface.
2307 if ((bifp
->if_flags
& IFF_MONITOR
) != 0) {
2308 m
->m_pkthdr
.rcvif
= bifp
;
2309 ETHER_BPF_MTAP(bifp
, m
);
2310 if_inc_counter(bifp
, IFCOUNTER_IPACKETS
, 1);
2311 if_inc_counter(bifp
, IFCOUNTER_IBYTES
, m
->m_pkthdr
.len
);
2316 bif
= bridge_lookup_member_if(sc
, ifp
);
2322 eh
= mtod(m
, struct ether_header
*);
2326 if (m
->m_flags
& (M_BCAST
|M_MCAST
)) {
2327 /* Tap off 802.1D packets; they do not get forwarded. */
2328 if (memcmp(eh
->ether_dhost
, bstp_etheraddr
,
2329 ETHER_ADDR_LEN
) == 0) {
2330 bstp_input(&bif
->bif_stp
, ifp
, m
); /* consumes mbuf */
2335 if ((bif
->bif_flags
& IFBIF_STP
) &&
2336 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
2342 * Make a deep copy of the packet and enqueue the copy
2343 * for bridge processing; return the original packet for
2346 mc
= m_dup(m
, M_NOWAIT
);
2352 /* Perform the bridge forwarding function with the copy. */
2353 bridge_forward(sc
, bif
, mc
);
2356 * Reinject the mbuf as arriving on the bridge so we have a
2357 * chance at claiming multicast packets. We can not loop back
2358 * here from ether_input as a bridge is never a member of a
2361 KASSERT(bifp
->if_bridge
== NULL
,
2362 ("loop created in bridge_input"));
2363 mc2
= m_dup(m
, M_NOWAIT
);
2365 /* Keep the layer3 header aligned */
2366 int i
= min(mc2
->m_pkthdr
.len
, max_protohdr
);
2367 mc2
= m_copyup(mc2
, i
, ETHER_ALIGN
);
2370 mc2
->m_pkthdr
.rcvif
= bifp
;
2371 (*bifp
->if_input
)(bifp
, mc2
);
2374 /* Return the original packet for local processing. */
2378 if ((bif
->bif_flags
& IFBIF_STP
) &&
2379 bif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
) {
2384 #if (defined(INET) || defined(INET6))
2385 # define OR_CARP_CHECK_WE_ARE_DST(iface) \
2386 || ((iface)->if_carp \
2387 && (*carp_forus_p)((iface), eh->ether_dhost))
2388 # define OR_CARP_CHECK_WE_ARE_SRC(iface) \
2389 || ((iface)->if_carp \
2390 && (*carp_forus_p)((iface), eh->ether_shost))
2392 # define OR_CARP_CHECK_WE_ARE_DST(iface)
2393 # define OR_CARP_CHECK_WE_ARE_SRC(iface)
2397 # define OR_PFIL_HOOKED_INET6 \
2398 || PFIL_HOOKED(&V_inet6_pfil_hook)
2400 # define OR_PFIL_HOOKED_INET6
2403 #define GRAB_OUR_PACKETS(iface) \
2404 if ((iface)->if_type == IFT_GIF) \
2406 /* It is destined for us. */ \
2407 if (memcmp(IF_LLADDR((iface)), eh->ether_dhost, ETHER_ADDR_LEN) == 0 \
2408 OR_CARP_CHECK_WE_ARE_DST((iface)) \
2410 if ((iface)->if_type == IFT_BRIDGE) { \
2411 ETHER_BPF_MTAP(iface, m); \
2412 if_inc_counter(iface, IFCOUNTER_IPACKETS, 1); \
2413 if_inc_counter(iface, IFCOUNTER_IBYTES, m->m_pkthdr.len); \
2414 /* Filter on the physical interface. */ \
2415 if (V_pfil_local_phys && \
2416 (PFIL_HOOKED(&V_inet_pfil_hook) \
2417 OR_PFIL_HOOKED_INET6)) { \
2418 if (bridge_pfil(&m, NULL, ifp, \
2419 PFIL_IN) != 0 || m == NULL) { \
2420 BRIDGE_UNLOCK(sc); \
2423 eh = mtod(m, struct ether_header *); \
2426 if (bif->bif_flags & IFBIF_LEARNING) { \
2427 error = bridge_rtupdate(sc, eh->ether_shost, \
2428 vlan, bif, 0, IFBAF_DYNAMIC); \
2429 if (error && bif->bif_addrmax) { \
2430 BRIDGE_UNLOCK(sc); \
2435 m->m_pkthdr.rcvif = iface; \
2436 BRIDGE_UNLOCK(sc); \
2440 /* We just received a packet that we sent out. */ \
2441 if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
2442 OR_CARP_CHECK_WE_ARE_SRC((iface)) \
2444 BRIDGE_UNLOCK(sc); \
2450 * Unicast. Make sure it's not for the bridge.
2452 do { GRAB_OUR_PACKETS(bifp
) } while (0);
2455 * Give a chance for ifp at first priority. This will help when the
2456 * packet comes through the interface like VLAN's with the same MACs
2457 * on several interfaces from the same bridge. This also will save
2458 * some CPU cycles in case the destination interface and the input
2459 * interface (eq ifp) are the same.
2461 do { GRAB_OUR_PACKETS(ifp
) } while (0);
2463 /* Now check the all bridge members. */
2464 LIST_FOREACH(bif2
, &sc
->sc_iflist
, bif_next
) {
2465 GRAB_OUR_PACKETS(bif2
->bif_ifp
)
2468 #undef OR_CARP_CHECK_WE_ARE_DST
2469 #undef OR_CARP_CHECK_WE_ARE_SRC
2470 #undef OR_PFIL_HOOKED_INET6
2471 #undef GRAB_OUR_PACKETS
2473 /* Perform the bridge forwarding function. */
2474 bridge_forward(sc
, bif
, m
);
2482 * Send a frame to all interfaces that are members of
2483 * the bridge, except for the one on which the packet
2486 * NOTE: Releases the lock on return.
2489 bridge_broadcast(struct bridge_softc
*sc
, struct ifnet
*src_if
,
2490 struct mbuf
*m
, int runfilt
)
2492 struct bridge_iflist
*dbif
, *sbif
;
2494 struct ifnet
*dst_if
;
2495 int error
= 0, used
= 0, i
;
2497 sbif
= bridge_lookup_member_if(sc
, src_if
);
2499 BRIDGE_LOCK2REF(sc
, error
);
2505 /* Filter on the bridge interface before broadcasting */
2506 if (runfilt
&& (PFIL_HOOKED(&V_inet_pfil_hook
)
2508 || PFIL_HOOKED(&V_inet6_pfil_hook
)
2511 if (bridge_pfil(&m
, sc
->sc_ifp
, NULL
, PFIL_OUT
) != 0)
2517 LIST_FOREACH(dbif
, &sc
->sc_iflist
, bif_next
) {
2518 dst_if
= dbif
->bif_ifp
;
2519 if (dst_if
== src_if
)
2522 /* Private segments can not talk to each other */
2523 if (sbif
&& (sbif
->bif_flags
& dbif
->bif_flags
& IFBIF_PRIVATE
))
2526 if ((dbif
->bif_flags
& IFBIF_STP
) &&
2527 dbif
->bif_stp
.bp_state
== BSTP_IFSTATE_DISCARDING
)
2530 if ((dbif
->bif_flags
& IFBIF_DISCOVER
) == 0 &&
2531 (m
->m_flags
& (M_BCAST
|M_MCAST
)) == 0)
2534 if ((dst_if
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
2537 if (LIST_NEXT(dbif
, bif_next
) == NULL
) {
2541 mc
= m_dup(m
, M_NOWAIT
);
2543 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OERRORS
, 1);
2549 * Filter on the output interface. Pass a NULL bridge interface
2550 * pointer so we do not redundantly filter on the bridge for
2551 * each interface we broadcast on.
2553 if (runfilt
&& (PFIL_HOOKED(&V_inet_pfil_hook
)
2555 || PFIL_HOOKED(&V_inet6_pfil_hook
)
2559 /* Keep the layer3 header aligned */
2560 i
= min(mc
->m_pkthdr
.len
, max_protohdr
);
2561 mc
= m_copyup(mc
, i
, ETHER_ALIGN
);
2563 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OERRORS
, 1);
2567 if (bridge_pfil(&mc
, NULL
, dst_if
, PFIL_OUT
) != 0)
2573 bridge_enqueue(sc
, dst_if
, mc
);
2585 * Duplicate a packet out one or more interfaces that are in span mode,
2586 * the original mbuf is unmodified.
2589 bridge_span(struct bridge_softc
*sc
, struct mbuf
*m
)
2591 struct bridge_iflist
*bif
;
2592 struct ifnet
*dst_if
;
2595 if (LIST_EMPTY(&sc
->sc_spanlist
))
2598 LIST_FOREACH(bif
, &sc
->sc_spanlist
, bif_next
) {
2599 dst_if
= bif
->bif_ifp
;
2601 if ((dst_if
->if_drv_flags
& IFF_DRV_RUNNING
) == 0)
2604 mc
= m_copypacket(m
, M_NOWAIT
);
2606 if_inc_counter(sc
->sc_ifp
, IFCOUNTER_OERRORS
, 1);
2610 bridge_enqueue(sc
, dst_if
, mc
);
2617 * Add a bridge routing entry.
2620 bridge_rtupdate(struct bridge_softc
*sc
, const uint8_t *dst
, uint16_t vlan
,
2621 struct bridge_iflist
*bif
, int setflags
, uint8_t flags
)
2623 struct bridge_rtnode
*brt
;
2626 BRIDGE_LOCK_ASSERT(sc
);
2628 /* Check the source address is valid and not multicast. */
2629 if (ETHER_IS_MULTICAST(dst
) ||
2630 (dst
[0] == 0 && dst
[1] == 0 && dst
[2] == 0 &&
2631 dst
[3] == 0 && dst
[4] == 0 && dst
[5] == 0) != 0)
2634 /* 802.1p frames map to vlan 1 */
2639 * A route for this destination might already exist. If so,
2640 * update it, otherwise create a new one.
2642 if ((brt
= bridge_rtnode_lookup(sc
, dst
, vlan
)) == NULL
) {
2643 if (sc
->sc_brtcnt
>= sc
->sc_brtmax
) {
2644 sc
->sc_brtexceeded
++;
2647 /* Check per interface address limits (if enabled) */
2648 if (bif
->bif_addrmax
&& bif
->bif_addrcnt
>= bif
->bif_addrmax
) {
2649 bif
->bif_addrexceeded
++;
2654 * Allocate a new bridge forwarding node, and
2655 * initialize the expiration time and Ethernet
2658 brt
= uma_zalloc(bridge_rtnode_zone
, M_NOWAIT
| M_ZERO
);
2662 if (bif
->bif_flags
& IFBIF_STICKY
)
2663 brt
->brt_flags
= IFBAF_STICKY
;
2665 brt
->brt_flags
= IFBAF_DYNAMIC
;
2667 memcpy(brt
->brt_addr
, dst
, ETHER_ADDR_LEN
);
2668 brt
->brt_vlan
= vlan
;
2670 if ((error
= bridge_rtnode_insert(sc
, brt
)) != 0) {
2671 uma_zfree(bridge_rtnode_zone
, brt
);
2678 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
&&
2679 brt
->brt_dst
!= bif
) {
2680 brt
->brt_dst
->bif_addrcnt
--;
2682 brt
->brt_dst
->bif_addrcnt
++;
2685 if ((flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2686 brt
->brt_expire
= time_uptime
+ sc
->sc_brttimeout
;
2688 brt
->brt_flags
= flags
;
2696 * Lookup the destination interface for an address.
2698 static struct ifnet
*
2699 bridge_rtlookup(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
2701 struct bridge_rtnode
*brt
;
2703 BRIDGE_LOCK_ASSERT(sc
);
2705 if ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) == NULL
)
2708 return (brt
->brt_ifp
);
2714 * Trim the routine table so that we have a number
2715 * of routing entries less than or equal to the
2719 bridge_rttrim(struct bridge_softc
*sc
)
2721 struct bridge_rtnode
*brt
, *nbrt
;
2723 BRIDGE_LOCK_ASSERT(sc
);
2725 /* Make sure we actually need to do this. */
2726 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2729 /* Force an aging cycle; this might trim enough addresses. */
2731 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2734 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
2735 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2736 bridge_rtnode_destroy(sc
, brt
);
2737 if (sc
->sc_brtcnt
<= sc
->sc_brtmax
)
2746 * Aging timer for the bridge.
2749 bridge_timer(void *arg
)
2751 struct bridge_softc
*sc
= arg
;
2753 BRIDGE_LOCK_ASSERT(sc
);
2757 if (sc
->sc_ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
2758 callout_reset(&sc
->sc_brcallout
,
2759 bridge_rtable_prune_period
* hz
, bridge_timer
, sc
);
2765 * Perform an aging cycle.
2768 bridge_rtage(struct bridge_softc
*sc
)
2770 struct bridge_rtnode
*brt
, *nbrt
;
2772 BRIDGE_LOCK_ASSERT(sc
);
2774 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
2775 if ((brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
) {
2776 if (time_uptime
>= brt
->brt_expire
)
2777 bridge_rtnode_destroy(sc
, brt
);
2785 * Remove all dynamic addresses from the bridge.
2788 bridge_rtflush(struct bridge_softc
*sc
, int full
)
2790 struct bridge_rtnode
*brt
, *nbrt
;
2792 BRIDGE_LOCK_ASSERT(sc
);
2794 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
2795 if (full
|| (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
2796 bridge_rtnode_destroy(sc
, brt
);
2803 * Remove an address from the table.
2806 bridge_rtdaddr(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
2808 struct bridge_rtnode
*brt
;
2811 BRIDGE_LOCK_ASSERT(sc
);
2814 * If vlan is zero then we want to delete for all vlans so the lookup
2815 * may return more than one.
2817 while ((brt
= bridge_rtnode_lookup(sc
, addr
, vlan
)) != NULL
) {
2818 bridge_rtnode_destroy(sc
, brt
);
2822 return (found
? 0 : ENOENT
);
2828 * Delete routes to a speicifc member interface.
2831 bridge_rtdelete(struct bridge_softc
*sc
, struct ifnet
*ifp
, int full
)
2833 struct bridge_rtnode
*brt
, *nbrt
;
2835 BRIDGE_LOCK_ASSERT(sc
);
2837 LIST_FOREACH_SAFE(brt
, &sc
->sc_rtlist
, brt_list
, nbrt
) {
2838 if (brt
->brt_ifp
== ifp
&& (full
||
2839 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
))
2840 bridge_rtnode_destroy(sc
, brt
);
2845 * bridge_rtable_init:
2847 * Initialize the route table for this bridge.
2850 bridge_rtable_init(struct bridge_softc
*sc
)
2854 sc
->sc_rthash
= malloc(sizeof(*sc
->sc_rthash
) * BRIDGE_RTHASH_SIZE
,
2855 M_DEVBUF
, M_WAITOK
);
2857 for (i
= 0; i
< BRIDGE_RTHASH_SIZE
; i
++)
2858 LIST_INIT(&sc
->sc_rthash
[i
]);
2860 sc
->sc_rthash_key
= arc4random();
2861 LIST_INIT(&sc
->sc_rtlist
);
2865 * bridge_rtable_fini:
2867 * Deconstruct the route table for this bridge.
2870 bridge_rtable_fini(struct bridge_softc
*sc
)
2873 KASSERT(sc
->sc_brtcnt
== 0,
2874 ("%s: %d bridge routes referenced", __func__
, sc
->sc_brtcnt
));
2875 free(sc
->sc_rthash
, M_DEVBUF
);
2879 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2880 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2882 #define mix(a, b, c) \
2884 a -= b; a -= c; a ^= (c >> 13); \
2885 b -= c; b -= a; b ^= (a << 8); \
2886 c -= a; c -= b; c ^= (b >> 13); \
2887 a -= b; a -= c; a ^= (c >> 12); \
2888 b -= c; b -= a; b ^= (a << 16); \
2889 c -= a; c -= b; c ^= (b >> 5); \
2890 a -= b; a -= c; a ^= (c >> 3); \
2891 b -= c; b -= a; b ^= (a << 10); \
2892 c -= a; c -= b; c ^= (b >> 15); \
2893 } while (/*CONSTCOND*/0)
2895 static __inline
uint32_t
2896 bridge_rthash(struct bridge_softc
*sc
, const uint8_t *addr
)
2898 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= sc
->sc_rthash_key
;
2909 return (c
& BRIDGE_RTHASH_MASK
);
2915 bridge_rtnode_addr_cmp(const uint8_t *a
, const uint8_t *b
)
2919 for (i
= 0, d
= 0; i
< ETHER_ADDR_LEN
&& d
== 0; i
++) {
2920 d
= ((int)a
[i
]) - ((int)b
[i
]);
2927 * bridge_rtnode_lookup:
2929 * Look up a bridge route node for the specified destination. Compare the
2930 * vlan id or if zero then just return the first match.
2932 static struct bridge_rtnode
*
2933 bridge_rtnode_lookup(struct bridge_softc
*sc
, const uint8_t *addr
, uint16_t vlan
)
2935 struct bridge_rtnode
*brt
;
2939 BRIDGE_LOCK_ASSERT(sc
);
2941 hash
= bridge_rthash(sc
, addr
);
2942 LIST_FOREACH(brt
, &sc
->sc_rthash
[hash
], brt_hash
) {
2943 dir
= bridge_rtnode_addr_cmp(addr
, brt
->brt_addr
);
2944 if (dir
== 0 && (brt
->brt_vlan
== vlan
|| vlan
== 0))
2954 * bridge_rtnode_insert:
2956 * Insert the specified bridge node into the route table. We
2957 * assume the entry is not already in the table.
2960 bridge_rtnode_insert(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
2962 struct bridge_rtnode
*lbrt
;
2966 BRIDGE_LOCK_ASSERT(sc
);
2968 hash
= bridge_rthash(sc
, brt
->brt_addr
);
2970 lbrt
= LIST_FIRST(&sc
->sc_rthash
[hash
]);
2972 LIST_INSERT_HEAD(&sc
->sc_rthash
[hash
], brt
, brt_hash
);
2977 dir
= bridge_rtnode_addr_cmp(brt
->brt_addr
, lbrt
->brt_addr
);
2978 if (dir
== 0 && brt
->brt_vlan
== lbrt
->brt_vlan
)
2981 LIST_INSERT_BEFORE(lbrt
, brt
, brt_hash
);
2984 if (LIST_NEXT(lbrt
, brt_hash
) == NULL
) {
2985 LIST_INSERT_AFTER(lbrt
, brt
, brt_hash
);
2988 lbrt
= LIST_NEXT(lbrt
, brt_hash
);
2989 } while (lbrt
!= NULL
);
2992 panic("bridge_rtnode_insert: impossible");
2996 LIST_INSERT_HEAD(&sc
->sc_rtlist
, brt
, brt_list
);
3003 * bridge_rtnode_destroy:
3005 * Destroy a bridge rtnode.
3008 bridge_rtnode_destroy(struct bridge_softc
*sc
, struct bridge_rtnode
*brt
)
3010 BRIDGE_LOCK_ASSERT(sc
);
3012 LIST_REMOVE(brt
, brt_hash
);
3014 LIST_REMOVE(brt
, brt_list
);
3016 brt
->brt_dst
->bif_addrcnt
--;
3017 uma_zfree(bridge_rtnode_zone
, brt
);
3021 * bridge_rtable_expire:
3023 * Set the expiry time for all routes on an interface.
3026 bridge_rtable_expire(struct ifnet
*ifp
, int age
)
3028 struct bridge_softc
*sc
= ifp
->if_bridge
;
3029 struct bridge_rtnode
*brt
;
3034 * If the age is zero then flush, otherwise set all the expiry times to
3035 * age for the interface
3038 bridge_rtdelete(sc
, ifp
, IFBF_FLUSHDYN
);
3040 LIST_FOREACH(brt
, &sc
->sc_rtlist
, brt_list
) {
3041 /* Cap the expiry time to 'age' */
3042 if (brt
->brt_ifp
== ifp
&&
3043 brt
->brt_expire
> time_uptime
+ age
&&
3044 (brt
->brt_flags
& IFBAF_TYPEMASK
) == IFBAF_DYNAMIC
)
3045 brt
->brt_expire
= time_uptime
+ age
;
3052 * bridge_state_change:
3054 * Callback from the bridgestp code when a port changes states.
3057 bridge_state_change(struct ifnet
*ifp
, int state
)
3059 struct bridge_softc
*sc
= ifp
->if_bridge
;
3060 static const char *stpstates
[] = {
3069 CURVNET_SET(ifp
->if_vnet
);
3071 log(LOG_NOTICE
, "%s: state changed to %s on %s\n",
3072 sc
->sc_ifp
->if_xname
, stpstates
[state
], ifp
->if_xname
);
3077 * Send bridge packets through pfil if they are one of the types pfil can deal
3078 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3079 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3083 bridge_pfil(struct mbuf
**mp
, struct ifnet
*bifp
, struct ifnet
*ifp
, int dir
)
3085 int snap
, error
, i
, hlen
;
3086 struct ether_header
*eh1
, eh2
;
3089 u_int16_t ether_type
;
3092 error
= -1; /* Default error if not error == 0 */
3095 /* we may return with the IP fields swapped, ensure its not shared */
3096 KASSERT(M_WRITABLE(*mp
), ("%s: modifying a shared mbuf", __func__
));
3099 if (V_pfil_bridge
== 0 && V_pfil_member
== 0 && V_pfil_ipfw
== 0)
3100 return (0); /* filtering is disabled */
3102 i
= min((*mp
)->m_pkthdr
.len
, max_protohdr
);
3103 if ((*mp
)->m_len
< i
) {
3104 *mp
= m_pullup(*mp
, i
);
3106 printf("%s: m_pullup failed\n", __func__
);
3111 eh1
= mtod(*mp
, struct ether_header
*);
3112 ether_type
= ntohs(eh1
->ether_type
);
3115 * Check for SNAP/LLC.
3117 if (ether_type
< ETHERMTU
) {
3118 struct llc
*llc2
= (struct llc
*)(eh1
+ 1);
3120 if ((*mp
)->m_len
>= ETHER_HDR_LEN
+ 8 &&
3121 llc2
->llc_dsap
== LLC_SNAP_LSAP
&&
3122 llc2
->llc_ssap
== LLC_SNAP_LSAP
&&
3123 llc2
->llc_control
== LLC_UI
) {
3124 ether_type
= htons(llc2
->llc_un
.type_snap
.ether_type
);
3130 * If we're trying to filter bridge traffic, don't look at anything
3131 * other than IP and ARP traffic. If the filter doesn't understand
3132 * IPv6, don't allow IPv6 through the bridge either. This is lame
3133 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3134 * but of course we don't have an AppleTalk filter to begin with.
3135 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3138 switch (ether_type
) {
3140 case ETHERTYPE_REVARP
:
3141 if (V_pfil_ipfw_arp
== 0)
3142 return (0); /* Automatically pass */
3147 case ETHERTYPE_IPV6
:
3152 * Check to see if the user wants to pass non-ip
3153 * packets, these will not be checked by pfil(9) and
3154 * passed unconditionally so the default is to drop.
3160 /* Run the packet through pfil before stripping link headers */
3161 if (PFIL_HOOKED(&V_link_pfil_hook
) && V_pfil_ipfw
!= 0 &&
3162 dir
== PFIL_OUT
&& ifp
!= NULL
) {
3164 error
= pfil_run_hooks(&V_link_pfil_hook
, mp
, ifp
, dir
, NULL
);
3166 if (*mp
== NULL
|| error
!= 0) /* packet consumed by filter */
3170 /* Strip off the Ethernet header and keep a copy. */
3171 m_copydata(*mp
, 0, ETHER_HDR_LEN
, (caddr_t
) &eh2
);
3172 m_adj(*mp
, ETHER_HDR_LEN
);
3174 /* Strip off snap header, if present */
3176 m_copydata(*mp
, 0, sizeof(struct llc
), (caddr_t
) &llc1
);
3177 m_adj(*mp
, sizeof(struct llc
));
3181 * Check the IP header for alignment and errors
3183 if (dir
== PFIL_IN
) {
3184 switch (ether_type
) {
3186 error
= bridge_ip_checkbasic(mp
);
3189 case ETHERTYPE_IPV6
:
3190 error
= bridge_ip6_checkbasic(mp
);
3203 * Run the packet through pfil
3205 switch (ether_type
) {
3208 * Run pfil on the member interface and the bridge, both can
3209 * be skipped by clearing pfil_member or pfil_bridge.
3212 * in_if -> bridge_if -> out_if
3214 if (V_pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
3215 error
= pfil_run_hooks(&V_inet_pfil_hook
, mp
, bifp
,
3218 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
3221 if (V_pfil_member
&& ifp
!= NULL
)
3222 error
= pfil_run_hooks(&V_inet_pfil_hook
, mp
, ifp
,
3225 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
3228 if (V_pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
3229 error
= pfil_run_hooks(&V_inet_pfil_hook
, mp
, bifp
,
3232 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
3235 /* check if we need to fragment the packet */
3236 if (V_pfil_member
&& ifp
!= NULL
&& dir
== PFIL_OUT
) {
3237 i
= (*mp
)->m_pkthdr
.len
;
3238 if (i
> ifp
->if_mtu
) {
3239 error
= bridge_fragment(ifp
, *mp
, &eh2
, snap
,
3245 /* Recalculate the ip checksum. */
3246 ip
= mtod(*mp
, struct ip
*);
3247 hlen
= ip
->ip_hl
<< 2;
3248 if (hlen
< sizeof(struct ip
))
3250 if (hlen
> (*mp
)->m_len
) {
3251 if ((*mp
= m_pullup(*mp
, hlen
)) == NULL
)
3253 ip
= mtod(*mp
, struct ip
*);
3258 if (hlen
== sizeof(struct ip
))
3259 ip
->ip_sum
= in_cksum_hdr(ip
);
3261 ip
->ip_sum
= in_cksum(*mp
, hlen
);
3265 case ETHERTYPE_IPV6
:
3266 if (V_pfil_bridge
&& dir
== PFIL_OUT
&& bifp
!= NULL
)
3267 error
= pfil_run_hooks(&V_inet6_pfil_hook
, mp
, bifp
,
3270 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
3273 if (V_pfil_member
&& ifp
!= NULL
)
3274 error
= pfil_run_hooks(&V_inet6_pfil_hook
, mp
, ifp
,
3277 if (*mp
== NULL
|| error
!= 0) /* filter may consume */
3280 if (V_pfil_bridge
&& dir
== PFIL_IN
&& bifp
!= NULL
)
3281 error
= pfil_run_hooks(&V_inet6_pfil_hook
, mp
, bifp
,
3298 * Finally, put everything back the way it was and return
3301 M_PREPEND(*mp
, sizeof(struct llc
), M_NOWAIT
);
3304 bcopy(&llc1
, mtod(*mp
, caddr_t
), sizeof(struct llc
));
3307 M_PREPEND(*mp
, ETHER_HDR_LEN
, M_NOWAIT
);
3310 bcopy(&eh2
, mtod(*mp
, caddr_t
), ETHER_HDR_LEN
);
3321 * Perform basic checks on header size since
3322 * pfil assumes ip_input has already processed
3323 * it for it. Cut-and-pasted from ip_input.c.
3324 * Given how simple the IPv6 version is,
3325 * does the IPv4 version really need to be
3328 * XXX Should we update ipstat here, or not?
3329 * XXX Right now we update ipstat but not
3333 bridge_ip_checkbasic(struct mbuf
**mp
)
3335 struct mbuf
*m
= *mp
;
3343 if (IP_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
3344 if ((m
= m_copyup(m
, sizeof(struct ip
),
3345 (max_linkhdr
+ 3) & ~3)) == NULL
) {
3346 /* XXXJRT new stat, please */
3347 KMOD_IPSTAT_INC(ips_toosmall
);
3350 } else if (__predict_false(m
->m_len
< sizeof (struct ip
))) {
3351 if ((m
= m_pullup(m
, sizeof (struct ip
))) == NULL
) {
3352 KMOD_IPSTAT_INC(ips_toosmall
);
3356 ip
= mtod(m
, struct ip
*);
3357 if (ip
== NULL
) goto bad
;
3359 if (ip
->ip_v
!= IPVERSION
) {
3360 KMOD_IPSTAT_INC(ips_badvers
);
3363 hlen
= ip
->ip_hl
<< 2;
3364 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
3365 KMOD_IPSTAT_INC(ips_badhlen
);
3368 if (hlen
> m
->m_len
) {
3369 if ((m
= m_pullup(m
, hlen
)) == NULL
) {
3370 KMOD_IPSTAT_INC(ips_badhlen
);
3373 ip
= mtod(m
, struct ip
*);
3374 if (ip
== NULL
) goto bad
;
3377 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
3378 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
3380 if (hlen
== sizeof(struct ip
)) {
3381 sum
= in_cksum_hdr(ip
);
3383 sum
= in_cksum(m
, hlen
);
3387 KMOD_IPSTAT_INC(ips_badsum
);
3391 /* Retrieve the packet length. */
3392 len
= ntohs(ip
->ip_len
);
3395 * Check for additional length bogosity
3398 KMOD_IPSTAT_INC(ips_badlen
);
3403 * Check that the amount of data in the buffers
3404 * is as at least much as the IP header would have us expect.
3405 * Drop packet if shorter than we expect.
3407 if (m
->m_pkthdr
.len
< len
) {
3408 KMOD_IPSTAT_INC(ips_tooshort
);
3412 /* Checks out, proceed */
3423 * Same as above, but for IPv6.
3424 * Cut-and-pasted from ip6_input.c.
3425 * XXX Should we update ip6stat, or not?
3428 bridge_ip6_checkbasic(struct mbuf
**mp
)
3430 struct mbuf
*m
= *mp
;
3431 struct ip6_hdr
*ip6
;
3434 * If the IPv6 header is not aligned, slurp it up into a new
3435 * mbuf with space for link headers, in the event we forward
3436 * it. Otherwise, if it is aligned, make sure the entire base
3437 * IPv6 header is in the first mbuf of the chain.
3439 if (IP6_HDR_ALIGNED_P(mtod(m
, caddr_t
)) == 0) {
3440 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
3441 if ((m
= m_copyup(m
, sizeof(struct ip6_hdr
),
3442 (max_linkhdr
+ 3) & ~3)) == NULL
) {
3443 /* XXXJRT new stat, please */
3444 IP6STAT_INC(ip6s_toosmall
);
3445 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
3448 } else if (__predict_false(m
->m_len
< sizeof(struct ip6_hdr
))) {
3449 struct ifnet
*inifp
= m
->m_pkthdr
.rcvif
;
3450 if ((m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
3451 IP6STAT_INC(ip6s_toosmall
);
3452 in6_ifstat_inc(inifp
, ifs6_in_hdrerr
);
3457 ip6
= mtod(m
, struct ip6_hdr
*);
3459 if ((ip6
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
3460 IP6STAT_INC(ip6s_badvers
);
3461 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_hdrerr
);
3465 /* Checks out, proceed */
3478 * Return a fragmented mbuf chain.
3481 bridge_fragment(struct ifnet
*ifp
, struct mbuf
*m
, struct ether_header
*eh
,
3482 int snap
, struct llc
*llc
)
3488 if (m
->m_len
< sizeof(struct ip
) &&
3489 (m
= m_pullup(m
, sizeof(struct ip
))) == NULL
)
3491 ip
= mtod(m
, struct ip
*);
3493 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
3494 error
= ip_fragment(ip
, &m
, ifp
->if_mtu
, ifp
->if_hwassist
);
3498 /* walk the chain and re-add the Ethernet header */
3499 for (m0
= m
; m0
; m0
= m0
->m_nextpkt
) {
3502 M_PREPEND(m0
, sizeof(struct llc
), M_NOWAIT
);
3507 bcopy(llc
, mtod(m0
, caddr_t
),
3508 sizeof(struct llc
));
3510 M_PREPEND(m0
, ETHER_HDR_LEN
, M_NOWAIT
);
3515 bcopy(eh
, mtod(m0
, caddr_t
), ETHER_HDR_LEN
);
3521 KMOD_IPSTAT_INC(ips_fragmented
);
3532 bridge_linkstate(struct ifnet
*ifp
)
3534 struct bridge_softc
*sc
= ifp
->if_bridge
;
3535 struct bridge_iflist
*bif
;
3538 bif
= bridge_lookup_member_if(sc
, ifp
);
3543 bridge_linkcheck(sc
);
3546 bstp_linkstate(&bif
->bif_stp
);
3550 bridge_linkcheck(struct bridge_softc
*sc
)
3552 struct bridge_iflist
*bif
;
3553 int new_link
, hasls
;
3555 BRIDGE_LOCK_ASSERT(sc
);
3556 new_link
= LINK_STATE_DOWN
;
3558 /* Our link is considered up if at least one of our ports is active */
3559 LIST_FOREACH(bif
, &sc
->sc_iflist
, bif_next
) {
3560 if (bif
->bif_ifp
->if_capabilities
& IFCAP_LINKSTATE
)
3562 if (bif
->bif_ifp
->if_link_state
== LINK_STATE_UP
) {
3563 new_link
= LINK_STATE_UP
;
3567 if (!LIST_EMPTY(&sc
->sc_iflist
) && !hasls
) {
3568 /* If no interfaces support link-state then we default to up */
3569 new_link
= LINK_STATE_UP
;
3571 if_link_state_change(sc
->sc_ifp
, new_link
);