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