xref: /freebsd/sys/netgraph/ng_ether.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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 IFP2AC(IFP)  ((struct arpcom *)IFP)
68 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
69 
70 /* Per-node private data */
71 struct private {
72 	struct ifnet	*ifp;		/* associated interface */
73 	hook_p		upper;		/* upper hook connection */
74 	hook_p		lower;		/* lower OR orphan hook connection */
75 	u_char		lowerOrphan;	/* whether lower is lower or orphan */
76 	u_char		autoSrcAddr;	/* always overwrite source address */
77 	u_char		promisc;	/* promiscuous mode enabled */
78 	u_long		hwassist;	/* hardware checksum capabilities */
79 	u_int		flags;		/* flags e.g. really die */
80 };
81 typedef struct private *priv_p;
82 
83 /* Functional hooks called from if_ethersubr.c */
84 static void	ng_ether_input(struct ifnet *ifp,
85 		    struct mbuf **mp, struct ether_header *eh);
86 static void	ng_ether_input_orphan(struct ifnet *ifp,
87 		    struct mbuf *m, struct ether_header *eh);
88 static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
89 static void	ng_ether_attach(struct ifnet *ifp);
90 static void	ng_ether_detach(struct ifnet *ifp);
91 
92 /* Other functions */
93 static void	ng_ether_input2(node_p node,
94 		    struct mbuf **mp, struct ether_header *eh);
95 static int	ng_ether_glueback_header(struct mbuf **mp,
96 			struct ether_header *eh);
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 /* Parse type for an Ethernet address */
111 static ng_parse_t	ng_enaddr_parse;
112 static ng_unparse_t	ng_enaddr_unparse;
113 const struct ng_parse_type ng_ether_enaddr_type = {
114 	NULL,
115 	NULL,
116 	NULL,
117 	ng_enaddr_parse,
118 	ng_enaddr_unparse,
119 	NULL,			/* no such thing as a "default" EN address */
120 	0
121 };
122 
123 /* List of commands and how to convert arguments to/from ASCII */
124 static const struct ng_cmdlist ng_ether_cmdlist[] = {
125 	{
126 	  NGM_ETHER_COOKIE,
127 	  NGM_ETHER_GET_IFNAME,
128 	  "getifname",
129 	  NULL,
130 	  &ng_parse_string_type
131 	},
132 	{
133 	  NGM_ETHER_COOKIE,
134 	  NGM_ETHER_GET_IFINDEX,
135 	  "getifindex",
136 	  NULL,
137 	  &ng_parse_int32_type
138 	},
139 	{
140 	  NGM_ETHER_COOKIE,
141 	  NGM_ETHER_GET_ENADDR,
142 	  "getenaddr",
143 	  NULL,
144 	  &ng_ether_enaddr_type
145 	},
146 	{
147 	  NGM_ETHER_COOKIE,
148 	  NGM_ETHER_SET_ENADDR,
149 	  "setenaddr",
150 	  &ng_ether_enaddr_type,
151 	  NULL
152 	},
153 	{
154 	  NGM_ETHER_COOKIE,
155 	  NGM_ETHER_GET_PROMISC,
156 	  "getpromisc",
157 	  NULL,
158 	  &ng_parse_int32_type
159 	},
160 	{
161 	  NGM_ETHER_COOKIE,
162 	  NGM_ETHER_SET_PROMISC,
163 	  "setpromisc",
164 	  &ng_parse_int32_type,
165 	  NULL
166 	},
167 	{
168 	  NGM_ETHER_COOKIE,
169 	  NGM_ETHER_GET_AUTOSRC,
170 	  "getautosrc",
171 	  NULL,
172 	  &ng_parse_int32_type
173 	},
174 	{
175 	  NGM_ETHER_COOKIE,
176 	  NGM_ETHER_SET_AUTOSRC,
177 	  "setautosrc",
178 	  &ng_parse_int32_type,
179 	  NULL
180 	},
181 	{ 0 }
182 };
183 
184 static struct ng_type ng_ether_typestruct = {
185 	NG_ABI_VERSION,
186 	NG_ETHER_NODE_TYPE,
187 	ng_ether_mod_event,
188 	ng_ether_constructor,
189 	ng_ether_rcvmsg,
190 	ng_ether_shutdown,
191 	ng_ether_newhook,
192 	NULL,
193 	ng_ether_connect,
194 	ng_ether_rcvdata,
195 	ng_ether_disconnect,
196 	ng_ether_cmdlist,
197 };
198 MODULE_VERSION(ng_ether, 1);
199 NETGRAPH_INIT(ether, &ng_ether_typestruct);
200 
201 /******************************************************************
202 		    ETHERNET FUNCTION HOOKS
203 ******************************************************************/
204 
205 /*
206  * Handle a packet that has come in on an interface. We get to
207  * look at it here before any upper layer protocols do.
208  *
209  * NOTE: this function will get called at splimp()
210  */
211 static void
212 ng_ether_input(struct ifnet *ifp,
213 	struct mbuf **mp, struct ether_header *eh)
214 {
215 	const node_p node = IFP2NG(ifp);
216 	const priv_p priv = NG_NODE_PRIVATE(node);
217 
218 	/* If "lower" hook not connected, let packet continue */
219 	if (priv->lower == NULL || priv->lowerOrphan)
220 		return;
221 	ng_ether_input2(node, mp, eh);
222 }
223 
224 /*
225  * Handle a packet that has come in on an interface, and which
226  * does not match any of our known protocols (an ``orphan'').
227  *
228  * NOTE: this function will get called at splimp()
229  */
230 static void
231 ng_ether_input_orphan(struct ifnet *ifp,
232 	struct mbuf *m, struct ether_header *eh)
233 {
234 	const node_p node = IFP2NG(ifp);
235 	const priv_p priv = NG_NODE_PRIVATE(node);
236 
237 	/* If "orphan" hook not connected, let packet continue */
238 	if (priv->lower == NULL || !priv->lowerOrphan) {
239 		m_freem(m);
240 		return;
241 	}
242 	ng_ether_input2(node, &m, eh);
243 	if (m != NULL)
244 		m_freem(m);
245 }
246 
247 /*
248  * Handle a packet that has come in on an ethernet interface.
249  * The Ethernet header has already been detached from the mbuf,
250  * so we have to put it back.
251  *
252  * NOTE: this function will get called at splimp()
253  */
254 static void
255 ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
256 {
257 	const priv_p priv = NG_NODE_PRIVATE(node);
258 	int error;
259 
260 	/* Glue Ethernet header back on */
261 	if ((error = ng_ether_glueback_header(mp, eh)) != 0)
262 		return;
263 
264 	/* Send out lower/orphan hook */
265 	NG_SEND_DATA_ONLY(error, priv->lower, *mp);
266 	*mp = NULL;
267 }
268 
269 /*
270  * Handle a packet that is going out on an interface.
271  * The Ethernet header is already attached to the mbuf.
272  */
273 static int
274 ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
275 {
276 	const node_p node = IFP2NG(ifp);
277 	const priv_p priv = NG_NODE_PRIVATE(node);
278 	int error = 0;
279 
280 	/* If "upper" hook not connected, let packet continue */
281 	if (priv->upper == NULL)
282 		return (0);
283 
284 	/* Send it out "upper" hook */
285 	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
286 	return (error);
287 }
288 
289 /*
290  * A new Ethernet interface has been attached.
291  * Create a new node for it, etc.
292  */
293 static void
294 ng_ether_attach(struct ifnet *ifp)
295 {
296 	char name[IFNAMSIZ + 1];
297 	priv_p priv;
298 	node_p node;
299 
300 	/* Create node */
301 	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
302 	snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
303 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
304 		log(LOG_ERR, "%s: can't %s for %s\n",
305 		    __func__, "create node", name);
306 		return;
307 	}
308 
309 	/* Allocate private data */
310 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
311 	if (priv == NULL) {
312 		log(LOG_ERR, "%s: can't %s for %s\n",
313 		    __func__, "allocate memory", name);
314 		NG_NODE_UNREF(node);
315 		return;
316 	}
317 	NG_NODE_SET_PRIVATE(node, priv);
318 	priv->ifp = ifp;
319 	IFP2NG(ifp) = node;
320 	priv->autoSrcAddr = 1;
321 	priv->hwassist = ifp->if_hwassist;
322 
323 	/* Try to give the node the same name as the interface */
324 	if (ng_name_node(node, name) != 0) {
325 		log(LOG_WARNING, "%s: can't name node %s\n",
326 		    __func__, name);
327 	}
328 }
329 
330 /*
331  * An Ethernet interface is being detached.
332  * REALLY Destroy its node.
333  */
334 static void
335 ng_ether_detach(struct ifnet *ifp)
336 {
337 	const node_p node = IFP2NG(ifp);
338 	const priv_p priv = NG_NODE_PRIVATE(node);
339 
340 	if (node == NULL)		/* no node (why not?), ignore */
341 		return;
342 	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
343 	/*
344 	 * We can't assume the ifnet is still around when we run shutdown
345 	 * So zap it now. XXX We HOPE that anything running at this time
346 	 * handles it (as it should in the non netgraph case).
347 	 */
348 	IFP2NG(ifp) = NULL;
349 	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
350 	ng_rmnode_self(node);		/* remove all netgraph parts */
351 }
352 
353 /*
354  * Optimization for gluing the Ethernet header back onto
355  * the front of an incoming packet.
356  */
357 static int
358 ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
359 {
360 	struct mbuf *m = *mp;
361 	uintfptr_t room;
362 	int error = 0;
363 
364 	/*
365 	 * Possibly the header is already on the front.
366 	 * If this is the case so just move the markers back
367 	 * to re-include it. We lucked out.
368 	 * This allows us to avoid a yucky m_pullup
369 	 * in later nodes if it works.
370 	 */
371 	if (eh == mtod(m, struct ether_header *) - 1) {
372 		m->m_len += sizeof(*eh);
373 		m->m_data -= sizeof(*eh);
374 		m->m_pkthdr.len += sizeof(*eh);
375 		goto done;
376 	}
377 
378 	/*
379 	 * Alternatively there may be room even though
380 	 * it is stored somewhere else. If so, copy it in.
381 	 * This only safe because we KNOW that this packet has
382 	 * just been generated by an ethernet card, so there are
383 	 * no aliases to the buffer (not so for outgoing packets).
384 	 * Nearly all ethernet cards will end up producing mbufs
385 	 * that fall into these cases. So we are not optimizing
386 	 * contorted cases.
387 	 */
388 	if ((m->m_flags & M_EXT) != 0) {
389 		room = mtod(m, caddr_t) - m->m_ext.ext_buf;
390 		if (room > m->m_ext.ext_size)	/* garbage, fail immediately */
391 			room = 0;
392 	} else
393 		room = mtod(m, caddr_t) - m->m_pktdat;
394 
395 	/*
396 	 * If we have room, just copy it and adjust
397 	 */
398 	if (room >= sizeof(*eh)) {
399 		m->m_len += sizeof(*eh);
400 		m->m_data -= sizeof(*eh);
401 		m->m_pkthdr.len += sizeof(*eh);
402 		goto copy;
403 	}
404 
405 	/*
406 	 * Doing anything more is likely to get more
407 	 * expensive than it's worth..
408 	 * it's probable that everything else is in one
409 	 * big lump. The next node will do an m_pullup()
410 	 * for exactly the amount of data it needs and
411 	 * hopefully everything after that will not
412 	 * need one. So let's just use M_PREPEND.
413 	 */
414 	M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
415 	if (m == NULL) {
416 		error = ENOBUFS;
417 		goto done;
418 	}
419 
420 copy:
421 	/* Copy header and return (possibly new) mbuf */
422 	bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
423 done:
424 	*mp = m;
425 	return error;
426 }
427 
428 /******************************************************************
429 		    NETGRAPH NODE METHODS
430 ******************************************************************/
431 
432 /*
433  * It is not possible or allowable to create a node of this type.
434  * Nodes get created when the interface is attached (or, when
435  * this node type's KLD is loaded).
436  */
437 static int
438 ng_ether_constructor(node_p node)
439 {
440 	return (EINVAL);
441 }
442 
443 /*
444  * Check for attaching a new hook.
445  */
446 static	int
447 ng_ether_newhook(node_p node, hook_p hook, const char *name)
448 {
449 	const priv_p priv = NG_NODE_PRIVATE(node);
450 	u_char orphan = priv->lowerOrphan;
451 	hook_p *hookptr;
452 
453 	/* Divert hook is an alias for lower */
454 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
455 		name = NG_ETHER_HOOK_LOWER;
456 
457 	/* Which hook? */
458 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
459 		hookptr = &priv->upper;
460 	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
461 		hookptr = &priv->lower;
462 		orphan = 0;
463 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
464 		hookptr = &priv->lower;
465 		orphan = 1;
466 	} else
467 		return (EINVAL);
468 
469 	/* Check if already connected (shouldn't be, but doesn't hurt) */
470 	if (*hookptr != NULL)
471 		return (EISCONN);
472 
473 	/* Disable hardware checksums while 'upper' hook is connected */
474 	if (hookptr == &priv->upper)
475 		priv->ifp->if_hwassist = 0;
476 
477 	/* OK */
478 	*hookptr = hook;
479 	priv->lowerOrphan = orphan;
480 	return (0);
481 }
482 
483 /*
484  * Hooks are attached, adjust to force queueing.
485  * We don't really care which hook it is.
486  * they should all be queuing for outgoing data.
487  */
488 static	int
489 ng_ether_connect(hook_p hook)
490 {
491 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
492 	return (0);
493 }
494 
495 /*
496  * Receive an incoming control message.
497  */
498 static int
499 ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
500 {
501 	const priv_p priv = NG_NODE_PRIVATE(node);
502 	struct ng_mesg *resp = NULL;
503 	int error = 0;
504 	struct ng_mesg *msg;
505 
506 	NGI_GET_MSG(item, msg);
507 	switch (msg->header.typecookie) {
508 	case NGM_ETHER_COOKIE:
509 		switch (msg->header.cmd) {
510 		case NGM_ETHER_GET_IFNAME:
511 			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
512 			if (resp == NULL) {
513 				error = ENOMEM;
514 				break;
515 			}
516 			snprintf(resp->data, IFNAMSIZ + 1,
517 			    "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
518 			break;
519 		case NGM_ETHER_GET_IFINDEX:
520 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
521 			if (resp == NULL) {
522 				error = ENOMEM;
523 				break;
524 			}
525 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
526 			break;
527 		case NGM_ETHER_GET_ENADDR:
528 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
529 			if (resp == NULL) {
530 				error = ENOMEM;
531 				break;
532 			}
533 			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
534 			    resp->data, ETHER_ADDR_LEN);
535 			break;
536 		case NGM_ETHER_SET_ENADDR:
537 		    {
538 			if (msg->header.arglen != ETHER_ADDR_LEN) {
539 				error = EINVAL;
540 				break;
541 			}
542 			error = if_setlladdr(priv->ifp,
543 			    (u_char *)msg->data, ETHER_ADDR_LEN);
544 			break;
545 		    }
546 		case NGM_ETHER_GET_PROMISC:
547 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
548 			if (resp == NULL) {
549 				error = ENOMEM;
550 				break;
551 			}
552 			*((u_int32_t *)resp->data) = priv->promisc;
553 			break;
554 		case NGM_ETHER_SET_PROMISC:
555 		    {
556 			u_char want;
557 
558 			if (msg->header.arglen != sizeof(u_int32_t)) {
559 				error = EINVAL;
560 				break;
561 			}
562 			want = !!*((u_int32_t *)msg->data);
563 			if (want ^ priv->promisc) {
564 				if ((error = ifpromisc(priv->ifp, want)) != 0)
565 					break;
566 				priv->promisc = want;
567 			}
568 			break;
569 		    }
570 		case NGM_ETHER_GET_AUTOSRC:
571 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
572 			if (resp == NULL) {
573 				error = ENOMEM;
574 				break;
575 			}
576 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
577 			break;
578 		case NGM_ETHER_SET_AUTOSRC:
579 			if (msg->header.arglen != sizeof(u_int32_t)) {
580 				error = EINVAL;
581 				break;
582 			}
583 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
584 			break;
585 		default:
586 			error = EINVAL;
587 			break;
588 		}
589 		break;
590 	default:
591 		error = EINVAL;
592 		break;
593 	}
594 	NG_RESPOND_MSG(error, node, item, resp);
595 	NG_FREE_MSG(msg);
596 	return (error);
597 }
598 
599 /*
600  * Receive data on a hook.
601  */
602 static int
603 ng_ether_rcvdata(hook_p hook, item_p item)
604 {
605 	const node_p node = NG_HOOK_NODE(hook);
606 	const priv_p priv = NG_NODE_PRIVATE(node);
607 	struct mbuf *m;
608 	meta_p meta;
609 
610 	NGI_GET_M(item, m);
611 	NGI_GET_META(item, meta);
612 	NG_FREE_ITEM(item);
613 	if (hook == priv->lower)
614 		return ng_ether_rcv_lower(node, m, meta);
615 	if (hook == priv->upper)
616 		return ng_ether_rcv_upper(node, m, meta);
617 	panic("%s: weird hook", __func__);
618 #ifdef RESTARTABLE_PANICS /* so we don;t get an error msg in LINT */
619 	return NULL;
620 #endif
621 }
622 
623 /*
624  * Handle an mbuf received on the "lower" hook.
625  */
626 static int
627 ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
628 {
629 	const priv_p priv = NG_NODE_PRIVATE(node);
630 
631 	/* Make sure header is fully pulled up */
632 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
633 		NG_FREE_M(m);
634 		NG_FREE_META(meta);
635 		return (EINVAL);
636 	}
637 	if (m->m_len < sizeof(struct ether_header)
638 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
639 		NG_FREE_META(meta);
640 		return (ENOBUFS);
641 	}
642 
643 	/* Drop in the MAC address if desired */
644 	if (priv->autoSrcAddr) {
645 		bcopy((IFP2AC(priv->ifp))->ac_enaddr,
646 		    mtod(m, struct ether_header *)->ether_shost,
647 		    ETHER_ADDR_LEN);
648 	}
649 
650 	/* Send it on its way */
651 	NG_FREE_META(meta);
652 	return ether_output_frame(priv->ifp, m);
653 }
654 
655 /*
656  * Handle an mbuf received on the "upper" hook.
657  */
658 static int
659 ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
660 {
661 	const priv_p priv = NG_NODE_PRIVATE(node);
662 	struct ether_header *eh;
663 
664 	/* Check length and pull off header */
665 	if (m->m_pkthdr.len < sizeof(*eh)) {
666 		NG_FREE_M(m);
667 		NG_FREE_META(meta);
668 		return (EINVAL);
669 	}
670 	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
671 		NG_FREE_META(meta);
672 		return (ENOBUFS);
673 	}
674 	eh = mtod(m, struct ether_header *);
675 	m->m_data += sizeof(*eh);
676 	m->m_len -= sizeof(*eh);
677 	m->m_pkthdr.len -= sizeof(*eh);
678 	m->m_pkthdr.rcvif = priv->ifp;
679 
680 	/* Route packet back in */
681 	NG_FREE_META(meta);
682 	ether_demux(priv->ifp, eh, m);
683 	return (0);
684 }
685 
686 /*
687  * Shutdown node. This resets the node but does not remove it
688  * unless the REALLY_DIE flag is set.
689  */
690 static int
691 ng_ether_shutdown(node_p node)
692 {
693 	const priv_p priv = NG_NODE_PRIVATE(node);
694 
695 	if (priv->promisc) {		/* disable promiscuous mode */
696 		(void)ifpromisc(priv->ifp, 0);
697 		priv->promisc = 0;
698 	}
699 	if (node->nd_flags & NG_REALLY_DIE) {
700 		/*
701 		 * WE came here because the ethernet card is being unloaded,
702 		 * so stop being persistant.
703 		 * Actually undo all the things we did on creation.
704 		 * Assume the ifp has already been freed.
705 		 */
706 		NG_NODE_SET_PRIVATE(node, NULL);
707 		FREE(priv, M_NETGRAPH);
708 		NG_NODE_UNREF(node);	/* free node itself */
709 		return (0);
710 	}
711 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
712 	node->nd_flags &= ~NG_INVALID;	/* Signal ng_rmnode we are persisant */
713 	return (0);
714 }
715 
716 /*
717  * Hook disconnection.
718  */
719 static int
720 ng_ether_disconnect(hook_p hook)
721 {
722 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
723 
724 	if (hook == priv->upper) {
725 		priv->upper = NULL;
726 		priv->ifp->if_hwassist = priv->hwassist;  /* restore h/w csum */
727 	} else if (hook == priv->lower) {
728 		priv->lower = NULL;
729 		priv->lowerOrphan = 0;
730 	} else
731 		panic("%s: weird hook", __func__);
732 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
733 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
734 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
735 	return (0);
736 }
737 
738 static int
739 ng_enaddr_parse(const struct ng_parse_type *type,
740 	const char *s, int *const off, const u_char *const start,
741 	u_char *const buf, int *const buflen)
742 {
743 	char *eptr;
744 	u_long val;
745 	int i;
746 
747 	if (*buflen < ETHER_ADDR_LEN)
748 		return (ERANGE);
749 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
750 		val = strtoul(s + *off, &eptr, 16);
751 		if (val > 0xff || eptr == s + *off)
752 			return (EINVAL);
753 		buf[i] = (u_char)val;
754 		*off = (eptr - s);
755 		if (i < ETHER_ADDR_LEN - 1) {
756 			if (*eptr != ':')
757 				return (EINVAL);
758 			(*off)++;
759 		}
760 	}
761 	*buflen = ETHER_ADDR_LEN;
762 	return (0);
763 }
764 
765 static int
766 ng_enaddr_unparse(const struct ng_parse_type *type,
767 	const u_char *data, int *off, char *cbuf, int cbuflen)
768 {
769 	int len;
770 
771 	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
772 	    data[*off], data[*off + 1], data[*off + 2],
773 	    data[*off + 3], data[*off + 4], data[*off + 5]);
774 	if (len >= cbuflen)
775 		return (ERANGE);
776 	*off += ETHER_ADDR_LEN;
777 	return (0);
778 }
779 
780 /******************************************************************
781 		    	INITIALIZATION
782 ******************************************************************/
783 
784 /*
785  * Handle loading and unloading for this node type.
786  */
787 static int
788 ng_ether_mod_event(module_t mod, int event, void *data)
789 {
790 	struct ifnet *ifp;
791 	int error = 0;
792 	int s;
793 
794 	s = splnet();
795 	switch (event) {
796 	case MOD_LOAD:
797 
798 		/* Register function hooks */
799 		if (ng_ether_attach_p != NULL) {
800 			error = EEXIST;
801 			break;
802 		}
803 		ng_ether_attach_p = ng_ether_attach;
804 		ng_ether_detach_p = ng_ether_detach;
805 		ng_ether_output_p = ng_ether_output;
806 		ng_ether_input_p = ng_ether_input;
807 		ng_ether_input_orphan_p = ng_ether_input_orphan;
808 
809 		/* Create nodes for any already-existing Ethernet interfaces */
810 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
811 			if (ifp->if_type == IFT_ETHER
812 			    || ifp->if_type == IFT_L2VLAN)
813 				ng_ether_attach(ifp);
814 		}
815 		break;
816 
817 	case MOD_UNLOAD:
818 
819 		/*
820 		 * Note that the base code won't try to unload us until
821 		 * all nodes have been removed, and that can't happen
822 		 * until all Ethernet interfaces are removed. In any
823 		 * case, we know there are no nodes left if the action
824 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
825 		 */
826 
827 		/* Unregister function hooks */
828 		ng_ether_attach_p = NULL;
829 		ng_ether_detach_p = NULL;
830 		ng_ether_output_p = NULL;
831 		ng_ether_input_p = NULL;
832 		ng_ether_input_orphan_p = NULL;
833 		break;
834 
835 	default:
836 		error = EOPNOTSUPP;
837 		break;
838 	}
839 	splx(s);
840 	return (error);
841 }
842 
843