ng_ether.c revision 194739
1
2/*
3 * ng_ether.c
4 */
5
6/*-
7 * Copyright (c) 1996-2000 Whistle Communications, Inc.
8 * All rights reserved.
9 *
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Whistle Communications;
13 * provided, however, that:
14 * 1. Any and all reproductions of the source or object code must include the
15 *    copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Whistle
17 *    Communications, Inc. trademarks, including the mark "WHISTLE
18 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19 *    such appears in the above copyright notice or in the software.
20 *
21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * Authors: Archie Cobbs <archie@freebsd.org>
40 *	    Julian Elischer <julian@freebsd.org>
41 *
42 * $FreeBSD: head/sys/netgraph/ng_ether.c 194739 2009-06-23 17:03:45Z bz $
43 */
44
45/*
46 * ng_ether(4) netgraph node type
47 */
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/mbuf.h>
54#include <sys/errno.h>
55#include <sys/syslog.h>
56#include <sys/socket.h>
57#include <sys/vimage.h>
58
59#include <net/if.h>
60#include <net/if_dl.h>
61#include <net/if_types.h>
62#include <net/if_arp.h>
63#include <net/if_var.h>
64#include <net/ethernet.h>
65#include <net/if_bridgevar.h>
66#include <net/vnet.h>
67
68#include <netgraph/ng_message.h>
69#include <netgraph/netgraph.h>
70#include <netgraph/ng_parse.h>
71#include <netgraph/ng_ether.h>
72
73#define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
74
75static vnet_attach_fn ng_ether_iattach;
76
77#ifndef VIMAGE_GLOBALS
78static vnet_modinfo_t vnet_ng_ether_modinfo = {
79	.vmi_id		= VNET_MOD_NG_ETHER,
80	.vmi_name	= "ng_ether",
81	.vmi_dependson	= VNET_MOD_NETGRAPH,
82	.vmi_iattach	= ng_ether_iattach,
83};
84#endif
85
86/* Per-node private data */
87struct private {
88	struct ifnet	*ifp;		/* associated interface */
89	hook_p		upper;		/* upper hook connection */
90	hook_p		lower;		/* lower hook connection */
91	hook_p		orphan;		/* orphan hook connection */
92	u_char		autoSrcAddr;	/* always overwrite source address */
93	u_char		promisc;	/* promiscuous mode enabled */
94	u_long		hwassist;	/* hardware checksum capabilities */
95	u_int		flags;		/* flags e.g. really die */
96};
97typedef struct private *priv_p;
98
99/* Hook pointers used by if_ethersubr.c to callback to netgraph */
100extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
101extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
102extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
103extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
104extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
105extern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
106
107/* Functional hooks called from if_ethersubr.c */
108static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
109static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
110static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
111static void	ng_ether_attach(struct ifnet *ifp);
112static void	ng_ether_detach(struct ifnet *ifp);
113static void	ng_ether_link_state(struct ifnet *ifp, int state);
114
115/* Other functions */
116static int	ng_ether_rcv_lower(hook_p node, item_p item);
117static int	ng_ether_rcv_upper(hook_p node, item_p item);
118
119/* Netgraph node methods */
120static ng_constructor_t	ng_ether_constructor;
121static ng_rcvmsg_t	ng_ether_rcvmsg;
122static ng_shutdown_t	ng_ether_shutdown;
123static ng_newhook_t	ng_ether_newhook;
124static ng_rcvdata_t	ng_ether_rcvdata;
125static ng_disconnect_t	ng_ether_disconnect;
126static int		ng_ether_mod_event(module_t mod, int event, void *data);
127
128/* List of commands and how to convert arguments to/from ASCII */
129static const struct ng_cmdlist ng_ether_cmdlist[] = {
130	{
131	  NGM_ETHER_COOKIE,
132	  NGM_ETHER_GET_IFNAME,
133	  "getifname",
134	  NULL,
135	  &ng_parse_string_type
136	},
137	{
138	  NGM_ETHER_COOKIE,
139	  NGM_ETHER_GET_IFINDEX,
140	  "getifindex",
141	  NULL,
142	  &ng_parse_int32_type
143	},
144	{
145	  NGM_ETHER_COOKIE,
146	  NGM_ETHER_GET_ENADDR,
147	  "getenaddr",
148	  NULL,
149	  &ng_parse_enaddr_type
150	},
151	{
152	  NGM_ETHER_COOKIE,
153	  NGM_ETHER_SET_ENADDR,
154	  "setenaddr",
155	  &ng_parse_enaddr_type,
156	  NULL
157	},
158	{
159	  NGM_ETHER_COOKIE,
160	  NGM_ETHER_GET_PROMISC,
161	  "getpromisc",
162	  NULL,
163	  &ng_parse_int32_type
164	},
165	{
166	  NGM_ETHER_COOKIE,
167	  NGM_ETHER_SET_PROMISC,
168	  "setpromisc",
169	  &ng_parse_int32_type,
170	  NULL
171	},
172	{
173	  NGM_ETHER_COOKIE,
174	  NGM_ETHER_GET_AUTOSRC,
175	  "getautosrc",
176	  NULL,
177	  &ng_parse_int32_type
178	},
179	{
180	  NGM_ETHER_COOKIE,
181	  NGM_ETHER_SET_AUTOSRC,
182	  "setautosrc",
183	  &ng_parse_int32_type,
184	  NULL
185	},
186	{
187	  NGM_ETHER_COOKIE,
188	  NGM_ETHER_ADD_MULTI,
189	  "addmulti",
190	  &ng_parse_enaddr_type,
191	  NULL
192	},
193	{
194	  NGM_ETHER_COOKIE,
195	  NGM_ETHER_DEL_MULTI,
196	  "delmulti",
197	  &ng_parse_enaddr_type,
198	  NULL
199	},
200	{
201	  NGM_ETHER_COOKIE,
202	  NGM_ETHER_DETACH,
203	  "detach",
204	  NULL,
205	  NULL
206	},
207	{ 0 }
208};
209
210static struct ng_type ng_ether_typestruct = {
211	.version =	NG_ABI_VERSION,
212	.name =		NG_ETHER_NODE_TYPE,
213	.mod_event =	ng_ether_mod_event,
214	.constructor =	ng_ether_constructor,
215	.rcvmsg =	ng_ether_rcvmsg,
216	.shutdown =	ng_ether_shutdown,
217	.newhook =	ng_ether_newhook,
218	.rcvdata =	ng_ether_rcvdata,
219	.disconnect =	ng_ether_disconnect,
220	.cmdlist =	ng_ether_cmdlist,
221};
222NETGRAPH_INIT(ether, &ng_ether_typestruct);
223
224/******************************************************************
225		    ETHERNET FUNCTION HOOKS
226******************************************************************/
227
228/*
229 * Handle a packet that has come in on an interface. We get to
230 * look at it here before any upper layer protocols do.
231 *
232 * NOTE: this function will get called at splimp()
233 */
234static void
235ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
236{
237	const node_p node = IFP2NG(ifp);
238	const priv_p priv = NG_NODE_PRIVATE(node);
239	int error;
240
241	/* If "lower" hook not connected, let packet continue */
242	if (priv->lower == NULL)
243		return;
244	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
245}
246
247/*
248 * Handle a packet that has come in on an interface, and which
249 * does not match any of our known protocols (an ``orphan'').
250 *
251 * NOTE: this function will get called at splimp()
252 */
253static void
254ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
255{
256	const node_p node = IFP2NG(ifp);
257	const priv_p priv = NG_NODE_PRIVATE(node);
258	int error;
259
260	/* If "orphan" hook not connected, discard packet */
261	if (priv->orphan == NULL) {
262		m_freem(m);
263		return;
264	}
265	NG_SEND_DATA_ONLY(error, priv->orphan, m);
266}
267
268/*
269 * Handle a packet that is going out on an interface.
270 * The Ethernet header is already attached to the mbuf.
271 */
272static int
273ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
274{
275	const node_p node = IFP2NG(ifp);
276	const priv_p priv = NG_NODE_PRIVATE(node);
277	int error = 0;
278
279	/* If "upper" hook not connected, let packet continue */
280	if (priv->upper == NULL)
281		return (0);
282
283	/* Send it out "upper" hook */
284	NG_OUTBOUND_THREAD_REF();
285	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
286	NG_OUTBOUND_THREAD_UNREF();
287	return (error);
288}
289
290/*
291 * A new Ethernet interface has been attached.
292 * Create a new node for it, etc.
293 */
294static void
295ng_ether_attach(struct ifnet *ifp)
296{
297	priv_p priv;
298	node_p node;
299
300	/*
301	 * Do not create / attach an ether node to this ifnet if
302	 * a netgraph node with the same name already exists.
303	 * This should prevent ether nodes to become attached to
304	 * eiface nodes, which may be problematic due to naming
305	 * clashes.
306	 */
307	if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
308		NG_NODE_UNREF(node);
309		return;
310	}
311
312	/* Create node */
313	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
314	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
315		log(LOG_ERR, "%s: can't %s for %s\n",
316		    __func__, "create node", ifp->if_xname);
317		return;
318	}
319
320	/* Allocate private data */
321	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
322	if (priv == NULL) {
323		log(LOG_ERR, "%s: can't %s for %s\n",
324		    __func__, "allocate memory", ifp->if_xname);
325		NG_NODE_UNREF(node);
326		return;
327	}
328	NG_NODE_SET_PRIVATE(node, priv);
329	priv->ifp = ifp;
330	IFP2NG(ifp) = node;
331	priv->hwassist = ifp->if_hwassist;
332
333	/* Try to give the node the same name as the interface */
334	if (ng_name_node(node, ifp->if_xname) != 0) {
335		log(LOG_WARNING, "%s: can't name node %s\n",
336		    __func__, ifp->if_xname);
337	}
338}
339
340/*
341 * An Ethernet interface is being detached.
342 * REALLY Destroy its node.
343 */
344static void
345ng_ether_detach(struct ifnet *ifp)
346{
347	const node_p node = IFP2NG(ifp);
348	const priv_p priv = NG_NODE_PRIVATE(node);
349
350	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
351	/*
352	 * We can't assume the ifnet is still around when we run shutdown
353	 * So zap it now. XXX We HOPE that anything running at this time
354	 * handles it (as it should in the non netgraph case).
355	 */
356	IFP2NG(ifp) = NULL;
357	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
358	ng_rmnode_self(node);		/* remove all netgraph parts */
359}
360
361/*
362 * Notify graph about link event.
363 * if_link_state_change() has already checked that the state has changed.
364 */
365static void
366ng_ether_link_state(struct ifnet *ifp, int state)
367{
368	const node_p node = IFP2NG(ifp);
369	const priv_p priv = NG_NODE_PRIVATE(node);
370	struct ng_mesg *msg;
371	int cmd, dummy_error = 0;
372
373	if (priv->lower == NULL)
374                return;
375
376	if (state == LINK_STATE_UP)
377		cmd = NGM_LINK_IS_UP;
378	else if (state == LINK_STATE_DOWN)
379		cmd = NGM_LINK_IS_DOWN;
380	else
381		return;
382
383	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
384	if (msg != NULL)
385		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
386}
387
388/******************************************************************
389		    NETGRAPH NODE METHODS
390******************************************************************/
391
392/*
393 * It is not possible or allowable to create a node of this type.
394 * Nodes get created when the interface is attached (or, when
395 * this node type's KLD is loaded).
396 */
397static int
398ng_ether_constructor(node_p node)
399{
400	return (EINVAL);
401}
402
403/*
404 * Check for attaching a new hook.
405 */
406static	int
407ng_ether_newhook(node_p node, hook_p hook, const char *name)
408{
409	const priv_p priv = NG_NODE_PRIVATE(node);
410	hook_p *hookptr;
411
412	/* Divert hook is an alias for lower */
413	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
414		name = NG_ETHER_HOOK_LOWER;
415
416	/* Which hook? */
417	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
418		hookptr = &priv->upper;
419		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
420		NG_HOOK_SET_TO_INBOUND(hook);
421	} else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
422		hookptr = &priv->lower;
423		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
424	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
425		hookptr = &priv->orphan;
426		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
427	} else
428		return (EINVAL);
429
430	/* Check if already connected (shouldn't be, but doesn't hurt) */
431	if (*hookptr != NULL)
432		return (EISCONN);
433
434	/* Disable hardware checksums while 'upper' hook is connected */
435	if (hookptr == &priv->upper)
436		priv->ifp->if_hwassist = 0;
437	NG_HOOK_HI_STACK(hook);
438	/* OK */
439	*hookptr = hook;
440	return (0);
441}
442
443/*
444 * Receive an incoming control message.
445 */
446static int
447ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
448{
449	const priv_p priv = NG_NODE_PRIVATE(node);
450	struct ng_mesg *resp = NULL;
451	int error = 0;
452	struct ng_mesg *msg;
453
454	NGI_GET_MSG(item, msg);
455	switch (msg->header.typecookie) {
456	case NGM_ETHER_COOKIE:
457		switch (msg->header.cmd) {
458		case NGM_ETHER_GET_IFNAME:
459			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
460			if (resp == NULL) {
461				error = ENOMEM;
462				break;
463			}
464			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
465			break;
466		case NGM_ETHER_GET_IFINDEX:
467			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
468			if (resp == NULL) {
469				error = ENOMEM;
470				break;
471			}
472			*((u_int32_t *)resp->data) = priv->ifp->if_index;
473			break;
474		case NGM_ETHER_GET_ENADDR:
475			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
476			if (resp == NULL) {
477				error = ENOMEM;
478				break;
479			}
480			bcopy(IF_LLADDR(priv->ifp),
481			    resp->data, ETHER_ADDR_LEN);
482			break;
483		case NGM_ETHER_SET_ENADDR:
484		    {
485			if (msg->header.arglen != ETHER_ADDR_LEN) {
486				error = EINVAL;
487				break;
488			}
489			error = if_setlladdr(priv->ifp,
490			    (u_char *)msg->data, ETHER_ADDR_LEN);
491			break;
492		    }
493		case NGM_ETHER_GET_PROMISC:
494			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
495			if (resp == NULL) {
496				error = ENOMEM;
497				break;
498			}
499			*((u_int32_t *)resp->data) = priv->promisc;
500			break;
501		case NGM_ETHER_SET_PROMISC:
502		    {
503			u_char want;
504
505			if (msg->header.arglen != sizeof(u_int32_t)) {
506				error = EINVAL;
507				break;
508			}
509			want = !!*((u_int32_t *)msg->data);
510			if (want ^ priv->promisc) {
511				if ((error = ifpromisc(priv->ifp, want)) != 0)
512					break;
513				priv->promisc = want;
514			}
515			break;
516		    }
517		case NGM_ETHER_GET_AUTOSRC:
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->autoSrcAddr;
524			break;
525		case NGM_ETHER_SET_AUTOSRC:
526			if (msg->header.arglen != sizeof(u_int32_t)) {
527				error = EINVAL;
528				break;
529			}
530			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
531			break;
532		case NGM_ETHER_ADD_MULTI:
533		    {
534			struct sockaddr_dl sa_dl;
535			struct ifmultiaddr *ifma;
536
537			if (msg->header.arglen != ETHER_ADDR_LEN) {
538				error = EINVAL;
539				break;
540			}
541			bzero(&sa_dl, sizeof(struct sockaddr_dl));
542			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
543			sa_dl.sdl_family = AF_LINK;
544			sa_dl.sdl_alen = ETHER_ADDR_LEN;
545			bcopy((void *)msg->data, LLADDR(&sa_dl),
546			    ETHER_ADDR_LEN);
547			/*
548			 * Netgraph is only permitted to join groups once
549			 * via the if_addmulti() KPI, because it cannot hold
550			 * struct ifmultiaddr * between calls. It may also
551			 * lose a race while we check if the membership
552			 * already exists.
553			 */
554			IF_ADDR_LOCK(priv->ifp);
555			ifma = if_findmulti(priv->ifp,
556			    (struct sockaddr *)&sa_dl);
557			IF_ADDR_UNLOCK(priv->ifp);
558			if (ifma != NULL) {
559				error = EADDRINUSE;
560			} else {
561				error = if_addmulti(priv->ifp,
562				    (struct sockaddr *)&sa_dl, &ifma);
563			}
564			break;
565		    }
566		case NGM_ETHER_DEL_MULTI:
567		    {
568			struct sockaddr_dl sa_dl;
569
570			if (msg->header.arglen != ETHER_ADDR_LEN) {
571				error = EINVAL;
572				break;
573			}
574			bzero(&sa_dl, sizeof(struct sockaddr_dl));
575			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
576			sa_dl.sdl_family = AF_LINK;
577			sa_dl.sdl_alen = ETHER_ADDR_LEN;
578			bcopy((void *)msg->data, LLADDR(&sa_dl),
579			    ETHER_ADDR_LEN);
580			error = if_delmulti(priv->ifp,
581			    (struct sockaddr *)&sa_dl);
582			break;
583		    }
584		case NGM_ETHER_DETACH:
585			ng_ether_detach(priv->ifp);
586			break;
587		default:
588			error = EINVAL;
589			break;
590		}
591		break;
592	default:
593		error = EINVAL;
594		break;
595	}
596	NG_RESPOND_MSG(error, node, item, resp);
597	NG_FREE_MSG(msg);
598	return (error);
599}
600
601/*
602 * Receive data on a hook.
603 * Since we use per-hook recveive methods this should never be called.
604 */
605static int
606ng_ether_rcvdata(hook_p hook, item_p item)
607{
608	NG_FREE_ITEM(item);
609
610	panic("%s: weird hook", __func__);
611#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
612	return (0);
613#endif
614}
615
616/*
617 * Handle an mbuf received on the "lower" or "orphan" hook.
618 */
619static int
620ng_ether_rcv_lower(hook_p hook, item_p item)
621{
622	struct mbuf *m;
623	const node_p node = NG_HOOK_NODE(hook);
624	const priv_p priv = NG_NODE_PRIVATE(node);
625 	struct ifnet *const ifp = priv->ifp;
626
627	NGI_GET_M(item, m);
628	NG_FREE_ITEM(item);
629
630	/* Check whether interface is ready for packets */
631
632	if (!((ifp->if_flags & IFF_UP) &&
633	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
634		NG_FREE_M(m);
635		return (ENETDOWN);
636	}
637
638	/* Make sure header is fully pulled up */
639	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
640		NG_FREE_M(m);
641		return (EINVAL);
642	}
643	if (m->m_len < sizeof(struct ether_header)
644	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
645		return (ENOBUFS);
646
647	/* Drop in the MAC address if desired */
648	if (priv->autoSrcAddr) {
649
650		/* Make the mbuf writable if it's not already */
651		if (!M_WRITABLE(m)
652		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
653			return (ENOBUFS);
654
655		/* Overwrite source MAC address */
656		bcopy(IF_LLADDR(ifp),
657		    mtod(m, struct ether_header *)->ether_shost,
658		    ETHER_ADDR_LEN);
659	}
660
661	/* Send it on its way */
662	return ether_output_frame(ifp, m);
663}
664
665/*
666 * Handle an mbuf received on the "upper" hook.
667 */
668static int
669ng_ether_rcv_upper(hook_p hook, item_p item)
670{
671	struct mbuf *m;
672	const node_p node = NG_HOOK_NODE(hook);
673	const priv_p priv = NG_NODE_PRIVATE(node);
674	struct ifnet *ifp = priv->ifp;
675
676	NGI_GET_M(item, m);
677	NG_FREE_ITEM(item);
678
679	/* Check length and pull off header */
680	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
681		NG_FREE_M(m);
682		return (EINVAL);
683	}
684	if (m->m_len < sizeof(struct ether_header) &&
685	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
686		return (ENOBUFS);
687
688	m->m_pkthdr.rcvif = ifp;
689
690	/* Pass the packet to the bridge, it may come back to us */
691	if (ifp->if_bridge) {
692		BRIDGE_INPUT(ifp, m);
693		if (m == NULL)
694			return (0);
695	}
696
697	/* Route packet back in */
698	ether_demux(ifp, m);
699	return (0);
700}
701
702/*
703 * Shutdown node. This resets the node but does not remove it
704 * unless the REALLY_DIE flag is set.
705 */
706static int
707ng_ether_shutdown(node_p node)
708{
709	const priv_p priv = NG_NODE_PRIVATE(node);
710
711	if (node->nd_flags & NGF_REALLY_DIE) {
712		/*
713		 * WE came here because the ethernet card is being unloaded,
714		 * so stop being persistant.
715		 * Actually undo all the things we did on creation.
716		 * Assume the ifp has already been freed.
717		 */
718		NG_NODE_SET_PRIVATE(node, NULL);
719		free(priv, M_NETGRAPH);
720		NG_NODE_UNREF(node);	/* free node itself */
721		return (0);
722	}
723	if (priv->promisc) {		/* disable promiscuous mode */
724		(void)ifpromisc(priv->ifp, 0);
725		priv->promisc = 0;
726	}
727	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
728	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
729
730	return (0);
731}
732
733/*
734 * Hook disconnection.
735 */
736static int
737ng_ether_disconnect(hook_p hook)
738{
739	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
740
741	if (hook == priv->upper) {
742		priv->upper = NULL;
743		if (priv->ifp != NULL)		/* restore h/w csum */
744			priv->ifp->if_hwassist = priv->hwassist;
745	} else if (hook == priv->lower)
746		priv->lower = NULL;
747	else if (hook == priv->orphan)
748		priv->orphan = NULL;
749	else
750		panic("%s: weird hook", __func__);
751	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
752	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
753		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
754	return (0);
755}
756
757/******************************************************************
758		    	INITIALIZATION
759******************************************************************/
760
761/*
762 * Handle loading and unloading for this node type.
763 */
764static int
765ng_ether_mod_event(module_t mod, int event, void *data)
766{
767	int error = 0;
768	int s;
769
770	s = splnet();
771	switch (event) {
772	case MOD_LOAD:
773
774		/* Register function hooks */
775		if (ng_ether_attach_p != NULL) {
776			error = EEXIST;
777			break;
778		}
779		ng_ether_attach_p = ng_ether_attach;
780		ng_ether_detach_p = ng_ether_detach;
781		ng_ether_output_p = ng_ether_output;
782		ng_ether_input_p = ng_ether_input;
783		ng_ether_input_orphan_p = ng_ether_input_orphan;
784		ng_ether_link_state_p = ng_ether_link_state;
785
786#ifndef VIMAGE_GLOBALS
787		vnet_mod_register(&vnet_ng_ether_modinfo);
788#else
789		error = ng_ether_iattach(NULL);
790#endif
791		break;
792
793	case MOD_UNLOAD:
794
795		/*
796		 * Note that the base code won't try to unload us until
797		 * all nodes have been removed, and that can't happen
798		 * until all Ethernet interfaces are removed. In any
799		 * case, we know there are no nodes left if the action
800		 * is MOD_UNLOAD, so there's no need to detach any nodes.
801		 */
802
803#ifndef VIMAGE_GLOBALS
804		vnet_mod_deregister(&vnet_ng_ether_modinfo);
805#endif
806
807		/* Unregister function hooks */
808		ng_ether_attach_p = NULL;
809		ng_ether_detach_p = NULL;
810		ng_ether_output_p = NULL;
811		ng_ether_input_p = NULL;
812		ng_ether_input_orphan_p = NULL;
813		ng_ether_link_state_p = NULL;
814		break;
815
816	default:
817		error = EOPNOTSUPP;
818		break;
819	}
820	splx(s);
821	return (error);
822}
823
824static int ng_ether_iattach(const void *unused)
825{
826	INIT_VNET_NET(curvnet);
827	struct ifnet *ifp;
828
829	/* Create nodes for any already-existing Ethernet interfaces. */
830	IFNET_RLOCK();
831	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
832		if (ifp->if_type == IFT_ETHER
833		    || ifp->if_type == IFT_L2VLAN)
834			ng_ether_attach(ifp);
835	}
836	IFNET_RUNLOCK();
837
838	return (0);
839}
840