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