xref: /freebsd/sys/netgraph/ng_ether.c (revision a6af156093af25c3a4ab5d467ff4fb7828e1fd33)
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 
69 /* Per-node private data */
70 struct private {
71 	struct ifnet	*ifp;		/* associated interface */
72 	hook_p		upper;		/* upper hook connection */
73 	hook_p		lower;		/* lower hook connection */
74 	hook_p		orphan;		/* orphan hook connection */
75 	u_char		autoSrcAddr;	/* always overwrite source address */
76 	u_char		promisc;	/* promiscuous mode enabled */
77 	u_long		hwassist;	/* hardware checksum capabilities */
78 	u_int		flags;		/* flags e.g. really die */
79 };
80 typedef struct private *priv_p;
81 
82 /* Hook pointers used by if_ethersubr.c to callback to netgraph */
83 extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
84 extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
85 extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
86 extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
87 extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
88 
89 /* Functional hooks called from if_ethersubr.c */
90 static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
91 static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
92 static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
93 static void	ng_ether_attach(struct ifnet *ifp);
94 static void	ng_ether_detach(struct ifnet *ifp);
95 
96 /* Other functions */
97 static int	ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
98 static int	ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
99 
100 /* Netgraph node methods */
101 static ng_constructor_t	ng_ether_constructor;
102 static ng_rcvmsg_t	ng_ether_rcvmsg;
103 static ng_shutdown_t	ng_ether_shutdown;
104 static ng_newhook_t	ng_ether_newhook;
105 static ng_connect_t	ng_ether_connect;
106 static ng_rcvdata_t	ng_ether_rcvdata;
107 static ng_disconnect_t	ng_ether_disconnect;
108 static int		ng_ether_mod_event(module_t mod, int event, void *data);
109 
110 /* List of commands and how to convert arguments to/from ASCII */
111 static const struct ng_cmdlist ng_ether_cmdlist[] = {
112 	{
113 	  NGM_ETHER_COOKIE,
114 	  NGM_ETHER_GET_IFNAME,
115 	  "getifname",
116 	  NULL,
117 	  &ng_parse_string_type
118 	},
119 	{
120 	  NGM_ETHER_COOKIE,
121 	  NGM_ETHER_GET_IFINDEX,
122 	  "getifindex",
123 	  NULL,
124 	  &ng_parse_int32_type
125 	},
126 	{
127 	  NGM_ETHER_COOKIE,
128 	  NGM_ETHER_GET_ENADDR,
129 	  "getenaddr",
130 	  NULL,
131 	  &ng_parse_enaddr_type
132 	},
133 	{
134 	  NGM_ETHER_COOKIE,
135 	  NGM_ETHER_SET_ENADDR,
136 	  "setenaddr",
137 	  &ng_parse_enaddr_type,
138 	  NULL
139 	},
140 	{
141 	  NGM_ETHER_COOKIE,
142 	  NGM_ETHER_GET_PROMISC,
143 	  "getpromisc",
144 	  NULL,
145 	  &ng_parse_int32_type
146 	},
147 	{
148 	  NGM_ETHER_COOKIE,
149 	  NGM_ETHER_SET_PROMISC,
150 	  "setpromisc",
151 	  &ng_parse_int32_type,
152 	  NULL
153 	},
154 	{
155 	  NGM_ETHER_COOKIE,
156 	  NGM_ETHER_GET_AUTOSRC,
157 	  "getautosrc",
158 	  NULL,
159 	  &ng_parse_int32_type
160 	},
161 	{
162 	  NGM_ETHER_COOKIE,
163 	  NGM_ETHER_SET_AUTOSRC,
164 	  "setautosrc",
165 	  &ng_parse_int32_type,
166 	  NULL
167 	},
168 	{ 0 }
169 };
170 
171 static struct ng_type ng_ether_typestruct = {
172 	NG_ABI_VERSION,
173 	NG_ETHER_NODE_TYPE,
174 	ng_ether_mod_event,
175 	ng_ether_constructor,
176 	ng_ether_rcvmsg,
177 	ng_ether_shutdown,
178 	ng_ether_newhook,
179 	NULL,
180 	ng_ether_connect,
181 	ng_ether_rcvdata,
182 	ng_ether_disconnect,
183 	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(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(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 	meta_p meta;
490 
491 	NGI_GET_M(item, m);
492 	NGI_GET_META(item, meta);
493 	NG_FREE_ITEM(item);
494 	if (hook == priv->lower || hook == priv->orphan)
495 		return ng_ether_rcv_lower(node, m, meta);
496 	if (hook == priv->upper)
497 		return ng_ether_rcv_upper(node, m, meta);
498 	panic("%s: weird hook", __func__);
499 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
500 	return NULL;
501 #endif
502 }
503 
504 /*
505  * Handle an mbuf received on the "lower" or "orphan" hook.
506  */
507 static int
508 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
509 {
510 	const priv_p priv = NG_NODE_PRIVATE(node);
511  	struct ifnet *const ifp = priv->ifp;
512 
513 	/* Discard meta info */
514 	NG_FREE_META(meta);
515 
516 	/* Check whether interface is ready for packets */
517 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
518 		NG_FREE_M(m);
519 		return (ENETDOWN);
520 	}
521 
522 	/* Make sure header is fully pulled up */
523 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
524 		NG_FREE_M(m);
525 		return (EINVAL);
526 	}
527 	if (m->m_len < sizeof(struct ether_header)
528 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
529 		return (ENOBUFS);
530 
531 	/* Drop in the MAC address if desired */
532 	if (priv->autoSrcAddr) {
533 
534 		/* Make the mbuf writable if it's not already */
535 		if (!M_WRITABLE(m)
536 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
537 			return (ENOBUFS);
538 
539 		/* Overwrite source MAC address */
540 		bcopy((IFP2AC(ifp))->ac_enaddr,
541 		    mtod(m, struct ether_header *)->ether_shost,
542 		    ETHER_ADDR_LEN);
543 	}
544 
545 	/* Send it on its way */
546 	return ether_output_frame(ifp, m);
547 }
548 
549 /*
550  * Handle an mbuf received on the "upper" hook.
551  */
552 static int
553 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
554 {
555 	const priv_p priv = NG_NODE_PRIVATE(node);
556 
557 	/* Discard meta info */
558 	NG_FREE_META(meta);
559 
560 	m->m_pkthdr.rcvif = priv->ifp;
561 
562 	/* Route packet back in */
563 	ether_demux(priv->ifp, m);
564 	return (0);
565 }
566 
567 /*
568  * Shutdown node. This resets the node but does not remove it
569  * unless the REALLY_DIE flag is set.
570  */
571 static int
572 ng_ether_shutdown(node_p node)
573 {
574 	const priv_p priv = NG_NODE_PRIVATE(node);
575 
576 	if (node->nd_flags & NG_REALLY_DIE) {
577 		/*
578 		 * WE came here because the ethernet card is being unloaded,
579 		 * so stop being persistant.
580 		 * Actually undo all the things we did on creation.
581 		 * Assume the ifp has already been freed.
582 		 */
583 		NG_NODE_SET_PRIVATE(node, NULL);
584 		FREE(priv, M_NETGRAPH);
585 		NG_NODE_UNREF(node);	/* free node itself */
586 		return (0);
587 	}
588 	if (priv->promisc) {		/* disable promiscuous mode */
589 		(void)ifpromisc(priv->ifp, 0);
590 		priv->promisc = 0;
591 	}
592 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
593 	node->nd_flags &= ~NG_INVALID;	/* Signal ng_rmnode we are persisant */
594 	return (0);
595 }
596 
597 /*
598  * Hook disconnection.
599  */
600 static int
601 ng_ether_disconnect(hook_p hook)
602 {
603 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
604 
605 	if (hook == priv->upper) {
606 		priv->upper = NULL;
607 		if (priv->ifp != NULL)		/* restore h/w csum */
608 			priv->ifp->if_hwassist = priv->hwassist;
609 	} else if (hook == priv->lower)
610 		priv->lower = NULL;
611 	else if (hook == priv->orphan)
612 		priv->orphan = NULL;
613 	else
614 		panic("%s: weird hook", __func__);
615 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
616 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
617 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
618 	return (0);
619 }
620 
621 /******************************************************************
622 		    	INITIALIZATION
623 ******************************************************************/
624 
625 /*
626  * Handle loading and unloading for this node type.
627  */
628 static int
629 ng_ether_mod_event(module_t mod, int event, void *data)
630 {
631 	struct ifnet *ifp;
632 	int error = 0;
633 	int s;
634 
635 	s = splnet();
636 	switch (event) {
637 	case MOD_LOAD:
638 
639 		/* Register function hooks */
640 		if (ng_ether_attach_p != NULL) {
641 			error = EEXIST;
642 			break;
643 		}
644 		ng_ether_attach_p = ng_ether_attach;
645 		ng_ether_detach_p = ng_ether_detach;
646 		ng_ether_output_p = ng_ether_output;
647 		ng_ether_input_p = ng_ether_input;
648 		ng_ether_input_orphan_p = ng_ether_input_orphan;
649 
650 		/* Create nodes for any already-existing Ethernet interfaces */
651 		IFNET_RLOCK();
652 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
653 			if (ifp->if_type == IFT_ETHER
654 			    || ifp->if_type == IFT_L2VLAN)
655 				ng_ether_attach(ifp);
656 		}
657 		IFNET_RUNLOCK();
658 		break;
659 
660 	case MOD_UNLOAD:
661 
662 		/*
663 		 * Note that the base code won't try to unload us until
664 		 * all nodes have been removed, and that can't happen
665 		 * until all Ethernet interfaces are removed. In any
666 		 * case, we know there are no nodes left if the action
667 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
668 		 */
669 
670 		/* Unregister function hooks */
671 		ng_ether_attach_p = NULL;
672 		ng_ether_detach_p = NULL;
673 		ng_ether_output_p = NULL;
674 		ng_ether_input_p = NULL;
675 		ng_ether_input_orphan_p = NULL;
676 		break;
677 
678 	default:
679 		error = EOPNOTSUPP;
680 		break;
681 	}
682 	splx(s);
683 	return (error);
684 }
685 
686