if_bridge.c revision 1.88 1 /* $NetBSD: if_bridge.c,v 1.88 2014/07/20 16:40:34 ozaki-r 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 (at) 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 __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.88 2014/07/20 16:40:34 ozaki-r Exp $");
84
85 #ifdef _KERNEL_OPT
86 #include "opt_bridge_ipf.h"
87 #include "opt_inet.h"
88 #endif /* _KERNEL_OPT */
89
90 #include <sys/param.h>
91 #include <sys/kernel.h>
92 #include <sys/mbuf.h>
93 #include <sys/queue.h>
94 #include <sys/socket.h>
95 #include <sys/socketvar.h> /* for softnet_lock */
96 #include <sys/sockio.h>
97 #include <sys/systm.h>
98 #include <sys/proc.h>
99 #include <sys/pool.h>
100 #include <sys/kauth.h>
101 #include <sys/cpu.h>
102 #include <sys/cprng.h>
103 #include <sys/mutex.h>
104
105 #include <net/bpf.h>
106 #include <net/if.h>
107 #include <net/if_dl.h>
108 #include <net/if_types.h>
109 #include <net/if_llc.h>
110 #include <net/pktqueue.h>
111
112 #include <net/if_ether.h>
113 #include <net/if_bridgevar.h>
114
115 #if defined(BRIDGE_IPF)
116 /* Used for bridge_ip[6]_checkbasic */
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/ip_private.h> /* XXX */
122
123 #include <netinet/ip6.h>
124 #include <netinet6/in6_var.h>
125 #include <netinet6/ip6_var.h>
126 #include <netinet6/ip6_private.h> /* XXX */
127 #endif /* BRIDGE_IPF */
128
129 /*
130 * Size of the route hash table. Must be a power of two.
131 */
132 #ifndef BRIDGE_RTHASH_SIZE
133 #define BRIDGE_RTHASH_SIZE 1024
134 #endif
135
136 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
137
138 #include "carp.h"
139 #if NCARP > 0
140 #include <netinet/in.h>
141 #include <netinet/in_var.h>
142 #include <netinet/ip_carp.h>
143 #endif
144
145 /*
146 * Maximum number of addresses to cache.
147 */
148 #ifndef BRIDGE_RTABLE_MAX
149 #define BRIDGE_RTABLE_MAX 100
150 #endif
151
152 /*
153 * Spanning tree defaults.
154 */
155 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
156 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
157 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
158 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
159 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
160 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
161 #define BSTP_DEFAULT_PATH_COST 55
162
163 /*
164 * Timeout (in seconds) for entries learned dynamically.
165 */
166 #ifndef BRIDGE_RTABLE_TIMEOUT
167 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
168 #endif
169
170 /*
171 * Number of seconds between walks of the route list.
172 */
173 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
174 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
175 #endif
176
177 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
178
179 static struct pool bridge_rtnode_pool;
180
181 void bridgeattach(int);
182
183 static int bridge_clone_create(struct if_clone *, int);
184 static int bridge_clone_destroy(struct ifnet *);
185
186 static int bridge_ioctl(struct ifnet *, u_long, void *);
187 static int bridge_init(struct ifnet *);
188 static void bridge_stop(struct ifnet *, int);
189 static void bridge_start(struct ifnet *);
190
191 static void bridge_input(struct ifnet *, struct mbuf *);
192 static void bridge_forward(void *);
193
194 static void bridge_timer(void *);
195
196 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
197 struct mbuf *);
198
199 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
200 struct ifnet *, int, uint8_t);
201 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
202 static void bridge_rttrim(struct bridge_softc *);
203 static void bridge_rtage(struct bridge_softc *);
204 static void bridge_rtflush(struct bridge_softc *, int);
205 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
206 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
207
208 static int bridge_rtable_init(struct bridge_softc *);
209 static void bridge_rtable_fini(struct bridge_softc *);
210
211 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
212 const uint8_t *);
213 static int bridge_rtnode_insert(struct bridge_softc *,
214 struct bridge_rtnode *);
215 static void bridge_rtnode_destroy(struct bridge_softc *,
216 struct bridge_rtnode *);
217
218 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
219 const char *name);
220 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
221 struct ifnet *ifp);
222 static void bridge_release_member(struct bridge_softc *, struct bridge_iflist *);
223 static void bridge_delete_member(struct bridge_softc *,
224 struct bridge_iflist *);
225
226 static int bridge_ioctl_add(struct bridge_softc *, void *);
227 static int bridge_ioctl_del(struct bridge_softc *, void *);
228 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
229 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
230 static int bridge_ioctl_scache(struct bridge_softc *, void *);
231 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
232 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
233 static int bridge_ioctl_rts(struct bridge_softc *, void *);
234 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
235 static int bridge_ioctl_sto(struct bridge_softc *, void *);
236 static int bridge_ioctl_gto(struct bridge_softc *, void *);
237 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
238 static int bridge_ioctl_flush(struct bridge_softc *, void *);
239 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
240 static int bridge_ioctl_spri(struct bridge_softc *, void *);
241 static int bridge_ioctl_ght(struct bridge_softc *, void *);
242 static int bridge_ioctl_sht(struct bridge_softc *, void *);
243 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
244 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
245 static int bridge_ioctl_gma(struct bridge_softc *, void *);
246 static int bridge_ioctl_sma(struct bridge_softc *, void *);
247 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
248 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
249 #if defined(BRIDGE_IPF)
250 static int bridge_ioctl_gfilt(struct bridge_softc *, void *);
251 static int bridge_ioctl_sfilt(struct bridge_softc *, void *);
252 static int bridge_ipf(void *, struct mbuf **, struct ifnet *, int);
253 static int bridge_ip_checkbasic(struct mbuf **mp);
254 # ifdef INET6
255 static int bridge_ip6_checkbasic(struct mbuf **mp);
256 # endif /* INET6 */
257 #endif /* BRIDGE_IPF */
258
259 static void bridge_sysctl_fwdq_setup(struct sysctllog **clog,
260 struct bridge_softc *sc);
261
262 struct bridge_control {
263 int (*bc_func)(struct bridge_softc *, void *);
264 int bc_argsize;
265 int bc_flags;
266 };
267
268 #define BC_F_COPYIN 0x01 /* copy arguments in */
269 #define BC_F_COPYOUT 0x02 /* copy arguments out */
270 #define BC_F_SUSER 0x04 /* do super-user check */
271
272 static const struct bridge_control bridge_control_table[] = {
273 [BRDGADD] = {bridge_ioctl_add, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
274 [BRDGDEL] = {bridge_ioctl_del, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
275
276 [BRDGGIFFLGS] = {bridge_ioctl_gifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_COPYOUT},
277 [BRDGSIFFLGS] = {bridge_ioctl_sifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
278
279 [BRDGSCACHE] = {bridge_ioctl_scache, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
280 [BRDGGCACHE] = {bridge_ioctl_gcache, sizeof(struct ifbrparam), BC_F_COPYOUT},
281
282 [BRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_COPYIN|BC_F_COPYOUT},
283 [BRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_COPYIN|BC_F_COPYOUT},
284
285 [BRDGSADDR] = {bridge_ioctl_saddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
286
287 [BRDGSTO] = {bridge_ioctl_sto, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
288 [BRDGGTO] = {bridge_ioctl_gto, sizeof(struct ifbrparam), BC_F_COPYOUT},
289
290 [BRDGDADDR] = {bridge_ioctl_daddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
291
292 [BRDGFLUSH] = {bridge_ioctl_flush, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
293
294 [BRDGGPRI] = {bridge_ioctl_gpri, sizeof(struct ifbrparam), BC_F_COPYOUT},
295 [BRDGSPRI] = {bridge_ioctl_spri, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
296
297 [BRDGGHT] = {bridge_ioctl_ght, sizeof(struct ifbrparam), BC_F_COPYOUT},
298 [BRDGSHT] = {bridge_ioctl_sht, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
299
300 [BRDGGFD] = {bridge_ioctl_gfd, sizeof(struct ifbrparam), BC_F_COPYOUT},
301 [BRDGSFD] = {bridge_ioctl_sfd, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
302
303 [BRDGGMA] = {bridge_ioctl_gma, sizeof(struct ifbrparam), BC_F_COPYOUT},
304 [BRDGSMA] = {bridge_ioctl_sma, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
305
306 [BRDGSIFPRIO] = {bridge_ioctl_sifprio, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
307
308 [BRDGSIFCOST] = {bridge_ioctl_sifcost, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
309 #if defined(BRIDGE_IPF)
310 [BRDGGFILT] = {bridge_ioctl_gfilt, sizeof(struct ifbrparam), BC_F_COPYOUT},
311 [BRDGSFILT] = {bridge_ioctl_sfilt, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
312 #endif /* BRIDGE_IPF */
313 };
314 static const int bridge_control_table_size = __arraycount(bridge_control_table);
315
316 static LIST_HEAD(, bridge_softc) bridge_list;
317 static kmutex_t bridge_list_lock;
318
319 static struct if_clone bridge_cloner =
320 IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
321
322 /*
323 * bridgeattach:
324 *
325 * Pseudo-device attach routine.
326 */
327 void
328 bridgeattach(int n)
329 {
330
331 pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
332 0, 0, 0, "brtpl", NULL, IPL_NET);
333
334 LIST_INIT(&bridge_list);
335 mutex_init(&bridge_list_lock, MUTEX_DEFAULT, IPL_NET);
336 if_clone_attach(&bridge_cloner);
337 }
338
339 /*
340 * bridge_clone_create:
341 *
342 * Create a new bridge instance.
343 */
344 static int
345 bridge_clone_create(struct if_clone *ifc, int unit)
346 {
347 struct bridge_softc *sc;
348 struct ifnet *ifp;
349
350 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
351 ifp = &sc->sc_if;
352
353 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
354 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
355 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
356 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
357 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
358 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
359 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
360 sc->sc_filter_flags = 0;
361
362 /* Initialize our routing table. */
363 bridge_rtable_init(sc);
364
365 callout_init(&sc->sc_brcallout, 0);
366 callout_init(&sc->sc_bstpcallout, 0);
367
368 LIST_INIT(&sc->sc_iflist);
369 #ifdef BRIDGE_MPSAFE
370 sc->sc_iflist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
371 #else
372 sc->sc_iflist_lock = NULL;
373 #endif
374 cv_init(&sc->sc_iflist_cv, "if_bridge_cv");
375
376 if_initname(ifp, ifc->ifc_name, unit);
377 ifp->if_softc = sc;
378 ifp->if_mtu = ETHERMTU;
379 ifp->if_ioctl = bridge_ioctl;
380 ifp->if_output = bridge_output;
381 ifp->if_start = bridge_start;
382 ifp->if_stop = bridge_stop;
383 ifp->if_init = bridge_init;
384 ifp->if_type = IFT_BRIDGE;
385 ifp->if_addrlen = 0;
386 ifp->if_dlt = DLT_EN10MB;
387 ifp->if_hdrlen = ETHER_HDR_LEN;
388
389 sc->sc_fwd_pktq = pktq_create(IFQ_MAXLEN, bridge_forward, sc);
390 KASSERT(sc->sc_fwd_pktq != NULL);
391
392 bridge_sysctl_fwdq_setup(&ifp->if_sysctl_log, sc);
393
394 if_attach(ifp);
395
396 if_alloc_sadl(ifp);
397
398 mutex_enter(&bridge_list_lock);
399 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
400 mutex_exit(&bridge_list_lock);
401
402 return (0);
403 }
404
405 /*
406 * bridge_clone_destroy:
407 *
408 * Destroy a bridge instance.
409 */
410 static int
411 bridge_clone_destroy(struct ifnet *ifp)
412 {
413 struct bridge_softc *sc = ifp->if_softc;
414 struct bridge_iflist *bif;
415 int s;
416
417 /* Must be called during IFF_RUNNING, i.e., before bridge_stop */
418 pktq_barrier(sc->sc_fwd_pktq);
419
420 s = splnet();
421
422 bridge_stop(ifp, 1);
423
424 BRIDGE_LOCK(sc);
425 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
426 bridge_delete_member(sc, bif);
427 BRIDGE_UNLOCK(sc);
428
429 mutex_enter(&bridge_list_lock);
430 LIST_REMOVE(sc, sc_list);
431 mutex_exit(&bridge_list_lock);
432
433 splx(s);
434
435 if_detach(ifp);
436
437 /* Should be called after if_detach for safe */
438 pktq_flush(sc->sc_fwd_pktq);
439 pktq_destroy(sc->sc_fwd_pktq);
440
441 /* Tear down the routing table. */
442 bridge_rtable_fini(sc);
443
444 cv_destroy(&sc->sc_iflist_cv);
445 if (sc->sc_iflist_lock)
446 mutex_obj_free(sc->sc_iflist_lock);
447
448 free(sc, M_DEVBUF);
449
450 return (0);
451 }
452
453 static int
454 bridge_sysctl_fwdq_maxlen(SYSCTLFN_ARGS)
455 {
456 struct sysctlnode node = *rnode;
457 const struct bridge_softc *sc = node.sysctl_data;
458 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), sc->sc_fwd_pktq);
459 }
460
461 #define SYSCTL_BRIDGE_PKTQ(cn, c) \
462 static int \
463 bridge_sysctl_fwdq_##cn(SYSCTLFN_ARGS) \
464 { \
465 struct sysctlnode node = *rnode; \
466 const struct bridge_softc *sc = node.sysctl_data; \
467 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), \
468 sc->sc_fwd_pktq, c); \
469 }
470
471 SYSCTL_BRIDGE_PKTQ(items, PKTQ_NITEMS)
472 SYSCTL_BRIDGE_PKTQ(drops, PKTQ_DROPS)
473
474 static void
475 bridge_sysctl_fwdq_setup(struct sysctllog **clog, struct bridge_softc *sc)
476 {
477 const struct sysctlnode *cnode, *rnode;
478 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
479 const char *ifname = sc->sc_if.if_xname;
480
481 len_func = bridge_sysctl_fwdq_items;
482 maxlen_func = bridge_sysctl_fwdq_maxlen;
483 drops_func = bridge_sysctl_fwdq_drops;
484
485 if (sysctl_createv(clog, 0, NULL, &rnode,
486 CTLFLAG_PERMANENT,
487 CTLTYPE_NODE, "interfaces",
488 SYSCTL_DESCR("Per-interface controls"),
489 NULL, 0, NULL, 0,
490 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
491 goto bad;
492
493 if (sysctl_createv(clog, 0, &rnode, &rnode,
494 CTLFLAG_PERMANENT,
495 CTLTYPE_NODE, ifname,
496 SYSCTL_DESCR("Interface controls"),
497 NULL, 0, NULL, 0,
498 CTL_CREATE, CTL_EOL) != 0)
499 goto bad;
500
501 if (sysctl_createv(clog, 0, &rnode, &rnode,
502 CTLFLAG_PERMANENT,
503 CTLTYPE_NODE, "fwdq",
504 SYSCTL_DESCR("Protocol input queue controls"),
505 NULL, 0, NULL, 0,
506 CTL_CREATE, CTL_EOL) != 0)
507 goto bad;
508
509 if (sysctl_createv(clog, 0, &rnode, &cnode,
510 CTLFLAG_PERMANENT,
511 CTLTYPE_INT, "len",
512 SYSCTL_DESCR("Current forwarding queue length"),
513 len_func, 0, (void *)sc, 0,
514 CTL_CREATE, IFQCTL_LEN, CTL_EOL) != 0)
515 goto bad;
516
517 if (sysctl_createv(clog, 0, &rnode, &cnode,
518 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
519 CTLTYPE_INT, "maxlen",
520 SYSCTL_DESCR("Maximum allowed forwarding queue length"),
521 maxlen_func, 0, (void *)sc, 0,
522 CTL_CREATE, IFQCTL_MAXLEN, CTL_EOL) != 0)
523 goto bad;
524
525 if (sysctl_createv(clog, 0, &rnode, &cnode,
526 CTLFLAG_PERMANENT,
527 CTLTYPE_INT, "drops",
528 SYSCTL_DESCR("Packets dropped due to full forwarding queue"),
529 drops_func, 0, (void *)sc, 0,
530 CTL_CREATE, IFQCTL_DROPS, CTL_EOL) != 0)
531 goto bad;
532
533 return;
534 bad:
535 aprint_error("%s: could not attach sysctl nodes\n", ifname);
536 return;
537 }
538
539 /*
540 * bridge_ioctl:
541 *
542 * Handle a control request from the operator.
543 */
544 static int
545 bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
546 {
547 struct bridge_softc *sc = ifp->if_softc;
548 struct lwp *l = curlwp; /* XXX */
549 union {
550 struct ifbreq ifbreq;
551 struct ifbifconf ifbifconf;
552 struct ifbareq ifbareq;
553 struct ifbaconf ifbaconf;
554 struct ifbrparam ifbrparam;
555 } args;
556 struct ifdrv *ifd = (struct ifdrv *) data;
557 const struct bridge_control *bc = NULL; /* XXXGCC */
558 int s, error = 0;
559
560 /* Authorize command before calling splnet(). */
561 switch (cmd) {
562 case SIOCGDRVSPEC:
563 case SIOCSDRVSPEC:
564 if (ifd->ifd_cmd >= bridge_control_table_size) {
565 error = EINVAL;
566 return error;
567 }
568
569 bc = &bridge_control_table[ifd->ifd_cmd];
570
571 /* We only care about BC_F_SUSER at this point. */
572 if ((bc->bc_flags & BC_F_SUSER) == 0)
573 break;
574
575 error = kauth_authorize_network(l->l_cred,
576 KAUTH_NETWORK_INTERFACE_BRIDGE,
577 cmd == SIOCGDRVSPEC ?
578 KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV :
579 KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV,
580 ifd, NULL, NULL);
581 if (error)
582 return (error);
583
584 break;
585 }
586
587 s = splnet();
588
589 switch (cmd) {
590 case SIOCGDRVSPEC:
591 case SIOCSDRVSPEC:
592 KASSERT(bc != NULL);
593 if (cmd == SIOCGDRVSPEC &&
594 (bc->bc_flags & BC_F_COPYOUT) == 0) {
595 error = EINVAL;
596 break;
597 }
598 else if (cmd == SIOCSDRVSPEC &&
599 (bc->bc_flags & BC_F_COPYOUT) != 0) {
600 error = EINVAL;
601 break;
602 }
603
604 /* BC_F_SUSER is checked above, before splnet(). */
605
606 if (ifd->ifd_len != bc->bc_argsize ||
607 ifd->ifd_len > sizeof(args)) {
608 error = EINVAL;
609 break;
610 }
611
612 memset(&args, 0, sizeof(args));
613 if (bc->bc_flags & BC_F_COPYIN) {
614 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
615 if (error)
616 break;
617 }
618
619 error = (*bc->bc_func)(sc, &args);
620 if (error)
621 break;
622
623 if (bc->bc_flags & BC_F_COPYOUT)
624 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
625
626 break;
627
628 case SIOCSIFFLAGS:
629 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
630 break;
631 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
632 case IFF_RUNNING:
633 /*
634 * If interface is marked down and it is running,
635 * then stop and disable it.
636 */
637 (*ifp->if_stop)(ifp, 1);
638 break;
639 case IFF_UP:
640 /*
641 * If interface is marked up and it is stopped, then
642 * start it.
643 */
644 error = (*ifp->if_init)(ifp);
645 break;
646 default:
647 break;
648 }
649 break;
650
651 case SIOCSIFMTU:
652 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
653 error = 0;
654 break;
655
656 default:
657 error = ifioctl_common(ifp, cmd, data);
658 break;
659 }
660
661 splx(s);
662
663 return (error);
664 }
665
666 /*
667 * bridge_lookup_member:
668 *
669 * Lookup a bridge member interface.
670 */
671 static struct bridge_iflist *
672 bridge_lookup_member(struct bridge_softc *sc, const char *name)
673 {
674 struct bridge_iflist *bif;
675 struct ifnet *ifp;
676
677 BRIDGE_LOCK(sc);
678
679 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
680 ifp = bif->bif_ifp;
681 if (strcmp(ifp->if_xname, name) == 0)
682 break;
683 }
684
685 #ifdef BRIDGE_MPSAFE
686 if (bif != NULL) {
687 if (bif->bif_waiting)
688 bif = NULL;
689 else
690 atomic_inc_32(&bif->bif_refs);
691 }
692 #endif
693
694 BRIDGE_UNLOCK(sc);
695
696 return bif;
697 }
698
699 /*
700 * bridge_lookup_member_if:
701 *
702 * Lookup a bridge member interface by ifnet*.
703 */
704 static struct bridge_iflist *
705 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
706 {
707 struct bridge_iflist *bif;
708
709 BRIDGE_LOCK(sc);
710
711 bif = member_ifp->if_bridgeif;
712
713 #ifdef BRIDGE_MPSAFE
714 if (bif != NULL) {
715 if (bif->bif_waiting)
716 bif = NULL;
717 else
718 atomic_inc_32(&bif->bif_refs);
719 }
720 #endif
721
722 BRIDGE_UNLOCK(sc);
723
724 return bif;
725 }
726
727 /*
728 * bridge_release_member:
729 *
730 * Release the specified member interface.
731 */
732 static void
733 bridge_release_member(struct bridge_softc *sc, struct bridge_iflist *bif)
734 {
735 #ifdef BRIDGE_MPSAFE
736 atomic_dec_32(&bif->bif_refs);
737 membar_sync();
738 if (__predict_false(bif->bif_waiting && bif->bif_refs == 0)) {
739 BRIDGE_LOCK(sc);
740 cv_broadcast(&sc->sc_iflist_cv);
741 BRIDGE_UNLOCK(sc);
742 }
743 #else
744 (void)sc;
745 (void)bif;
746 #endif
747 }
748
749 /*
750 * bridge_delete_member:
751 *
752 * Delete the specified member interface.
753 */
754 static void
755 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
756 {
757 struct ifnet *ifs = bif->bif_ifp;
758
759 KASSERT(BRIDGE_LOCKED(sc));
760
761 ifs->if_input = ether_input;
762 ifs->if_bridge = NULL;
763 ifs->if_bridgeif = NULL;
764
765 LIST_REMOVE(bif, bif_next);
766
767 #ifdef BRIDGE_MPSAFE
768 bif->bif_waiting = true;
769 membar_sync();
770 while (bif->bif_refs > 0) {
771 aprint_debug("%s: cv_wait on iflist\n", __func__);
772 cv_wait(&sc->sc_iflist_cv, sc->sc_iflist_lock);
773 }
774 #endif
775
776 free(bif, M_DEVBUF);
777 }
778
779 static int
780 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
781 {
782 struct ifbreq *req = arg;
783 struct bridge_iflist *bif = NULL;
784 struct ifnet *ifs;
785 int error = 0;
786
787 ifs = ifunit(req->ifbr_ifsname);
788 if (ifs == NULL)
789 return (ENOENT);
790
791 if (sc->sc_if.if_mtu != ifs->if_mtu)
792 return (EINVAL);
793
794 if (ifs->if_bridge == sc)
795 return (EEXIST);
796
797 if (ifs->if_bridge != NULL)
798 return (EBUSY);
799
800 if (ifs->if_input != ether_input)
801 return EINVAL;
802
803 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT);
804 if (bif == NULL)
805 return (ENOMEM);
806
807 switch (ifs->if_type) {
808 case IFT_ETHER:
809 /*
810 * Place the interface into promiscuous mode.
811 */
812 error = ifpromisc(ifs, 1);
813 if (error)
814 goto out;
815 break;
816 default:
817 error = EINVAL;
818 goto out;
819 }
820
821 bif->bif_ifp = ifs;
822 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
823 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
824 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
825 bif->bif_refs = 0;
826 bif->bif_waiting = false;
827
828 BRIDGE_LOCK(sc);
829
830 ifs->if_bridge = sc;
831 ifs->if_bridgeif = bif;
832 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
833 ifs->if_input = bridge_input;
834
835 BRIDGE_UNLOCK(sc);
836
837 if (sc->sc_if.if_flags & IFF_RUNNING)
838 bstp_initialization(sc);
839 else
840 bstp_stop(sc);
841
842 out:
843 if (error) {
844 if (bif != NULL)
845 free(bif, M_DEVBUF);
846 }
847 return (error);
848 }
849
850 static int
851 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
852 {
853 struct ifbreq *req = arg;
854 const char *name = req->ifbr_ifsname;
855 struct bridge_iflist *bif;
856 struct ifnet *ifs;
857
858 BRIDGE_LOCK(sc);
859
860 /*
861 * Don't use bridge_lookup_member. We want to get a member
862 * with bif_refs == 0.
863 */
864 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
865 ifs = bif->bif_ifp;
866 if (strcmp(ifs->if_xname, name) == 0)
867 break;
868 }
869
870 if (bif == NULL) {
871 BRIDGE_UNLOCK(sc);
872 return ENOENT;
873 }
874
875 bridge_delete_member(sc, bif);
876
877 BRIDGE_UNLOCK(sc);
878
879 switch (ifs->if_type) {
880 case IFT_ETHER:
881 /*
882 * Take the interface out of promiscuous mode.
883 * Don't call it with holding sc_iflist_lock.
884 */
885 (void) ifpromisc(ifs, 0);
886 break;
887 default:
888 #ifdef DIAGNOSTIC
889 panic("bridge_delete_member: impossible");
890 #endif
891 break;
892 }
893
894 bridge_rtdelete(sc, ifs);
895
896 if (sc->sc_if.if_flags & IFF_RUNNING)
897 bstp_initialization(sc);
898
899 return 0;
900 }
901
902 static int
903 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
904 {
905 struct ifbreq *req = arg;
906 struct bridge_iflist *bif;
907
908 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
909 if (bif == NULL)
910 return (ENOENT);
911
912 req->ifbr_ifsflags = bif->bif_flags;
913 req->ifbr_state = bif->bif_state;
914 req->ifbr_priority = bif->bif_priority;
915 req->ifbr_path_cost = bif->bif_path_cost;
916 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
917
918 bridge_release_member(sc, bif);
919
920 return (0);
921 }
922
923 static int
924 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
925 {
926 struct ifbreq *req = arg;
927 struct bridge_iflist *bif;
928
929 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
930 if (bif == NULL)
931 return (ENOENT);
932
933 if (req->ifbr_ifsflags & IFBIF_STP) {
934 switch (bif->bif_ifp->if_type) {
935 case IFT_ETHER:
936 /* These can do spanning tree. */
937 break;
938
939 default:
940 /* Nothing else can. */
941 bridge_release_member(sc, bif);
942 return (EINVAL);
943 }
944 }
945
946 bif->bif_flags = req->ifbr_ifsflags;
947
948 bridge_release_member(sc, bif);
949
950 if (sc->sc_if.if_flags & IFF_RUNNING)
951 bstp_initialization(sc);
952
953 return (0);
954 }
955
956 static int
957 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
958 {
959 struct ifbrparam *param = arg;
960
961 sc->sc_brtmax = param->ifbrp_csize;
962 bridge_rttrim(sc);
963
964 return (0);
965 }
966
967 static int
968 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
969 {
970 struct ifbrparam *param = arg;
971
972 param->ifbrp_csize = sc->sc_brtmax;
973
974 return (0);
975 }
976
977 static int
978 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
979 {
980 struct ifbifconf *bifc = arg;
981 struct bridge_iflist *bif;
982 struct ifbreq breq;
983 int count, len, error = 0;
984
985 BRIDGE_LOCK(sc);
986
987 count = 0;
988 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
989 count++;
990
991 if (bifc->ifbic_len == 0) {
992 bifc->ifbic_len = sizeof(breq) * count;
993 return (0);
994 }
995
996 count = 0;
997 len = bifc->ifbic_len;
998 memset(&breq, 0, sizeof breq);
999 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1000 if (len < sizeof(breq))
1001 break;
1002
1003 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1004 sizeof(breq.ifbr_ifsname));
1005 breq.ifbr_ifsflags = bif->bif_flags;
1006 breq.ifbr_state = bif->bif_state;
1007 breq.ifbr_priority = bif->bif_priority;
1008 breq.ifbr_path_cost = bif->bif_path_cost;
1009 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
1010 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1011 if (error)
1012 break;
1013 count++;
1014 len -= sizeof(breq);
1015 }
1016
1017 BRIDGE_UNLOCK(sc);
1018
1019 bifc->ifbic_len = sizeof(breq) * count;
1020 return (error);
1021 }
1022
1023 static int
1024 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1025 {
1026 struct ifbaconf *bac = arg;
1027 struct bridge_rtnode *brt;
1028 struct ifbareq bareq;
1029 int count = 0, error = 0, len;
1030
1031 if (bac->ifbac_len == 0)
1032 return (0);
1033
1034 mutex_enter(sc->sc_rtlist_lock);
1035
1036 len = bac->ifbac_len;
1037 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1038 if (len < sizeof(bareq))
1039 goto out;
1040 memset(&bareq, 0, sizeof(bareq));
1041 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1042 sizeof(bareq.ifba_ifsname));
1043 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1044 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1045 bareq.ifba_expire = brt->brt_expire - time_uptime;
1046 } else
1047 bareq.ifba_expire = 0;
1048 bareq.ifba_flags = brt->brt_flags;
1049
1050 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1051 if (error)
1052 goto out;
1053 count++;
1054 len -= sizeof(bareq);
1055 }
1056 out:
1057 mutex_exit(sc->sc_rtlist_lock);
1058
1059 bac->ifbac_len = sizeof(bareq) * count;
1060 return (error);
1061 }
1062
1063 static int
1064 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1065 {
1066 struct ifbareq *req = arg;
1067 struct bridge_iflist *bif;
1068 int error;
1069
1070 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1071 if (bif == NULL)
1072 return (ENOENT);
1073
1074 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1075 req->ifba_flags);
1076
1077 bridge_release_member(sc, bif);
1078
1079 return (error);
1080 }
1081
1082 static int
1083 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1084 {
1085 struct ifbrparam *param = arg;
1086
1087 sc->sc_brttimeout = param->ifbrp_ctime;
1088
1089 return (0);
1090 }
1091
1092 static int
1093 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1094 {
1095 struct ifbrparam *param = arg;
1096
1097 param->ifbrp_ctime = sc->sc_brttimeout;
1098
1099 return (0);
1100 }
1101
1102 static int
1103 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1104 {
1105 struct ifbareq *req = arg;
1106
1107 return (bridge_rtdaddr(sc, req->ifba_dst));
1108 }
1109
1110 static int
1111 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1112 {
1113 struct ifbreq *req = arg;
1114
1115 bridge_rtflush(sc, req->ifbr_ifsflags);
1116
1117 return (0);
1118 }
1119
1120 static int
1121 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1122 {
1123 struct ifbrparam *param = arg;
1124
1125 param->ifbrp_prio = sc->sc_bridge_priority;
1126
1127 return (0);
1128 }
1129
1130 static int
1131 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1132 {
1133 struct ifbrparam *param = arg;
1134
1135 sc->sc_bridge_priority = param->ifbrp_prio;
1136
1137 if (sc->sc_if.if_flags & IFF_RUNNING)
1138 bstp_initialization(sc);
1139
1140 return (0);
1141 }
1142
1143 static int
1144 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1145 {
1146 struct ifbrparam *param = arg;
1147
1148 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1149
1150 return (0);
1151 }
1152
1153 static int
1154 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1155 {
1156 struct ifbrparam *param = arg;
1157
1158 if (param->ifbrp_hellotime == 0)
1159 return (EINVAL);
1160 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1161
1162 if (sc->sc_if.if_flags & IFF_RUNNING)
1163 bstp_initialization(sc);
1164
1165 return (0);
1166 }
1167
1168 static int
1169 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1170 {
1171 struct ifbrparam *param = arg;
1172
1173 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1174
1175 return (0);
1176 }
1177
1178 static int
1179 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1180 {
1181 struct ifbrparam *param = arg;
1182
1183 if (param->ifbrp_fwddelay == 0)
1184 return (EINVAL);
1185 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1186
1187 if (sc->sc_if.if_flags & IFF_RUNNING)
1188 bstp_initialization(sc);
1189
1190 return (0);
1191 }
1192
1193 static int
1194 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1195 {
1196 struct ifbrparam *param = arg;
1197
1198 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1199
1200 return (0);
1201 }
1202
1203 static int
1204 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1205 {
1206 struct ifbrparam *param = arg;
1207
1208 if (param->ifbrp_maxage == 0)
1209 return (EINVAL);
1210 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1211
1212 if (sc->sc_if.if_flags & IFF_RUNNING)
1213 bstp_initialization(sc);
1214
1215 return (0);
1216 }
1217
1218 static int
1219 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1220 {
1221 struct ifbreq *req = arg;
1222 struct bridge_iflist *bif;
1223
1224 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1225 if (bif == NULL)
1226 return (ENOENT);
1227
1228 bif->bif_priority = req->ifbr_priority;
1229
1230 if (sc->sc_if.if_flags & IFF_RUNNING)
1231 bstp_initialization(sc);
1232
1233 bridge_release_member(sc, bif);
1234
1235 return (0);
1236 }
1237
1238 #if defined(BRIDGE_IPF)
1239 static int
1240 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
1241 {
1242 struct ifbrparam *param = arg;
1243
1244 param->ifbrp_filter = sc->sc_filter_flags;
1245
1246 return (0);
1247 }
1248
1249 static int
1250 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
1251 {
1252 struct ifbrparam *param = arg;
1253 uint32_t nflags, oflags;
1254
1255 if (param->ifbrp_filter & ~IFBF_FILT_MASK)
1256 return (EINVAL);
1257
1258 nflags = param->ifbrp_filter;
1259 oflags = sc->sc_filter_flags;
1260
1261 if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
1262 pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1263 sc->sc_if.if_pfil);
1264 }
1265 if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
1266 pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1267 sc->sc_if.if_pfil);
1268 }
1269
1270 sc->sc_filter_flags = nflags;
1271
1272 return (0);
1273 }
1274 #endif /* BRIDGE_IPF */
1275
1276 static int
1277 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1278 {
1279 struct ifbreq *req = arg;
1280 struct bridge_iflist *bif;
1281
1282 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1283 if (bif == NULL)
1284 return (ENOENT);
1285
1286 bif->bif_path_cost = req->ifbr_path_cost;
1287
1288 if (sc->sc_if.if_flags & IFF_RUNNING)
1289 bstp_initialization(sc);
1290
1291 bridge_release_member(sc, bif);
1292
1293 return (0);
1294 }
1295
1296 /*
1297 * bridge_ifdetach:
1298 *
1299 * Detach an interface from a bridge. Called when a member
1300 * interface is detaching.
1301 */
1302 void
1303 bridge_ifdetach(struct ifnet *ifp)
1304 {
1305 struct bridge_softc *sc = ifp->if_bridge;
1306 struct ifbreq breq;
1307
1308 /* ioctl_lock should prevent this from happening */
1309 KASSERT(sc != NULL);
1310
1311 memset(&breq, 0, sizeof(breq));
1312 strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
1313
1314 (void) bridge_ioctl_del(sc, &breq);
1315 }
1316
1317 /*
1318 * bridge_init:
1319 *
1320 * Initialize a bridge interface.
1321 */
1322 static int
1323 bridge_init(struct ifnet *ifp)
1324 {
1325 struct bridge_softc *sc = ifp->if_softc;
1326
1327 if (ifp->if_flags & IFF_RUNNING)
1328 return (0);
1329
1330 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1331 bridge_timer, sc);
1332
1333 ifp->if_flags |= IFF_RUNNING;
1334 bstp_initialization(sc);
1335 return (0);
1336 }
1337
1338 /*
1339 * bridge_stop:
1340 *
1341 * Stop the bridge interface.
1342 */
1343 static void
1344 bridge_stop(struct ifnet *ifp, int disable)
1345 {
1346 struct bridge_softc *sc = ifp->if_softc;
1347
1348 if ((ifp->if_flags & IFF_RUNNING) == 0)
1349 return;
1350
1351 callout_stop(&sc->sc_brcallout);
1352 bstp_stop(sc);
1353
1354 bridge_rtflush(sc, IFBF_FLUSHDYN);
1355
1356 ifp->if_flags &= ~IFF_RUNNING;
1357 }
1358
1359 /*
1360 * bridge_enqueue:
1361 *
1362 * Enqueue a packet on a bridge member interface.
1363 */
1364 void
1365 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
1366 int runfilt)
1367 {
1368 ALTQ_DECL(struct altq_pktattr pktattr;)
1369 int len, error;
1370 short mflags;
1371
1372 /*
1373 * Clear any in-bound checksum flags for this packet.
1374 */
1375 m->m_pkthdr.csum_flags = 0;
1376
1377 if (runfilt) {
1378 if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1379 dst_ifp, PFIL_OUT) != 0) {
1380 if (m != NULL)
1381 m_freem(m);
1382 return;
1383 }
1384 if (m == NULL)
1385 return;
1386 }
1387
1388 #ifdef ALTQ
1389 /*
1390 * If ALTQ is enabled on the member interface, do
1391 * classification; the queueing discipline might
1392 * not require classification, but might require
1393 * the address family/header pointer in the pktattr.
1394 */
1395 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1396 /* XXX IFT_ETHER */
1397 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1398 }
1399 #endif /* ALTQ */
1400
1401 len = m->m_pkthdr.len;
1402 m->m_flags |= M_PROTO1;
1403 mflags = m->m_flags;
1404
1405 IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error);
1406
1407 if (error) {
1408 /* mbuf is already freed */
1409 sc->sc_if.if_oerrors++;
1410 return;
1411 }
1412
1413 sc->sc_if.if_opackets++;
1414 sc->sc_if.if_obytes += len;
1415
1416 dst_ifp->if_obytes += len;
1417
1418 if (mflags & M_MCAST) {
1419 sc->sc_if.if_omcasts++;
1420 dst_ifp->if_omcasts++;
1421 }
1422
1423 if ((dst_ifp->if_flags & IFF_OACTIVE) == 0)
1424 (*dst_ifp->if_start)(dst_ifp);
1425 }
1426
1427 /*
1428 * bridge_output:
1429 *
1430 * Send output from a bridge member interface. This
1431 * performs the bridging function for locally originated
1432 * packets.
1433 *
1434 * The mbuf has the Ethernet header already attached. We must
1435 * enqueue or free the mbuf before returning.
1436 */
1437 int
1438 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
1439 struct rtentry *rt)
1440 {
1441 struct ether_header *eh;
1442 struct ifnet *dst_if;
1443 struct bridge_softc *sc;
1444 #ifndef BRIDGE_MPSAFE
1445 int s;
1446 #endif
1447
1448 if (m->m_len < ETHER_HDR_LEN) {
1449 m = m_pullup(m, ETHER_HDR_LEN);
1450 if (m == NULL)
1451 return (0);
1452 }
1453
1454 eh = mtod(m, struct ether_header *);
1455 sc = ifp->if_bridge;
1456
1457 #ifndef BRIDGE_MPSAFE
1458 s = splnet();
1459 #endif
1460
1461 /*
1462 * If bridge is down, but the original output interface is up,
1463 * go ahead and send out that interface. Otherwise, the packet
1464 * is dropped below.
1465 */
1466 if (__predict_false(sc == NULL) ||
1467 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1468 dst_if = ifp;
1469 goto sendunicast;
1470 }
1471
1472 /*
1473 * If the packet is a multicast, or we don't know a better way to
1474 * get there, send to all interfaces.
1475 */
1476 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1477 dst_if = NULL;
1478 else
1479 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1480 if (dst_if == NULL) {
1481 struct bridge_iflist *bif;
1482 struct mbuf *mc;
1483 int used = 0;
1484
1485 BRIDGE_LOCK(sc);
1486
1487 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1488 dst_if = bif->bif_ifp;
1489 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1490 continue;
1491
1492 /*
1493 * If this is not the original output interface,
1494 * and the interface is participating in spanning
1495 * tree, make sure the port is in a state that
1496 * allows forwarding.
1497 */
1498 if (dst_if != ifp &&
1499 (bif->bif_flags & IFBIF_STP) != 0) {
1500 switch (bif->bif_state) {
1501 case BSTP_IFSTATE_BLOCKING:
1502 case BSTP_IFSTATE_LISTENING:
1503 case BSTP_IFSTATE_DISABLED:
1504 continue;
1505 }
1506 }
1507
1508 if (LIST_NEXT(bif, bif_next) == NULL) {
1509 used = 1;
1510 mc = m;
1511 } else {
1512 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1513 if (mc == NULL) {
1514 sc->sc_if.if_oerrors++;
1515 continue;
1516 }
1517 }
1518
1519 bridge_enqueue(sc, dst_if, mc, 0);
1520 }
1521
1522 BRIDGE_UNLOCK(sc);
1523
1524 if (used == 0)
1525 m_freem(m);
1526 #ifndef BRIDGE_MPSAFE
1527 splx(s);
1528 #endif
1529 return (0);
1530 }
1531
1532 sendunicast:
1533 /*
1534 * XXX Spanning tree consideration here?
1535 */
1536
1537 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1538 m_freem(m);
1539 #ifndef BRIDGE_MPSAFE
1540 splx(s);
1541 #endif
1542 return (0);
1543 }
1544
1545 bridge_enqueue(sc, dst_if, m, 0);
1546
1547 #ifndef BRIDGE_MPSAFE
1548 splx(s);
1549 #endif
1550 return (0);
1551 }
1552
1553 /*
1554 * bridge_start:
1555 *
1556 * Start output on a bridge.
1557 *
1558 * NOTE: This routine should never be called in this implementation.
1559 */
1560 static void
1561 bridge_start(struct ifnet *ifp)
1562 {
1563
1564 printf("%s: bridge_start() called\n", ifp->if_xname);
1565 }
1566
1567 /*
1568 * bridge_forward:
1569 *
1570 * The forwarding function of the bridge.
1571 */
1572 static void
1573 bridge_forward(void *v)
1574 {
1575 struct bridge_softc *sc = v;
1576 struct mbuf *m;
1577 struct bridge_iflist *bif;
1578 struct ifnet *src_if, *dst_if;
1579 struct ether_header *eh;
1580 #ifndef BRIDGE_MPSAFE
1581 int s;
1582
1583 KERNEL_LOCK(1, NULL);
1584 mutex_enter(softnet_lock);
1585 #endif
1586
1587 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1588 #ifndef BRIDGE_MPSAFE
1589 mutex_exit(softnet_lock);
1590 KERNEL_UNLOCK_ONE(NULL);
1591 #endif
1592 return;
1593 }
1594
1595 #ifndef BRIDGE_MPSAFE
1596 s = splnet();
1597 #endif
1598 while ((m = pktq_dequeue(sc->sc_fwd_pktq)) != NULL) {
1599 src_if = m->m_pkthdr.rcvif;
1600
1601 sc->sc_if.if_ipackets++;
1602 sc->sc_if.if_ibytes += m->m_pkthdr.len;
1603
1604 /*
1605 * Look up the bridge_iflist.
1606 */
1607 bif = bridge_lookup_member_if(sc, src_if);
1608 if (bif == NULL) {
1609 /* Interface is not a bridge member (anymore?) */
1610 m_freem(m);
1611 continue;
1612 }
1613
1614 if (bif->bif_flags & IFBIF_STP) {
1615 switch (bif->bif_state) {
1616 case BSTP_IFSTATE_BLOCKING:
1617 case BSTP_IFSTATE_LISTENING:
1618 case BSTP_IFSTATE_DISABLED:
1619 m_freem(m);
1620 bridge_release_member(sc, bif);
1621 continue;
1622 }
1623 }
1624
1625 eh = mtod(m, struct ether_header *);
1626
1627 /*
1628 * If the interface is learning, and the source
1629 * address is valid and not multicast, record
1630 * the address.
1631 */
1632 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1633 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1634 (eh->ether_shost[0] == 0 &&
1635 eh->ether_shost[1] == 0 &&
1636 eh->ether_shost[2] == 0 &&
1637 eh->ether_shost[3] == 0 &&
1638 eh->ether_shost[4] == 0 &&
1639 eh->ether_shost[5] == 0) == 0) {
1640 (void) bridge_rtupdate(sc, eh->ether_shost,
1641 src_if, 0, IFBAF_DYNAMIC);
1642 }
1643
1644 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1645 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1646 m_freem(m);
1647 bridge_release_member(sc, bif);
1648 continue;
1649 }
1650
1651 bridge_release_member(sc, bif);
1652
1653 /*
1654 * At this point, the port either doesn't participate
1655 * in spanning tree or it is in the forwarding state.
1656 */
1657
1658 /*
1659 * If the packet is unicast, destined for someone on
1660 * "this" side of the bridge, drop it.
1661 */
1662 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1663 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1664 if (src_if == dst_if) {
1665 m_freem(m);
1666 continue;
1667 }
1668 } else {
1669 /* ...forward it to all interfaces. */
1670 sc->sc_if.if_imcasts++;
1671 dst_if = NULL;
1672 }
1673
1674 if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1675 m->m_pkthdr.rcvif, PFIL_IN) != 0) {
1676 if (m != NULL)
1677 m_freem(m);
1678 continue;
1679 }
1680 if (m == NULL)
1681 continue;
1682
1683 if (dst_if == NULL) {
1684 bridge_broadcast(sc, src_if, m);
1685 continue;
1686 }
1687
1688 /*
1689 * At this point, we're dealing with a unicast frame
1690 * going to a different interface.
1691 */
1692 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1693 m_freem(m);
1694 continue;
1695 }
1696
1697 bif = bridge_lookup_member_if(sc, dst_if);
1698 if (bif == NULL) {
1699 /* Not a member of the bridge (anymore?) */
1700 m_freem(m);
1701 continue;
1702 }
1703
1704 if (bif->bif_flags & IFBIF_STP) {
1705 switch (bif->bif_state) {
1706 case BSTP_IFSTATE_DISABLED:
1707 case BSTP_IFSTATE_BLOCKING:
1708 m_freem(m);
1709 bridge_release_member(sc, bif);
1710 continue;
1711 }
1712 }
1713
1714 bridge_release_member(sc, bif);
1715
1716 bridge_enqueue(sc, dst_if, m, 1);
1717 }
1718 #ifndef BRIDGE_MPSAFE
1719 splx(s);
1720 mutex_exit(softnet_lock);
1721 KERNEL_UNLOCK_ONE(NULL);
1722 #endif
1723 }
1724
1725 static bool
1726 bstp_state_before_learning(struct bridge_iflist *bif)
1727 {
1728 if (bif->bif_flags & IFBIF_STP) {
1729 switch (bif->bif_state) {
1730 case BSTP_IFSTATE_BLOCKING:
1731 case BSTP_IFSTATE_LISTENING:
1732 case BSTP_IFSTATE_DISABLED:
1733 return true;
1734 }
1735 }
1736 return false;
1737 }
1738
1739 static bool
1740 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
1741 {
1742 uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
1743
1744 if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
1745 #if NCARP > 0
1746 || (bif->bif_ifp->if_carp &&
1747 carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
1748 #endif /* NCARP > 0 */
1749 )
1750 return true;
1751
1752 return false;
1753 }
1754
1755 /*
1756 * bridge_input:
1757 *
1758 * Receive input from a member interface. Queue the packet for
1759 * bridging if it is not for us.
1760 */
1761 static void
1762 bridge_input(struct ifnet *ifp, struct mbuf *m)
1763 {
1764 struct bridge_softc *sc = ifp->if_bridge;
1765 struct bridge_iflist *bif;
1766 struct ether_header *eh;
1767
1768 if (__predict_false(sc == NULL) ||
1769 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1770 ether_input(ifp, m);
1771 return;
1772 }
1773
1774 bif = bridge_lookup_member_if(sc, ifp);
1775 if (bif == NULL) {
1776 ether_input(ifp, m);
1777 return;
1778 }
1779
1780 eh = mtod(m, struct ether_header *);
1781
1782 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1783 if (memcmp(etherbroadcastaddr,
1784 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1785 m->m_flags |= M_BCAST;
1786 else
1787 m->m_flags |= M_MCAST;
1788 }
1789
1790 /*
1791 * A 'fast' path for packets addressed to interfaces that are
1792 * part of this bridge.
1793 */
1794 if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
1795 !bstp_state_before_learning(bif)) {
1796 struct bridge_iflist *_bif;
1797
1798 BRIDGE_LOCK(sc);
1799 LIST_FOREACH(_bif, &sc->sc_iflist, bif_next) {
1800 /* It is destined for us. */
1801 if (bridge_ourether(_bif, eh, 0)) {
1802 if (_bif->bif_flags & IFBIF_LEARNING)
1803 (void) bridge_rtupdate(sc,
1804 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1805 m->m_pkthdr.rcvif = _bif->bif_ifp;
1806 ether_input(_bif->bif_ifp, m);
1807 break;
1808 }
1809
1810 /* We just received a packet that we sent out. */
1811 if (bridge_ourether(_bif, eh, 1)) {
1812 m_freem(m);
1813 break;
1814 }
1815 }
1816 BRIDGE_UNLOCK(sc);
1817
1818 if (_bif != NULL) {
1819 bridge_release_member(sc, bif);
1820 return;
1821 }
1822 }
1823
1824 /* Tap off 802.1D packets; they do not get forwarded. */
1825 if (bif->bif_flags & IFBIF_STP &&
1826 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
1827 bstp_input(sc, bif, m);
1828 bridge_release_member(sc, bif);
1829 return;
1830 }
1831
1832 /*
1833 * A normal switch would discard the packet here, but that's not what
1834 * we've done historically. This also prevents some obnoxious behaviour.
1835 */
1836 if (bstp_state_before_learning(bif)) {
1837 ether_input(ifp, m);
1838 bridge_release_member(sc, bif);
1839 return;
1840 }
1841
1842 bridge_release_member(sc, bif);
1843
1844 /* Queue the packet for bridge forwarding. */
1845 if (__predict_false(!pktq_enqueue(sc->sc_fwd_pktq, m, 0)))
1846 m_freem(m);
1847 }
1848
1849 /*
1850 * bridge_broadcast:
1851 *
1852 * Send a frame to all interfaces that are members of
1853 * the bridge, except for the one on which the packet
1854 * arrived.
1855 */
1856 static void
1857 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1858 struct mbuf *m)
1859 {
1860 struct bridge_iflist *bif;
1861 struct mbuf *mc;
1862 struct ifnet *dst_if;
1863 bool used, bmcast;
1864
1865 used = bmcast = m->m_flags & (M_BCAST|M_MCAST);
1866
1867 BRIDGE_LOCK(sc);
1868
1869 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1870 dst_if = bif->bif_ifp;
1871 if (dst_if == src_if)
1872 continue;
1873
1874 if (bif->bif_flags & IFBIF_STP) {
1875 switch (bif->bif_state) {
1876 case BSTP_IFSTATE_BLOCKING:
1877 case BSTP_IFSTATE_DISABLED:
1878 continue;
1879 }
1880 }
1881
1882 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
1883 continue;
1884
1885 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1886 continue;
1887
1888 if (!used && LIST_NEXT(bif, bif_next) == NULL) {
1889 mc = m;
1890 used = true;
1891 } else {
1892 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1893 if (mc == NULL) {
1894 sc->sc_if.if_oerrors++;
1895 continue;
1896 }
1897 }
1898
1899 bridge_enqueue(sc, dst_if, mc, 1);
1900 }
1901
1902 if (bmcast)
1903 ether_input(src_if, m);
1904 else if (!used)
1905 m_freem(m);
1906
1907 BRIDGE_UNLOCK(sc);
1908 }
1909
1910 /*
1911 * bridge_rtupdate:
1912 *
1913 * Add a bridge routing entry.
1914 */
1915 static int
1916 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1917 struct ifnet *dst_if, int setflags, uint8_t flags)
1918 {
1919 struct bridge_rtnode *brt;
1920 int error = 0;
1921
1922 mutex_enter(sc->sc_rtlist_lock);
1923
1924 /*
1925 * A route for this destination might already exist. If so,
1926 * update it, otherwise create a new one.
1927 */
1928 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
1929 if (sc->sc_brtcnt >= sc->sc_brtmax) {
1930 error = ENOSPC;
1931 goto out;
1932 }
1933
1934 /*
1935 * Allocate a new bridge forwarding node, and
1936 * initialize the expiration time and Ethernet
1937 * address.
1938 */
1939 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
1940 if (brt == NULL) {
1941 error = ENOMEM;
1942 goto out;
1943 }
1944
1945 memset(brt, 0, sizeof(*brt));
1946 brt->brt_expire = time_uptime + sc->sc_brttimeout;
1947 brt->brt_flags = IFBAF_DYNAMIC;
1948 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
1949
1950 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
1951 pool_put(&bridge_rtnode_pool, brt);
1952 goto out;
1953 }
1954 }
1955
1956 brt->brt_ifp = dst_if;
1957 if (setflags) {
1958 brt->brt_flags = flags;
1959 if (flags & IFBAF_STATIC)
1960 brt->brt_expire = 0;
1961 else
1962 brt->brt_expire = time_uptime + sc->sc_brttimeout;
1963 }
1964
1965 out:
1966 mutex_exit(sc->sc_rtlist_lock);
1967
1968 return error;
1969 }
1970
1971 /*
1972 * bridge_rtlookup:
1973 *
1974 * Lookup the destination interface for an address.
1975 */
1976 static struct ifnet *
1977 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
1978 {
1979 struct bridge_rtnode *brt;
1980 struct ifnet *ifs = NULL;
1981
1982 mutex_enter(sc->sc_rtlist_lock);
1983
1984 brt = bridge_rtnode_lookup(sc, addr);
1985 if (brt != NULL)
1986 ifs = brt->brt_ifp;
1987
1988 mutex_exit(sc->sc_rtlist_lock);
1989
1990 return ifs;
1991 }
1992
1993 /*
1994 * bridge_rttrim:
1995 *
1996 * Trim the routine table so that we have a number
1997 * of routing entries less than or equal to the
1998 * maximum number.
1999 */
2000 static void
2001 bridge_rttrim(struct bridge_softc *sc)
2002 {
2003 struct bridge_rtnode *brt, *nbrt;
2004
2005 mutex_enter(sc->sc_rtlist_lock);
2006
2007 /* Make sure we actually need to do this. */
2008 if (sc->sc_brtcnt <= sc->sc_brtmax)
2009 goto out;
2010
2011 /* Force an aging cycle; this might trim enough addresses. */
2012 bridge_rtage(sc);
2013 if (sc->sc_brtcnt <= sc->sc_brtmax)
2014 goto out;
2015
2016 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2017 nbrt = LIST_NEXT(brt, brt_list);
2018 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2019 bridge_rtnode_destroy(sc, brt);
2020 if (sc->sc_brtcnt <= sc->sc_brtmax)
2021 goto out;
2022 }
2023 }
2024
2025 out:
2026 mutex_exit(sc->sc_rtlist_lock);
2027
2028 return;
2029 }
2030
2031 /*
2032 * bridge_timer:
2033 *
2034 * Aging timer for the bridge.
2035 */
2036 static void
2037 bridge_timer(void *arg)
2038 {
2039 struct bridge_softc *sc = arg;
2040
2041 mutex_enter(sc->sc_rtlist_lock);
2042
2043 bridge_rtage(sc);
2044
2045 if (sc->sc_if.if_flags & IFF_RUNNING)
2046 callout_reset(&sc->sc_brcallout,
2047 bridge_rtable_prune_period * hz, bridge_timer, sc);
2048
2049 mutex_exit(sc->sc_rtlist_lock);
2050 }
2051
2052 /*
2053 * bridge_rtage:
2054 *
2055 * Perform an aging cycle.
2056 */
2057 static void
2058 bridge_rtage(struct bridge_softc *sc)
2059 {
2060 struct bridge_rtnode *brt, *nbrt;
2061
2062 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2063
2064 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2065 nbrt = LIST_NEXT(brt, brt_list);
2066 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2067 if (time_uptime >= brt->brt_expire)
2068 bridge_rtnode_destroy(sc, brt);
2069 }
2070 }
2071 }
2072
2073 /*
2074 * bridge_rtflush:
2075 *
2076 * Remove all dynamic addresses from the bridge.
2077 */
2078 static void
2079 bridge_rtflush(struct bridge_softc *sc, int full)
2080 {
2081 struct bridge_rtnode *brt, *nbrt;
2082
2083 mutex_enter(sc->sc_rtlist_lock);
2084
2085 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2086 nbrt = LIST_NEXT(brt, brt_list);
2087 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2088 bridge_rtnode_destroy(sc, brt);
2089 }
2090
2091 mutex_exit(sc->sc_rtlist_lock);
2092 }
2093
2094 /*
2095 * bridge_rtdaddr:
2096 *
2097 * Remove an address from the table.
2098 */
2099 static int
2100 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2101 {
2102 struct bridge_rtnode *brt;
2103 int error = 0;
2104
2105 mutex_enter(sc->sc_rtlist_lock);
2106
2107 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
2108 error = ENOENT;
2109 goto out;
2110 }
2111
2112 bridge_rtnode_destroy(sc, brt);
2113
2114 out:
2115 mutex_exit(sc->sc_rtlist_lock);
2116
2117 return error;
2118 }
2119
2120 /*
2121 * bridge_rtdelete:
2122 *
2123 * Delete routes to a speicifc member interface.
2124 */
2125 static void
2126 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
2127 {
2128 struct bridge_rtnode *brt, *nbrt;
2129
2130 mutex_enter(sc->sc_rtlist_lock);
2131
2132 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2133 nbrt = LIST_NEXT(brt, brt_list);
2134 if (brt->brt_ifp == ifp)
2135 bridge_rtnode_destroy(sc, brt);
2136 }
2137
2138 mutex_exit(sc->sc_rtlist_lock);
2139 }
2140
2141 /*
2142 * bridge_rtable_init:
2143 *
2144 * Initialize the route table for this bridge.
2145 */
2146 static int
2147 bridge_rtable_init(struct bridge_softc *sc)
2148 {
2149 int i;
2150
2151 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2152 M_DEVBUF, M_NOWAIT);
2153 if (sc->sc_rthash == NULL)
2154 return (ENOMEM);
2155
2156 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2157 LIST_INIT(&sc->sc_rthash[i]);
2158
2159 sc->sc_rthash_key = cprng_fast32();
2160
2161 LIST_INIT(&sc->sc_rtlist);
2162
2163 sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
2164
2165 return (0);
2166 }
2167
2168 /*
2169 * bridge_rtable_fini:
2170 *
2171 * Deconstruct the route table for this bridge.
2172 */
2173 static void
2174 bridge_rtable_fini(struct bridge_softc *sc)
2175 {
2176
2177 free(sc->sc_rthash, M_DEVBUF);
2178 if (sc->sc_rtlist_lock)
2179 mutex_obj_free(sc->sc_rtlist_lock);
2180 }
2181
2182 /*
2183 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2184 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2185 */
2186 #define mix(a, b, c) \
2187 do { \
2188 a -= b; a -= c; a ^= (c >> 13); \
2189 b -= c; b -= a; b ^= (a << 8); \
2190 c -= a; c -= b; c ^= (b >> 13); \
2191 a -= b; a -= c; a ^= (c >> 12); \
2192 b -= c; b -= a; b ^= (a << 16); \
2193 c -= a; c -= b; c ^= (b >> 5); \
2194 a -= b; a -= c; a ^= (c >> 3); \
2195 b -= c; b -= a; b ^= (a << 10); \
2196 c -= a; c -= b; c ^= (b >> 15); \
2197 } while (/*CONSTCOND*/0)
2198
2199 static inline uint32_t
2200 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2201 {
2202 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2203
2204 b += addr[5] << 8;
2205 b += addr[4];
2206 a += addr[3] << 24;
2207 a += addr[2] << 16;
2208 a += addr[1] << 8;
2209 a += addr[0];
2210
2211 mix(a, b, c);
2212
2213 return (c & BRIDGE_RTHASH_MASK);
2214 }
2215
2216 #undef mix
2217
2218 /*
2219 * bridge_rtnode_lookup:
2220 *
2221 * Look up a bridge route node for the specified destination.
2222 */
2223 static struct bridge_rtnode *
2224 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2225 {
2226 struct bridge_rtnode *brt;
2227 uint32_t hash;
2228 int dir;
2229
2230 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2231
2232 hash = bridge_rthash(sc, addr);
2233 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2234 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2235 if (dir == 0)
2236 return (brt);
2237 if (dir > 0)
2238 return (NULL);
2239 }
2240
2241 return (NULL);
2242 }
2243
2244 /*
2245 * bridge_rtnode_insert:
2246 *
2247 * Insert the specified bridge node into the route table. We
2248 * assume the entry is not already in the table.
2249 */
2250 static int
2251 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2252 {
2253 struct bridge_rtnode *lbrt;
2254 uint32_t hash;
2255 int dir;
2256
2257 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2258
2259 hash = bridge_rthash(sc, brt->brt_addr);
2260
2261 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2262 if (lbrt == NULL) {
2263 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2264 goto out;
2265 }
2266
2267 do {
2268 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2269 if (dir == 0)
2270 return (EEXIST);
2271 if (dir > 0) {
2272 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2273 goto out;
2274 }
2275 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2276 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2277 goto out;
2278 }
2279 lbrt = LIST_NEXT(lbrt, brt_hash);
2280 } while (lbrt != NULL);
2281
2282 #ifdef DIAGNOSTIC
2283 panic("bridge_rtnode_insert: impossible");
2284 #endif
2285
2286 out:
2287 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2288 sc->sc_brtcnt++;
2289
2290 return (0);
2291 }
2292
2293 /*
2294 * bridge_rtnode_destroy:
2295 *
2296 * Destroy a bridge rtnode.
2297 */
2298 static void
2299 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2300 {
2301
2302 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2303
2304 LIST_REMOVE(brt, brt_hash);
2305
2306 LIST_REMOVE(brt, brt_list);
2307 sc->sc_brtcnt--;
2308 pool_put(&bridge_rtnode_pool, brt);
2309 }
2310
2311 #if defined(BRIDGE_IPF)
2312 extern pfil_head_t *inet_pfil_hook; /* XXX */
2313 extern pfil_head_t *inet6_pfil_hook; /* XXX */
2314
2315 /*
2316 * Send bridge packets through IPF if they are one of the types IPF can deal
2317 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without
2318 * question.)
2319 */
2320 static int
2321 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
2322 {
2323 int snap, error;
2324 struct ether_header *eh1, eh2;
2325 struct llc llc1;
2326 uint16_t ether_type;
2327
2328 snap = 0;
2329 error = -1; /* Default error if not error == 0 */
2330 eh1 = mtod(*mp, struct ether_header *);
2331 ether_type = ntohs(eh1->ether_type);
2332
2333 /*
2334 * Check for SNAP/LLC.
2335 */
2336 if (ether_type < ETHERMTU) {
2337 struct llc *llc2 = (struct llc *)(eh1 + 1);
2338
2339 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2340 llc2->llc_dsap == LLC_SNAP_LSAP &&
2341 llc2->llc_ssap == LLC_SNAP_LSAP &&
2342 llc2->llc_control == LLC_UI) {
2343 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2344 snap = 1;
2345 }
2346 }
2347
2348 /*
2349 * If we're trying to filter bridge traffic, don't look at anything
2350 * other than IP and ARP traffic. If the filter doesn't understand
2351 * IPv6, don't allow IPv6 through the bridge either. This is lame
2352 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2353 * but of course we don't have an AppleTalk filter to begin with.
2354 * (Note that since IPF doesn't understand ARP it will pass *ALL*
2355 * ARP traffic.)
2356 */
2357 switch (ether_type) {
2358 case ETHERTYPE_ARP:
2359 case ETHERTYPE_REVARP:
2360 return 0; /* Automatically pass */
2361 case ETHERTYPE_IP:
2362 # ifdef INET6
2363 case ETHERTYPE_IPV6:
2364 # endif /* INET6 */
2365 break;
2366 default:
2367 goto bad;
2368 }
2369
2370 /* Strip off the Ethernet header and keep a copy. */
2371 m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
2372 m_adj(*mp, ETHER_HDR_LEN);
2373
2374 /* Strip off snap header, if present */
2375 if (snap) {
2376 m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
2377 m_adj(*mp, sizeof(struct llc));
2378 }
2379
2380 /*
2381 * Check basic packet sanity and run IPF through pfil.
2382 */
2383 KASSERT(!cpu_intr_p());
2384 switch (ether_type)
2385 {
2386 case ETHERTYPE_IP :
2387 error = (dir == PFIL_IN) ? bridge_ip_checkbasic(mp) : 0;
2388 if (error == 0)
2389 error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
2390 break;
2391 # ifdef INET6
2392 case ETHERTYPE_IPV6 :
2393 error = (dir == PFIL_IN) ? bridge_ip6_checkbasic(mp) : 0;
2394 if (error == 0)
2395 error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
2396 break;
2397 # endif
2398 default :
2399 error = 0;
2400 break;
2401 }
2402
2403 if (*mp == NULL)
2404 return error;
2405 if (error != 0)
2406 goto bad;
2407
2408 error = -1;
2409
2410 /*
2411 * Finally, put everything back the way it was and return
2412 */
2413 if (snap) {
2414 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2415 if (*mp == NULL)
2416 return error;
2417 bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
2418 }
2419
2420 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2421 if (*mp == NULL)
2422 return error;
2423 bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
2424
2425 return 0;
2426
2427 bad:
2428 m_freem(*mp);
2429 *mp = NULL;
2430 return error;
2431 }
2432
2433 /*
2434 * Perform basic checks on header size since
2435 * IPF assumes ip_input has already processed
2436 * it for it. Cut-and-pasted from ip_input.c.
2437 * Given how simple the IPv6 version is,
2438 * does the IPv4 version really need to be
2439 * this complicated?
2440 *
2441 * XXX Should we update ipstat here, or not?
2442 * XXX Right now we update ipstat but not
2443 * XXX csum_counter.
2444 */
2445 static int
2446 bridge_ip_checkbasic(struct mbuf **mp)
2447 {
2448 struct mbuf *m = *mp;
2449 struct ip *ip;
2450 int len, hlen;
2451
2452 if (*mp == NULL)
2453 return -1;
2454
2455 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2456 if ((m = m_copyup(m, sizeof(struct ip),
2457 (max_linkhdr + 3) & ~3)) == NULL) {
2458 /* XXXJRT new stat, please */
2459 ip_statinc(IP_STAT_TOOSMALL);
2460 goto bad;
2461 }
2462 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
2463 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2464 ip_statinc(IP_STAT_TOOSMALL);
2465 goto bad;
2466 }
2467 }
2468 ip = mtod(m, struct ip *);
2469 if (ip == NULL) goto bad;
2470
2471 if (ip->ip_v != IPVERSION) {
2472 ip_statinc(IP_STAT_BADVERS);
2473 goto bad;
2474 }
2475 hlen = ip->ip_hl << 2;
2476 if (hlen < sizeof(struct ip)) { /* minimum header length */
2477 ip_statinc(IP_STAT_BADHLEN);
2478 goto bad;
2479 }
2480 if (hlen > m->m_len) {
2481 if ((m = m_pullup(m, hlen)) == 0) {
2482 ip_statinc(IP_STAT_BADHLEN);
2483 goto bad;
2484 }
2485 ip = mtod(m, struct ip *);
2486 if (ip == NULL) goto bad;
2487 }
2488
2489 switch (m->m_pkthdr.csum_flags &
2490 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
2491 M_CSUM_IPv4_BAD)) {
2492 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2493 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2494 goto bad;
2495
2496 case M_CSUM_IPv4:
2497 /* Checksum was okay. */
2498 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2499 break;
2500
2501 default:
2502 /* Must compute it ourselves. */
2503 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2504 if (in_cksum(m, hlen) != 0)
2505 goto bad;
2506 break;
2507 }
2508
2509 /* Retrieve the packet length. */
2510 len = ntohs(ip->ip_len);
2511
2512 /*
2513 * Check for additional length bogosity
2514 */
2515 if (len < hlen) {
2516 ip_statinc(IP_STAT_BADLEN);
2517 goto bad;
2518 }
2519
2520 /*
2521 * Check that the amount of data in the buffers
2522 * is as at least much as the IP header would have us expect.
2523 * Drop packet if shorter than we expect.
2524 */
2525 if (m->m_pkthdr.len < len) {
2526 ip_statinc(IP_STAT_TOOSHORT);
2527 goto bad;
2528 }
2529
2530 /* Checks out, proceed */
2531 *mp = m;
2532 return 0;
2533
2534 bad:
2535 *mp = m;
2536 return -1;
2537 }
2538
2539 # ifdef INET6
2540 /*
2541 * Same as above, but for IPv6.
2542 * Cut-and-pasted from ip6_input.c.
2543 * XXX Should we update ip6stat, or not?
2544 */
2545 static int
2546 bridge_ip6_checkbasic(struct mbuf **mp)
2547 {
2548 struct mbuf *m = *mp;
2549 struct ip6_hdr *ip6;
2550
2551 /*
2552 * If the IPv6 header is not aligned, slurp it up into a new
2553 * mbuf with space for link headers, in the event we forward
2554 * it. Otherwise, if it is aligned, make sure the entire base
2555 * IPv6 header is in the first mbuf of the chain.
2556 */
2557 if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2558 struct ifnet *inifp = m->m_pkthdr.rcvif;
2559 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2560 (max_linkhdr + 3) & ~3)) == NULL) {
2561 /* XXXJRT new stat, please */
2562 ip6_statinc(IP6_STAT_TOOSMALL);
2563 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2564 goto bad;
2565 }
2566 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2567 struct ifnet *inifp = m->m_pkthdr.rcvif;
2568 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2569 ip6_statinc(IP6_STAT_TOOSMALL);
2570 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2571 goto bad;
2572 }
2573 }
2574
2575 ip6 = mtod(m, struct ip6_hdr *);
2576
2577 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2578 ip6_statinc(IP6_STAT_BADVERS);
2579 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2580 goto bad;
2581 }
2582
2583 /* Checks out, proceed */
2584 *mp = m;
2585 return 0;
2586
2587 bad:
2588 *mp = m;
2589 return -1;
2590 }
2591 # endif /* INET6 */
2592 #endif /* BRIDGE_IPF */
2593