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