xref: /freebsd/sys/netgraph/ng_ether.c (revision 499f60b10b6c85bc7d196723c8d310ed5994d076)
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 
120*499f60b1SAndriy Gapon static eventhandler_tag	ng_ether_ifnet_arrival_cookie;
121*499f60b1SAndriy 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 /******************************************************************
219*499f60b1SAndriy Gapon 		    UTILITY FUNCTIONS
220*499f60b1SAndriy Gapon ******************************************************************/
221*499f60b1SAndriy Gapon static void
222*499f60b1SAndriy Gapon ng_ether_sanitize_ifname(const char *ifname, char *name)
223*499f60b1SAndriy Gapon {
224*499f60b1SAndriy Gapon 	int i;
225*499f60b1SAndriy Gapon 
226*499f60b1SAndriy Gapon 	for (i = 0; i < IFNAMSIZ; i++) {
227*499f60b1SAndriy Gapon 		if (ifname[i] == '.' || ifname[i] == ':')
228*499f60b1SAndriy Gapon 			name[i] = '_';
229*499f60b1SAndriy Gapon 		else
230*499f60b1SAndriy Gapon 			name[i] = ifname[i];
231*499f60b1SAndriy Gapon 		if (name[i] == '\0')
232*499f60b1SAndriy Gapon 			break;
233*499f60b1SAndriy Gapon 	}
234*499f60b1SAndriy Gapon }
235*499f60b1SAndriy Gapon 
236*499f60b1SAndriy 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 {
305*499f60b1SAndriy 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 */
343*499f60b1SAndriy Gapon 	ng_ether_sanitize_ifname(ifp->if_xname, name);
344*499f60b1SAndriy Gapon 	if (ng_name_node(node, name) != 0)
345*499f60b1SAndriy 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 
401*499f60b1SAndriy Gapon /*
402*499f60b1SAndriy Gapon  * Interface arrival notification handler.
403*499f60b1SAndriy Gapon  * The notification is produced in two cases:
404*499f60b1SAndriy Gapon  *  o a new interface arrives
405*499f60b1SAndriy Gapon  *  o an existing interface got renamed
406*499f60b1SAndriy Gapon  * Currently the first case is handled by ng_ether_attach via special
407*499f60b1SAndriy Gapon  * hook ng_ether_attach_p.
408*499f60b1SAndriy Gapon  */
409*499f60b1SAndriy Gapon static void
410*499f60b1SAndriy Gapon ng_ether_ifnet_arrival_event(void *arg __unused, struct ifnet *ifp)
411*499f60b1SAndriy Gapon {
412*499f60b1SAndriy Gapon 	char name[IFNAMSIZ];
413*499f60b1SAndriy Gapon 	node_p node = IFP2NG(ifp);
414*499f60b1SAndriy Gapon 
415*499f60b1SAndriy Gapon 	/*
416*499f60b1SAndriy Gapon 	 * Just return if it's a new interface without an ng_ether companion.
417*499f60b1SAndriy Gapon 	 */
418*499f60b1SAndriy Gapon 	if (node == NULL)
419*499f60b1SAndriy Gapon 		return;
420*499f60b1SAndriy Gapon 
421*499f60b1SAndriy Gapon 	/* Try to give the node the same name as the new interface name */
422*499f60b1SAndriy Gapon 	ng_ether_sanitize_ifname(ifp->if_xname, name);
423*499f60b1SAndriy Gapon 	if (ng_name_node(node, name) != 0)
424*499f60b1SAndriy Gapon 		log(LOG_WARNING, "%s: can't re-name node %s\n", __func__, name);
425*499f60b1SAndriy Gapon }
426*499f60b1SAndriy Gapon 
427e1e1452dSArchie Cobbs /******************************************************************
428e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
429e1e1452dSArchie Cobbs ******************************************************************/
430e1e1452dSArchie Cobbs 
431e1e1452dSArchie Cobbs /*
432e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
433e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
434e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
435e1e1452dSArchie Cobbs  */
436e1e1452dSArchie Cobbs static int
437069154d5SJulian Elischer ng_ether_constructor(node_p node)
438e1e1452dSArchie Cobbs {
439e1e1452dSArchie Cobbs 	return (EINVAL);
440e1e1452dSArchie Cobbs }
441e1e1452dSArchie Cobbs 
442e1e1452dSArchie Cobbs /*
443e1e1452dSArchie Cobbs  * Check for attaching a new hook.
444e1e1452dSArchie Cobbs  */
445e1e1452dSArchie Cobbs static	int
446e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
447e1e1452dSArchie Cobbs {
44830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
449e1e1452dSArchie Cobbs 	hook_p *hookptr;
450e1e1452dSArchie Cobbs 
451e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
452e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
453e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
454e1e1452dSArchie Cobbs 
455e1e1452dSArchie Cobbs 	/* Which hook? */
456f664dcdeSJulian Elischer 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
457e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
458f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
459f089869fSMarko Zec 		NG_HOOK_SET_TO_INBOUND(hook);
460f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
461e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
462f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
463f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
4641a292b80SArchie Cobbs 		hookptr = &priv->orphan;
465f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
466f664dcdeSJulian Elischer 	} else
467e1e1452dSArchie Cobbs 		return (EINVAL);
468e1e1452dSArchie Cobbs 
469e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
470e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
471e1e1452dSArchie Cobbs 		return (EISCONN);
472e1e1452dSArchie Cobbs 
473a3e232d6SArchie Cobbs 	/* Disable hardware checksums while 'upper' hook is connected */
474a3e232d6SArchie Cobbs 	if (hookptr == &priv->upper)
475a3e232d6SArchie Cobbs 		priv->ifp->if_hwassist = 0;
476c1b8a9edSAlexander Motin 	NG_HOOK_HI_STACK(hook);
477e1e1452dSArchie Cobbs 	/* OK */
478e1e1452dSArchie Cobbs 	*hookptr = hook;
479e1e1452dSArchie Cobbs 	return (0);
480e1e1452dSArchie Cobbs }
481e1e1452dSArchie Cobbs 
482e1e1452dSArchie Cobbs /*
483e1e1452dSArchie Cobbs  * Receive an incoming control message.
484e1e1452dSArchie Cobbs  */
485e1e1452dSArchie Cobbs static int
486069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
487e1e1452dSArchie Cobbs {
48830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
489e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
490e1e1452dSArchie Cobbs 	int error = 0;
491069154d5SJulian Elischer 	struct ng_mesg *msg;
492e1e1452dSArchie Cobbs 
493069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
494e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
495e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
496e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
497e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
498bbb75d78SRuslan Ermilov 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
499e1e1452dSArchie Cobbs 			if (resp == NULL) {
500e1e1452dSArchie Cobbs 				error = ENOMEM;
501e1e1452dSArchie Cobbs 				break;
502e1e1452dSArchie Cobbs 			}
503bbb75d78SRuslan Ermilov 			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
504e1e1452dSArchie Cobbs 			break;
505e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
506e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
507e1e1452dSArchie Cobbs 			if (resp == NULL) {
508e1e1452dSArchie Cobbs 				error = ENOMEM;
509e1e1452dSArchie Cobbs 				break;
510e1e1452dSArchie Cobbs 			}
511e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
512e1e1452dSArchie Cobbs 			break;
5134b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
5144b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
5154b39c3c7SArchie Cobbs 			if (resp == NULL) {
5164b39c3c7SArchie Cobbs 				error = ENOMEM;
5174b39c3c7SArchie Cobbs 				break;
5184b39c3c7SArchie Cobbs 			}
5194a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(priv->ifp),
5204b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
5214b39c3c7SArchie Cobbs 			break;
52256045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
52356045c66SArchie Cobbs 		    {
52456045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
52556045c66SArchie Cobbs 				error = EINVAL;
52656045c66SArchie Cobbs 				break;
52756045c66SArchie Cobbs 			}
52856045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
52956045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
530ea4ca115SAndrew Thompson 			EVENTHANDLER_INVOKE(iflladdr_event, priv->ifp);
53156045c66SArchie Cobbs 			break;
53256045c66SArchie Cobbs 		    }
53356045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
53456045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
53556045c66SArchie Cobbs 			if (resp == NULL) {
53656045c66SArchie Cobbs 				error = ENOMEM;
53756045c66SArchie Cobbs 				break;
53856045c66SArchie Cobbs 			}
53956045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
54056045c66SArchie Cobbs 			break;
5414b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
5424b39c3c7SArchie Cobbs 		    {
5434b39c3c7SArchie Cobbs 			u_char want;
5444b39c3c7SArchie Cobbs 
5454b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5464b39c3c7SArchie Cobbs 				error = EINVAL;
5474b39c3c7SArchie Cobbs 				break;
5484b39c3c7SArchie Cobbs 			}
5494b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
5504b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
5514b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
5524b39c3c7SArchie Cobbs 					break;
5534b39c3c7SArchie Cobbs 				priv->promisc = want;
5544b39c3c7SArchie Cobbs 			}
5554b39c3c7SArchie Cobbs 			break;
5564b39c3c7SArchie Cobbs 		    }
55756045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
55856045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
55956045c66SArchie Cobbs 			if (resp == NULL) {
56056045c66SArchie Cobbs 				error = ENOMEM;
56156045c66SArchie Cobbs 				break;
56256045c66SArchie Cobbs 			}
56356045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
56456045c66SArchie Cobbs 			break;
5654b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
5664b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5674b39c3c7SArchie Cobbs 				error = EINVAL;
5684b39c3c7SArchie Cobbs 				break;
5694b39c3c7SArchie Cobbs 			}
5704b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5714b39c3c7SArchie Cobbs 			break;
572810d5e89SGleb Smirnoff 		case NGM_ETHER_ADD_MULTI:
573810d5e89SGleb Smirnoff 		    {
574810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
575ec002feeSBruce M Simpson 			struct ifmultiaddr *ifma;
576810d5e89SGleb Smirnoff 
577810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
578810d5e89SGleb Smirnoff 				error = EINVAL;
579810d5e89SGleb Smirnoff 				break;
580810d5e89SGleb Smirnoff 			}
581ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
582810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
583810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
584ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
585810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
586810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
587ec002feeSBruce M Simpson 			/*
588ec002feeSBruce M Simpson 			 * Netgraph is only permitted to join groups once
589ec002feeSBruce M Simpson 			 * via the if_addmulti() KPI, because it cannot hold
590ec002feeSBruce M Simpson 			 * struct ifmultiaddr * between calls. It may also
591ec002feeSBruce M Simpson 			 * lose a race while we check if the membership
592ec002feeSBruce M Simpson 			 * already exists.
593ec002feeSBruce M Simpson 			 */
594eb956cd0SRobert Watson 			if_maddr_rlock(priv->ifp);
595ec002feeSBruce M Simpson 			ifma = if_findmulti(priv->ifp,
596ec002feeSBruce M Simpson 			    (struct sockaddr *)&sa_dl);
597eb956cd0SRobert Watson 			if_maddr_runlock(priv->ifp);
598ec002feeSBruce M Simpson 			if (ifma != NULL) {
599ec002feeSBruce M Simpson 				error = EADDRINUSE;
600ec002feeSBruce M Simpson 			} else {
601810d5e89SGleb Smirnoff 				error = if_addmulti(priv->ifp,
602ec002feeSBruce M Simpson 				    (struct sockaddr *)&sa_dl, &ifma);
603ec002feeSBruce M Simpson 			}
604810d5e89SGleb Smirnoff 			break;
605810d5e89SGleb Smirnoff 		    }
606810d5e89SGleb Smirnoff 		case NGM_ETHER_DEL_MULTI:
607810d5e89SGleb Smirnoff 		    {
608810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
609810d5e89SGleb Smirnoff 
610810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
611810d5e89SGleb Smirnoff 				error = EINVAL;
612810d5e89SGleb Smirnoff 				break;
613810d5e89SGleb Smirnoff 			}
614ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
615810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
616810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
617ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
618810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
619810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
620810d5e89SGleb Smirnoff 			error = if_delmulti(priv->ifp,
621810d5e89SGleb Smirnoff 			    (struct sockaddr *)&sa_dl);
622810d5e89SGleb Smirnoff 			break;
623810d5e89SGleb Smirnoff 		    }
624cefddd66SGleb Smirnoff 		case NGM_ETHER_DETACH:
625cefddd66SGleb Smirnoff 			ng_ether_detach(priv->ifp);
626cefddd66SGleb Smirnoff 			break;
627e1e1452dSArchie Cobbs 		default:
628e1e1452dSArchie Cobbs 			error = EINVAL;
629e1e1452dSArchie Cobbs 			break;
630e1e1452dSArchie Cobbs 		}
631e1e1452dSArchie Cobbs 		break;
632e1e1452dSArchie Cobbs 	default:
633e1e1452dSArchie Cobbs 		error = EINVAL;
634e1e1452dSArchie Cobbs 		break;
635e1e1452dSArchie Cobbs 	}
636069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
637069154d5SJulian Elischer 	NG_FREE_MSG(msg);
638e1e1452dSArchie Cobbs 	return (error);
639e1e1452dSArchie Cobbs }
640e1e1452dSArchie Cobbs 
641e1e1452dSArchie Cobbs /*
642e1e1452dSArchie Cobbs  * Receive data on a hook.
643f664dcdeSJulian Elischer  * Since we use per-hook recveive methods this should never be called.
644e1e1452dSArchie Cobbs  */
645e1e1452dSArchie Cobbs static int
646069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
647e1e1452dSArchie Cobbs {
648069154d5SJulian Elischer 	NG_FREE_ITEM(item);
6493ca24c28SJulian Elischer 
6506e551fb6SDavid E. O'Brien 	panic("%s: weird hook", __func__);
651e1e1452dSArchie Cobbs }
652e1e1452dSArchie Cobbs 
653e1e1452dSArchie Cobbs /*
6541a292b80SArchie Cobbs  * Handle an mbuf received on the "lower" or "orphan" hook.
655e1e1452dSArchie Cobbs  */
656e1e1452dSArchie Cobbs static int
657f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item)
658e1e1452dSArchie Cobbs {
659f664dcdeSJulian Elischer 	struct mbuf *m;
660f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
66130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
662a1479aa2SArchie Cobbs  	struct ifnet *const ifp = priv->ifp;
663a1479aa2SArchie Cobbs 
664f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
665f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
666f664dcdeSJulian Elischer 
667a1479aa2SArchie Cobbs 	/* Check whether interface is ready for packets */
668f664dcdeSJulian Elischer 
66913f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
67013f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
671a1479aa2SArchie Cobbs 		NG_FREE_M(m);
672a1479aa2SArchie Cobbs 		return (ENETDOWN);
673a1479aa2SArchie Cobbs 	}
674e1e1452dSArchie Cobbs 
675e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
676e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
677069154d5SJulian Elischer 		NG_FREE_M(m);
678e1e1452dSArchie Cobbs 		return (EINVAL);
679e1e1452dSArchie Cobbs 	}
680e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
6817b9f235fSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
682e1e1452dSArchie Cobbs 		return (ENOBUFS);
683e1e1452dSArchie Cobbs 
6844b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6854b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6867b9f235fSArchie Cobbs 
6877b9f235fSArchie Cobbs 		/* Make the mbuf writable if it's not already */
6887b9f235fSArchie Cobbs 		if (!M_WRITABLE(m)
6897b9f235fSArchie Cobbs 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
6907b9f235fSArchie Cobbs 			return (ENOBUFS);
6917b9f235fSArchie Cobbs 
6927b9f235fSArchie Cobbs 		/* Overwrite source MAC address */
6934a0d6638SRuslan Ermilov 		bcopy(IF_LLADDR(ifp),
6944b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
6954b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
6964b39c3c7SArchie Cobbs 	}
697561e4fb9SJulian Elischer 
698e1e1452dSArchie Cobbs 	/* Send it on its way */
699a1479aa2SArchie Cobbs 	return ether_output_frame(ifp, m);
700e1e1452dSArchie Cobbs }
701e1e1452dSArchie Cobbs 
702e1e1452dSArchie Cobbs /*
703e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
704e1e1452dSArchie Cobbs  */
705e1e1452dSArchie Cobbs static int
706f664dcdeSJulian Elischer ng_ether_rcv_upper(hook_p hook, item_p item)
707e1e1452dSArchie Cobbs {
708f664dcdeSJulian Elischer 	struct mbuf *m;
709f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
71030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7116512768bSGleb Smirnoff 	struct ifnet *ifp = priv->ifp;
712e1e1452dSArchie Cobbs 
713f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
714f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
715f664dcdeSJulian Elischer 
716c60c00bcSRuslan Ermilov 	/* Check length and pull off header */
717c60c00bcSRuslan Ermilov 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
718c60c00bcSRuslan Ermilov 		NG_FREE_M(m);
719c60c00bcSRuslan Ermilov 		return (EINVAL);
720c60c00bcSRuslan Ermilov 	}
721c60c00bcSRuslan Ermilov 	if (m->m_len < sizeof(struct ether_header) &&
722c60c00bcSRuslan Ermilov 	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
723c60c00bcSRuslan Ermilov 		return (ENOBUFS);
724c60c00bcSRuslan Ermilov 
7256512768bSGleb Smirnoff 	m->m_pkthdr.rcvif = ifp;
726e1e1452dSArchie Cobbs 
727fd6238a6SAndrew Thompson 	/* Pass the packet to the bridge, it may come back to us */
7286512768bSGleb Smirnoff 	if (ifp->if_bridge) {
729fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
7306512768bSGleb Smirnoff 		if (m == NULL)
7316512768bSGleb Smirnoff 			return (0);
7326512768bSGleb Smirnoff 	}
733a176c2aeSGleb Smirnoff 
734e1e1452dSArchie Cobbs 	/* Route packet back in */
735fd6238a6SAndrew Thompson 	ether_demux(ifp, m);
736e1e1452dSArchie Cobbs 	return (0);
737e1e1452dSArchie Cobbs }
738e1e1452dSArchie Cobbs 
739e1e1452dSArchie Cobbs /*
7401acb27c6SJulian Elischer  * Shutdown node. This resets the node but does not remove it
7411acb27c6SJulian Elischer  * unless the REALLY_DIE flag is set.
742e1e1452dSArchie Cobbs  */
743e1e1452dSArchie Cobbs static int
744069154d5SJulian Elischer ng_ether_shutdown(node_p node)
745e1e1452dSArchie Cobbs {
74630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7474b39c3c7SArchie Cobbs 
748be4252b3SJulian Elischer 	if (node->nd_flags & NGF_REALLY_DIE) {
7491acb27c6SJulian Elischer 		/*
7501acb27c6SJulian Elischer 		 * WE came here because the ethernet card is being unloaded,
7511acb27c6SJulian Elischer 		 * so stop being persistant.
7521acb27c6SJulian Elischer 		 * Actually undo all the things we did on creation.
7531acb27c6SJulian Elischer 		 * Assume the ifp has already been freed.
7541acb27c6SJulian Elischer 		 */
7551acb27c6SJulian Elischer 		NG_NODE_SET_PRIVATE(node, NULL);
7561ede983cSDag-Erling Smørgrav 		free(priv, M_NETGRAPH);
7571acb27c6SJulian Elischer 		NG_NODE_UNREF(node);	/* free node itself */
7581acb27c6SJulian Elischer 		return (0);
759069154d5SJulian Elischer 	}
760018df1c3SBrian Feldman 	if (priv->promisc) {		/* disable promiscuous mode */
761018df1c3SBrian Feldman 		(void)ifpromisc(priv->ifp, 0);
762018df1c3SBrian Feldman 		priv->promisc = 0;
763018df1c3SBrian Feldman 	}
764be4252b3SJulian Elischer 	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
765be4252b3SJulian Elischer 
766e1e1452dSArchie Cobbs 	return (0);
767e1e1452dSArchie Cobbs }
768e1e1452dSArchie Cobbs 
769e1e1452dSArchie Cobbs /*
770e1e1452dSArchie Cobbs  * Hook disconnection.
771e1e1452dSArchie Cobbs  */
772e1e1452dSArchie Cobbs static int
773e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
774e1e1452dSArchie Cobbs {
77530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
776e1e1452dSArchie Cobbs 
777a3e232d6SArchie Cobbs 	if (hook == priv->upper) {
778e1e1452dSArchie Cobbs 		priv->upper = NULL;
779b712e9ecSBrian Feldman 		if (priv->ifp != NULL)		/* restore h/w csum */
780b712e9ecSBrian Feldman 			priv->ifp->if_hwassist = priv->hwassist;
7811a292b80SArchie Cobbs 	} else if (hook == priv->lower)
782e1e1452dSArchie Cobbs 		priv->lower = NULL;
7831a292b80SArchie Cobbs 	else if (hook == priv->orphan)
7841a292b80SArchie Cobbs 		priv->orphan = NULL;
7851a292b80SArchie Cobbs 	else
7866e551fb6SDavid E. O'Brien 		panic("%s: weird hook", __func__);
78730400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
78830400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
78930400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
790e1e1452dSArchie Cobbs 	return (0);
791e1e1452dSArchie Cobbs }
792e1e1452dSArchie Cobbs 
793e1e1452dSArchie Cobbs /******************************************************************
794e1e1452dSArchie Cobbs 		    	INITIALIZATION
795e1e1452dSArchie Cobbs ******************************************************************/
796e1e1452dSArchie Cobbs 
797e1e1452dSArchie Cobbs /*
798e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
799e1e1452dSArchie Cobbs  */
800e1e1452dSArchie Cobbs static int
801e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
802e1e1452dSArchie Cobbs {
803e1e1452dSArchie Cobbs 	int error = 0;
804e1e1452dSArchie Cobbs 
805e1e1452dSArchie Cobbs 	switch (event) {
806e1e1452dSArchie Cobbs 	case MOD_LOAD:
807e1e1452dSArchie Cobbs 
808e1e1452dSArchie Cobbs 		/* Register function hooks */
809e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
810e1e1452dSArchie Cobbs 			error = EEXIST;
811e1e1452dSArchie Cobbs 			break;
812e1e1452dSArchie Cobbs 		}
813e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
814e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
815e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
816e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
817e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
8181c7899c7SGleb Smirnoff 		ng_ether_link_state_p = ng_ether_link_state;
819e1e1452dSArchie Cobbs 
820*499f60b1SAndriy Gapon 		ng_ether_ifnet_arrival_cookie =
821*499f60b1SAndriy Gapon 		    EVENTHANDLER_REGISTER(ifnet_arrival_event,
822*499f60b1SAndriy Gapon 		    ng_ether_ifnet_arrival_event, NULL, EVENTHANDLER_PRI_ANY);
823e1e1452dSArchie Cobbs 		break;
824e1e1452dSArchie Cobbs 
825e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
826e1e1452dSArchie Cobbs 
827e1e1452dSArchie Cobbs 		/*
828e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
829e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
830e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
831e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
832e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
833e1e1452dSArchie Cobbs 		 */
834e1e1452dSArchie Cobbs 
835*499f60b1SAndriy Gapon 		EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
836*499f60b1SAndriy Gapon 		    ng_ether_ifnet_arrival_cookie);
837*499f60b1SAndriy Gapon 
838e1e1452dSArchie Cobbs 		/* Unregister function hooks */
839e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
840e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
841e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
842e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
843e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
8441c7899c7SGleb Smirnoff 		ng_ether_link_state_p = NULL;
845e1e1452dSArchie Cobbs 		break;
846e1e1452dSArchie Cobbs 
847e1e1452dSArchie Cobbs 	default:
848e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
849e1e1452dSArchie Cobbs 		break;
850e1e1452dSArchie Cobbs 	}
851e1e1452dSArchie Cobbs 	return (error);
852e1e1452dSArchie Cobbs }
853e1e1452dSArchie Cobbs 
854d0728d71SRobert Watson static void
855d0728d71SRobert Watson vnet_ng_ether_init(const void *unused)
856aef8f344SMarko Zec {
857aef8f344SMarko Zec 	struct ifnet *ifp;
858aef8f344SMarko Zec 
859d0728d71SRobert Watson 	/* If module load was rejected, don't attach to vnets. */
860d0728d71SRobert Watson 	if (ng_ether_attach_p != ng_ether_attach)
861d0728d71SRobert Watson 		return;
862d0728d71SRobert Watson 
863aef8f344SMarko Zec 	/* Create nodes for any already-existing Ethernet interfaces. */
864aef8f344SMarko Zec 	IFNET_RLOCK();
865aef8f344SMarko Zec 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
866aef8f344SMarko Zec 		if (ifp->if_type == IFT_ETHER
867aef8f344SMarko Zec 		    || ifp->if_type == IFT_L2VLAN)
868aef8f344SMarko Zec 			ng_ether_attach(ifp);
869aef8f344SMarko Zec 	}
870aef8f344SMarko Zec 	IFNET_RUNLOCK();
871aef8f344SMarko Zec }
872d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
873d0728d71SRobert Watson     vnet_ng_ether_init, NULL);
874