ng_ether.c revision 70700
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 70700 2001-01-06 00:46:47Z 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 = node->private;
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 = node->private;
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 = node->private;
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 = node->private;
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_unref(node);
312		return;
313	}
314	node->private = 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	node->flags |= NG_INVALID;
340	IFP2NG(ifp) = NULL;		/* detach node from interface */
341	priv = node->private;		/* free node private info */
342	bzero(priv, sizeof(*priv));
343	FREE(priv, M_NETGRAPH);
344	node->private = NULL;
345	ng_unref(node);			/* free node itself */
346}
347
348/*
349 * Optimization for gluing the Ethernet header back onto
350 * the front of an incoming packet.
351 */
352static int
353ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
354{
355	struct mbuf *m = *mp;
356	uintfptr_t room;
357	int error = 0;
358
359	/*
360	 * Possibly the header is already on the front.
361	 * If this is the case so just move the markers back
362	 * to re-include it. We lucked out.
363	 * This allows us to avoid a yucky m_pullup
364	 * in later nodes if it works.
365	 */
366	if (eh == mtod(m, struct ether_header *) - 1) {
367		m->m_len += sizeof(*eh);
368		m->m_data -= sizeof(*eh);
369		m->m_pkthdr.len += sizeof(*eh);
370		goto done;
371	}
372
373	/*
374	 * Alternatively there may be room even though
375	 * it is stored somewhere else. If so, copy it in.
376	 * This only safe because we KNOW that this packet has
377	 * just been generated by an ethernet card, so there are
378	 * no aliases to the buffer (not so for outgoing packets).
379	 * Nearly all ethernet cards will end up producing mbufs
380	 * that fall into these cases. So we are not optimizing
381	 * contorted cases.
382	 */
383	if ((m->m_flags & M_EXT) != 0) {
384		room = mtod(m, caddr_t) - m->m_ext.ext_buf;
385		if (room > m->m_ext.ext_size)	/* garbage, fail immediately */
386			room = 0;
387	} else
388		room = mtod(m, caddr_t) - m->m_pktdat;
389
390	/*
391	 * If we have room, just copy it and adjust
392	 */
393	if (room >= sizeof(*eh)) {
394		m->m_len += sizeof(*eh);
395		m->m_data -= sizeof(*eh);
396		m->m_pkthdr.len += sizeof(*eh);
397		goto copy;
398	}
399
400	/*
401	 * Doing anything more is likely to get more
402	 * expensive than it's worth..
403	 * it's probable that everything else is in one
404	 * big lump. The next node will do an m_pullup()
405	 * for exactly the amount of data it needs and
406	 * hopefully everything after that will not
407	 * need one. So let's just use M_PREPEND.
408	 */
409	M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
410	if (m == NULL) {
411		error = ENOBUFS;
412		goto done;
413	}
414
415copy:
416	/* Copy header and return (possibly new) mbuf */
417	bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
418done:
419	*mp = m;
420	return error;
421}
422
423/******************************************************************
424		    NETGRAPH NODE METHODS
425******************************************************************/
426
427/*
428 * It is not possible or allowable to create a node of this type.
429 * Nodes get created when the interface is attached (or, when
430 * this node type's KLD is loaded).
431 */
432static int
433ng_ether_constructor(node_p node)
434{
435	return (EINVAL);
436}
437
438/*
439 * Check for attaching a new hook.
440 */
441static	int
442ng_ether_newhook(node_p node, hook_p hook, const char *name)
443{
444	const priv_p priv = node->private;
445	u_char orphan = priv->lowerOrphan;
446	hook_p *hookptr;
447
448	/* Divert hook is an alias for lower */
449	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
450		name = NG_ETHER_HOOK_LOWER;
451
452	/* Which hook? */
453	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
454		hookptr = &priv->upper;
455	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
456		hookptr = &priv->lower;
457		orphan = 0;
458	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
459		hookptr = &priv->lower;
460		orphan = 1;
461	} else
462		return (EINVAL);
463
464	/* Check if already connected (shouldn't be, but doesn't hurt) */
465	if (*hookptr != NULL)
466		return (EISCONN);
467
468	/* OK */
469	*hookptr = hook;
470	priv->lowerOrphan = orphan;
471	return (0);
472}
473
474/*
475 * Hooks are attached, adjust to force queueing.
476 * We don't really care which hook it is.
477 * they should all be queuing for outgoing data.
478 */
479static	int
480ng_ether_connect(hook_p hook)
481{
482	hook->peer->flags |= HK_QUEUE;
483	return (0);
484}
485
486/*
487 * Receive an incoming control message.
488 */
489static int
490ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
491{
492	const priv_p priv = node->private;
493	struct ng_mesg *resp = NULL;
494	int error = 0;
495	struct ng_mesg *msg;
496
497	NGI_GET_MSG(item, msg);
498	switch (msg->header.typecookie) {
499	case NGM_ETHER_COOKIE:
500		switch (msg->header.cmd) {
501		case NGM_ETHER_GET_IFNAME:
502			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
503			if (resp == NULL) {
504				error = ENOMEM;
505				break;
506			}
507			snprintf(resp->data, IFNAMSIZ + 1,
508			    "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
509			break;
510		case NGM_ETHER_GET_IFINDEX:
511			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
512			if (resp == NULL) {
513				error = ENOMEM;
514				break;
515			}
516			*((u_int32_t *)resp->data) = priv->ifp->if_index;
517			break;
518		case NGM_ETHER_GET_ENADDR:
519			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
520			if (resp == NULL) {
521				error = ENOMEM;
522				break;
523			}
524			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
525			    resp->data, ETHER_ADDR_LEN);
526			break;
527		case NGM_ETHER_SET_ENADDR:
528		    {
529			if (msg->header.arglen != ETHER_ADDR_LEN) {
530				error = EINVAL;
531				break;
532			}
533			error = if_setlladdr(priv->ifp,
534			    (u_char *)msg->data, ETHER_ADDR_LEN);
535			break;
536		    }
537		case NGM_ETHER_GET_PROMISC:
538			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
539			if (resp == NULL) {
540				error = ENOMEM;
541				break;
542			}
543			*((u_int32_t *)resp->data) = priv->promisc;
544			break;
545		case NGM_ETHER_SET_PROMISC:
546		    {
547			u_char want;
548
549			if (msg->header.arglen != sizeof(u_int32_t)) {
550				error = EINVAL;
551				break;
552			}
553			want = !!*((u_int32_t *)msg->data);
554			if (want ^ priv->promisc) {
555				if ((error = ifpromisc(priv->ifp, want)) != 0)
556					break;
557				priv->promisc = want;
558			}
559			break;
560		    }
561		case NGM_ETHER_GET_AUTOSRC:
562			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
563			if (resp == NULL) {
564				error = ENOMEM;
565				break;
566			}
567			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
568			break;
569		case NGM_ETHER_SET_AUTOSRC:
570			if (msg->header.arglen != sizeof(u_int32_t)) {
571				error = EINVAL;
572				break;
573			}
574			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
575			break;
576		default:
577			error = EINVAL;
578			break;
579		}
580		break;
581	default:
582		error = EINVAL;
583		break;
584	}
585	NG_RESPOND_MSG(error, node, item, resp);
586	NG_FREE_MSG(msg);
587	return (error);
588}
589
590/*
591 * Receive data on a hook.
592 */
593static int
594ng_ether_rcvdata(hook_p hook, item_p item)
595{
596	const node_p node = hook->node;
597	const priv_p priv = node->private;
598	struct mbuf *m;
599	meta_p meta;
600
601	NGI_GET_M(item, m);
602	NGI_GET_META(item, meta);
603	NG_FREE_ITEM(item);
604	if (hook == priv->lower)
605		return ng_ether_rcv_lower(node, m, meta);
606	if (hook == priv->upper)
607		return ng_ether_rcv_upper(node, m, meta);
608	panic("%s: weird hook", __FUNCTION__);
609}
610
611/*
612 * Handle an mbuf received on the "lower" hook.
613 */
614static int
615ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
616{
617	const priv_p priv = node->private;
618
619	/* Make sure header is fully pulled up */
620	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
621		NG_FREE_M(m);
622		NG_FREE_META(meta);
623		return (EINVAL);
624	}
625	if (m->m_len < sizeof(struct ether_header)
626	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
627		NG_FREE_META(meta);
628		return (ENOBUFS);
629	}
630
631	/* Drop in the MAC address if desired */
632	if (priv->autoSrcAddr) {
633		bcopy((IFP2AC(priv->ifp))->ac_enaddr,
634		    mtod(m, struct ether_header *)->ether_shost,
635		    ETHER_ADDR_LEN);
636	}
637
638	/* Send it on its way */
639	NG_FREE_META(meta);
640	return ether_output_frame(priv->ifp, m);
641}
642
643/*
644 * Handle an mbuf received on the "upper" hook.
645 */
646static int
647ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
648{
649	const priv_p priv = node->private;
650	struct ether_header *eh;
651
652	/* Check length and pull off header */
653	if (m->m_pkthdr.len < sizeof(*eh)) {
654		NG_FREE_M(m);
655		NG_FREE_META(meta);
656		return (EINVAL);
657	}
658	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
659		NG_FREE_META(meta);
660		return (ENOBUFS);
661	}
662	eh = mtod(m, struct ether_header *);
663	m->m_data += sizeof(*eh);
664	m->m_len -= sizeof(*eh);
665	m->m_pkthdr.len -= sizeof(*eh);
666	m->m_pkthdr.rcvif = priv->ifp;
667
668	/* Route packet back in */
669	NG_FREE_META(meta);
670	ether_demux(priv->ifp, eh, m);
671	return (0);
672}
673
674/*
675 * Shutdown node. This resets the node but does not remove it.
676 * Actually it produces a new node. XXX The problem  is what to do when
677 * the node really DOES need to go away,
678 * or if our re-make of the node fails.
679 */
680static int
681ng_ether_shutdown(node_p node)
682{
683	char name[IFNAMSIZ + 1];
684	const priv_p priv = node->private;
685
686	if (priv->promisc) {		/* disable promiscuous mode */
687		(void)ifpromisc(priv->ifp, 0);
688		priv->promisc = 0;
689	}
690	ng_unref(node);
691	snprintf(name, sizeof(name), "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
692	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
693		log(LOG_ERR, "%s: can't %s for %s\n",
694		    __FUNCTION__, "create node", name);
695		return (ENOMEM);
696	}
697
698	/* Allocate private data */
699	node->private = priv;
700	IFP2NG(priv->ifp) = node;
701	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
702
703	/* Try to give the node the same name as the interface */
704	if (ng_name_node(node, name) != 0) {
705		log(LOG_WARNING, "%s: can't name node %s\n",
706		    __FUNCTION__, name);
707	}
708	return (0);
709}
710
711/*
712 * Hook disconnection.
713 */
714static int
715ng_ether_disconnect(hook_p hook)
716{
717	const priv_p priv = hook->node->private;
718
719	if (hook == priv->upper)
720		priv->upper = NULL;
721	else if (hook == priv->lower) {
722		priv->lower = NULL;
723		priv->lowerOrphan = 0;
724	} else
725		panic("%s: weird hook", __FUNCTION__);
726	if ((hook->node->numhooks == 0)
727	&& ((hook->node->flags & NG_INVALID) == 0))
728		ng_rmnode_self(hook->node);	/* reset node */
729	return (0);
730}
731
732static int
733ng_enaddr_parse(const struct ng_parse_type *type,
734	const char *s, int *const off, const u_char *const start,
735	u_char *const buf, int *const buflen)
736{
737	char *eptr;
738	u_long val;
739	int i;
740
741	if (*buflen < ETHER_ADDR_LEN)
742		return (ERANGE);
743	for (i = 0; i < ETHER_ADDR_LEN; i++) {
744		val = strtoul(s + *off, &eptr, 16);
745		if (val > 0xff || eptr == s + *off)
746			return (EINVAL);
747		buf[i] = (u_char)val;
748		*off = (eptr - s);
749		if (i < ETHER_ADDR_LEN - 1) {
750			if (*eptr != ':')
751				return (EINVAL);
752			(*off)++;
753		}
754	}
755	*buflen = ETHER_ADDR_LEN;
756	return (0);
757}
758
759static int
760ng_enaddr_unparse(const struct ng_parse_type *type,
761	const u_char *data, int *off, char *cbuf, int cbuflen)
762{
763	int len;
764
765	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
766	    data[*off], data[*off + 1], data[*off + 2],
767	    data[*off + 3], data[*off + 4], data[*off + 5]);
768	if (len >= cbuflen)
769		return (ERANGE);
770	*off += ETHER_ADDR_LEN;
771	return (0);
772}
773
774/******************************************************************
775		    	INITIALIZATION
776******************************************************************/
777
778/*
779 * Handle loading and unloading for this node type.
780 */
781static int
782ng_ether_mod_event(module_t mod, int event, void *data)
783{
784	struct ifnet *ifp;
785	int error = 0;
786	int s;
787
788	s = splnet();
789	switch (event) {
790	case MOD_LOAD:
791
792		/* Register function hooks */
793		if (ng_ether_attach_p != NULL) {
794			error = EEXIST;
795			break;
796		}
797		ng_ether_attach_p = ng_ether_attach;
798		ng_ether_detach_p = ng_ether_detach;
799		ng_ether_output_p = ng_ether_output;
800		ng_ether_input_p = ng_ether_input;
801		ng_ether_input_orphan_p = ng_ether_input_orphan;
802
803		/* Create nodes for any already-existing Ethernet interfaces */
804		TAILQ_FOREACH(ifp, &ifnet, if_link) {
805			if (ifp->if_type == IFT_ETHER)
806				ng_ether_attach(ifp);
807		}
808		break;
809
810	case MOD_UNLOAD:
811
812		/*
813		 * Note that the base code won't try to unload us until
814		 * all nodes have been removed, and that can't happen
815		 * until all Ethernet interfaces are removed. In any
816		 * case, we know there are no nodes left if the action
817		 * is MOD_UNLOAD, so there's no need to detach any nodes.
818		 */
819
820		/* Unregister function hooks */
821		ng_ether_attach_p = NULL;
822		ng_ether_detach_p = NULL;
823		ng_ether_output_p = NULL;
824		ng_ether_input_p = NULL;
825		ng_ether_input_orphan_p = NULL;
826		break;
827
828	default:
829		error = EOPNOTSUPP;
830		break;
831	}
832	splx(s);
833	return (error);
834}
835
836