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