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