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