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