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