if_bridge.c revision 1.134.6.1 1 /* $NetBSD: if_bridge.c,v 1.134.6.1 2017/10/02 13:33:41 martin 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.134.6.1 2017/10/02 13:33:41 martin 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 = splsoftnet(); \
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 = splsoftnet();
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 splsoftnet(). */
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 = splsoftnet();
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 splsoftnet(). */
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 (ifs->if_bridge == sc) {
739 error = EEXIST;
740 goto out;
741 }
742
743 if (ifs->if_bridge != NULL) {
744 error = EBUSY;
745 goto out;
746 }
747
748 if (ifs->_if_input != ether_input) {
749 error = EINVAL;
750 goto out;
751 }
752
753 /* FIXME: doesn't work with non-IFF_SIMPLEX interfaces */
754 if ((ifs->if_flags & IFF_SIMPLEX) == 0) {
755 error = EINVAL;
756 goto out;
757 }
758
759 bif = kmem_alloc(sizeof(*bif), KM_SLEEP);
760
761 switch (ifs->if_type) {
762 case IFT_ETHER:
763 if (sc->sc_if.if_mtu != ifs->if_mtu) {
764 error = EINVAL;
765 goto out;
766 }
767 /* FALLTHROUGH */
768 case IFT_L2TP:
769 if ((error = ether_enable_vlan_mtu(ifs)) > 0)
770 goto out;
771 /*
772 * Place the interface into promiscuous mode.
773 */
774 error = ifpromisc(ifs, 1);
775 if (error)
776 goto out;
777 break;
778 default:
779 error = EINVAL;
780 goto out;
781 }
782
783 bif->bif_ifp = ifs;
784 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
785 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
786 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
787 PSLIST_ENTRY_INIT(bif, bif_next);
788 psref_target_init(&bif->bif_psref, bridge_psref_class);
789
790 BRIDGE_LOCK(sc);
791
792 ifs->if_bridge = sc;
793 ifs->if_bridgeif = bif;
794 PSLIST_WRITER_INSERT_HEAD(&sc->sc_iflist_psref.bip_iflist, bif, bif_next);
795 ifs->_if_input = bridge_input;
796
797 BRIDGE_UNLOCK(sc);
798
799 if (sc->sc_if.if_flags & IFF_RUNNING)
800 bstp_initialization(sc);
801 else
802 bstp_stop(sc);
803
804 out:
805 if_put(ifs, &psref);
806 if (error) {
807 if (bif != NULL)
808 kmem_free(bif, sizeof(*bif));
809 }
810 return (error);
811 }
812
813 static int
814 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
815 {
816 struct ifbreq *req = arg;
817 const char *name = req->ifbr_ifsname;
818 struct bridge_iflist *bif;
819 struct ifnet *ifs;
820
821 BRIDGE_LOCK(sc);
822
823 /*
824 * Don't use bridge_lookup_member. We want to get a member
825 * with bif_refs == 0.
826 */
827 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
828 ifs = bif->bif_ifp;
829 if (strcmp(ifs->if_xname, name) == 0)
830 break;
831 }
832
833 if (bif == NULL) {
834 BRIDGE_UNLOCK(sc);
835 return ENOENT;
836 }
837
838 bridge_delete_member(sc, bif);
839
840 BRIDGE_UNLOCK(sc);
841
842 switch (ifs->if_type) {
843 case IFT_ETHER:
844 case IFT_L2TP:
845 /*
846 * Take the interface out of promiscuous mode.
847 * Don't call it with holding a spin lock.
848 */
849 (void) ifpromisc(ifs, 0);
850 (void) ether_disable_vlan_mtu(ifs);
851 break;
852 default:
853 #ifdef DIAGNOSTIC
854 panic("bridge_delete_member: impossible");
855 #endif
856 break;
857 }
858
859 bridge_rtdelete(sc, ifs);
860
861 if (sc->sc_if.if_flags & IFF_RUNNING)
862 bstp_initialization(sc);
863
864 return 0;
865 }
866
867 static int
868 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
869 {
870 struct ifbreq *req = arg;
871 struct bridge_iflist *bif;
872 struct psref psref;
873
874 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
875 if (bif == NULL)
876 return (ENOENT);
877
878 req->ifbr_ifsflags = bif->bif_flags;
879 req->ifbr_state = bif->bif_state;
880 req->ifbr_priority = bif->bif_priority;
881 req->ifbr_path_cost = bif->bif_path_cost;
882 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
883
884 bridge_release_member(sc, bif, &psref);
885
886 return (0);
887 }
888
889 static int
890 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
891 {
892 struct ifbreq *req = arg;
893 struct bridge_iflist *bif;
894 struct psref psref;
895
896 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
897 if (bif == NULL)
898 return (ENOENT);
899
900 if (req->ifbr_ifsflags & IFBIF_STP) {
901 switch (bif->bif_ifp->if_type) {
902 case IFT_ETHER:
903 case IFT_L2TP:
904 /* These can do spanning tree. */
905 break;
906
907 default:
908 /* Nothing else can. */
909 bridge_release_member(sc, bif, &psref);
910 return (EINVAL);
911 }
912 }
913
914 bif->bif_flags = req->ifbr_ifsflags;
915
916 bridge_release_member(sc, bif, &psref);
917
918 if (sc->sc_if.if_flags & IFF_RUNNING)
919 bstp_initialization(sc);
920
921 return (0);
922 }
923
924 static int
925 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
926 {
927 struct ifbrparam *param = arg;
928
929 sc->sc_brtmax = param->ifbrp_csize;
930 bridge_rttrim(sc);
931
932 return (0);
933 }
934
935 static int
936 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
937 {
938 struct ifbrparam *param = arg;
939
940 param->ifbrp_csize = sc->sc_brtmax;
941
942 return (0);
943 }
944
945 static int
946 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
947 {
948 struct ifbifconf *bifc = arg;
949 struct bridge_iflist *bif;
950 struct ifbreq *breqs;
951 int i, count, error = 0;
952
953 retry:
954 BRIDGE_LOCK(sc);
955 count = 0;
956 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
957 count++;
958 BRIDGE_UNLOCK(sc);
959
960 if (count == 0) {
961 bifc->ifbic_len = 0;
962 return 0;
963 }
964
965 if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
966 /* Tell that a larger buffer is needed */
967 bifc->ifbic_len = sizeof(*breqs) * count;
968 return 0;
969 }
970
971 breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP);
972
973 BRIDGE_LOCK(sc);
974
975 i = 0;
976 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
977 i++;
978 if (i > count) {
979 /*
980 * The number of members has been increased.
981 * We need more memory!
982 */
983 BRIDGE_UNLOCK(sc);
984 kmem_free(breqs, sizeof(*breqs) * count);
985 goto retry;
986 }
987
988 i = 0;
989 BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
990 struct ifbreq *breq = &breqs[i++];
991 memset(breq, 0, sizeof(*breq));
992
993 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
994 sizeof(breq->ifbr_ifsname));
995 breq->ifbr_ifsflags = bif->bif_flags;
996 breq->ifbr_state = bif->bif_state;
997 breq->ifbr_priority = bif->bif_priority;
998 breq->ifbr_path_cost = bif->bif_path_cost;
999 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1000 }
1001
1002 /* Don't call copyout with holding the mutex */
1003 BRIDGE_UNLOCK(sc);
1004
1005 for (i = 0; i < count; i++) {
1006 error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs));
1007 if (error)
1008 break;
1009 }
1010 bifc->ifbic_len = sizeof(*breqs) * i;
1011
1012 kmem_free(breqs, sizeof(*breqs) * count);
1013
1014 return error;
1015 }
1016
1017 static int
1018 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1019 {
1020 struct ifbaconf *bac = arg;
1021 struct bridge_rtnode *brt;
1022 struct ifbareq bareq;
1023 int count = 0, error = 0, len;
1024
1025 if (bac->ifbac_len == 0)
1026 return (0);
1027
1028 BRIDGE_RT_LOCK(sc);
1029
1030 len = bac->ifbac_len;
1031 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1032 if (len < sizeof(bareq))
1033 goto out;
1034 memset(&bareq, 0, sizeof(bareq));
1035 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1036 sizeof(bareq.ifba_ifsname));
1037 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1038 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1039 bareq.ifba_expire = brt->brt_expire - time_uptime;
1040 } else
1041 bareq.ifba_expire = 0;
1042 bareq.ifba_flags = brt->brt_flags;
1043
1044 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1045 if (error)
1046 goto out;
1047 count++;
1048 len -= sizeof(bareq);
1049 }
1050 out:
1051 BRIDGE_RT_UNLOCK(sc);
1052
1053 bac->ifbac_len = sizeof(bareq) * count;
1054 return (error);
1055 }
1056
1057 static int
1058 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1059 {
1060 struct ifbareq *req = arg;
1061 struct bridge_iflist *bif;
1062 int error;
1063 struct psref psref;
1064
1065 bif = bridge_lookup_member(sc, req->ifba_ifsname, &psref);
1066 if (bif == NULL)
1067 return (ENOENT);
1068
1069 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1070 req->ifba_flags);
1071
1072 bridge_release_member(sc, bif, &psref);
1073
1074 return (error);
1075 }
1076
1077 static int
1078 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1079 {
1080 struct ifbrparam *param = arg;
1081
1082 sc->sc_brttimeout = param->ifbrp_ctime;
1083
1084 return (0);
1085 }
1086
1087 static int
1088 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1089 {
1090 struct ifbrparam *param = arg;
1091
1092 param->ifbrp_ctime = sc->sc_brttimeout;
1093
1094 return (0);
1095 }
1096
1097 static int
1098 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1099 {
1100 struct ifbareq *req = arg;
1101
1102 return (bridge_rtdaddr(sc, req->ifba_dst));
1103 }
1104
1105 static int
1106 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1107 {
1108 struct ifbreq *req = arg;
1109
1110 bridge_rtflush(sc, req->ifbr_ifsflags);
1111
1112 return (0);
1113 }
1114
1115 static int
1116 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1117 {
1118 struct ifbrparam *param = arg;
1119
1120 param->ifbrp_prio = sc->sc_bridge_priority;
1121
1122 return (0);
1123 }
1124
1125 static int
1126 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1127 {
1128 struct ifbrparam *param = arg;
1129
1130 sc->sc_bridge_priority = param->ifbrp_prio;
1131
1132 if (sc->sc_if.if_flags & IFF_RUNNING)
1133 bstp_initialization(sc);
1134
1135 return (0);
1136 }
1137
1138 static int
1139 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1140 {
1141 struct ifbrparam *param = arg;
1142
1143 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1144
1145 return (0);
1146 }
1147
1148 static int
1149 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1150 {
1151 struct ifbrparam *param = arg;
1152
1153 if (param->ifbrp_hellotime == 0)
1154 return (EINVAL);
1155 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1156
1157 if (sc->sc_if.if_flags & IFF_RUNNING)
1158 bstp_initialization(sc);
1159
1160 return (0);
1161 }
1162
1163 static int
1164 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1165 {
1166 struct ifbrparam *param = arg;
1167
1168 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1169
1170 return (0);
1171 }
1172
1173 static int
1174 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1175 {
1176 struct ifbrparam *param = arg;
1177
1178 if (param->ifbrp_fwddelay == 0)
1179 return (EINVAL);
1180 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1181
1182 if (sc->sc_if.if_flags & IFF_RUNNING)
1183 bstp_initialization(sc);
1184
1185 return (0);
1186 }
1187
1188 static int
1189 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1190 {
1191 struct ifbrparam *param = arg;
1192
1193 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1194
1195 return (0);
1196 }
1197
1198 static int
1199 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1200 {
1201 struct ifbrparam *param = arg;
1202
1203 if (param->ifbrp_maxage == 0)
1204 return (EINVAL);
1205 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1206
1207 if (sc->sc_if.if_flags & IFF_RUNNING)
1208 bstp_initialization(sc);
1209
1210 return (0);
1211 }
1212
1213 static int
1214 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1215 {
1216 struct ifbreq *req = arg;
1217 struct bridge_iflist *bif;
1218 struct psref psref;
1219
1220 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
1221 if (bif == NULL)
1222 return (ENOENT);
1223
1224 bif->bif_priority = req->ifbr_priority;
1225
1226 if (sc->sc_if.if_flags & IFF_RUNNING)
1227 bstp_initialization(sc);
1228
1229 bridge_release_member(sc, bif, &psref);
1230
1231 return (0);
1232 }
1233
1234 #if defined(BRIDGE_IPF)
1235 static int
1236 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
1237 {
1238 struct ifbrparam *param = arg;
1239
1240 param->ifbrp_filter = sc->sc_filter_flags;
1241
1242 return (0);
1243 }
1244
1245 static int
1246 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
1247 {
1248 struct ifbrparam *param = arg;
1249 uint32_t nflags, oflags;
1250
1251 if (param->ifbrp_filter & ~IFBF_FILT_MASK)
1252 return (EINVAL);
1253
1254 nflags = param->ifbrp_filter;
1255 oflags = sc->sc_filter_flags;
1256
1257 if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
1258 pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1259 sc->sc_if.if_pfil);
1260 }
1261 if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
1262 pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1263 sc->sc_if.if_pfil);
1264 }
1265
1266 sc->sc_filter_flags = nflags;
1267
1268 return (0);
1269 }
1270 #endif /* BRIDGE_IPF */
1271
1272 static int
1273 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1274 {
1275 struct ifbreq *req = arg;
1276 struct bridge_iflist *bif;
1277 struct psref psref;
1278
1279 bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
1280 if (bif == NULL)
1281 return (ENOENT);
1282
1283 bif->bif_path_cost = req->ifbr_path_cost;
1284
1285 if (sc->sc_if.if_flags & IFF_RUNNING)
1286 bstp_initialization(sc);
1287
1288 bridge_release_member(sc, bif, &psref);
1289
1290 return (0);
1291 }
1292
1293 /*
1294 * bridge_ifdetach:
1295 *
1296 * Detach an interface from a bridge. Called when a member
1297 * interface is detaching.
1298 */
1299 void
1300 bridge_ifdetach(struct ifnet *ifp)
1301 {
1302 struct bridge_softc *sc = ifp->if_bridge;
1303 struct ifbreq breq;
1304
1305 /* ioctl_lock should prevent this from happening */
1306 KASSERT(sc != NULL);
1307
1308 memset(&breq, 0, sizeof(breq));
1309 strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
1310
1311 (void) bridge_ioctl_del(sc, &breq);
1312 }
1313
1314 /*
1315 * bridge_init:
1316 *
1317 * Initialize a bridge interface.
1318 */
1319 static int
1320 bridge_init(struct ifnet *ifp)
1321 {
1322 struct bridge_softc *sc = ifp->if_softc;
1323
1324 if (ifp->if_flags & IFF_RUNNING)
1325 return (0);
1326
1327 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1328 bridge_timer, sc);
1329
1330 ifp->if_flags |= IFF_RUNNING;
1331 bstp_initialization(sc);
1332 return (0);
1333 }
1334
1335 /*
1336 * bridge_stop:
1337 *
1338 * Stop the bridge interface.
1339 */
1340 static void
1341 bridge_stop(struct ifnet *ifp, int disable)
1342 {
1343 struct bridge_softc *sc = ifp->if_softc;
1344
1345 if ((ifp->if_flags & IFF_RUNNING) == 0)
1346 return;
1347
1348 callout_stop(&sc->sc_brcallout);
1349 bstp_stop(sc);
1350
1351 bridge_rtflush(sc, IFBF_FLUSHDYN);
1352
1353 ifp->if_flags &= ~IFF_RUNNING;
1354 }
1355
1356 /*
1357 * bridge_enqueue:
1358 *
1359 * Enqueue a packet on a bridge member interface.
1360 */
1361 void
1362 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
1363 int runfilt)
1364 {
1365 int len, error;
1366 short mflags;
1367
1368 /*
1369 * Clear any in-bound checksum flags for this packet.
1370 */
1371 m->m_pkthdr.csum_flags = 0;
1372
1373 if (runfilt) {
1374 if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1375 dst_ifp, PFIL_OUT) != 0) {
1376 if (m != NULL)
1377 m_freem(m);
1378 return;
1379 }
1380 if (m == NULL)
1381 return;
1382 }
1383
1384 #ifdef ALTQ
1385 KERNEL_LOCK(1, NULL);
1386 /*
1387 * If ALTQ is enabled on the member interface, do
1388 * classification; the queueing discipline might
1389 * not require classification, but might require
1390 * the address family/header pointer in the pktattr.
1391 */
1392 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1393 /* XXX IFT_ETHER */
1394 altq_etherclassify(&dst_ifp->if_snd, m);
1395 }
1396 KERNEL_UNLOCK_ONE(NULL);
1397 #endif /* ALTQ */
1398
1399 len = m->m_pkthdr.len;
1400 mflags = m->m_flags;
1401
1402 error = if_transmit_lock(dst_ifp, m);
1403 if (error) {
1404 /* mbuf is already freed */
1405 sc->sc_if.if_oerrors++;
1406 return;
1407 }
1408
1409 sc->sc_if.if_opackets++;
1410 sc->sc_if.if_obytes += len;
1411 if (mflags & M_MCAST)
1412 sc->sc_if.if_omcasts++;
1413 }
1414
1415 /*
1416 * bridge_output:
1417 *
1418 * Send output from a bridge member interface. This
1419 * performs the bridging function for locally originated
1420 * packets.
1421 *
1422 * The mbuf has the Ethernet header already attached. We must
1423 * enqueue or free the mbuf before returning.
1424 */
1425 int
1426 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
1427 const struct rtentry *rt)
1428 {
1429 struct ether_header *eh;
1430 struct ifnet *dst_if;
1431 struct bridge_softc *sc;
1432 int s;
1433
1434 /*
1435 * bridge_output() is called from ether_output(), furthermore
1436 * ifp argument doesn't point to bridge(4). So, don't assert
1437 * IFEF_OUTPUT_MPSAFE here.
1438 */
1439
1440 if (m->m_len < ETHER_HDR_LEN) {
1441 m = m_pullup(m, ETHER_HDR_LEN);
1442 if (m == NULL)
1443 return (0);
1444 }
1445
1446 eh = mtod(m, struct ether_header *);
1447 sc = ifp->if_bridge;
1448
1449 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1450 if (memcmp(etherbroadcastaddr,
1451 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1452 m->m_flags |= M_BCAST;
1453 else
1454 m->m_flags |= M_MCAST;
1455 }
1456
1457 /*
1458 * If bridge is down, but the original output interface is up,
1459 * go ahead and send out that interface. Otherwise, the packet
1460 * is dropped below.
1461 */
1462 if (__predict_false(sc == NULL) ||
1463 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1464 dst_if = ifp;
1465 goto sendunicast;
1466 }
1467
1468 /*
1469 * If the packet is a multicast, or we don't know a better way to
1470 * get there, send to all interfaces.
1471 */
1472 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0)
1473 dst_if = NULL;
1474 else
1475 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1476 if (dst_if == NULL) {
1477 /* XXX Should call bridge_broadcast, but there are locking
1478 * issues which need resolving first. */
1479 struct bridge_iflist *bif;
1480 struct mbuf *mc;
1481 bool used = false;
1482
1483 BRIDGE_PSZ_RENTER(s);
1484 BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
1485 struct psref psref;
1486
1487 bridge_acquire_member(sc, bif, &psref);
1488 BRIDGE_PSZ_REXIT(s);
1489
1490 dst_if = bif->bif_ifp;
1491 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1492 goto next;
1493
1494 /*
1495 * If this is not the original output interface,
1496 * and the interface is participating in spanning
1497 * tree, make sure the port is in a state that
1498 * allows forwarding.
1499 */
1500 if (dst_if != ifp &&
1501 (bif->bif_flags & IFBIF_STP) != 0) {
1502 switch (bif->bif_state) {
1503 case BSTP_IFSTATE_BLOCKING:
1504 case BSTP_IFSTATE_LISTENING:
1505 case BSTP_IFSTATE_DISABLED:
1506 goto next;
1507 }
1508 }
1509
1510 if (PSLIST_READER_NEXT(bif, struct bridge_iflist,
1511 bif_next) == NULL &&
1512 ((m->m_flags & (M_MCAST | M_BCAST)) == 0 ||
1513 dst_if == ifp))
1514 {
1515 used = true;
1516 mc = m;
1517 } else {
1518 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1519 if (mc == NULL) {
1520 sc->sc_if.if_oerrors++;
1521 goto next;
1522 }
1523 }
1524
1525 bridge_enqueue(sc, dst_if, mc, 0);
1526
1527 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 &&
1528 dst_if != ifp)
1529 {
1530 if (PSLIST_READER_NEXT(bif,
1531 struct bridge_iflist, bif_next) == NULL)
1532 {
1533 used = true;
1534 mc = m;
1535 } else {
1536 mc = m_copym(m, 0, M_COPYALL,
1537 M_DONTWAIT);
1538 if (mc == NULL) {
1539 sc->sc_if.if_oerrors++;
1540 goto next;
1541 }
1542 }
1543
1544 m_set_rcvif(mc, dst_if);
1545 mc->m_flags &= ~M_PROMISC;
1546
1547 #ifndef NET_MPSAFE
1548 s = splsoftnet();
1549 #endif
1550 ether_input(dst_if, mc);
1551 #ifndef NET_MPSAFE
1552 splx(s);
1553 #endif
1554 }
1555
1556 next:
1557 BRIDGE_PSZ_RENTER(s);
1558 bridge_release_member(sc, bif, &psref);
1559
1560 /* Guarantee we don't re-enter the loop as we already
1561 * decided we're at the end. */
1562 if (used)
1563 break;
1564 }
1565 BRIDGE_PSZ_REXIT(s);
1566
1567 if (!used)
1568 m_freem(m);
1569 return (0);
1570 }
1571
1572 sendunicast:
1573 /*
1574 * XXX Spanning tree consideration here?
1575 */
1576
1577 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1578 m_freem(m);
1579 return (0);
1580 }
1581
1582 bridge_enqueue(sc, dst_if, m, 0);
1583
1584 return (0);
1585 }
1586
1587 /*
1588 * bridge_start:
1589 *
1590 * Start output on a bridge.
1591 *
1592 * NOTE: This routine should never be called in this implementation.
1593 */
1594 static void
1595 bridge_start(struct ifnet *ifp)
1596 {
1597
1598 printf("%s: bridge_start() called\n", ifp->if_xname);
1599 }
1600
1601 /*
1602 * bridge_forward:
1603 *
1604 * The forwarding function of the bridge.
1605 */
1606 static void
1607 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1608 {
1609 struct bridge_iflist *bif;
1610 struct ifnet *src_if, *dst_if;
1611 struct ether_header *eh;
1612 struct psref psref;
1613 struct psref psref_src;
1614 DECLARE_LOCK_VARIABLE;
1615
1616 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1617 return;
1618
1619 src_if = m_get_rcvif_psref(m, &psref_src);
1620 if (src_if == NULL) {
1621 /* Interface is being destroyed? */
1622 m_freem(m);
1623 goto out;
1624 }
1625
1626 sc->sc_if.if_ipackets++;
1627 sc->sc_if.if_ibytes += m->m_pkthdr.len;
1628
1629 /*
1630 * Look up the bridge_iflist.
1631 */
1632 bif = bridge_lookup_member_if(sc, src_if, &psref);
1633 if (bif == NULL) {
1634 /* Interface is not a bridge member (anymore?) */
1635 m_freem(m);
1636 goto out;
1637 }
1638
1639 if (bif->bif_flags & IFBIF_STP) {
1640 switch (bif->bif_state) {
1641 case BSTP_IFSTATE_BLOCKING:
1642 case BSTP_IFSTATE_LISTENING:
1643 case BSTP_IFSTATE_DISABLED:
1644 m_freem(m);
1645 bridge_release_member(sc, bif, &psref);
1646 goto out;
1647 }
1648 }
1649
1650 eh = mtod(m, struct ether_header *);
1651
1652 /*
1653 * If the interface is learning, and the source
1654 * address is valid and not multicast, record
1655 * the address.
1656 */
1657 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1658 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1659 (eh->ether_shost[0] == 0 &&
1660 eh->ether_shost[1] == 0 &&
1661 eh->ether_shost[2] == 0 &&
1662 eh->ether_shost[3] == 0 &&
1663 eh->ether_shost[4] == 0 &&
1664 eh->ether_shost[5] == 0) == 0) {
1665 (void) bridge_rtupdate(sc, eh->ether_shost,
1666 src_if, 0, IFBAF_DYNAMIC);
1667 }
1668
1669 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1670 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1671 m_freem(m);
1672 bridge_release_member(sc, bif, &psref);
1673 goto out;
1674 }
1675
1676 bridge_release_member(sc, bif, &psref);
1677
1678 /*
1679 * At this point, the port either doesn't participate
1680 * in spanning tree or it is in the forwarding state.
1681 */
1682
1683 /*
1684 * If the packet is unicast, destined for someone on
1685 * "this" side of the bridge, drop it.
1686 */
1687 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1688 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1689 if (src_if == dst_if) {
1690 m_freem(m);
1691 goto out;
1692 }
1693 } else {
1694 /* ...forward it to all interfaces. */
1695 sc->sc_if.if_imcasts++;
1696 dst_if = NULL;
1697 }
1698
1699 if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) {
1700 if (m != NULL)
1701 m_freem(m);
1702 goto out;
1703 }
1704 if (m == NULL)
1705 goto out;
1706
1707 if (dst_if == NULL) {
1708 bridge_broadcast(sc, src_if, m);
1709 goto out;
1710 }
1711
1712 m_put_rcvif_psref(src_if, &psref_src);
1713 src_if = NULL;
1714
1715 /*
1716 * At this point, we're dealing with a unicast frame
1717 * going to a different interface.
1718 */
1719 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1720 m_freem(m);
1721 goto out;
1722 }
1723
1724 bif = bridge_lookup_member_if(sc, dst_if, &psref);
1725 if (bif == NULL) {
1726 /* Not a member of the bridge (anymore?) */
1727 m_freem(m);
1728 goto out;
1729 }
1730
1731 if (bif->bif_flags & IFBIF_STP) {
1732 switch (bif->bif_state) {
1733 case BSTP_IFSTATE_DISABLED:
1734 case BSTP_IFSTATE_BLOCKING:
1735 m_freem(m);
1736 bridge_release_member(sc, bif, &psref);
1737 goto out;
1738 }
1739 }
1740
1741 bridge_release_member(sc, bif, &psref);
1742
1743 ACQUIRE_GLOBAL_LOCKS();
1744 bridge_enqueue(sc, dst_if, m, 1);
1745 RELEASE_GLOBAL_LOCKS();
1746 out:
1747 if (src_if != NULL)
1748 m_put_rcvif_psref(src_if, &psref_src);
1749 return;
1750 }
1751
1752 static bool
1753 bstp_state_before_learning(struct bridge_iflist *bif)
1754 {
1755 if (bif->bif_flags & IFBIF_STP) {
1756 switch (bif->bif_state) {
1757 case BSTP_IFSTATE_BLOCKING:
1758 case BSTP_IFSTATE_LISTENING:
1759 case BSTP_IFSTATE_DISABLED:
1760 return true;
1761 }
1762 }
1763 return false;
1764 }
1765
1766 static bool
1767 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
1768 {
1769 uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
1770
1771 if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
1772 #if NCARP > 0
1773 || (bif->bif_ifp->if_carp &&
1774 carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
1775 #endif /* NCARP > 0 */
1776 )
1777 return true;
1778
1779 return false;
1780 }
1781
1782 /*
1783 * bridge_input:
1784 *
1785 * Receive input from a member interface. Queue the packet for
1786 * bridging if it is not for us.
1787 */
1788 static void
1789 bridge_input(struct ifnet *ifp, struct mbuf *m)
1790 {
1791 struct bridge_softc *sc = ifp->if_bridge;
1792 struct bridge_iflist *bif;
1793 struct ether_header *eh;
1794 struct psref psref;
1795 int bound;
1796 DECLARE_LOCK_VARIABLE;
1797
1798 KASSERT(!cpu_intr_p());
1799
1800 if (__predict_false(sc == NULL) ||
1801 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1802 ACQUIRE_GLOBAL_LOCKS();
1803 ether_input(ifp, m);
1804 RELEASE_GLOBAL_LOCKS();
1805 return;
1806 }
1807
1808 bound = curlwp_bind();
1809 bif = bridge_lookup_member_if(sc, ifp, &psref);
1810 if (bif == NULL) {
1811 curlwp_bindx(bound);
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 curlwp_bindx(bound);
1864 if (_ifp != NULL) {
1865 m->m_flags &= ~M_PROMISC;
1866 ACQUIRE_GLOBAL_LOCKS();
1867 ether_input(_ifp, m);
1868 RELEASE_GLOBAL_LOCKS();
1869 } else
1870 m_freem(m);
1871 return;
1872 }
1873 }
1874
1875 /* Tap off 802.1D packets; they do not get forwarded. */
1876 if (bif->bif_flags & IFBIF_STP &&
1877 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
1878 bstp_input(sc, bif, m);
1879 bridge_release_member(sc, bif, &psref);
1880 curlwp_bindx(bound);
1881 return;
1882 }
1883
1884 /*
1885 * A normal switch would discard the packet here, but that's not what
1886 * we've done historically. This also prevents some obnoxious behaviour.
1887 */
1888 if (bstp_state_before_learning(bif)) {
1889 bridge_release_member(sc, bif, &psref);
1890 curlwp_bindx(bound);
1891 ACQUIRE_GLOBAL_LOCKS();
1892 ether_input(ifp, m);
1893 RELEASE_GLOBAL_LOCKS();
1894 return;
1895 }
1896
1897 bridge_release_member(sc, bif, &psref);
1898
1899 bridge_forward(sc, m);
1900
1901 curlwp_bindx(bound);
1902 }
1903
1904 /*
1905 * bridge_broadcast:
1906 *
1907 * Send a frame to all interfaces that are members of
1908 * the bridge, except for the one on which the packet
1909 * arrived.
1910 */
1911 static void
1912 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1913 struct mbuf *m)
1914 {
1915 struct bridge_iflist *bif;
1916 struct mbuf *mc;
1917 struct ifnet *dst_if;
1918 bool bmcast;
1919 int s;
1920 DECLARE_LOCK_VARIABLE;
1921
1922 bmcast = m->m_flags & (M_BCAST|M_MCAST);
1923
1924 BRIDGE_PSZ_RENTER(s);
1925 BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
1926 struct psref psref;
1927
1928 bridge_acquire_member(sc, bif, &psref);
1929 BRIDGE_PSZ_REXIT(s);
1930
1931 dst_if = bif->bif_ifp;
1932
1933 if (bif->bif_flags & IFBIF_STP) {
1934 switch (bif->bif_state) {
1935 case BSTP_IFSTATE_BLOCKING:
1936 case BSTP_IFSTATE_DISABLED:
1937 goto next;
1938 }
1939 }
1940
1941 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
1942 goto next;
1943
1944 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1945 goto next;
1946
1947 if (dst_if != src_if) {
1948 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1949 if (mc == NULL) {
1950 sc->sc_if.if_oerrors++;
1951 goto next;
1952 }
1953 ACQUIRE_GLOBAL_LOCKS();
1954 bridge_enqueue(sc, dst_if, mc, 1);
1955 RELEASE_GLOBAL_LOCKS();
1956 }
1957
1958 if (bmcast) {
1959 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1960 if (mc == NULL) {
1961 sc->sc_if.if_oerrors++;
1962 goto next;
1963 }
1964
1965 m_set_rcvif(mc, dst_if);
1966 mc->m_flags &= ~M_PROMISC;
1967
1968 ACQUIRE_GLOBAL_LOCKS();
1969 ether_input(dst_if, mc);
1970 RELEASE_GLOBAL_LOCKS();
1971 }
1972 next:
1973 BRIDGE_PSZ_RENTER(s);
1974 bridge_release_member(sc, bif, &psref);
1975 }
1976 BRIDGE_PSZ_REXIT(s);
1977
1978 m_freem(m);
1979 }
1980
1981 static int
1982 bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst,
1983 struct bridge_rtnode **brtp)
1984 {
1985 struct bridge_rtnode *brt;
1986 int error;
1987
1988 if (sc->sc_brtcnt >= sc->sc_brtmax)
1989 return ENOSPC;
1990
1991 /*
1992 * Allocate a new bridge forwarding node, and
1993 * initialize the expiration time and Ethernet
1994 * address.
1995 */
1996 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
1997 if (brt == NULL)
1998 return ENOMEM;
1999
2000 memset(brt, 0, sizeof(*brt));
2001 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2002 brt->brt_flags = IFBAF_DYNAMIC;
2003 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2004
2005 BRIDGE_RT_LOCK(sc);
2006 error = bridge_rtnode_insert(sc, brt);
2007 BRIDGE_RT_UNLOCK(sc);
2008
2009 if (error != 0) {
2010 pool_put(&bridge_rtnode_pool, brt);
2011 return error;
2012 }
2013
2014 *brtp = brt;
2015 return 0;
2016 }
2017
2018 /*
2019 * bridge_rtupdate:
2020 *
2021 * Add a bridge routing entry.
2022 */
2023 static int
2024 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2025 struct ifnet *dst_if, int setflags, uint8_t flags)
2026 {
2027 struct bridge_rtnode *brt;
2028 int s;
2029
2030 again:
2031 /*
2032 * A route for this destination might already exist. If so,
2033 * update it, otherwise create a new one.
2034 */
2035 BRIDGE_RT_RENTER(s);
2036 brt = bridge_rtnode_lookup(sc, dst);
2037
2038 if (brt != NULL) {
2039 brt->brt_ifp = dst_if;
2040 if (setflags) {
2041 brt->brt_flags = flags;
2042 if (flags & IFBAF_STATIC)
2043 brt->brt_expire = 0;
2044 else
2045 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2046 } else {
2047 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2048 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2049 }
2050 }
2051 BRIDGE_RT_REXIT(s);
2052
2053 if (brt == NULL) {
2054 int r;
2055
2056 r = bridge_rtalloc(sc, dst, &brt);
2057 if (r != 0)
2058 return r;
2059 goto again;
2060 }
2061
2062 return 0;
2063 }
2064
2065 /*
2066 * bridge_rtlookup:
2067 *
2068 * Lookup the destination interface for an address.
2069 */
2070 static struct ifnet *
2071 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2072 {
2073 struct bridge_rtnode *brt;
2074 struct ifnet *ifs = NULL;
2075 int s;
2076
2077 BRIDGE_RT_RENTER(s);
2078 brt = bridge_rtnode_lookup(sc, addr);
2079 if (brt != NULL)
2080 ifs = brt->brt_ifp;
2081 BRIDGE_RT_REXIT(s);
2082
2083 return ifs;
2084 }
2085
2086 typedef bool (*bridge_iterate_cb_t)
2087 (struct bridge_softc *, struct bridge_rtnode *, bool *, void *);
2088
2089 /*
2090 * bridge_rtlist_iterate_remove:
2091 *
2092 * It iterates on sc->sc_rtlist and removes rtnodes of it which func
2093 * callback judges to remove. Removals of rtnodes are done in a manner
2094 * of pserialize. To this end, all kmem_* operations are placed out of
2095 * mutexes.
2096 */
2097 static void
2098 bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg)
2099 {
2100 struct bridge_rtnode *brt, *nbrt;
2101 struct bridge_rtnode **brt_list;
2102 int i, count;
2103
2104 retry:
2105 count = sc->sc_brtcnt;
2106 if (count == 0)
2107 return;
2108 brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
2109
2110 BRIDGE_RT_LOCK(sc);
2111 if (__predict_false(sc->sc_brtcnt > count)) {
2112 /* The rtnodes increased, we need more memory */
2113 BRIDGE_RT_UNLOCK(sc);
2114 kmem_free(brt_list, sizeof(*brt_list) * count);
2115 goto retry;
2116 }
2117
2118 i = 0;
2119 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2120 bool need_break = false;
2121 if (func(sc, brt, &need_break, arg)) {
2122 bridge_rtnode_remove(sc, brt);
2123 brt_list[i++] = brt;
2124 }
2125 if (need_break)
2126 break;
2127 }
2128
2129 if (i > 0)
2130 BRIDGE_RT_PSZ_PERFORM(sc);
2131 BRIDGE_RT_UNLOCK(sc);
2132
2133 while (--i >= 0)
2134 bridge_rtnode_destroy(brt_list[i]);
2135
2136 kmem_free(brt_list, sizeof(*brt_list) * count);
2137 }
2138
2139 static bool
2140 bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2141 bool *need_break, void *arg)
2142 {
2143 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2144 /* Take into account of the subsequent removal */
2145 if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax)
2146 *need_break = true;
2147 return true;
2148 } else
2149 return false;
2150 }
2151
2152 static void
2153 bridge_rttrim0(struct bridge_softc *sc)
2154 {
2155 bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL);
2156 }
2157
2158 /*
2159 * bridge_rttrim:
2160 *
2161 * Trim the routine table so that we have a number
2162 * of routing entries less than or equal to the
2163 * maximum number.
2164 */
2165 static void
2166 bridge_rttrim(struct bridge_softc *sc)
2167 {
2168
2169 /* Make sure we actually need to do this. */
2170 if (sc->sc_brtcnt <= sc->sc_brtmax)
2171 return;
2172
2173 /* Force an aging cycle; this might trim enough addresses. */
2174 bridge_rtage(sc);
2175 if (sc->sc_brtcnt <= sc->sc_brtmax)
2176 return;
2177
2178 bridge_rttrim0(sc);
2179
2180 return;
2181 }
2182
2183 /*
2184 * bridge_timer:
2185 *
2186 * Aging timer for the bridge.
2187 */
2188 static void
2189 bridge_timer(void *arg)
2190 {
2191 struct bridge_softc *sc = arg;
2192
2193 workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL);
2194 }
2195
2196 static void
2197 bridge_rtage_work(struct work *wk, void *arg)
2198 {
2199 struct bridge_softc *sc = arg;
2200
2201 KASSERT(wk == &sc->sc_rtage_wk);
2202
2203 bridge_rtage(sc);
2204
2205 if (sc->sc_if.if_flags & IFF_RUNNING)
2206 callout_reset(&sc->sc_brcallout,
2207 bridge_rtable_prune_period * hz, bridge_timer, sc);
2208 }
2209
2210 static bool
2211 bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2212 bool *need_break, void *arg)
2213 {
2214 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2215 time_uptime >= brt->brt_expire)
2216 return true;
2217 else
2218 return false;
2219 }
2220
2221 /*
2222 * bridge_rtage:
2223 *
2224 * Perform an aging cycle.
2225 */
2226 static void
2227 bridge_rtage(struct bridge_softc *sc)
2228 {
2229 bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL);
2230 }
2231
2232
2233 static bool
2234 bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2235 bool *need_break, void *arg)
2236 {
2237 int full = *(int*)arg;
2238
2239 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2240 return true;
2241 else
2242 return false;
2243 }
2244
2245 /*
2246 * bridge_rtflush:
2247 *
2248 * Remove all dynamic addresses from the bridge.
2249 */
2250 static void
2251 bridge_rtflush(struct bridge_softc *sc, int full)
2252 {
2253 bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full);
2254 }
2255
2256 /*
2257 * bridge_rtdaddr:
2258 *
2259 * Remove an address from the table.
2260 */
2261 static int
2262 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2263 {
2264 struct bridge_rtnode *brt;
2265
2266 BRIDGE_RT_LOCK(sc);
2267 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
2268 BRIDGE_RT_UNLOCK(sc);
2269 return ENOENT;
2270 }
2271 bridge_rtnode_remove(sc, brt);
2272 BRIDGE_RT_PSZ_PERFORM(sc);
2273 BRIDGE_RT_UNLOCK(sc);
2274
2275 bridge_rtnode_destroy(brt);
2276
2277 return 0;
2278 }
2279
2280 /*
2281 * bridge_rtdelete:
2282 *
2283 * Delete routes to a speicifc member interface.
2284 */
2285 static void
2286 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
2287 {
2288 struct bridge_rtnode *brt;
2289
2290 BRIDGE_RT_LOCK(sc);
2291 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
2292 if (brt->brt_ifp == ifp)
2293 break;
2294 }
2295 if (brt == NULL) {
2296 BRIDGE_RT_UNLOCK(sc);
2297 return;
2298 }
2299 bridge_rtnode_remove(sc, brt);
2300 BRIDGE_RT_PSZ_PERFORM(sc);
2301 BRIDGE_RT_UNLOCK(sc);
2302
2303 bridge_rtnode_destroy(brt);
2304 }
2305
2306 /*
2307 * bridge_rtable_init:
2308 *
2309 * Initialize the route table for this bridge.
2310 */
2311 static void
2312 bridge_rtable_init(struct bridge_softc *sc)
2313 {
2314 int i;
2315
2316 sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2317 KM_SLEEP);
2318
2319 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2320 LIST_INIT(&sc->sc_rthash[i]);
2321
2322 sc->sc_rthash_key = cprng_fast32();
2323
2324 LIST_INIT(&sc->sc_rtlist);
2325
2326 sc->sc_rtlist_psz = pserialize_create();
2327 sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
2328 }
2329
2330 /*
2331 * bridge_rtable_fini:
2332 *
2333 * Deconstruct the route table for this bridge.
2334 */
2335 static void
2336 bridge_rtable_fini(struct bridge_softc *sc)
2337 {
2338
2339 kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
2340 if (sc->sc_rtlist_lock)
2341 mutex_obj_free(sc->sc_rtlist_lock);
2342 if (sc->sc_rtlist_psz)
2343 pserialize_destroy(sc->sc_rtlist_psz);
2344 }
2345
2346 /*
2347 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2348 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2349 */
2350 #define mix(a, b, c) \
2351 do { \
2352 a -= b; a -= c; a ^= (c >> 13); \
2353 b -= c; b -= a; b ^= (a << 8); \
2354 c -= a; c -= b; c ^= (b >> 13); \
2355 a -= b; a -= c; a ^= (c >> 12); \
2356 b -= c; b -= a; b ^= (a << 16); \
2357 c -= a; c -= b; c ^= (b >> 5); \
2358 a -= b; a -= c; a ^= (c >> 3); \
2359 b -= c; b -= a; b ^= (a << 10); \
2360 c -= a; c -= b; c ^= (b >> 15); \
2361 } while (/*CONSTCOND*/0)
2362
2363 static inline uint32_t
2364 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2365 {
2366 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2367
2368 b += addr[5] << 8;
2369 b += addr[4];
2370 a += addr[3] << 24;
2371 a += addr[2] << 16;
2372 a += addr[1] << 8;
2373 a += addr[0];
2374
2375 mix(a, b, c);
2376
2377 return (c & BRIDGE_RTHASH_MASK);
2378 }
2379
2380 #undef mix
2381
2382 /*
2383 * bridge_rtnode_lookup:
2384 *
2385 * Look up a bridge route node for the specified destination.
2386 */
2387 static struct bridge_rtnode *
2388 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2389 {
2390 struct bridge_rtnode *brt;
2391 uint32_t hash;
2392 int dir;
2393
2394 hash = bridge_rthash(sc, addr);
2395 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2396 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2397 if (dir == 0)
2398 return (brt);
2399 if (dir > 0)
2400 return (NULL);
2401 }
2402
2403 return (NULL);
2404 }
2405
2406 /*
2407 * bridge_rtnode_insert:
2408 *
2409 * Insert the specified bridge node into the route table. We
2410 * assume the entry is not already in the table.
2411 */
2412 static int
2413 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2414 {
2415 struct bridge_rtnode *lbrt;
2416 uint32_t hash;
2417 int dir;
2418
2419 KASSERT(BRIDGE_RT_LOCKED(sc));
2420
2421 hash = bridge_rthash(sc, brt->brt_addr);
2422
2423 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2424 if (lbrt == NULL) {
2425 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2426 goto out;
2427 }
2428
2429 do {
2430 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2431 if (dir == 0)
2432 return (EEXIST);
2433 if (dir > 0) {
2434 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2435 goto out;
2436 }
2437 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2438 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2439 goto out;
2440 }
2441 lbrt = LIST_NEXT(lbrt, brt_hash);
2442 } while (lbrt != NULL);
2443
2444 #ifdef DIAGNOSTIC
2445 panic("bridge_rtnode_insert: impossible");
2446 #endif
2447
2448 out:
2449 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2450 sc->sc_brtcnt++;
2451
2452 return (0);
2453 }
2454
2455 /*
2456 * bridge_rtnode_remove:
2457 *
2458 * Remove a bridge rtnode from the rthash and the rtlist of a bridge.
2459 */
2460 static void
2461 bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt)
2462 {
2463
2464 KASSERT(BRIDGE_RT_LOCKED(sc));
2465
2466 LIST_REMOVE(brt, brt_hash);
2467 LIST_REMOVE(brt, brt_list);
2468 sc->sc_brtcnt--;
2469 }
2470
2471 /*
2472 * bridge_rtnode_destroy:
2473 *
2474 * Destroy a bridge rtnode.
2475 */
2476 static void
2477 bridge_rtnode_destroy(struct bridge_rtnode *brt)
2478 {
2479
2480 pool_put(&bridge_rtnode_pool, brt);
2481 }
2482
2483 #if defined(BRIDGE_IPF)
2484 extern pfil_head_t *inet_pfil_hook; /* XXX */
2485 extern pfil_head_t *inet6_pfil_hook; /* XXX */
2486
2487 /*
2488 * Send bridge packets through IPF if they are one of the types IPF can deal
2489 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without
2490 * question.)
2491 */
2492 static int
2493 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
2494 {
2495 int snap, error;
2496 struct ether_header *eh1, eh2;
2497 struct llc llc1;
2498 uint16_t ether_type;
2499
2500 snap = 0;
2501 error = -1; /* Default error if not error == 0 */
2502 eh1 = mtod(*mp, struct ether_header *);
2503 ether_type = ntohs(eh1->ether_type);
2504
2505 /*
2506 * Check for SNAP/LLC.
2507 */
2508 if (ether_type < ETHERMTU) {
2509 struct llc *llc2 = (struct llc *)(eh1 + 1);
2510
2511 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2512 llc2->llc_dsap == LLC_SNAP_LSAP &&
2513 llc2->llc_ssap == LLC_SNAP_LSAP &&
2514 llc2->llc_control == LLC_UI) {
2515 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2516 snap = 1;
2517 }
2518 }
2519
2520 /*
2521 * If we're trying to filter bridge traffic, don't look at anything
2522 * other than IP and ARP traffic. If the filter doesn't understand
2523 * IPv6, don't allow IPv6 through the bridge either. This is lame
2524 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2525 * but of course we don't have an AppleTalk filter to begin with.
2526 * (Note that since IPF doesn't understand ARP it will pass *ALL*
2527 * ARP traffic.)
2528 */
2529 switch (ether_type) {
2530 case ETHERTYPE_ARP:
2531 case ETHERTYPE_REVARP:
2532 return 0; /* Automatically pass */
2533 case ETHERTYPE_IP:
2534 # ifdef INET6
2535 case ETHERTYPE_IPV6:
2536 # endif /* INET6 */
2537 break;
2538 default:
2539 goto bad;
2540 }
2541
2542 /* Strip off the Ethernet header and keep a copy. */
2543 m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
2544 m_adj(*mp, ETHER_HDR_LEN);
2545
2546 /* Strip off snap header, if present */
2547 if (snap) {
2548 m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
2549 m_adj(*mp, sizeof(struct llc));
2550 }
2551
2552 /*
2553 * Check basic packet sanity and run IPF through pfil.
2554 */
2555 KASSERT(!cpu_intr_p());
2556 switch (ether_type)
2557 {
2558 case ETHERTYPE_IP :
2559 error = bridge_ip_checkbasic(mp);
2560 if (error == 0)
2561 error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
2562 break;
2563 # ifdef INET6
2564 case ETHERTYPE_IPV6 :
2565 error = bridge_ip6_checkbasic(mp);
2566 if (error == 0)
2567 error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
2568 break;
2569 # endif
2570 default :
2571 error = 0;
2572 break;
2573 }
2574
2575 if (*mp == NULL)
2576 return error;
2577 if (error != 0)
2578 goto bad;
2579
2580 error = -1;
2581
2582 /*
2583 * Finally, put everything back the way it was and return
2584 */
2585 if (snap) {
2586 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2587 if (*mp == NULL)
2588 return error;
2589 bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
2590 }
2591
2592 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2593 if (*mp == NULL)
2594 return error;
2595 bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
2596
2597 return 0;
2598
2599 bad:
2600 m_freem(*mp);
2601 *mp = NULL;
2602 return error;
2603 }
2604
2605 /*
2606 * Perform basic checks on header size since
2607 * IPF assumes ip_input has already processed
2608 * it for it. Cut-and-pasted from ip_input.c.
2609 * Given how simple the IPv6 version is,
2610 * does the IPv4 version really need to be
2611 * this complicated?
2612 *
2613 * XXX Should we update ipstat here, or not?
2614 * XXX Right now we update ipstat but not
2615 * XXX csum_counter.
2616 */
2617 static int
2618 bridge_ip_checkbasic(struct mbuf **mp)
2619 {
2620 struct mbuf *m = *mp;
2621 struct ip *ip;
2622 int len, hlen;
2623
2624 if (*mp == NULL)
2625 return -1;
2626
2627 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2628 if ((m = m_copyup(m, sizeof(struct ip),
2629 (max_linkhdr + 3) & ~3)) == NULL) {
2630 /* XXXJRT new stat, please */
2631 ip_statinc(IP_STAT_TOOSMALL);
2632 goto bad;
2633 }
2634 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
2635 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2636 ip_statinc(IP_STAT_TOOSMALL);
2637 goto bad;
2638 }
2639 }
2640 ip = mtod(m, struct ip *);
2641 if (ip == NULL) goto bad;
2642
2643 if (ip->ip_v != IPVERSION) {
2644 ip_statinc(IP_STAT_BADVERS);
2645 goto bad;
2646 }
2647 hlen = ip->ip_hl << 2;
2648 if (hlen < sizeof(struct ip)) { /* minimum header length */
2649 ip_statinc(IP_STAT_BADHLEN);
2650 goto bad;
2651 }
2652 if (hlen > m->m_len) {
2653 if ((m = m_pullup(m, hlen)) == 0) {
2654 ip_statinc(IP_STAT_BADHLEN);
2655 goto bad;
2656 }
2657 ip = mtod(m, struct ip *);
2658 if (ip == NULL) goto bad;
2659 }
2660
2661 switch (m->m_pkthdr.csum_flags &
2662 ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) |
2663 M_CSUM_IPv4_BAD)) {
2664 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2665 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2666 goto bad;
2667
2668 case M_CSUM_IPv4:
2669 /* Checksum was okay. */
2670 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2671 break;
2672
2673 default:
2674 /* Must compute it ourselves. */
2675 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2676 if (in_cksum(m, hlen) != 0)
2677 goto bad;
2678 break;
2679 }
2680
2681 /* Retrieve the packet length. */
2682 len = ntohs(ip->ip_len);
2683
2684 /*
2685 * Check for additional length bogosity
2686 */
2687 if (len < hlen) {
2688 ip_statinc(IP_STAT_BADLEN);
2689 goto bad;
2690 }
2691
2692 /*
2693 * Check that the amount of data in the buffers
2694 * is as at least much as the IP header would have us expect.
2695 * Drop packet if shorter than we expect.
2696 */
2697 if (m->m_pkthdr.len < len) {
2698 ip_statinc(IP_STAT_TOOSHORT);
2699 goto bad;
2700 }
2701
2702 /* Checks out, proceed */
2703 *mp = m;
2704 return 0;
2705
2706 bad:
2707 *mp = m;
2708 return -1;
2709 }
2710
2711 # ifdef INET6
2712 /*
2713 * Same as above, but for IPv6.
2714 * Cut-and-pasted from ip6_input.c.
2715 * XXX Should we update ip6stat, or not?
2716 */
2717 static int
2718 bridge_ip6_checkbasic(struct mbuf **mp)
2719 {
2720 struct mbuf *m = *mp;
2721 struct ip6_hdr *ip6;
2722
2723 /*
2724 * If the IPv6 header is not aligned, slurp it up into a new
2725 * mbuf with space for link headers, in the event we forward
2726 * it. Otherwise, if it is aligned, make sure the entire base
2727 * IPv6 header is in the first mbuf of the chain.
2728 */
2729 if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2730 struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
2731 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2732 (max_linkhdr + 3) & ~3)) == NULL) {
2733 /* XXXJRT new stat, please */
2734 ip6_statinc(IP6_STAT_TOOSMALL);
2735 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2736 goto bad;
2737 }
2738 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2739 struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
2740 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2741 ip6_statinc(IP6_STAT_TOOSMALL);
2742 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2743 goto bad;
2744 }
2745 }
2746
2747 ip6 = mtod(m, struct ip6_hdr *);
2748
2749 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2750 ip6_statinc(IP6_STAT_BADVERS);
2751 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr);
2752 goto bad;
2753 }
2754
2755 /* Checks out, proceed */
2756 *mp = m;
2757 return 0;
2758
2759 bad:
2760 *mp = m;
2761 return -1;
2762 }
2763 # endif /* INET6 */
2764 #endif /* BRIDGE_IPF */
2765