xref: /dragonfly/sys/netgraph/fec/ng_fec.c (revision 7bc7e232)
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.21 2007/11/16 05:07:36 sephe 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 #include <sys/thread2.h>
103 
104 #include <net/if.h>
105 #include <net/if_types.h>
106 #include <net/if_arp.h>
107 #include <net/if_dl.h>
108 #include <net/if_media.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 fec_timeout;	/* callout 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 			const 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", __func__, 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", __func__, unit, ng_fec_units_len));
308 	KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
309 	    ("%s: unit=%d is free", __func__, 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 sockaddr_dl	*sdl;
336 	struct ng_fec_portlist	*p, *new;
337 
338 	if (priv == NULL || iface == NULL)
339 		return(EINVAL);
340 
341 	b = &priv->fec_bundle;
342 	ifp = &priv->arpcom.ac_if;
343 
344 	/* Find the interface */
345 	bifp = ifunit(iface);
346 	if (bifp == NULL) {
347 		kprintf("fec%d: tried to add iface %s, which "
348 		    "doesn't seem to exist\n", priv->unit, iface);
349 		return(ENOENT);
350 	}
351 
352 	/* See if we have room in the bundle */
353 	if (b->fec_ifcnt == FEC_BUNDLESIZ) {
354 		kprintf("fec%d: can't add new iface; bundle is full\n",
355 		    priv->unit);
356 		return(ENOSPC);
357 	}
358 
359 	/* See if the interface is already in the bundle */
360 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
361 		if (p->fec_if == bifp) {
362 			kprintf("fec%d: iface %s is already in this "
363 			    "bundle\n", priv->unit, iface);
364 			return(EINVAL);
365 		}
366 	}
367 
368 	/* Allocate new list entry. */
369 	MALLOC(new, struct ng_fec_portlist *,
370 	    sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
371 	if (new == NULL)
372 		return(ENOMEM);
373 
374 	ac = (struct arpcom *)bifp;
375 	ac->ac_netgraph = priv->node;
376 
377 	/*
378 	 * If this is the first interface added to the bundle,
379 	 * use its MAC address for the virtual interface (and,
380 	 * by extension, all the other ports in the bundle).
381 	 */
382 	if (b->fec_ifcnt == 0) {
383 		sdl = IF_LLSOCKADDR(ifp);
384 		bcopy((char *)ac->ac_enaddr,
385 		    priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
386 		bcopy((char *)ac->ac_enaddr,
387 		    LLADDR(sdl), ETHER_ADDR_LEN);
388 	}
389 
390 	b->fec_btype = FEC_BTYPE_MAC;
391 	new->fec_idx = b->fec_ifcnt;
392 	b->fec_ifcnt++;
393 
394 	/* Save the real MAC address. */
395 	bcopy((char *)ac->ac_enaddr,
396 	    (char *)&new->fec_mac, ETHER_ADDR_LEN);
397 
398 	/* Set up phony MAC address. */
399 	sdl = IF_LLSOCKADDR(bifp);
400 	bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
401 	bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
402 
403 	/* Add to the queue */
404 	new->fec_if = bifp;
405 	TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
406 
407 	return(0);
408 }
409 
410 static int
411 ng_fec_delport(struct ng_fec_private *priv, char *iface)
412 {
413 	struct ng_fec_bundle	*b;
414 	struct ifnet		*ifp, *bifp;
415 	struct arpcom		*ac;
416 	struct sockaddr_dl	*sdl;
417 	struct ng_fec_portlist	*p;
418 
419 	if (priv == NULL || iface == NULL)
420 		return(EINVAL);
421 
422 	b = &priv->fec_bundle;
423 	ifp = &priv->arpcom.ac_if;
424 
425 	/* Find the interface */
426 	bifp = ifunit(iface);
427 	if (bifp == NULL) {
428 		kprintf("fec%d: tried to remove iface %s, which "
429 		    "doesn't seem to exist\n", priv->unit, iface);
430 		return(ENOENT);
431 	}
432 
433 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
434 		if (p->fec_if == bifp)
435 			break;
436 	}
437 
438 	if (p == NULL) {
439 		kprintf("fec%d: tried to remove iface %s which "
440 		    "is not in our bundle\n", priv->unit, iface);
441 		return(EINVAL);
442 	}
443 
444 	/* Stop interface */
445 	bifp->if_flags &= ~IFF_UP;
446 	bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
447 
448 	/* Restore MAC address. */
449 	ac = (struct arpcom *)bifp;
450 	sdl = IF_LLSOCKADDR(bifp);
451 	bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
452 	bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
453 
454 	/* Delete port */
455 	TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
456 	FREE(p, M_NETGRAPH);
457 	b->fec_ifcnt--;
458 
459 	return(0);
460 }
461 
462 /*
463  * Pass an ioctl command down to all the underyling interfaces in a
464  * bundle. Used for setting multicast filters and flags.
465  */
466 static int
467 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
468 {
469 	struct ng_fec_private	*priv;
470 	struct ng_fec_bundle	*b;
471 	struct ifnet		*oifp;
472 	struct ng_fec_portlist	*p;
473 
474 	priv = ifp->if_softc;
475 	b = &priv->fec_bundle;
476 
477 	lwkt_serialize_exit(ifp->if_serializer);	/* XXX */
478 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
479 		oifp = p->fec_if;
480 		if (oifp != NULL) {
481 			lwkt_serialize_enter(oifp->if_serializer);
482 			oifp->if_ioctl(oifp, command, data, NULL);
483 			lwkt_serialize_exit(oifp->if_serializer);
484 		}
485 	}
486 	lwkt_serialize_enter(ifp->if_serializer);
487 
488 	return(0);
489 }
490 
491 static void
492 ng_fec_init(void *arg)
493 {
494 	struct ng_fec_private	*priv;
495 	struct ng_fec_bundle	*b;
496 	struct ifnet		*ifp, *bifp;
497 	struct ng_fec_portlist	*p;
498 
499 	ifp = arg;
500 	priv = ifp->if_softc;
501 	b = &priv->fec_bundle;
502 
503 	if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
504 		kprintf("fec%d: invalid bundle "
505 		    "size: %d\n", priv->unit,
506 		    b->fec_ifcnt);
507 		return;
508 	}
509 
510 	ng_fec_stop(ifp);
511 
512 	lwkt_serialize_exit(ifp->if_serializer);	/* XXX */
513 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
514 		bifp = p->fec_if;
515 		lwkt_serialize_enter(bifp->if_serializer);
516 		bifp->if_flags |= IFF_UP;
517                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
518 		/* mark iface as up and let the monitor check it */
519 		p->fec_ifstat = -1;
520 		lwkt_serialize_exit(bifp->if_serializer);
521 	}
522 	lwkt_serialize_enter(ifp->if_serializer);
523 
524 	callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
525 }
526 
527 static void
528 ng_fec_stop(struct ifnet *ifp)
529 {
530 	struct ng_fec_private	*priv;
531 	struct ng_fec_bundle	*b;
532 	struct ifnet		*bifp;
533 	struct ng_fec_portlist	*p;
534 
535 	priv = ifp->if_softc;
536 	b = &priv->fec_bundle;
537 
538 	lwkt_serialize_exit(ifp->if_serializer);	/* XXX */
539 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
540 		bifp = p->fec_if;
541 		lwkt_serialize_enter(bifp->if_serializer);
542 		bifp->if_flags &= ~IFF_UP;
543                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
544 		lwkt_serialize_exit(bifp->if_serializer);
545 	}
546 	callout_stop(&priv->fec_timeout);
547 	lwkt_serialize_enter(ifp->if_serializer);	/* XXX */
548 }
549 
550 static void
551 ng_fec_tick(void *arg)
552 {
553 	struct ng_fec_private	*priv;
554 	struct ng_fec_bundle	*b;
555         struct ifmediareq	ifmr;
556 	struct ifnet		*ifp;
557 	struct ng_fec_portlist	*p;
558 	int			error = 0;
559 
560 	priv = arg;
561 	b = &priv->fec_bundle;
562 
563 	/*
564 	 * Note: serializer for parent interface not held on entry, and
565 	 * cannot be held during the loop to avoid a deadlock.
566 	 */
567 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
568 		bzero((char *)&ifmr, sizeof(ifmr));
569 		ifp = p->fec_if;
570 		lwkt_serialize_enter(ifp->if_serializer);
571 		error = ifp->if_ioctl(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
572 		if (error) {
573 			kprintf("fec%d: failed to check status "
574 			    "of link %s\n", priv->unit, ifp->if_xname);
575 			lwkt_serialize_exit(ifp->if_serializer);
576 			continue;
577 		}
578 
579         	if (ifmr.ifm_status & IFM_AVALID &&
580                     IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
581 			if (ifmr.ifm_status & IFM_ACTIVE) {
582 				if (p->fec_ifstat == -1 ||
583 				    p->fec_ifstat == 0) {
584 					p->fec_ifstat = 1;
585 					kprintf("fec%d: port %s in bundle "
586 					    "is up\n", priv->unit,
587 					    ifp->if_xname);
588 				}
589 			} else {
590 				if (p->fec_ifstat == -1 ||
591 				    p->fec_ifstat == 1) {
592 					p->fec_ifstat = 0;
593 					kprintf("fec%d: port %s in bundle "
594 					    "is down\n", priv->unit,
595 					    ifp->if_xname);
596 				}
597 			}
598 		}
599 		lwkt_serialize_exit(ifp->if_serializer);
600 	}
601 
602 	ifp = &priv->arpcom.ac_if;
603 	if (ifp->if_flags & IFF_RUNNING)
604 		callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
605 }
606 
607 static int
608 ng_fec_ifmedia_upd(struct ifnet *ifp)
609 {
610 	return(0);
611 }
612 
613 static void
614 ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
615 {
616 	struct ng_fec_private	*priv;
617 	struct ng_fec_bundle	*b;
618 	struct ng_fec_portlist	*p;
619 
620 	priv = ifp->if_softc;
621 	b = &priv->fec_bundle;
622 
623 	ifmr->ifm_status = IFM_AVALID;
624 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
625 		if (p->fec_ifstat) {
626 			ifmr->ifm_status |= IFM_ACTIVE;
627 			break;
628 		}
629 	}
630 }
631 
632 /*
633  * Process an ioctl for the virtual interface
634  */
635 static int
636 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
637 {
638 	struct ifreq *const ifr = (struct ifreq *) data;
639 	int error = 0;
640 	struct ng_fec_private	*priv;
641 	struct ng_fec_bundle	*b;
642 
643 	priv = ifp->if_softc;
644 	b = &priv->fec_bundle;
645 
646 #ifdef DEBUG
647 	ng_fec_print_ioctl(ifp, command, data);
648 #endif
649 	crit_enter();
650 	switch (command) {
651 
652 	/* These two are mostly handled at a higher layer */
653 	case SIOCSIFADDR:
654 	case SIOCGIFADDR:
655 	case SIOCSIFMTU:
656 		error = ether_ioctl(ifp, command, data);
657 		break;
658 
659 	/* Set flags */
660 	case SIOCSIFFLAGS:
661 		/*
662 		 * If the interface is marked up and stopped, then start it.
663 		 * If it is marked down and running, then stop it.
664 		 */
665 		if (ifr->ifr_flags & IFF_UP) {
666 			if (!(ifp->if_flags & IFF_RUNNING)) {
667 				/* Sanity. */
668 				if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
669 					kprintf("fec%d: invalid bundle "
670 					    "size: %d\n", priv->unit,
671 					    b->fec_ifcnt);
672 					error = EINVAL;
673 					break;
674 				}
675 				ifp->if_flags &= ~(IFF_OACTIVE);
676 				ifp->if_flags |= IFF_RUNNING;
677 				ng_fec_init(ifp);
678 			}
679 			/*
680 			 * Bubble down changes in promisc mode to
681 			 * underlying interfaces.
682 			 */
683 			if ((ifp->if_flags & IFF_PROMISC) !=
684 			    (priv->if_flags & IFF_PROMISC)) {
685 				ng_fec_setport(ifp, command, data);
686 				priv->if_flags = ifp->if_flags;
687 			}
688 		} else {
689 			if (ifp->if_flags & IFF_RUNNING)
690 				ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
691 			ng_fec_stop(ifp);
692 		}
693 		break;
694 
695 	case SIOCADDMULTI:
696 	case SIOCDELMULTI:
697 		ng_fec_setport(ifp, command, data);
698 		error = 0;
699 		break;
700 	case SIOCGIFMEDIA:
701 	case SIOCSIFMEDIA:
702 		error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
703 		break;
704 	/* Stuff that's not supported */
705 	case SIOCSIFPHYS:
706 		error = EOPNOTSUPP;
707 		break;
708 
709 	default:
710 		error = EINVAL;
711 		break;
712 	}
713 	crit_exit();
714 	return (error);
715 }
716 
717 /*
718  * This routine spies on mbufs passing through ether_input(). If
719  * they come from one of the interfaces that are aggregated into
720  * our bundle, we fix up the ifnet pointer and increment our
721  * packet counters so that it looks like the frames are actually
722  * coming from us.
723  */
724 static void
725 ng_fec_input(struct ifnet *ifp, struct mbuf **m0,
726 		const struct ether_header *eh)
727 {
728 	struct ng_node		*node;
729 	struct ng_fec_private	*priv;
730 	struct ng_fec_bundle	*b;
731 	struct mbuf		*m;
732 	struct ifnet		*bifp;
733 	struct ng_fec_portlist	*p;
734 
735 	/* Sanity check */
736 	if (ifp == NULL || m0 == NULL || eh == NULL)
737 		return;
738 
739 	node = IFP2NG(ifp);
740 
741 	/* Sanity check part II */
742 	if (node == NULL)
743 		return;
744 
745 	priv = node->private;
746 	b = &priv->fec_bundle;
747 	bifp = &priv->arpcom.ac_if;
748 
749 	m = *m0;
750 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
751 		if (p->fec_if == m->m_pkthdr.rcvif)
752 			break;
753 	}
754 
755 	/* Wasn't meant for us; leave this frame alone. */
756 	if (p == NULL)
757 		return;
758 
759 	/* Pretend this is our frame. */
760 	m->m_pkthdr.rcvif = bifp;
761 	bifp->if_ipackets++;
762 	bifp->if_ibytes += m->m_pkthdr.len + sizeof(struct ether_header);
763 
764 	if (bifp->if_bpf)
765 		bpf_ptap(bifp->if_bpf, m, eh, ETHER_HDR_LEN);
766 }
767 
768 /*
769  * Take a quick peek at the packet and see if it's ok for us to use
770  * the inet or inet6 hash methods on it, if they're enabled. We do
771  * this by setting flags in the mbuf header. Once we've made up our
772  * mind what to do, we pass the frame to ether_output() for further
773  * processing.
774  */
775 
776 static int
777 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
778 		struct sockaddr *dst, struct rtentry *rt0)
779 {
780 	const priv_p priv = (priv_p) ifp->if_softc;
781 	struct ng_fec_bundle *b;
782 	int error;
783 
784 	/* Check interface flags */
785 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
786 		m_freem(m);
787 		return (ENETDOWN);
788 	}
789 
790 	b = &priv->fec_bundle;
791 
792 	switch (b->fec_btype) {
793 	case FEC_BTYPE_MAC:
794 		m->m_flags |= M_FEC_MAC;
795 		break;
796 #ifdef INET
797 	case FEC_BTYPE_INET:
798 		/*
799 		 * We can't use the INET address port selection
800 		 * scheme if this isn't an INET packet.
801 		 */
802 		if (dst->sa_family == AF_INET)
803 			m->m_flags |= M_FEC_INET;
804 #ifdef INET6
805 		else if (dst->sa_family == AF_INET6)
806 			m->m_flags |= M_FEC_INET6;
807 #endif
808 		else {
809 #ifdef DEBUG
810 			kprintf("%s: can't do inet aggregation of non "
811 			    "inet packet\n", ifp->if_xname);
812 #endif
813 			m->m_flags |= M_FEC_MAC;
814 		}
815 		break;
816 #endif
817 	default:
818 		kprintf("%s: bogus hash type: %d\n", ifp->if_xname,
819 		    b->fec_btype);
820 		m_freem(m);
821 		return(EINVAL);
822 		break;
823 	}
824 
825 	/*
826 	 * Pass the frame to ether_output() for all the protocol
827 	 * handling. This will put the ethernet header on the packet
828 	 * for us.
829 	 */
830 	priv->if_error = 0;
831 	error = priv->real_if_output(ifp, m, dst, rt0);
832 	if (priv->if_error && !error)
833 		error = priv->if_error;
834 
835 	return(error);
836 }
837 
838 /*
839  * Apply a hash to the source and destination addresses in the packet
840  * in order to select an interface. Also check link status and handle
841  * dead links accordingly.
842  */
843 
844 static int
845 ng_fec_choose_port(struct ng_fec_bundle *b,
846 	struct mbuf *m, struct ifnet **ifp)
847 {
848 	struct ether_header	*eh;
849 	struct mbuf		*m0;
850 #ifdef INET
851 	struct ip		*ip;
852 #ifdef INET6
853 	struct ip6_hdr		*ip6;
854 #endif
855 #endif
856 
857 	struct ng_fec_portlist	*p;
858 	int			port = 0, mask;
859 
860 	/*
861 	 * If there are only two ports, mask off all but the
862 	 * last bit for XORing. If there are 4, mask off all
863 	 * but the last 2 bits.
864 	 */
865 	mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
866 	eh = mtod(m, struct ether_header *);
867 #ifdef INET
868 	ip = (struct ip *)(mtod(m, char *) +
869 	    sizeof(struct ether_header));
870 #ifdef INET6
871 	ip6 = (struct ip6_hdr *)(mtod(m, char *) +
872 	    sizeof(struct ether_header));
873 #endif
874 #endif
875 
876 	/*
877 	 * The fg_fec_output() routine is supposed to leave a
878 	 * flag for us in the mbuf that tells us what hash to
879 	 * use, but sometimes a new mbuf is prepended to the
880 	 * chain, so we have to search every mbuf in the chain
881 	 * to find the flags.
882 	 */
883 	m0 = m;
884 	while (m0) {
885 		if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
886 			break;
887 		m0 = m0->m_next;
888 	}
889 	if (m0 == NULL)
890 		return(EINVAL);
891 
892 	switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
893 	case M_FEC_MAC:
894 		port = (eh->ether_dhost[5] ^
895 		    eh->ether_shost[5]) & mask;
896 		break;
897 #ifdef INET
898 	case M_FEC_INET:
899 		port = (ntohl(ip->ip_dst.s_addr) ^
900 		    ntohl(ip->ip_src.s_addr)) & mask;
901 		break;
902 #ifdef INET6
903 	case M_FEC_INET6:
904 		port = (ip6->ip6_dst.s6_addr[15] ^
905 		    ip6->ip6_dst.s6_addr[15]) & mask;
906 		break;
907 #endif
908 #endif
909 	default:
910 		return(EINVAL);
911 			break;
912 	}
913 
914 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
915 		if (port == p->fec_idx)
916 			break;
917 	}
918 
919 	/*
920 	 * Now that we've chosen a port, make sure it's
921 	 * alive. If it's not alive, cycle through the bundle
922 	 * looking for a port that is alive. If we don't find
923 	 * any, return an error.
924 	 */
925 	if (p->fec_ifstat != 1) {
926 		struct ng_fec_portlist	*n = NULL;
927 
928 		n = TAILQ_NEXT(p, fec_list);
929 		if (n == NULL)
930 			n = TAILQ_FIRST(&b->ng_fec_ports);
931 		while (n != p) {
932 			if (n->fec_ifstat == 1)
933 				break;
934 			n = TAILQ_NEXT(n, fec_list);
935 			if (n == NULL)
936 				n = TAILQ_FIRST(&b->ng_fec_ports);
937 		}
938 		if (n == p)
939 			return(EAGAIN);
940 		p = n;
941 	}
942 
943 	*ifp = p->fec_if;
944 
945 	return(0);
946 }
947 
948 /*
949  * Now that the packet has been run through ether_output(), yank it
950  * off our own send queue and stick it on the queue for the appropriate
951  * underlying physical interface. Note that if the interface's send
952  * queue is full, we save an error status in our private netgraph
953  * space which will eventually be handed up to ng_fec_output(), which
954  * will return it to the rest of the IP stack. We need to do this
955  * in order to duplicate the effect of ether_output() returning ENOBUFS
956  * when it detects that an interface's send queue is full. There's no
957  * other way to signal the error status from here since the if_start()
958  * routine is spec'ed to return void.
959  *
960  * Once the frame is queued, we call ether_output_frame() to initiate
961  * transmission.
962  */
963 static void
964 ng_fec_start(struct ifnet *ifp)
965 {
966 	struct ng_fec_private	*priv;
967 	struct ng_fec_bundle	*b;
968 	struct ifnet		*oifp = NULL;
969 	struct mbuf		*m0;
970 	int			error;
971 
972 	priv = ifp->if_softc;
973 	b = &priv->fec_bundle;
974 
975 	IF_DEQUEUE(&ifp->if_snd, m0);
976 	if (m0 == NULL)
977 		return;
978 
979 	BPF_MTAP(ifp, m0);
980 
981 	/* Queue up packet on the proper port. */
982 	error = ng_fec_choose_port(b, m0, &oifp);
983 	if (error) {
984 		ifp->if_ierrors++;
985 		m_freem(m0);
986 		priv->if_error = ENOBUFS;
987 		return;
988 	}
989 	ifp->if_opackets++;
990 
991 	lwkt_serialize_exit(ifp->if_serializer);
992 	lwkt_serialize_enter(oifp->if_serializer);
993 	priv->if_error = ether_output_frame(oifp, m0);
994 	lwkt_serialize_exit(oifp->if_serializer);
995 	lwkt_serialize_enter(ifp->if_serializer);
996 }
997 
998 #ifdef DEBUG
999 /*
1000  * Display an ioctl to the virtual interface
1001  */
1002 
1003 static void
1004 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1005 {
1006 	char   *str;
1007 
1008 	switch (command & IOC_DIRMASK) {
1009 	case IOC_VOID:
1010 		str = "IO";
1011 		break;
1012 	case IOC_OUT:
1013 		str = "IOR";
1014 		break;
1015 	case IOC_IN:
1016 		str = "IOW";
1017 		break;
1018 	case IOC_INOUT:
1019 		str = "IORW";
1020 		break;
1021 	default:
1022 		str = "IO??";
1023 	}
1024 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1025 	       ifp->if_xname,
1026 	       str,
1027 	       IOCGROUP(command),
1028 	       command & 0xff,
1029 	       IOCPARM_LEN(command));
1030 }
1031 #endif /* DEBUG */
1032 
1033 /************************************************************************
1034 			NETGRAPH NODE STUFF
1035  ************************************************************************/
1036 
1037 /*
1038  * Constructor for a node
1039  */
1040 static int
1041 ng_fec_constructor(node_p *nodep)
1042 {
1043 	char ifname[NG_FEC_FEC_NAME_MAX + 1];
1044 	struct ifnet *ifp;
1045 	node_p node;
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 	/* Call generic node constructor */
1070 	if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1071 		ng_fec_free_unit(priv->unit);
1072 		FREE(ifp, M_NETGRAPH);
1073 		FREE(priv, M_NETGRAPH);
1074 		return (error);
1075 	}
1076 	node = *nodep;
1077 
1078 	/* Link together node and private info */
1079 	node->private = priv;
1080 	priv->node = node;
1081 	priv->arpcom.ac_netgraph = node;
1082 
1083 	/* Initialize interface structure */
1084 	if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1085 	ifp->if_start = ng_fec_start;
1086 	ifp->if_ioctl = ng_fec_ioctl;
1087 	ifp->if_init = ng_fec_init;
1088 	ifp->if_watchdog = NULL;
1089 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1090 	ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1091 	ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1092 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
1093 	ifp->if_addrlen = 0;			/* XXX */
1094 	ifp->if_hdrlen = 0;			/* XXX */
1095 	ifp->if_baudrate = 100000000;		/* XXX */
1096 	TAILQ_INIT(&ifp->if_addrhead);
1097 
1098 	/* Give this node the same name as the interface (if possible) */
1099 	bzero(ifname, sizeof(ifname));
1100 	strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1101 	if (ng_name_node(node, ifname) != 0)
1102 		log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1103 
1104 	/* Grab hold of the ether_input pipe. */
1105 	if (ng_ether_input_p == NULL)
1106 		ng_ether_input_p = ng_fec_input;
1107 
1108 	/* Attach the interface */
1109 	ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
1110 	priv->real_if_output = ifp->if_output;
1111 	ifp->if_output = ng_fec_output;
1112 	callout_init(&priv->fec_timeout);
1113 
1114 	TAILQ_INIT(&b->ng_fec_ports);
1115 	b->fec_ifcnt = 0;
1116 
1117 	ifmedia_init(&priv->ifmedia, 0,
1118 	    ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1119 	ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1120 	ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1121 
1122 	/* Done */
1123 	return (0);
1124 }
1125 
1126 /*
1127  * Receive a control message
1128  */
1129 static int
1130 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1131 		const char *retaddr, struct ng_mesg **rptr)
1132 {
1133 	const priv_p priv = node->private;
1134 	struct ng_fec_bundle	*b;
1135 	struct ng_mesg *resp = NULL;
1136 	char *ifname;
1137 	int error = 0;
1138 
1139 	b = &priv->fec_bundle;
1140 
1141 	switch (msg->header.typecookie) {
1142 	case NGM_FEC_COOKIE:
1143 		switch (msg->header.cmd) {
1144 		case NGM_FEC_ADD_IFACE:
1145 			ifname = msg->data;
1146 			error = ng_fec_addport(priv, ifname);
1147 			break;
1148 		case NGM_FEC_DEL_IFACE:
1149 			ifname = msg->data;
1150 			error = ng_fec_delport(priv, ifname);
1151 			break;
1152 		case NGM_FEC_SET_MODE_MAC:
1153 			b->fec_btype = FEC_BTYPE_MAC;
1154 			break;
1155 #ifdef INET
1156 		case NGM_FEC_SET_MODE_INET:
1157 			b->fec_btype = FEC_BTYPE_INET;
1158 			break;
1159 #ifdef INET6
1160 		case NGM_FEC_SET_MODE_INET6:
1161 			b->fec_btype = FEC_BTYPE_INET6;
1162 			break;
1163 #endif
1164 #endif
1165 		default:
1166 			error = EINVAL;
1167 			break;
1168 		}
1169 		break;
1170 	default:
1171 		error = EINVAL;
1172 		break;
1173 	}
1174 	if (rptr)
1175 		*rptr = resp;
1176 	else if (resp)
1177 		FREE(resp, M_NETGRAPH);
1178 	FREE(msg, M_NETGRAPH);
1179 	return (error);
1180 }
1181 
1182 /*
1183  * Shutdown and remove the node and its associated interface.
1184  */
1185 static int
1186 ng_fec_rmnode(node_p node)
1187 {
1188 	const priv_p priv = node->private;
1189 	struct ng_fec_bundle *b;
1190 	struct ng_fec_portlist	*p;
1191 	char ifname[IFNAMSIZ];
1192 
1193 	b = &priv->fec_bundle;
1194 	ng_fec_stop(&priv->arpcom.ac_if);
1195 
1196 	while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1197 		p = TAILQ_FIRST(&b->ng_fec_ports);
1198 		ksprintf(ifname, "%s",
1199 		    p->fec_if->if_xname); /* XXX: strings */
1200 		ng_fec_delport(priv, ifname);
1201 	}
1202 
1203 	ng_cutlinks(node);
1204 	ng_unname(node);
1205 	if (ng_ether_input_p != NULL)
1206 		ng_ether_input_p = NULL;
1207 	ether_ifdetach(&priv->arpcom.ac_if);
1208 	ifmedia_removeall(&priv->ifmedia);
1209 	ng_fec_free_unit(priv->unit);
1210 	FREE(priv, M_NETGRAPH);
1211 	node->private = NULL;
1212 	ng_unref(node);
1213 	return (0);
1214 }
1215