xref: /freebsd/sys/net/if_lagg.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
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 	/* Check if port has already been associated to a lagg */
488 	if (ifp->if_lagg != NULL)
489 		return (EBUSY);
490 
491 	/* XXX Disallow non-ethernet interfaces (this should be any of 802) */
492 	if (ifp->if_type != IFT_ETHER)
493 		return (EPROTONOSUPPORT);
494 
495 	/* Allow the first Ethernet member to define the MTU */
496 	if (SLIST_EMPTY(&sc->sc_ports))
497 		sc->sc_ifp->if_mtu = ifp->if_mtu;
498 	else if (sc->sc_ifp->if_mtu != ifp->if_mtu) {
499 		if_printf(sc->sc_ifp, "invalid MTU for %s\n",
500 		    ifp->if_xname);
501 		return (EINVAL);
502 	}
503 
504 	if ((lp = malloc(sizeof(struct lagg_port),
505 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
506 		return (ENOMEM);
507 
508 	/* Check if port is a stacked lagg */
509 	mtx_lock(&lagg_list_mtx);
510 	SLIST_FOREACH(sc_ptr, &lagg_list, sc_entries) {
511 		if (ifp == sc_ptr->sc_ifp) {
512 			mtx_unlock(&lagg_list_mtx);
513 			free(lp, M_DEVBUF);
514 			return (EINVAL);
515 			/* XXX disable stacking for the moment, its untested */
516 #ifdef LAGG_PORT_STACKING
517 			lp->lp_flags |= LAGG_PORT_STACK;
518 			if (lagg_port_checkstacking(sc_ptr) >=
519 			    LAGG_MAX_STACKING) {
520 				mtx_unlock(&lagg_list_mtx);
521 				free(lp, M_DEVBUF);
522 				return (E2BIG);
523 			}
524 #endif
525 		}
526 	}
527 	mtx_unlock(&lagg_list_mtx);
528 
529 	/* Change the interface type */
530 	lp->lp_iftype = ifp->if_type;
531 	ifp->if_type = IFT_IEEE8023ADLAG;
532 	ifp->if_lagg = lp;
533 	lp->lp_ioctl = ifp->if_ioctl;
534 	ifp->if_ioctl = lagg_port_ioctl;
535 	lp->lp_output = ifp->if_output;
536 	ifp->if_output = lagg_port_output;
537 
538 	lp->lp_ifp = ifp;
539 	lp->lp_softc = sc;
540 
541 	/* Save port link layer address */
542 	bcopy(IF_LLADDR(ifp), lp->lp_lladdr, ETHER_ADDR_LEN);
543 
544 	if (SLIST_EMPTY(&sc->sc_ports)) {
545 		sc->sc_primary = lp;
546 		lagg_lladdr(sc, IF_LLADDR(ifp));
547 	} else {
548 		/* Update link layer address for this port */
549 		lagg_port_lladdr(lp, IF_LLADDR(sc->sc_ifp));
550 	}
551 
552 	/* Insert into the list of ports */
553 	SLIST_INSERT_HEAD(&sc->sc_ports, lp, lp_entries);
554 	sc->sc_count++;
555 
556 	/* Update lagg capabilities */
557 	lagg_capabilities(sc);
558 	lagg_linkstate(sc);
559 
560 	/* Add multicast addresses and interface flags to this port */
561 	lagg_ether_cmdmulti(lp, 1);
562 	lagg_setflags(lp, 1);
563 
564 	if (sc->sc_port_create != NULL)
565 		error = (*sc->sc_port_create)(lp);
566 	if (error) {
567 		/* remove the port again, without calling sc_port_destroy */
568 		lagg_port_destroy(lp, 0);
569 		return (error);
570 	}
571 
572 	return (error);
573 }
574 
575 #ifdef LAGG_PORT_STACKING
576 static int
577 lagg_port_checkstacking(struct lagg_softc *sc)
578 {
579 	struct lagg_softc *sc_ptr;
580 	struct lagg_port *lp;
581 	int m = 0;
582 
583 	LAGG_WLOCK_ASSERT(sc);
584 
585 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
586 		if (lp->lp_flags & LAGG_PORT_STACK) {
587 			sc_ptr = (struct lagg_softc *)lp->lp_ifp->if_softc;
588 			m = MAX(m, lagg_port_checkstacking(sc_ptr));
589 		}
590 	}
591 
592 	return (m + 1);
593 }
594 #endif
595 
596 static int
597 lagg_port_destroy(struct lagg_port *lp, int runpd)
598 {
599 	struct lagg_softc *sc = lp->lp_softc;
600 	struct lagg_port *lp_ptr;
601 	struct lagg_llq *llq;
602 	struct ifnet *ifp = lp->lp_ifp;
603 
604 	LAGG_WLOCK_ASSERT(sc);
605 
606 	if (runpd && sc->sc_port_destroy != NULL)
607 		(*sc->sc_port_destroy)(lp);
608 
609 	/*
610 	 * Remove multicast addresses and interface flags from this port and
611 	 * reset the MAC address, skip if the interface is being detached.
612 	 */
613 	if (!lp->lp_detaching) {
614 		lagg_ether_cmdmulti(lp, 0);
615 		lagg_setflags(lp, 0);
616 		lagg_port_lladdr(lp, lp->lp_lladdr);
617 	}
618 
619 	/* Restore interface */
620 	ifp->if_type = lp->lp_iftype;
621 	ifp->if_ioctl = lp->lp_ioctl;
622 	ifp->if_output = lp->lp_output;
623 	ifp->if_lagg = NULL;
624 
625 	/* Finally, remove the port from the lagg */
626 	SLIST_REMOVE(&sc->sc_ports, lp, lagg_port, lp_entries);
627 	sc->sc_count--;
628 
629 	/* Update the primary interface */
630 	if (lp == sc->sc_primary) {
631 		uint8_t lladdr[ETHER_ADDR_LEN];
632 
633 		if ((lp_ptr = SLIST_FIRST(&sc->sc_ports)) == NULL) {
634 			bzero(&lladdr, ETHER_ADDR_LEN);
635 		} else {
636 			bcopy(lp_ptr->lp_lladdr,
637 			    lladdr, ETHER_ADDR_LEN);
638 		}
639 		lagg_lladdr(sc, lladdr);
640 		sc->sc_primary = lp_ptr;
641 
642 		/* Update link layer address for each port */
643 		SLIST_FOREACH(lp_ptr, &sc->sc_ports, lp_entries)
644 			lagg_port_lladdr(lp_ptr, lladdr);
645 	}
646 
647 	/* Remove any pending lladdr changes from the queue */
648 	if (lp->lp_detaching) {
649 		SLIST_FOREACH(llq, &sc->sc_llq_head, llq_entries) {
650 			if (llq->llq_ifp == ifp) {
651 				SLIST_REMOVE(&sc->sc_llq_head, llq, lagg_llq,
652 				    llq_entries);
653 				free(llq, M_DEVBUF);
654 				break;	/* Only appears once */
655 			}
656 		}
657 	}
658 
659 	if (lp->lp_ifflags)
660 		if_printf(ifp, "%s: lp_ifflags unclean\n", __func__);
661 
662 	free(lp, M_DEVBUF);
663 
664 	/* Update lagg capabilities */
665 	lagg_capabilities(sc);
666 	lagg_linkstate(sc);
667 
668 	return (0);
669 }
670 
671 static int
672 lagg_port_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
673 {
674 	struct lagg_reqport *rp = (struct lagg_reqport *)data;
675 	struct lagg_softc *sc;
676 	struct lagg_port *lp = NULL;
677 	int error = 0;
678 
679 	/* Should be checked by the caller */
680 	if (ifp->if_type != IFT_IEEE8023ADLAG ||
681 	    (lp = ifp->if_lagg) == NULL || (sc = lp->lp_softc) == NULL)
682 		goto fallback;
683 
684 	switch (cmd) {
685 	case SIOCGLAGGPORT:
686 		if (rp->rp_portname[0] == '\0' ||
687 		    ifunit(rp->rp_portname) != ifp) {
688 			error = EINVAL;
689 			break;
690 		}
691 
692 		LAGG_RLOCK(sc);
693 		if ((lp = ifp->if_lagg) == NULL || lp->lp_softc != sc) {
694 			error = ENOENT;
695 			LAGG_RUNLOCK(sc);
696 			break;
697 		}
698 
699 		lagg_port2req(lp, rp);
700 		LAGG_RUNLOCK(sc);
701 		break;
702 
703 	case SIOCSIFCAP:
704 		if (lp->lp_ioctl == NULL) {
705 			error = EINVAL;
706 			break;
707 		}
708 		error = (*lp->lp_ioctl)(ifp, cmd, data);
709 		if (error)
710 			break;
711 
712 		/* Update lagg interface capabilities */
713 		LAGG_WLOCK(sc);
714 		lagg_capabilities(sc);
715 		LAGG_WUNLOCK(sc);
716 		break;
717 
718 	case SIOCSIFMTU:
719 		/* Do not allow the MTU to be changed once joined */
720 		error = EINVAL;
721 		break;
722 
723 	default:
724 		goto fallback;
725 	}
726 
727 	return (error);
728 
729 fallback:
730 	if (lp->lp_ioctl != NULL)
731 		return ((*lp->lp_ioctl)(ifp, cmd, data));
732 
733 	return (EINVAL);
734 }
735 
736 static int
737 lagg_port_output(struct ifnet *ifp, struct mbuf *m,
738 	struct sockaddr *dst, struct route *ro)
739 {
740 	struct lagg_port *lp = ifp->if_lagg;
741 	struct ether_header *eh;
742 	short type = 0;
743 
744 	switch (dst->sa_family) {
745 		case pseudo_AF_HDRCMPLT:
746 		case AF_UNSPEC:
747 			eh = (struct ether_header *)dst->sa_data;
748 			type = eh->ether_type;
749 			break;
750 	}
751 
752 	/*
753 	 * Only allow ethernet types required to initiate or maintain the link,
754 	 * aggregated frames take a different path.
755 	 */
756 	switch (ntohs(type)) {
757 		case ETHERTYPE_PAE:	/* EAPOL PAE/802.1x */
758 			return ((*lp->lp_output)(ifp, m, dst, ro));
759 	}
760 
761 	/* drop any other frames */
762 	m_freem(m);
763 	return (EBUSY);
764 }
765 
766 static void
767 lagg_port_ifdetach(void *arg __unused, struct ifnet *ifp)
768 {
769 	struct lagg_port *lp;
770 	struct lagg_softc *sc;
771 
772 	if ((lp = ifp->if_lagg) == NULL)
773 		return;
774 
775 	sc = lp->lp_softc;
776 
777 	LAGG_WLOCK(sc);
778 	lp->lp_detaching = 1;
779 	lagg_port_destroy(lp, 1);
780 	LAGG_WUNLOCK(sc);
781 }
782 
783 static void
784 lagg_port2req(struct lagg_port *lp, struct lagg_reqport *rp)
785 {
786 	struct lagg_softc *sc = lp->lp_softc;
787 
788 	strlcpy(rp->rp_ifname, sc->sc_ifname, sizeof(rp->rp_ifname));
789 	strlcpy(rp->rp_portname, lp->lp_ifp->if_xname, sizeof(rp->rp_portname));
790 	rp->rp_prio = lp->lp_prio;
791 	rp->rp_flags = lp->lp_flags;
792 	if (sc->sc_portreq != NULL)
793 		(*sc->sc_portreq)(lp, (caddr_t)&rp->rp_psc);
794 
795 	/* Add protocol specific flags */
796 	switch (sc->sc_proto) {
797 		case LAGG_PROTO_FAILOVER:
798 			if (lp == sc->sc_primary)
799 				rp->rp_flags |= LAGG_PORT_MASTER;
800 			if (lp == lagg_link_active(sc, sc->sc_primary))
801 				rp->rp_flags |= LAGG_PORT_ACTIVE;
802 			break;
803 
804 		case LAGG_PROTO_ROUNDROBIN:
805 		case LAGG_PROTO_LOADBALANCE:
806 		case LAGG_PROTO_ETHERCHANNEL:
807 			if (LAGG_PORTACTIVE(lp))
808 				rp->rp_flags |= LAGG_PORT_ACTIVE;
809 			break;
810 
811 		case LAGG_PROTO_LACP:
812 			/* LACP has a different definition of active */
813 			if (lacp_isactive(lp))
814 				rp->rp_flags |= LAGG_PORT_ACTIVE;
815 			if (lacp_iscollecting(lp))
816 				rp->rp_flags |= LAGG_PORT_COLLECTING;
817 			if (lacp_isdistributing(lp))
818 				rp->rp_flags |= LAGG_PORT_DISTRIBUTING;
819 			break;
820 	}
821 
822 }
823 
824 static void
825 lagg_init(void *xsc)
826 {
827 	struct lagg_softc *sc = (struct lagg_softc *)xsc;
828 	struct lagg_port *lp;
829 	struct ifnet *ifp = sc->sc_ifp;
830 
831 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
832 		return;
833 
834 	LAGG_WLOCK(sc);
835 
836 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
837 	/* Update the port lladdrs */
838 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
839 		lagg_port_lladdr(lp, IF_LLADDR(ifp));
840 
841 	if (sc->sc_init != NULL)
842 		(*sc->sc_init)(sc);
843 
844 	LAGG_WUNLOCK(sc);
845 }
846 
847 static void
848 lagg_stop(struct lagg_softc *sc)
849 {
850 	struct ifnet *ifp = sc->sc_ifp;
851 
852 	LAGG_WLOCK_ASSERT(sc);
853 
854 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
855 		return;
856 
857 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
858 
859 	if (sc->sc_stop != NULL)
860 		(*sc->sc_stop)(sc);
861 }
862 
863 static int
864 lagg_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
865 {
866 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
867 	struct lagg_reqall *ra = (struct lagg_reqall *)data;
868 	struct lagg_reqport *rp = (struct lagg_reqport *)data, rpbuf;
869 	struct ifreq *ifr = (struct ifreq *)data;
870 	struct lagg_port *lp;
871 	struct ifnet *tpif;
872 	struct thread *td = curthread;
873 	char *buf, *outbuf;
874 	int count, buflen, len, error = 0;
875 
876 	bzero(&rpbuf, sizeof(rpbuf));
877 
878 	switch (cmd) {
879 	case SIOCGLAGG:
880 		LAGG_RLOCK(sc);
881 		count = 0;
882 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
883 			count++;
884 		buflen = count * sizeof(struct lagg_reqport);
885 		LAGG_RUNLOCK(sc);
886 
887 		outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
888 
889 		LAGG_RLOCK(sc);
890 		ra->ra_proto = sc->sc_proto;
891 		if (sc->sc_req != NULL)
892 			(*sc->sc_req)(sc, (caddr_t)&ra->ra_psc);
893 
894 		count = 0;
895 		buf = outbuf;
896 		len = min(ra->ra_size, buflen);
897 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
898 			if (len < sizeof(rpbuf))
899 				break;
900 
901 			lagg_port2req(lp, &rpbuf);
902 			memcpy(buf, &rpbuf, sizeof(rpbuf));
903 			count++;
904 			buf += sizeof(rpbuf);
905 			len -= sizeof(rpbuf);
906 		}
907 		LAGG_RUNLOCK(sc);
908 		ra->ra_ports = count;
909 		ra->ra_size = count * sizeof(rpbuf);
910 		error = copyout(outbuf, ra->ra_port, ra->ra_size);
911 		free(outbuf, M_TEMP);
912 		break;
913 	case SIOCSLAGG:
914 		error = priv_check(td, PRIV_NET_LAGG);
915 		if (error)
916 			break;
917 		if (ra->ra_proto >= LAGG_PROTO_MAX) {
918 			error = EPROTONOSUPPORT;
919 			break;
920 		}
921 		if (sc->sc_proto != LAGG_PROTO_NONE) {
922 			LAGG_WLOCK(sc);
923 			error = sc->sc_detach(sc);
924 			/* Reset protocol and pointers */
925 			sc->sc_proto = LAGG_PROTO_NONE;
926 			sc->sc_detach = NULL;
927 			sc->sc_start = NULL;
928 			sc->sc_input = NULL;
929 			sc->sc_port_create = NULL;
930 			sc->sc_port_destroy = NULL;
931 			sc->sc_linkstate = NULL;
932 			sc->sc_init = NULL;
933 			sc->sc_stop = NULL;
934 			sc->sc_lladdr = NULL;
935 			sc->sc_req = NULL;
936 			sc->sc_portreq = NULL;
937 			LAGG_WUNLOCK(sc);
938 		}
939 		if (error != 0)
940 			break;
941 		for (int i = 0; i < (sizeof(lagg_protos) /
942 		    sizeof(lagg_protos[0])); i++) {
943 			if (lagg_protos[i].ti_proto == ra->ra_proto) {
944 				if (sc->sc_ifflags & IFF_DEBUG)
945 					printf("%s: using proto %u\n",
946 					    sc->sc_ifname,
947 					    lagg_protos[i].ti_proto);
948 				LAGG_WLOCK(sc);
949 				sc->sc_proto = lagg_protos[i].ti_proto;
950 				if (sc->sc_proto != LAGG_PROTO_NONE)
951 					error = lagg_protos[i].ti_attach(sc);
952 				LAGG_WUNLOCK(sc);
953 				return (error);
954 			}
955 		}
956 		error = EPROTONOSUPPORT;
957 		break;
958 	case SIOCGLAGGPORT:
959 		if (rp->rp_portname[0] == '\0' ||
960 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
961 			error = EINVAL;
962 			break;
963 		}
964 
965 		LAGG_RLOCK(sc);
966 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
967 		    lp->lp_softc != sc) {
968 			error = ENOENT;
969 			LAGG_RUNLOCK(sc);
970 			break;
971 		}
972 
973 		lagg_port2req(lp, rp);
974 		LAGG_RUNLOCK(sc);
975 		break;
976 	case SIOCSLAGGPORT:
977 		error = priv_check(td, PRIV_NET_LAGG);
978 		if (error)
979 			break;
980 		if (rp->rp_portname[0] == '\0' ||
981 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
982 			error = EINVAL;
983 			break;
984 		}
985 		LAGG_WLOCK(sc);
986 		error = lagg_port_create(sc, tpif);
987 		LAGG_WUNLOCK(sc);
988 		break;
989 	case SIOCSLAGGDELPORT:
990 		error = priv_check(td, PRIV_NET_LAGG);
991 		if (error)
992 			break;
993 		if (rp->rp_portname[0] == '\0' ||
994 		    (tpif = ifunit(rp->rp_portname)) == NULL) {
995 			error = EINVAL;
996 			break;
997 		}
998 
999 		LAGG_WLOCK(sc);
1000 		if ((lp = (struct lagg_port *)tpif->if_lagg) == NULL ||
1001 		    lp->lp_softc != sc) {
1002 			error = ENOENT;
1003 			LAGG_WUNLOCK(sc);
1004 			break;
1005 		}
1006 
1007 		error = lagg_port_destroy(lp, 1);
1008 		LAGG_WUNLOCK(sc);
1009 		break;
1010 	case SIOCSIFFLAGS:
1011 		/* Set flags on ports too */
1012 		LAGG_WLOCK(sc);
1013 		SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1014 			lagg_setflags(lp, 1);
1015 		}
1016 		LAGG_WUNLOCK(sc);
1017 
1018 		if (!(ifp->if_flags & IFF_UP) &&
1019 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1020 			/*
1021 			 * If interface is marked down and it is running,
1022 			 * then stop and disable it.
1023 			 */
1024 			LAGG_WLOCK(sc);
1025 			lagg_stop(sc);
1026 			LAGG_WUNLOCK(sc);
1027 		} else if ((ifp->if_flags & IFF_UP) &&
1028 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1029 			/*
1030 			 * If interface is marked up and it is stopped, then
1031 			 * start it.
1032 			 */
1033 			(*ifp->if_init)(sc);
1034 		}
1035 		break;
1036 	case SIOCADDMULTI:
1037 	case SIOCDELMULTI:
1038 		LAGG_WLOCK(sc);
1039 		error = lagg_ether_setmulti(sc);
1040 		LAGG_WUNLOCK(sc);
1041 		break;
1042 	case SIOCSIFMEDIA:
1043 	case SIOCGIFMEDIA:
1044 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1045 		break;
1046 
1047 	case SIOCSIFCAP:
1048 	case SIOCSIFMTU:
1049 		/* Do not allow the MTU or caps to be directly changed */
1050 		error = EINVAL;
1051 		break;
1052 
1053 	default:
1054 		error = ether_ioctl(ifp, cmd, data);
1055 		break;
1056 	}
1057 	return (error);
1058 }
1059 
1060 static int
1061 lagg_ether_setmulti(struct lagg_softc *sc)
1062 {
1063 	struct lagg_port *lp;
1064 
1065 	LAGG_WLOCK_ASSERT(sc);
1066 
1067 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1068 		/* First, remove any existing filter entries. */
1069 		lagg_ether_cmdmulti(lp, 0);
1070 		/* copy all addresses from the lagg interface to the port */
1071 		lagg_ether_cmdmulti(lp, 1);
1072 	}
1073 	return (0);
1074 }
1075 
1076 static int
1077 lagg_ether_cmdmulti(struct lagg_port *lp, int set)
1078 {
1079 	struct lagg_softc *sc = lp->lp_softc;
1080 	struct ifnet *ifp = lp->lp_ifp;
1081 	struct ifnet *scifp = sc->sc_ifp;
1082 	struct lagg_mc *mc;
1083 	struct ifmultiaddr *ifma, *rifma = NULL;
1084 	struct sockaddr_dl sdl;
1085 	int error;
1086 
1087 	LAGG_WLOCK_ASSERT(sc);
1088 
1089 	bzero((char *)&sdl, sizeof(sdl));
1090 	sdl.sdl_len = sizeof(sdl);
1091 	sdl.sdl_family = AF_LINK;
1092 	sdl.sdl_type = IFT_ETHER;
1093 	sdl.sdl_alen = ETHER_ADDR_LEN;
1094 	sdl.sdl_index = ifp->if_index;
1095 
1096 	if (set) {
1097 		TAILQ_FOREACH(ifma, &scifp->if_multiaddrs, ifma_link) {
1098 			if (ifma->ifma_addr->sa_family != AF_LINK)
1099 				continue;
1100 			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1101 			    LLADDR(&sdl), ETHER_ADDR_LEN);
1102 
1103 			error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
1104 			if (error)
1105 				return (error);
1106 			mc = malloc(sizeof(struct lagg_mc), M_DEVBUF, M_NOWAIT);
1107 			if (mc == NULL)
1108 				return (ENOMEM);
1109 			mc->mc_ifma = rifma;
1110 			SLIST_INSERT_HEAD(&lp->lp_mc_head, mc, mc_entries);
1111 		}
1112 	} else {
1113 		while ((mc = SLIST_FIRST(&lp->lp_mc_head)) != NULL) {
1114 			SLIST_REMOVE(&lp->lp_mc_head, mc, lagg_mc, mc_entries);
1115 			if_delmulti_ifma(mc->mc_ifma);
1116 			free(mc, M_DEVBUF);
1117 		}
1118 	}
1119 	return (0);
1120 }
1121 
1122 /* Handle a ref counted flag that should be set on the lagg port as well */
1123 static int
1124 lagg_setflag(struct lagg_port *lp, int flag, int status,
1125 	     int (*func)(struct ifnet *, int))
1126 {
1127 	struct lagg_softc *sc = lp->lp_softc;
1128 	struct ifnet *scifp = sc->sc_ifp;
1129 	struct ifnet *ifp = lp->lp_ifp;
1130 	int error;
1131 
1132 	LAGG_WLOCK_ASSERT(sc);
1133 
1134 	status = status ? (scifp->if_flags & flag) : 0;
1135 	/* Now "status" contains the flag value or 0 */
1136 
1137 	/*
1138 	 * See if recorded ports status is different from what
1139 	 * we want it to be.  If it is, flip it.  We record ports
1140 	 * status in lp_ifflags so that we won't clear ports flag
1141 	 * we haven't set.  In fact, we don't clear or set ports
1142 	 * flags directly, but get or release references to them.
1143 	 * That's why we can be sure that recorded flags still are
1144 	 * in accord with actual ports flags.
1145 	 */
1146 	if (status != (lp->lp_ifflags & flag)) {
1147 		error = (*func)(ifp, status);
1148 		if (error)
1149 			return (error);
1150 		lp->lp_ifflags &= ~flag;
1151 		lp->lp_ifflags |= status;
1152 	}
1153 	return (0);
1154 }
1155 
1156 /*
1157  * Handle IFF_* flags that require certain changes on the lagg port
1158  * if "status" is true, update ports flags respective to the lagg
1159  * if "status" is false, forcedly clear the flags set on port.
1160  */
1161 static int
1162 lagg_setflags(struct lagg_port *lp, int status)
1163 {
1164 	int error, i;
1165 
1166 	for (i = 0; lagg_pflags[i].flag; i++) {
1167 		error = lagg_setflag(lp, lagg_pflags[i].flag,
1168 		    status, lagg_pflags[i].func);
1169 		if (error)
1170 			return (error);
1171 	}
1172 	return (0);
1173 }
1174 
1175 static void
1176 lagg_start(struct ifnet *ifp)
1177 {
1178 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1179 	struct mbuf *m;
1180 	int error = 0;
1181 
1182 	LAGG_RLOCK(sc);
1183 	/* We need a Tx algorithm and at least one port */
1184 	if (sc->sc_proto == LAGG_PROTO_NONE || sc->sc_count == 0) {
1185 		IF_DRAIN(&ifp->if_snd);
1186 		LAGG_RUNLOCK(sc);
1187 		return;
1188 	}
1189 
1190 	for (;; error = 0) {
1191 		IFQ_DEQUEUE(&ifp->if_snd, m);
1192 		if (m == NULL)
1193 			break;
1194 
1195 		ETHER_BPF_MTAP(ifp, m);
1196 
1197 		error = (*sc->sc_start)(sc, m);
1198 		if (error == 0)
1199 			ifp->if_opackets++;
1200 		else
1201 			ifp->if_oerrors++;
1202 	}
1203 	LAGG_RUNLOCK(sc);
1204 }
1205 
1206 static struct mbuf *
1207 lagg_input(struct ifnet *ifp, struct mbuf *m)
1208 {
1209 	struct lagg_port *lp = ifp->if_lagg;
1210 	struct lagg_softc *sc = lp->lp_softc;
1211 	struct ifnet *scifp = sc->sc_ifp;
1212 
1213 	if ((scifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1214 	    (lp->lp_flags & LAGG_PORT_DISABLED) ||
1215 	    sc->sc_proto == LAGG_PROTO_NONE) {
1216 		m_freem(m);
1217 		return (NULL);
1218 	}
1219 
1220 	LAGG_RLOCK(sc);
1221 	ETHER_BPF_MTAP(scifp, m);
1222 
1223 	m = (*sc->sc_input)(sc, lp, m);
1224 
1225 	if (m != NULL) {
1226 		scifp->if_ipackets++;
1227 		scifp->if_ibytes += m->m_pkthdr.len;
1228 
1229 		if (scifp->if_flags & IFF_MONITOR) {
1230 			m_freem(m);
1231 			m = NULL;
1232 		}
1233 	}
1234 
1235 	LAGG_RUNLOCK(sc);
1236 	return (m);
1237 }
1238 
1239 static int
1240 lagg_media_change(struct ifnet *ifp)
1241 {
1242 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1243 
1244 	if (sc->sc_ifflags & IFF_DEBUG)
1245 		printf("%s\n", __func__);
1246 
1247 	/* Ignore */
1248 	return (0);
1249 }
1250 
1251 static void
1252 lagg_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1253 {
1254 	struct lagg_softc *sc = (struct lagg_softc *)ifp->if_softc;
1255 	struct lagg_port *lp;
1256 
1257 	imr->ifm_status = IFM_AVALID;
1258 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
1259 
1260 	LAGG_RLOCK(sc);
1261 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1262 		if (LAGG_PORTACTIVE(lp))
1263 			imr->ifm_status |= IFM_ACTIVE;
1264 	}
1265 	LAGG_RUNLOCK(sc);
1266 }
1267 
1268 static void
1269 lagg_linkstate(struct lagg_softc *sc)
1270 {
1271 	struct lagg_port *lp;
1272 	int new_link = LINK_STATE_DOWN;
1273 	uint64_t speed;
1274 
1275 	/* Our link is considered up if at least one of our ports is active */
1276 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries) {
1277 		if (lp->lp_link_state == LINK_STATE_UP) {
1278 			new_link = LINK_STATE_UP;
1279 			break;
1280 		}
1281 	}
1282 	if_link_state_change(sc->sc_ifp, new_link);
1283 
1284 	/* Update if_baudrate to reflect the max possible speed */
1285 	switch (sc->sc_proto) {
1286 		case LAGG_PROTO_FAILOVER:
1287 			sc->sc_ifp->if_baudrate = sc->sc_primary != NULL ?
1288 			    sc->sc_primary->lp_ifp->if_baudrate : 0;
1289 			break;
1290 		case LAGG_PROTO_ROUNDROBIN:
1291 		case LAGG_PROTO_LOADBALANCE:
1292 		case LAGG_PROTO_ETHERCHANNEL:
1293 			speed = 0;
1294 			SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1295 				speed += lp->lp_ifp->if_baudrate;
1296 			sc->sc_ifp->if_baudrate = speed;
1297 			break;
1298 		case LAGG_PROTO_LACP:
1299 			/* LACP updates if_baudrate itself */
1300 			break;
1301 	}
1302 }
1303 
1304 static void
1305 lagg_port_state(struct ifnet *ifp, int state)
1306 {
1307 	struct lagg_port *lp = (struct lagg_port *)ifp->if_lagg;
1308 	struct lagg_softc *sc = NULL;
1309 
1310 	if (lp != NULL)
1311 		sc = lp->lp_softc;
1312 	if (sc == NULL)
1313 		return;
1314 
1315 	LAGG_WLOCK(sc);
1316 	lagg_linkstate(sc);
1317 	if (sc->sc_linkstate != NULL)
1318 		(*sc->sc_linkstate)(lp);
1319 	LAGG_WUNLOCK(sc);
1320 }
1321 
1322 struct lagg_port *
1323 lagg_link_active(struct lagg_softc *sc, struct lagg_port *lp)
1324 {
1325 	struct lagg_port *lp_next, *rval = NULL;
1326 	// int new_link = LINK_STATE_DOWN;
1327 
1328 	LAGG_RLOCK_ASSERT(sc);
1329 	/*
1330 	 * Search a port which reports an active link state.
1331 	 */
1332 
1333 	if (lp == NULL)
1334 		goto search;
1335 	if (LAGG_PORTACTIVE(lp)) {
1336 		rval = lp;
1337 		goto found;
1338 	}
1339 	if ((lp_next = SLIST_NEXT(lp, lp_entries)) != NULL &&
1340 	    LAGG_PORTACTIVE(lp_next)) {
1341 		rval = lp_next;
1342 		goto found;
1343 	}
1344 
1345 search:
1346 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1347 		if (LAGG_PORTACTIVE(lp_next)) {
1348 			rval = lp_next;
1349 			goto found;
1350 		}
1351 	}
1352 
1353 found:
1354 	if (rval != NULL) {
1355 		/*
1356 		 * The IEEE 802.1D standard assumes that a lagg with
1357 		 * multiple ports is always full duplex. This is valid
1358 		 * for load sharing laggs and if at least two links
1359 		 * are active. Unfortunately, checking the latter would
1360 		 * be too expensive at this point.
1361 		 XXX
1362 		if ((sc->sc_capabilities & IFCAP_LAGG_FULLDUPLEX) &&
1363 		    (sc->sc_count > 1))
1364 			new_link = LINK_STATE_FULL_DUPLEX;
1365 		else
1366 			new_link = rval->lp_link_state;
1367 		 */
1368 	}
1369 
1370 	return (rval);
1371 }
1372 
1373 static const void *
1374 lagg_gethdr(struct mbuf *m, u_int off, u_int len, void *buf)
1375 {
1376 	if (m->m_pkthdr.len < (off + len)) {
1377 		return (NULL);
1378 	} else if (m->m_len < (off + len)) {
1379 		m_copydata(m, off, len, buf);
1380 		return (buf);
1381 	}
1382 	return (mtod(m, char *) + off);
1383 }
1384 
1385 uint32_t
1386 lagg_hashmbuf(struct mbuf *m, uint32_t key)
1387 {
1388 	uint16_t etype;
1389 	uint32_t p = 0;
1390 	int off;
1391 	struct ether_header *eh;
1392 	struct ether_vlan_header vlanbuf;
1393 	const struct ether_vlan_header *vlan;
1394 #ifdef INET
1395 	const struct ip *ip;
1396 	struct ip ipbuf;
1397 #endif
1398 #ifdef INET6
1399 	const struct ip6_hdr *ip6;
1400 	struct ip6_hdr ip6buf;
1401 	uint32_t flow;
1402 #endif
1403 
1404 	off = sizeof(*eh);
1405 	if (m->m_len < off)
1406 		goto out;
1407 	eh = mtod(m, struct ether_header *);
1408 	etype = ntohs(eh->ether_type);
1409 	p = hash32_buf(&eh->ether_shost, ETHER_ADDR_LEN, key);
1410 	p = hash32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
1411 
1412 	/* Special handling for encapsulating VLAN frames */
1413 	if (m->m_flags & M_VLANTAG) {
1414 		p = hash32_buf(&m->m_pkthdr.ether_vtag,
1415 		    sizeof(m->m_pkthdr.ether_vtag), p);
1416 	} else if (etype == ETHERTYPE_VLAN) {
1417 		vlan = lagg_gethdr(m, off,  sizeof(*vlan), &vlanbuf);
1418 		if (vlan == NULL)
1419 			goto out;
1420 
1421 		p = hash32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
1422 		etype = ntohs(vlan->evl_proto);
1423 		off += sizeof(*vlan) - sizeof(*eh);
1424 	}
1425 
1426 	switch (etype) {
1427 #ifdef INET
1428 	case ETHERTYPE_IP:
1429 		ip = lagg_gethdr(m, off, sizeof(*ip), &ipbuf);
1430 		if (ip == NULL)
1431 			goto out;
1432 
1433 		p = hash32_buf(&ip->ip_src, sizeof(struct in_addr), p);
1434 		p = hash32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
1435 		break;
1436 #endif
1437 #ifdef INET6
1438 	case ETHERTYPE_IPV6:
1439 		ip6 = lagg_gethdr(m, off, sizeof(*ip6), &ip6buf);
1440 		if (ip6 == NULL)
1441 			goto out;
1442 
1443 		p = hash32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
1444 		p = hash32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
1445 		flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
1446 		p = hash32_buf(&flow, sizeof(flow), p);	/* IPv6 flow label */
1447 		break;
1448 #endif
1449 	}
1450 out:
1451 	return (p);
1452 }
1453 
1454 int
1455 lagg_enqueue(struct ifnet *ifp, struct mbuf *m)
1456 {
1457 
1458 	return (ifp->if_transmit)(ifp, m);
1459 }
1460 
1461 /*
1462  * Simple round robin aggregation
1463  */
1464 
1465 static int
1466 lagg_rr_attach(struct lagg_softc *sc)
1467 {
1468 	sc->sc_detach = lagg_rr_detach;
1469 	sc->sc_start = lagg_rr_start;
1470 	sc->sc_input = lagg_rr_input;
1471 	sc->sc_port_create = NULL;
1472 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1473 	sc->sc_seq = 0;
1474 
1475 	return (0);
1476 }
1477 
1478 static int
1479 lagg_rr_detach(struct lagg_softc *sc)
1480 {
1481 	return (0);
1482 }
1483 
1484 static int
1485 lagg_rr_start(struct lagg_softc *sc, struct mbuf *m)
1486 {
1487 	struct lagg_port *lp;
1488 	uint32_t p;
1489 
1490 	p = atomic_fetchadd_32(&sc->sc_seq, 1);
1491 	p %= sc->sc_count;
1492 	lp = SLIST_FIRST(&sc->sc_ports);
1493 	while (p--)
1494 		lp = SLIST_NEXT(lp, lp_entries);
1495 
1496 	/*
1497 	 * Check the port's link state. This will return the next active
1498 	 * port if the link is down or the port is NULL.
1499 	 */
1500 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1501 		m_freem(m);
1502 		return (ENOENT);
1503 	}
1504 
1505 	/* Send mbuf */
1506 	return (lagg_enqueue(lp->lp_ifp, m));
1507 }
1508 
1509 static struct mbuf *
1510 lagg_rr_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1511 {
1512 	struct ifnet *ifp = sc->sc_ifp;
1513 
1514 	/* Just pass in the packet to our lagg device */
1515 	m->m_pkthdr.rcvif = ifp;
1516 
1517 	return (m);
1518 }
1519 
1520 /*
1521  * Active failover
1522  */
1523 
1524 static int
1525 lagg_fail_attach(struct lagg_softc *sc)
1526 {
1527 	sc->sc_detach = lagg_fail_detach;
1528 	sc->sc_start = lagg_fail_start;
1529 	sc->sc_input = lagg_fail_input;
1530 	sc->sc_port_create = NULL;
1531 	sc->sc_port_destroy = NULL;
1532 
1533 	return (0);
1534 }
1535 
1536 static int
1537 lagg_fail_detach(struct lagg_softc *sc)
1538 {
1539 	return (0);
1540 }
1541 
1542 static int
1543 lagg_fail_start(struct lagg_softc *sc, struct mbuf *m)
1544 {
1545 	struct lagg_port *lp;
1546 
1547 	/* Use the master port if active or the next available port */
1548 	if ((lp = lagg_link_active(sc, sc->sc_primary)) == NULL) {
1549 		m_freem(m);
1550 		return (ENOENT);
1551 	}
1552 
1553 	/* Send mbuf */
1554 	return (lagg_enqueue(lp->lp_ifp, m));
1555 }
1556 
1557 static struct mbuf *
1558 lagg_fail_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1559 {
1560 	struct ifnet *ifp = sc->sc_ifp;
1561 	struct lagg_port *tmp_tp;
1562 
1563 	if (lp == sc->sc_primary) {
1564 		m->m_pkthdr.rcvif = ifp;
1565 		return (m);
1566 	}
1567 
1568 	if (!LAGG_PORTACTIVE(sc->sc_primary)) {
1569 		tmp_tp = lagg_link_active(sc, sc->sc_primary);
1570 		/*
1571 		 * If tmp_tp is null, we've recieved a packet when all
1572 		 * our links are down. Weird, but process it anyways.
1573 		 */
1574 		if ((tmp_tp == NULL || tmp_tp == lp)) {
1575 			m->m_pkthdr.rcvif = ifp;
1576 			return (m);
1577 		}
1578 	}
1579 
1580 	m_freem(m);
1581 	return (NULL);
1582 }
1583 
1584 /*
1585  * Loadbalancing
1586  */
1587 
1588 static int
1589 lagg_lb_attach(struct lagg_softc *sc)
1590 {
1591 	struct lagg_port *lp;
1592 	struct lagg_lb *lb;
1593 
1594 	if ((lb = (struct lagg_lb *)malloc(sizeof(struct lagg_lb),
1595 	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
1596 		return (ENOMEM);
1597 
1598 	sc->sc_detach = lagg_lb_detach;
1599 	sc->sc_start = lagg_lb_start;
1600 	sc->sc_input = lagg_lb_input;
1601 	sc->sc_port_create = lagg_lb_port_create;
1602 	sc->sc_port_destroy = lagg_lb_port_destroy;
1603 	sc->sc_capabilities = IFCAP_LAGG_FULLDUPLEX;
1604 
1605 	lb->lb_key = arc4random();
1606 	sc->sc_psc = (caddr_t)lb;
1607 
1608 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1609 		lagg_lb_port_create(lp);
1610 
1611 	return (0);
1612 }
1613 
1614 static int
1615 lagg_lb_detach(struct lagg_softc *sc)
1616 {
1617 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1618 	if (lb != NULL)
1619 		free(lb, M_DEVBUF);
1620 	return (0);
1621 }
1622 
1623 static int
1624 lagg_lb_porttable(struct lagg_softc *sc, struct lagg_port *lp)
1625 {
1626 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1627 	struct lagg_port *lp_next;
1628 	int i = 0;
1629 
1630 	bzero(&lb->lb_ports, sizeof(lb->lb_ports));
1631 	SLIST_FOREACH(lp_next, &sc->sc_ports, lp_entries) {
1632 		if (lp_next == lp)
1633 			continue;
1634 		if (i >= LAGG_MAX_PORTS)
1635 			return (EINVAL);
1636 		if (sc->sc_ifflags & IFF_DEBUG)
1637 			printf("%s: port %s at index %d\n",
1638 			    sc->sc_ifname, lp_next->lp_ifname, i);
1639 		lb->lb_ports[i++] = lp_next;
1640 	}
1641 
1642 	return (0);
1643 }
1644 
1645 static int
1646 lagg_lb_port_create(struct lagg_port *lp)
1647 {
1648 	struct lagg_softc *sc = lp->lp_softc;
1649 	return (lagg_lb_porttable(sc, NULL));
1650 }
1651 
1652 static void
1653 lagg_lb_port_destroy(struct lagg_port *lp)
1654 {
1655 	struct lagg_softc *sc = lp->lp_softc;
1656 	lagg_lb_porttable(sc, lp);
1657 }
1658 
1659 static int
1660 lagg_lb_start(struct lagg_softc *sc, struct mbuf *m)
1661 {
1662 	struct lagg_lb *lb = (struct lagg_lb *)sc->sc_psc;
1663 	struct lagg_port *lp = NULL;
1664 	uint32_t p = 0;
1665 
1666 	if (m->m_flags & M_FLOWID)
1667 		p = m->m_pkthdr.flowid;
1668 	else
1669 		p = lagg_hashmbuf(m, lb->lb_key);
1670 	p %= sc->sc_count;
1671 	lp = lb->lb_ports[p];
1672 
1673 	/*
1674 	 * Check the port's link state. This will return the next active
1675 	 * port if the link is down or the port is NULL.
1676 	 */
1677 	if ((lp = lagg_link_active(sc, lp)) == NULL) {
1678 		m_freem(m);
1679 		return (ENOENT);
1680 	}
1681 
1682 	/* Send mbuf */
1683 	return (lagg_enqueue(lp->lp_ifp, m));
1684 }
1685 
1686 static struct mbuf *
1687 lagg_lb_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1688 {
1689 	struct ifnet *ifp = sc->sc_ifp;
1690 
1691 	/* Just pass in the packet to our lagg device */
1692 	m->m_pkthdr.rcvif = ifp;
1693 
1694 	return (m);
1695 }
1696 
1697 /*
1698  * 802.3ad LACP
1699  */
1700 
1701 static int
1702 lagg_lacp_attach(struct lagg_softc *sc)
1703 {
1704 	struct lagg_port *lp;
1705 	int error;
1706 
1707 	sc->sc_detach = lagg_lacp_detach;
1708 	sc->sc_port_create = lacp_port_create;
1709 	sc->sc_port_destroy = lacp_port_destroy;
1710 	sc->sc_linkstate = lacp_linkstate;
1711 	sc->sc_start = lagg_lacp_start;
1712 	sc->sc_input = lagg_lacp_input;
1713 	sc->sc_init = lacp_init;
1714 	sc->sc_stop = lacp_stop;
1715 	sc->sc_lladdr = lagg_lacp_lladdr;
1716 	sc->sc_req = lacp_req;
1717 	sc->sc_portreq = lacp_portreq;
1718 
1719 	error = lacp_attach(sc);
1720 	if (error)
1721 		return (error);
1722 
1723 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1724 		lacp_port_create(lp);
1725 
1726 	return (error);
1727 }
1728 
1729 static int
1730 lagg_lacp_detach(struct lagg_softc *sc)
1731 {
1732 	struct lagg_port *lp;
1733 	int error;
1734 
1735 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1736 		lacp_port_destroy(lp);
1737 
1738 	/* unlocking is safe here */
1739 	LAGG_WUNLOCK(sc);
1740 	error = lacp_detach(sc);
1741 	LAGG_WLOCK(sc);
1742 
1743 	return (error);
1744 }
1745 
1746 static void
1747 lagg_lacp_lladdr(struct lagg_softc *sc)
1748 {
1749 	struct lagg_port *lp;
1750 
1751 	/* purge all the lacp ports */
1752 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1753 		lacp_port_destroy(lp);
1754 
1755 	/* add them back in */
1756 	SLIST_FOREACH(lp, &sc->sc_ports, lp_entries)
1757 		lacp_port_create(lp);
1758 }
1759 
1760 static int
1761 lagg_lacp_start(struct lagg_softc *sc, struct mbuf *m)
1762 {
1763 	struct lagg_port *lp;
1764 
1765 	lp = lacp_select_tx_port(sc, m);
1766 	if (lp == NULL) {
1767 		m_freem(m);
1768 		return (EBUSY);
1769 	}
1770 
1771 	/* Send mbuf */
1772 	return (lagg_enqueue(lp->lp_ifp, m));
1773 }
1774 
1775 static struct mbuf *
1776 lagg_lacp_input(struct lagg_softc *sc, struct lagg_port *lp, struct mbuf *m)
1777 {
1778 	struct ifnet *ifp = sc->sc_ifp;
1779 	struct ether_header *eh;
1780 	u_short etype;
1781 
1782 	eh = mtod(m, struct ether_header *);
1783 	etype = ntohs(eh->ether_type);
1784 
1785 	/* Tap off LACP control messages */
1786 	if (etype == ETHERTYPE_SLOW) {
1787 		m = lacp_input(lp, m);
1788 		if (m == NULL)
1789 			return (NULL);
1790 	}
1791 
1792 	/*
1793 	 * If the port is not collecting or not in the active aggregator then
1794 	 * free and return.
1795 	 */
1796 	if (lacp_iscollecting(lp) == 0 || lacp_isactive(lp) == 0) {
1797 		m_freem(m);
1798 		return (NULL);
1799 	}
1800 
1801 	m->m_pkthdr.rcvif = ifp;
1802 	return (m);
1803 }
1804