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