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