xref: /freebsd/sys/net/if_lagg.c (revision 9fd69f37d28cfd7438cac3eeb45fe9dd46b4d7dd)
1 /*	$OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 #include "opt_inet.h"
24 #include "opt_inet6.h"
25 
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
29 #include <sys/mbuf.h>
30 #include <sys/queue.h>
31 #include <sys/socket.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/module.h>
35 #include <sys/priv.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/hash.h>
39 #include <sys/lock.h>
40 #include <sys/rwlock.h>
41 #include <sys/taskqueue.h>
42 #include <sys/eventhandler.h>
43 
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_clone.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/if_var.h>
53 #include <net/bpf.h>
54 
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
60 #endif
61 
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #endif
65 
66 #include <net/if_vlan_var.h>
67 #include <net/if_lagg.h>
68 #include <net/ieee8023ad_lacp.h>
69 
70 /* Special flags we should propagate to the lagg ports. */
71 static struct {
72 	int flag;
73 	int (*func)(struct ifnet *, int);
74 } lagg_pflags[] = {
75 	{IFF_PROMISC, ifpromisc},
76 	{IFF_ALLMULTI, if_allmulti},
77 	{0, NULL}
78 };
79 
80 SLIST_HEAD(__trhead, lagg_softc) lagg_list;	/* list of laggs */
81 static struct mtx	lagg_list_mtx;
82 eventhandler_tag	lagg_detach_cookie = NULL;
83 
84 static int	lagg_clone_create(struct if_clone *, int, caddr_t);
85 static void	lagg_clone_destroy(struct ifnet *);
86 static void	lagg_lladdr(struct lagg_softc *, uint8_t *);
87 static void	lagg_capabilities(struct lagg_softc *);
88 static void	lagg_port_lladdr(struct lagg_port *, uint8_t *);
89 static void	lagg_port_setlladdr(void *, int);
90 static int	lagg_port_create(struct lagg_softc *, struct ifnet *);
91 static int	lagg_port_destroy(struct lagg_port *, int);
92 static struct mbuf *lagg_input(struct ifnet *, struct mbuf *);
93 static void	lagg_linkstate(struct lagg_softc *);
94 static void	lagg_port_state(struct ifnet *, int);
95 static int	lagg_port_ioctl(struct ifnet *, u_long, caddr_t);
96 static int	lagg_port_output(struct ifnet *, struct mbuf *,
97 		    struct sockaddr *, struct route *);
98 static void	lagg_port_ifdetach(void *arg __unused, struct ifnet *);
99 #ifdef LAGG_PORT_STACKING
100 static int	lagg_port_checkstacking(struct lagg_softc *);
101 #endif
102 static void	lagg_port2req(struct lagg_port *, struct lagg_reqport *);
103 static void	lagg_init(void *);
104 static void	lagg_stop(struct lagg_softc *);
105 static int	lagg_ioctl(struct ifnet *, u_long, caddr_t);
106 static int	lagg_ether_setmulti(struct lagg_softc *);
107 static int	lagg_ether_cmdmulti(struct lagg_port *, int);
108 static	int	lagg_setflag(struct lagg_port *, int, int,
109 		    int (*func)(struct ifnet *, int));
110 static	int	lagg_setflags(struct lagg_port *, int status);
111 static void	lagg_start(struct ifnet *);
112 static int	lagg_media_change(struct ifnet *);
113 static void	lagg_media_status(struct ifnet *, struct ifmediareq *);
114 static struct lagg_port *lagg_link_active(struct lagg_softc *,
115 	    struct lagg_port *);
116 static const void *lagg_gethdr(struct mbuf *, u_int, u_int, void *);
117 
118 IFC_SIMPLE_DECLARE(lagg, 0);
119 
120 /* Simple round robin */
121 static int	lagg_rr_attach(struct lagg_softc *);
122 static int	lagg_rr_detach(struct lagg_softc *);
123 static int	lagg_rr_start(struct lagg_softc *, struct mbuf *);
124 static struct mbuf *lagg_rr_input(struct lagg_softc *, struct lagg_port *,
125 		    struct mbuf *);
126 
127 /* Active failover */
128 static int	lagg_fail_attach(struct lagg_softc *);
129 static int	lagg_fail_detach(struct lagg_softc *);
130 static int	lagg_fail_start(struct lagg_softc *, struct mbuf *);
131 static struct mbuf *lagg_fail_input(struct lagg_softc *, struct lagg_port *,
132 		    struct mbuf *);
133 
134 /* Loadbalancing */
135 static int	lagg_lb_attach(struct lagg_softc *);
136 static int	lagg_lb_detach(struct lagg_softc *);
137 static int	lagg_lb_port_create(struct lagg_port *);
138 static void	lagg_lb_port_destroy(struct lagg_port *);
139 static int	lagg_lb_start(struct lagg_softc *, struct mbuf *);
140 static struct mbuf *lagg_lb_input(struct lagg_softc *, struct lagg_port *,
141 		    struct mbuf *);
142 static int	lagg_lb_porttable(struct lagg_softc *, struct lagg_port *);
143 
144 /* 802.3ad LACP */
145 static int	lagg_lacp_attach(struct lagg_softc *);
146 static int	lagg_lacp_detach(struct lagg_softc *);
147 static int	lagg_lacp_start(struct lagg_softc *, struct mbuf *);
148 static struct mbuf *lagg_lacp_input(struct lagg_softc *, struct lagg_port *,
149 		    struct mbuf *);
150 static void	lagg_lacp_lladdr(struct lagg_softc *);
151 
152 /* lagg protocol table */
153 static const struct {
154 	int			ti_proto;
155 	int			(*ti_attach)(struct lagg_softc *);
156 } lagg_protos[] = {
157 	{ LAGG_PROTO_ROUNDROBIN,	lagg_rr_attach },
158 	{ LAGG_PROTO_FAILOVER,		lagg_fail_attach },
159 	{ LAGG_PROTO_LOADBALANCE,	lagg_lb_attach },
160 	{ LAGG_PROTO_ETHERCHANNEL,	lagg_lb_attach },
161 	{ LAGG_PROTO_LACP,		lagg_lacp_attach },
162 	{ LAGG_PROTO_NONE,		NULL }
163 };
164 
165 static int
166 lagg_modevent(module_t mod, int type, void *data)
167 {
168 
169 	switch (type) {
170 	case MOD_LOAD:
171 		mtx_init(&lagg_list_mtx, "if_lagg list", NULL, MTX_DEF);
172 		SLIST_INIT(&lagg_list);
173 		if_clone_attach(&lagg_cloner);
174 		lagg_input_p = lagg_input;
175 		lagg_linkstate_p = lagg_port_state;
176 		lagg_detach_cookie = EVENTHANDLER_REGISTER(
177 		    ifnet_departure_event, lagg_port_ifdetach, NULL,
178 		    EVENTHANDLER_PRI_ANY);
179 		break;
180 	case MOD_UNLOAD:
181 		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
182 		    lagg_detach_cookie);
183 		if_clone_detach(&lagg_cloner);
184 		lagg_input_p = NULL;
185 		lagg_linkstate_p = NULL;
186 		mtx_destroy(&lagg_list_mtx);
187 		break;
188 	default:
189 		return (EOPNOTSUPP);
190 	}
191 	return (0);
192 }
193 
194 static moduledata_t lagg_mod = {
195 	"if_lagg",
196 	lagg_modevent,
197 	0
198 };
199 
200 DECLARE_MODULE(if_lagg, lagg_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
201 
202 #if __FreeBSD_version >= 800000
203 /*
204  * This routine is run via an vlan
205  * config EVENT
206  */
207 static void
208 lagg_register_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
209 {
210         struct lagg_softc       *sc = ifp->if_softc;
211         struct lagg_port        *lp;
212 
213         if (ifp->if_softc !=  arg)   /* Not our event */
214                 return;
215 
216         LAGG_RLOCK(sc);
217         if (!SLIST_EMPTY(&sc->sc_ports)) {
218                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
219                         EVENTHANDLER_INVOKE(vlan_config, lp->lp_ifp, vtag);
220         }
221         LAGG_RUNLOCK(sc);
222 }
223 
224 /*
225  * This routine is run via an vlan
226  * unconfig EVENT
227  */
228 static void
229 lagg_unregister_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
230 {
231         struct lagg_softc       *sc = ifp->if_softc;
232         struct lagg_port        *lp;
233 
234         if (ifp->if_softc !=  arg)   /* Not our event */
235                 return;
236 
237         LAGG_RLOCK(sc);
238         if (!SLIST_EMPTY(&sc->sc_ports)) {
239                 SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
240                         EVENTHANDLER_INVOKE(vlan_unconfig, lp->lp_ifp, vtag);
241         }
242         LAGG_RUNLOCK(sc);
243 }
244 #endif
245 
246 static int
247 lagg_clone_create(struct if_clone *ifc, int unit, caddr_t params)
248 {
249 	struct lagg_softc *sc;
250 	struct ifnet *ifp;
251 	int i, error = 0;
252 	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
253 
254 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
255 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
256 	if (ifp == NULL) {
257 		free(sc, M_DEVBUF);
258 		return (ENOSPC);
259 	}
260 
261 	sc->sc_proto = LAGG_PROTO_NONE;
262 	for (i = 0; lagg_protos[i].ti_proto != LAGG_PROTO_NONE; i++) {
263 		if (lagg_protos[i].ti_proto == LAGG_PROTO_DEFAULT) {
264 			sc->sc_proto = lagg_protos[i].ti_proto;
265 			if ((error = lagg_protos[i].ti_attach(sc)) != 0) {
266 				if_free_type(ifp, IFT_ETHER);
267 				free(sc, M_DEVBUF);
268 				return (error);
269 			}
270 			break;
271 		}
272 	}
273 	LAGG_LOCK_INIT(sc);
274 	SLIST_INIT(&sc->sc_ports);
275 	TASK_INIT(&sc->sc_lladdr_task, 0, lagg_port_setlladdr, sc);
276 
277 	/* Initialise pseudo media types */
278 	ifmedia_init(&sc->sc_media, 0, lagg_media_change,
279 	    lagg_media_status);
280 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
281 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
282 
283 	if_initname(ifp, ifc->ifc_name, unit);
284 	ifp->if_type = IFT_ETHER;
285 	ifp->if_softc = sc;
286 	ifp->if_start = lagg_start;
287 	ifp->if_init = lagg_init;
288 	ifp->if_ioctl = lagg_ioctl;
289 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
290 
291 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
292 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
293 	IFQ_SET_READY(&ifp->if_snd);
294 
295 	/*
296 	 * Attach as an ordinary ethernet device, childs will be attached
297 	 * as special device IFT_IEEE8023ADLAG.
298 	 */
299 	ether_ifattach(ifp, eaddr);
300 
301 #if __FreeBSD_version >= 800000
302 	sc->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
303 		lagg_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
304 	sc->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
305 		lagg_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
306 #endif
307 
308 	/* Insert into the global list of laggs */
309 	mtx_lock(&lagg_list_mtx);
310 	SLIST_INSERT_HEAD(&lagg_list, sc, sc_entries);
311 	mtx_unlock(&lagg_list_mtx);
312 
313 	return (0);
314 }
315 
316 static void
317 lagg_clone_destroy(struct ifnet *ifp)
318 {
319 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
320 	struct lagg_port *lp;
321 
322 	LAGG_WLOCK(sc);
323 
324 	lagg_stop(sc);
325 	ifp->if_flags &= ~IFF_UP;
326 
327 #if __FreeBSD_version >= 800000
328 	EVENTHANDLER_DEREGISTER(vlan_config, sc->vlan_attach);
329 	EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vlan_detach);
330 #endif
331 
332 	/* Shutdown and remove lagg ports */
333 	while ((lp = SLIST_FIRST(&sc->sc_ports)) != NULL)
334 		lagg_port_destroy(lp, 1);
335 	/* Unhook the aggregation protocol */
336 	(*sc->sc_detach)(sc);
337 
338 	LAGG_WUNLOCK(sc);
339 
340 	ifmedia_removeall(&sc->sc_media);
341 	ether_ifdetach(ifp);
342 	if_free_type(ifp, IFT_ETHER);
343 
344 	mtx_lock(&lagg_list_mtx);
345 	SLIST_REMOVE(&lagg_list, sc, lagg_softc, sc_entries);
346 	mtx_unlock(&lagg_list_mtx);
347 
348 	taskqueue_drain(taskqueue_swi, &sc->sc_lladdr_task);
349 	LAGG_LOCK_DESTROY(sc);
350 	free(sc, M_DEVBUF);
351 }
352 
353 static void
354 lagg_lladdr(struct lagg_softc *sc, uint8_t *lladdr)
355 {
356 	struct ifnet *ifp = sc->sc_ifp;
357 
358 	if (memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
359 		return;
360 
361 	bcopy(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
362 	/* Let the protocol know the MAC has changed */
363 	if (sc->sc_lladdr != NULL)
364 		(*sc->sc_lladdr)(sc);
365 	EVENTHANDLER_INVOKE(iflladdr_event, ifp);
366 }
367 
368 static void
369 lagg_capabilities(struct lagg_softc *sc)
370 {
371 	struct lagg_port *lp;
372 	int cap = ~0, ena = ~0;
373 	u_long hwa = ~0UL;
374 
375 	LAGG_WLOCK_ASSERT(sc);
376 
377 	/* Get capabilities from the lagg ports */
378 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
379 		cap &= lp->lp_ifp->if_capabilities;
380 		ena &= lp->lp_ifp->if_capenable;
381 		hwa &= lp->lp_ifp->if_hwassist;
382 	}
383 	cap = (cap == ~0 ? 0 : cap);
384 	ena = (ena == ~0 ? 0 : ena);
385 	hwa = (hwa == ~0 ? 0 : hwa);
386 
387 	if (sc->sc_ifp->if_capabilities != cap ||
388 	    sc->sc_ifp->if_capenable != ena ||
389 	    sc->sc_ifp->if_hwassist != hwa) {
390 		sc->sc_ifp->if_capabilities = cap;
391 		sc->sc_ifp->if_capenable = ena;
392 		sc->sc_ifp->if_hwassist = hwa;
393 		getmicrotime(&sc->sc_ifp->if_lastchange);
394 
395 		if (sc->sc_ifflags & IFF_DEBUG)
396 			if_printf(sc->sc_ifp,
397 			    "capabilities 0x%08x enabled 0x%08x\n", cap, ena);
398 	}
399 }
400 
401 static void
402 lagg_port_lladdr(struct lagg_port *lp, uint8_t *lladdr)
403 {
404 	struct lagg_softc *sc = lp->lp_softc;
405 	struct ifnet *ifp = lp->lp_ifp;
406 	struct lagg_llq *llq;
407 	int pending = 0;
408 
409 	LAGG_WLOCK_ASSERT(sc);
410 
411 	if (lp->lp_detaching ||
412 	    memcmp(lladdr, IF_LLADDR(ifp), ETHER_ADDR_LEN) == 0)
413 		return;
414 
415 	/* Check to make sure its not already queued to be changed */
416 	SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
417 		if (llq->llq_ifp == ifp) {
418 			pending = 1;
419 			break;
420 		}
421 	}
422 
423 	if (!pending) {
424 		llq = malloc(sizeof(struct lagg_llq), M_DEVBUF, M_NOWAIT);
425 		if (llq == NULL)	/* XXX what to do */
426 			return;
427 	}
428 
429 	/* Update the lladdr even if pending, it may have changed */
430 	llq->llq_ifp = ifp;
431 	bcopy(lladdr, llq->llq_lladdr, ETHER_ADDR_LEN);
432 
433 	if (!pending)
434 		SLIST_INSERT_HEAD(&sc->sc_llq_head, llq, llq_entries);
435 
436 	taskqueue_enqueue(taskqueue_swi, &sc->sc_lladdr_task);
437 }
438 
439 /*
440  * Set the interface MAC address from a taskqueue to avoid a LOR.
441  */
442 static void
443 lagg_port_setlladdr(void *arg, int pending)
444 {
445 	struct lagg_softc *sc = (struct lagg_softc *)arg;
446 	struct lagg_llq *llq, *head;
447 	struct ifnet *ifp;
448 	int error;
449 
450 	/* Grab a local reference of the queue and remove it from the softc */
451 	LAGG_WLOCK(sc);
452 	head = SLIST_FIRST(&sc->sc_llq_head);
453 	SLIST_FIRST(&sc->sc_llq_head) = NULL;
454 	LAGG_WUNLOCK(sc);
455 
456 	/*
457 	 * Traverse the queue and set the lladdr on each ifp. It is safe to do
458 	 * unlocked as we have the only reference to it.
459 	 */
460 	for (llq = head; llq != NULL; llq = head) {
461 		ifp = llq->llq_ifp;
462 
463 		/* Set the link layer address */
464 		error = if_setlladdr(ifp, llq->llq_lladdr, ETHER_ADDR_LEN);
465 		if (error)
466 			printf("%s: setlladdr failed on %s\n", __func__,
467 			    ifp->if_xname);
468 
469 		head = SLIST_NEXT(llq, llq_entries);
470 		free(llq, M_DEVBUF);
471 	}
472 }
473 
474 static int
475 lagg_port_create(struct lagg_softc *sc, struct ifnet *ifp)
476 {
477 	struct lagg_softc *sc_ptr;
478 	struct lagg_port *lp;
479 	int error = 0;
480 
481 	LAGG_WLOCK_ASSERT(sc);
482 
483 	/* Limit the maximal number of lagg ports */
484 	if (sc->sc_count >= LAGG_MAX_PORTS)
485 		return (ENOSPC);
486 
487 	/* New lagg port has to be in an idle state */
488 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
489 		return (EBUSY);
490 
491 	/* Check if port has already been associated to a lagg */
492 	if (ifp->if_lagg != NULL)
493 		return (EBUSY);
494 
495 	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
496 	if (ifp->if_type != IFT_ETHER)
497 		return (EPROTONOSUPPORT);
498 
499 	/* Allow the first Ethernet member to define the MTU */
500 	if (SLIST_EMPTY(&sc->sc_ports))
501 		sc->sc_ifp->if_mtu = ifp->if_mtu;
502 	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
503 		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
504 		    ifp->if_xname);
505 		return (EINVAL);
506 	}
507 
508 	if ((lp = malloc(sizeof(struct lagg_port),
509 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
510 		return (ENOMEM);
511 
512 	/* Check if port is a stacked lagg */
513 	mtx_lock(&lagg_list_mtx);
514 	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
515 		if (ifp == sc_ptr->sc_ifp) {
516 			mtx_unlock(&lagg_list_mtx);
517 			free(lp, M_DEVBUF);
518 			return (EINVAL);
519 			/* XXX disable stacking for the moment, its untested */
520 #ifdef LAGG_PORT_STACKING
521 			lp->lp_flags |= LAGG_PORT_STACK;
522 			if (lagg_port_checkstacking(sc_ptr) >=
523 			    LAGG_MAX_STACKING) {
524 				mtx_unlock(&lagg_list_mtx);
525 				free(lp, M_DEVBUF);
526 				return (E2BIG);
527 			}
528 #endif
529 		}
530 	}
531 	mtx_unlock(&lagg_list_mtx);
532 
533 	/* Change the interface type */
534 	lp->lp_iftype = ifp->if_type;
535 	ifp->if_type = IFT_IEEE8023ADLAG;
536 	ifp->if_lagg = lp;
537 	lp->lp_ioctl = ifp->if_ioctl;
538 	ifp->if_ioctl = lagg_port_ioctl;
539 	lp->lp_output = ifp->if_output;
540 	ifp->if_output = lagg_port_output;
541 
542 	lp->lp_ifp = ifp;
543 	lp->lp_softc = sc;
544 
545 	/* Save port link layer address */
546 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
547 
548 	if (SLIST_EMPTY(&sc->sc_ports)) {
549 		sc->sc_primary = lp;
550 		lagg_lladdr(sc, IF_LLADDR(ifp));
551 	} else {
552 		/* Update link layer address for this port */
553 		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
554 	}
555 
556 	/* Insert into the list of ports */
557 	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
558 	sc->sc_count++;
559 
560 	/* Update lagg capabilities */
561 	lagg_capabilities(sc);
562 	lagg_linkstate(sc);
563 
564 	/* Add multicast addresses and interface flags to this port */
565 	lagg_ether_cmdmulti(lp, 1);
566 	lagg_setflags(lp, 1);
567 
568 	if (sc->sc_port_create != NULL)
569 		error = (*sc->sc_port_create)(lp);
570 	if (error) {
571 		/* remove the port again, without calling sc_port_destroy */
572 		lagg_port_destroy(lp, 0);
573 		return (error);
574 	}
575 
576 	return (error);
577 }
578 
579 #ifdef LAGG_PORT_STACKING
580 static int
581 lagg_port_checkstacking(struct lagg_softc *sc)
582 {
583 	struct lagg_softc *sc_ptr;
584 	struct lagg_port *lp;
585 	int m = 0;
586 
587 	LAGG_WLOCK_ASSERT(sc);
588 
589 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
590 		if (lp->lp_flags & LAGG_PORT_STACK) {
591 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
592 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
593 		}
594 	}
595 
596 	return (m + 1);
597 }
598 #endif
599 
600 static int
601 lagg_port_destroy(struct lagg_port *lp, int runpd)
602 {
603 	struct lagg_softc *sc = lp->lp_softc;
604 	struct lagg_port *lp_ptr;
605 	struct lagg_llq *llq;
606 	struct ifnet *ifp = lp->lp_ifp;
607 
608 	LAGG_WLOCK_ASSERT(sc);
609 
610 	if (runpd && sc->sc_port_destroy != NULL)
611 		(*sc->sc_port_destroy)(lp);
612 
613 	/*
614 	 * Remove multicast addresses and interface flags from this port and
615 	 * reset the MAC address, skip if the interface is being detached.
616 	 */
617 	if (!lp->lp_detaching) {
618 		lagg_ether_cmdmulti(lp, 0);
619 		lagg_setflags(lp, 0);
620 		lagg_port_lladdr(lp, lp->lp_lladdr);
621 	}
622 
623 	/* Restore interface */
624 	ifp->if_type = lp->lp_iftype;
625 	ifp->if_ioctl = lp->lp_ioctl;
626 	ifp->if_output = lp->lp_output;
627 	ifp->if_lagg = NULL;
628 
629 	/* Finally, remove the port from the lagg */
630 	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
631 	sc->sc_count--;
632 
633 	/* Update the primary interface */
634 	if (lp == sc->sc_primary) {
635 		uint8_t lladdr[ETHER_ADDR_LEN];
636 
637 		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
638 			bzero(&lladdr, ETHER_ADDR_LEN);
639 		} else {
640 			bcopy(lp_ptr->lp_lladdr,
641 			    lladdr, ETHER_ADDR_LEN);
642 		}
643 		lagg_lladdr(sc, lladdr);
644 		sc->sc_primary = lp_ptr;
645 
646 		/* Update link layer address for each port */
647 		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
648 			lagg_port_lladdr(lp_ptr, lladdr);
649 	}
650 
651 	/* Remove any pending lladdr changes from the queue */
652 	if (lp->lp_detaching) {
653 		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
654 			if (llq->llq_ifp == ifp) {
655 				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
656 				    llq_entries);
657 				free(llq, M_DEVBUF);
658 				break;	/* Only appears once */
659 			}
660 		}
661 	}
662 
663 	if (lp->lp_ifflags)
664 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
665 
666 	free(lp, M_DEVBUF);
667 
668 	/* Update lagg capabilities */
669 	lagg_capabilities(sc);
670 	lagg_linkstate(sc);
671 
672 	return (0);
673 }
674 
675 static int
676 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
677 {
678 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
679 	struct lagg_softc *sc;
680 	struct lagg_port *lp = NULL;
681 	int error = 0;
682 
683 	/* Should be checked by the caller */
684 	if (ifp->if_type != IFT_IEEE8023ADLAG ||
685 	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
686 		goto fallback;
687 
688 	switch (cmd) {
689 	case SIOCGLAGGPORT:
690 		if (rp->rp_portname[0] == '\0' ||
691 		    ifunit(rp->rp_portname) != ifp) {
692 			error = EINVAL;
693 			break;
694 		}
695 
696 		LAGG_RLOCK(sc);
697 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
698 			error = ENOENT;
699 			LAGG_RUNLOCK(sc);
700 			break;
701 		}
702 
703 		lagg_port2req(lp, rp);
704 		LAGG_RUNLOCK(sc);
705 		break;
706 
707 	case SIOCSIFCAP:
708 		if (lp->lp_ioctl == NULL) {
709 			error = EINVAL;
710 			break;
711 		}
712 		error = (*lp->lp_ioctl)(ifp, cmd, data);
713 		if (error)
714 			break;
715 
716 		/* Update lagg interface capabilities */
717 		LAGG_WLOCK(sc);
718 		lagg_capabilities(sc);
719 		LAGG_WUNLOCK(sc);
720 		break;
721 
722 	case SIOCSIFMTU:
723 		/* Do not allow the MTU to be changed once joined */
724 		error = EINVAL;
725 		break;
726 
727 	default:
728 		goto fallback;
729 	}
730 
731 	return (error);
732 
733 fallback:
734 	if (lp->lp_ioctl != NULL)
735 		return ((*lp->lp_ioctl)(ifp, cmd, data));
736 
737 	return (EINVAL);
738 }
739 
740 static int
741 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
742 	struct sockaddr *dst, struct route *ro)
743 {
744 	struct lagg_port *lp = ifp->if_lagg;
745 	struct ether_header *eh;
746 	short type = 0;
747 
748 	switch (dst->sa_family) {
749 		case pseudo_AF_HDRCMPLT:
750 		case AF_UNSPEC:
751 			eh = (struct ether_header *)dst->sa_data;
752 			type = eh->ether_type;
753 			break;
754 	}
755 
756 	/*
757 	 * Only allow ethernet types required to initiate or maintain the link,
758 	 * aggregated frames take a different path.
759 	 */
760 	switch (ntohs(type)) {
761 		case ETHERTYPE_PAE:	/* EAPOL PAE/802.1x */
762 			return ((*lp->lp_output)(ifp, m, dst, ro));
763 	}
764 
765 	/* drop any other frames */
766 	m_freem(m);
767 	return (EBUSY);
768 }
769 
770 static void
771 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
772 {
773 	struct lagg_port *lp;
774 	struct lagg_softc *sc;
775 
776 	if ((lp = ifp->if_lagg) == NULL)
777 		return;
778 
779 	sc = lp->lp_softc;
780 
781 	LAGG_WLOCK(sc);
782 	lp->lp_detaching = 1;
783 	lagg_port_destroy(lp, 1);
784 	LAGG_WUNLOCK(sc);
785 }
786 
787 static void
788 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
789 {
790 	struct lagg_softc *sc = lp->lp_softc;
791 
792 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
793 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
794 	rp->rp_prio = lp->lp_prio;
795 	rp->rp_flags = lp->lp_flags;
796 	if (sc->sc_portreq != NULL)
797 		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
798 
799 	/* Add protocol specific flags */
800 	switch (sc->sc_proto) {
801 		case LAGG_PROTO_FAILOVER:
802 			if (lp == sc->sc_primary)
803 				rp->rp_flags |= LAGG_PORT_MASTER;
804 			if (lp == lagg_link_active(sc, sc->sc_primary))
805 				rp->rp_flags |= LAGG_PORT_ACTIVE;
806 			break;
807 
808 		case LAGG_PROTO_ROUNDROBIN:
809 		case LAGG_PROTO_LOADBALANCE:
810 		case LAGG_PROTO_ETHERCHANNEL:
811 			if (LAGG_PORTACTIVE(lp))
812 				rp->rp_flags |= LAGG_PORT_ACTIVE;
813 			break;
814 
815 		case LAGG_PROTO_LACP:
816 			/* LACP has a different definition of active */
817 			if (lacp_isactive(lp))
818 				rp->rp_flags |= LAGG_PORT_ACTIVE;
819 			if (lacp_iscollecting(lp))
820 				rp->rp_flags |= LAGG_PORT_COLLECTING;
821 			if (lacp_isdistributing(lp))
822 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
823 			break;
824 	}
825 
826 }
827 
828 static void
829 lagg_init(void *xsc)
830 {
831 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
832 	struct lagg_port *lp;
833 	struct ifnet *ifp = sc->sc_ifp;
834 
835 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
836 		return;
837 
838 	LAGG_WLOCK(sc);
839 
840 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
841 	/* Update the port lladdrs */
842 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
843 		lagg_port_lladdr(lp, IF_LLADDR(ifp));
844 
845 	if (sc->sc_init != NULL)
846 		(*sc->sc_init)(sc);
847 
848 	LAGG_WUNLOCK(sc);
849 }
850 
851 static void
852 lagg_stop(struct lagg_softc *sc)
853 {
854 	struct ifnet *ifp = sc->sc_ifp;
855 
856 	LAGG_WLOCK_ASSERT(sc);
857 
858 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
859 		return;
860 
861 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
862 
863 	if (sc->sc_stop != NULL)
864 		(*sc->sc_stop)(sc);
865 }
866 
867 static int
868 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
869 {
870 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
871 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
872 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
873 	struct ifreq *ifr = (struct ifreq *)data;
874 	struct lagg_port *lp;
875 	struct ifnet *tpif;
876 	struct thread *td = curthread;
877 	char *buf, *outbuf;
878 	int count, buflen, len, error = 0;
879 
880 	bzero(&rpbuf, sizeof(rpbuf));
881 
882 	switch (cmd) {
883 	case SIOCGLAGG:
884 		LAGG_RLOCK(sc);
885 		count = 0;
886 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
887 			count++;
888 		buflen = count * sizeof(struct lagg_reqport);
889 		LAGG_RUNLOCK(sc);
890 
891 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
892 
893 		LAGG_RLOCK(sc);
894 		ra->ra_proto = sc->sc_proto;
895 		if (sc->sc_req != NULL)
896 			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
897 
898 		count = 0;
899 		buf = outbuf;
900 		len = min(ra->ra_size, buflen);
901 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
902 			if (len < sizeof(rpbuf))
903 				break;
904 
905 			lagg_port2req(lp, &rpbuf);
906 			memcpy(buf, &rpbuf, sizeof(rpbuf));
907 			count++;
908 			buf += sizeof(rpbuf);
909 			len -= sizeof(rpbuf);
910 		}
911 		LAGG_RUNLOCK(sc);
912 		ra->ra_ports = count;
913 		ra->ra_size = count * sizeof(rpbuf);
914 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
915 		free(outbuf, M_TEMP);
916 		break;
917 	case SIOCSLAGG:
918 		error = priv_check(td, PRIV_NET_LAGG);
919 		if (error)
920 			break;
921 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
922 			error = EPROTONOSUPPORT;
923 			break;
924 		}
925 		if (sc->sc_proto != LAGG_PROTO_NONE) {
926 			LAGG_WLOCK(sc);
927 			error = sc->sc_detach(sc);
928 			/* Reset protocol and pointers */
929 			sc->sc_proto = LAGG_PROTO_NONE;
930 			sc->sc_detach = NULL;
931 			sc->sc_start = NULL;
932 			sc->sc_input = NULL;
933 			sc->sc_port_create = NULL;
934 			sc->sc_port_destroy = NULL;
935 			sc->sc_linkstate = NULL;
936 			sc->sc_init = NULL;
937 			sc->sc_stop = NULL;
938 			sc->sc_lladdr = NULL;
939 			sc->sc_req = NULL;
940 			sc->sc_portreq = NULL;
941 			LAGG_WUNLOCK(sc);
942 		}
943 		if (error != 0)
944 			break;
945 		for (int i = 0; i < (sizeof(lagg_protos) /
946 		    sizeof(lagg_protos[0])); i++) {
947 			if (lagg_protos[i].ti_proto == ra->ra_proto) {
948 				if (sc->sc_ifflags & IFF_DEBUG)
949 					printf("%s: using proto %u\n",
950 					    sc->sc_ifname,
951 					    lagg_protos[i].ti_proto);
952 				LAGG_WLOCK(sc);
953 				sc->sc_proto = lagg_protos[i].ti_proto;
954 				if (sc->sc_proto != LAGG_PROTO_NONE)
955 					error = lagg_protos[i].ti_attach(sc);
956 				LAGG_WUNLOCK(sc);
957 				return (error);
958 			}
959 		}
960 		error = EPROTONOSUPPORT;
961 		break;
962 	case SIOCGLAGGPORT:
963 		if (rp->rp_portname[0] == '\0' ||
964 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
965 			error = EINVAL;
966 			break;
967 		}
968 
969 		LAGG_RLOCK(sc);
970 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
971 		    lp->lp_softc != sc) {
972 			error = ENOENT;
973 			LAGG_RUNLOCK(sc);
974 			break;
975 		}
976 
977 		lagg_port2req(lp, rp);
978 		LAGG_RUNLOCK(sc);
979 		break;
980 	case SIOCSLAGGPORT:
981 		error = priv_check(td, PRIV_NET_LAGG);
982 		if (error)
983 			break;
984 		if (rp->rp_portname[0] == '\0' ||
985 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
986 			error = EINVAL;
987 			break;
988 		}
989 		LAGG_WLOCK(sc);
990 		error = lagg_port_create(sc, tpif);
991 		LAGG_WUNLOCK(sc);
992 		break;
993 	case SIOCSLAGGDELPORT:
994 		error = priv_check(td, PRIV_NET_LAGG);
995 		if (error)
996 			break;
997 		if (rp->rp_portname[0] == '\0' ||
998 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
999 			error = EINVAL;
1000 			break;
1001 		}
1002 
1003 		LAGG_WLOCK(sc);
1004 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1005 		    lp->lp_softc != sc) {
1006 			error = ENOENT;
1007 			LAGG_WUNLOCK(sc);
1008 			break;
1009 		}
1010 
1011 		error = lagg_port_destroy(lp, 1);
1012 		LAGG_WUNLOCK(sc);
1013 		break;
1014 	case SIOCSIFFLAGS:
1015 		/* Set flags on ports too */
1016 		LAGG_WLOCK(sc);
1017 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1018 			lagg_setflags(lp, 1);
1019 		}
1020 		LAGG_WUNLOCK(sc);
1021 
1022 		if (!(ifp->if_flags & IFF_UP) &&
1023 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1024 			/*
1025 			 * If interface is marked down and it is running,
1026 			 * then stop and disable it.
1027 			 */
1028 			LAGG_WLOCK(sc);
1029 			lagg_stop(sc);
1030 			LAGG_WUNLOCK(sc);
1031 		} else if ((ifp->if_flags & IFF_UP) &&
1032 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1033 			/*
1034 			 * If interface is marked up and it is stopped, then
1035 			 * start it.
1036 			 */
1037 			(*ifp->if_init)(sc);
1038 		}
1039 		break;
1040 	case SIOCADDMULTI:
1041 	case SIOCDELMULTI:
1042 		LAGG_WLOCK(sc);
1043 		error = lagg_ether_setmulti(sc);
1044 		LAGG_WUNLOCK(sc);
1045 		break;
1046 	case SIOCSIFMEDIA:
1047 	case SIOCGIFMEDIA:
1048 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1049 		break;
1050 
1051 	case SIOCSIFCAP:
1052 	case SIOCSIFMTU:
1053 		/* Do not allow the MTU or caps to be directly changed */
1054 		error = EINVAL;
1055 		break;
1056 
1057 	default:
1058 		error = ether_ioctl(ifp, cmd, data);
1059 		break;
1060 	}
1061 	return (error);
1062 }
1063 
1064 static int
1065 lagg_ether_setmulti(struct lagg_softc *sc)
1066 {
1067 	struct lagg_port *lp;
1068 
1069 	LAGG_WLOCK_ASSERT(sc);
1070 
1071 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1072 		/* First, remove any existing filter entries. */
1073 		lagg_ether_cmdmulti(lp, 0);
1074 		/* copy all addresses from the lagg interface to the port */
1075 		lagg_ether_cmdmulti(lp, 1);
1076 	}
1077 	return (0);
1078 }
1079 
1080 static int
1081 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1082 {
1083 	struct lagg_softc *sc = lp->lp_softc;
1084 	struct ifnet *ifp = lp->lp_ifp;
1085 	struct ifnet *scifp = sc->sc_ifp;
1086 	struct lagg_mc *mc;
1087 	struct ifmultiaddr *ifma, *rifma = NULL;
1088 	struct sockaddr_dl sdl;
1089 	int error;
1090 
1091 	LAGG_WLOCK_ASSERT(sc);
1092 
1093 	bzero((char *)&sdl, sizeof(sdl));
1094 	sdl.sdl_len = sizeof(sdl);
1095 	sdl.sdl_family = AF_LINK;
1096 	sdl.sdl_type = IFT_ETHER;
1097 	sdl.sdl_alen = ETHER_ADDR_LEN;
1098 	sdl.sdl_index = ifp->if_index;
1099 
1100 	if (set) {
1101 		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1102 			if (ifma->ifma_addr->sa_family != AF_LINK)
1103 				continue;
1104 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1105 			    LLADDR(&sdl), ETHER_ADDR_LEN);
1106 
1107 			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1108 			if (error)
1109 				return (error);
1110 			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1111 			if (mc == NULL)
1112 				return (ENOMEM);
1113 			mc->mc_ifma = rifma;
1114 			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1115 		}
1116 	} else {
1117 		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1118 			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1119 			if_delmulti_ifma(mc->mc_ifma);
1120 			free(mc, M_DEVBUF);
1121 		}
1122 	}
1123 	return (0);
1124 }
1125 
1126 /* Handle a ref counted flag that should be set on the lagg port as well */
1127 static int
1128 lagg_setflag(struct lagg_port *lp, int flag, int status,
1129 	     int (*func)(struct ifnet *, int))
1130 {
1131 	struct lagg_softc *sc = lp->lp_softc;
1132 	struct ifnet *scifp = sc->sc_ifp;
1133 	struct ifnet *ifp = lp->lp_ifp;
1134 	int error;
1135 
1136 	LAGG_WLOCK_ASSERT(sc);
1137 
1138 	status = status ? (scifp->if_flags & flag) : 0;
1139 	/* Now "status" contains the flag value or 0 */
1140 
1141 	/*
1142 	 * See if recorded ports status is different from what
1143 	 * we want it to be.  If it is, flip it.  We record ports
1144 	 * status in lp_ifflags so that we won't clear ports flag
1145 	 * we haven't set.  In fact, we don't clear or set ports
1146 	 * flags directly, but get or release references to them.
1147 	 * That's why we can be sure that recorded flags still are
1148 	 * in accord with actual ports flags.
1149 	 */
1150 	if (status != (lp->lp_ifflags & flag)) {
1151 		error = (*func)(ifp, status);
1152 		if (error)
1153 			return (error);
1154 		lp->lp_ifflags &= ~flag;
1155 		lp->lp_ifflags |= status;
1156 	}
1157 	return (0);
1158 }
1159 
1160 /*
1161  * Handle IFF_* flags that require certain changes on the lagg port
1162  * if "status" is true, update ports flags respective to the lagg
1163  * if "status" is false, forcedly clear the flags set on port.
1164  */
1165 static int
1166 lagg_setflags(struct lagg_port *lp, int status)
1167 {
1168 	int error, i;
1169 
1170 	for (i = 0; lagg_pflags[i].flag; i++) {
1171 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1172 		    status, lagg_pflags[i].func);
1173 		if (error)
1174 			return (error);
1175 	}
1176 	return (0);
1177 }
1178 
1179 static void
1180 lagg_start(struct ifnet *ifp)
1181 {
1182 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1183 	struct mbuf *m;
1184 	int error = 0;
1185 
1186 	LAGG_RLOCK(sc);
1187 	/* We need a Tx algorithm and at least one port */
1188 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1189 		IF_DRAIN(&ifp->if_snd);
1190 		LAGG_RUNLOCK(sc);
1191 		return;
1192 	}
1193 
1194 	for (;; error = 0) {
1195 		IFQ_DEQUEUE(&ifp->if_snd, m);
1196 		if (m == NULL)
1197 			break;
1198 
1199 		ETHER_BPF_MTAP(ifp, m);
1200 
1201 		error = (*sc->sc_start)(sc, m);
1202 		if (error == 0)
1203 			ifp->if_opackets++;
1204 		else
1205 			ifp->if_oerrors++;
1206 	}
1207 	LAGG_RUNLOCK(sc);
1208 }
1209 
1210 static struct mbuf *
1211 lagg_input(struct ifnet *ifp, struct mbuf *m)
1212 {
1213 	struct lagg_port *lp = ifp->if_lagg;
1214 	struct lagg_softc *sc = lp->lp_softc;
1215 	struct ifnet *scifp = sc->sc_ifp;
1216 
1217 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1218 	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1219 	    sc->sc_proto == LAGG_PROTO_NONE) {
1220 		m_freem(m);
1221 		return (NULL);
1222 	}
1223 
1224 	LAGG_RLOCK(sc);
1225 	ETHER_BPF_MTAP(scifp, m);
1226 
1227 	m = (*sc->sc_input)(sc, lp, m);
1228 
1229 	if (m != NULL) {
1230 		scifp->if_ipackets++;
1231 		scifp->if_ibytes += m->m_pkthdr.len;
1232 
1233 		if (scifp->if_flags & IFF_MONITOR) {
1234 			m_freem(m);
1235 			m = NULL;
1236 		}
1237 	}
1238 
1239 	LAGG_RUNLOCK(sc);
1240 	return (m);
1241 }
1242 
1243 static int
1244 lagg_media_change(struct ifnet *ifp)
1245 {
1246 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1247 
1248 	if (sc->sc_ifflags & IFF_DEBUG)
1249 		printf("%s\n", __func__);
1250 
1251 	/* Ignore */
1252 	return (0);
1253 }
1254 
1255 static void
1256 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1257 {
1258 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1259 	struct lagg_port *lp;
1260 
1261 	imr->ifm_status = IFM_AVALID;
1262 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1263 
1264 	LAGG_RLOCK(sc);
1265 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1266 		if (LAGG_PORTACTIVE(lp))
1267 			imr->ifm_status |= IFM_ACTIVE;
1268 	}
1269 	LAGG_RUNLOCK(sc);
1270 }
1271 
1272 static void
1273 lagg_linkstate(struct lagg_softc *sc)
1274 {
1275 	struct lagg_port *lp;
1276 	int new_link = LINK_STATE_DOWN;
1277 	uint64_t speed;
1278 
1279 	/* Our link is considered up if at least one of our ports is active */
1280 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1281 		if (lp->lp_link_state == LINK_STATE_UP) {
1282 			new_link = LINK_STATE_UP;
1283 			break;
1284 		}
1285 	}
1286 	if_link_state_change(sc->sc_ifp, new_link);
1287 
1288 	/* Update if_baudrate to reflect the max possible speed */
1289 	switch (sc->sc_proto) {
1290 		case LAGG_PROTO_FAILOVER:
1291 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1292 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1293 			break;
1294 		case LAGG_PROTO_ROUNDROBIN:
1295 		case LAGG_PROTO_LOADBALANCE:
1296 		case LAGG_PROTO_ETHERCHANNEL:
1297 			speed = 0;
1298 			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1299 				speed += lp->lp_ifp->if_baudrate;
1300 			sc->sc_ifp->if_baudrate = speed;
1301 			break;
1302 		case LAGG_PROTO_LACP:
1303 			/* LACP updates if_baudrate itself */
1304 			break;
1305 	}
1306 }
1307 
1308 static void
1309 lagg_port_state(struct ifnet *ifp, int state)
1310 {
1311 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1312 	struct lagg_softc *sc = NULL;
1313 
1314 	if (lp != NULL)
1315 		sc = lp->lp_softc;
1316 	if (sc == NULL)
1317 		return;
1318 
1319 	LAGG_WLOCK(sc);
1320 	lagg_linkstate(sc);
1321 	if (sc->sc_linkstate != NULL)
1322 		(*sc->sc_linkstate)(lp);
1323 	LAGG_WUNLOCK(sc);
1324 }
1325 
1326 struct lagg_port *
1327 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1328 {
1329 	struct lagg_port *lp_next, *rval = NULL;
1330 	// int new_link = LINK_STATE_DOWN;
1331 
1332 	LAGG_RLOCK_ASSERT(sc);
1333 	/*
1334 	 * Search a port which reports an active link state.
1335 	 */
1336 
1337 	if (lp == NULL)
1338 		goto search;
1339 	if (LAGG_PORTACTIVE(lp)) {
1340 		rval = lp;
1341 		goto found;
1342 	}
1343 	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1344 	    LAGG_PORTACTIVE(lp_next)) {
1345 		rval = lp_next;
1346 		goto found;
1347 	}
1348 
1349 search:
1350 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1351 		if (LAGG_PORTACTIVE(lp_next)) {
1352 			rval = lp_next;
1353 			goto found;
1354 		}
1355 	}
1356 
1357 found:
1358 	if (rval != NULL) {
1359 		/*
1360 		 * The IEEE 802.1D standard assumes that a lagg with
1361 		 * multiple ports is always full duplex. This is valid
1362 		 * for load sharing laggs and if at least two links
1363 		 * are active. Unfortunately, checking the latter would
1364 		 * be too expensive at this point.
1365 		 XXX
1366 		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1367 		    (sc->sc_count > 1))
1368 			new_link = LINK_STATE_FULL_DUPLEX;
1369 		else
1370 			new_link = rval->lp_link_state;
1371 		 */
1372 	}
1373 
1374 	return (rval);
1375 }
1376 
1377 static const void *
1378 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1379 {
1380 	if (m->m_pkthdr.len < (off + len)) {
1381 		return (NULL);
1382 	} else if (m->m_len < (off + len)) {
1383 		m_copydata(m, off, len, buf);
1384 		return (buf);
1385 	}
1386 	return (mtod(m, char *) + off);
1387 }
1388 
1389 uint32_t
1390 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1391 {
1392 	uint16_t etype;
1393 	uint32_t p = 0;
1394 	int off;
1395 	struct ether_header *eh;
1396 	struct ether_vlan_header vlanbuf;
1397 	const struct ether_vlan_header *vlan;
1398 #ifdef INET
1399 	const struct ip *ip;
1400 	struct ip ipbuf;
1401 #endif
1402 #ifdef INET6
1403 	const struct ip6_hdr *ip6;
1404 	struct ip6_hdr ip6buf;
1405 	uint32_t flow;
1406 #endif
1407 
1408 	off = sizeof(*eh);
1409 	if (m->m_len < off)
1410 		goto out;
1411 	eh = mtod(m, struct ether_header *);
1412 	etype = ntohs(eh->ether_type);
1413 	p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1414 	p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1415 
1416 	/* Special handling for encapsulating VLAN frames */
1417 	if (m->m_flags & M_VLANTAG) {
1418 		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1419 		    sizeof(m->m_pkthdr.ether_vtag), p);
1420 	} else if (etype == ETHERTYPE_VLAN) {
1421 		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &vlanbuf);
1422 		if (vlan == NULL)
1423 			goto out;
1424 
1425 		p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1426 		etype = ntohs(vlan->evl_proto);
1427 		off += sizeof(*vlan) - sizeof(*eh);
1428 	}
1429 
1430 	switch (etype) {
1431 #ifdef INET
1432 	case ETHERTYPE_IP:
1433 		ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1434 		if (ip == NULL)
1435 			goto out;
1436 
1437 		p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1438 		p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1439 		break;
1440 #endif
1441 #ifdef INET6
1442 	case ETHERTYPE_IPV6:
1443 		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1444 		if (ip6 == NULL)
1445 			goto out;
1446 
1447 		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1448 		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1449 		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1450 		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1451 		break;
1452 #endif
1453 	}
1454 out:
1455 	return (p);
1456 }
1457 
1458 int
1459 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1460 {
1461 
1462 	return (ifp->if_transmit)(ifp, m);
1463 }
1464 
1465 /*
1466  * Simple round robin aggregation
1467  */
1468 
1469 static int
1470 lagg_rr_attach(struct lagg_softc *sc)
1471 {
1472 	sc->sc_detach = lagg_rr_detach;
1473 	sc->sc_start = lagg_rr_start;
1474 	sc->sc_input = lagg_rr_input;
1475 	sc->sc_port_create = NULL;
1476 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1477 	sc->sc_seq = 0;
1478 
1479 	return (0);
1480 }
1481 
1482 static int
1483 lagg_rr_detach(struct lagg_softc *sc)
1484 {
1485 	return (0);
1486 }
1487 
1488 static int
1489 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1490 {
1491 	struct lagg_port *lp;
1492 	uint32_t p;
1493 
1494 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1495 	p %= sc->sc_count;
1496 	lp = SLIST_FIRST(&sc->sc_ports);
1497 	while (p--)
1498 		lp = SLIST_NEXT(lp, lp_entries);
1499 
1500 	/*
1501 	 * Check the port's link state. This will return the next active
1502 	 * port if the link is down or the port is NULL.
1503 	 */
1504 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1505 		m_freem(m);
1506 		return (ENOENT);
1507 	}
1508 
1509 	/* Send mbuf */
1510 	return (lagg_enqueue(lp->lp_ifp, m));
1511 }
1512 
1513 static struct mbuf *
1514 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1515 {
1516 	struct ifnet *ifp = sc->sc_ifp;
1517 
1518 	/* Just pass in the packet to our lagg device */
1519 	m->m_pkthdr.rcvif = ifp;
1520 
1521 	return (m);
1522 }
1523 
1524 /*
1525  * Active failover
1526  */
1527 
1528 static int
1529 lagg_fail_attach(struct lagg_softc *sc)
1530 {
1531 	sc->sc_detach = lagg_fail_detach;
1532 	sc->sc_start = lagg_fail_start;
1533 	sc->sc_input = lagg_fail_input;
1534 	sc->sc_port_create = NULL;
1535 	sc->sc_port_destroy = NULL;
1536 
1537 	return (0);
1538 }
1539 
1540 static int
1541 lagg_fail_detach(struct lagg_softc *sc)
1542 {
1543 	return (0);
1544 }
1545 
1546 static int
1547 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1548 {
1549 	struct lagg_port *lp;
1550 
1551 	/* Use the master port if active or the next available port */
1552 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1553 		m_freem(m);
1554 		return (ENOENT);
1555 	}
1556 
1557 	/* Send mbuf */
1558 	return (lagg_enqueue(lp->lp_ifp, m));
1559 }
1560 
1561 static struct mbuf *
1562 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1563 {
1564 	struct ifnet *ifp = sc->sc_ifp;
1565 	struct lagg_port *tmp_tp;
1566 
1567 	if (lp == sc->sc_primary) {
1568 		m->m_pkthdr.rcvif = ifp;
1569 		return (m);
1570 	}
1571 
1572 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1573 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1574 		/*
1575 		 * If tmp_tp is null, we've recieved a packet when all
1576 		 * our links are down. Weird, but process it anyways.
1577 		 */
1578 		if ((tmp_tp == NULL || tmp_tp == lp)) {
1579 			m->m_pkthdr.rcvif = ifp;
1580 			return (m);
1581 		}
1582 	}
1583 
1584 	m_freem(m);
1585 	return (NULL);
1586 }
1587 
1588 /*
1589  * Loadbalancing
1590  */
1591 
1592 static int
1593 lagg_lb_attach(struct lagg_softc *sc)
1594 {
1595 	struct lagg_port *lp;
1596 	struct lagg_lb *lb;
1597 
1598 	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1599 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1600 		return (ENOMEM);
1601 
1602 	sc->sc_detach = lagg_lb_detach;
1603 	sc->sc_start = lagg_lb_start;
1604 	sc->sc_input = lagg_lb_input;
1605 	sc->sc_port_create = lagg_lb_port_create;
1606 	sc->sc_port_destroy = lagg_lb_port_destroy;
1607 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1608 
1609 	lb->lb_key = arc4random();
1610 	sc->sc_psc = (caddr_t)lb;
1611 
1612 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1613 		lagg_lb_port_create(lp);
1614 
1615 	return (0);
1616 }
1617 
1618 static int
1619 lagg_lb_detach(struct lagg_softc *sc)
1620 {
1621 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1622 	if (lb != NULL)
1623 		free(lb, M_DEVBUF);
1624 	return (0);
1625 }
1626 
1627 static int
1628 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1629 {
1630 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1631 	struct lagg_port *lp_next;
1632 	int i = 0;
1633 
1634 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1635 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1636 		if (lp_next == lp)
1637 			continue;
1638 		if (i >= LAGG_MAX_PORTS)
1639 			return (EINVAL);
1640 		if (sc->sc_ifflags & IFF_DEBUG)
1641 			printf("%s: port %s at index %d\n",
1642 			    sc->sc_ifname, lp_next->lp_ifname, i);
1643 		lb->lb_ports[i++] = lp_next;
1644 	}
1645 
1646 	return (0);
1647 }
1648 
1649 static int
1650 lagg_lb_port_create(struct lagg_port *lp)
1651 {
1652 	struct lagg_softc *sc = lp->lp_softc;
1653 	return (lagg_lb_porttable(sc, NULL));
1654 }
1655 
1656 static void
1657 lagg_lb_port_destroy(struct lagg_port *lp)
1658 {
1659 	struct lagg_softc *sc = lp->lp_softc;
1660 	lagg_lb_porttable(sc, lp);
1661 }
1662 
1663 static int
1664 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1665 {
1666 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1667 	struct lagg_port *lp = NULL;
1668 	uint32_t p = 0;
1669 
1670 	if (m->m_flags & M_FLOWID)
1671 		p = m->m_pkthdr.flowid;
1672 	else
1673 		p = lagg_hashmbuf(m, lb->lb_key);
1674 	p %= sc->sc_count;
1675 	lp = lb->lb_ports[p];
1676 
1677 	/*
1678 	 * Check the port's link state. This will return the next active
1679 	 * port if the link is down or the port is NULL.
1680 	 */
1681 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1682 		m_freem(m);
1683 		return (ENOENT);
1684 	}
1685 
1686 	/* Send mbuf */
1687 	return (lagg_enqueue(lp->lp_ifp, m));
1688 }
1689 
1690 static struct mbuf *
1691 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1692 {
1693 	struct ifnet *ifp = sc->sc_ifp;
1694 
1695 	/* Just pass in the packet to our lagg device */
1696 	m->m_pkthdr.rcvif = ifp;
1697 
1698 	return (m);
1699 }
1700 
1701 /*
1702  * 802.3ad LACP
1703  */
1704 
1705 static int
1706 lagg_lacp_attach(struct lagg_softc *sc)
1707 {
1708 	struct lagg_port *lp;
1709 	int error;
1710 
1711 	sc->sc_detach = lagg_lacp_detach;
1712 	sc->sc_port_create = lacp_port_create;
1713 	sc->sc_port_destroy = lacp_port_destroy;
1714 	sc->sc_linkstate = lacp_linkstate;
1715 	sc->sc_start = lagg_lacp_start;
1716 	sc->sc_input = lagg_lacp_input;
1717 	sc->sc_init = lacp_init;
1718 	sc->sc_stop = lacp_stop;
1719 	sc->sc_lladdr = lagg_lacp_lladdr;
1720 	sc->sc_req = lacp_req;
1721 	sc->sc_portreq = lacp_portreq;
1722 
1723 	error = lacp_attach(sc);
1724 	if (error)
1725 		return (error);
1726 
1727 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1728 		lacp_port_create(lp);
1729 
1730 	return (error);
1731 }
1732 
1733 static int
1734 lagg_lacp_detach(struct lagg_softc *sc)
1735 {
1736 	struct lagg_port *lp;
1737 	int error;
1738 
1739 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1740 		lacp_port_destroy(lp);
1741 
1742 	/* unlocking is safe here */
1743 	LAGG_WUNLOCK(sc);
1744 	error = lacp_detach(sc);
1745 	LAGG_WLOCK(sc);
1746 
1747 	return (error);
1748 }
1749 
1750 static void
1751 lagg_lacp_lladdr(struct lagg_softc *sc)
1752 {
1753 	struct lagg_port *lp;
1754 
1755 	/* purge all the lacp ports */
1756 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1757 		lacp_port_destroy(lp);
1758 
1759 	/* add them back in */
1760 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1761 		lacp_port_create(lp);
1762 }
1763 
1764 static int
1765 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1766 {
1767 	struct lagg_port *lp;
1768 
1769 	lp = lacp_select_tx_port(sc, m);
1770 	if (lp == NULL) {
1771 		m_freem(m);
1772 		return (EBUSY);
1773 	}
1774 
1775 	/* Send mbuf */
1776 	return (lagg_enqueue(lp->lp_ifp, m));
1777 }
1778 
1779 static struct mbuf *
1780 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1781 {
1782 	struct ifnet *ifp = sc->sc_ifp;
1783 	struct ether_header *eh;
1784 	u_short etype;
1785 
1786 	eh = mtod(m, struct ether_header *);
1787 	etype = ntohs(eh->ether_type);
1788 
1789 	/* Tap off LACP control messages */
1790 	if (etype == ETHERTYPE_SLOW) {
1791 		m = lacp_input(lp, m);
1792 		if (m == NULL)
1793 			return (NULL);
1794 	}
1795 
1796 	/*
1797 	 * If the port is not collecting or not in the active aggregator then
1798 	 * free and return.
1799 	 */
1800 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1801 		m_freem(m);
1802 		return (NULL);
1803 	}
1804 
1805 	m->m_pkthdr.rcvif = ifp;
1806 	return (m);
1807 }
1808