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