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