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