xref: /freebsd/sys/netgraph/ng_ether.c (revision 63f9a4cb2684a303e3eb2ffed39c03a2e2b28ae0)
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 	if (node == NULL)		/* no node (why not?), ignore */
303 		return;
304 	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
305 	/*
306 	 * We can't assume the ifnet is still around when we run shutdown
307 	 * So zap it now. XXX We HOPE that anything running at this time
308 	 * handles it (as it should in the non netgraph case).
309 	 */
310 	IFP2NG_SET(ifp, NULL);
311 	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
312 	ng_rmnode_self(node);		/* remove all netgraph parts */
313 }
314 
315 /******************************************************************
316 		    NETGRAPH NODE METHODS
317 ******************************************************************/
318 
319 /*
320  * It is not possible or allowable to create a node of this type.
321  * Nodes get created when the interface is attached (or, when
322  * this node type's KLD is loaded).
323  */
324 static int
325 ng_ether_constructor(node_p node)
326 {
327 	return (EINVAL);
328 }
329 
330 /*
331  * Check for attaching a new hook.
332  */
333 static	int
334 ng_ether_newhook(node_p node, hook_p hook, const char *name)
335 {
336 	const priv_p priv = NG_NODE_PRIVATE(node);
337 	hook_p *hookptr;
338 
339 	/* Divert hook is an alias for lower */
340 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
341 		name = NG_ETHER_HOOK_LOWER;
342 
343 	/* Which hook? */
344 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
345 		hookptr = &priv->upper;
346 	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
347 		hookptr = &priv->lower;
348 	else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
349 		hookptr = &priv->orphan;
350 	else
351 		return (EINVAL);
352 
353 	/* Check if already connected (shouldn't be, but doesn't hurt) */
354 	if (*hookptr != NULL)
355 		return (EISCONN);
356 
357 	/* Disable hardware checksums while 'upper' hook is connected */
358 	if (hookptr == &priv->upper)
359 		priv->ifp->if_hwassist = 0;
360 
361 	/* OK */
362 	*hookptr = hook;
363 	return (0);
364 }
365 
366 /*
367  * Hooks are attached, adjust to force queueing.
368  * We don't really care which hook it is.
369  * they should all be queuing for outgoing data.
370  */
371 static	int
372 ng_ether_connect(hook_p hook)
373 {
374 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
375 	return (0);
376 }
377 
378 /*
379  * Receive an incoming control message.
380  */
381 static int
382 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
383 {
384 	const priv_p priv = NG_NODE_PRIVATE(node);
385 	struct ng_mesg *resp = NULL;
386 	int error = 0;
387 	struct ng_mesg *msg;
388 
389 	NGI_GET_MSG(item, msg);
390 	switch (msg->header.typecookie) {
391 	case NGM_ETHER_COOKIE:
392 		switch (msg->header.cmd) {
393 		case NGM_ETHER_GET_IFNAME:
394 			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
395 			if (resp == NULL) {
396 				error = ENOMEM;
397 				break;
398 			}
399 			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ + 1);
400 			break;
401 		case NGM_ETHER_GET_IFINDEX:
402 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
403 			if (resp == NULL) {
404 				error = ENOMEM;
405 				break;
406 			}
407 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
408 			break;
409 		case NGM_ETHER_GET_ENADDR:
410 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
411 			if (resp == NULL) {
412 				error = ENOMEM;
413 				break;
414 			}
415 			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
416 			    resp->data, ETHER_ADDR_LEN);
417 			break;
418 		case NGM_ETHER_SET_ENADDR:
419 		    {
420 			if (msg->header.arglen != ETHER_ADDR_LEN) {
421 				error = EINVAL;
422 				break;
423 			}
424 			error = if_setlladdr(priv->ifp,
425 			    (u_char *)msg->data, ETHER_ADDR_LEN);
426 			break;
427 		    }
428 		case NGM_ETHER_GET_PROMISC:
429 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
430 			if (resp == NULL) {
431 				error = ENOMEM;
432 				break;
433 			}
434 			*((u_int32_t *)resp->data) = priv->promisc;
435 			break;
436 		case NGM_ETHER_SET_PROMISC:
437 		    {
438 			u_char want;
439 
440 			if (msg->header.arglen != sizeof(u_int32_t)) {
441 				error = EINVAL;
442 				break;
443 			}
444 			want = !!*((u_int32_t *)msg->data);
445 			if (want ^ priv->promisc) {
446 				if ((error = ifpromisc(priv->ifp, want)) != 0)
447 					break;
448 				priv->promisc = want;
449 			}
450 			break;
451 		    }
452 		case NGM_ETHER_GET_AUTOSRC:
453 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
454 			if (resp == NULL) {
455 				error = ENOMEM;
456 				break;
457 			}
458 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
459 			break;
460 		case NGM_ETHER_SET_AUTOSRC:
461 			if (msg->header.arglen != sizeof(u_int32_t)) {
462 				error = EINVAL;
463 				break;
464 			}
465 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
466 			break;
467 		default:
468 			error = EINVAL;
469 			break;
470 		}
471 		break;
472 	default:
473 		error = EINVAL;
474 		break;
475 	}
476 	NG_RESPOND_MSG(error, node, item, resp);
477 	NG_FREE_MSG(msg);
478 	return (error);
479 }
480 
481 /*
482  * Receive data on a hook.
483  */
484 static int
485 ng_ether_rcvdata(hook_p hook, item_p item)
486 {
487 	const node_p node = NG_HOOK_NODE(hook);
488 	const priv_p priv = NG_NODE_PRIVATE(node);
489 	struct mbuf *m;
490 
491 	NGI_GET_M(item, m);
492 	NG_FREE_ITEM(item);
493 
494 	if (hook == priv->lower || hook == priv->orphan)
495 		return ng_ether_rcv_lower(node, m);
496 	if (hook == priv->upper)
497 		return ng_ether_rcv_upper(node, m);
498 	panic("%s: weird hook", __func__);
499 #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
500 	return (0);
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)
509 {
510 	const priv_p priv = NG_NODE_PRIVATE(node);
511  	struct ifnet *const ifp = priv->ifp;
512 
513 	/* Check whether interface is ready for packets */
514 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
515 		NG_FREE_M(m);
516 		return (ENETDOWN);
517 	}
518 
519 	/* Make sure header is fully pulled up */
520 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
521 		NG_FREE_M(m);
522 		return (EINVAL);
523 	}
524 	if (m->m_len < sizeof(struct ether_header)
525 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
526 		return (ENOBUFS);
527 
528 	/* Drop in the MAC address if desired */
529 	if (priv->autoSrcAddr) {
530 
531 		/* Make the mbuf writable if it's not already */
532 		if (!M_WRITABLE(m)
533 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
534 			return (ENOBUFS);
535 
536 		/* Overwrite source MAC address */
537 		bcopy((IFP2AC(ifp))->ac_enaddr,
538 		    mtod(m, struct ether_header *)->ether_shost,
539 		    ETHER_ADDR_LEN);
540 	}
541 
542 	/* Send it on its way */
543 	return ether_output_frame(ifp, m);
544 }
545 
546 /*
547  * Handle an mbuf received on the "upper" hook.
548  */
549 static int
550 ng_ether_rcv_upper(node_p node, struct mbuf *m)
551 {
552 	const priv_p priv = NG_NODE_PRIVATE(node);
553 
554 	m->m_pkthdr.rcvif = priv->ifp;
555 
556 	if (BDG_ACTIVE(priv->ifp) )
557 		if ((m = bridge_in_ptr(priv->ifp, m)) == NULL)
558 			return (0);
559 
560 	/* Route packet back in */
561 	ether_demux(priv->ifp, m);
562 	return (0);
563 }
564 
565 /*
566  * Shutdown node. This resets the node but does not remove it
567  * unless the REALLY_DIE flag is set.
568  */
569 static int
570 ng_ether_shutdown(node_p node)
571 {
572 	const priv_p priv = NG_NODE_PRIVATE(node);
573 
574 	if (node->nd_flags & NGF_REALLY_DIE) {
575 		/*
576 		 * WE came here because the ethernet card is being unloaded,
577 		 * so stop being persistant.
578 		 * Actually undo all the things we did on creation.
579 		 * Assume the ifp has already been freed.
580 		 */
581 		NG_NODE_SET_PRIVATE(node, NULL);
582 		FREE(priv, M_NETGRAPH);
583 		NG_NODE_UNREF(node);	/* free node itself */
584 		return (0);
585 	}
586 	if (priv->promisc) {		/* disable promiscuous mode */
587 		(void)ifpromisc(priv->ifp, 0);
588 		priv->promisc = 0;
589 	}
590 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
591 	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
592 
593 	return (0);
594 }
595 
596 /*
597  * Hook disconnection.
598  */
599 static int
600 ng_ether_disconnect(hook_p hook)
601 {
602 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
603 
604 	if (hook == priv->upper) {
605 		priv->upper = NULL;
606 		if (priv->ifp != NULL)		/* restore h/w csum */
607 			priv->ifp->if_hwassist = priv->hwassist;
608 	} else if (hook == priv->lower)
609 		priv->lower = NULL;
610 	else if (hook == priv->orphan)
611 		priv->orphan = NULL;
612 	else
613 		panic("%s: weird hook", __func__);
614 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
615 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
616 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
617 	return (0);
618 }
619 
620 /******************************************************************
621 		    	INITIALIZATION
622 ******************************************************************/
623 
624 /*
625  * Handle loading and unloading for this node type.
626  */
627 static int
628 ng_ether_mod_event(module_t mod, int event, void *data)
629 {
630 	struct ifnet *ifp;
631 	int error = 0;
632 	int s;
633 
634 	s = splnet();
635 	switch (event) {
636 	case MOD_LOAD:
637 
638 		/* Register function hooks */
639 		if (ng_ether_attach_p != NULL) {
640 			error = EEXIST;
641 			break;
642 		}
643 		ng_ether_attach_p = ng_ether_attach;
644 		ng_ether_detach_p = ng_ether_detach;
645 		ng_ether_output_p = ng_ether_output;
646 		ng_ether_input_p = ng_ether_input;
647 		ng_ether_input_orphan_p = ng_ether_input_orphan;
648 
649 		/* Create nodes for any already-existing Ethernet interfaces */
650 		IFNET_RLOCK();
651 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
652 			if (ifp->if_type == IFT_ETHER
653 			    || ifp->if_type == IFT_L2VLAN)
654 				ng_ether_attach(ifp);
655 		}
656 		IFNET_RUNLOCK();
657 		break;
658 
659 	case MOD_UNLOAD:
660 
661 		/*
662 		 * Note that the base code won't try to unload us until
663 		 * all nodes have been removed, and that can't happen
664 		 * until all Ethernet interfaces are removed. In any
665 		 * case, we know there are no nodes left if the action
666 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
667 		 */
668 
669 		/* Unregister function hooks */
670 		ng_ether_attach_p = NULL;
671 		ng_ether_detach_p = NULL;
672 		ng_ether_output_p = NULL;
673 		ng_ether_input_p = NULL;
674 		ng_ether_input_orphan_p = NULL;
675 		break;
676 
677 	default:
678 		error = EOPNOTSUPP;
679 		break;
680 	}
681 	splx(s);
682 	return (error);
683 }
684 
685