Unify the input handling of the low-level network stack by introducing
[dragonfly.git] / sys / netgraph / fec / ng_fec.c
1 /*
2  * ng_fec.c
3  *
4  * Copyright (c) 2001 Berkeley Software Design, Inc.
5  * Copyright (c) 2000, 2001
6  *      Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Bill Paul.
19  * 4. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.1.2.1 2002/11/01 21:39:31 julian Exp $
36  * $DragonFly: src/sys/netgraph/fec/ng_fec.c,v 1.8 2004/07/23 07:16:31 joerg Exp $
37  */
38 /*
39  * Copyright (c) 1996-1999 Whistle Communications, Inc.
40  * All rights reserved.
41  * 
42  * Subject to the following obligations and disclaimer of warranty, use and
43  * redistribution of this software, in source or object code forms, with or
44  * without modifications are expressly permitted by Whistle Communications;
45  * provided, however, that:
46  * 1. Any and all reproductions of the source or object code must include the
47  *    copyright notice above and the following disclaimer of warranties; and
48  * 2. No rights are granted, in any manner or form, to use Whistle
49  *    Communications, Inc. trademarks, including the mark "WHISTLE
50  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
51  *    such appears in the above copyright notice or in the software.
52  * 
53  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
54  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
55  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
56  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
57  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
58  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
59  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
60  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
61  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
62  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
63  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
64  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
65  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
69  * OF SUCH DAMAGE.
70  *
71  * Author: Archie Cobbs <archie@freebsd.org>
72  *
73  * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
74  */
75
76 /*
77  * This module implements ethernet channel bonding using the Cisco
78  * Fast EtherChannel mechanism. Two or four ports may be combined
79  * into a single aggregate interface.
80  *
81  * Interfaces are named fec0, fec1, etc.  New nodes take the
82  * first available interface name.
83  *
84  * This node also includes Berkeley packet filter support.
85  *
86  * Note that this node doesn't need to connect to any other
87  * netgraph nodes in order to do its work.
88  */
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/errno.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 #include <sys/mbuf.h>
96 #include <sys/errno.h>
97 #include <sys/sockio.h>
98 #include <sys/socket.h>
99 #include <sys/syslog.h>
100 #include <sys/libkern.h>
101 #include <sys/queue.h>
102
103 #include <net/if.h>
104 #include <net/if_types.h>
105 #include <net/if_arp.h>
106 #include <net/if_dl.h>
107 #include <net/if_media.h>
108 #include <net/intrq.h>
109 #include <net/bpf.h>
110 #include <net/ethernet.h>
111
112 #include "opt_inet.h"
113 #include "opt_inet6.h"
114
115 #include <netinet/in.h>
116 #ifdef INET
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #endif
120
121 #ifdef INET6
122 #include <netinet/ip6.h>
123 #endif
124
125 #include <netgraph/ng_message.h>
126 #include <netgraph/netgraph.h>
127 #include <netgraph/ng_parse.h>
128 #include "ng_fec.h"
129
130 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
131 #define FEC_INC(x, y)   (x) = (x + 1) % y
132
133 /*
134  * Current fast etherchannel implementations use either 2 or 4
135  * ports, so for now we limit the maximum bundle size to 4 interfaces.
136  */
137 #define FEC_BUNDLESIZ   4
138
139 struct ng_fec_portlist {
140         struct ifnet            *fec_if;
141         int                     fec_idx;
142         int                     fec_ifstat;
143         struct ether_addr       fec_mac;
144         TAILQ_ENTRY(ng_fec_portlist) fec_list;
145 };
146
147 struct ng_fec_bundle {
148         TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
149         int                     fec_ifcnt;
150         int                     fec_btype;
151 };
152
153 #define FEC_BTYPE_MAC           0x01
154 #define FEC_BTYPE_INET          0x02
155 #define FEC_BTYPE_INET6         0x03
156
157 /* Node private data */
158 struct ng_fec_private {
159         struct arpcom arpcom;
160         struct ifmedia ifmedia;
161         int     if_flags;
162         int     if_error;               /* XXX */
163         int     unit;                   /* Interface unit number */
164         node_p  node;                   /* Our netgraph node */
165         struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
166         struct callout_handle fec_ch;   /* callout handle for ticker */
167         int     (*real_if_output)(struct ifnet *, struct mbuf *,
168                                   struct sockaddr *, struct rtentry *);
169 };
170 typedef struct ng_fec_private *priv_p;
171
172 /* Interface methods */
173 static void     ng_fec_input(struct ifnet *, struct mbuf **,
174                         struct ether_header *);
175 static void     ng_fec_start(struct ifnet *ifp);
176 static int      ng_fec_choose_port(struct ng_fec_bundle *b,
177                         struct mbuf *m, struct ifnet **ifp);
178 static int      ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
179 static void     ng_fec_init(void *arg);
180 static void     ng_fec_stop(struct ifnet *ifp);
181 static int      ng_fec_ifmedia_upd(struct ifnet *ifp);
182 static void     ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
183 static int      ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
184                              struct ucred *);
185 static int      ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
186                         struct sockaddr *dst, struct rtentry *rt0);
187 static void     ng_fec_tick(void *arg);
188 static int      ng_fec_addport(struct ng_fec_private *priv, char *iface);
189 static int      ng_fec_delport(struct ng_fec_private *priv, char *iface);
190
191 #ifdef DEBUG
192 static void     ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
193 #endif
194
195 /* Netgraph methods */
196 static ng_constructor_t ng_fec_constructor;
197 static ng_rcvmsg_t      ng_fec_rcvmsg;
198 static ng_shutdown_t    ng_fec_rmnode;
199
200 /* List of commands and how to convert arguments to/from ASCII */
201 static const struct ng_cmdlist ng_fec_cmds[] = {
202         {
203           NGM_FEC_COOKIE,
204           NGM_FEC_ADD_IFACE,
205           "add_iface",
206           &ng_parse_string_type,
207           NULL,
208         },
209         {
210           NGM_FEC_COOKIE,
211           NGM_FEC_DEL_IFACE,
212           "del_iface",
213           &ng_parse_string_type,
214           NULL,
215         },
216         {
217           NGM_FEC_COOKIE,
218           NGM_FEC_SET_MODE_MAC,
219           "set_mode_mac",
220           NULL,
221           NULL,
222         },
223         {
224           NGM_FEC_COOKIE,
225           NGM_FEC_SET_MODE_INET,
226           "set_mode_inet",
227           NULL,
228           NULL,
229         },
230         { 0 }
231 };
232
233 /* Node type descriptor */
234 static struct ng_type typestruct = {
235         NG_VERSION,
236         NG_FEC_NODE_TYPE,
237         NULL,
238         ng_fec_constructor,
239         ng_fec_rcvmsg,
240         ng_fec_rmnode,
241         NULL,
242         NULL,
243         NULL,
244         NULL,
245         NULL,
246         NULL,
247         ng_fec_cmds
248 };
249 NETGRAPH_INIT(fec, &typestruct);
250
251 /* We keep a bitmap indicating which unit numbers are free.
252    One means the unit number is free, zero means it's taken. */
253 static int      *ng_fec_units = NULL;
254 static int      ng_fec_units_len = 0;
255 static int      ng_units_in_use = 0;
256
257 #define UNITS_BITSPERWORD       (sizeof(*ng_fec_units) * NBBY)
258
259 /*
260  * Find the first free unit number for a new interface.
261  * Increase the size of the unit bitmap as necessary.
262  */
263 static __inline__ int
264 ng_fec_get_unit(int *unit)
265 {
266         int index, bit;
267
268         for (index = 0; index < ng_fec_units_len
269             && ng_fec_units[index] == 0; index++);
270         if (index == ng_fec_units_len) {                /* extend array */
271                 int i, *newarray, newlen;
272
273                 newlen = (2 * ng_fec_units_len) + 4;
274                 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
275                     M_NETGRAPH, M_NOWAIT);
276                 if (newarray == NULL)
277                         return (ENOMEM);
278                 bcopy(ng_fec_units, newarray,
279                     ng_fec_units_len * sizeof(*ng_fec_units));
280                 for (i = ng_fec_units_len; i < newlen; i++)
281                         newarray[i] = ~0;
282                 if (ng_fec_units != NULL)
283                         FREE(ng_fec_units, M_NETGRAPH);
284                 ng_fec_units = newarray;
285                 ng_fec_units_len = newlen;
286         }
287         bit = ffs(ng_fec_units[index]) - 1;
288         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
289             ("%s: word=%d bit=%d", __FUNCTION__, ng_fec_units[index], bit));
290         ng_fec_units[index] &= ~(1 << bit);
291         *unit = (index * UNITS_BITSPERWORD) + bit;
292         ng_units_in_use++;
293         return (0);
294 }
295
296 /*
297  * Free a no longer needed unit number.
298  */
299 static __inline__ void
300 ng_fec_free_unit(int unit)
301 {
302         int index, bit;
303
304         index = unit / UNITS_BITSPERWORD;
305         bit = unit % UNITS_BITSPERWORD;
306         KASSERT(index < ng_fec_units_len,
307             ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_fec_units_len));
308         KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
309             ("%s: unit=%d is free", __FUNCTION__, unit));
310         ng_fec_units[index] |= (1 << bit);
311         /*
312          * XXX We could think about reducing the size of ng_fec_units[]
313          * XXX here if the last portion is all ones
314          * XXX At least free it if no more units.
315          * Needed if we are eventually be able to unload.
316          */
317         ng_units_in_use++;
318         if (ng_units_in_use == 0) { /* XXX make SMP safe */
319                 FREE(ng_fec_units, M_NETGRAPH);
320                 ng_fec_units_len = 0;
321                 ng_fec_units = NULL;
322         }
323 }
324
325 /************************************************************************
326                         INTERFACE STUFF
327  ************************************************************************/
328
329 static int
330 ng_fec_addport(struct ng_fec_private *priv, char *iface)
331 {
332         struct ng_fec_bundle    *b;
333         struct ifnet            *ifp, *bifp;
334         struct arpcom           *ac;
335         struct ifaddr           *ifa;
336         struct sockaddr_dl      *sdl;
337         struct ng_fec_portlist  *p, *new;
338
339         if (priv == NULL || iface == NULL)
340                 return(EINVAL);
341
342         b = &priv->fec_bundle;
343         ifp = &priv->arpcom.ac_if;
344
345         /* Find the interface */
346         bifp = ifunit(iface);
347         if (bifp == NULL) {
348                 printf("fec%d: tried to add iface %s, which "
349                     "doesn't seem to exist\n", priv->unit, iface);
350                 return(ENOENT);
351         }
352
353         /* See if we have room in the bundle */
354         if (b->fec_ifcnt == FEC_BUNDLESIZ) {
355                 printf("fec%d: can't add new iface; bundle is full\n",
356                     priv->unit);
357                 return(ENOSPC);
358         }
359
360         /* See if the interface is already in the bundle */
361         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
362                 if (p->fec_if == bifp) {
363                         printf("fec%d: iface %s is already in this "
364                             "bundle\n", priv->unit, iface);
365                         return(EINVAL);
366                 }
367         }
368
369         /* Allocate new list entry. */
370         MALLOC(new, struct ng_fec_portlist *,
371             sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
372         if (new == NULL)
373                 return(ENOMEM);
374
375         ac = (struct arpcom *)bifp;
376         ac->ac_netgraph = priv->node;
377
378         /*
379          * If this is the first interface added to the bundle,
380          * use its MAC address for the virtual interface (and,
381          * by extension, all the other ports in the bundle).
382          */
383         if (b->fec_ifcnt == 0) {
384                 ifa = ifnet_addrs[ifp->if_index - 1];
385                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
386                 bcopy((char *)ac->ac_enaddr,
387                     priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
388                 bcopy((char *)ac->ac_enaddr,
389                     LLADDR(sdl), ETHER_ADDR_LEN);
390         }
391
392         b->fec_btype = FEC_BTYPE_MAC;
393         new->fec_idx = b->fec_ifcnt;
394         b->fec_ifcnt++;
395
396         /* Save the real MAC address. */
397         bcopy((char *)ac->ac_enaddr,
398             (char *)&new->fec_mac, ETHER_ADDR_LEN);
399
400         /* Set up phony MAC address. */
401         ifa = ifnet_addrs[bifp->if_index - 1];
402         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
403         bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
404         bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
405
406         /* Add to the queue */
407         new->fec_if = bifp;
408         TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
409
410         return(0);
411 }
412
413 static int
414 ng_fec_delport(struct ng_fec_private *priv, char *iface)
415 {
416         struct ng_fec_bundle    *b;
417         struct ifnet            *ifp, *bifp;
418         struct arpcom           *ac;
419         struct ifaddr           *ifa;
420         struct sockaddr_dl      *sdl;
421         struct ng_fec_portlist  *p;
422
423         if (priv == NULL || iface == NULL)
424                 return(EINVAL);
425
426         b = &priv->fec_bundle;
427         ifp = &priv->arpcom.ac_if;
428
429         /* Find the interface */
430         bifp = ifunit(iface);
431         if (bifp == NULL) {
432                 printf("fec%d: tried to remove iface %s, which "
433                     "doesn't seem to exist\n", priv->unit, iface);
434                 return(ENOENT);
435         }
436
437         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
438                 if (p->fec_if == bifp)
439                         break;
440         }
441
442         if (p == NULL) {
443                 printf("fec%d: tried to remove iface %s which "
444                     "is not in our bundle\n", priv->unit, iface);
445                 return(EINVAL);
446         }
447
448         /* Stop interface */
449         bifp->if_flags &= ~IFF_UP;
450         (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
451
452         /* Restore MAC address. */
453         ac = (struct arpcom *)bifp;
454         ifa = ifnet_addrs[bifp->if_index - 1];
455         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
456         bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
457         bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
458
459         /* Delete port */
460         TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
461         FREE(p, M_NETGRAPH);
462         b->fec_ifcnt--;
463
464         return(0);
465 }
466
467 /*
468  * Pass an ioctl command down to all the underyling interfaces in a
469  * bundle. Used for setting multicast filters and flags.
470  */
471
472 static int 
473 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
474 {
475         struct ng_fec_private   *priv;
476         struct ng_fec_bundle    *b;
477         struct ifnet            *oifp;
478         struct ng_fec_portlist  *p;
479
480         priv = ifp->if_softc;
481         b = &priv->fec_bundle;
482
483         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
484                 oifp = p->fec_if;
485                 if (oifp != NULL)
486                         (*oifp->if_ioctl)(oifp, command, data, NULL);
487         }
488
489         return(0);
490 }
491
492 static void
493 ng_fec_init(void *arg)
494 {
495         struct ng_fec_private   *priv;
496         struct ng_fec_bundle    *b;
497         struct ifnet            *ifp, *bifp;
498         struct ng_fec_portlist  *p;
499
500         ifp = arg;
501         priv = ifp->if_softc;
502         b = &priv->fec_bundle;
503
504         if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
505                 printf("fec%d: invalid bundle "
506                     "size: %d\n", priv->unit,
507                     b->fec_ifcnt);
508                 return;
509         }
510
511         ng_fec_stop(ifp);
512
513         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
514                 bifp = p->fec_if;
515                 bifp->if_flags |= IFF_UP;
516                 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
517                 /* mark iface as up and let the monitor check it */
518                 p->fec_ifstat = -1;
519         }
520
521         priv->fec_ch = timeout(ng_fec_tick, priv, hz);
522
523         return;
524 }
525
526 static void
527 ng_fec_stop(struct ifnet *ifp)
528 {
529         struct ng_fec_private   *priv;
530         struct ng_fec_bundle    *b;
531         struct ifnet            *bifp;
532         struct ng_fec_portlist  *p;
533
534         priv = ifp->if_softc;
535         b = &priv->fec_bundle;
536
537         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
538                 bifp = p->fec_if;
539                 bifp->if_flags &= ~IFF_UP;
540                 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
541         }
542
543         untimeout(ng_fec_tick, priv, priv->fec_ch);
544
545         return;
546 }
547
548 static void
549 ng_fec_tick(void *arg)
550 {
551         struct ng_fec_private   *priv;
552         struct ng_fec_bundle    *b;
553         struct ifmediareq       ifmr;
554         struct ifnet            *ifp;
555         struct ng_fec_portlist  *p;
556         int                     error = 0;
557
558         priv = arg;
559         b = &priv->fec_bundle;
560
561         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
562                 bzero((char *)&ifmr, sizeof(ifmr));
563                 ifp = p->fec_if;
564                 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr,
565                                          NULL);
566                 if (error) {
567                         printf("fec%d: failed to check status "
568                             "of link %s\n", priv->unit, ifp->if_xname);
569                         continue;
570                 }
571
572                 if (ifmr.ifm_status & IFM_AVALID &&
573                     IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
574                         if (ifmr.ifm_status & IFM_ACTIVE) {
575                                 if (p->fec_ifstat == -1 ||
576                                     p->fec_ifstat == 0) {
577                                         p->fec_ifstat = 1;
578                                         printf("fec%d: port %s in bundle "
579                                             "is up\n", priv->unit,
580                                             ifp->if_xname);
581                                 }
582                         } else {
583                                 if (p->fec_ifstat == -1 ||
584                                     p->fec_ifstat == 1) {
585                                         p->fec_ifstat = 0;
586                                         printf("fec%d: port %s in bundle "
587                                             "is down\n", priv->unit,
588                                             ifp->if_xname);
589                                 }
590                         }
591                 }
592         }
593
594         ifp = &priv->arpcom.ac_if;
595         if (ifp->if_flags & IFF_RUNNING)
596                 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
597
598         return;
599 }
600
601 static int
602 ng_fec_ifmedia_upd(struct ifnet *ifp)
603 {
604         return(0);
605 }
606
607 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
608         struct ifmediareq *ifmr)
609 {
610         struct ng_fec_private   *priv;
611         struct ng_fec_bundle    *b;
612         struct ng_fec_portlist  *p;
613
614         priv = ifp->if_softc;
615         b = &priv->fec_bundle;
616
617         ifmr->ifm_status = IFM_AVALID;
618         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
619                 if (p->fec_ifstat) {
620                         ifmr->ifm_status |= IFM_ACTIVE;
621                         break;
622                 }
623         }
624
625         return;
626 }
627
628 /*
629  * Process an ioctl for the virtual interface
630  */
631 static int
632 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
633 {
634         struct ifreq *const ifr = (struct ifreq *) data;
635         int s, error = 0;
636         struct ng_fec_private   *priv;
637         struct ng_fec_bundle    *b;
638
639         priv = ifp->if_softc;
640         b = &priv->fec_bundle;
641
642 #ifdef DEBUG
643         ng_fec_print_ioctl(ifp, command, data);
644 #endif
645         s = splimp();
646         switch (command) {
647
648         /* These two are mostly handled at a higher layer */
649         case SIOCSIFADDR:
650         case SIOCGIFADDR:
651         case SIOCSIFMTU:
652                 error = ether_ioctl(ifp, command, data);
653                 break;
654
655         /* Set flags */
656         case SIOCSIFFLAGS:
657                 /*
658                  * If the interface is marked up and stopped, then start it.
659                  * If it is marked down and running, then stop it.
660                  */
661                 if (ifr->ifr_flags & IFF_UP) {
662                         if (!(ifp->if_flags & IFF_RUNNING)) {
663                                 /* Sanity. */
664                                 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
665                                         printf("fec%d: invalid bundle "
666                                             "size: %d\n", priv->unit,
667                                             b->fec_ifcnt);
668                                         error = EINVAL;
669                                         break;
670                                 }
671                                 ifp->if_flags &= ~(IFF_OACTIVE);
672                                 ifp->if_flags |= IFF_RUNNING;
673                                 ng_fec_init(ifp);
674                         }
675                         /*
676                          * Bubble down changes in promisc mode to
677                          * underlying interfaces.
678                          */
679                         if ((ifp->if_flags & IFF_PROMISC) !=
680                             (priv->if_flags & IFF_PROMISC)) {
681                                 ng_fec_setport(ifp, command, data);
682                                 priv->if_flags = ifp->if_flags;
683                         }
684                 } else {
685                         if (ifp->if_flags & IFF_RUNNING)
686                                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
687                         ng_fec_stop(ifp);
688                 }
689                 break;
690
691         case SIOCADDMULTI:
692         case SIOCDELMULTI:
693                 ng_fec_setport(ifp, command, data);
694                 error = 0;
695                 break;
696         case SIOCGIFMEDIA:
697         case SIOCSIFMEDIA:
698                 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
699                 break;
700         /* Stuff that's not supported */
701         case SIOCSIFPHYS:
702                 error = EOPNOTSUPP;
703                 break;
704
705         default:
706                 error = EINVAL;
707                 break;
708         }
709         (void) splx(s);
710         return (error);
711 }
712
713 /*
714  * This routine spies on mbufs passing through ether_input(). If
715  * they come from one of the interfaces that are aggregated into
716  * our bundle, we fix up the ifnet pointer and increment our
717  * packet counters so that it looks like the frames are actually
718  * coming from us.
719  */
720 static void 
721 ng_fec_input(struct ifnet *ifp, struct mbuf **m0,
722                 struct ether_header *eh)
723 {
724         struct ng_node          *node;
725         struct ng_fec_private   *priv;
726         struct ng_fec_bundle    *b;
727         struct mbuf             *m;
728         struct ifnet            *bifp;
729         struct ng_fec_portlist  *p;
730
731         /* Sanity check */
732         if (ifp == NULL || m0 == NULL || eh == NULL)
733                 return;
734
735         node = IFP2NG(ifp);
736
737         /* Sanity check part II */
738         if (node == NULL)
739                 return;
740
741         priv = node->private;
742         b = &priv->fec_bundle;
743         bifp = &priv->arpcom.ac_if;
744
745         m = *m0;
746         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
747                 if (p->fec_if == m->m_pkthdr.rcvif)
748                         break;
749         }
750
751         /* Wasn't meant for us; leave this frame alone. */
752         if (p == NULL)
753                 return;
754
755         /* Pretend this is our frame. */
756         m->m_pkthdr.rcvif = bifp;
757         bifp->if_ipackets++;
758         bifp->if_ibytes += m->m_pkthdr.len + sizeof(struct ether_header);
759
760         /* Check for a BPF tap */
761         if (bifp->if_bpf != NULL) {
762                 struct m_hdr mh;
763
764                 /* This kludge is OK; BPF treats the "mbuf" as read-only */
765                 mh.mh_next = m;
766                 mh.mh_data = (char *)eh;
767                 mh.mh_len = ETHER_HDR_LEN;
768                 bpf_mtap(bifp, (struct mbuf *)&mh);
769         }
770
771         return;
772 }
773
774 /*
775  * Take a quick peek at the packet and see if it's ok for us to use
776  * the inet or inet6 hash methods on it, if they're enabled. We do
777  * this by setting flags in the mbuf header. Once we've made up our
778  * mind what to do, we pass the frame to ether_output() for further
779  * processing.
780  */
781
782 static int
783 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
784                 struct sockaddr *dst, struct rtentry *rt0)
785 {
786         const priv_p priv = (priv_p) ifp->if_softc;
787         struct ng_fec_bundle *b;
788         int error;
789
790         /* Check interface flags */
791         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
792                 m_freem(m);
793                 return (ENETDOWN);
794         }
795
796         b = &priv->fec_bundle;
797
798         switch (b->fec_btype) {
799         case FEC_BTYPE_MAC:
800                 m->m_flags |= M_FEC_MAC;
801                 break;
802 #ifdef INET
803         case FEC_BTYPE_INET:
804                 /*
805                  * We can't use the INET address port selection
806                  * scheme if this isn't an INET packet.
807                  */
808                 if (dst->sa_family == AF_INET)
809                         m->m_flags |= M_FEC_INET;
810 #ifdef INET6
811                 else if (dst->sa_family == AF_INET6)
812                         m->m_flags |= M_FEC_INET6;
813 #endif
814                 else {
815 #ifdef DEBUG
816                         printf("%s: can't do inet aggregation of non "
817                             "inet packet\n", ifp->if_xname);
818 #endif
819                         m->m_flags |= M_FEC_MAC;
820                 }
821                 break;
822 #endif
823         default:
824                 printf("%s: bogus hash type: %d\n", ifp->if_xname,
825                     b->fec_btype);
826                 m_freem(m);
827                 return(EINVAL);
828                 break;
829         }
830
831         /*
832          * Pass the frame to ether_output() for all the protocol
833          * handling. This will put the ethernet header on the packet
834          * for us.
835          */
836         priv->if_error = 0;
837         error = priv->real_if_output(ifp, m, dst, rt0);
838         if (priv->if_error && !error)
839                 error = priv->if_error;
840
841         return(error);
842 }
843
844 /*
845  * Apply a hash to the source and destination addresses in the packet
846  * in order to select an interface. Also check link status and handle
847  * dead links accordingly.
848  */
849
850 static int
851 ng_fec_choose_port(struct ng_fec_bundle *b,
852         struct mbuf *m, struct ifnet **ifp)
853 {
854         struct ether_header     *eh;
855         struct mbuf             *m0;
856 #ifdef INET
857         struct ip               *ip;
858 #ifdef INET6
859         struct ip6_hdr          *ip6;
860 #endif
861 #endif
862
863         struct ng_fec_portlist  *p;
864         int                     port = 0, mask;
865
866         /*
867          * If there are only two ports, mask off all but the
868          * last bit for XORing. If there are 4, mask off all
869          * but the last 2 bits.
870          */
871         mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
872         eh = mtod(m, struct ether_header *);
873 #ifdef INET
874         ip = (struct ip *)(mtod(m, char *) +
875             sizeof(struct ether_header));
876 #ifdef INET6
877         ip6 = (struct ip6_hdr *)(mtod(m, char *) +
878             sizeof(struct ether_header));
879 #endif
880 #endif
881
882         /*
883          * The fg_fec_output() routine is supposed to leave a
884          * flag for us in the mbuf that tells us what hash to
885          * use, but sometimes a new mbuf is prepended to the
886          * chain, so we have to search every mbuf in the chain
887          * to find the flags.
888          */
889         m0 = m;
890         while (m0) {
891                 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
892                         break;
893                 m0 = m0->m_next;
894         }
895         if (m0 == NULL)
896                 return(EINVAL);
897
898         switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
899         case M_FEC_MAC:
900                 port = (eh->ether_dhost[5] ^
901                     eh->ether_shost[5]) & mask;
902                 break;
903 #ifdef INET
904         case M_FEC_INET:
905                 port = (ntohl(ip->ip_dst.s_addr) ^
906                     ntohl(ip->ip_src.s_addr)) & mask;
907                 break;
908 #ifdef INET6
909         case M_FEC_INET6:
910                 port = (ip6->ip6_dst.s6_addr[15] ^
911                     ip6->ip6_dst.s6_addr[15]) & mask;
912                 break;
913 #endif
914 #endif
915         default:
916                 return(EINVAL);
917                         break;
918         }
919
920         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
921                 if (port == p->fec_idx)
922                         break;
923         }
924
925         /*
926          * Now that we've chosen a port, make sure it's
927          * alive. If it's not alive, cycle through the bundle
928          * looking for a port that is alive. If we don't find
929          * any, return an error.
930          */
931         if (p->fec_ifstat != 1) {
932                 struct ng_fec_portlist  *n = NULL;
933
934                 n = TAILQ_NEXT(p, fec_list);
935                 if (n == NULL)
936                         n = TAILQ_FIRST(&b->ng_fec_ports);
937                 while (n != p) {
938                         if (n->fec_ifstat == 1)
939                                 break;
940                         n = TAILQ_NEXT(n, fec_list);
941                         if (n == NULL)
942                                 n = TAILQ_FIRST(&b->ng_fec_ports);
943                 }
944                 if (n == p)
945                         return(EAGAIN);
946                 p = n;
947         }
948
949         *ifp = p->fec_if;
950
951         return(0);
952 }
953
954 /*
955  * Now that the packet has been run through ether_output(), yank it
956  * off our own send queue and stick it on the queue for the appropriate
957  * underlying physical interface. Note that if the interface's send
958  * queue is full, we save an error status in our private netgraph
959  * space which will eventually be handed up to ng_fec_output(), which
960  * will return it to the rest of the IP stack. We need to do this
961  * in order to duplicate the effect of ether_output() returning ENOBUFS
962  * when it detects that an interface's send queue is full. There's no
963  * other way to signal the error status from here since the if_start()
964  * routine is spec'ed to return void.
965  *
966  * Once the frame is queued, we call ether_output_frame() to initiate
967  * transmission.
968  */
969 static void
970 ng_fec_start(struct ifnet *ifp)
971 {
972         struct ng_fec_private   *priv;
973         struct ng_fec_bundle    *b;
974         struct ifnet            *oifp = NULL;
975         struct mbuf             *m0;
976         int                     error;
977
978         priv = ifp->if_softc;
979         b = &priv->fec_bundle;
980
981         IF_DEQUEUE(&ifp->if_snd, m0);
982         if (m0 == NULL)
983                 return;
984
985         if (ifp->if_bpf)
986                 bpf_mtap(ifp, m0);
987
988         /* Queue up packet on the proper port. */
989         error = ng_fec_choose_port(b, m0, &oifp);
990         if (error) {
991                 ifp->if_ierrors++;
992                 m_freem(m0);
993                 priv->if_error = ENOBUFS;
994                 return;
995         }
996         ifp->if_opackets++;
997
998         priv->if_error = ether_output_frame(oifp, m0);
999         return;
1000 }
1001
1002 #ifdef DEBUG
1003 /*
1004  * Display an ioctl to the virtual interface
1005  */
1006
1007 static void
1008 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1009 {
1010         char   *str;
1011
1012         switch (command & IOC_DIRMASK) {
1013         case IOC_VOID:
1014                 str = "IO";
1015                 break;
1016         case IOC_OUT:
1017                 str = "IOR";
1018                 break;
1019         case IOC_IN:
1020                 str = "IOW";
1021                 break;
1022         case IOC_INOUT:
1023                 str = "IORW";
1024                 break;
1025         default:
1026                 str = "IO??";
1027         }
1028         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1029                ifp->if_xname,
1030                str,
1031                IOCGROUP(command),
1032                command & 0xff,
1033                IOCPARM_LEN(command));
1034 }
1035 #endif /* DEBUG */
1036
1037 /************************************************************************
1038                         NETGRAPH NODE STUFF
1039  ************************************************************************/
1040
1041 /*
1042  * Constructor for a node
1043  */
1044 static int
1045 ng_fec_constructor(node_p *nodep)
1046 {
1047         char ifname[NG_FEC_FEC_NAME_MAX + 1];
1048         struct ifnet *ifp;
1049         node_p node;
1050         priv_p priv;
1051         struct ng_fec_bundle *b;
1052         int error = 0;
1053
1054         /* Allocate node and interface private structures */
1055         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1056         if (priv == NULL)
1057                 return (ENOMEM);
1058         bzero(priv, sizeof(*priv));
1059
1060         ifp = &priv->arpcom.ac_if;
1061         b = &priv->fec_bundle;
1062
1063         /* Link them together */
1064         ifp->if_softc = priv;
1065
1066         /* Get an interface unit number */
1067         if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1068                 FREE(ifp, M_NETGRAPH);
1069                 FREE(priv, M_NETGRAPH);
1070                 return (error);
1071         }
1072
1073         /* Call generic node constructor */
1074         if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1075                 ng_fec_free_unit(priv->unit);
1076                 FREE(ifp, M_NETGRAPH);
1077                 FREE(priv, M_NETGRAPH);
1078                 return (error);
1079         }
1080         node = *nodep;
1081
1082         /* Link together node and private info */
1083         node->private = priv;
1084         priv->node = node;
1085         priv->arpcom.ac_netgraph = node;
1086
1087         /* Initialize interface structure */
1088         if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1089         ifp->if_start = ng_fec_start;
1090         ifp->if_ioctl = ng_fec_ioctl;
1091         ifp->if_init = ng_fec_init;
1092         ifp->if_watchdog = NULL;
1093         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1094         ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1095         ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1096         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
1097         ifp->if_addrlen = 0;                    /* XXX */
1098         ifp->if_hdrlen = 0;                     /* XXX */
1099         ifp->if_baudrate = 100000000;           /* XXX */
1100         TAILQ_INIT(&ifp->if_addrhead);
1101
1102         /* Give this node the same name as the interface (if possible) */
1103         bzero(ifname, sizeof(ifname));
1104         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1105         if (ng_name_node(node, ifname) != 0)
1106                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1107
1108         /* Grab hold of the ether_input pipe. */
1109         if (ng_ether_input_p == NULL)
1110                 ng_ether_input_p = ng_fec_input;
1111
1112         /* Attach the interface */
1113         ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1114         priv->real_if_output = ifp->if_output;
1115         ifp->if_output = ng_fec_output;
1116         callout_handle_init(&priv->fec_ch);
1117
1118         TAILQ_INIT(&b->ng_fec_ports);
1119         b->fec_ifcnt = 0;
1120
1121         ifmedia_init(&priv->ifmedia, 0,
1122             ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1123         ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1124         ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1125
1126         /* Done */
1127         return (0);
1128 }
1129
1130 /*
1131  * Receive a control message
1132  */
1133 static int
1134 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1135                 const char *retaddr, struct ng_mesg **rptr)
1136 {
1137         const priv_p priv = node->private;
1138         struct ng_fec_bundle    *b;
1139         struct ng_mesg *resp = NULL;
1140         char *ifname;
1141         int error = 0;
1142
1143         b = &priv->fec_bundle;
1144
1145         switch (msg->header.typecookie) {
1146         case NGM_FEC_COOKIE:
1147                 switch (msg->header.cmd) {
1148                 case NGM_FEC_ADD_IFACE:
1149                         ifname = msg->data;
1150                         error = ng_fec_addport(priv, ifname);
1151                         break;
1152                 case NGM_FEC_DEL_IFACE:
1153                         ifname = msg->data;
1154                         error = ng_fec_delport(priv, ifname);
1155                         break;
1156                 case NGM_FEC_SET_MODE_MAC:
1157                         b->fec_btype = FEC_BTYPE_MAC;
1158                         break;
1159 #ifdef INET
1160                 case NGM_FEC_SET_MODE_INET:
1161                         b->fec_btype = FEC_BTYPE_INET;
1162                         break;
1163 #ifdef INET6
1164                 case NGM_FEC_SET_MODE_INET6:
1165                         b->fec_btype = FEC_BTYPE_INET6;
1166                         break;
1167 #endif
1168 #endif
1169                 default:
1170                         error = EINVAL;
1171                         break;
1172                 }
1173                 break;
1174         default:
1175                 error = EINVAL;
1176                 break;
1177         }
1178         if (rptr)
1179                 *rptr = resp;
1180         else if (resp)
1181                 FREE(resp, M_NETGRAPH);
1182         FREE(msg, M_NETGRAPH);
1183         return (error);
1184 }
1185
1186 /*
1187  * Shutdown and remove the node and its associated interface.
1188  */
1189 static int
1190 ng_fec_rmnode(node_p node)
1191 {
1192         const priv_p priv = node->private;
1193         struct ng_fec_bundle *b;
1194         struct ng_fec_portlist  *p;
1195         char ifname[IFNAMSIZ];
1196
1197         b = &priv->fec_bundle;
1198         ng_fec_stop(&priv->arpcom.ac_if);
1199
1200         while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1201                 p = TAILQ_FIRST(&b->ng_fec_ports);
1202                 sprintf(ifname, "%s",
1203                     p->fec_if->if_xname); /* XXX: strings */
1204                 ng_fec_delport(priv, ifname);
1205         }
1206
1207         ng_cutlinks(node);
1208         ng_unname(node);
1209         if (ng_ether_input_p != NULL)
1210                 ng_ether_input_p = NULL;
1211         ether_ifdetach(&priv->arpcom.ac_if);
1212         ifmedia_removeall(&priv->ifmedia);
1213         ng_fec_free_unit(priv->unit);
1214         FREE(priv, M_NETGRAPH);
1215         node->private = NULL;
1216         ng_unref(node);
1217         return (0);
1218 }