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