ng_ether.c revision 141721
1222613Snwhitehorn
2222613Snwhitehorn/*
3222613Snwhitehorn * ng_ether.c
4222613Snwhitehorn */
5222613Snwhitehorn
6222613Snwhitehorn/*-
7222613Snwhitehorn * Copyright (c) 1996-2000 Whistle Communications, Inc.
8222613Snwhitehorn * All rights reserved.
9222613Snwhitehorn *
10222613Snwhitehorn * Subject to the following obligations and disclaimer of warranty, use and
11222613Snwhitehorn * redistribution of this software, in source or object code forms, with or
12222613Snwhitehorn * without modifications are expressly permitted by Whistle Communications;
13222613Snwhitehorn * provided, however, that:
14222613Snwhitehorn * 1. Any and all reproductions of the source or object code must include the
15222613Snwhitehorn *    copyright notice above and the following disclaimer of warranties; and
16222613Snwhitehorn * 2. No rights are granted, in any manner or form, to use Whistle
17222613Snwhitehorn *    Communications, Inc. trademarks, including the mark "WHISTLE
18222613Snwhitehorn *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19222613Snwhitehorn *    such appears in the above copyright notice or in the software.
20222613Snwhitehorn *
21222613Snwhitehorn * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22222613Snwhitehorn * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23222613Snwhitehorn * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24222613Snwhitehorn * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25222613Snwhitehorn * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26222613Snwhitehorn * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27222613Snwhitehorn * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28222613Snwhitehorn * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29222613Snwhitehorn * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30222613Snwhitehorn * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31222613Snwhitehorn * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32222613Snwhitehorn * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33222613Snwhitehorn * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34222613Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35223571Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36222613Snwhitehorn * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37222613Snwhitehorn * OF SUCH DAMAGE.
38222613Snwhitehorn *
39222613Snwhitehorn * Authors: Archie Cobbs <archie@freebsd.org>
40222613Snwhitehorn *	    Julian Elischer <julian@freebsd.org>
41222613Snwhitehorn *
42222613Snwhitehorn * $FreeBSD: head/sys/netgraph/ng_ether.c 141721 2005-02-12 11:41:32Z glebius $
43223571Snwhitehorn */
44222613Snwhitehorn
45222613Snwhitehorn/*
46222613Snwhitehorn * ng_ether(4) netgraph node type
47222613Snwhitehorn */
48222613Snwhitehorn
49222613Snwhitehorn#include <sys/param.h>
50227293Sed#include <sys/systm.h>
51222613Snwhitehorn#include <sys/kernel.h>
52222613Snwhitehorn#include <sys/malloc.h>
53222613Snwhitehorn#include <sys/mbuf.h>
54222613Snwhitehorn#include <sys/errno.h>
55222613Snwhitehorn#include <sys/syslog.h>
56222613Snwhitehorn#include <sys/socket.h>
57222613Snwhitehorn
58222613Snwhitehorn#include <net/bridge.h>
59222613Snwhitehorn#include <net/if.h>
60222613Snwhitehorn#include <net/if_dl.h>
61222613Snwhitehorn#include <net/if_types.h>
62222613Snwhitehorn#include <net/if_arp.h>
63222613Snwhitehorn#include <net/if_var.h>
64222613Snwhitehorn#include <net/ethernet.h>
65223571Snwhitehorn
66223571Snwhitehorn#include <netgraph/ng_message.h>
67222613Snwhitehorn#include <netgraph/netgraph.h>
68222613Snwhitehorn#include <netgraph/ng_parse.h>
69222613Snwhitehorn#include <netgraph/ng_ether.h>
70222613Snwhitehorn
71222613Snwhitehorn#define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
72222613Snwhitehorn#define IFP2NG_SET(ifp, val)	(((struct arpcom *)(ifp))->ac_netgraph = (val))
73222613Snwhitehorn
74222613Snwhitehorn/* Per-node private data */
75222613Snwhitehornstruct private {
76222613Snwhitehorn	struct ifnet	*ifp;		/* associated interface */
77222613Snwhitehorn	hook_p		upper;		/* upper hook connection */
78222613Snwhitehorn	hook_p		lower;		/* lower hook connection */
79222613Snwhitehorn	hook_p		orphan;		/* orphan hook connection */
80222613Snwhitehorn	u_char		autoSrcAddr;	/* always overwrite source address */
81222613Snwhitehorn	u_char		promisc;	/* promiscuous mode enabled */
82222613Snwhitehorn	u_long		hwassist;	/* hardware checksum capabilities */
83222613Snwhitehorn	u_int		flags;		/* flags e.g. really die */
84222613Snwhitehorn};
85222613Snwhitehorntypedef struct private *priv_p;
86222613Snwhitehorn
87222613Snwhitehorn/* Hook pointers used by if_ethersubr.c to callback to netgraph */
88222613Snwhitehornextern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
89222613Snwhitehornextern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
90222613Snwhitehornextern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
91222613Snwhitehornextern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
92222613Snwhitehornextern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
93222613Snwhitehornextern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
94222613Snwhitehorn
95222613Snwhitehorn/* Functional hooks called from if_ethersubr.c */
96255416Snwhitehornstatic void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
97222613Snwhitehornstatic void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
98222613Snwhitehornstatic int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
99222613Snwhitehornstatic void	ng_ether_attach(struct ifnet *ifp);
100222613Snwhitehornstatic void	ng_ether_detach(struct ifnet *ifp);
101222613Snwhitehornstatic void	ng_ether_link_state(struct ifnet *ifp, int state);
102222613Snwhitehorn
103222613Snwhitehorn/* Other functions */
104222613Snwhitehornstatic int	ng_ether_rcv_lower(node_p node, struct mbuf *m);
105222613Snwhitehornstatic int	ng_ether_rcv_upper(node_p node, struct mbuf *m);
106222613Snwhitehorn
107222613Snwhitehorn/* Netgraph node methods */
108222613Snwhitehornstatic ng_constructor_t	ng_ether_constructor;
109222613Snwhitehornstatic ng_rcvmsg_t	ng_ether_rcvmsg;
110222613Snwhitehornstatic ng_shutdown_t	ng_ether_shutdown;
111222613Snwhitehornstatic ng_newhook_t	ng_ether_newhook;
112222613Snwhitehornstatic ng_connect_t	ng_ether_connect;
113222613Snwhitehornstatic ng_rcvdata_t	ng_ether_rcvdata;
114222613Snwhitehornstatic ng_disconnect_t	ng_ether_disconnect;
115222613Snwhitehornstatic int		ng_ether_mod_event(module_t mod, int event, void *data);
116222613Snwhitehorn
117222613Snwhitehorn/* List of commands and how to convert arguments to/from ASCII */
118222613Snwhitehornstatic const struct ng_cmdlist ng_ether_cmdlist[] = {
119222613Snwhitehorn	{
120222613Snwhitehorn	  NGM_ETHER_COOKIE,
121222613Snwhitehorn	  NGM_ETHER_GET_IFNAME,
122222613Snwhitehorn	  "getifname",
123222613Snwhitehorn	  NULL,
124222613Snwhitehorn	  &ng_parse_string_type
125222613Snwhitehorn	},
126222613Snwhitehorn	{
127222613Snwhitehorn	  NGM_ETHER_COOKIE,
128222613Snwhitehorn	  NGM_ETHER_GET_IFINDEX,
129222613Snwhitehorn	  "getifindex",
130222613Snwhitehorn	  NULL,
131222613Snwhitehorn	  &ng_parse_int32_type
132222613Snwhitehorn	},
133222613Snwhitehorn	{
134222613Snwhitehorn	  NGM_ETHER_COOKIE,
135222613Snwhitehorn	  NGM_ETHER_GET_ENADDR,
136222613Snwhitehorn	  "getenaddr",
137222613Snwhitehorn	  NULL,
138222613Snwhitehorn	  &ng_parse_enaddr_type
139222613Snwhitehorn	},
140222613Snwhitehorn	{
141222613Snwhitehorn	  NGM_ETHER_COOKIE,
142222613Snwhitehorn	  NGM_ETHER_SET_ENADDR,
143222613Snwhitehorn	  "setenaddr",
144222613Snwhitehorn	  &ng_parse_enaddr_type,
145222613Snwhitehorn	  NULL
146222613Snwhitehorn	},
147222613Snwhitehorn	{
148222613Snwhitehorn	  NGM_ETHER_COOKIE,
149222613Snwhitehorn	  NGM_ETHER_GET_PROMISC,
150222613Snwhitehorn	  "getpromisc",
151222613Snwhitehorn	  NULL,
152222613Snwhitehorn	  &ng_parse_int32_type
153222613Snwhitehorn	},
154222613Snwhitehorn	{
155222613Snwhitehorn	  NGM_ETHER_COOKIE,
156222613Snwhitehorn	  NGM_ETHER_SET_PROMISC,
157222613Snwhitehorn	  "setpromisc",
158222613Snwhitehorn	  &ng_parse_int32_type,
159222613Snwhitehorn	  NULL
160222613Snwhitehorn	},
161222613Snwhitehorn	{
162222613Snwhitehorn	  NGM_ETHER_COOKIE,
163222613Snwhitehorn	  NGM_ETHER_GET_AUTOSRC,
164222613Snwhitehorn	  "getautosrc",
165222613Snwhitehorn	  NULL,
166222613Snwhitehorn	  &ng_parse_int32_type
167222613Snwhitehorn	},
168222613Snwhitehorn	{
169222613Snwhitehorn	  NGM_ETHER_COOKIE,
170222613Snwhitehorn	  NGM_ETHER_SET_AUTOSRC,
171222613Snwhitehorn	  "setautosrc",
172222613Snwhitehorn	  &ng_parse_int32_type,
173222613Snwhitehorn	  NULL
174222613Snwhitehorn	},
175222613Snwhitehorn	{
176222613Snwhitehorn	  NGM_ETHER_COOKIE,
177222613Snwhitehorn	  NGM_ETHER_ADD_MULTI,
178222613Snwhitehorn	  "addmulti",
179222613Snwhitehorn	  &ng_parse_enaddr_type,
180222613Snwhitehorn	  NULL
181222613Snwhitehorn	},
182222613Snwhitehorn	{
183222613Snwhitehorn	  NGM_ETHER_COOKIE,
184222613Snwhitehorn	  NGM_ETHER_DEL_MULTI,
185222613Snwhitehorn	  "delmulti",
186222613Snwhitehorn	  &ng_parse_enaddr_type,
187222613Snwhitehorn	  NULL
188222613Snwhitehorn	},
189222613Snwhitehorn	{ 0 }
190222613Snwhitehorn};
191222613Snwhitehorn
192222613Snwhitehornstatic struct ng_type ng_ether_typestruct = {
193222613Snwhitehorn	.version =	NG_ABI_VERSION,
194222613Snwhitehorn	.name =		NG_ETHER_NODE_TYPE,
195259258Sandreast	.mod_event =	ng_ether_mod_event,
196222613Snwhitehorn	.constructor =	ng_ether_constructor,
197222613Snwhitehorn	.rcvmsg =	ng_ether_rcvmsg,
198222613Snwhitehorn	.shutdown =	ng_ether_shutdown,
199222613Snwhitehorn	.newhook =	ng_ether_newhook,
200222613Snwhitehorn	.connect =	ng_ether_connect,
201222613Snwhitehorn	.rcvdata =	ng_ether_rcvdata,
202222613Snwhitehorn	.disconnect =	ng_ether_disconnect,
203222613Snwhitehorn	.cmdlist =	ng_ether_cmdlist,
204222613Snwhitehorn};
205222613SnwhitehornNETGRAPH_INIT(ether, &ng_ether_typestruct);
206222613Snwhitehorn
207222613Snwhitehorn/******************************************************************
208222613Snwhitehorn		    ETHERNET FUNCTION HOOKS
209222613Snwhitehorn******************************************************************/
210222613Snwhitehorn
211255416Snwhitehorn/*
212222613Snwhitehorn * Handle a packet that has come in on an interface. We get to
213222613Snwhitehorn * look at it here before any upper layer protocols do.
214222613Snwhitehorn *
215222613Snwhitehorn * NOTE: this function will get called at splimp()
216222613Snwhitehorn */
217222613Snwhitehornstatic void
218222613Snwhitehornng_ether_input(struct ifnet *ifp, struct mbuf **mp)
219222613Snwhitehorn{
220222613Snwhitehorn	const node_p node = IFP2NG(ifp);
221223571Snwhitehorn	const priv_p priv = NG_NODE_PRIVATE(node);
222223571Snwhitehorn	int error;
223223571Snwhitehorn
224259258Sandreast	/* If "lower" hook not connected, let packet continue */
225223571Snwhitehorn	if (priv->lower == NULL)
226223571Snwhitehorn		return;
227223571Snwhitehorn	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
228223571Snwhitehorn}
229223571Snwhitehorn
230223571Snwhitehorn/*
231223571Snwhitehorn * Handle a packet that has come in on an interface, and which
232259258Sandreast * does not match any of our known protocols (an ``orphan'').
233223571Snwhitehorn *
234223571Snwhitehorn * NOTE: this function will get called at splimp()
235222613Snwhitehorn */
236255416Snwhitehornstatic void
237222613Snwhitehornng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
238222613Snwhitehorn{
239222613Snwhitehorn	const node_p node = IFP2NG(ifp);
240222613Snwhitehorn	const priv_p priv = NG_NODE_PRIVATE(node);
241222613Snwhitehorn	int error;
242222613Snwhitehorn
243222613Snwhitehorn	/* If "orphan" hook not connected, discard packet */
244222613Snwhitehorn	if (priv->orphan == NULL) {
245222613Snwhitehorn		m_freem(m);
246222613Snwhitehorn		return;
247222613Snwhitehorn	}
248222613Snwhitehorn	NG_SEND_DATA_ONLY(error, priv->orphan, m);
249222613Snwhitehorn}
250222613Snwhitehorn
251222613Snwhitehorn/*
252222613Snwhitehorn * Handle a packet that is going out on an interface.
253222613Snwhitehorn * The Ethernet header is already attached to the mbuf.
254222613Snwhitehorn */
255222613Snwhitehornstatic int
256222613Snwhitehornng_ether_output(struct ifnet *ifp, struct mbuf **mp)
257222613Snwhitehorn{
258222613Snwhitehorn	const node_p node = IFP2NG(ifp);
259222613Snwhitehorn	const priv_p priv = NG_NODE_PRIVATE(node);
260222613Snwhitehorn	int error = 0;
261222613Snwhitehorn
262	/* If "upper" hook not connected, let packet continue */
263	if (priv->upper == NULL)
264		return (0);
265
266	/* Send it out "upper" hook */
267	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
268	return (error);
269}
270
271/*
272 * A new Ethernet interface has been attached.
273 * Create a new node for it, etc.
274 */
275static void
276ng_ether_attach(struct ifnet *ifp)
277{
278	priv_p priv;
279	node_p node;
280
281	/* Create node */
282	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
283	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
284		log(LOG_ERR, "%s: can't %s for %s\n",
285		    __func__, "create node", ifp->if_xname);
286		return;
287	}
288
289	/* Allocate private data */
290	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
291	if (priv == NULL) {
292		log(LOG_ERR, "%s: can't %s for %s\n",
293		    __func__, "allocate memory", ifp->if_xname);
294		NG_NODE_UNREF(node);
295		return;
296	}
297	NG_NODE_SET_PRIVATE(node, priv);
298	priv->ifp = ifp;
299	IFP2NG_SET(ifp, node);
300	priv->autoSrcAddr = 1;
301	priv->hwassist = ifp->if_hwassist;
302
303	/* Try to give the node the same name as the interface */
304	if (ng_name_node(node, ifp->if_xname) != 0) {
305		log(LOG_WARNING, "%s: can't name node %s\n",
306		    __func__, ifp->if_xname);
307	}
308}
309
310/*
311 * An Ethernet interface is being detached.
312 * REALLY Destroy its node.
313 */
314static void
315ng_ether_detach(struct ifnet *ifp)
316{
317	const node_p node = IFP2NG(ifp);
318	const priv_p priv = NG_NODE_PRIVATE(node);
319
320	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
321	/*
322	 * We can't assume the ifnet is still around when we run shutdown
323	 * So zap it now. XXX We HOPE that anything running at this time
324	 * handles it (as it should in the non netgraph case).
325	 */
326	IFP2NG_SET(ifp, NULL);
327	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
328	ng_rmnode_self(node);		/* remove all netgraph parts */
329}
330
331/*
332 * Notify graph about link event.
333 * if_link_state_change() has already checked that the state has changed.
334 */
335static void
336ng_ether_link_state(struct ifnet *ifp, int state)
337{
338	const node_p node = IFP2NG(ifp);
339	const priv_p priv = NG_NODE_PRIVATE(node);
340	struct ng_mesg *msg;
341	int cmd, dummy_error = 0;
342
343	if (priv->lower == NULL)
344                return;
345
346	if (state == LINK_STATE_UP)
347		cmd = NGM_LINK_IS_UP;
348	else if (state == LINK_STATE_DOWN)
349		cmd = NGM_LINK_IS_DOWN;
350	else
351		return;
352
353	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
354	if (msg != NULL)
355		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
356}
357
358/******************************************************************
359		    NETGRAPH NODE METHODS
360******************************************************************/
361
362/*
363 * It is not possible or allowable to create a node of this type.
364 * Nodes get created when the interface is attached (or, when
365 * this node type's KLD is loaded).
366 */
367static int
368ng_ether_constructor(node_p node)
369{
370	return (EINVAL);
371}
372
373/*
374 * Check for attaching a new hook.
375 */
376static	int
377ng_ether_newhook(node_p node, hook_p hook, const char *name)
378{
379	const priv_p priv = NG_NODE_PRIVATE(node);
380	hook_p *hookptr;
381
382	/* Divert hook is an alias for lower */
383	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
384		name = NG_ETHER_HOOK_LOWER;
385
386	/* Which hook? */
387	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
388		hookptr = &priv->upper;
389	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
390		hookptr = &priv->lower;
391	else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
392		hookptr = &priv->orphan;
393	else
394		return (EINVAL);
395
396	/* Check if already connected (shouldn't be, but doesn't hurt) */
397	if (*hookptr != NULL)
398		return (EISCONN);
399
400	/* Disable hardware checksums while 'upper' hook is connected */
401	if (hookptr == &priv->upper)
402		priv->ifp->if_hwassist = 0;
403
404	/* OK */
405	*hookptr = hook;
406	return (0);
407}
408
409/*
410 * Hooks are attached, adjust to force queueing.
411 * We don't really care which hook it is.
412 * they should all be queuing for outgoing data.
413 */
414static	int
415ng_ether_connect(hook_p hook)
416{
417	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
418	return (0);
419}
420
421/*
422 * Receive an incoming control message.
423 */
424static int
425ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
426{
427	const priv_p priv = NG_NODE_PRIVATE(node);
428	struct ng_mesg *resp = NULL;
429	int error = 0;
430	struct ng_mesg *msg;
431
432	NGI_GET_MSG(item, msg);
433	switch (msg->header.typecookie) {
434	case NGM_ETHER_COOKIE:
435		switch (msg->header.cmd) {
436		case NGM_ETHER_GET_IFNAME:
437			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
438			if (resp == NULL) {
439				error = ENOMEM;
440				break;
441			}
442			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
443			break;
444		case NGM_ETHER_GET_IFINDEX:
445			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
446			if (resp == NULL) {
447				error = ENOMEM;
448				break;
449			}
450			*((u_int32_t *)resp->data) = priv->ifp->if_index;
451			break;
452		case NGM_ETHER_GET_ENADDR:
453			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
454			if (resp == NULL) {
455				error = ENOMEM;
456				break;
457			}
458			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
459			    resp->data, ETHER_ADDR_LEN);
460			break;
461		case NGM_ETHER_SET_ENADDR:
462		    {
463			if (msg->header.arglen != ETHER_ADDR_LEN) {
464				error = EINVAL;
465				break;
466			}
467			error = if_setlladdr(priv->ifp,
468			    (u_char *)msg->data, ETHER_ADDR_LEN);
469			break;
470		    }
471		case NGM_ETHER_GET_PROMISC:
472			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
473			if (resp == NULL) {
474				error = ENOMEM;
475				break;
476			}
477			*((u_int32_t *)resp->data) = priv->promisc;
478			break;
479		case NGM_ETHER_SET_PROMISC:
480		    {
481			u_char want;
482
483			if (msg->header.arglen != sizeof(u_int32_t)) {
484				error = EINVAL;
485				break;
486			}
487			want = !!*((u_int32_t *)msg->data);
488			if (want ^ priv->promisc) {
489				if ((error = ifpromisc(priv->ifp, want)) != 0)
490					break;
491				priv->promisc = want;
492			}
493			break;
494		    }
495		case NGM_ETHER_GET_AUTOSRC:
496			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
497			if (resp == NULL) {
498				error = ENOMEM;
499				break;
500			}
501			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
502			break;
503		case NGM_ETHER_SET_AUTOSRC:
504			if (msg->header.arglen != sizeof(u_int32_t)) {
505				error = EINVAL;
506				break;
507			}
508			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
509			break;
510		case NGM_ETHER_ADD_MULTI:
511		    {
512			struct sockaddr_dl sa_dl;
513			struct ifmultiaddr *ifm;
514
515			if (msg->header.arglen != ETHER_ADDR_LEN) {
516				error = EINVAL;
517				break;
518			}
519			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
520			sa_dl.sdl_family = AF_LINK;
521			sa_dl.sdl_index = 0;
522			sa_dl.sdl_nlen = 0;
523			sa_dl.sdl_alen = 6;
524			sa_dl.sdl_slen = 0;
525			bcopy((void *)msg->data, LLADDR(&sa_dl),
526			    ETHER_ADDR_LEN);
527			error = if_addmulti(priv->ifp,
528			    (struct sockaddr *)&sa_dl, &ifm);
529			break;
530		    }
531		case NGM_ETHER_DEL_MULTI:
532		    {
533			struct sockaddr_dl sa_dl;
534
535			if (msg->header.arglen != ETHER_ADDR_LEN) {
536				error = EINVAL;
537				break;
538			}
539			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
540			sa_dl.sdl_family = AF_LINK;
541			sa_dl.sdl_index = 0;
542			sa_dl.sdl_nlen = 0;
543			sa_dl.sdl_alen = 6;
544			sa_dl.sdl_slen = 0;
545			bcopy((void *)msg->data, LLADDR(&sa_dl),
546			    ETHER_ADDR_LEN);
547			error = if_delmulti(priv->ifp,
548			    (struct sockaddr *)&sa_dl);
549			break;
550		    }
551		default:
552			error = EINVAL;
553			break;
554		}
555		break;
556	default:
557		error = EINVAL;
558		break;
559	}
560	NG_RESPOND_MSG(error, node, item, resp);
561	NG_FREE_MSG(msg);
562	return (error);
563}
564
565/*
566 * Receive data on a hook.
567 */
568static int
569ng_ether_rcvdata(hook_p hook, item_p item)
570{
571	const node_p node = NG_HOOK_NODE(hook);
572	const priv_p priv = NG_NODE_PRIVATE(node);
573	struct mbuf *m;
574
575	NGI_GET_M(item, m);
576	NG_FREE_ITEM(item);
577
578	if (hook == priv->lower || hook == priv->orphan)
579		return ng_ether_rcv_lower(node, m);
580	if (hook == priv->upper)
581		return ng_ether_rcv_upper(node, m);
582	panic("%s: weird hook", __func__);
583#ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
584	return (0);
585#endif
586}
587
588/*
589 * Handle an mbuf received on the "lower" or "orphan" hook.
590 */
591static int
592ng_ether_rcv_lower(node_p node, struct mbuf *m)
593{
594	const priv_p priv = NG_NODE_PRIVATE(node);
595 	struct ifnet *const ifp = priv->ifp;
596
597	/* Check whether interface is ready for packets */
598	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
599		NG_FREE_M(m);
600		return (ENETDOWN);
601	}
602
603	/* Make sure header is fully pulled up */
604	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
605		NG_FREE_M(m);
606		return (EINVAL);
607	}
608	if (m->m_len < sizeof(struct ether_header)
609	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
610		return (ENOBUFS);
611
612	/* Drop in the MAC address if desired */
613	if (priv->autoSrcAddr) {
614
615		/* Make the mbuf writable if it's not already */
616		if (!M_WRITABLE(m)
617		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
618			return (ENOBUFS);
619
620		/* Overwrite source MAC address */
621		bcopy((IFP2AC(ifp))->ac_enaddr,
622		    mtod(m, struct ether_header *)->ether_shost,
623		    ETHER_ADDR_LEN);
624	}
625
626	/* Send it on its way */
627	return ether_output_frame(ifp, m);
628}
629
630/*
631 * Handle an mbuf received on the "upper" hook.
632 */
633static int
634ng_ether_rcv_upper(node_p node, struct mbuf *m)
635{
636	const priv_p priv = NG_NODE_PRIVATE(node);
637
638	m->m_pkthdr.rcvif = priv->ifp;
639
640	if (BDG_ACTIVE(priv->ifp) )
641		if ((m = bridge_in_ptr(priv->ifp, m)) == NULL)
642			return (0);
643
644	/* Route packet back in */
645	ether_demux(priv->ifp, m);
646	return (0);
647}
648
649/*
650 * Shutdown node. This resets the node but does not remove it
651 * unless the REALLY_DIE flag is set.
652 */
653static int
654ng_ether_shutdown(node_p node)
655{
656	const priv_p priv = NG_NODE_PRIVATE(node);
657
658	if (node->nd_flags & NGF_REALLY_DIE) {
659		/*
660		 * WE came here because the ethernet card is being unloaded,
661		 * so stop being persistant.
662		 * Actually undo all the things we did on creation.
663		 * Assume the ifp has already been freed.
664		 */
665		NG_NODE_SET_PRIVATE(node, NULL);
666		FREE(priv, M_NETGRAPH);
667		NG_NODE_UNREF(node);	/* free node itself */
668		return (0);
669	}
670	if (priv->promisc) {		/* disable promiscuous mode */
671		(void)ifpromisc(priv->ifp, 0);
672		priv->promisc = 0;
673	}
674	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
675	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
676
677	return (0);
678}
679
680/*
681 * Hook disconnection.
682 */
683static int
684ng_ether_disconnect(hook_p hook)
685{
686	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
687
688	if (hook == priv->upper) {
689		priv->upper = NULL;
690		if (priv->ifp != NULL)		/* restore h/w csum */
691			priv->ifp->if_hwassist = priv->hwassist;
692	} else if (hook == priv->lower)
693		priv->lower = NULL;
694	else if (hook == priv->orphan)
695		priv->orphan = NULL;
696	else
697		panic("%s: weird hook", __func__);
698	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
699	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
700		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
701	return (0);
702}
703
704/******************************************************************
705		    	INITIALIZATION
706******************************************************************/
707
708/*
709 * Handle loading and unloading for this node type.
710 */
711static int
712ng_ether_mod_event(module_t mod, int event, void *data)
713{
714	struct ifnet *ifp;
715	int error = 0;
716	int s;
717
718	s = splnet();
719	switch (event) {
720	case MOD_LOAD:
721
722		/* Register function hooks */
723		if (ng_ether_attach_p != NULL) {
724			error = EEXIST;
725			break;
726		}
727		ng_ether_attach_p = ng_ether_attach;
728		ng_ether_detach_p = ng_ether_detach;
729		ng_ether_output_p = ng_ether_output;
730		ng_ether_input_p = ng_ether_input;
731		ng_ether_input_orphan_p = ng_ether_input_orphan;
732		ng_ether_link_state_p = ng_ether_link_state;
733
734		/* Create nodes for any already-existing Ethernet interfaces */
735		IFNET_RLOCK();
736		TAILQ_FOREACH(ifp, &ifnet, if_link) {
737			if (ifp->if_type == IFT_ETHER
738			    || ifp->if_type == IFT_L2VLAN)
739				ng_ether_attach(ifp);
740		}
741		IFNET_RUNLOCK();
742		break;
743
744	case MOD_UNLOAD:
745
746		/*
747		 * Note that the base code won't try to unload us until
748		 * all nodes have been removed, and that can't happen
749		 * until all Ethernet interfaces are removed. In any
750		 * case, we know there are no nodes left if the action
751		 * is MOD_UNLOAD, so there's no need to detach any nodes.
752		 */
753
754		/* Unregister function hooks */
755		ng_ether_attach_p = NULL;
756		ng_ether_detach_p = NULL;
757		ng_ether_output_p = NULL;
758		ng_ether_input_p = NULL;
759		ng_ether_input_orphan_p = NULL;
760		ng_ether_link_state_p = NULL;
761		break;
762
763	default:
764		error = EOPNOTSUPP;
765		break;
766	}
767	splx(s);
768	return (error);
769}
770
771