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