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