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