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