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