xref: /freebsd/sys/netgraph/ng_ether.c (revision d0728d71742e6b2304a3b370f4f4bec9e7d1dc5e)
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>
55e1e1452dSArchie Cobbs #include <sys/syslog.h>
56e1e1452dSArchie Cobbs #include <sys/socket.h>
57603724d3SBjoern A. Zeeb #include <sys/vimage.h>
58e1e1452dSArchie Cobbs 
59e1e1452dSArchie Cobbs #include <net/if.h>
60810d5e89SGleb Smirnoff #include <net/if_dl.h>
61e1e1452dSArchie Cobbs #include <net/if_types.h>
62e1e1452dSArchie Cobbs #include <net/if_arp.h>
63e1e1452dSArchie Cobbs #include <net/if_var.h>
64e1e1452dSArchie Cobbs #include <net/ethernet.h>
65fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
664b79449eSBjoern A. Zeeb #include <net/vnet.h>
67e1e1452dSArchie Cobbs 
68e1e1452dSArchie Cobbs #include <netgraph/ng_message.h>
69e1e1452dSArchie Cobbs #include <netgraph/netgraph.h>
70e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h>
71e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h>
72e1e1452dSArchie Cobbs 
735240dcdbSRuslan Ermilov #define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
74e1e1452dSArchie Cobbs 
753c976c3fSPawel Jakub Dawidek /* Per-node private data */
763c976c3fSPawel Jakub Dawidek struct private {
773c976c3fSPawel Jakub Dawidek 	struct ifnet	*ifp;		/* associated interface */
783c976c3fSPawel Jakub Dawidek 	hook_p		upper;		/* upper hook connection */
791a292b80SArchie Cobbs 	hook_p		lower;		/* lower hook connection */
801a292b80SArchie Cobbs 	hook_p		orphan;		/* orphan hook connection */
813c976c3fSPawel Jakub Dawidek 	u_char		autoSrcAddr;	/* always overwrite source address */
823c976c3fSPawel Jakub Dawidek 	u_char		promisc;	/* promiscuous mode enabled */
833c976c3fSPawel Jakub Dawidek 	u_long		hwassist;	/* hardware checksum capabilities */
843c976c3fSPawel Jakub Dawidek 	u_int		flags;		/* flags e.g. really die */
853c976c3fSPawel Jakub Dawidek };
863c976c3fSPawel Jakub Dawidek typedef struct private *priv_p;
87e1e1452dSArchie Cobbs 
88edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */
89edbb5246SSam Leffler extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
90edbb5246SSam Leffler extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
91edbb5246SSam Leffler extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
92edbb5246SSam Leffler extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
93edbb5246SSam Leffler extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
941c7899c7SGleb Smirnoff extern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
95edbb5246SSam Leffler 
96e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */
97edbb5246SSam Leffler static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
98edbb5246SSam Leffler static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
99e1e1452dSArchie Cobbs static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
100e1e1452dSArchie Cobbs static void	ng_ether_attach(struct ifnet *ifp);
101e1e1452dSArchie Cobbs static void	ng_ether_detach(struct ifnet *ifp);
1021c7899c7SGleb Smirnoff static void	ng_ether_link_state(struct ifnet *ifp, int state);
103e1e1452dSArchie Cobbs 
104e1e1452dSArchie Cobbs /* Other functions */
105f664dcdeSJulian Elischer static int	ng_ether_rcv_lower(hook_p node, item_p item);
106f664dcdeSJulian Elischer static int	ng_ether_rcv_upper(hook_p node, item_p item);
107e1e1452dSArchie Cobbs 
108e1e1452dSArchie Cobbs /* Netgraph node methods */
109e1e1452dSArchie Cobbs static ng_constructor_t	ng_ether_constructor;
110e1e1452dSArchie Cobbs static ng_rcvmsg_t	ng_ether_rcvmsg;
111069154d5SJulian Elischer static ng_shutdown_t	ng_ether_shutdown;
112e1e1452dSArchie Cobbs static ng_newhook_t	ng_ether_newhook;
113e1e1452dSArchie Cobbs static ng_rcvdata_t	ng_ether_rcvdata;
114e1e1452dSArchie Cobbs static ng_disconnect_t	ng_ether_disconnect;
115e1e1452dSArchie Cobbs static int		ng_ether_mod_event(module_t mod, int event, void *data);
116e1e1452dSArchie Cobbs 
117e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
118e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = {
119e1e1452dSArchie Cobbs 	{
120e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
121e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFNAME,
122e1e1452dSArchie Cobbs 	  "getifname",
123e1e1452dSArchie Cobbs 	  NULL,
124e1e1452dSArchie Cobbs 	  &ng_parse_string_type
125e1e1452dSArchie Cobbs 	},
126e1e1452dSArchie Cobbs 	{
127e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
128e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFINDEX,
129e1e1452dSArchie Cobbs 	  "getifindex",
130e1e1452dSArchie Cobbs 	  NULL,
131e1e1452dSArchie Cobbs 	  &ng_parse_int32_type
132e1e1452dSArchie Cobbs 	},
1334b39c3c7SArchie Cobbs 	{
1344b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
1354b39c3c7SArchie Cobbs 	  NGM_ETHER_GET_ENADDR,
1364b39c3c7SArchie Cobbs 	  "getenaddr",
1374b39c3c7SArchie Cobbs 	  NULL,
1388c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type
1394b39c3c7SArchie Cobbs 	},
1404b39c3c7SArchie Cobbs 	{
1414b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
14256045c66SArchie Cobbs 	  NGM_ETHER_SET_ENADDR,
14356045c66SArchie Cobbs 	  "setenaddr",
1448c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type,
14556045c66SArchie Cobbs 	  NULL
14656045c66SArchie Cobbs 	},
14756045c66SArchie Cobbs 	{
14856045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
14956045c66SArchie Cobbs 	  NGM_ETHER_GET_PROMISC,
15056045c66SArchie Cobbs 	  "getpromisc",
15156045c66SArchie Cobbs 	  NULL,
15256045c66SArchie Cobbs 	  &ng_parse_int32_type
15356045c66SArchie Cobbs 	},
15456045c66SArchie Cobbs 	{
15556045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1564b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_PROMISC,
1574b39c3c7SArchie Cobbs 	  "setpromisc",
1584b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1594b39c3c7SArchie Cobbs 	  NULL
1604b39c3c7SArchie Cobbs 	},
1614b39c3c7SArchie Cobbs 	{
1624b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
16356045c66SArchie Cobbs 	  NGM_ETHER_GET_AUTOSRC,
16456045c66SArchie Cobbs 	  "getautosrc",
16556045c66SArchie Cobbs 	  NULL,
16656045c66SArchie Cobbs 	  &ng_parse_int32_type
16756045c66SArchie Cobbs 	},
16856045c66SArchie Cobbs 	{
16956045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1704b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_AUTOSRC,
1714b39c3c7SArchie Cobbs 	  "setautosrc",
1724b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1734b39c3c7SArchie Cobbs 	  NULL
1744b39c3c7SArchie Cobbs 	},
175810d5e89SGleb Smirnoff 	{
176810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
177810d5e89SGleb Smirnoff 	  NGM_ETHER_ADD_MULTI,
178810d5e89SGleb Smirnoff 	  "addmulti",
179810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
180810d5e89SGleb Smirnoff 	  NULL
181810d5e89SGleb Smirnoff 	},
182810d5e89SGleb Smirnoff 	{
183810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
184810d5e89SGleb Smirnoff 	  NGM_ETHER_DEL_MULTI,
185810d5e89SGleb Smirnoff 	  "delmulti",
186810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
187810d5e89SGleb Smirnoff 	  NULL
188810d5e89SGleb Smirnoff 	},
189cefddd66SGleb Smirnoff 	{
190cefddd66SGleb Smirnoff 	  NGM_ETHER_COOKIE,
191cefddd66SGleb Smirnoff 	  NGM_ETHER_DETACH,
192cefddd66SGleb Smirnoff 	  "detach",
193cefddd66SGleb Smirnoff 	  NULL,
194cefddd66SGleb Smirnoff 	  NULL
195cefddd66SGleb Smirnoff 	},
196e1e1452dSArchie Cobbs 	{ 0 }
197e1e1452dSArchie Cobbs };
198e1e1452dSArchie Cobbs 
199e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = {
200f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
201f8aae777SJulian Elischer 	.name =		NG_ETHER_NODE_TYPE,
202f8aae777SJulian Elischer 	.mod_event =	ng_ether_mod_event,
203f8aae777SJulian Elischer 	.constructor =	ng_ether_constructor,
204f8aae777SJulian Elischer 	.rcvmsg =	ng_ether_rcvmsg,
205f8aae777SJulian Elischer 	.shutdown =	ng_ether_shutdown,
206f8aae777SJulian Elischer 	.newhook =	ng_ether_newhook,
207f8aae777SJulian Elischer 	.rcvdata =	ng_ether_rcvdata,
208f8aae777SJulian Elischer 	.disconnect =	ng_ether_disconnect,
209f8aae777SJulian Elischer 	.cmdlist =	ng_ether_cmdlist,
210e1e1452dSArchie Cobbs };
211e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct);
212e1e1452dSArchie Cobbs 
213e1e1452dSArchie Cobbs /******************************************************************
214e1e1452dSArchie Cobbs 		    ETHERNET FUNCTION HOOKS
215e1e1452dSArchie Cobbs ******************************************************************/
216e1e1452dSArchie Cobbs 
217e1e1452dSArchie Cobbs /*
218e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface. We get to
219e1e1452dSArchie Cobbs  * look at it here before any upper layer protocols do.
220e1e1452dSArchie Cobbs  *
221e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
222e1e1452dSArchie Cobbs  */
223e1e1452dSArchie Cobbs static void
224edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
225e1e1452dSArchie Cobbs {
226e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
22730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2281a292b80SArchie Cobbs 	int error;
229e1e1452dSArchie Cobbs 
230e1e1452dSArchie Cobbs 	/* If "lower" hook not connected, let packet continue */
2311a292b80SArchie Cobbs 	if (priv->lower == NULL)
232e1e1452dSArchie Cobbs 		return;
2331a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
234e1e1452dSArchie Cobbs }
235e1e1452dSArchie Cobbs 
236e1e1452dSArchie Cobbs /*
237e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface, and which
238e1e1452dSArchie Cobbs  * does not match any of our known protocols (an ``orphan'').
239e1e1452dSArchie Cobbs  *
240e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
241e1e1452dSArchie Cobbs  */
242e1e1452dSArchie Cobbs static void
243edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
244e1e1452dSArchie Cobbs {
245e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
24630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2471a292b80SArchie Cobbs 	int error;
248e1e1452dSArchie Cobbs 
2491a292b80SArchie Cobbs 	/* If "orphan" hook not connected, discard packet */
2501a292b80SArchie Cobbs 	if (priv->orphan == NULL) {
251e1e1452dSArchie Cobbs 		m_freem(m);
252e1e1452dSArchie Cobbs 		return;
253e1e1452dSArchie Cobbs 	}
2541a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->orphan, m);
255e1e1452dSArchie Cobbs }
256e1e1452dSArchie Cobbs 
257e1e1452dSArchie Cobbs /*
258e1e1452dSArchie Cobbs  * Handle a packet that is going out on an interface.
259e1e1452dSArchie Cobbs  * The Ethernet header is already attached to the mbuf.
260e1e1452dSArchie Cobbs  */
261e1e1452dSArchie Cobbs static int
262e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
263e1e1452dSArchie Cobbs {
264e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
26530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
266e1e1452dSArchie Cobbs 	int error = 0;
267e1e1452dSArchie Cobbs 
268e1e1452dSArchie Cobbs 	/* If "upper" hook not connected, let packet continue */
269e1e1452dSArchie Cobbs 	if (priv->upper == NULL)
270e1e1452dSArchie Cobbs 		return (0);
271e1e1452dSArchie Cobbs 
272e1e1452dSArchie Cobbs 	/* Send it out "upper" hook */
273f089869fSMarko Zec 	NG_OUTBOUND_THREAD_REF();
274069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
275f089869fSMarko Zec 	NG_OUTBOUND_THREAD_UNREF();
276e1e1452dSArchie Cobbs 	return (error);
277e1e1452dSArchie Cobbs }
278e1e1452dSArchie Cobbs 
279e1e1452dSArchie Cobbs /*
280e1e1452dSArchie Cobbs  * A new Ethernet interface has been attached.
281e1e1452dSArchie Cobbs  * Create a new node for it, etc.
282e1e1452dSArchie Cobbs  */
283e1e1452dSArchie Cobbs static void
284e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp)
285e1e1452dSArchie Cobbs {
286e1e1452dSArchie Cobbs 	priv_p priv;
287e1e1452dSArchie Cobbs 	node_p node;
288e1e1452dSArchie Cobbs 
289aef8f344SMarko Zec 	/*
290aef8f344SMarko Zec 	 * Do not create / attach an ether node to this ifnet if
291aef8f344SMarko Zec 	 * a netgraph node with the same name already exists.
292aef8f344SMarko Zec 	 * This should prevent ether nodes to become attached to
293aef8f344SMarko Zec 	 * eiface nodes, which may be problematic due to naming
294aef8f344SMarko Zec 	 * clashes.
295aef8f344SMarko Zec 	 */
296aef8f344SMarko Zec 	if ((node = ng_name2noderef(NULL, ifp->if_xname)) != NULL) {
297aef8f344SMarko Zec 		NG_NODE_UNREF(node);
298aef8f344SMarko Zec 		return;
299aef8f344SMarko Zec 	}
300aef8f344SMarko Zec 
301e1e1452dSArchie Cobbs 	/* Create node */
3026e551fb6SDavid E. O'Brien 	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
303e1e1452dSArchie Cobbs 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
304e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
3059bf40edeSBrooks Davis 		    __func__, "create node", ifp->if_xname);
306e1e1452dSArchie Cobbs 		return;
307e1e1452dSArchie Cobbs 	}
308e1e1452dSArchie Cobbs 
309e1e1452dSArchie Cobbs 	/* Allocate private data */
3101ede983cSDag-Erling Smørgrav 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
311e1e1452dSArchie Cobbs 	if (priv == NULL) {
312e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
3139bf40edeSBrooks Davis 		    __func__, "allocate memory", ifp->if_xname);
31430400f03SJulian Elischer 		NG_NODE_UNREF(node);
315e1e1452dSArchie Cobbs 		return;
316e1e1452dSArchie Cobbs 	}
31730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
318e1e1452dSArchie Cobbs 	priv->ifp = ifp;
3195240dcdbSRuslan Ermilov 	IFP2NG(ifp) = node;
320a3e232d6SArchie Cobbs 	priv->hwassist = ifp->if_hwassist;
321e1e1452dSArchie Cobbs 
322e1e1452dSArchie Cobbs 	/* Try to give the node the same name as the interface */
3239bf40edeSBrooks Davis 	if (ng_name_node(node, ifp->if_xname) != 0) {
324e1e1452dSArchie Cobbs 		log(LOG_WARNING, "%s: can't name node %s\n",
3259bf40edeSBrooks Davis 		    __func__, ifp->if_xname);
326e1e1452dSArchie Cobbs 	}
327e1e1452dSArchie Cobbs }
328e1e1452dSArchie Cobbs 
329e1e1452dSArchie Cobbs /*
330e1e1452dSArchie Cobbs  * An Ethernet interface is being detached.
3311acb27c6SJulian Elischer  * REALLY Destroy its node.
332e1e1452dSArchie Cobbs  */
333e1e1452dSArchie Cobbs static void
334e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp)
335e1e1452dSArchie Cobbs {
336e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
3371acb27c6SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
338e1e1452dSArchie Cobbs 
3391acb27c6SJulian Elischer 	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
3401acb27c6SJulian Elischer 	/*
3411acb27c6SJulian Elischer 	 * We can't assume the ifnet is still around when we run shutdown
3421acb27c6SJulian Elischer 	 * So zap it now. XXX We HOPE that anything running at this time
3431acb27c6SJulian Elischer 	 * handles it (as it should in the non netgraph case).
3441acb27c6SJulian Elischer 	 */
3455240dcdbSRuslan Ermilov 	IFP2NG(ifp) = NULL;
3461acb27c6SJulian Elischer 	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
3471acb27c6SJulian Elischer 	ng_rmnode_self(node);		/* remove all netgraph parts */
348e1e1452dSArchie Cobbs }
349e1e1452dSArchie Cobbs 
3501c7899c7SGleb Smirnoff /*
3511c7899c7SGleb Smirnoff  * Notify graph about link event.
3521c7899c7SGleb Smirnoff  * if_link_state_change() has already checked that the state has changed.
3531c7899c7SGleb Smirnoff  */
3541c7899c7SGleb Smirnoff static void
3551c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state)
3561c7899c7SGleb Smirnoff {
3571c7899c7SGleb Smirnoff 	const node_p node = IFP2NG(ifp);
3581c7899c7SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
3591c7899c7SGleb Smirnoff 	struct ng_mesg *msg;
3601c7899c7SGleb Smirnoff 	int cmd, dummy_error = 0;
3611c7899c7SGleb Smirnoff 
3621c7899c7SGleb Smirnoff 	if (priv->lower == NULL)
3631c7899c7SGleb Smirnoff                 return;
3641c7899c7SGleb Smirnoff 
3651c7899c7SGleb Smirnoff 	if (state == LINK_STATE_UP)
3661c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_UP;
3671c7899c7SGleb Smirnoff 	else if (state == LINK_STATE_DOWN)
3681c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_DOWN;
3691c7899c7SGleb Smirnoff 	else
3701c7899c7SGleb Smirnoff 		return;
3711c7899c7SGleb Smirnoff 
3721c7899c7SGleb Smirnoff 	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
3731c7899c7SGleb Smirnoff 	if (msg != NULL)
3741c7899c7SGleb Smirnoff 		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
3751c7899c7SGleb Smirnoff }
3761c7899c7SGleb Smirnoff 
377e1e1452dSArchie Cobbs /******************************************************************
378e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
379e1e1452dSArchie Cobbs ******************************************************************/
380e1e1452dSArchie Cobbs 
381e1e1452dSArchie Cobbs /*
382e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
383e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
384e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
385e1e1452dSArchie Cobbs  */
386e1e1452dSArchie Cobbs static int
387069154d5SJulian Elischer ng_ether_constructor(node_p node)
388e1e1452dSArchie Cobbs {
389e1e1452dSArchie Cobbs 	return (EINVAL);
390e1e1452dSArchie Cobbs }
391e1e1452dSArchie Cobbs 
392e1e1452dSArchie Cobbs /*
393e1e1452dSArchie Cobbs  * Check for attaching a new hook.
394e1e1452dSArchie Cobbs  */
395e1e1452dSArchie Cobbs static	int
396e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
397e1e1452dSArchie Cobbs {
39830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
399e1e1452dSArchie Cobbs 	hook_p *hookptr;
400e1e1452dSArchie Cobbs 
401e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
402e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
403e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
404e1e1452dSArchie Cobbs 
405e1e1452dSArchie Cobbs 	/* Which hook? */
406f664dcdeSJulian Elischer 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0) {
407e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
408f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_upper);
409f089869fSMarko Zec 		NG_HOOK_SET_TO_INBOUND(hook);
410f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
411e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
412f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
413f664dcdeSJulian Elischer 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
4141a292b80SArchie Cobbs 		hookptr = &priv->orphan;
415f664dcdeSJulian Elischer 		NG_HOOK_SET_RCVDATA(hook, ng_ether_rcv_lower);
416f664dcdeSJulian Elischer 	} else
417e1e1452dSArchie Cobbs 		return (EINVAL);
418e1e1452dSArchie Cobbs 
419e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
420e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
421e1e1452dSArchie Cobbs 		return (EISCONN);
422e1e1452dSArchie Cobbs 
423a3e232d6SArchie Cobbs 	/* Disable hardware checksums while 'upper' hook is connected */
424a3e232d6SArchie Cobbs 	if (hookptr == &priv->upper)
425a3e232d6SArchie Cobbs 		priv->ifp->if_hwassist = 0;
426c1b8a9edSAlexander Motin 	NG_HOOK_HI_STACK(hook);
427e1e1452dSArchie Cobbs 	/* OK */
428e1e1452dSArchie Cobbs 	*hookptr = hook;
429e1e1452dSArchie Cobbs 	return (0);
430e1e1452dSArchie Cobbs }
431e1e1452dSArchie Cobbs 
432e1e1452dSArchie Cobbs /*
433e1e1452dSArchie Cobbs  * Receive an incoming control message.
434e1e1452dSArchie Cobbs  */
435e1e1452dSArchie Cobbs static int
436069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
437e1e1452dSArchie Cobbs {
43830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
439e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
440e1e1452dSArchie Cobbs 	int error = 0;
441069154d5SJulian Elischer 	struct ng_mesg *msg;
442e1e1452dSArchie Cobbs 
443069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
444e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
445e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
446e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
447e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
448bbb75d78SRuslan Ermilov 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
449e1e1452dSArchie Cobbs 			if (resp == NULL) {
450e1e1452dSArchie Cobbs 				error = ENOMEM;
451e1e1452dSArchie Cobbs 				break;
452e1e1452dSArchie Cobbs 			}
453bbb75d78SRuslan Ermilov 			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
454e1e1452dSArchie Cobbs 			break;
455e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
456e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
457e1e1452dSArchie Cobbs 			if (resp == NULL) {
458e1e1452dSArchie Cobbs 				error = ENOMEM;
459e1e1452dSArchie Cobbs 				break;
460e1e1452dSArchie Cobbs 			}
461e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
462e1e1452dSArchie Cobbs 			break;
4634b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
4644b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
4654b39c3c7SArchie Cobbs 			if (resp == NULL) {
4664b39c3c7SArchie Cobbs 				error = ENOMEM;
4674b39c3c7SArchie Cobbs 				break;
4684b39c3c7SArchie Cobbs 			}
4694a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(priv->ifp),
4704b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
4714b39c3c7SArchie Cobbs 			break;
47256045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
47356045c66SArchie Cobbs 		    {
47456045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
47556045c66SArchie Cobbs 				error = EINVAL;
47656045c66SArchie Cobbs 				break;
47756045c66SArchie Cobbs 			}
47856045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
47956045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
48056045c66SArchie Cobbs 			break;
48156045c66SArchie Cobbs 		    }
48256045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
48356045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
48456045c66SArchie Cobbs 			if (resp == NULL) {
48556045c66SArchie Cobbs 				error = ENOMEM;
48656045c66SArchie Cobbs 				break;
48756045c66SArchie Cobbs 			}
48856045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
48956045c66SArchie Cobbs 			break;
4904b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
4914b39c3c7SArchie Cobbs 		    {
4924b39c3c7SArchie Cobbs 			u_char want;
4934b39c3c7SArchie Cobbs 
4944b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
4954b39c3c7SArchie Cobbs 				error = EINVAL;
4964b39c3c7SArchie Cobbs 				break;
4974b39c3c7SArchie Cobbs 			}
4984b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
4994b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
5004b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
5014b39c3c7SArchie Cobbs 					break;
5024b39c3c7SArchie Cobbs 				priv->promisc = want;
5034b39c3c7SArchie Cobbs 			}
5044b39c3c7SArchie Cobbs 			break;
5054b39c3c7SArchie Cobbs 		    }
50656045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
50756045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
50856045c66SArchie Cobbs 			if (resp == NULL) {
50956045c66SArchie Cobbs 				error = ENOMEM;
51056045c66SArchie Cobbs 				break;
51156045c66SArchie Cobbs 			}
51256045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
51356045c66SArchie Cobbs 			break;
5144b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
5154b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5164b39c3c7SArchie Cobbs 				error = EINVAL;
5174b39c3c7SArchie Cobbs 				break;
5184b39c3c7SArchie Cobbs 			}
5194b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5204b39c3c7SArchie Cobbs 			break;
521810d5e89SGleb Smirnoff 		case NGM_ETHER_ADD_MULTI:
522810d5e89SGleb Smirnoff 		    {
523810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
524ec002feeSBruce M Simpson 			struct ifmultiaddr *ifma;
525810d5e89SGleb Smirnoff 
526810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
527810d5e89SGleb Smirnoff 				error = EINVAL;
528810d5e89SGleb Smirnoff 				break;
529810d5e89SGleb Smirnoff 			}
530ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
531810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
532810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
533ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
534810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
535810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
536ec002feeSBruce M Simpson 			/*
537ec002feeSBruce M Simpson 			 * Netgraph is only permitted to join groups once
538ec002feeSBruce M Simpson 			 * via the if_addmulti() KPI, because it cannot hold
539ec002feeSBruce M Simpson 			 * struct ifmultiaddr * between calls. It may also
540ec002feeSBruce M Simpson 			 * lose a race while we check if the membership
541ec002feeSBruce M Simpson 			 * already exists.
542ec002feeSBruce M Simpson 			 */
543eb956cd0SRobert Watson 			if_maddr_rlock(priv->ifp);
544ec002feeSBruce M Simpson 			ifma = if_findmulti(priv->ifp,
545ec002feeSBruce M Simpson 			    (struct sockaddr *)&sa_dl);
546eb956cd0SRobert Watson 			if_maddr_runlock(priv->ifp);
547ec002feeSBruce M Simpson 			if (ifma != NULL) {
548ec002feeSBruce M Simpson 				error = EADDRINUSE;
549ec002feeSBruce M Simpson 			} else {
550810d5e89SGleb Smirnoff 				error = if_addmulti(priv->ifp,
551ec002feeSBruce M Simpson 				    (struct sockaddr *)&sa_dl, &ifma);
552ec002feeSBruce M Simpson 			}
553810d5e89SGleb Smirnoff 			break;
554810d5e89SGleb Smirnoff 		    }
555810d5e89SGleb Smirnoff 		case NGM_ETHER_DEL_MULTI:
556810d5e89SGleb Smirnoff 		    {
557810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
558810d5e89SGleb Smirnoff 
559810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
560810d5e89SGleb Smirnoff 				error = EINVAL;
561810d5e89SGleb Smirnoff 				break;
562810d5e89SGleb Smirnoff 			}
563ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
564810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
565810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
566ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
567810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
568810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
569810d5e89SGleb Smirnoff 			error = if_delmulti(priv->ifp,
570810d5e89SGleb Smirnoff 			    (struct sockaddr *)&sa_dl);
571810d5e89SGleb Smirnoff 			break;
572810d5e89SGleb Smirnoff 		    }
573cefddd66SGleb Smirnoff 		case NGM_ETHER_DETACH:
574cefddd66SGleb Smirnoff 			ng_ether_detach(priv->ifp);
575cefddd66SGleb Smirnoff 			break;
576e1e1452dSArchie Cobbs 		default:
577e1e1452dSArchie Cobbs 			error = EINVAL;
578e1e1452dSArchie Cobbs 			break;
579e1e1452dSArchie Cobbs 		}
580e1e1452dSArchie Cobbs 		break;
581e1e1452dSArchie Cobbs 	default:
582e1e1452dSArchie Cobbs 		error = EINVAL;
583e1e1452dSArchie Cobbs 		break;
584e1e1452dSArchie Cobbs 	}
585069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
586069154d5SJulian Elischer 	NG_FREE_MSG(msg);
587e1e1452dSArchie Cobbs 	return (error);
588e1e1452dSArchie Cobbs }
589e1e1452dSArchie Cobbs 
590e1e1452dSArchie Cobbs /*
591e1e1452dSArchie Cobbs  * Receive data on a hook.
592f664dcdeSJulian Elischer  * Since we use per-hook recveive methods this should never be called.
593e1e1452dSArchie Cobbs  */
594e1e1452dSArchie Cobbs static int
595069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
596e1e1452dSArchie Cobbs {
597069154d5SJulian Elischer 	NG_FREE_ITEM(item);
5983ca24c28SJulian Elischer 
5996e551fb6SDavid E. O'Brien 	panic("%s: weird hook", __func__);
6001a292b80SArchie Cobbs #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
6017ea5573cSDag-Erling Smørgrav 	return (0);
602b40ce416SJulian Elischer #endif
603e1e1452dSArchie Cobbs }
604e1e1452dSArchie Cobbs 
605e1e1452dSArchie Cobbs /*
6061a292b80SArchie Cobbs  * Handle an mbuf received on the "lower" or "orphan" hook.
607e1e1452dSArchie Cobbs  */
608e1e1452dSArchie Cobbs static int
609f664dcdeSJulian Elischer ng_ether_rcv_lower(hook_p hook, item_p item)
610e1e1452dSArchie Cobbs {
611f664dcdeSJulian Elischer 	struct mbuf *m;
612f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
61330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
614a1479aa2SArchie Cobbs  	struct ifnet *const ifp = priv->ifp;
615a1479aa2SArchie Cobbs 
616f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
617f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
618f664dcdeSJulian Elischer 
619a1479aa2SArchie Cobbs 	/* Check whether interface is ready for packets */
620f664dcdeSJulian Elischer 
62113f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
62213f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
623a1479aa2SArchie Cobbs 		NG_FREE_M(m);
624a1479aa2SArchie Cobbs 		return (ENETDOWN);
625a1479aa2SArchie Cobbs 	}
626e1e1452dSArchie Cobbs 
627e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
628e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
629069154d5SJulian Elischer 		NG_FREE_M(m);
630e1e1452dSArchie Cobbs 		return (EINVAL);
631e1e1452dSArchie Cobbs 	}
632e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
6337b9f235fSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
634e1e1452dSArchie Cobbs 		return (ENOBUFS);
635e1e1452dSArchie Cobbs 
6364b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6374b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6387b9f235fSArchie Cobbs 
6397b9f235fSArchie Cobbs 		/* Make the mbuf writable if it's not already */
6407b9f235fSArchie Cobbs 		if (!M_WRITABLE(m)
6417b9f235fSArchie Cobbs 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
6427b9f235fSArchie Cobbs 			return (ENOBUFS);
6437b9f235fSArchie Cobbs 
6447b9f235fSArchie Cobbs 		/* Overwrite source MAC address */
6454a0d6638SRuslan Ermilov 		bcopy(IF_LLADDR(ifp),
6464b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
6474b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
6484b39c3c7SArchie Cobbs 	}
649561e4fb9SJulian Elischer 
650e1e1452dSArchie Cobbs 	/* Send it on its way */
651a1479aa2SArchie Cobbs 	return ether_output_frame(ifp, m);
652e1e1452dSArchie Cobbs }
653e1e1452dSArchie Cobbs 
654e1e1452dSArchie Cobbs /*
655e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
656e1e1452dSArchie Cobbs  */
657e1e1452dSArchie Cobbs static int
658f664dcdeSJulian Elischer ng_ether_rcv_upper(hook_p hook, item_p item)
659e1e1452dSArchie Cobbs {
660f664dcdeSJulian Elischer 	struct mbuf *m;
661f664dcdeSJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
66230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6636512768bSGleb Smirnoff 	struct ifnet *ifp = priv->ifp;
664e1e1452dSArchie Cobbs 
665f664dcdeSJulian Elischer 	NGI_GET_M(item, m);
666f664dcdeSJulian Elischer 	NG_FREE_ITEM(item);
667f664dcdeSJulian Elischer 
668c60c00bcSRuslan Ermilov 	/* Check length and pull off header */
669c60c00bcSRuslan Ermilov 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
670c60c00bcSRuslan Ermilov 		NG_FREE_M(m);
671c60c00bcSRuslan Ermilov 		return (EINVAL);
672c60c00bcSRuslan Ermilov 	}
673c60c00bcSRuslan Ermilov 	if (m->m_len < sizeof(struct ether_header) &&
674c60c00bcSRuslan Ermilov 	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
675c60c00bcSRuslan Ermilov 		return (ENOBUFS);
676c60c00bcSRuslan Ermilov 
6776512768bSGleb Smirnoff 	m->m_pkthdr.rcvif = ifp;
678e1e1452dSArchie Cobbs 
679fd6238a6SAndrew Thompson 	/* Pass the packet to the bridge, it may come back to us */
6806512768bSGleb Smirnoff 	if (ifp->if_bridge) {
681fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
6826512768bSGleb Smirnoff 		if (m == NULL)
6836512768bSGleb Smirnoff 			return (0);
6846512768bSGleb Smirnoff 	}
685a176c2aeSGleb Smirnoff 
686e1e1452dSArchie Cobbs 	/* Route packet back in */
687fd6238a6SAndrew Thompson 	ether_demux(ifp, m);
688e1e1452dSArchie Cobbs 	return (0);
689e1e1452dSArchie Cobbs }
690e1e1452dSArchie Cobbs 
691e1e1452dSArchie Cobbs /*
6921acb27c6SJulian Elischer  * Shutdown node. This resets the node but does not remove it
6931acb27c6SJulian Elischer  * unless the REALLY_DIE flag is set.
694e1e1452dSArchie Cobbs  */
695e1e1452dSArchie Cobbs static int
696069154d5SJulian Elischer ng_ether_shutdown(node_p node)
697e1e1452dSArchie Cobbs {
69830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6994b39c3c7SArchie Cobbs 
700be4252b3SJulian Elischer 	if (node->nd_flags & NGF_REALLY_DIE) {
7011acb27c6SJulian Elischer 		/*
7021acb27c6SJulian Elischer 		 * WE came here because the ethernet card is being unloaded,
7031acb27c6SJulian Elischer 		 * so stop being persistant.
7041acb27c6SJulian Elischer 		 * Actually undo all the things we did on creation.
7051acb27c6SJulian Elischer 		 * Assume the ifp has already been freed.
7061acb27c6SJulian Elischer 		 */
7071acb27c6SJulian Elischer 		NG_NODE_SET_PRIVATE(node, NULL);
7081ede983cSDag-Erling Smørgrav 		free(priv, M_NETGRAPH);
7091acb27c6SJulian Elischer 		NG_NODE_UNREF(node);	/* free node itself */
7101acb27c6SJulian Elischer 		return (0);
711069154d5SJulian Elischer 	}
712018df1c3SBrian Feldman 	if (priv->promisc) {		/* disable promiscuous mode */
713018df1c3SBrian Feldman 		(void)ifpromisc(priv->ifp, 0);
714018df1c3SBrian Feldman 		priv->promisc = 0;
715018df1c3SBrian Feldman 	}
7164b39c3c7SArchie Cobbs 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
717be4252b3SJulian Elischer 	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
718be4252b3SJulian Elischer 
719e1e1452dSArchie Cobbs 	return (0);
720e1e1452dSArchie Cobbs }
721e1e1452dSArchie Cobbs 
722e1e1452dSArchie Cobbs /*
723e1e1452dSArchie Cobbs  * Hook disconnection.
724e1e1452dSArchie Cobbs  */
725e1e1452dSArchie Cobbs static int
726e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
727e1e1452dSArchie Cobbs {
72830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
729e1e1452dSArchie Cobbs 
730a3e232d6SArchie Cobbs 	if (hook == priv->upper) {
731e1e1452dSArchie Cobbs 		priv->upper = NULL;
732b712e9ecSBrian Feldman 		if (priv->ifp != NULL)		/* restore h/w csum */
733b712e9ecSBrian Feldman 			priv->ifp->if_hwassist = priv->hwassist;
7341a292b80SArchie Cobbs 	} else if (hook == priv->lower)
735e1e1452dSArchie Cobbs 		priv->lower = NULL;
7361a292b80SArchie Cobbs 	else if (hook == priv->orphan)
7371a292b80SArchie Cobbs 		priv->orphan = NULL;
7381a292b80SArchie Cobbs 	else
7396e551fb6SDavid E. O'Brien 		panic("%s: weird hook", __func__);
74030400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
74130400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
74230400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
743e1e1452dSArchie Cobbs 	return (0);
744e1e1452dSArchie Cobbs }
745e1e1452dSArchie Cobbs 
746e1e1452dSArchie Cobbs /******************************************************************
747e1e1452dSArchie Cobbs 		    	INITIALIZATION
748e1e1452dSArchie Cobbs ******************************************************************/
749e1e1452dSArchie Cobbs 
750e1e1452dSArchie Cobbs /*
751e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
752e1e1452dSArchie Cobbs  */
753e1e1452dSArchie Cobbs static int
754e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
755e1e1452dSArchie Cobbs {
756e1e1452dSArchie Cobbs 	int error = 0;
757e1e1452dSArchie Cobbs 	int s;
758e1e1452dSArchie Cobbs 
759e1e1452dSArchie Cobbs 	s = splnet();
760e1e1452dSArchie Cobbs 	switch (event) {
761e1e1452dSArchie Cobbs 	case MOD_LOAD:
762e1e1452dSArchie Cobbs 
763e1e1452dSArchie Cobbs 		/* Register function hooks */
764e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
765e1e1452dSArchie Cobbs 			error = EEXIST;
766e1e1452dSArchie Cobbs 			break;
767e1e1452dSArchie Cobbs 		}
768e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
769e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
770e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
771e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
772e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
7731c7899c7SGleb Smirnoff 		ng_ether_link_state_p = ng_ether_link_state;
774e1e1452dSArchie Cobbs 
775e1e1452dSArchie Cobbs 		break;
776e1e1452dSArchie Cobbs 
777e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
778e1e1452dSArchie Cobbs 
779e1e1452dSArchie Cobbs 		/*
780e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
781e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
782e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
783e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
784e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
785e1e1452dSArchie Cobbs 		 */
786e1e1452dSArchie Cobbs 
787e1e1452dSArchie Cobbs 		/* Unregister function hooks */
788e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
789e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
790e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
791e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
792e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
7931c7899c7SGleb Smirnoff 		ng_ether_link_state_p = NULL;
794e1e1452dSArchie Cobbs 		break;
795e1e1452dSArchie Cobbs 
796e1e1452dSArchie Cobbs 	default:
797e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
798e1e1452dSArchie Cobbs 		break;
799e1e1452dSArchie Cobbs 	}
800e1e1452dSArchie Cobbs 	splx(s);
801e1e1452dSArchie Cobbs 	return (error);
802e1e1452dSArchie Cobbs }
803e1e1452dSArchie Cobbs 
804d0728d71SRobert Watson static void
805d0728d71SRobert Watson vnet_ng_ether_init(const void *unused)
806aef8f344SMarko Zec {
807aef8f344SMarko Zec 	struct ifnet *ifp;
808aef8f344SMarko Zec 
809d0728d71SRobert Watson 	/* If module load was rejected, don't attach to vnets. */
810d0728d71SRobert Watson 	if (ng_ether_attach_p != ng_ether_attach)
811d0728d71SRobert Watson 		return;
812d0728d71SRobert Watson 
813aef8f344SMarko Zec 	/* Create nodes for any already-existing Ethernet interfaces. */
814aef8f344SMarko Zec 	IFNET_RLOCK();
815aef8f344SMarko Zec 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
816aef8f344SMarko Zec 		if (ifp->if_type == IFT_ETHER
817aef8f344SMarko Zec 		    || ifp->if_type == IFT_L2VLAN)
818aef8f344SMarko Zec 			ng_ether_attach(ifp);
819aef8f344SMarko Zec 	}
820aef8f344SMarko Zec 	IFNET_RUNLOCK();
821aef8f344SMarko Zec }
822d0728d71SRobert Watson VNET_SYSINIT(vnet_ng_ether_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
823d0728d71SRobert Watson     vnet_ng_ether_init, NULL);
824