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