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