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