xref: /freebsd/sys/netgraph/ng_ether.c (revision 30bc1032a97e3a1b18af9cf83624c62003630cd9)
1e1e1452dSArchie Cobbs 
2e1e1452dSArchie Cobbs /*
3e1e1452dSArchie Cobbs  * ng_ether.c
4c398230bSWarner Losh  */
5c398230bSWarner Losh 
6c398230bSWarner Losh /*-
7e1e1452dSArchie Cobbs  * Copyright (c) 1996-2000 Whistle Communications, Inc.
8e1e1452dSArchie Cobbs  * All rights reserved.
9e1e1452dSArchie Cobbs  *
10e1e1452dSArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
11e1e1452dSArchie Cobbs  * redistribution of this software, in source or object code forms, with or
12e1e1452dSArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
13e1e1452dSArchie Cobbs  * provided, however, that:
14e1e1452dSArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
15e1e1452dSArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
16e1e1452dSArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
17e1e1452dSArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
18e1e1452dSArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19e1e1452dSArchie Cobbs  *    such appears in the above copyright notice or in the software.
20e1e1452dSArchie Cobbs  *
21e1e1452dSArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22e1e1452dSArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23e1e1452dSArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24e1e1452dSArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25e1e1452dSArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26e1e1452dSArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27e1e1452dSArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28e1e1452dSArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29e1e1452dSArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30e1e1452dSArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31e1e1452dSArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32e1e1452dSArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33e1e1452dSArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34e1e1452dSArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35e1e1452dSArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36e1e1452dSArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37e1e1452dSArchie Cobbs  * OF SUCH DAMAGE.
38e1e1452dSArchie Cobbs  *
39e1e1452dSArchie Cobbs  * Authors: Archie Cobbs <archie@freebsd.org>
40e1e1452dSArchie Cobbs  *	    Julian Elischer <julian@freebsd.org>
41e1e1452dSArchie Cobbs  *
42e1e1452dSArchie Cobbs  * $FreeBSD$
43e1e1452dSArchie Cobbs  */
44e1e1452dSArchie Cobbs 
45e1e1452dSArchie Cobbs /*
46e1e1452dSArchie Cobbs  * ng_ether(4) netgraph node type
47e1e1452dSArchie Cobbs  */
48e1e1452dSArchie Cobbs 
49e1e1452dSArchie Cobbs #include <sys/param.h>
50e1e1452dSArchie Cobbs #include <sys/systm.h>
51e1e1452dSArchie Cobbs #include <sys/kernel.h>
52e1e1452dSArchie Cobbs #include <sys/malloc.h>
53e1e1452dSArchie Cobbs #include <sys/mbuf.h>
54e1e1452dSArchie Cobbs #include <sys/errno.h>
55530c0060SRobert Watson #include <sys/proc.h>
56e1e1452dSArchie Cobbs #include <sys/syslog.h>
57e1e1452dSArchie Cobbs #include <sys/socket.h>
582cdf8c49SMarko Zec #include <sys/taskqueue.h>
59e1e1452dSArchie Cobbs 
60e1e1452dSArchie Cobbs #include <net/if.h>
61810d5e89SGleb Smirnoff #include <net/if_dl.h>
62e1e1452dSArchie Cobbs #include <net/if_types.h>
63e1e1452dSArchie Cobbs #include <net/if_arp.h>
64e1e1452dSArchie Cobbs #include <net/if_var.h>
65e1e1452dSArchie Cobbs #include <net/ethernet.h>
66fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
674b79449eSBjoern A. Zeeb #include <net/vnet.h>
68e1e1452dSArchie Cobbs 
69e1e1452dSArchie Cobbs #include <netgraph/ng_message.h>
70e1e1452dSArchie Cobbs #include <netgraph/netgraph.h>
71e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h>
72e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h>
73e1e1452dSArchie Cobbs 
7439b553ceSEd Maste MODULE_VERSION(ng_ether, 1);
7539b553ceSEd Maste 
765240dcdbSRuslan Ermilov #define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
77e1e1452dSArchie Cobbs 
783c976c3fSPawel Jakub Dawidek /* Per-node private data */
793c976c3fSPawel Jakub Dawidek struct private {
803c976c3fSPawel Jakub Dawidek 	struct ifnet	*ifp;		/* associated interface */
813c976c3fSPawel Jakub Dawidek 	hook_p		upper;		/* upper hook connection */
821a292b80SArchie Cobbs 	hook_p		lower;		/* lower hook connection */
831a292b80SArchie Cobbs 	hook_p		orphan;		/* orphan hook connection */
843c976c3fSPawel Jakub Dawidek 	u_char		autoSrcAddr;	/* always overwrite source address */
853c976c3fSPawel Jakub Dawidek 	u_char		promisc;	/* promiscuous mode enabled */
863c976c3fSPawel Jakub Dawidek 	u_long		hwassist;	/* hardware checksum capabilities */
873c976c3fSPawel Jakub Dawidek 	u_int		flags;		/* flags e.g. really die */
883c976c3fSPawel Jakub Dawidek };
893c976c3fSPawel Jakub Dawidek typedef struct private *priv_p;
90e1e1452dSArchie Cobbs 
91edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */
92edbb5246SSam Leffler extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
93edbb5246SSam Leffler extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
94edbb5246SSam Leffler extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
95edbb5246SSam Leffler extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
96edbb5246SSam Leffler extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
971c7899c7SGleb Smirnoff extern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
98edbb5246SSam Leffler 
99e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */
100edbb5246SSam Leffler static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
101edbb5246SSam Leffler static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
102e1e1452dSArchie Cobbs static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
103e1e1452dSArchie Cobbs static void	ng_ether_attach(struct ifnet *ifp);
104e1e1452dSArchie Cobbs static void	ng_ether_detach(struct ifnet *ifp);
1051c7899c7SGleb Smirnoff static void	ng_ether_link_state(struct ifnet *ifp, int state);
106e1e1452dSArchie Cobbs 
107e1e1452dSArchie Cobbs /* Other functions */
108f664dcdeSJulian Elischer static int	ng_ether_rcv_lower(hook_p node, item_p item);
109f664dcdeSJulian Elischer static int	ng_ether_rcv_upper(hook_p node, item_p item);
110e1e1452dSArchie Cobbs 
111e1e1452dSArchie Cobbs /* Netgraph node methods */
112e1e1452dSArchie Cobbs static ng_constructor_t	ng_ether_constructor;
113e1e1452dSArchie Cobbs static ng_rcvmsg_t	ng_ether_rcvmsg;
114069154d5SJulian Elischer static ng_shutdown_t	ng_ether_shutdown;
115e1e1452dSArchie Cobbs static ng_newhook_t	ng_ether_newhook;
116e1e1452dSArchie Cobbs static ng_rcvdata_t	ng_ether_rcvdata;
117e1e1452dSArchie Cobbs static ng_disconnect_t	ng_ether_disconnect;
118e1e1452dSArchie Cobbs static int		ng_ether_mod_event(module_t mod, int event, void *data);
119e1e1452dSArchie Cobbs 
120499f60b1SAndriy Gapon static eventhandler_tag	ng_ether_ifnet_arrival_cookie;
121499f60b1SAndriy Gapon 
122e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
123e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = {
124e1e1452dSArchie Cobbs 	{
125e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
126e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFNAME,
127e1e1452dSArchie Cobbs 	  "getifname",
128e1e1452dSArchie Cobbs 	  NULL,
129e1e1452dSArchie Cobbs 	  &ng_parse_string_type
130e1e1452dSArchie Cobbs 	},
131e1e1452dSArchie Cobbs 	{
132e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
133e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFINDEX,
134e1e1452dSArchie Cobbs 	  "getifindex",
135e1e1452dSArchie Cobbs 	  NULL,
136e1e1452dSArchie Cobbs 	  &ng_parse_int32_type
137e1e1452dSArchie Cobbs 	},
1384b39c3c7SArchie Cobbs 	{
1394b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
1404b39c3c7SArchie Cobbs 	  NGM_ETHER_GET_ENADDR,
1414b39c3c7SArchie Cobbs 	  "getenaddr",
1424b39c3c7SArchie Cobbs 	  NULL,
1438c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type
1444b39c3c7SArchie Cobbs 	},
1454b39c3c7SArchie Cobbs 	{
1464b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
14756045c66SArchie Cobbs 	  NGM_ETHER_SET_ENADDR,
14856045c66SArchie Cobbs 	  "setenaddr",
1498c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type,
15056045c66SArchie Cobbs 	  NULL
15156045c66SArchie Cobbs 	},
15256045c66SArchie Cobbs 	{
15356045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
15456045c66SArchie Cobbs 	  NGM_ETHER_GET_PROMISC,
15556045c66SArchie Cobbs 	  "getpromisc",
15656045c66SArchie Cobbs 	  NULL,
15756045c66SArchie Cobbs 	  &ng_parse_int32_type
15856045c66SArchie Cobbs 	},
15956045c66SArchie Cobbs 	{
16056045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1614b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_PROMISC,
1624b39c3c7SArchie Cobbs 	  "setpromisc",
1634b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1644b39c3c7SArchie Cobbs 	  NULL
1654b39c3c7SArchie Cobbs 	},
1664b39c3c7SArchie Cobbs 	{
1674b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
16856045c66SArchie Cobbs 	  NGM_ETHER_GET_AUTOSRC,
16956045c66SArchie Cobbs 	  "getautosrc",
17056045c66SArchie Cobbs 	  NULL,
17156045c66SArchie Cobbs 	  &ng_parse_int32_type
17256045c66SArchie Cobbs 	},
17356045c66SArchie Cobbs 	{
17456045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1754b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_AUTOSRC,
1764b39c3c7SArchie Cobbs 	  "setautosrc",
1774b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1784b39c3c7SArchie Cobbs 	  NULL
1794b39c3c7SArchie Cobbs 	},
180810d5e89SGleb Smirnoff 	{
181810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
182810d5e89SGleb Smirnoff 	  NGM_ETHER_ADD_MULTI,
183810d5e89SGleb Smirnoff 	  "addmulti",
184810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
185810d5e89SGleb Smirnoff 	  NULL
186810d5e89SGleb Smirnoff 	},
187810d5e89SGleb Smirnoff 	{
188810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
189810d5e89SGleb Smirnoff 	  NGM_ETHER_DEL_MULTI,
190810d5e89SGleb Smirnoff 	  "delmulti",
191810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
192810d5e89SGleb Smirnoff 	  NULL
193810d5e89SGleb Smirnoff 	},
194cefddd66SGleb Smirnoff 	{
195cefddd66SGleb Smirnoff 	  NGM_ETHER_COOKIE,
196cefddd66SGleb Smirnoff 	  NGM_ETHER_DETACH,
197cefddd66SGleb Smirnoff 	  "detach",
198cefddd66SGleb Smirnoff 	  NULL,
199cefddd66SGleb Smirnoff 	  NULL
200cefddd66SGleb Smirnoff 	},
201e1e1452dSArchie Cobbs 	{ 0 }
202e1e1452dSArchie Cobbs };
203e1e1452dSArchie Cobbs 
204e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = {
205f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
206f8aae777SJulian Elischer 	.name =		NG_ETHER_NODE_TYPE,
207f8aae777SJulian Elischer 	.mod_event =	ng_ether_mod_event,
208f8aae777SJulian Elischer 	.constructor =	ng_ether_constructor,
209f8aae777SJulian Elischer 	.rcvmsg =	ng_ether_rcvmsg,
210f8aae777SJulian Elischer 	.shutdown =	ng_ether_shutdown,
211f8aae777SJulian Elischer 	.newhook =	ng_ether_newhook,
212f8aae777SJulian Elischer 	.rcvdata =	ng_ether_rcvdata,
213f8aae777SJulian Elischer 	.disconnect =	ng_ether_disconnect,
214f8aae777SJulian Elischer 	.cmdlist =	ng_ether_cmdlist,
215e1e1452dSArchie Cobbs };
216e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct);
217e1e1452dSArchie Cobbs 
218e1e1452dSArchie Cobbs /******************************************************************
219499f60b1SAndriy Gapon 		    UTILITY FUNCTIONS
220499f60b1SAndriy Gapon ******************************************************************/
221499f60b1SAndriy Gapon static void
222499f60b1SAndriy Gapon ng_ether_sanitize_ifname(const char *ifname, char *name)
223499f60b1SAndriy Gapon {
224499f60b1SAndriy Gapon 	int i;
225499f60b1SAndriy Gapon 
226499f60b1SAndriy Gapon 	for (i = 0; i < IFNAMSIZ; i++) {
227499f60b1SAndriy Gapon 		if (ifname[i] == '.' || ifname[i] == ':')
228499f60b1SAndriy Gapon 			name[i] = '_';
229499f60b1SAndriy Gapon 		else
230499f60b1SAndriy Gapon 			name[i] = ifname[i];
231499f60b1SAndriy Gapon 		if (name[i] == '\0')
232499f60b1SAndriy Gapon 			break;
233499f60b1SAndriy Gapon 	}
234499f60b1SAndriy Gapon }
235499f60b1SAndriy Gapon 
236499f60b1SAndriy Gapon /******************************************************************
237e1e1452dSArchie Cobbs 		    ETHERNET FUNCTION HOOKS
238e1e1452dSArchie Cobbs ******************************************************************/
239e1e1452dSArchie Cobbs 
240e1e1452dSArchie Cobbs /*
241e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface. We get to
242e1e1452dSArchie Cobbs  * look at it here before any upper layer protocols do.
243e1e1452dSArchie Cobbs  */
244e1e1452dSArchie Cobbs static void
245edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
246e1e1452dSArchie Cobbs {
247e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
24830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2491a292b80SArchie Cobbs 	int error;
250e1e1452dSArchie Cobbs 
251e1e1452dSArchie Cobbs 	/* If "lower" hook not connected, let packet continue */
2521a292b80SArchie Cobbs 	if (priv->lower == NULL)
253e1e1452dSArchie Cobbs 		return;
2541a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
255e1e1452dSArchie Cobbs }
256e1e1452dSArchie Cobbs 
257e1e1452dSArchie Cobbs /*
258e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface, and which
259e1e1452dSArchie Cobbs  * does not match any of our known protocols (an ``orphan'').
260e1e1452dSArchie Cobbs  */
261e1e1452dSArchie Cobbs static void
262edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
263e1e1452dSArchie Cobbs {
264e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
26530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2661a292b80SArchie Cobbs 	int error;
267e1e1452dSArchie Cobbs 
2681a292b80SArchie Cobbs 	/* If "orphan" hook not connected, discard packet */
2691a292b80SArchie Cobbs 	if (priv->orphan == NULL) {
270e1e1452dSArchie Cobbs 		m_freem(m);
271e1e1452dSArchie Cobbs 		return;
272e1e1452dSArchie Cobbs 	}
2731a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->orphan, m);
274e1e1452dSArchie Cobbs }
275e1e1452dSArchie Cobbs 
276e1e1452dSArchie Cobbs /*
277e1e1452dSArchie Cobbs  * Handle a packet that is going out on an interface.
278e1e1452dSArchie Cobbs  * The Ethernet header is already attached to the mbuf.
279e1e1452dSArchie Cobbs  */
280e1e1452dSArchie Cobbs static int
281e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
282e1e1452dSArchie Cobbs {
283e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
28430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
285e1e1452dSArchie Cobbs 	int error = 0;
286e1e1452dSArchie Cobbs 
287e1e1452dSArchie Cobbs 	/* If "upper" hook not connected, let packet continue */
288e1e1452dSArchie Cobbs 	if (priv->upper == NULL)
289e1e1452dSArchie Cobbs 		return (0);
290e1e1452dSArchie Cobbs 
291e1e1452dSArchie Cobbs 	/* Send it out "upper" hook */
292f089869fSMarko Zec 	NG_OUTBOUND_THREAD_REF();
293069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
294f089869fSMarko Zec 	NG_OUTBOUND_THREAD_UNREF();
295e1e1452dSArchie Cobbs 	return (error);
296e1e1452dSArchie Cobbs }
297e1e1452dSArchie Cobbs 
298e1e1452dSArchie Cobbs /*
299e1e1452dSArchie Cobbs  * A new Ethernet interface has been attached.
300e1e1452dSArchie Cobbs  * Create a new node for it, etc.
301e1e1452dSArchie Cobbs  */
302e1e1452dSArchie Cobbs static void
303e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp)
304e1e1452dSArchie Cobbs {
305499f60b1SAndriy Gapon 	char name[IFNAMSIZ];
306e1e1452dSArchie Cobbs 	priv_p priv;
307e1e1452dSArchie Cobbs 	node_p node;
308e1e1452dSArchie Cobbs 
309aef8f344SMarko Zec 	/*
310aef8f344SMarko Zec 	 * Do not create / attach an ether node to this ifnet if
311aef8f344SMarko Zec 	 * a netgraph node with the same name already exists.
312aef8f344SMarko Zec 	 * This should prevent ether nodes to become attached to
313aef8f344SMarko Zec 	 * eiface nodes, which may be problematic due to naming
314aef8f344SMarko Zec 	 * clashes.
315aef8f344SMarko Zec 	 */
316aef8f344SMarko Zec 	if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
317aef8f344SMarko Zec 		NG_NODE_UNREF(node);
318aef8f344SMarko Zec 		return;
319aef8f344SMarko Zec 	}
320aef8f344SMarko Zec 
321e1e1452dSArchie Cobbs 	/* Create node */
3226e551fb6SDavid E. O'Brien 	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
323e1e1452dSArchie Cobbs 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
324e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
3259bf40edeSBrooks Davis 		    __func__, "create node", ifp->if_xname);
326e1e1452dSArchie Cobbs 		return;
327e1e1452dSArchie Cobbs 	}
328e1e1452dSArchie Cobbs 
329e1e1452dSArchie Cobbs 	/* Allocate private data */
3301ede983cSDag-Erling Smørgrav 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
331e1e1452dSArchie Cobbs 	if (priv == NULL) {
332e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
3339bf40edeSBrooks Davis 		    __func__, "allocate memory", ifp->if_xname);
33430400f03SJulian Elischer 		NG_NODE_UNREF(node);
335e1e1452dSArchie Cobbs 		return;
336e1e1452dSArchie Cobbs 	}
33730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
338e1e1452dSArchie Cobbs 	priv->ifp = ifp;
3395240dcdbSRuslan Ermilov 	IFP2NG(ifp) = node;
340a3e232d6SArchie Cobbs 	priv->hwassist = ifp->if_hwassist;
341e1e1452dSArchie Cobbs 
342e1e1452dSArchie Cobbs 	/* Try to give the node the same name as the interface */
343499f60b1SAndriy Gapon 	ng_ether_sanitize_ifname(ifp->if_xname, name);
344499f60b1SAndriy Gapon 	if (ng_name_node(node, name) != 0)
345499f60b1SAndriy Gapon 		log(LOG_WARNING, "%s: can't name node %s\n", __func__, name);
346e1e1452dSArchie Cobbs }
347e1e1452dSArchie Cobbs 
348e1e1452dSArchie Cobbs /*
349e1e1452dSArchie Cobbs  * An Ethernet interface is being detached.
3501acb27c6SJulian Elischer  * REALLY Destroy its node.
351e1e1452dSArchie Cobbs  */
352e1e1452dSArchie Cobbs static void
353e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp)
354e1e1452dSArchie Cobbs {
355e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
3561acb27c6SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
357e1e1452dSArchie Cobbs 
3582cdf8c49SMarko Zec 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
3591acb27c6SJulian Elischer 	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
3601acb27c6SJulian Elischer 	/*
3611acb27c6SJulian Elischer 	 * We can't assume the ifnet is still around when we run shutdown
3621acb27c6SJulian Elischer 	 * So zap it now. XXX We HOPE that anything running at this time
3631acb27c6SJulian Elischer 	 * handles it (as it should in the non netgraph case).
3641acb27c6SJulian Elischer 	 */
3655240dcdbSRuslan Ermilov 	IFP2NG(ifp) = NULL;
3661acb27c6SJulian Elischer 	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
3671acb27c6SJulian Elischer 	ng_rmnode_self(node);		/* remove all netgraph parts */
368e1e1452dSArchie Cobbs }
369e1e1452dSArchie Cobbs 
3701c7899c7SGleb Smirnoff /*
3711c7899c7SGleb Smirnoff  * Notify graph about link event.
3721c7899c7SGleb Smirnoff  * if_link_state_change() has already checked that the state has changed.
3731c7899c7SGleb Smirnoff  */
3741c7899c7SGleb Smirnoff static void
3751c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state)
3761c7899c7SGleb Smirnoff {
3771c7899c7SGleb Smirnoff 	const node_p node = IFP2NG(ifp);
3781c7899c7SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
3791c7899c7SGleb Smirnoff 	struct ng_mesg *msg;
3801c7899c7SGleb Smirnoff 	int cmd, dummy_error = 0;
3811c7899c7SGleb Smirnoff 
3821c7899c7SGleb Smirnoff 	if (state == LINK_STATE_UP)
3831c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_UP;
3841c7899c7SGleb Smirnoff 	else if (state == LINK_STATE_DOWN)
3851c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_DOWN;
3861c7899c7SGleb Smirnoff 	else
3871c7899c7SGleb Smirnoff 		return;
3881c7899c7SGleb Smirnoff 
389bb027e06SMax Khon 	if (priv->lower != NULL) {
3901c7899c7SGleb Smirnoff 		NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
3911c7899c7SGleb Smirnoff 		if (msg != NULL)
3921c7899c7SGleb Smirnoff 			NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
3931c7899c7SGleb Smirnoff 	}
394bb027e06SMax Khon 	if (priv->orphan != NULL) {
395bb027e06SMax Khon 		NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
396bb027e06SMax Khon 		if (msg != NULL)
397bb027e06SMax Khon 			NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->orphan, 0);
398bb027e06SMax Khon 	}
399bb027e06SMax Khon }
4001c7899c7SGleb Smirnoff 
401499f60b1SAndriy Gapon /*
402499f60b1SAndriy Gapon  * Interface arrival notification handler.
403499f60b1SAndriy Gapon  * The notification is produced in two cases:
404499f60b1SAndriy Gapon  *  o a new interface arrives
405499f60b1SAndriy Gapon  *  o an existing interface got renamed
406499f60b1SAndriy Gapon  * Currently the first case is handled by ng_ether_attach via special
407499f60b1SAndriy Gapon  * hook ng_ether_attach_p.
408499f60b1SAndriy Gapon  */
409499f60b1SAndriy Gapon static void
410499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
411499f60b1SAndriy Gapon {
412499f60b1SAndriy Gapon 	char name[IFNAMSIZ];
413*30bc1032SAndriy Gapon 	node_p node;
414*30bc1032SAndriy Gapon 
415*30bc1032SAndriy Gapon 	/* Only ethernet interfaces are of interest. */
416*30bc1032SAndriy Gapon 	if (ifp->if_type != IFT_ETHER
417*30bc1032SAndriy Gapon 	    && ifp->if_type != IFT_L2VLAN)
418*30bc1032SAndriy Gapon 		return;
419499f60b1SAndriy Gapon 
420499f60b1SAndriy Gapon 	/*
421499f60b1SAndriy Gapon 	 * Just return if it's a new interface without an ng_ether companion.
422499f60b1SAndriy Gapon 	 */
423*30bc1032SAndriy Gapon 	node = IFP2NG(ifp);
424499f60b1SAndriy Gapon 	if (node == NULL)
425499f60b1SAndriy Gapon 		return;
426499f60b1SAndriy Gapon 
427499f60b1SAndriy Gapon 	/* Try to give the node the same name as the new interface name */
428499f60b1SAndriy Gapon 	ng_ether_sanitize_ifname(ifp->if_xname, name);
429499f60b1SAndriy Gapon 	if (ng_name_node(node, name) != 0)
430499f60b1SAndriy Gapon 		log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name);
431499f60b1SAndriy Gapon }
432499f60b1SAndriy Gapon 
433e1e1452dSArchie Cobbs /******************************************************************
434e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
435e1e1452dSArchie Cobbs ******************************************************************/
436e1e1452dSArchie Cobbs 
437e1e1452dSArchie Cobbs /*
438e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
439e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
440e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
441e1e1452dSArchie Cobbs  */
442e1e1452dSArchie Cobbs static int
443069154d5SJulian Elischer ng_ether_constructor(node_p node)
444e1e1452dSArchie Cobbs {
445e1e1452dSArchie Cobbs 	return (EINVAL);
446e1e1452dSArchie Cobbs }
447e1e1452dSArchie Cobbs 
448e1e1452dSArchie Cobbs /*
449e1e1452dSArchie Cobbs  * Check for attaching a new hook.
450e1e1452dSArchie Cobbs  */
451e1e1452dSArchie Cobbs static	int
452e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
453e1e1452dSArchie Cobbs {
45430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
455e1e1452dSArchie Cobbs 	hook_p *hookptr;
456e1e1452dSArchie Cobbs 
457e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
458e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
459e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
460e1e1452dSArchie Cobbs 
461e1e1452dSArchie Cobbs 	/* Which hook? */
462f664dcdeSJulian Elischer 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
463e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
464f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
465f089869fSMarko Zec 		NG_HOOK_SET_TO_INBOUND(hook);
466f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
467e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
468f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
469f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
4701a292b80SArchie Cobbs 		hookptr = &priv->orphan;
471f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
472f664dcdeSJulian Elischer 	} else
473e1e1452dSArchie Cobbs 		return (EINVAL);
474e1e1452dSArchie Cobbs 
475e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
476e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
477e1e1452dSArchie Cobbs 		return (EISCONN);
478e1e1452dSArchie Cobbs 
479a3e232d6SArchie Cobbs 	/* Disable hardware checksums while 'upper' hook is connected */
480a3e232d6SArchie Cobbs 	if (hookptr == &priv->upper)
481a3e232d6SArchie Cobbs 		priv->ifp->if_hwassist = 0;
482c1b8a9edSAlexander Motin 	NG_HOOK_HI_STACK(hook);
483e1e1452dSArchie Cobbs 	/* OK */
484e1e1452dSArchie Cobbs 	*hookptr = hook;
485e1e1452dSArchie Cobbs 	return (0);
486e1e1452dSArchie Cobbs }
487e1e1452dSArchie Cobbs 
488e1e1452dSArchie Cobbs /*
489e1e1452dSArchie Cobbs  * Receive an incoming control message.
490e1e1452dSArchie Cobbs  */
491e1e1452dSArchie Cobbs static int
492069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
493e1e1452dSArchie Cobbs {
49430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
495e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
496e1e1452dSArchie Cobbs 	int error = 0;
497069154d5SJulian Elischer 	struct ng_mesg *msg;
498e1e1452dSArchie Cobbs 
499069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
500e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
501e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
502e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
503e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
504bbb75d78SRuslan Ermilov 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
505e1e1452dSArchie Cobbs 			if (resp == NULL) {
506e1e1452dSArchie Cobbs 				error = ENOMEM;
507e1e1452dSArchie Cobbs 				break;
508e1e1452dSArchie Cobbs 			}
509bbb75d78SRuslan Ermilov 			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
510e1e1452dSArchie Cobbs 			break;
511e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
512e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
513e1e1452dSArchie Cobbs 			if (resp == NULL) {
514e1e1452dSArchie Cobbs 				error = ENOMEM;
515e1e1452dSArchie Cobbs 				break;
516e1e1452dSArchie Cobbs 			}
517e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
518e1e1452dSArchie Cobbs 			break;
5194b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
5204b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
5214b39c3c7SArchie Cobbs 			if (resp == NULL) {
5224b39c3c7SArchie Cobbs 				error = ENOMEM;
5234b39c3c7SArchie Cobbs 				break;
5244b39c3c7SArchie Cobbs 			}
5254a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(priv->ifp),
5264b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
5274b39c3c7SArchie Cobbs 			break;
52856045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
52956045c66SArchie Cobbs 		    {
53056045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
53156045c66SArchie Cobbs 				error = EINVAL;
53256045c66SArchie Cobbs 				break;
53356045c66SArchie Cobbs 			}
53456045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
53556045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
536ea4ca115SAndrew Thompson 			EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
53756045c66SArchie Cobbs 			break;
53856045c66SArchie Cobbs 		    }
53956045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
54056045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
54156045c66SArchie Cobbs 			if (resp == NULL) {
54256045c66SArchie Cobbs 				error = ENOMEM;
54356045c66SArchie Cobbs 				break;
54456045c66SArchie Cobbs 			}
54556045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
54656045c66SArchie Cobbs 			break;
5474b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
5484b39c3c7SArchie Cobbs 		    {
5494b39c3c7SArchie Cobbs 			u_char want;
5504b39c3c7SArchie Cobbs 
5514b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5524b39c3c7SArchie Cobbs 				error = EINVAL;
5534b39c3c7SArchie Cobbs 				break;
5544b39c3c7SArchie Cobbs 			}
5554b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
5564b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
5574b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
5584b39c3c7SArchie Cobbs 					break;
5594b39c3c7SArchie Cobbs 				priv->promisc = want;
5604b39c3c7SArchie Cobbs 			}
5614b39c3c7SArchie Cobbs 			break;
5624b39c3c7SArchie Cobbs 		    }
56356045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
56456045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
56556045c66SArchie Cobbs 			if (resp == NULL) {
56656045c66SArchie Cobbs 				error = ENOMEM;
56756045c66SArchie Cobbs 				break;
56856045c66SArchie Cobbs 			}
56956045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
57056045c66SArchie Cobbs 			break;
5714b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
5724b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5734b39c3c7SArchie Cobbs 				error = EINVAL;
5744b39c3c7SArchie Cobbs 				break;
5754b39c3c7SArchie Cobbs 			}
5764b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5774b39c3c7SArchie Cobbs 			break;
578810d5e89SGleb Smirnoff 		case NGM_ETHER_ADD_MULTI:
579810d5e89SGleb Smirnoff 		    {
580810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
581ec002feeSBruce M Simpson 			struct ifmultiaddr *ifma;
582810d5e89SGleb Smirnoff 
583810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
584810d5e89SGleb Smirnoff 				error = EINVAL;
585810d5e89SGleb Smirnoff 				break;
586810d5e89SGleb Smirnoff 			}
587ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
588810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
589810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
590ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
591810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
592810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
593ec002feeSBruce M Simpson 			/*
594ec002feeSBruce M Simpson 			 * Netgraph is only permitted to join groups once
595ec002feeSBruce M Simpson 			 * via the if_addmulti() KPI, because it cannot hold
596ec002feeSBruce M Simpson 			 * struct ifmultiaddr * between calls. It may also
597ec002feeSBruce M Simpson 			 * lose a race while we check if the membership
598ec002feeSBruce M Simpson 			 * already exists.
599ec002feeSBruce M Simpson 			 */
600eb956cd0SRobert Watson 			if_maddr_rlock(priv->ifp);
601ec002feeSBruce M Simpson 			ifma = if_findmulti(priv->ifp,
602ec002feeSBruce M Simpson 			    (struct sockaddr *)&sa_dl);
603eb956cd0SRobert Watson 			if_maddr_runlock(priv->ifp);
604ec002feeSBruce M Simpson 			if (ifma != NULL) {
605ec002feeSBruce M Simpson 				error = EADDRINUSE;
606ec002feeSBruce M Simpson 			} else {
607810d5e89SGleb Smirnoff 				error = if_addmulti(priv->ifp,
608ec002feeSBruce M Simpson 				    (struct sockaddr *)&sa_dl, &ifma);
609ec002feeSBruce M Simpson 			}
610810d5e89SGleb Smirnoff 			break;
611810d5e89SGleb Smirnoff 		    }
612810d5e89SGleb Smirnoff 		case NGM_ETHER_DEL_MULTI:
613810d5e89SGleb Smirnoff 		    {
614810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
615810d5e89SGleb Smirnoff 
616810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
617810d5e89SGleb Smirnoff 				error = EINVAL;
618810d5e89SGleb Smirnoff 				break;
619810d5e89SGleb Smirnoff 			}
620ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
621810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
622810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
623ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
624810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
625810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
626810d5e89SGleb Smirnoff 			error = if_delmulti(priv->ifp,
627810d5e89SGleb Smirnoff 			    (struct sockaddr *)&sa_dl);
628810d5e89SGleb Smirnoff 			break;
629810d5e89SGleb Smirnoff 		    }
630cefddd66SGleb Smirnoff 		case NGM_ETHER_DETACH:
631cefddd66SGleb Smirnoff 			ng_ether_detach(priv->ifp);
632cefddd66SGleb Smirnoff 			break;
633e1e1452dSArchie Cobbs 		default:
634e1e1452dSArchie Cobbs 			error = EINVAL;
635e1e1452dSArchie Cobbs 			break;
636e1e1452dSArchie Cobbs 		}
637e1e1452dSArchie Cobbs 		break;
638e1e1452dSArchie Cobbs 	default:
639e1e1452dSArchie Cobbs 		error = EINVAL;
640e1e1452dSArchie Cobbs 		break;
641e1e1452dSArchie Cobbs 	}
642069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
643069154d5SJulian Elischer 	NG_FREE_MSG(msg);
644e1e1452dSArchie Cobbs 	return (error);
645e1e1452dSArchie Cobbs }
646e1e1452dSArchie Cobbs 
647e1e1452dSArchie Cobbs /*
648e1e1452dSArchie Cobbs  * Receive data on a hook.
649f664dcdeSJulian Elischer  * Since we use per-hook recveive methods this should never be called.
650e1e1452dSArchie Cobbs  */
651e1e1452dSArchie Cobbs static int
652069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
653e1e1452dSArchie Cobbs {
654069154d5SJulian Elischer 	NG_FREE_ITEM(item);
6553ca24c28SJulian Elischer 
6566e551fb6SDavid E. O'Brien 	panic("%s: weird hook", __func__);
657e1e1452dSArchie Cobbs }
658e1e1452dSArchie Cobbs 
659e1e1452dSArchie Cobbs /*
6601a292b80SArchie Cobbs  * Handle an mbuf received on the "lower" or "orphan" hook.
661e1e1452dSArchie Cobbs  */
662e1e1452dSArchie Cobbs static int
663f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item)
664e1e1452dSArchie Cobbs {
665f664dcdeSJulian Elischer 	struct mbuf *m;
666f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
66730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
668a1479aa2SArchie Cobbs  	struct ifnet *const ifp = priv->ifp;
669a1479aa2SArchie Cobbs 
670f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
671f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
672f664dcdeSJulian Elischer 
673a1479aa2SArchie Cobbs 	/* Check whether interface is ready for packets */
674f664dcdeSJulian Elischer 
67513f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
67613f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
677a1479aa2SArchie Cobbs 		NG_FREE_M(m);
678a1479aa2SArchie Cobbs 		return (ENETDOWN);
679a1479aa2SArchie Cobbs 	}
680e1e1452dSArchie Cobbs 
681e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
682e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
683069154d5SJulian Elischer 		NG_FREE_M(m);
684e1e1452dSArchie Cobbs 		return (EINVAL);
685e1e1452dSArchie Cobbs 	}
686e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
6877b9f235fSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
688e1e1452dSArchie Cobbs 		return (ENOBUFS);
689e1e1452dSArchie Cobbs 
6904b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6914b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6927b9f235fSArchie Cobbs 
6937b9f235fSArchie Cobbs 		/* Make the mbuf writable if it's not already */
6947b9f235fSArchie Cobbs 		if (!M_WRITABLE(m)
6957b9f235fSArchie Cobbs 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
6967b9f235fSArchie Cobbs 			return (ENOBUFS);
6977b9f235fSArchie Cobbs 
6987b9f235fSArchie Cobbs 		/* Overwrite source MAC address */
6994a0d6638SRuslan Ermilov 		bcopy(IF_LLADDR(ifp),
7004b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
7014b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
7024b39c3c7SArchie Cobbs 	}
703561e4fb9SJulian Elischer 
704e1e1452dSArchie Cobbs 	/* Send it on its way */
705a1479aa2SArchie Cobbs 	return ether_output_frame(ifp, m);
706e1e1452dSArchie Cobbs }
707e1e1452dSArchie Cobbs 
708e1e1452dSArchie Cobbs /*
709e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
710e1e1452dSArchie Cobbs  */
711e1e1452dSArchie Cobbs static int
712f664dcdeSJulian Elischer ng_ether_rcv_upper(hook_p hook, item_p item)
713e1e1452dSArchie Cobbs {
714f664dcdeSJulian Elischer 	struct mbuf *m;
715f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
71630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7176512768bSGleb Smirnoff 	struct ifnet *ifp = priv->ifp;
718e1e1452dSArchie Cobbs 
719f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
720f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
721f664dcdeSJulian Elischer 
722c60c00bcSRuslan Ermilov 	/* Check length and pull off header */
723c60c00bcSRuslan Ermilov 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
724c60c00bcSRuslan Ermilov 		NG_FREE_M(m);
725c60c00bcSRuslan Ermilov 		return (EINVAL);
726c60c00bcSRuslan Ermilov 	}
727c60c00bcSRuslan Ermilov 	if (m->m_len < sizeof(struct ether_header) &&
728c60c00bcSRuslan Ermilov 	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
729c60c00bcSRuslan Ermilov 		return (ENOBUFS);
730c60c00bcSRuslan Ermilov 
7316512768bSGleb Smirnoff 	m->m_pkthdr.rcvif = ifp;
732e1e1452dSArchie Cobbs 
733fd6238a6SAndrew Thompson 	/* Pass the packet to the bridge, it may come back to us */
7346512768bSGleb Smirnoff 	if (ifp->if_bridge) {
735fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
7366512768bSGleb Smirnoff 		if (m == NULL)
7376512768bSGleb Smirnoff 			return (0);
7386512768bSGleb Smirnoff 	}
739a176c2aeSGleb Smirnoff 
740e1e1452dSArchie Cobbs 	/* Route packet back in */
741fd6238a6SAndrew Thompson 	ether_demux(ifp, m);
742e1e1452dSArchie Cobbs 	return (0);
743e1e1452dSArchie Cobbs }
744e1e1452dSArchie Cobbs 
745e1e1452dSArchie Cobbs /*
7461acb27c6SJulian Elischer  * Shutdown node. This resets the node but does not remove it
7471acb27c6SJulian Elischer  * unless the REALLY_DIE flag is set.
748e1e1452dSArchie Cobbs  */
749e1e1452dSArchie Cobbs static int
750069154d5SJulian Elischer ng_ether_shutdown(node_p node)
751e1e1452dSArchie Cobbs {
75230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7534b39c3c7SArchie Cobbs 
754be4252b3SJulian Elischer 	if (node->nd_flags & NGF_REALLY_DIE) {
7551acb27c6SJulian Elischer 		/*
7561acb27c6SJulian Elischer 		 * WE came here because the ethernet card is being unloaded,
7571acb27c6SJulian Elischer 		 * so stop being persistant.
7581acb27c6SJulian Elischer 		 * Actually undo all the things we did on creation.
7591acb27c6SJulian Elischer 		 * Assume the ifp has already been freed.
7601acb27c6SJulian Elischer 		 */
7611acb27c6SJulian Elischer 		NG_NODE_SET_PRIVATE(node, NULL);
7621ede983cSDag-Erling Smørgrav 		free(priv, M_NETGRAPH);
7631acb27c6SJulian Elischer 		NG_NODE_UNREF(node);	/* free node itself */
7641acb27c6SJulian Elischer 		return (0);
765069154d5SJulian Elischer 	}
766018df1c3SBrian Feldman 	if (priv->promisc) {		/* disable promiscuous mode */
767018df1c3SBrian Feldman 		(void)ifpromisc(priv->ifp, 0);
768018df1c3SBrian Feldman 		priv->promisc = 0;
769018df1c3SBrian Feldman 	}
770be4252b3SJulian Elischer 	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
771be4252b3SJulian Elischer 
772e1e1452dSArchie Cobbs 	return (0);
773e1e1452dSArchie Cobbs }
774e1e1452dSArchie Cobbs 
775e1e1452dSArchie Cobbs /*
776e1e1452dSArchie Cobbs  * Hook disconnection.
777e1e1452dSArchie Cobbs  */
778e1e1452dSArchie Cobbs static int
779e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
780e1e1452dSArchie Cobbs {
78130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
782e1e1452dSArchie Cobbs 
783a3e232d6SArchie Cobbs 	if (hook == priv->upper) {
784e1e1452dSArchie Cobbs 		priv->upper = NULL;
785b712e9ecSBrian Feldman 		if (priv->ifp != NULL)		/* restore h/w csum */
786b712e9ecSBrian Feldman 			priv->ifp->if_hwassist = priv->hwassist;
7871a292b80SArchie Cobbs 	} else if (hook == priv->lower)
788e1e1452dSArchie Cobbs 		priv->lower = NULL;
7891a292b80SArchie Cobbs 	else if (hook == priv->orphan)
7901a292b80SArchie Cobbs 		priv->orphan = NULL;
7911a292b80SArchie Cobbs 	else
7926e551fb6SDavid E. O'Brien 		panic("%s: weird hook", __func__);
79330400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
79430400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
79530400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
796e1e1452dSArchie Cobbs 	return (0);
797e1e1452dSArchie Cobbs }
798e1e1452dSArchie Cobbs 
799e1e1452dSArchie Cobbs /******************************************************************
800e1e1452dSArchie Cobbs 		    	INITIALIZATION
801e1e1452dSArchie Cobbs ******************************************************************/
802e1e1452dSArchie Cobbs 
803e1e1452dSArchie Cobbs /*
804e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
805e1e1452dSArchie Cobbs  */
806e1e1452dSArchie Cobbs static int
807e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
808e1e1452dSArchie Cobbs {
809e1e1452dSArchie Cobbs 	int error = 0;
810e1e1452dSArchie Cobbs 
811e1e1452dSArchie Cobbs 	switch (event) {
812e1e1452dSArchie Cobbs 	case MOD_LOAD:
813e1e1452dSArchie Cobbs 
814e1e1452dSArchie Cobbs 		/* Register function hooks */
815e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
816e1e1452dSArchie Cobbs 			error = EEXIST;
817e1e1452dSArchie Cobbs 			break;
818e1e1452dSArchie Cobbs 		}
819e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
820e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
821e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
822e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
823e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
8241c7899c7SGleb Smirnoff 		ng_ether_link_state_p = ng_ether_link_state;
825e1e1452dSArchie Cobbs 
826499f60b1SAndriy Gapon 		ng_ether_ifnet_arrival_cookie =
827499f60b1SAndriy Gapon 		    EVENTHANDLER_REGISTER(ifnet_arrival_event,
828499f60b1SAndriy Gapon 		    ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
829e1e1452dSArchie Cobbs 		break;
830e1e1452dSArchie Cobbs 
831e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
832e1e1452dSArchie Cobbs 
833e1e1452dSArchie Cobbs 		/*
834e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
835e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
836e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
837e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
838e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
839e1e1452dSArchie Cobbs 		 */
840e1e1452dSArchie Cobbs 
841499f60b1SAndriy Gapon 		EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
842499f60b1SAndriy Gapon 		    ng_ether_ifnet_arrival_cookie);
843499f60b1SAndriy Gapon 
844e1e1452dSArchie Cobbs 		/* Unregister function hooks */
845e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
846e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
847e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
848e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
849e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
8501c7899c7SGleb Smirnoff 		ng_ether_link_state_p = NULL;
851e1e1452dSArchie Cobbs 		break;
852e1e1452dSArchie Cobbs 
853e1e1452dSArchie Cobbs 	default:
854e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
855e1e1452dSArchie Cobbs 		break;
856e1e1452dSArchie Cobbs 	}
857e1e1452dSArchie Cobbs 	return (error);
858e1e1452dSArchie Cobbs }
859e1e1452dSArchie Cobbs 
860d0728d71SRobert Watson static void
861d0728d71SRobert Watson vnet_ng_ether_init(const void *unused)
862aef8f344SMarko Zec {
863aef8f344SMarko Zec 	struct ifnet *ifp;
864aef8f344SMarko Zec 
865d0728d71SRobert Watson 	/* If module load was rejected, don't attach to vnets. */
866d0728d71SRobert Watson 	if (ng_ether_attach_p != ng_ether_attach)
867d0728d71SRobert Watson 		return;
868d0728d71SRobert Watson 
869aef8f344SMarko Zec 	/* Create nodes for any already-existing Ethernet interfaces. */
870aef8f344SMarko Zec 	IFNET_RLOCK();
871aef8f344SMarko Zec 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
872aef8f344SMarko Zec 		if (ifp->if_type == IFT_ETHER
873aef8f344SMarko Zec 		    || ifp->if_type == IFT_L2VLAN)
874aef8f344SMarko Zec 			ng_ether_attach(ifp);
875aef8f344SMarko Zec 	}
876aef8f344SMarko Zec 	IFNET_RUNLOCK();
877aef8f344SMarko Zec }
878d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
879d0728d71SRobert Watson     vnet_ng_ether_init, NULL);
880