ng_ether.c revision 191510
1
2/*
3 * ng_ether.c
4 */
5
6/*-
7 * Copyright (c) 1996-2000 Whistle Communications, Inc.
8 * All rights reserved.
9 *
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Whistle Communications;
13 * provided, however, that:
14 * 1. Any and all reproductions of the source or object code must include the
15 *    copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Whistle
17 *    Communications, Inc. trademarks, including the mark "WHISTLE
18 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19 *    such appears in the above copyright notice or in the software.
20 *
21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * Authors: Archie Cobbs <archie@freebsd.org>
40 *	    Julian Elischer <julian@freebsd.org>
41 *
42 * $FreeBSD: head/sys/netgraph/ng_ether.c 191510 2009-04-26 07:14:50Z zec $
43 */
44
45/*
46 * ng_ether(4) netgraph node type
47 */
48
49#include "opt_route.h"
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
54#include <sys/malloc.h>
55#include <sys/mbuf.h>
56#include <sys/errno.h>
57#include <sys/syslog.h>
58#include <sys/socket.h>
59#include <sys/vimage.h>
60
61#include <net/if.h>
62#include <net/if_dl.h>
63#include <net/if_types.h>
64#include <net/if_arp.h>
65#include <net/if_var.h>
66#include <net/ethernet.h>
67#include <net/if_bridgevar.h>
68#include <net/route.h>
69#include <net/vnet.h>
70
71#include <netgraph/ng_message.h>
72#include <netgraph/netgraph.h>
73#include <netgraph/ng_parse.h>
74#include <netgraph/ng_ether.h>
75
76#define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
77
78static vnet_attach_fn ng_ether_iattach;
79
80#ifndef VIMAGE_GLOBALS
81static vnet_modinfo_t vnet_ng_ether_modinfo = {
82	.vmi_id		= VNET_MOD_NG_ETHER,
83	.vmi_name	= "ng_ether",
84	.vmi_dependson	= VNET_MOD_NETGRAPH,
85	.vmi_iattach	= ng_ether_iattach,
86};
87#endif
88
89/* Per-node private data */
90struct private {
91	struct ifnet	*ifp;		/* associated interface */
92	hook_p		upper;		/* upper hook connection */
93	hook_p		lower;		/* lower hook connection */
94	hook_p		orphan;		/* orphan hook connection */
95	u_char		autoSrcAddr;	/* always overwrite source address */
96	u_char		promisc;	/* promiscuous mode enabled */
97	u_long		hwassist;	/* hardware checksum capabilities */
98	u_int		flags;		/* flags e.g. really die */
99};
100typedef struct private *priv_p;
101
102/* Hook pointers used by if_ethersubr.c to callback to netgraph */
103extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
104extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
105extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
106extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
107extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
108extern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
109
110/* Functional hooks called from if_ethersubr.c */
111static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
112static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
113static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
114static void	ng_ether_attach(struct ifnet *ifp);
115static void	ng_ether_detach(struct ifnet *ifp);
116static void	ng_ether_link_state(struct ifnet *ifp, int state);
117
118/* Other functions */
119static int	ng_ether_rcv_lower(hook_p node, item_p item);
120static int	ng_ether_rcv_upper(hook_p node, item_p item);
121
122/* Netgraph node methods */
123static ng_constructor_t	ng_ether_constructor;
124static ng_rcvmsg_t	ng_ether_rcvmsg;
125static ng_shutdown_t	ng_ether_shutdown;
126static ng_newhook_t	ng_ether_newhook;
127static ng_rcvdata_t	ng_ether_rcvdata;
128static ng_disconnect_t	ng_ether_disconnect;
129static int		ng_ether_mod_event(module_t mod, int event, void *data);
130
131/* List of commands and how to convert arguments to/from ASCII */
132static const struct ng_cmdlist ng_ether_cmdlist[] = {
133	{
134	  NGM_ETHER_COOKIE,
135	  NGM_ETHER_GET_IFNAME,
136	  "getifname",
137	  NULL,
138	  &ng_parse_string_type
139	},
140	{
141	  NGM_ETHER_COOKIE,
142	  NGM_ETHER_GET_IFINDEX,
143	  "getifindex",
144	  NULL,
145	  &ng_parse_int32_type
146	},
147	{
148	  NGM_ETHER_COOKIE,
149	  NGM_ETHER_GET_ENADDR,
150	  "getenaddr",
151	  NULL,
152	  &ng_parse_enaddr_type
153	},
154	{
155	  NGM_ETHER_COOKIE,
156	  NGM_ETHER_SET_ENADDR,
157	  "setenaddr",
158	  &ng_parse_enaddr_type,
159	  NULL
160	},
161	{
162	  NGM_ETHER_COOKIE,
163	  NGM_ETHER_GET_PROMISC,
164	  "getpromisc",
165	  NULL,
166	  &ng_parse_int32_type
167	},
168	{
169	  NGM_ETHER_COOKIE,
170	  NGM_ETHER_SET_PROMISC,
171	  "setpromisc",
172	  &ng_parse_int32_type,
173	  NULL
174	},
175	{
176	  NGM_ETHER_COOKIE,
177	  NGM_ETHER_GET_AUTOSRC,
178	  "getautosrc",
179	  NULL,
180	  &ng_parse_int32_type
181	},
182	{
183	  NGM_ETHER_COOKIE,
184	  NGM_ETHER_SET_AUTOSRC,
185	  "setautosrc",
186	  &ng_parse_int32_type,
187	  NULL
188	},
189	{
190	  NGM_ETHER_COOKIE,
191	  NGM_ETHER_ADD_MULTI,
192	  "addmulti",
193	  &ng_parse_enaddr_type,
194	  NULL
195	},
196	{
197	  NGM_ETHER_COOKIE,
198	  NGM_ETHER_DEL_MULTI,
199	  "delmulti",
200	  &ng_parse_enaddr_type,
201	  NULL
202	},
203	{
204	  NGM_ETHER_COOKIE,
205	  NGM_ETHER_DETACH,
206	  "detach",
207	  NULL,
208	  NULL
209	},
210	{ 0 }
211};
212
213static struct ng_type ng_ether_typestruct = {
214	.version =	NG_ABI_VERSION,
215	.name =		NG_ETHER_NODE_TYPE,
216	.mod_event =	ng_ether_mod_event,
217	.constructor =	ng_ether_constructor,
218	.rcvmsg =	ng_ether_rcvmsg,
219	.shutdown =	ng_ether_shutdown,
220	.newhook =	ng_ether_newhook,
221	.rcvdata =	ng_ether_rcvdata,
222	.disconnect =	ng_ether_disconnect,
223	.cmdlist =	ng_ether_cmdlist,
224};
225NETGRAPH_INIT(ether, &ng_ether_typestruct);
226
227/******************************************************************
228		    ETHERNET FUNCTION HOOKS
229******************************************************************/
230
231/*
232 * Handle a packet that has come in on an interface. We get to
233 * look at it here before any upper layer protocols do.
234 *
235 * NOTE: this function will get called at splimp()
236 */
237static void
238ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
239{
240	const node_p node = IFP2NG(ifp);
241	const priv_p priv = NG_NODE_PRIVATE(node);
242	int error;
243
244	/* If "lower" hook not connected, let packet continue */
245	if (priv->lower == NULL)
246		return;
247	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
248}
249
250/*
251 * Handle a packet that has come in on an interface, and which
252 * does not match any of our known protocols (an ``orphan'').
253 *
254 * NOTE: this function will get called at splimp()
255 */
256static void
257ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
258{
259	const node_p node = IFP2NG(ifp);
260	const priv_p priv = NG_NODE_PRIVATE(node);
261	int error;
262
263	/* If "orphan" hook not connected, discard packet */
264	if (priv->orphan == NULL) {
265		m_freem(m);
266		return;
267	}
268	NG_SEND_DATA_ONLY(error, priv->orphan, m);
269}
270
271/*
272 * Handle a packet that is going out on an interface.
273 * The Ethernet header is already attached to the mbuf.
274 */
275static int
276ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
277{
278	const node_p node = IFP2NG(ifp);
279	const priv_p priv = NG_NODE_PRIVATE(node);
280	int error = 0;
281
282	/* If "upper" hook not connected, let packet continue */
283	if (priv->upper == NULL)
284		return (0);
285
286	/* Send it out "upper" hook */
287	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
288	return (error);
289}
290
291/*
292 * A new Ethernet interface has been attached.
293 * Create a new node for it, etc.
294 */
295static void
296ng_ether_attach(struct ifnet *ifp)
297{
298	priv_p priv;
299	node_p node;
300
301	/*
302	 * Do not create / attach an ether node to this ifnet if
303	 * a netgraph node with the same name already exists.
304	 * This should prevent ether nodes to become attached to
305	 * eiface nodes, which may be problematic due to naming
306	 * clashes.
307	 */
308	if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
309		NG_NODE_UNREF(node);
310		return;
311	}
312
313	/* Create node */
314	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
315	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
316		log(LOG_ERR, "%s: can't %s for %s\n",
317		    __func__, "create node", ifp->if_xname);
318		return;
319	}
320
321	/* Allocate private data */
322	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
323	if (priv == NULL) {
324		log(LOG_ERR, "%s: can't %s for %s\n",
325		    __func__, "allocate memory", ifp->if_xname);
326		NG_NODE_UNREF(node);
327		return;
328	}
329	NG_NODE_SET_PRIVATE(node, priv);
330	priv->ifp = ifp;
331	IFP2NG(ifp) = node;
332	priv->hwassist = ifp->if_hwassist;
333
334	/* Try to give the node the same name as the interface */
335	if (ng_name_node(node, ifp->if_xname) != 0) {
336		log(LOG_WARNING, "%s: can't name node %s\n",
337		    __func__, ifp->if_xname);
338	}
339}
340
341/*
342 * An Ethernet interface is being detached.
343 * REALLY Destroy its node.
344 */
345static void
346ng_ether_detach(struct ifnet *ifp)
347{
348	const node_p node = IFP2NG(ifp);
349	const priv_p priv = NG_NODE_PRIVATE(node);
350
351	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
352	/*
353	 * We can't assume the ifnet is still around when we run shutdown
354	 * So zap it now. XXX We HOPE that anything running at this time
355	 * handles it (as it should in the non netgraph case).
356	 */
357	IFP2NG(ifp) = NULL;
358	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
359	ng_rmnode_self(node);		/* remove all netgraph parts */
360}
361
362/*
363 * Notify graph about link event.
364 * if_link_state_change() has already checked that the state has changed.
365 */
366static void
367ng_ether_link_state(struct ifnet *ifp, int state)
368{
369	const node_p node = IFP2NG(ifp);
370	const priv_p priv = NG_NODE_PRIVATE(node);
371	struct ng_mesg *msg;
372	int cmd, dummy_error = 0;
373
374	if (priv->lower == NULL)
375                return;
376
377	if (state == LINK_STATE_UP)
378		cmd = NGM_LINK_IS_UP;
379	else if (state == LINK_STATE_DOWN)
380		cmd = NGM_LINK_IS_DOWN;
381	else
382		return;
383
384	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
385	if (msg != NULL)
386		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
387}
388
389/******************************************************************
390		    NETGRAPH NODE METHODS
391******************************************************************/
392
393/*
394 * It is not possible or allowable to create a node of this type.
395 * Nodes get created when the interface is attached (or, when
396 * this node type's KLD is loaded).
397 */
398static int
399ng_ether_constructor(node_p node)
400{
401	return (EINVAL);
402}
403
404/*
405 * Check for attaching a new hook.
406 */
407static	int
408ng_ether_newhook(node_p node, hook_p hook, const char *name)
409{
410	const priv_p priv = NG_NODE_PRIVATE(node);
411	hook_p *hookptr;
412
413	/* Divert hook is an alias for lower */
414	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
415		name = NG_ETHER_HOOK_LOWER;
416
417	/* Which hook? */
418	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
419		hookptr = &priv->upper;
420		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
421	} else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
422		hookptr = &priv->lower;
423		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
424	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
425		hookptr = &priv->orphan;
426		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
427	} else
428		return (EINVAL);
429
430	/* Check if already connected (shouldn't be, but doesn't hurt) */
431	if (*hookptr != NULL)
432		return (EISCONN);
433
434	/* Disable hardware checksums while 'upper' hook is connected */
435	if (hookptr == &priv->upper)
436		priv->ifp->if_hwassist = 0;
437
438	/* OK */
439	*hookptr = hook;
440	return (0);
441}
442
443/*
444 * Receive an incoming control message.
445 */
446static int
447ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
448{
449	const priv_p priv = NG_NODE_PRIVATE(node);
450	struct ng_mesg *resp = NULL;
451	int error = 0;
452	struct ng_mesg *msg;
453
454	NGI_GET_MSG(item, msg);
455	switch (msg->header.typecookie) {
456	case NGM_ETHER_COOKIE:
457		switch (msg->header.cmd) {
458		case NGM_ETHER_GET_IFNAME:
459			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
460			if (resp == NULL) {
461				error = ENOMEM;
462				break;
463			}
464			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
465			break;
466		case NGM_ETHER_GET_IFINDEX:
467			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
468			if (resp == NULL) {
469				error = ENOMEM;
470				break;
471			}
472			*((u_int32_t *)resp->data) = priv->ifp->if_index;
473			break;
474		case NGM_ETHER_GET_ENADDR:
475			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
476			if (resp == NULL) {
477				error = ENOMEM;
478				break;
479			}
480			bcopy(IF_LLADDR(priv->ifp),
481			    resp->data, ETHER_ADDR_LEN);
482			break;
483		case NGM_ETHER_SET_ENADDR:
484		    {
485			if (msg->header.arglen != ETHER_ADDR_LEN) {
486				error = EINVAL;
487				break;
488			}
489			error = if_setlladdr(priv->ifp,
490			    (u_char *)msg->data, ETHER_ADDR_LEN);
491			break;
492		    }
493		case NGM_ETHER_GET_PROMISC:
494			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
495			if (resp == NULL) {
496				error = ENOMEM;
497				break;
498			}
499			*((u_int32_t *)resp->data) = priv->promisc;
500			break;
501		case NGM_ETHER_SET_PROMISC:
502		    {
503			u_char want;
504
505			if (msg->header.arglen != sizeof(u_int32_t)) {
506				error = EINVAL;
507				break;
508			}
509			want = !!*((u_int32_t *)msg->data);
510			if (want ^ priv->promisc) {
511				if ((error = ifpromisc(priv->ifp, want)) != 0)
512					break;
513				priv->promisc = want;
514			}
515			break;
516		    }
517		case NGM_ETHER_GET_AUTOSRC:
518			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
519			if (resp == NULL) {
520				error = ENOMEM;
521				break;
522			}
523			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
524			break;
525		case NGM_ETHER_SET_AUTOSRC:
526			if (msg->header.arglen != sizeof(u_int32_t)) {
527				error = EINVAL;
528				break;
529			}
530			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
531			break;
532		case NGM_ETHER_ADD_MULTI:
533		    {
534			struct sockaddr_dl sa_dl;
535			struct ifmultiaddr *ifma;
536
537			if (msg->header.arglen != ETHER_ADDR_LEN) {
538				error = EINVAL;
539				break;
540			}
541			bzero(&sa_dl, sizeof(struct sockaddr_dl));
542			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
543			sa_dl.sdl_family = AF_LINK;
544			sa_dl.sdl_alen = ETHER_ADDR_LEN;
545			bcopy((void *)msg->data, LLADDR(&sa_dl),
546			    ETHER_ADDR_LEN);
547			/*
548			 * Netgraph is only permitted to join groups once
549			 * via the if_addmulti() KPI, because it cannot hold
550			 * struct ifmultiaddr * between calls. It may also
551			 * lose a race while we check if the membership
552			 * already exists.
553			 */
554			IF_ADDR_LOCK(priv->ifp);
555			ifma = if_findmulti(priv->ifp,
556			    (struct sockaddr *)&sa_dl);
557			IF_ADDR_UNLOCK(priv->ifp);
558			if (ifma != NULL) {
559				error = EADDRINUSE;
560			} else {
561				error = if_addmulti(priv->ifp,
562				    (struct sockaddr *)&sa_dl, &ifma);
563			}
564			break;
565		    }
566		case NGM_ETHER_DEL_MULTI:
567		    {
568			struct sockaddr_dl sa_dl;
569
570			if (msg->header.arglen != ETHER_ADDR_LEN) {
571				error = EINVAL;
572				break;
573			}
574			bzero(&sa_dl, sizeof(struct sockaddr_dl));
575			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
576			sa_dl.sdl_family = AF_LINK;
577			sa_dl.sdl_alen = ETHER_ADDR_LEN;
578			bcopy((void *)msg->data, LLADDR(&sa_dl),
579			    ETHER_ADDR_LEN);
580			error = if_delmulti(priv->ifp,
581			    (struct sockaddr *)&sa_dl);
582			break;
583		    }
584		case NGM_ETHER_DETACH:
585			ng_ether_detach(priv->ifp);
586			break;
587		default:
588			error = EINVAL;
589			break;
590		}
591		break;
592	default:
593		error = EINVAL;
594		break;
595	}
596	NG_RESPOND_MSG(error, node, item, resp);
597	NG_FREE_MSG(msg);
598	return (error);
599}
600
601/*
602 * Receive data on a hook.
603 * Since we use per-hook recveive methods this should never be called.
604 */
605static int
606ng_ether_rcvdata(hook_p hook, item_p item)
607{
608	NG_FREE_ITEM(item);
609
610	panic("%s: weird hook", __func__);
611#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
612	return (0);
613#endif
614}
615
616/*
617 * Handle an mbuf received on the "lower" or "orphan" hook.
618 */
619static int
620ng_ether_rcv_lower(hook_p hook, item_p item)
621{
622	struct mbuf *m;
623	const node_p node = NG_HOOK_NODE(hook);
624	const priv_p priv = NG_NODE_PRIVATE(node);
625 	struct ifnet *const ifp = priv->ifp;
626
627	NGI_GET_M(item, m);
628	NG_FREE_ITEM(item);
629
630	/* Check whether interface is ready for packets */
631
632	if (!((ifp->if_flags & IFF_UP) &&
633	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
634		NG_FREE_M(m);
635		return (ENETDOWN);
636	}
637
638	/* Make sure header is fully pulled up */
639	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
640		NG_FREE_M(m);
641		return (EINVAL);
642	}
643	if (m->m_len < sizeof(struct ether_header)
644	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
645		return (ENOBUFS);
646
647	/* Drop in the MAC address if desired */
648	if (priv->autoSrcAddr) {
649
650		/* Make the mbuf writable if it's not already */
651		if (!M_WRITABLE(m)
652		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
653			return (ENOBUFS);
654
655		/* Overwrite source MAC address */
656		bcopy(IF_LLADDR(ifp),
657		    mtod(m, struct ether_header *)->ether_shost,
658		    ETHER_ADDR_LEN);
659	}
660
661	/* Send it on its way */
662	return ether_output_frame(ifp, m);
663}
664
665/*
666 * Handle an mbuf received on the "upper" hook.
667 */
668static int
669ng_ether_rcv_upper(hook_p hook, item_p item)
670{
671	struct mbuf *m;
672	const node_p node = NG_HOOK_NODE(hook);
673	const priv_p priv = NG_NODE_PRIVATE(node);
674	struct ifnet *ifp = priv->ifp;
675
676	NGI_GET_M(item, m);
677	NG_FREE_ITEM(item);
678
679	/* Check length and pull off header */
680	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
681		NG_FREE_M(m);
682		return (EINVAL);
683	}
684	if (m->m_len < sizeof(struct ether_header) &&
685	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
686		return (ENOBUFS);
687
688	m->m_pkthdr.rcvif = ifp;
689
690	/* Pass the packet to the bridge, it may come back to us */
691	if (ifp->if_bridge) {
692		BRIDGE_INPUT(ifp, m);
693		if (m == NULL)
694			return (0);
695	}
696
697	/* Route packet back in */
698	ether_demux(ifp, m);
699	return (0);
700}
701
702/*
703 * Shutdown node. This resets the node but does not remove it
704 * unless the REALLY_DIE flag is set.
705 */
706static int
707ng_ether_shutdown(node_p node)
708{
709	const priv_p priv = NG_NODE_PRIVATE(node);
710
711	if (node->nd_flags & NGF_REALLY_DIE) {
712		/*
713		 * WE came here because the ethernet card is being unloaded,
714		 * so stop being persistant.
715		 * Actually undo all the things we did on creation.
716		 * Assume the ifp has already been freed.
717		 */
718		NG_NODE_SET_PRIVATE(node, NULL);
719		free(priv, M_NETGRAPH);
720		NG_NODE_UNREF(node);	/* free node itself */
721		return (0);
722	}
723	if (priv->promisc) {		/* disable promiscuous mode */
724		(void)ifpromisc(priv->ifp, 0);
725		priv->promisc = 0;
726	}
727	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
728	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
729
730	return (0);
731}
732
733/*
734 * Hook disconnection.
735 */
736static int
737ng_ether_disconnect(hook_p hook)
738{
739	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
740
741	if (hook == priv->upper) {
742		priv->upper = NULL;
743		if (priv->ifp != NULL)		/* restore h/w csum */
744			priv->ifp->if_hwassist = priv->hwassist;
745	} else if (hook == priv->lower)
746		priv->lower = NULL;
747	else if (hook == priv->orphan)
748		priv->orphan = NULL;
749	else
750		panic("%s: weird hook", __func__);
751	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
752	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
753		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
754	return (0);
755}
756
757/******************************************************************
758		    	INITIALIZATION
759******************************************************************/
760
761/*
762 * Handle loading and unloading for this node type.
763 */
764static int
765ng_ether_mod_event(module_t mod, int event, void *data)
766{
767	int error = 0;
768	int s;
769
770	s = splnet();
771	switch (event) {
772	case MOD_LOAD:
773
774		/* Register function hooks */
775		if (ng_ether_attach_p != NULL) {
776			error = EEXIST;
777			break;
778		}
779		ng_ether_attach_p = ng_ether_attach;
780		ng_ether_detach_p = ng_ether_detach;
781		ng_ether_output_p = ng_ether_output;
782		ng_ether_input_p = ng_ether_input;
783		ng_ether_input_orphan_p = ng_ether_input_orphan;
784		ng_ether_link_state_p = ng_ether_link_state;
785
786#ifndef VIMAGE_GLOBALS
787		vnet_mod_register(&vnet_ng_ether_modinfo);
788#else
789		error = ng_ether_iattach(NULL);
790#endif
791		break;
792
793	case MOD_UNLOAD:
794
795		/*
796		 * Note that the base code won't try to unload us until
797		 * all nodes have been removed, and that can't happen
798		 * until all Ethernet interfaces are removed. In any
799		 * case, we know there are no nodes left if the action
800		 * is MOD_UNLOAD, so there's no need to detach any nodes.
801		 */
802
803#ifndef VIMAGE_GLOBALS
804		vnet_mod_deregister(&vnet_ng_ether_modinfo);
805#endif
806
807		/* Unregister function hooks */
808		ng_ether_attach_p = NULL;
809		ng_ether_detach_p = NULL;
810		ng_ether_output_p = NULL;
811		ng_ether_input_p = NULL;
812		ng_ether_input_orphan_p = NULL;
813		ng_ether_link_state_p = NULL;
814		break;
815
816	default:
817		error = EOPNOTSUPP;
818		break;
819	}
820	splx(s);
821	return (error);
822}
823
824static int ng_ether_iattach(const void *unused)
825{
826	INIT_VNET_NET(curvnet);
827	struct ifnet *ifp;
828
829	/* Create nodes for any already-existing Ethernet interfaces. */
830	IFNET_RLOCK();
831	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
832		if (ifp->if_type == IFT_ETHER
833		    || ifp->if_type == IFT_L2VLAN)
834			ng_ether_attach(ifp);
835	}
836	IFNET_RUNLOCK();
837
838	return (0);
839}
840