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