xref: /freebsd/sys/netgraph/ng_ether.c (revision ec002fee9957c147af90cc5159ef6d2f5538dc73)
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>
57e1e1452dSArchie Cobbs 
58e1e1452dSArchie Cobbs #include <net/if.h>
59810d5e89SGleb Smirnoff #include <net/if_dl.h>
60e1e1452dSArchie Cobbs #include <net/if_types.h>
61e1e1452dSArchie Cobbs #include <net/if_arp.h>
62e1e1452dSArchie Cobbs #include <net/if_var.h>
63e1e1452dSArchie Cobbs #include <net/ethernet.h>
64fd6238a6SAndrew Thompson #include <net/if_bridgevar.h>
65e1e1452dSArchie Cobbs 
66e1e1452dSArchie Cobbs #include <netgraph/ng_message.h>
67e1e1452dSArchie Cobbs #include <netgraph/netgraph.h>
68e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h>
69e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h>
70e1e1452dSArchie Cobbs 
715240dcdbSRuslan Ermilov #define IFP2NG(ifp)  (IFP2AC((ifp))->ac_netgraph)
72e1e1452dSArchie Cobbs 
733c976c3fSPawel Jakub Dawidek /* Per-node private data */
743c976c3fSPawel Jakub Dawidek struct private {
753c976c3fSPawel Jakub Dawidek 	struct ifnet	*ifp;		/* associated interface */
763c976c3fSPawel Jakub Dawidek 	hook_p		upper;		/* upper hook connection */
771a292b80SArchie Cobbs 	hook_p		lower;		/* lower hook connection */
781a292b80SArchie Cobbs 	hook_p		orphan;		/* orphan hook connection */
793c976c3fSPawel Jakub Dawidek 	u_char		autoSrcAddr;	/* always overwrite source address */
803c976c3fSPawel Jakub Dawidek 	u_char		promisc;	/* promiscuous mode enabled */
813c976c3fSPawel Jakub Dawidek 	u_long		hwassist;	/* hardware checksum capabilities */
823c976c3fSPawel Jakub Dawidek 	u_int		flags;		/* flags e.g. really die */
833c976c3fSPawel Jakub Dawidek };
843c976c3fSPawel Jakub Dawidek typedef struct private *priv_p;
85e1e1452dSArchie Cobbs 
86edbb5246SSam Leffler /* Hook pointers used by if_ethersubr.c to callback to netgraph */
87edbb5246SSam Leffler extern	void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
88edbb5246SSam Leffler extern	void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
89edbb5246SSam Leffler extern	int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
90edbb5246SSam Leffler extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
91edbb5246SSam Leffler extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
921c7899c7SGleb Smirnoff extern	void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
93edbb5246SSam Leffler 
94e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */
95edbb5246SSam Leffler static void	ng_ether_input(struct ifnet *ifp, struct mbuf **mp);
96edbb5246SSam Leffler static void	ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m);
97e1e1452dSArchie Cobbs static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
98e1e1452dSArchie Cobbs static void	ng_ether_attach(struct ifnet *ifp);
99e1e1452dSArchie Cobbs static void	ng_ether_detach(struct ifnet *ifp);
1001c7899c7SGleb Smirnoff static void	ng_ether_link_state(struct ifnet *ifp, int state);
101e1e1452dSArchie Cobbs 
102e1e1452dSArchie Cobbs /* Other functions */
1033ca24c28SJulian Elischer static int	ng_ether_rcv_lower(node_p node, struct mbuf *m);
1043ca24c28SJulian Elischer static int	ng_ether_rcv_upper(node_p node, struct mbuf *m);
105e1e1452dSArchie Cobbs 
106e1e1452dSArchie Cobbs /* Netgraph node methods */
107e1e1452dSArchie Cobbs static ng_constructor_t	ng_ether_constructor;
108e1e1452dSArchie Cobbs static ng_rcvmsg_t	ng_ether_rcvmsg;
109069154d5SJulian Elischer static ng_shutdown_t	ng_ether_shutdown;
110e1e1452dSArchie Cobbs static ng_newhook_t	ng_ether_newhook;
111e1e1452dSArchie Cobbs static ng_rcvdata_t	ng_ether_rcvdata;
112e1e1452dSArchie Cobbs static ng_disconnect_t	ng_ether_disconnect;
113e1e1452dSArchie Cobbs static int		ng_ether_mod_event(module_t mod, int event, void *data);
114e1e1452dSArchie Cobbs 
115e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
116e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = {
117e1e1452dSArchie Cobbs 	{
118e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
119e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFNAME,
120e1e1452dSArchie Cobbs 	  "getifname",
121e1e1452dSArchie Cobbs 	  NULL,
122e1e1452dSArchie Cobbs 	  &ng_parse_string_type
123e1e1452dSArchie Cobbs 	},
124e1e1452dSArchie Cobbs 	{
125e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
126e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFINDEX,
127e1e1452dSArchie Cobbs 	  "getifindex",
128e1e1452dSArchie Cobbs 	  NULL,
129e1e1452dSArchie Cobbs 	  &ng_parse_int32_type
130e1e1452dSArchie Cobbs 	},
1314b39c3c7SArchie Cobbs 	{
1324b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
1334b39c3c7SArchie Cobbs 	  NGM_ETHER_GET_ENADDR,
1344b39c3c7SArchie Cobbs 	  "getenaddr",
1354b39c3c7SArchie Cobbs 	  NULL,
1368c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type
1374b39c3c7SArchie Cobbs 	},
1384b39c3c7SArchie Cobbs 	{
1394b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
14056045c66SArchie Cobbs 	  NGM_ETHER_SET_ENADDR,
14156045c66SArchie Cobbs 	  "setenaddr",
1428c7e4101SRuslan Ermilov 	  &ng_parse_enaddr_type,
14356045c66SArchie Cobbs 	  NULL
14456045c66SArchie Cobbs 	},
14556045c66SArchie Cobbs 	{
14656045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
14756045c66SArchie Cobbs 	  NGM_ETHER_GET_PROMISC,
14856045c66SArchie Cobbs 	  "getpromisc",
14956045c66SArchie Cobbs 	  NULL,
15056045c66SArchie Cobbs 	  &ng_parse_int32_type
15156045c66SArchie Cobbs 	},
15256045c66SArchie Cobbs 	{
15356045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1544b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_PROMISC,
1554b39c3c7SArchie Cobbs 	  "setpromisc",
1564b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1574b39c3c7SArchie Cobbs 	  NULL
1584b39c3c7SArchie Cobbs 	},
1594b39c3c7SArchie Cobbs 	{
1604b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
16156045c66SArchie Cobbs 	  NGM_ETHER_GET_AUTOSRC,
16256045c66SArchie Cobbs 	  "getautosrc",
16356045c66SArchie Cobbs 	  NULL,
16456045c66SArchie Cobbs 	  &ng_parse_int32_type
16556045c66SArchie Cobbs 	},
16656045c66SArchie Cobbs 	{
16756045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1684b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_AUTOSRC,
1694b39c3c7SArchie Cobbs 	  "setautosrc",
1704b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1714b39c3c7SArchie Cobbs 	  NULL
1724b39c3c7SArchie Cobbs 	},
173810d5e89SGleb Smirnoff 	{
174810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
175810d5e89SGleb Smirnoff 	  NGM_ETHER_ADD_MULTI,
176810d5e89SGleb Smirnoff 	  "addmulti",
177810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
178810d5e89SGleb Smirnoff 	  NULL
179810d5e89SGleb Smirnoff 	},
180810d5e89SGleb Smirnoff 	{
181810d5e89SGleb Smirnoff 	  NGM_ETHER_COOKIE,
182810d5e89SGleb Smirnoff 	  NGM_ETHER_DEL_MULTI,
183810d5e89SGleb Smirnoff 	  "delmulti",
184810d5e89SGleb Smirnoff 	  &ng_parse_enaddr_type,
185810d5e89SGleb Smirnoff 	  NULL
186810d5e89SGleb Smirnoff 	},
187cefddd66SGleb Smirnoff 	{
188cefddd66SGleb Smirnoff 	  NGM_ETHER_COOKIE,
189cefddd66SGleb Smirnoff 	  NGM_ETHER_DETACH,
190cefddd66SGleb Smirnoff 	  "detach",
191cefddd66SGleb Smirnoff 	  NULL,
192cefddd66SGleb Smirnoff 	  NULL
193cefddd66SGleb Smirnoff 	},
194e1e1452dSArchie Cobbs 	{ 0 }
195e1e1452dSArchie Cobbs };
196e1e1452dSArchie Cobbs 
197e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = {
198f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
199f8aae777SJulian Elischer 	.name =		NG_ETHER_NODE_TYPE,
200f8aae777SJulian Elischer 	.mod_event =	ng_ether_mod_event,
201f8aae777SJulian Elischer 	.constructor =	ng_ether_constructor,
202f8aae777SJulian Elischer 	.rcvmsg =	ng_ether_rcvmsg,
203f8aae777SJulian Elischer 	.shutdown =	ng_ether_shutdown,
204f8aae777SJulian Elischer 	.newhook =	ng_ether_newhook,
205f8aae777SJulian Elischer 	.rcvdata =	ng_ether_rcvdata,
206f8aae777SJulian Elischer 	.disconnect =	ng_ether_disconnect,
207f8aae777SJulian Elischer 	.cmdlist =	ng_ether_cmdlist,
208e1e1452dSArchie Cobbs };
209e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct);
210e1e1452dSArchie Cobbs 
211e1e1452dSArchie Cobbs /******************************************************************
212e1e1452dSArchie Cobbs 		    ETHERNET FUNCTION HOOKS
213e1e1452dSArchie Cobbs ******************************************************************/
214e1e1452dSArchie Cobbs 
215e1e1452dSArchie Cobbs /*
216e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface. We get to
217e1e1452dSArchie Cobbs  * look at it here before any upper layer protocols do.
218e1e1452dSArchie Cobbs  *
219e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
220e1e1452dSArchie Cobbs  */
221e1e1452dSArchie Cobbs static void
222edbb5246SSam Leffler ng_ether_input(struct ifnet *ifp, struct mbuf **mp)
223e1e1452dSArchie Cobbs {
224e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
22530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2261a292b80SArchie Cobbs 	int error;
227e1e1452dSArchie Cobbs 
228e1e1452dSArchie Cobbs 	/* If "lower" hook not connected, let packet continue */
2291a292b80SArchie Cobbs 	if (priv->lower == NULL)
230e1e1452dSArchie Cobbs 		return;
2311a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->lower, *mp);	/* sets *mp = NULL */
232e1e1452dSArchie Cobbs }
233e1e1452dSArchie Cobbs 
234e1e1452dSArchie Cobbs /*
235e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface, and which
236e1e1452dSArchie Cobbs  * does not match any of our known protocols (an ``orphan'').
237e1e1452dSArchie Cobbs  *
238e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
239e1e1452dSArchie Cobbs  */
240e1e1452dSArchie Cobbs static void
241edbb5246SSam Leffler ng_ether_input_orphan(struct ifnet *ifp, struct mbuf *m)
242e1e1452dSArchie Cobbs {
243e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
24430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2451a292b80SArchie Cobbs 	int error;
246e1e1452dSArchie Cobbs 
2471a292b80SArchie Cobbs 	/* If "orphan" hook not connected, discard packet */
2481a292b80SArchie Cobbs 	if (priv->orphan == NULL) {
249e1e1452dSArchie Cobbs 		m_freem(m);
250e1e1452dSArchie Cobbs 		return;
251e1e1452dSArchie Cobbs 	}
2521a292b80SArchie Cobbs 	NG_SEND_DATA_ONLY(error, priv->orphan, m);
253e1e1452dSArchie Cobbs }
254e1e1452dSArchie Cobbs 
255e1e1452dSArchie Cobbs /*
256e1e1452dSArchie Cobbs  * Handle a packet that is going out on an interface.
257e1e1452dSArchie Cobbs  * The Ethernet header is already attached to the mbuf.
258e1e1452dSArchie Cobbs  */
259e1e1452dSArchie Cobbs static int
260e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
261e1e1452dSArchie Cobbs {
262e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
26330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
264e1e1452dSArchie Cobbs 	int error = 0;
265e1e1452dSArchie Cobbs 
266e1e1452dSArchie Cobbs 	/* If "upper" hook not connected, let packet continue */
267e1e1452dSArchie Cobbs 	if (priv->upper == NULL)
268e1e1452dSArchie Cobbs 		return (0);
269e1e1452dSArchie Cobbs 
270e1e1452dSArchie Cobbs 	/* Send it out "upper" hook */
271069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
272e1e1452dSArchie Cobbs 	return (error);
273e1e1452dSArchie Cobbs }
274e1e1452dSArchie Cobbs 
275e1e1452dSArchie Cobbs /*
276e1e1452dSArchie Cobbs  * A new Ethernet interface has been attached.
277e1e1452dSArchie Cobbs  * Create a new node for it, etc.
278e1e1452dSArchie Cobbs  */
279e1e1452dSArchie Cobbs static void
280e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp)
281e1e1452dSArchie Cobbs {
282e1e1452dSArchie Cobbs 	priv_p priv;
283e1e1452dSArchie Cobbs 	node_p node;
284e1e1452dSArchie Cobbs 
285e1e1452dSArchie Cobbs 	/* Create node */
2866e551fb6SDavid E. O'Brien 	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __func__));
287e1e1452dSArchie Cobbs 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
288e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
2899bf40edeSBrooks Davis 		    __func__, "create node", ifp->if_xname);
290e1e1452dSArchie Cobbs 		return;
291e1e1452dSArchie Cobbs 	}
292e1e1452dSArchie Cobbs 
293e1e1452dSArchie Cobbs 	/* Allocate private data */
29499cdf4ccSDavid Malone 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
295e1e1452dSArchie Cobbs 	if (priv == NULL) {
296e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
2979bf40edeSBrooks Davis 		    __func__, "allocate memory", ifp->if_xname);
29830400f03SJulian Elischer 		NG_NODE_UNREF(node);
299e1e1452dSArchie Cobbs 		return;
300e1e1452dSArchie Cobbs 	}
30130400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
302e1e1452dSArchie Cobbs 	priv->ifp = ifp;
3035240dcdbSRuslan Ermilov 	IFP2NG(ifp) = node;
304a3e232d6SArchie Cobbs 	priv->hwassist = ifp->if_hwassist;
305e1e1452dSArchie Cobbs 
306e1e1452dSArchie Cobbs 	/* Try to give the node the same name as the interface */
3079bf40edeSBrooks Davis 	if (ng_name_node(node, ifp->if_xname) != 0) {
308e1e1452dSArchie Cobbs 		log(LOG_WARNING, "%s: can't name node %s\n",
3099bf40edeSBrooks Davis 		    __func__, ifp->if_xname);
310e1e1452dSArchie Cobbs 	}
311e1e1452dSArchie Cobbs }
312e1e1452dSArchie Cobbs 
313e1e1452dSArchie Cobbs /*
314e1e1452dSArchie Cobbs  * An Ethernet interface is being detached.
3151acb27c6SJulian Elischer  * REALLY Destroy its node.
316e1e1452dSArchie Cobbs  */
317e1e1452dSArchie Cobbs static void
318e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp)
319e1e1452dSArchie Cobbs {
320e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
3211acb27c6SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
322e1e1452dSArchie Cobbs 
3231acb27c6SJulian Elischer 	NG_NODE_REALLY_DIE(node);	/* Force real removal of node */
3241acb27c6SJulian Elischer 	/*
3251acb27c6SJulian Elischer 	 * We can't assume the ifnet is still around when we run shutdown
3261acb27c6SJulian Elischer 	 * So zap it now. XXX We HOPE that anything running at this time
3271acb27c6SJulian Elischer 	 * handles it (as it should in the non netgraph case).
3281acb27c6SJulian Elischer 	 */
3295240dcdbSRuslan Ermilov 	IFP2NG(ifp) = NULL;
3301acb27c6SJulian Elischer 	priv->ifp = NULL;	/* XXX race if interrupted an output packet */
3311acb27c6SJulian Elischer 	ng_rmnode_self(node);		/* remove all netgraph parts */
332e1e1452dSArchie Cobbs }
333e1e1452dSArchie Cobbs 
3341c7899c7SGleb Smirnoff /*
3351c7899c7SGleb Smirnoff  * Notify graph about link event.
3361c7899c7SGleb Smirnoff  * if_link_state_change() has already checked that the state has changed.
3371c7899c7SGleb Smirnoff  */
3381c7899c7SGleb Smirnoff static void
3391c7899c7SGleb Smirnoff ng_ether_link_state(struct ifnet *ifp, int state)
3401c7899c7SGleb Smirnoff {
3411c7899c7SGleb Smirnoff 	const node_p node = IFP2NG(ifp);
3421c7899c7SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
3431c7899c7SGleb Smirnoff 	struct ng_mesg *msg;
3441c7899c7SGleb Smirnoff 	int cmd, dummy_error = 0;
3451c7899c7SGleb Smirnoff 
3461c7899c7SGleb Smirnoff 	if (priv->lower == NULL)
3471c7899c7SGleb Smirnoff                 return;
3481c7899c7SGleb Smirnoff 
3491c7899c7SGleb Smirnoff 	if (state == LINK_STATE_UP)
3501c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_UP;
3511c7899c7SGleb Smirnoff 	else if (state == LINK_STATE_DOWN)
3521c7899c7SGleb Smirnoff 		cmd = NGM_LINK_IS_DOWN;
3531c7899c7SGleb Smirnoff 	else
3541c7899c7SGleb Smirnoff 		return;
3551c7899c7SGleb Smirnoff 
3561c7899c7SGleb Smirnoff 	NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
3571c7899c7SGleb Smirnoff 	if (msg != NULL)
3581c7899c7SGleb Smirnoff 		NG_SEND_MSG_HOOK(dummy_error, node, msg, priv->lower, 0);
3591c7899c7SGleb Smirnoff }
3601c7899c7SGleb Smirnoff 
361e1e1452dSArchie Cobbs /******************************************************************
362e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
363e1e1452dSArchie Cobbs ******************************************************************/
364e1e1452dSArchie Cobbs 
365e1e1452dSArchie Cobbs /*
366e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
367e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
368e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
369e1e1452dSArchie Cobbs  */
370e1e1452dSArchie Cobbs static int
371069154d5SJulian Elischer ng_ether_constructor(node_p node)
372e1e1452dSArchie Cobbs {
373e1e1452dSArchie Cobbs 	return (EINVAL);
374e1e1452dSArchie Cobbs }
375e1e1452dSArchie Cobbs 
376e1e1452dSArchie Cobbs /*
377e1e1452dSArchie Cobbs  * Check for attaching a new hook.
378e1e1452dSArchie Cobbs  */
379e1e1452dSArchie Cobbs static	int
380e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
381e1e1452dSArchie Cobbs {
38230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
383e1e1452dSArchie Cobbs 	hook_p *hookptr;
384e1e1452dSArchie Cobbs 
385e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
386e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
387e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
388e1e1452dSArchie Cobbs 
389e1e1452dSArchie Cobbs 	/* Which hook? */
390e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
391e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
3921a292b80SArchie Cobbs 	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0)
393e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
3941a292b80SArchie Cobbs 	else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0)
3951a292b80SArchie Cobbs 		hookptr = &priv->orphan;
3961a292b80SArchie Cobbs 	else
397e1e1452dSArchie Cobbs 		return (EINVAL);
398e1e1452dSArchie Cobbs 
399e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
400e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
401e1e1452dSArchie Cobbs 		return (EISCONN);
402e1e1452dSArchie Cobbs 
403a3e232d6SArchie Cobbs 	/* Disable hardware checksums while 'upper' hook is connected */
404a3e232d6SArchie Cobbs 	if (hookptr == &priv->upper)
405a3e232d6SArchie Cobbs 		priv->ifp->if_hwassist = 0;
406a3e232d6SArchie Cobbs 
407e1e1452dSArchie Cobbs 	/* OK */
408e1e1452dSArchie Cobbs 	*hookptr = hook;
409e1e1452dSArchie Cobbs 	return (0);
410e1e1452dSArchie Cobbs }
411e1e1452dSArchie Cobbs 
412e1e1452dSArchie Cobbs /*
413e1e1452dSArchie Cobbs  * Receive an incoming control message.
414e1e1452dSArchie Cobbs  */
415e1e1452dSArchie Cobbs static int
416069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
417e1e1452dSArchie Cobbs {
41830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
419e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
420e1e1452dSArchie Cobbs 	int error = 0;
421069154d5SJulian Elischer 	struct ng_mesg *msg;
422e1e1452dSArchie Cobbs 
423069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
424e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
425e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
426e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
427e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
428bbb75d78SRuslan Ermilov 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
429e1e1452dSArchie Cobbs 			if (resp == NULL) {
430e1e1452dSArchie Cobbs 				error = ENOMEM;
431e1e1452dSArchie Cobbs 				break;
432e1e1452dSArchie Cobbs 			}
433bbb75d78SRuslan Ermilov 			strlcpy(resp->data, priv->ifp->if_xname, IFNAMSIZ);
434e1e1452dSArchie Cobbs 			break;
435e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
436e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
437e1e1452dSArchie Cobbs 			if (resp == NULL) {
438e1e1452dSArchie Cobbs 				error = ENOMEM;
439e1e1452dSArchie Cobbs 				break;
440e1e1452dSArchie Cobbs 			}
441e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
442e1e1452dSArchie Cobbs 			break;
4434b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
4444b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
4454b39c3c7SArchie Cobbs 			if (resp == NULL) {
4464b39c3c7SArchie Cobbs 				error = ENOMEM;
4474b39c3c7SArchie Cobbs 				break;
4484b39c3c7SArchie Cobbs 			}
4494a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(priv->ifp),
4504b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
4514b39c3c7SArchie Cobbs 			break;
45256045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
45356045c66SArchie Cobbs 		    {
45456045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
45556045c66SArchie Cobbs 				error = EINVAL;
45656045c66SArchie Cobbs 				break;
45756045c66SArchie Cobbs 			}
45856045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
45956045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
46056045c66SArchie Cobbs 			break;
46156045c66SArchie Cobbs 		    }
46256045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
46356045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
46456045c66SArchie Cobbs 			if (resp == NULL) {
46556045c66SArchie Cobbs 				error = ENOMEM;
46656045c66SArchie Cobbs 				break;
46756045c66SArchie Cobbs 			}
46856045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
46956045c66SArchie Cobbs 			break;
4704b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
4714b39c3c7SArchie Cobbs 		    {
4724b39c3c7SArchie Cobbs 			u_char want;
4734b39c3c7SArchie Cobbs 
4744b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
4754b39c3c7SArchie Cobbs 				error = EINVAL;
4764b39c3c7SArchie Cobbs 				break;
4774b39c3c7SArchie Cobbs 			}
4784b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
4794b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
4804b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
4814b39c3c7SArchie Cobbs 					break;
4824b39c3c7SArchie Cobbs 				priv->promisc = want;
4834b39c3c7SArchie Cobbs 			}
4844b39c3c7SArchie Cobbs 			break;
4854b39c3c7SArchie Cobbs 		    }
48656045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
48756045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
48856045c66SArchie Cobbs 			if (resp == NULL) {
48956045c66SArchie Cobbs 				error = ENOMEM;
49056045c66SArchie Cobbs 				break;
49156045c66SArchie Cobbs 			}
49256045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
49356045c66SArchie Cobbs 			break;
4944b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
4954b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
4964b39c3c7SArchie Cobbs 				error = EINVAL;
4974b39c3c7SArchie Cobbs 				break;
4984b39c3c7SArchie Cobbs 			}
4994b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5004b39c3c7SArchie Cobbs 			break;
501810d5e89SGleb Smirnoff 		case NGM_ETHER_ADD_MULTI:
502810d5e89SGleb Smirnoff 		    {
503810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
504ec002feeSBruce M Simpson 			struct ifmultiaddr *ifma;
505810d5e89SGleb Smirnoff 
506810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
507810d5e89SGleb Smirnoff 				error = EINVAL;
508810d5e89SGleb Smirnoff 				break;
509810d5e89SGleb Smirnoff 			}
510ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
511810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
512810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
513ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
514810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
515810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
516ec002feeSBruce M Simpson 			/*
517ec002feeSBruce M Simpson 			 * Netgraph is only permitted to join groups once
518ec002feeSBruce M Simpson 			 * via the if_addmulti() KPI, because it cannot hold
519ec002feeSBruce M Simpson 			 * struct ifmultiaddr * between calls. It may also
520ec002feeSBruce M Simpson 			 * lose a race while we check if the membership
521ec002feeSBruce M Simpson 			 * already exists.
522ec002feeSBruce M Simpson 			 */
523ec002feeSBruce M Simpson 			IF_ADDR_LOCK(priv->ifp);
524ec002feeSBruce M Simpson 			ifma = if_findmulti(priv->ifp,
525ec002feeSBruce M Simpson 			    (struct sockaddr *)&sa_dl);
526ec002feeSBruce M Simpson 			IF_ADDR_UNLOCK(priv->ifp);
527ec002feeSBruce M Simpson 			if (ifma != NULL) {
528ec002feeSBruce M Simpson 				error = EADDRINUSE;
529ec002feeSBruce M Simpson 			} else {
530810d5e89SGleb Smirnoff 				error = if_addmulti(priv->ifp,
531ec002feeSBruce M Simpson 				    (struct sockaddr *)&sa_dl, &ifma);
532ec002feeSBruce M Simpson 			}
533810d5e89SGleb Smirnoff 			break;
534810d5e89SGleb Smirnoff 		    }
535810d5e89SGleb Smirnoff 		case NGM_ETHER_DEL_MULTI:
536810d5e89SGleb Smirnoff 		    {
537810d5e89SGleb Smirnoff 			struct sockaddr_dl sa_dl;
538810d5e89SGleb Smirnoff 
539810d5e89SGleb Smirnoff 			if (msg->header.arglen != ETHER_ADDR_LEN) {
540810d5e89SGleb Smirnoff 				error = EINVAL;
541810d5e89SGleb Smirnoff 				break;
542810d5e89SGleb Smirnoff 			}
543ba20540eSGleb Smirnoff 			bzero(&sa_dl, sizeof(struct sockaddr_dl));
544810d5e89SGleb Smirnoff 			sa_dl.sdl_len = sizeof(struct sockaddr_dl);
545810d5e89SGleb Smirnoff 			sa_dl.sdl_family = AF_LINK;
546ba20540eSGleb Smirnoff 			sa_dl.sdl_alen = ETHER_ADDR_LEN;
547810d5e89SGleb Smirnoff 			bcopy((void *)msg->data, LLADDR(&sa_dl),
548810d5e89SGleb Smirnoff 			    ETHER_ADDR_LEN);
549810d5e89SGleb Smirnoff 			error = if_delmulti(priv->ifp,
550810d5e89SGleb Smirnoff 			    (struct sockaddr *)&sa_dl);
551810d5e89SGleb Smirnoff 			break;
552810d5e89SGleb Smirnoff 		    }
553cefddd66SGleb Smirnoff 		case NGM_ETHER_DETACH:
554cefddd66SGleb Smirnoff 			ng_ether_detach(priv->ifp);
555cefddd66SGleb Smirnoff 			break;
556e1e1452dSArchie Cobbs 		default:
557e1e1452dSArchie Cobbs 			error = EINVAL;
558e1e1452dSArchie Cobbs 			break;
559e1e1452dSArchie Cobbs 		}
560e1e1452dSArchie Cobbs 		break;
561e1e1452dSArchie Cobbs 	default:
562e1e1452dSArchie Cobbs 		error = EINVAL;
563e1e1452dSArchie Cobbs 		break;
564e1e1452dSArchie Cobbs 	}
565069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
566069154d5SJulian Elischer 	NG_FREE_MSG(msg);
567e1e1452dSArchie Cobbs 	return (error);
568e1e1452dSArchie Cobbs }
569e1e1452dSArchie Cobbs 
570e1e1452dSArchie Cobbs /*
571e1e1452dSArchie Cobbs  * Receive data on a hook.
572e1e1452dSArchie Cobbs  */
573e1e1452dSArchie Cobbs static int
574069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
575e1e1452dSArchie Cobbs {
57630400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
57730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
578069154d5SJulian Elischer 	struct mbuf *m;
579e1e1452dSArchie Cobbs 
580069154d5SJulian Elischer 	NGI_GET_M(item, m);
581069154d5SJulian Elischer 	NG_FREE_ITEM(item);
5823ca24c28SJulian Elischer 
5831a292b80SArchie Cobbs 	if (hook == priv->lower || hook == priv->orphan)
5843ca24c28SJulian Elischer 		return ng_ether_rcv_lower(node, m);
585e1e1452dSArchie Cobbs 	if (hook == priv->upper)
5863ca24c28SJulian Elischer 		return ng_ether_rcv_upper(node, m);
5876e551fb6SDavid E. O'Brien 	panic("%s: weird hook", __func__);
5881a292b80SArchie Cobbs #ifdef RESTARTABLE_PANICS /* so we don't get an error msg in LINT */
5897ea5573cSDag-Erling Smørgrav 	return (0);
590b40ce416SJulian Elischer #endif
591e1e1452dSArchie Cobbs }
592e1e1452dSArchie Cobbs 
593e1e1452dSArchie Cobbs /*
5941a292b80SArchie Cobbs  * Handle an mbuf received on the "lower" or "orphan" hook.
595e1e1452dSArchie Cobbs  */
596e1e1452dSArchie Cobbs static int
5973ca24c28SJulian Elischer ng_ether_rcv_lower(node_p node, struct mbuf *m)
598e1e1452dSArchie Cobbs {
59930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
600a1479aa2SArchie Cobbs  	struct ifnet *const ifp = priv->ifp;
601a1479aa2SArchie Cobbs 
602a1479aa2SArchie Cobbs 	/* Check whether interface is ready for packets */
60313f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) &&
60413f4c340SRobert Watson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
605a1479aa2SArchie Cobbs 		NG_FREE_M(m);
606a1479aa2SArchie Cobbs 		return (ENETDOWN);
607a1479aa2SArchie Cobbs 	}
608e1e1452dSArchie Cobbs 
609e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
610e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
611069154d5SJulian Elischer 		NG_FREE_M(m);
612e1e1452dSArchie Cobbs 		return (EINVAL);
613e1e1452dSArchie Cobbs 	}
614e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
6157b9f235fSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
616e1e1452dSArchie Cobbs 		return (ENOBUFS);
617e1e1452dSArchie Cobbs 
6184b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6194b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6207b9f235fSArchie Cobbs 
6217b9f235fSArchie Cobbs 		/* Make the mbuf writable if it's not already */
6227b9f235fSArchie Cobbs 		if (!M_WRITABLE(m)
6237b9f235fSArchie Cobbs 		    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
6247b9f235fSArchie Cobbs 			return (ENOBUFS);
6257b9f235fSArchie Cobbs 
6267b9f235fSArchie Cobbs 		/* Overwrite source MAC address */
6274a0d6638SRuslan Ermilov 		bcopy(IF_LLADDR(ifp),
6284b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
6294b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
6304b39c3c7SArchie Cobbs 	}
631561e4fb9SJulian Elischer 
632e1e1452dSArchie Cobbs 	/* Send it on its way */
633a1479aa2SArchie Cobbs 	return ether_output_frame(ifp, m);
634e1e1452dSArchie Cobbs }
635e1e1452dSArchie Cobbs 
636e1e1452dSArchie Cobbs /*
637e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
638e1e1452dSArchie Cobbs  */
639e1e1452dSArchie Cobbs static int
6403ca24c28SJulian Elischer ng_ether_rcv_upper(node_p node, struct mbuf *m)
641e1e1452dSArchie Cobbs {
64230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6436512768bSGleb Smirnoff 	struct ifnet *ifp = priv->ifp;
644e1e1452dSArchie Cobbs 
645c60c00bcSRuslan Ermilov 	/* Check length and pull off header */
646c60c00bcSRuslan Ermilov 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
647c60c00bcSRuslan Ermilov 		NG_FREE_M(m);
648c60c00bcSRuslan Ermilov 		return (EINVAL);
649c60c00bcSRuslan Ermilov 	}
650c60c00bcSRuslan Ermilov 	if (m->m_len < sizeof(struct ether_header) &&
651c60c00bcSRuslan Ermilov 	    (m = m_pullup(m, sizeof(struct ether_header))) == NULL)
652c60c00bcSRuslan Ermilov 		return (ENOBUFS);
653c60c00bcSRuslan Ermilov 
6546512768bSGleb Smirnoff 	m->m_pkthdr.rcvif = ifp;
655e1e1452dSArchie Cobbs 
656fd6238a6SAndrew Thompson 	/* Pass the packet to the bridge, it may come back to us */
6576512768bSGleb Smirnoff 	if (ifp->if_bridge) {
658fd6238a6SAndrew Thompson 		BRIDGE_INPUT(ifp, m);
6596512768bSGleb Smirnoff 		if (m == NULL)
6606512768bSGleb Smirnoff 			return (0);
6616512768bSGleb Smirnoff 	}
662a176c2aeSGleb Smirnoff 
663e1e1452dSArchie Cobbs 	/* Route packet back in */
664fd6238a6SAndrew Thompson 	ether_demux(ifp, m);
665e1e1452dSArchie Cobbs 	return (0);
666e1e1452dSArchie Cobbs }
667e1e1452dSArchie Cobbs 
668e1e1452dSArchie Cobbs /*
6691acb27c6SJulian Elischer  * Shutdown node. This resets the node but does not remove it
6701acb27c6SJulian Elischer  * unless the REALLY_DIE flag is set.
671e1e1452dSArchie Cobbs  */
672e1e1452dSArchie Cobbs static int
673069154d5SJulian Elischer ng_ether_shutdown(node_p node)
674e1e1452dSArchie Cobbs {
67530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6764b39c3c7SArchie Cobbs 
677be4252b3SJulian Elischer 	if (node->nd_flags & NGF_REALLY_DIE) {
6781acb27c6SJulian Elischer 		/*
6791acb27c6SJulian Elischer 		 * WE came here because the ethernet card is being unloaded,
6801acb27c6SJulian Elischer 		 * so stop being persistant.
6811acb27c6SJulian Elischer 		 * Actually undo all the things we did on creation.
6821acb27c6SJulian Elischer 		 * Assume the ifp has already been freed.
6831acb27c6SJulian Elischer 		 */
6841acb27c6SJulian Elischer 		NG_NODE_SET_PRIVATE(node, NULL);
6851acb27c6SJulian Elischer 		FREE(priv, M_NETGRAPH);
6861acb27c6SJulian Elischer 		NG_NODE_UNREF(node);	/* free node itself */
6871acb27c6SJulian Elischer 		return (0);
688069154d5SJulian Elischer 	}
689018df1c3SBrian Feldman 	if (priv->promisc) {		/* disable promiscuous mode */
690018df1c3SBrian Feldman 		(void)ifpromisc(priv->ifp, 0);
691018df1c3SBrian Feldman 		priv->promisc = 0;
692018df1c3SBrian Feldman 	}
6934b39c3c7SArchie Cobbs 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
694be4252b3SJulian Elischer 	NG_NODE_REVIVE(node);		/* Signal ng_rmnode we are persisant */
695be4252b3SJulian Elischer 
696e1e1452dSArchie Cobbs 	return (0);
697e1e1452dSArchie Cobbs }
698e1e1452dSArchie Cobbs 
699e1e1452dSArchie Cobbs /*
700e1e1452dSArchie Cobbs  * Hook disconnection.
701e1e1452dSArchie Cobbs  */
702e1e1452dSArchie Cobbs static int
703e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
704e1e1452dSArchie Cobbs {
70530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
706e1e1452dSArchie Cobbs 
707a3e232d6SArchie Cobbs 	if (hook == priv->upper) {
708e1e1452dSArchie Cobbs 		priv->upper = NULL;
709b712e9ecSBrian Feldman 		if (priv->ifp != NULL)		/* restore h/w csum */
710b712e9ecSBrian Feldman 			priv->ifp->if_hwassist = priv->hwassist;
7111a292b80SArchie Cobbs 	} else if (hook == priv->lower)
712e1e1452dSArchie Cobbs 		priv->lower = NULL;
7131a292b80SArchie Cobbs 	else if (hook == priv->orphan)
7141a292b80SArchie Cobbs 		priv->orphan = NULL;
7151a292b80SArchie Cobbs 	else
7166e551fb6SDavid E. O'Brien 		panic("%s: weird hook", __func__);
71730400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
71830400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
71930400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
720e1e1452dSArchie Cobbs 	return (0);
721e1e1452dSArchie Cobbs }
722e1e1452dSArchie Cobbs 
723e1e1452dSArchie Cobbs /******************************************************************
724e1e1452dSArchie Cobbs 		    	INITIALIZATION
725e1e1452dSArchie Cobbs ******************************************************************/
726e1e1452dSArchie Cobbs 
727e1e1452dSArchie Cobbs /*
728e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
729e1e1452dSArchie Cobbs  */
730e1e1452dSArchie Cobbs static int
731e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
732e1e1452dSArchie Cobbs {
733e1e1452dSArchie Cobbs 	struct ifnet *ifp;
734e1e1452dSArchie Cobbs 	int error = 0;
735e1e1452dSArchie Cobbs 	int s;
736e1e1452dSArchie Cobbs 
737e1e1452dSArchie Cobbs 	s = splnet();
738e1e1452dSArchie Cobbs 	switch (event) {
739e1e1452dSArchie Cobbs 	case MOD_LOAD:
740e1e1452dSArchie Cobbs 
741e1e1452dSArchie Cobbs 		/* Register function hooks */
742e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
743e1e1452dSArchie Cobbs 			error = EEXIST;
744e1e1452dSArchie Cobbs 			break;
745e1e1452dSArchie Cobbs 		}
746e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
747e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
748e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
749e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
750e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
7511c7899c7SGleb Smirnoff 		ng_ether_link_state_p = ng_ether_link_state;
752e1e1452dSArchie Cobbs 
753e1e1452dSArchie Cobbs 		/* Create nodes for any already-existing Ethernet interfaces */
754b30a244cSJeffrey Hsu 		IFNET_RLOCK();
755e1e1452dSArchie Cobbs 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
756cf2010b8SArchie Cobbs 			if (ifp->if_type == IFT_ETHER
757cf2010b8SArchie Cobbs 			    || ifp->if_type == IFT_L2VLAN)
758e1e1452dSArchie Cobbs 				ng_ether_attach(ifp);
759e1e1452dSArchie Cobbs 		}
760b30a244cSJeffrey Hsu 		IFNET_RUNLOCK();
761e1e1452dSArchie Cobbs 		break;
762e1e1452dSArchie Cobbs 
763e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
764e1e1452dSArchie Cobbs 
765e1e1452dSArchie Cobbs 		/*
766e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
767e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
768e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
769e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
770e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
771e1e1452dSArchie Cobbs 		 */
772e1e1452dSArchie Cobbs 
773e1e1452dSArchie Cobbs 		/* Unregister function hooks */
774e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
775e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
776e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
777e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
778e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
7791c7899c7SGleb Smirnoff 		ng_ether_link_state_p = NULL;
780e1e1452dSArchie Cobbs 		break;
781e1e1452dSArchie Cobbs 
782e1e1452dSArchie Cobbs 	default:
783e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
784e1e1452dSArchie Cobbs 		break;
785e1e1452dSArchie Cobbs 	}
786e1e1452dSArchie Cobbs 	splx(s);
787e1e1452dSArchie Cobbs 	return (error);
788e1e1452dSArchie Cobbs }
789e1e1452dSArchie Cobbs 
790