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