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