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