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