xref: /dragonfly/sys/netgraph/fec/ng_fec.c (revision d37f73b6)
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  */
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/sockio.h>
96 #include <sys/socket.h>
97 #include <sys/syslog.h>
98 #include <sys/libkern.h>
99 #include <sys/queue.h>
100 #include <sys/thread2.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/ifq_var.h>
108 #include <net/bpf.h>
109 #include <net/ethernet.h>
110 
111 #include "opt_inet.h"
112 #include "opt_inet6.h"
113 
114 #include <netinet/in.h>
115 #ifdef INET
116 #include <netinet/in_systm.h>
117 #include <netinet/ip.h>
118 #endif
119 
120 #ifdef INET6
121 #include <netinet/ip6.h>
122 #endif
123 
124 #include <netgraph/ng_message.h>
125 #include <netgraph/netgraph.h>
126 #include <netgraph/ng_parse.h>
127 #include "ng_fec.h"
128 
129 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
130 #define FEC_INC(x, y)	(x) = (x + 1) % y
131 
132 /*
133  * Current fast etherchannel implementations use either 2 or 4
134  * ports, so for now we limit the maximum bundle size to 4 interfaces.
135  */
136 #define FEC_BUNDLESIZ	4
137 
138 struct ng_fec_portlist {
139 	struct ifnet		*fec_if;
140 	int			fec_idx;
141 	int			fec_ifstat;
142 	struct ether_addr	fec_mac;
143 	TAILQ_ENTRY(ng_fec_portlist) fec_list;
144 };
145 
146 struct 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 */
157 struct 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 fec_timeout;	/* callout for ticker */
166 	int	(*real_if_output)(struct ifnet *, struct mbuf *,
167 				  struct sockaddr *, struct rtentry *);
168 };
169 typedef struct ng_fec_private *priv_p;
170 
171 /* Interface methods */
172 static void	ng_fec_input(struct ifnet *, struct mbuf **);
173 static void	ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *);
174 static int	ng_fec_choose_port(struct ng_fec_bundle *b,
175 			struct mbuf *m, struct ifnet **ifp);
176 static int	ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
177 static void	ng_fec_init(void *arg);
178 static void	ng_fec_stop(struct ifnet *ifp);
179 static int	ng_fec_ifmedia_upd(struct ifnet *ifp);
180 static void	ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
181 static int	ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
182 			     struct ucred *);
183 static int	ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
184 			struct sockaddr *dst, struct rtentry *rt0);
185 static void	ng_fec_tick(void *arg);
186 static int	ng_fec_addport(struct ng_fec_private *priv, char *iface);
187 static int	ng_fec_delport(struct ng_fec_private *priv, char *iface);
188 
189 #ifdef DEBUG
190 static void	ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
191 #endif
192 
193 /* Netgraph methods */
194 static ng_constructor_t	ng_fec_constructor;
195 static ng_rcvmsg_t	ng_fec_rcvmsg;
196 static ng_shutdown_t	ng_fec_rmnode;
197 
198 /* List of commands and how to convert arguments to/from ASCII */
199 static const struct ng_cmdlist ng_fec_cmds[] = {
200 	{
201 	  NGM_FEC_COOKIE,
202 	  NGM_FEC_ADD_IFACE,
203 	  "add_iface",
204 	  &ng_parse_string_type,
205 	  NULL,
206 	},
207 	{
208 	  NGM_FEC_COOKIE,
209 	  NGM_FEC_DEL_IFACE,
210 	  "del_iface",
211 	  &ng_parse_string_type,
212 	  NULL,
213 	},
214 	{
215 	  NGM_FEC_COOKIE,
216 	  NGM_FEC_SET_MODE_MAC,
217 	  "set_mode_mac",
218 	  NULL,
219 	  NULL,
220 	},
221 	{
222 	  NGM_FEC_COOKIE,
223 	  NGM_FEC_SET_MODE_INET,
224 	  "set_mode_inet",
225 	  NULL,
226 	  NULL,
227 	},
228 	{ 0 }
229 };
230 
231 /* Node type descriptor */
232 static struct ng_type typestruct = {
233 	NG_VERSION,
234 	NG_FEC_NODE_TYPE,
235 	NULL,
236 	ng_fec_constructor,
237 	ng_fec_rcvmsg,
238 	ng_fec_rmnode,
239 	NULL,
240 	NULL,
241 	NULL,
242 	NULL,
243 	NULL,
244 	NULL,
245 	ng_fec_cmds
246 };
247 NETGRAPH_INIT(fec, &typestruct);
248 
249 /* We keep a bitmap indicating which unit numbers are free.
250    One means the unit number is free, zero means it's taken. */
251 static int	*ng_fec_units = NULL;
252 static int	ng_fec_units_len = 0;
253 static int	ng_units_in_use = 0;
254 
255 #define UNITS_BITSPERWORD	(sizeof(*ng_fec_units) * NBBY)
256 
257 /*
258  * Find the first free unit number for a new interface.
259  * Increase the size of the unit bitmap as necessary.
260  */
261 static __inline__ int
262 ng_fec_get_unit(int *unit)
263 {
264 	int index, bit;
265 
266 	for (index = 0; index < ng_fec_units_len
267 	    && ng_fec_units[index] == 0; index++);
268 	if (index == ng_fec_units_len) {		/* extend array */
269 		int i, *newarray, newlen;
270 
271 		newlen = (2 * ng_fec_units_len) + 4;
272 		newarray = kmalloc(newlen * sizeof(*ng_fec_units),
273 				   M_NETGRAPH, M_NOWAIT);
274 		if (newarray == NULL)
275 			return (ENOMEM);
276 		bcopy(ng_fec_units, newarray,
277 		    ng_fec_units_len * sizeof(*ng_fec_units));
278 		for (i = ng_fec_units_len; i < newlen; i++)
279 			newarray[i] = ~0;
280 		if (ng_fec_units != NULL)
281 			kfree(ng_fec_units, M_NETGRAPH);
282 		ng_fec_units = newarray;
283 		ng_fec_units_len = newlen;
284 	}
285 	bit = ffs(ng_fec_units[index]) - 1;
286 	KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
287 	    ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
288 	ng_fec_units[index] &= ~(1 << bit);
289 	*unit = (index * UNITS_BITSPERWORD) + bit;
290 	ng_units_in_use++;
291 	return (0);
292 }
293 
294 /*
295  * Free a no longer needed unit number.
296  */
297 static __inline__ void
298 ng_fec_free_unit(int unit)
299 {
300 	int index, bit;
301 
302 	index = unit / UNITS_BITSPERWORD;
303 	bit = unit % UNITS_BITSPERWORD;
304 	KASSERT(index < ng_fec_units_len,
305 	    ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
306 	KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
307 	    ("%s: unit=%d is free", __func__, unit));
308 	ng_fec_units[index] |= (1 << bit);
309 	/*
310 	 * XXX We could think about reducing the size of ng_fec_units[]
311 	 * XXX here if the last portion is all ones
312 	 * XXX At least free it if no more units.
313 	 * Needed if we are eventually be able to unload.
314 	 */
315 	ng_units_in_use++;
316 	if (ng_units_in_use == 0) { /* XXX make SMP safe */
317 		kfree(ng_fec_units, M_NETGRAPH);
318 		ng_fec_units_len = 0;
319 		ng_fec_units = NULL;
320 	}
321 }
322 
323 /************************************************************************
324 			INTERFACE STUFF
325  ************************************************************************/
326 
327 static int
328 ng_fec_addport(struct ng_fec_private *priv, char *iface)
329 {
330 	struct ng_fec_bundle	*b;
331 	struct ifnet		*ifp, *bifp;
332 	struct arpcom		*ac;
333 	struct sockaddr_dl	*sdl;
334 	struct ng_fec_portlist	*p, *new;
335 
336 	if (priv == NULL || iface == NULL)
337 		return(EINVAL);
338 
339 	b = &priv->fec_bundle;
340 	ifp = &priv->arpcom.ac_if;
341 
342 	/* Find the interface */
343 	bifp = ifunit(iface);
344 	if (bifp == NULL) {
345 		kprintf("fec%d: tried to add iface %s, which "
346 		    "doesn't seem to exist\n", priv->unit, iface);
347 		return(ENOENT);
348 	}
349 
350 	/* See if we have room in the bundle */
351 	if (b->fec_ifcnt == FEC_BUNDLESIZ) {
352 		kprintf("fec%d: can't add new iface; bundle is full\n",
353 		    priv->unit);
354 		return(ENOSPC);
355 	}
356 
357 	/* See if the interface is already in the bundle */
358 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
359 		if (p->fec_if == bifp) {
360 			kprintf("fec%d: iface %s is already in this "
361 			    "bundle\n", priv->unit, iface);
362 			return(EINVAL);
363 		}
364 	}
365 
366 	/* Allocate new list entry. */
367 	new = kmalloc(sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
368 	if (new == NULL)
369 		return(ENOMEM);
370 
371 	ac = (struct arpcom *)bifp;
372 	ac->ac_netgraph = priv->node;
373 
374 	/*
375 	 * If this is the first interface added to the bundle,
376 	 * use its MAC address for the virtual interface (and,
377 	 * by extension, all the other ports in the bundle).
378 	 */
379 	if (b->fec_ifcnt == 0) {
380 		sdl = IF_LLSOCKADDR(ifp);
381 		bcopy((char *)ac->ac_enaddr,
382 		    priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
383 		bcopy((char *)ac->ac_enaddr,
384 		    LLADDR(sdl), ETHER_ADDR_LEN);
385 	}
386 
387 	b->fec_btype = FEC_BTYPE_MAC;
388 	new->fec_idx = b->fec_ifcnt;
389 	b->fec_ifcnt++;
390 
391 	/* Save the real MAC address. */
392 	bcopy((char *)ac->ac_enaddr,
393 	    (char *)&new->fec_mac, ETHER_ADDR_LEN);
394 
395 	/* Set up phony MAC address. */
396 	sdl = IF_LLSOCKADDR(bifp);
397 	bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
398 	bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
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 
407 static int
408 ng_fec_delport(struct ng_fec_private *priv, char *iface)
409 {
410 	struct ng_fec_bundle	*b;
411 	struct ifnet		*bifp;
412 	struct arpcom		*ac;
413 	struct sockaddr_dl	*sdl;
414 	struct ng_fec_portlist	*p;
415 
416 	if (priv == NULL || iface == NULL)
417 		return(EINVAL);
418 
419 	b = &priv->fec_bundle;
420 
421 	/* Find the interface */
422 	bifp = ifunit(iface);
423 	if (bifp == NULL) {
424 		kprintf("fec%d: tried to remove iface %s, which "
425 		    "doesn't seem to exist\n", priv->unit, iface);
426 		return(ENOENT);
427 	}
428 
429 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
430 		if (p->fec_if == bifp)
431 			break;
432 	}
433 
434 	if (p == NULL) {
435 		kprintf("fec%d: tried to remove iface %s which "
436 		    "is not in our bundle\n", priv->unit, iface);
437 		return(EINVAL);
438 	}
439 
440 	/* Stop interface */
441 	bifp->if_flags &= ~IFF_UP;
442 	bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
443 
444 	/* Restore MAC address. */
445 	ac = (struct arpcom *)bifp;
446 	sdl = IF_LLSOCKADDR(bifp);
447 	bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
448 	bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
449 
450 	/* Delete port */
451 	TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
452 	kfree(p, M_NETGRAPH);
453 	b->fec_ifcnt--;
454 
455 	return(0);
456 }
457 
458 /*
459  * Pass an ioctl command down to all the underyling interfaces in a
460  * bundle. Used for setting multicast filters and flags.
461  */
462 static int
463 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
464 {
465 	struct ng_fec_private	*priv;
466 	struct ng_fec_bundle	*b;
467 	struct ifnet		*oifp;
468 	struct ng_fec_portlist	*p;
469 
470 	priv = ifp->if_softc;
471 	b = &priv->fec_bundle;
472 
473 	ifnet_deserialize_all(ifp);	/* XXX */
474 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
475 		oifp = p->fec_if;
476 		if (oifp != NULL) {
477 			ifnet_serialize_all(oifp);
478 			oifp->if_ioctl(oifp, command, data, NULL);
479 			ifnet_deserialize_all(oifp);
480 		}
481 	}
482 	ifnet_serialize_all(ifp);
483 
484 	return(0);
485 }
486 
487 static void
488 ng_fec_init(void *arg)
489 {
490 	struct ng_fec_private	*priv;
491 	struct ng_fec_bundle	*b;
492 	struct ifnet		*ifp, *bifp;
493 	struct ng_fec_portlist	*p;
494 
495 	ifp = arg;
496 	priv = ifp->if_softc;
497 	b = &priv->fec_bundle;
498 
499 	if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
500 		kprintf("fec%d: invalid bundle "
501 		    "size: %d\n", priv->unit,
502 		    b->fec_ifcnt);
503 		return;
504 	}
505 
506 	ng_fec_stop(ifp);
507 
508 	ifnet_deserialize_all(ifp);	/* XXX */
509 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
510 		bifp = p->fec_if;
511 		ifnet_serialize_all(bifp);
512 		bifp->if_flags |= IFF_UP;
513                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
514 		/* mark iface as up and let the monitor check it */
515 		p->fec_ifstat = -1;
516 		ifnet_deserialize_all(bifp);
517 	}
518 	ifnet_serialize_all(ifp);
519 
520 	callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
521 }
522 
523 static void
524 ng_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 	ifnet_deserialize_all(ifp);	/* XXX */
535 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
536 		bifp = p->fec_if;
537 		ifnet_serialize_all(bifp);
538 		bifp->if_flags &= ~IFF_UP;
539                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
540 		ifnet_deserialize_all(bifp);
541 	}
542 	ifnet_serialize_all(ifp);
543 
544 	callout_stop(&priv->fec_timeout);
545 }
546 
547 static void
548 ng_fec_tick(void *arg)
549 {
550 	struct ng_fec_private	*priv;
551 	struct ng_fec_bundle	*b;
552         struct ifmediareq	ifmr;
553 	struct ifnet		*ifp;
554 	struct ng_fec_portlist	*p;
555 	int			error = 0;
556 
557 	priv = arg;
558 	b = &priv->fec_bundle;
559 
560 	/*
561 	 * Note: serializer for parent interface not held on entry, and
562 	 * cannot be held during the loop to avoid a deadlock.
563 	 */
564 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
565 		bzero((char *)&ifmr, sizeof(ifmr));
566 		ifp = p->fec_if;
567 		ifnet_serialize_all(ifp);
568 		error = ifp->if_ioctl(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
569 		if (error) {
570 			kprintf("fec%d: failed to check status "
571 			    "of link %s\n", priv->unit, ifp->if_xname);
572 			ifnet_deserialize_all(ifp);
573 			continue;
574 		}
575 
576         	if (ifmr.ifm_status & IFM_AVALID &&
577                     IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
578 			if (ifmr.ifm_status & IFM_ACTIVE) {
579 				if (p->fec_ifstat == -1 ||
580 				    p->fec_ifstat == 0) {
581 					p->fec_ifstat = 1;
582 					kprintf("fec%d: port %s in bundle "
583 					    "is up\n", priv->unit,
584 					    ifp->if_xname);
585 				}
586 			} else {
587 				if (p->fec_ifstat == -1 ||
588 				    p->fec_ifstat == 1) {
589 					p->fec_ifstat = 0;
590 					kprintf("fec%d: port %s in bundle "
591 					    "is down\n", priv->unit,
592 					    ifp->if_xname);
593 				}
594 			}
595 		}
596 		ifnet_deserialize_all(ifp);
597 	}
598 
599 	ifp = &priv->arpcom.ac_if;
600 	if (ifp->if_flags & IFF_RUNNING)
601 		callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
602 }
603 
604 static int
605 ng_fec_ifmedia_upd(struct ifnet *ifp)
606 {
607 	return(0);
608 }
609 
610 static void
611 ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
612 {
613 	struct ng_fec_private	*priv;
614 	struct ng_fec_bundle	*b;
615 	struct ng_fec_portlist	*p;
616 
617 	priv = ifp->if_softc;
618 	b = &priv->fec_bundle;
619 
620 	ifmr->ifm_status = IFM_AVALID;
621 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
622 		if (p->fec_ifstat) {
623 			ifmr->ifm_status |= IFM_ACTIVE;
624 			break;
625 		}
626 	}
627 }
628 
629 /*
630  * Process an ioctl for the virtual interface
631  */
632 static int
633 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
634 {
635 	struct ifreq *const ifr = (struct ifreq *) data;
636 	int error = 0;
637 	struct ng_fec_private	*priv;
638 	struct ng_fec_bundle	*b;
639 
640 	priv = ifp->if_softc;
641 	b = &priv->fec_bundle;
642 
643 #ifdef DEBUG
644 	ng_fec_print_ioctl(ifp, command, data);
645 #endif
646 	crit_enter();
647 	switch (command) {
648 
649 	/* These two are mostly handled at a higher layer */
650 	case SIOCSIFADDR:
651 	case SIOCGIFADDR:
652 	case SIOCSIFMTU:
653 		error = ether_ioctl(ifp, command, data);
654 		break;
655 
656 	/* Set flags */
657 	case SIOCSIFFLAGS:
658 		/*
659 		 * If the interface is marked up and stopped, then start it.
660 		 * If it is marked down and running, then stop it.
661 		 */
662 		if (ifr->ifr_flags & IFF_UP) {
663 			if (!(ifp->if_flags & IFF_RUNNING)) {
664 				/* Sanity. */
665 				if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
666 					kprintf("fec%d: invalid bundle "
667 					    "size: %d\n", priv->unit,
668 					    b->fec_ifcnt);
669 					error = EINVAL;
670 					break;
671 				}
672 				ifq_clr_oactive(&ifp->if_snd);
673 				ifp->if_flags |= IFF_RUNNING;
674 				ng_fec_init(ifp);
675 			}
676 			/*
677 			 * Bubble down changes in promisc mode to
678 			 * underlying interfaces.
679 			 */
680 			if ((ifp->if_flags & IFF_PROMISC) !=
681 			    (priv->if_flags & IFF_PROMISC)) {
682 				ng_fec_setport(ifp, command, data);
683 				priv->if_flags = ifp->if_flags;
684 			}
685 		} else {
686 			if (ifp->if_flags & IFF_RUNNING) {
687 				ifp->if_flags &= ~IFF_RUNNING;
688 				ifq_clr_oactive(&ifp->if_snd);
689 			}
690 			ng_fec_stop(ifp);
691 		}
692 		break;
693 
694 	case SIOCADDMULTI:
695 	case SIOCDELMULTI:
696 		ng_fec_setport(ifp, command, data);
697 		error = 0;
698 		break;
699 	case SIOCGIFMEDIA:
700 	case SIOCSIFMEDIA:
701 		error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
702 		break;
703 	/* Stuff that's not supported */
704 	case SIOCSIFPHYS:
705 		error = EOPNOTSUPP;
706 		break;
707 
708 	default:
709 		error = EINVAL;
710 		break;
711 	}
712 	crit_exit();
713 	return (error);
714 }
715 
716 /*
717  * This routine spies on mbufs passing through ether_input(). If
718  * they come from one of the interfaces that are aggregated into
719  * our bundle, we fix up the ifnet pointer and increment our
720  * packet counters so that it looks like the frames are actually
721  * coming from us.
722  */
723 static void
724 ng_fec_input(struct ifnet *ifp, struct mbuf **m0)
725 {
726 	struct ng_node		*node;
727 	struct ng_fec_private	*priv;
728 	struct ng_fec_bundle	*b;
729 	struct mbuf		*m;
730 	struct ifnet		*bifp;
731 	struct ng_fec_portlist	*p;
732 
733 	/* Sanity check */
734 	if (ifp == NULL || m0 == NULL)
735 		return;
736 
737 	node = IFP2NG(ifp);
738 
739 	/* Sanity check part II */
740 	if (node == NULL)
741 		return;
742 
743 	priv = node->private;
744 	b = &priv->fec_bundle;
745 	bifp = &priv->arpcom.ac_if;
746 
747 	m = *m0;
748 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
749 		if (p->fec_if == m->m_pkthdr.rcvif)
750 			break;
751 	}
752 
753 	/* Wasn't meant for us; leave this frame alone. */
754 	if (p == NULL)
755 		return;
756 
757 	/* Pretend this is our frame. */
758 	m->m_pkthdr.rcvif = bifp;
759 	IFNET_STAT_INC(bifp, ipackets, 1);
760 	IFNET_STAT_INC(bifp, ibytes, m->m_pkthdr.len);
761 
762 	if (bifp->if_bpf) {
763 		bpf_gettoken();
764 		if (bifp->if_bpf)
765 			bpf_mtap(bifp->if_bpf, m);
766 		bpf_reltoken();
767 	}
768 }
769 
770 /*
771  * Take a quick peek at the packet and see if it's ok for us to use
772  * the inet or inet6 hash methods on it, if they're enabled. We do
773  * this by setting flags in the mbuf header. Once we've made up our
774  * mind what to do, we pass the frame to ether_output() for further
775  * processing.
776  */
777 
778 static int
779 ng_fec_output_serialized(struct ifnet *ifp, struct mbuf *m,
780 			 struct sockaddr *dst, struct rtentry *rt0)
781 {
782 	const priv_p priv = (priv_p) ifp->if_softc;
783 	struct ng_fec_bundle *b;
784 	int error;
785 
786 	/* Check interface flags */
787 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
788 		m_freem(m);
789 		return (ENETDOWN);
790 	}
791 
792 	b = &priv->fec_bundle;
793 
794 	switch (b->fec_btype) {
795 	case FEC_BTYPE_MAC:
796 		m->m_flags |= M_FEC_MAC;
797 		break;
798 #ifdef INET
799 	case FEC_BTYPE_INET:
800 		/*
801 		 * We can't use the INET address port selection
802 		 * scheme if this isn't an INET packet.
803 		 */
804 		if (dst->sa_family == AF_INET)
805 			m->m_flags |= M_FEC_INET;
806 #ifdef INET6
807 		else if (dst->sa_family == AF_INET6)
808 			m->m_flags |= M_FEC_INET6;
809 #endif
810 		else {
811 #ifdef DEBUG
812 			kprintf("%s: can't do inet aggregation of non "
813 			    "inet packet\n", ifp->if_xname);
814 #endif
815 			m->m_flags |= M_FEC_MAC;
816 		}
817 		break;
818 #endif
819 	default:
820 		kprintf("%s: bogus hash type: %d\n", ifp->if_xname,
821 		    b->fec_btype);
822 		m_freem(m);
823 		return(EINVAL);
824 		break;
825 	}
826 
827 	/*
828 	 * Pass the frame to ether_output() for all the protocol
829 	 * handling. This will put the ethernet header on the packet
830 	 * for us.
831 	 */
832 	priv->if_error = 0;
833 	error = priv->real_if_output(ifp, m, dst, rt0);
834 	if (priv->if_error && !error)
835 		error = priv->if_error;
836 
837 	return(error);
838 }
839 
840 static int
841 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
842 	      struct sockaddr *dst, struct rtentry *rt0)
843 {
844 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
845 	int error;
846 
847 	ifsq_serialize_hw(ifsq);
848 	error = ng_fec_output_serialized(ifp, m, dst, rt0);
849 	ifsq_deserialize_hw(ifsq);
850 
851 	return error;
852 }
853 
854 /*
855  * Apply a hash to the source and destination addresses in the packet
856  * in order to select an interface. Also check link status and handle
857  * dead links accordingly.
858  */
859 
860 static int
861 ng_fec_choose_port(struct ng_fec_bundle *b,
862 	struct mbuf *m, struct ifnet **ifp)
863 {
864 	struct ether_header	*eh;
865 	struct mbuf		*m0;
866 #ifdef INET
867 	struct ip		*ip;
868 #ifdef INET6
869 	struct ip6_hdr		*ip6;
870 #endif
871 #endif
872 
873 	struct ng_fec_portlist	*p;
874 	int			port = 0, mask;
875 
876 	/*
877 	 * If there are only two ports, mask off all but the
878 	 * last bit for XORing. If there are 4, mask off all
879 	 * but the last 2 bits.
880 	 */
881 	mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
882 	eh = mtod(m, struct ether_header *);
883 #ifdef INET
884 	ip = (struct ip *)(mtod(m, char *) +
885 	    sizeof(struct ether_header));
886 #ifdef INET6
887 	ip6 = (struct ip6_hdr *)(mtod(m, char *) +
888 	    sizeof(struct ether_header));
889 #endif
890 #endif
891 
892 	/*
893 	 * The fg_fec_output() routine is supposed to leave a
894 	 * flag for us in the mbuf that tells us what hash to
895 	 * use, but sometimes a new mbuf is prepended to the
896 	 * chain, so we have to search every mbuf in the chain
897 	 * to find the flags.
898 	 */
899 	m0 = m;
900 	while (m0) {
901 		if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
902 			break;
903 		m0 = m0->m_next;
904 	}
905 	if (m0 == NULL)
906 		return(EINVAL);
907 
908 	switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
909 	case M_FEC_MAC:
910 		port = (eh->ether_dhost[5] ^
911 		    eh->ether_shost[5]) & mask;
912 		break;
913 #ifdef INET
914 	case M_FEC_INET:
915 		port = (ntohl(ip->ip_dst.s_addr) ^
916 		    ntohl(ip->ip_src.s_addr)) & mask;
917 		break;
918 #ifdef INET6
919 	case M_FEC_INET6:
920 		port = (ip6->ip6_dst.s6_addr[15] ^
921 		    ip6->ip6_dst.s6_addr[15]) & mask;
922 		break;
923 #endif
924 #endif
925 	default:
926 		return(EINVAL);
927 			break;
928 	}
929 
930 	TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
931 		if (port == p->fec_idx)
932 			break;
933 	}
934 
935 	/*
936 	 * Now that we've chosen a port, make sure it's
937 	 * alive. If it's not alive, cycle through the bundle
938 	 * looking for a port that is alive. If we don't find
939 	 * any, return an error.
940 	 */
941 	if (p->fec_ifstat != 1) {
942 		struct ng_fec_portlist	*n = NULL;
943 
944 		n = TAILQ_NEXT(p, fec_list);
945 		if (n == NULL)
946 			n = TAILQ_FIRST(&b->ng_fec_ports);
947 		while (n != p) {
948 			if (n->fec_ifstat == 1)
949 				break;
950 			n = TAILQ_NEXT(n, fec_list);
951 			if (n == NULL)
952 				n = TAILQ_FIRST(&b->ng_fec_ports);
953 		}
954 		if (n == p)
955 			return(EAGAIN);
956 		p = n;
957 	}
958 
959 	*ifp = p->fec_if;
960 
961 	return(0);
962 }
963 
964 /*
965  * Now that the packet has been run through ether_output(), yank it
966  * off our own send queue and stick it on the queue for the appropriate
967  * underlying physical interface. Note that if the interface's send
968  * queue is full, we save an error status in our private netgraph
969  * space which will eventually be handed up to ng_fec_output(), which
970  * will return it to the rest of the IP stack. We need to do this
971  * in order to duplicate the effect of ether_output() returning ENOBUFS
972  * when it detects that an interface's send queue is full. There's no
973  * other way to signal the error status from here since the if_start()
974  * routine is spec'ed to return void.
975  *
976  * Once the frame is queued, we call ether_output_frame() to initiate
977  * transmission.
978  */
979 static void
980 ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
981 {
982 	struct ng_fec_private	*priv;
983 	struct ng_fec_bundle	*b;
984 	struct ifnet		*oifp = NULL;
985 	struct mbuf		*m0;
986 	int			error;
987 
988 	priv = ifp->if_softc;
989 	b = &priv->fec_bundle;
990 
991 	m0 = ifq_dequeue(&ifp->if_snd);
992 	if (m0 == NULL)
993 		return;
994 
995 	BPF_MTAP(ifp, m0);
996 
997 	/* Queue up packet on the proper port. */
998 	error = ng_fec_choose_port(b, m0, &oifp);
999 	if (error) {
1000 		IFNET_STAT_INC(ifp, ierrors, 1);
1001 		m_freem(m0);
1002 		priv->if_error = ENOBUFS;
1003 		return;
1004 	}
1005 	IFNET_STAT_INC(ifp, opackets, 1);
1006 
1007 	/*
1008 	 * Release current iface's serializer to avoid possible dead lock
1009 	 */
1010 	priv->if_error = ether_output_frame(oifp, m0);
1011 }
1012 
1013 #ifdef DEBUG
1014 /*
1015  * Display an ioctl to the virtual interface
1016  */
1017 
1018 static void
1019 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1020 {
1021 	char   *str;
1022 
1023 	switch (command & IOC_DIRMASK) {
1024 	case IOC_VOID:
1025 		str = "IO";
1026 		break;
1027 	case IOC_OUT:
1028 		str = "IOR";
1029 		break;
1030 	case IOC_IN:
1031 		str = "IOW";
1032 		break;
1033 	case IOC_INOUT:
1034 		str = "IORW";
1035 		break;
1036 	default:
1037 		str = "IO??";
1038 	}
1039 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1040 	       ifp->if_xname,
1041 	       str,
1042 	       IOCGROUP(command),
1043 	       command & 0xff,
1044 	       IOCPARM_LEN(command));
1045 }
1046 #endif /* DEBUG */
1047 
1048 /************************************************************************
1049 			NETGRAPH NODE STUFF
1050  ************************************************************************/
1051 
1052 /*
1053  * Constructor for a node
1054  */
1055 static int
1056 ng_fec_constructor(node_p *nodep)
1057 {
1058 	char ifname[NG_FEC_FEC_NAME_MAX + 1];
1059 	struct ifnet *ifp;
1060 	node_p node;
1061 	priv_p priv;
1062 	struct ng_fec_bundle *b;
1063 	int error = 0;
1064 
1065 	/* Allocate node and interface private structures */
1066 	priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1067 	if (priv == NULL)
1068 		return (ENOMEM);
1069 
1070 	ifp = &priv->arpcom.ac_if;
1071 	b = &priv->fec_bundle;
1072 
1073 	/* Link them together */
1074 	ifp->if_softc = priv;
1075 
1076 	/* Get an interface unit number */
1077 	if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1078 		kfree(ifp, M_NETGRAPH);
1079 		kfree(priv, M_NETGRAPH);
1080 		return (error);
1081 	}
1082 
1083 	/* Call generic node constructor */
1084 	if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1085 		ng_fec_free_unit(priv->unit);
1086 		kfree(ifp, M_NETGRAPH);
1087 		kfree(priv, M_NETGRAPH);
1088 		return (error);
1089 	}
1090 	node = *nodep;
1091 
1092 	/* Link together node and private info */
1093 	node->private = priv;
1094 	priv->node = node;
1095 	priv->arpcom.ac_netgraph = node;
1096 
1097 	/* Initialize interface structure */
1098 	if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1099 	ifp->if_start = ng_fec_start;
1100 	ifp->if_ioctl = ng_fec_ioctl;
1101 	ifp->if_init = ng_fec_init;
1102 	ifp->if_watchdog = NULL;
1103 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
1104 	ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1105 	ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1106 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
1107 	ifp->if_addrlen = 0;			/* XXX */
1108 	ifp->if_hdrlen = 0;			/* XXX */
1109 	ifp->if_baudrate = 100000000;		/* XXX */
1110 
1111 	/* Give this node the same name as the interface (if possible) */
1112 	bzero(ifname, sizeof(ifname));
1113 	strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1114 	if (ng_name_node(node, ifname) != 0)
1115 		log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1116 
1117 	/* Grab hold of the ether_input pipe. */
1118 	if (ng_ether_input_p == NULL)
1119 		ng_ether_input_p = ng_fec_input;
1120 
1121 	/* Attach the interface */
1122 	ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
1123 	priv->real_if_output = ifp->if_output;
1124 	ifp->if_output = ng_fec_output;
1125 	callout_init(&priv->fec_timeout);
1126 
1127 	TAILQ_INIT(&b->ng_fec_ports);
1128 	b->fec_ifcnt = 0;
1129 
1130 	ifmedia_init(&priv->ifmedia, 0,
1131 	    ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1132 	ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1133 	ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1134 
1135 	/* Done */
1136 	return (0);
1137 }
1138 
1139 /*
1140  * Receive a control message
1141  */
1142 static int
1143 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1144 		const char *retaddr, struct ng_mesg **rptr)
1145 {
1146 	const priv_p priv = node->private;
1147 	struct ng_fec_bundle	*b;
1148 	struct ng_mesg *resp = NULL;
1149 	char *ifname;
1150 	int error = 0;
1151 
1152 	b = &priv->fec_bundle;
1153 
1154 	switch (msg->header.typecookie) {
1155 	case NGM_FEC_COOKIE:
1156 		switch (msg->header.cmd) {
1157 		case NGM_FEC_ADD_IFACE:
1158 			ifname = msg->data;
1159 			error = ng_fec_addport(priv, ifname);
1160 			break;
1161 		case NGM_FEC_DEL_IFACE:
1162 			ifname = msg->data;
1163 			error = ng_fec_delport(priv, ifname);
1164 			break;
1165 		case NGM_FEC_SET_MODE_MAC:
1166 			b->fec_btype = FEC_BTYPE_MAC;
1167 			break;
1168 #ifdef INET
1169 		case NGM_FEC_SET_MODE_INET:
1170 			b->fec_btype = FEC_BTYPE_INET;
1171 			break;
1172 #ifdef INET6
1173 		case NGM_FEC_SET_MODE_INET6:
1174 			b->fec_btype = FEC_BTYPE_INET6;
1175 			break;
1176 #endif
1177 #endif
1178 		default:
1179 			error = EINVAL;
1180 			break;
1181 		}
1182 		break;
1183 	default:
1184 		error = EINVAL;
1185 		break;
1186 	}
1187 	if (rptr)
1188 		*rptr = resp;
1189 	else if (resp)
1190 		kfree(resp, M_NETGRAPH);
1191 	kfree(msg, M_NETGRAPH);
1192 	return (error);
1193 }
1194 
1195 /*
1196  * Shutdown and remove the node and its associated interface.
1197  */
1198 static int
1199 ng_fec_rmnode(node_p node)
1200 {
1201 	const priv_p priv = node->private;
1202 	struct ng_fec_bundle *b;
1203 	struct ng_fec_portlist	*p;
1204 	char ifname[IFNAMSIZ];
1205 
1206 	b = &priv->fec_bundle;
1207 	ng_fec_stop(&priv->arpcom.ac_if);
1208 
1209 	while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1210 		p = TAILQ_FIRST(&b->ng_fec_ports);
1211 		ksprintf(ifname, "%s",
1212 		    p->fec_if->if_xname); /* XXX: strings */
1213 		ng_fec_delport(priv, ifname);
1214 	}
1215 
1216 	ng_cutlinks(node);
1217 	ng_unname(node);
1218 	if (ng_ether_input_p != NULL)
1219 		ng_ether_input_p = NULL;
1220 	ether_ifdetach(&priv->arpcom.ac_if);
1221 	ifmedia_removeall(&priv->ifmedia);
1222 	ng_fec_free_unit(priv->unit);
1223 	kfree(priv, M_NETGRAPH);
1224 	node->private = NULL;
1225 	ng_unref(node);
1226 	return (0);
1227 }
1228