ng_ether.c revision 96265
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: head/sys/netgraph/ng_ether.c 96265 2002-05-09 20:19:00Z archie $
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 */
71struct 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};
81typedef struct private *priv_p;
82
83/* Functional hooks called from if_ethersubr.c */
84static void	ng_ether_input(struct ifnet *ifp,
85		    struct mbuf **mp, struct ether_header *eh);
86static void	ng_ether_input_orphan(struct ifnet *ifp,
87		    struct mbuf *m, struct ether_header *eh);
88static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
89static void	ng_ether_attach(struct ifnet *ifp);
90static void	ng_ether_detach(struct ifnet *ifp);
91
92/* Other functions */
93static void	ng_ether_input2(node_p node,
94		    struct mbuf **mp, struct ether_header *eh);
95static int	ng_ether_glueback_header(struct mbuf **mp,
96			struct ether_header *eh);
97static int	ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
98static int	ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
99
100/* Netgraph node methods */
101static ng_constructor_t	ng_ether_constructor;
102static ng_rcvmsg_t	ng_ether_rcvmsg;
103static ng_shutdown_t	ng_ether_shutdown;
104static ng_newhook_t	ng_ether_newhook;
105static ng_connect_t	ng_ether_connect;
106static ng_rcvdata_t	ng_ether_rcvdata;
107static ng_disconnect_t	ng_ether_disconnect;
108static int		ng_ether_mod_event(module_t mod, int event, void *data);
109
110/* Parse type for an Ethernet address */
111static ng_parse_t	ng_enaddr_parse;
112static ng_unparse_t	ng_enaddr_unparse;
113const 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 */
124static 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
184static 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};
198MODULE_VERSION(ng_ether, 1);
199NETGRAPH_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 */
211static void
212ng_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 */
230static void
231ng_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 */
254static void
255ng_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 */
273static int
274ng_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 */
293static void
294ng_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 */
334static void
335ng_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 */
357static int
358ng_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
420copy:
421	/* Copy header and return (possibly new) mbuf */
422	bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
423done:
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 */
437static int
438ng_ether_constructor(node_p node)
439{
440	return (EINVAL);
441}
442
443/*
444 * Check for attaching a new hook.
445 */
446static	int
447ng_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 */
488static	int
489ng_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 */
498static int
499ng_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 */
602static int
603ng_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 */
626static int
627ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
628{
629	const priv_p priv = NG_NODE_PRIVATE(node);
630 	struct ifnet *const ifp = priv->ifp;
631
632	/* Check whether interface is ready for packets */
633	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
634		NG_FREE_M(m);
635		NG_FREE_META(meta);
636		return (ENETDOWN);
637	}
638
639	/* Make sure header is fully pulled up */
640	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
641		NG_FREE_M(m);
642		NG_FREE_META(meta);
643		return (EINVAL);
644	}
645	if (m->m_len < sizeof(struct ether_header)
646	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
647		NG_FREE_META(meta);
648		return (ENOBUFS);
649	}
650
651	/* Drop in the MAC address if desired */
652	if (priv->autoSrcAddr) {
653		bcopy((IFP2AC(ifp))->ac_enaddr,
654		    mtod(m, struct ether_header *)->ether_shost,
655		    ETHER_ADDR_LEN);
656	}
657
658	/* Send it on its way */
659	NG_FREE_META(meta);
660	return ether_output_frame(ifp, m);
661}
662
663/*
664 * Handle an mbuf received on the "upper" hook.
665 */
666static int
667ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
668{
669	const priv_p priv = NG_NODE_PRIVATE(node);
670	struct ether_header *eh;
671
672	/* Check length and pull off header */
673	if (m->m_pkthdr.len < sizeof(*eh)) {
674		NG_FREE_M(m);
675		NG_FREE_META(meta);
676		return (EINVAL);
677	}
678	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
679		NG_FREE_META(meta);
680		return (ENOBUFS);
681	}
682	eh = mtod(m, struct ether_header *);
683	m->m_data += sizeof(*eh);
684	m->m_len -= sizeof(*eh);
685	m->m_pkthdr.len -= sizeof(*eh);
686	m->m_pkthdr.rcvif = priv->ifp;
687
688	/* Route packet back in */
689	NG_FREE_META(meta);
690	ether_demux(priv->ifp, eh, m);
691	return (0);
692}
693
694/*
695 * Shutdown node. This resets the node but does not remove it
696 * unless the REALLY_DIE flag is set.
697 */
698static int
699ng_ether_shutdown(node_p node)
700{
701	const priv_p priv = NG_NODE_PRIVATE(node);
702
703	if (priv->promisc) {		/* disable promiscuous mode */
704		(void)ifpromisc(priv->ifp, 0);
705		priv->promisc = 0;
706	}
707	if (node->nd_flags & NG_REALLY_DIE) {
708		/*
709		 * WE came here because the ethernet card is being unloaded,
710		 * so stop being persistant.
711		 * Actually undo all the things we did on creation.
712		 * Assume the ifp has already been freed.
713		 */
714		NG_NODE_SET_PRIVATE(node, NULL);
715		FREE(priv, M_NETGRAPH);
716		NG_NODE_UNREF(node);	/* free node itself */
717		return (0);
718	}
719	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
720	node->nd_flags &= ~NG_INVALID;	/* Signal ng_rmnode we are persisant */
721	return (0);
722}
723
724/*
725 * Hook disconnection.
726 */
727static int
728ng_ether_disconnect(hook_p hook)
729{
730	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
731
732	if (hook == priv->upper) {
733		priv->upper = NULL;
734		priv->ifp->if_hwassist = priv->hwassist;  /* restore h/w csum */
735	} else if (hook == priv->lower) {
736		priv->lower = NULL;
737		priv->lowerOrphan = 0;
738	} else
739		panic("%s: weird hook", __func__);
740	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
741	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
742		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
743	return (0);
744}
745
746static int
747ng_enaddr_parse(const struct ng_parse_type *type,
748	const char *s, int *const off, const u_char *const start,
749	u_char *const buf, int *const buflen)
750{
751	char *eptr;
752	u_long val;
753	int i;
754
755	if (*buflen < ETHER_ADDR_LEN)
756		return (ERANGE);
757	for (i = 0; i < ETHER_ADDR_LEN; i++) {
758		val = strtoul(s + *off, &eptr, 16);
759		if (val > 0xff || eptr == s + *off)
760			return (EINVAL);
761		buf[i] = (u_char)val;
762		*off = (eptr - s);
763		if (i < ETHER_ADDR_LEN - 1) {
764			if (*eptr != ':')
765				return (EINVAL);
766			(*off)++;
767		}
768	}
769	*buflen = ETHER_ADDR_LEN;
770	return (0);
771}
772
773static int
774ng_enaddr_unparse(const struct ng_parse_type *type,
775	const u_char *data, int *off, char *cbuf, int cbuflen)
776{
777	int len;
778
779	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
780	    data[*off], data[*off + 1], data[*off + 2],
781	    data[*off + 3], data[*off + 4], data[*off + 5]);
782	if (len >= cbuflen)
783		return (ERANGE);
784	*off += ETHER_ADDR_LEN;
785	return (0);
786}
787
788/******************************************************************
789		    	INITIALIZATION
790******************************************************************/
791
792/*
793 * Handle loading and unloading for this node type.
794 */
795static int
796ng_ether_mod_event(module_t mod, int event, void *data)
797{
798	struct ifnet *ifp;
799	int error = 0;
800	int s;
801
802	s = splnet();
803	switch (event) {
804	case MOD_LOAD:
805
806		/* Register function hooks */
807		if (ng_ether_attach_p != NULL) {
808			error = EEXIST;
809			break;
810		}
811		ng_ether_attach_p = ng_ether_attach;
812		ng_ether_detach_p = ng_ether_detach;
813		ng_ether_output_p = ng_ether_output;
814		ng_ether_input_p = ng_ether_input;
815		ng_ether_input_orphan_p = ng_ether_input_orphan;
816
817		/* Create nodes for any already-existing Ethernet interfaces */
818		TAILQ_FOREACH(ifp, &ifnet, if_link) {
819			if (ifp->if_type == IFT_ETHER
820			    || ifp->if_type == IFT_L2VLAN)
821				ng_ether_attach(ifp);
822		}
823		break;
824
825	case MOD_UNLOAD:
826
827		/*
828		 * Note that the base code won't try to unload us until
829		 * all nodes have been removed, and that can't happen
830		 * until all Ethernet interfaces are removed. In any
831		 * case, we know there are no nodes left if the action
832		 * is MOD_UNLOAD, so there's no need to detach any nodes.
833		 */
834
835		/* Unregister function hooks */
836		ng_ether_attach_p = NULL;
837		ng_ether_detach_p = NULL;
838		ng_ether_output_p = NULL;
839		ng_ether_input_p = NULL;
840		ng_ether_input_orphan_p = NULL;
841		break;
842
843	default:
844		error = EOPNOTSUPP;
845		break;
846	}
847	splx(s);
848	return (error);
849}
850
851