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