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