ng_ether.c revision 141720
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 141720 2005-02-12 11:14:25Z ru $
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/bridge.h>
59#include <net/if.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 *)((struct arpcom *)(ifp))->ac_netgraph)
71#define IFP2NG_SET(ifp, val)	(((struct arpcom *)(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	{ 0 }
175};
176
177static struct ng_type ng_ether_typestruct = {
178	.version =	NG_ABI_VERSION,
179	.name =		NG_ETHER_NODE_TYPE,
180	.mod_event =	ng_ether_mod_event,
181	.constructor =	ng_ether_constructor,
182	.rcvmsg =	ng_ether_rcvmsg,
183	.shutdown =	ng_ether_shutdown,
184	.newhook =	ng_ether_newhook,
185	.connect =	ng_ether_connect,
186	.rcvdata =	ng_ether_rcvdata,
187	.disconnect =	ng_ether_disconnect,
188	.cmdlist =	ng_ether_cmdlist,
189};
190NETGRAPH_INIT(ether, &ng_ether_typestruct);
191
192/******************************************************************
193		    ETHERNET FUNCTION HOOKS
194******************************************************************/
195
196/*
197 * Handle a packet that has come in on an interface. We get to
198 * look at it here before any upper layer protocols do.
199 *
200 * NOTE: this function will get called at splimp()
201 */
202static void
203ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
204{
205	const node_p node = IFP2NG(ifp);
206	const priv_p priv = NG_NODE_PRIVATE(node);
207	int error;
208
209	/* If "lower" hook not connected, let packet continue */
210	if (priv->lower == NULL)
211		return;
212	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
213}
214
215/*
216 * Handle a packet that has come in on an interface, and which
217 * does not match any of our known protocols (an ``orphan'').
218 *
219 * NOTE: this function will get called at splimp()
220 */
221static void
222ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
223{
224	const node_p node = IFP2NG(ifp);
225	const priv_p priv = NG_NODE_PRIVATE(node);
226	int error;
227
228	/* If "orphan" hook not connected, discard packet */
229	if (priv->orphan == NULL) {
230		m_freem(m);
231		return;
232	}
233	NG_SEND_DATA_ONLY(error, priv->orphan, m);
234}
235
236/*
237 * Handle a packet that is going out on an interface.
238 * The Ethernet header is already attached to the mbuf.
239 */
240static int
241ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
242{
243	const node_p node = IFP2NG(ifp);
244	const priv_p priv = NG_NODE_PRIVATE(node);
245	int error = 0;
246
247	/* If "upper" hook not connected, let packet continue */
248	if (priv->upper == NULL)
249		return (0);
250
251	/* Send it out "upper" hook */
252	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
253	return (error);
254}
255
256/*
257 * A new Ethernet interface has been attached.
258 * Create a new node for it, etc.
259 */
260static void
261ng_ether_attach(struct ifnet *ifp)
262{
263	priv_p priv;
264	node_p node;
265
266	/* Create node */
267	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
268	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
269		log(LOG_ERR, "%s: can't %s for %s\n",
270		    __func__, "create node", ifp->if_xname);
271		return;
272	}
273
274	/* Allocate private data */
275	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
276	if (priv == NULL) {
277		log(LOG_ERR, "%s: can't %s for %s\n",
278		    __func__, "allocate memory", ifp->if_xname);
279		NG_NODE_UNREF(node);
280		return;
281	}
282	NG_NODE_SET_PRIVATE(node, priv);
283	priv->ifp = ifp;
284	IFP2NG_SET(ifp, node);
285	priv->autoSrcAddr = 1;
286	priv->hwassist = ifp->if_hwassist;
287
288	/* Try to give the node the same name as the interface */
289	if (ng_name_node(node, ifp->if_xname) != 0) {
290		log(LOG_WARNING, "%s: can't name node %s\n",
291		    __func__, ifp->if_xname);
292	}
293}
294
295/*
296 * An Ethernet interface is being detached.
297 * REALLY Destroy its node.
298 */
299static void
300ng_ether_detach(struct ifnet *ifp)
301{
302	const node_p node = IFP2NG(ifp);
303	const priv_p priv = NG_NODE_PRIVATE(node);
304
305	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
306	/*
307	 * We can't assume the ifnet is still around when we run shutdown
308	 * So zap it now. XXX We HOPE that anything running at this time
309	 * handles it (as it should in the non netgraph case).
310	 */
311	IFP2NG_SET(ifp, NULL);
312	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
313	ng_rmnode_self(node);		/* remove all netgraph parts */
314}
315
316/*
317 * Notify graph about link event.
318 * if_link_state_change() has already checked that the state has changed.
319 */
320static void
321ng_ether_link_state(struct ifnet *ifp, int state)
322{
323	const node_p node = IFP2NG(ifp);
324	const priv_p priv = NG_NODE_PRIVATE(node);
325	struct ng_mesg *msg;
326	int cmd, dummy_error = 0;
327
328	if (priv->lower == NULL)
329                return;
330
331	if (state == LINK_STATE_UP)
332		cmd = NGM_LINK_IS_UP;
333	else if (state == LINK_STATE_DOWN)
334		cmd = NGM_LINK_IS_DOWN;
335	else
336		return;
337
338	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
339	if (msg != NULL)
340		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
341}
342
343/******************************************************************
344		    NETGRAPH NODE METHODS
345******************************************************************/
346
347/*
348 * It is not possible or allowable to create a node of this type.
349 * Nodes get created when the interface is attached (or, when
350 * this node type's KLD is loaded).
351 */
352static int
353ng_ether_constructor(node_p node)
354{
355	return (EINVAL);
356}
357
358/*
359 * Check for attaching a new hook.
360 */
361static	int
362ng_ether_newhook(node_p node, hook_p hook, const char *name)
363{
364	const priv_p priv = NG_NODE_PRIVATE(node);
365	hook_p *hookptr;
366
367	/* Divert hook is an alias for lower */
368	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
369		name = NG_ETHER_HOOK_LOWER;
370
371	/* Which hook? */
372	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
373		hookptr = &priv->upper;
374	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
375		hookptr = &priv->lower;
376	else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
377		hookptr = &priv->orphan;
378	else
379		return (EINVAL);
380
381	/* Check if already connected (shouldn't be, but doesn't hurt) */
382	if (*hookptr != NULL)
383		return (EISCONN);
384
385	/* Disable hardware checksums while 'upper' hook is connected */
386	if (hookptr == &priv->upper)
387		priv->ifp->if_hwassist = 0;
388
389	/* OK */
390	*hookptr = hook;
391	return (0);
392}
393
394/*
395 * Hooks are attached, adjust to force queueing.
396 * We don't really care which hook it is.
397 * they should all be queuing for outgoing data.
398 */
399static	int
400ng_ether_connect(hook_p hook)
401{
402	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
403	return (0);
404}
405
406/*
407 * Receive an incoming control message.
408 */
409static int
410ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
411{
412	const priv_p priv = NG_NODE_PRIVATE(node);
413	struct ng_mesg *resp = NULL;
414	int error = 0;
415	struct ng_mesg *msg;
416
417	NGI_GET_MSG(item, msg);
418	switch (msg->header.typecookie) {
419	case NGM_ETHER_COOKIE:
420		switch (msg->header.cmd) {
421		case NGM_ETHER_GET_IFNAME:
422			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
423			if (resp == NULL) {
424				error = ENOMEM;
425				break;
426			}
427			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
428			break;
429		case NGM_ETHER_GET_IFINDEX:
430			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
431			if (resp == NULL) {
432				error = ENOMEM;
433				break;
434			}
435			*((u_int32_t *)resp->data) = priv->ifp->if_index;
436			break;
437		case NGM_ETHER_GET_ENADDR:
438			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
439			if (resp == NULL) {
440				error = ENOMEM;
441				break;
442			}
443			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
444			    resp->data, ETHER_ADDR_LEN);
445			break;
446		case NGM_ETHER_SET_ENADDR:
447		    {
448			if (msg->header.arglen != ETHER_ADDR_LEN) {
449				error = EINVAL;
450				break;
451			}
452			error = if_setlladdr(priv->ifp,
453			    (u_char *)msg->data, ETHER_ADDR_LEN);
454			break;
455		    }
456		case NGM_ETHER_GET_PROMISC:
457			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
458			if (resp == NULL) {
459				error = ENOMEM;
460				break;
461			}
462			*((u_int32_t *)resp->data) = priv->promisc;
463			break;
464		case NGM_ETHER_SET_PROMISC:
465		    {
466			u_char want;
467
468			if (msg->header.arglen != sizeof(u_int32_t)) {
469				error = EINVAL;
470				break;
471			}
472			want = !!*((u_int32_t *)msg->data);
473			if (want ^ priv->promisc) {
474				if ((error = ifpromisc(priv->ifp, want)) != 0)
475					break;
476				priv->promisc = want;
477			}
478			break;
479		    }
480		case NGM_ETHER_GET_AUTOSRC:
481			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
482			if (resp == NULL) {
483				error = ENOMEM;
484				break;
485			}
486			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
487			break;
488		case NGM_ETHER_SET_AUTOSRC:
489			if (msg->header.arglen != sizeof(u_int32_t)) {
490				error = EINVAL;
491				break;
492			}
493			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
494			break;
495		default:
496			error = EINVAL;
497			break;
498		}
499		break;
500	default:
501		error = EINVAL;
502		break;
503	}
504	NG_RESPOND_MSG(error, node, item, resp);
505	NG_FREE_MSG(msg);
506	return (error);
507}
508
509/*
510 * Receive data on a hook.
511 */
512static int
513ng_ether_rcvdata(hook_p hook, item_p item)
514{
515	const node_p node = NG_HOOK_NODE(hook);
516	const priv_p priv = NG_NODE_PRIVATE(node);
517	struct mbuf *m;
518
519	NGI_GET_M(item, m);
520	NG_FREE_ITEM(item);
521
522	if (hook == priv->lower || hook == priv->orphan)
523		return ng_ether_rcv_lower(node, m);
524	if (hook == priv->upper)
525		return ng_ether_rcv_upper(node, m);
526	panic("%s: weird hook", __func__);
527#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
528	return (0);
529#endif
530}
531
532/*
533 * Handle an mbuf received on the "lower" or "orphan" hook.
534 */
535static int
536ng_ether_rcv_lower(node_p node, struct mbuf *m)
537{
538	const priv_p priv = NG_NODE_PRIVATE(node);
539 	struct ifnet *const ifp = priv->ifp;
540
541	/* Check whether interface is ready for packets */
542	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
543		NG_FREE_M(m);
544		return (ENETDOWN);
545	}
546
547	/* Make sure header is fully pulled up */
548	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
549		NG_FREE_M(m);
550		return (EINVAL);
551	}
552	if (m->m_len < sizeof(struct ether_header)
553	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
554		return (ENOBUFS);
555
556	/* Drop in the MAC address if desired */
557	if (priv->autoSrcAddr) {
558
559		/* Make the mbuf writable if it's not already */
560		if (!M_WRITABLE(m)
561		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
562			return (ENOBUFS);
563
564		/* Overwrite source MAC address */
565		bcopy((IFP2AC(ifp))->ac_enaddr,
566		    mtod(m, struct ether_header *)->ether_shost,
567		    ETHER_ADDR_LEN);
568	}
569
570	/* Send it on its way */
571	return ether_output_frame(ifp, m);
572}
573
574/*
575 * Handle an mbuf received on the "upper" hook.
576 */
577static int
578ng_ether_rcv_upper(node_p node, struct mbuf *m)
579{
580	const priv_p priv = NG_NODE_PRIVATE(node);
581
582	m->m_pkthdr.rcvif = priv->ifp;
583
584	if (BDG_ACTIVE(priv->ifp) )
585		if ((m = bridge_in_ptr(priv->ifp, m)) == NULL)
586			return (0);
587
588	/* Route packet back in */
589	ether_demux(priv->ifp, m);
590	return (0);
591}
592
593/*
594 * Shutdown node. This resets the node but does not remove it
595 * unless the REALLY_DIE flag is set.
596 */
597static int
598ng_ether_shutdown(node_p node)
599{
600	const priv_p priv = NG_NODE_PRIVATE(node);
601
602	if (node->nd_flags & NGF_REALLY_DIE) {
603		/*
604		 * WE came here because the ethernet card is being unloaded,
605		 * so stop being persistant.
606		 * Actually undo all the things we did on creation.
607		 * Assume the ifp has already been freed.
608		 */
609		NG_NODE_SET_PRIVATE(node, NULL);
610		FREE(priv, M_NETGRAPH);
611		NG_NODE_UNREF(node);	/* free node itself */
612		return (0);
613	}
614	if (priv->promisc) {		/* disable promiscuous mode */
615		(void)ifpromisc(priv->ifp, 0);
616		priv->promisc = 0;
617	}
618	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
619	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
620
621	return (0);
622}
623
624/*
625 * Hook disconnection.
626 */
627static int
628ng_ether_disconnect(hook_p hook)
629{
630	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
631
632	if (hook == priv->upper) {
633		priv->upper = NULL;
634		if (priv->ifp != NULL)		/* restore h/w csum */
635			priv->ifp->if_hwassist = priv->hwassist;
636	} else if (hook == priv->lower)
637		priv->lower = NULL;
638	else if (hook == priv->orphan)
639		priv->orphan = NULL;
640	else
641		panic("%s: weird hook", __func__);
642	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
643	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
644		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
645	return (0);
646}
647
648/******************************************************************
649		    	INITIALIZATION
650******************************************************************/
651
652/*
653 * Handle loading and unloading for this node type.
654 */
655static int
656ng_ether_mod_event(module_t mod, int event, void *data)
657{
658	struct ifnet *ifp;
659	int error = 0;
660	int s;
661
662	s = splnet();
663	switch (event) {
664	case MOD_LOAD:
665
666		/* Register function hooks */
667		if (ng_ether_attach_p != NULL) {
668			error = EEXIST;
669			break;
670		}
671		ng_ether_attach_p = ng_ether_attach;
672		ng_ether_detach_p = ng_ether_detach;
673		ng_ether_output_p = ng_ether_output;
674		ng_ether_input_p = ng_ether_input;
675		ng_ether_input_orphan_p = ng_ether_input_orphan;
676		ng_ether_link_state_p = ng_ether_link_state;
677
678		/* Create nodes for any already-existing Ethernet interfaces */
679		IFNET_RLOCK();
680		TAILQ_FOREACH(ifp, &ifnet, if_link) {
681			if (ifp->if_type == IFT_ETHER
682			    || ifp->if_type == IFT_L2VLAN)
683				ng_ether_attach(ifp);
684		}
685		IFNET_RUNLOCK();
686		break;
687
688	case MOD_UNLOAD:
689
690		/*
691		 * Note that the base code won't try to unload us until
692		 * all nodes have been removed, and that can't happen
693		 * until all Ethernet interfaces are removed. In any
694		 * case, we know there are no nodes left if the action
695		 * is MOD_UNLOAD, so there's no need to detach any nodes.
696		 */
697
698		/* Unregister function hooks */
699		ng_ether_attach_p = NULL;
700		ng_ether_detach_p = NULL;
701		ng_ether_output_p = NULL;
702		ng_ether_input_p = NULL;
703		ng_ether_input_orphan_p = NULL;
704		ng_ether_link_state_p = NULL;
705		break;
706
707	default:
708		error = EOPNOTSUPP;
709		break;
710	}
711	splx(s);
712	return (error);
713}
714
715