xref: /freebsd/sys/netgraph/ng_ether.c (revision 069154d55f4e6cb7243ee37c5aa0391aa639e721)
1e1e1452dSArchie Cobbs 
2e1e1452dSArchie Cobbs /*
3e1e1452dSArchie Cobbs  * ng_ether.c
4e1e1452dSArchie Cobbs  *
5e1e1452dSArchie Cobbs  * Copyright (c) 1996-2000 Whistle Communications, Inc.
6e1e1452dSArchie Cobbs  * All rights reserved.
7e1e1452dSArchie Cobbs  *
8e1e1452dSArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9e1e1452dSArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10e1e1452dSArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
11e1e1452dSArchie Cobbs  * provided, however, that:
12e1e1452dSArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13e1e1452dSArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
14e1e1452dSArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
15e1e1452dSArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
16e1e1452dSArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17e1e1452dSArchie Cobbs  *    such appears in the above copyright notice or in the software.
18e1e1452dSArchie Cobbs  *
19e1e1452dSArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20e1e1452dSArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21e1e1452dSArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22e1e1452dSArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23e1e1452dSArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24e1e1452dSArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25e1e1452dSArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26e1e1452dSArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27e1e1452dSArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28e1e1452dSArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29e1e1452dSArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30e1e1452dSArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31e1e1452dSArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32e1e1452dSArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33e1e1452dSArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34e1e1452dSArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35e1e1452dSArchie Cobbs  * OF SUCH DAMAGE.
36e1e1452dSArchie Cobbs  *
37e1e1452dSArchie Cobbs  * Authors: Archie Cobbs <archie@freebsd.org>
38e1e1452dSArchie Cobbs  *	    Julian Elischer <julian@freebsd.org>
39e1e1452dSArchie Cobbs  *
40e1e1452dSArchie Cobbs  * $FreeBSD$
41e1e1452dSArchie Cobbs  */
42e1e1452dSArchie Cobbs 
43e1e1452dSArchie Cobbs /*
44e1e1452dSArchie Cobbs  * ng_ether(4) netgraph node type
45e1e1452dSArchie Cobbs  */
46e1e1452dSArchie Cobbs 
47e1e1452dSArchie Cobbs #include <sys/param.h>
48e1e1452dSArchie Cobbs #include <sys/systm.h>
49e1e1452dSArchie Cobbs #include <sys/kernel.h>
50e1e1452dSArchie Cobbs #include <sys/malloc.h>
51e1e1452dSArchie Cobbs #include <sys/mbuf.h>
52e1e1452dSArchie Cobbs #include <sys/errno.h>
53e1e1452dSArchie Cobbs #include <sys/syslog.h>
54e1e1452dSArchie Cobbs #include <sys/socket.h>
55e1e1452dSArchie Cobbs 
56e1e1452dSArchie Cobbs #include <net/if.h>
57e1e1452dSArchie Cobbs #include <net/if_types.h>
58e1e1452dSArchie Cobbs #include <net/if_arp.h>
59e1e1452dSArchie Cobbs #include <net/if_var.h>
60e1e1452dSArchie Cobbs #include <net/ethernet.h>
61e1e1452dSArchie Cobbs 
62e1e1452dSArchie Cobbs #include <netgraph/ng_message.h>
63e1e1452dSArchie Cobbs #include <netgraph/netgraph.h>
64e1e1452dSArchie Cobbs #include <netgraph/ng_parse.h>
65e1e1452dSArchie Cobbs #include <netgraph/ng_ether.h>
66e1e1452dSArchie Cobbs 
67561e4fb9SJulian Elischer #define IFP2AC(IFP)  ((struct arpcom *)IFP)
68e1e1452dSArchie Cobbs #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
69e1e1452dSArchie Cobbs 
70e1e1452dSArchie Cobbs /* Per-node private data */
71e1e1452dSArchie Cobbs struct private {
72e1e1452dSArchie Cobbs 	struct ifnet	*ifp;		/* associated interface */
73e1e1452dSArchie Cobbs 	hook_p		upper;		/* upper hook connection */
74e1e1452dSArchie Cobbs 	hook_p		lower;		/* lower OR orphan hook connection */
75e1e1452dSArchie Cobbs 	u_char		lowerOrphan;	/* whether lower is lower or orphan */
764b39c3c7SArchie Cobbs 	u_char		autoSrcAddr;	/* always overwrite source address */
774b39c3c7SArchie Cobbs 	u_char		promisc;	/* promiscuous mode enabled */
78e1e1452dSArchie Cobbs };
79e1e1452dSArchie Cobbs typedef struct private *priv_p;
80e1e1452dSArchie Cobbs 
81e1e1452dSArchie Cobbs /* Functional hooks called from if_ethersubr.c */
82e1e1452dSArchie Cobbs static void	ng_ether_input(struct ifnet *ifp,
83e1e1452dSArchie Cobbs 		    struct mbuf **mp, struct ether_header *eh);
84e1e1452dSArchie Cobbs static void	ng_ether_input_orphan(struct ifnet *ifp,
85e1e1452dSArchie Cobbs 		    struct mbuf *m, struct ether_header *eh);
86e1e1452dSArchie Cobbs static int	ng_ether_output(struct ifnet *ifp, struct mbuf **mp);
87e1e1452dSArchie Cobbs static void	ng_ether_attach(struct ifnet *ifp);
88e1e1452dSArchie Cobbs static void	ng_ether_detach(struct ifnet *ifp);
89e1e1452dSArchie Cobbs 
90e1e1452dSArchie Cobbs /* Other functions */
91e1e1452dSArchie Cobbs static void	ng_ether_input2(node_p node,
92e1e1452dSArchie Cobbs 		    struct mbuf **mp, struct ether_header *eh);
93e1e1452dSArchie Cobbs static int	ng_ether_glueback_header(struct mbuf **mp,
94e1e1452dSArchie Cobbs 			struct ether_header *eh);
95e1e1452dSArchie Cobbs static int	ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta);
96e1e1452dSArchie Cobbs static int	ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta);
97e1e1452dSArchie Cobbs 
98e1e1452dSArchie Cobbs /* Netgraph node methods */
99e1e1452dSArchie Cobbs static ng_constructor_t	ng_ether_constructor;
100e1e1452dSArchie Cobbs static ng_rcvmsg_t	ng_ether_rcvmsg;
101069154d5SJulian Elischer static ng_shutdown_t	ng_ether_shutdown;
102e1e1452dSArchie Cobbs static ng_newhook_t	ng_ether_newhook;
103859a4d16SJulian Elischer static ng_connect_t	ng_ether_connect;
104e1e1452dSArchie Cobbs static ng_rcvdata_t	ng_ether_rcvdata;
105e1e1452dSArchie Cobbs static ng_disconnect_t	ng_ether_disconnect;
106e1e1452dSArchie Cobbs static int		ng_ether_mod_event(module_t mod, int event, void *data);
107e1e1452dSArchie Cobbs 
10856045c66SArchie Cobbs /* Parse type for an Ethernet address */
10956045c66SArchie Cobbs static ng_parse_t	ng_enaddr_parse;
11056045c66SArchie Cobbs static ng_unparse_t	ng_enaddr_unparse;
11156045c66SArchie Cobbs const struct ng_parse_type ng_ether_enaddr_type = {
11256045c66SArchie Cobbs 	NULL,
11356045c66SArchie Cobbs 	NULL,
11456045c66SArchie Cobbs 	NULL,
11556045c66SArchie Cobbs 	ng_enaddr_parse,
11656045c66SArchie Cobbs 	ng_enaddr_unparse,
11756045c66SArchie Cobbs 	NULL,			/* no such thing as a "default" EN address */
11856045c66SArchie Cobbs 	0
1194b39c3c7SArchie Cobbs };
1204b39c3c7SArchie Cobbs 
121e1e1452dSArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
122e1e1452dSArchie Cobbs static const struct ng_cmdlist ng_ether_cmdlist[] = {
123e1e1452dSArchie Cobbs 	{
124e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
125e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFNAME,
126e1e1452dSArchie Cobbs 	  "getifname",
127e1e1452dSArchie Cobbs 	  NULL,
128e1e1452dSArchie Cobbs 	  &ng_parse_string_type
129e1e1452dSArchie Cobbs 	},
130e1e1452dSArchie Cobbs 	{
131e1e1452dSArchie Cobbs 	  NGM_ETHER_COOKIE,
132e1e1452dSArchie Cobbs 	  NGM_ETHER_GET_IFINDEX,
133e1e1452dSArchie Cobbs 	  "getifindex",
134e1e1452dSArchie Cobbs 	  NULL,
135e1e1452dSArchie Cobbs 	  &ng_parse_int32_type
136e1e1452dSArchie Cobbs 	},
1374b39c3c7SArchie Cobbs 	{
1384b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
1394b39c3c7SArchie Cobbs 	  NGM_ETHER_GET_ENADDR,
1404b39c3c7SArchie Cobbs 	  "getenaddr",
1414b39c3c7SArchie Cobbs 	  NULL,
1424b39c3c7SArchie Cobbs 	  &ng_ether_enaddr_type
1434b39c3c7SArchie Cobbs 	},
1444b39c3c7SArchie Cobbs 	{
1454b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
14656045c66SArchie Cobbs 	  NGM_ETHER_SET_ENADDR,
14756045c66SArchie Cobbs 	  "setenaddr",
14856045c66SArchie Cobbs 	  &ng_ether_enaddr_type,
14956045c66SArchie Cobbs 	  NULL
15056045c66SArchie Cobbs 	},
15156045c66SArchie Cobbs 	{
15256045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
15356045c66SArchie Cobbs 	  NGM_ETHER_GET_PROMISC,
15456045c66SArchie Cobbs 	  "getpromisc",
15556045c66SArchie Cobbs 	  NULL,
15656045c66SArchie Cobbs 	  &ng_parse_int32_type
15756045c66SArchie Cobbs 	},
15856045c66SArchie Cobbs 	{
15956045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1604b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_PROMISC,
1614b39c3c7SArchie Cobbs 	  "setpromisc",
1624b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1634b39c3c7SArchie Cobbs 	  NULL
1644b39c3c7SArchie Cobbs 	},
1654b39c3c7SArchie Cobbs 	{
1664b39c3c7SArchie Cobbs 	  NGM_ETHER_COOKIE,
16756045c66SArchie Cobbs 	  NGM_ETHER_GET_AUTOSRC,
16856045c66SArchie Cobbs 	  "getautosrc",
16956045c66SArchie Cobbs 	  NULL,
17056045c66SArchie Cobbs 	  &ng_parse_int32_type
17156045c66SArchie Cobbs 	},
17256045c66SArchie Cobbs 	{
17356045c66SArchie Cobbs 	  NGM_ETHER_COOKIE,
1744b39c3c7SArchie Cobbs 	  NGM_ETHER_SET_AUTOSRC,
1754b39c3c7SArchie Cobbs 	  "setautosrc",
1764b39c3c7SArchie Cobbs 	  &ng_parse_int32_type,
1774b39c3c7SArchie Cobbs 	  NULL
1784b39c3c7SArchie Cobbs 	},
179e1e1452dSArchie Cobbs 	{ 0 }
180e1e1452dSArchie Cobbs };
181e1e1452dSArchie Cobbs 
182e1e1452dSArchie Cobbs static struct ng_type ng_ether_typestruct = {
183589f6ed8SJulian Elischer 	NG_ABI_VERSION,
184e1e1452dSArchie Cobbs 	NG_ETHER_NODE_TYPE,
185e1e1452dSArchie Cobbs 	ng_ether_mod_event,
186e1e1452dSArchie Cobbs 	ng_ether_constructor,
187e1e1452dSArchie Cobbs 	ng_ether_rcvmsg,
188069154d5SJulian Elischer 	ng_ether_shutdown,
189e1e1452dSArchie Cobbs 	ng_ether_newhook,
190e1e1452dSArchie Cobbs 	NULL,
191859a4d16SJulian Elischer 	ng_ether_connect,
192e1e1452dSArchie Cobbs 	ng_ether_rcvdata,
193e1e1452dSArchie Cobbs 	ng_ether_disconnect,
194e1e1452dSArchie Cobbs 	ng_ether_cmdlist,
195e1e1452dSArchie Cobbs };
196e1e1452dSArchie Cobbs NETGRAPH_INIT(ether, &ng_ether_typestruct);
197e1e1452dSArchie Cobbs 
198e1e1452dSArchie Cobbs /******************************************************************
199e1e1452dSArchie Cobbs 		    ETHERNET FUNCTION HOOKS
200e1e1452dSArchie Cobbs ******************************************************************/
201e1e1452dSArchie Cobbs 
202e1e1452dSArchie Cobbs /*
203e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface. We get to
204e1e1452dSArchie Cobbs  * look at it here before any upper layer protocols do.
205e1e1452dSArchie Cobbs  *
206e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
207e1e1452dSArchie Cobbs  */
208e1e1452dSArchie Cobbs static void
209e1e1452dSArchie Cobbs ng_ether_input(struct ifnet *ifp,
210e1e1452dSArchie Cobbs 	struct mbuf **mp, struct ether_header *eh)
211e1e1452dSArchie Cobbs {
212e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
213e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
214e1e1452dSArchie Cobbs 
215e1e1452dSArchie Cobbs 	/* If "lower" hook not connected, let packet continue */
216e1e1452dSArchie Cobbs 	if (priv->lower == NULL || priv->lowerOrphan)
217e1e1452dSArchie Cobbs 		return;
218e1e1452dSArchie Cobbs 	ng_ether_input2(node, mp, eh);
219e1e1452dSArchie Cobbs }
220e1e1452dSArchie Cobbs 
221e1e1452dSArchie Cobbs /*
222e1e1452dSArchie Cobbs  * Handle a packet that has come in on an interface, and which
223e1e1452dSArchie Cobbs  * does not match any of our known protocols (an ``orphan'').
224e1e1452dSArchie Cobbs  *
225e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
226e1e1452dSArchie Cobbs  */
227e1e1452dSArchie Cobbs static void
228e1e1452dSArchie Cobbs ng_ether_input_orphan(struct ifnet *ifp,
229e1e1452dSArchie Cobbs 	struct mbuf *m, struct ether_header *eh)
230e1e1452dSArchie Cobbs {
231e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
232e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
233e1e1452dSArchie Cobbs 
234e1e1452dSArchie Cobbs 	/* If "orphan" hook not connected, let packet continue */
235e1e1452dSArchie Cobbs 	if (priv->lower == NULL || !priv->lowerOrphan) {
236e1e1452dSArchie Cobbs 		m_freem(m);
237e1e1452dSArchie Cobbs 		return;
238e1e1452dSArchie Cobbs 	}
239e1e1452dSArchie Cobbs 	ng_ether_input2(node, &m, eh);
240e1e1452dSArchie Cobbs 	if (m != NULL)
241e1e1452dSArchie Cobbs 		m_freem(m);
242e1e1452dSArchie Cobbs }
243e1e1452dSArchie Cobbs 
244e1e1452dSArchie Cobbs /*
245859a4d16SJulian Elischer  * Handle a packet that has come in on an ethernet interface.
246e1e1452dSArchie Cobbs  * The Ethernet header has already been detached from the mbuf,
247e1e1452dSArchie Cobbs  * so we have to put it back.
248e1e1452dSArchie Cobbs  *
249e1e1452dSArchie Cobbs  * NOTE: this function will get called at splimp()
250e1e1452dSArchie Cobbs  */
251e1e1452dSArchie Cobbs static void
252e1e1452dSArchie Cobbs ng_ether_input2(node_p node, struct mbuf **mp, struct ether_header *eh)
253e1e1452dSArchie Cobbs {
254e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
255e1e1452dSArchie Cobbs 	int error;
256e1e1452dSArchie Cobbs 
257e1e1452dSArchie Cobbs 	/* Glue Ethernet header back on */
258e1e1452dSArchie Cobbs 	if ((error = ng_ether_glueback_header(mp, eh)) != 0)
259e1e1452dSArchie Cobbs 		return;
260e1e1452dSArchie Cobbs 
261e1e1452dSArchie Cobbs 	/* Send out lower/orphan hook */
262859a4d16SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->lower, *mp);
263e1e1452dSArchie Cobbs 	*mp = NULL;
264e1e1452dSArchie Cobbs }
265e1e1452dSArchie Cobbs 
266e1e1452dSArchie Cobbs /*
267e1e1452dSArchie Cobbs  * Handle a packet that is going out on an interface.
268e1e1452dSArchie Cobbs  * The Ethernet header is already attached to the mbuf.
269e1e1452dSArchie Cobbs  */
270e1e1452dSArchie Cobbs static int
271e1e1452dSArchie Cobbs ng_ether_output(struct ifnet *ifp, struct mbuf **mp)
272e1e1452dSArchie Cobbs {
273e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
274e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
275e1e1452dSArchie Cobbs 	int error = 0;
276e1e1452dSArchie Cobbs 
277e1e1452dSArchie Cobbs 	/* If "upper" hook not connected, let packet continue */
278e1e1452dSArchie Cobbs 	if (priv->upper == NULL)
279e1e1452dSArchie Cobbs 		return (0);
280e1e1452dSArchie Cobbs 
281e1e1452dSArchie Cobbs 	/* Send it out "upper" hook */
282069154d5SJulian Elischer 	NG_SEND_DATA_ONLY(error, priv->upper, *mp);
283e1e1452dSArchie Cobbs 	return (error);
284e1e1452dSArchie Cobbs }
285e1e1452dSArchie Cobbs 
286e1e1452dSArchie Cobbs /*
287e1e1452dSArchie Cobbs  * A new Ethernet interface has been attached.
288e1e1452dSArchie Cobbs  * Create a new node for it, etc.
289e1e1452dSArchie Cobbs  */
290e1e1452dSArchie Cobbs static void
291e1e1452dSArchie Cobbs ng_ether_attach(struct ifnet *ifp)
292e1e1452dSArchie Cobbs {
293e1e1452dSArchie Cobbs 	char name[IFNAMSIZ + 1];
294e1e1452dSArchie Cobbs 	priv_p priv;
295e1e1452dSArchie Cobbs 	node_p node;
296e1e1452dSArchie Cobbs 
297e1e1452dSArchie Cobbs 	/* Create node */
298e1e1452dSArchie Cobbs 	KASSERT(!IFP2NG(ifp), ("%s: node already exists?", __FUNCTION__));
299e1e1452dSArchie Cobbs 	snprintf(name, sizeof(name), "%s%d", ifp->if_name, ifp->if_unit);
300e1e1452dSArchie Cobbs 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
301e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
302e1e1452dSArchie Cobbs 		    __FUNCTION__, "create node", name);
303e1e1452dSArchie Cobbs 		return;
304e1e1452dSArchie Cobbs 	}
305e1e1452dSArchie Cobbs 
306e1e1452dSArchie Cobbs 	/* Allocate private data */
30799cdf4ccSDavid Malone 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
308e1e1452dSArchie Cobbs 	if (priv == NULL) {
309e1e1452dSArchie Cobbs 		log(LOG_ERR, "%s: can't %s for %s\n",
310e1e1452dSArchie Cobbs 		    __FUNCTION__, "allocate memory", name);
311e1e1452dSArchie Cobbs 		ng_unref(node);
312e1e1452dSArchie Cobbs 		return;
313e1e1452dSArchie Cobbs 	}
314e1e1452dSArchie Cobbs 	node->private = priv;
315e1e1452dSArchie Cobbs 	priv->ifp = ifp;
316e1e1452dSArchie Cobbs 	IFP2NG(ifp) = node;
3174b39c3c7SArchie Cobbs 	priv->autoSrcAddr = 1;
318e1e1452dSArchie Cobbs 
319e1e1452dSArchie Cobbs 	/* Try to give the node the same name as the interface */
320e1e1452dSArchie Cobbs 	if (ng_name_node(node, name) != 0) {
321e1e1452dSArchie Cobbs 		log(LOG_WARNING, "%s: can't name node %s\n",
322e1e1452dSArchie Cobbs 		    __FUNCTION__, name);
323e1e1452dSArchie Cobbs 	}
324e1e1452dSArchie Cobbs }
325e1e1452dSArchie Cobbs 
326e1e1452dSArchie Cobbs /*
327e1e1452dSArchie Cobbs  * An Ethernet interface is being detached.
328e1e1452dSArchie Cobbs  * Destroy its node.
329e1e1452dSArchie Cobbs  */
330e1e1452dSArchie Cobbs static void
331e1e1452dSArchie Cobbs ng_ether_detach(struct ifnet *ifp)
332e1e1452dSArchie Cobbs {
333e1e1452dSArchie Cobbs 	const node_p node = IFP2NG(ifp);
334e1e1452dSArchie Cobbs 	priv_p priv;
335e1e1452dSArchie Cobbs 
336e1e1452dSArchie Cobbs 	if (node == NULL)		/* no node (why not?), ignore */
337e1e1452dSArchie Cobbs 		return;
338069154d5SJulian Elischer 	ng_rmnode_self(node);		/* break all links to other nodes */
339840e9778SArchie Cobbs 	node->flags |= NG_INVALID;
340e1e1452dSArchie Cobbs 	IFP2NG(ifp) = NULL;		/* detach node from interface */
341e1e1452dSArchie Cobbs 	priv = node->private;		/* free node private info */
342e1e1452dSArchie Cobbs 	bzero(priv, sizeof(*priv));
343e1e1452dSArchie Cobbs 	FREE(priv, M_NETGRAPH);
344e1e1452dSArchie Cobbs 	node->private = NULL;
345e1e1452dSArchie Cobbs 	ng_unref(node);			/* free node itself */
346e1e1452dSArchie Cobbs }
347e1e1452dSArchie Cobbs 
348e1e1452dSArchie Cobbs /*
349e1e1452dSArchie Cobbs  * Optimization for gluing the Ethernet header back onto
350e1e1452dSArchie Cobbs  * the front of an incoming packet.
351e1e1452dSArchie Cobbs  */
352e1e1452dSArchie Cobbs static int
353e1e1452dSArchie Cobbs ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
354e1e1452dSArchie Cobbs {
355e1e1452dSArchie Cobbs 	struct mbuf *m = *mp;
356e1e1452dSArchie Cobbs 	uintfptr_t room;
357e1e1452dSArchie Cobbs 	int error = 0;
358e1e1452dSArchie Cobbs 
359e1e1452dSArchie Cobbs 	/*
360e1e1452dSArchie Cobbs 	 * Possibly the header is already on the front.
361e1e1452dSArchie Cobbs 	 * If this is the case so just move the markers back
362e1e1452dSArchie Cobbs 	 * to re-include it. We lucked out.
363e1e1452dSArchie Cobbs 	 * This allows us to avoid a yucky m_pullup
364e1e1452dSArchie Cobbs 	 * in later nodes if it works.
365e1e1452dSArchie Cobbs 	 */
366e1e1452dSArchie Cobbs 	if (eh == mtod(m, struct ether_header *) - 1) {
367e1e1452dSArchie Cobbs 		m->m_len += sizeof(*eh);
368e1e1452dSArchie Cobbs 		m->m_data -= sizeof(*eh);
369e1e1452dSArchie Cobbs 		m->m_pkthdr.len += sizeof(*eh);
370e1e1452dSArchie Cobbs 		goto done;
371e1e1452dSArchie Cobbs 	}
372e1e1452dSArchie Cobbs 
373e1e1452dSArchie Cobbs 	/*
374e1e1452dSArchie Cobbs 	 * Alternatively there may be room even though
375e1e1452dSArchie Cobbs 	 * it is stored somewhere else. If so, copy it in.
376e1e1452dSArchie Cobbs 	 * This only safe because we KNOW that this packet has
377e1e1452dSArchie Cobbs 	 * just been generated by an ethernet card, so there are
378e1e1452dSArchie Cobbs 	 * no aliases to the buffer (not so for outgoing packets).
379e1e1452dSArchie Cobbs 	 * Nearly all ethernet cards will end up producing mbufs
380e1e1452dSArchie Cobbs 	 * that fall into these cases. So we are not optimizing
381e1e1452dSArchie Cobbs 	 * contorted cases.
382e1e1452dSArchie Cobbs 	 */
383e1e1452dSArchie Cobbs 	if ((m->m_flags & M_EXT) != 0) {
384e1e1452dSArchie Cobbs 		room = mtod(m, caddr_t) - m->m_ext.ext_buf;
385e1e1452dSArchie Cobbs 		if (room > m->m_ext.ext_size)	/* garbage, fail immediately */
386e1e1452dSArchie Cobbs 			room = 0;
387e1e1452dSArchie Cobbs 	} else
388e1e1452dSArchie Cobbs 		room = mtod(m, caddr_t) - m->m_pktdat;
389e1e1452dSArchie Cobbs 
390e1e1452dSArchie Cobbs 	/*
391e1e1452dSArchie Cobbs 	 * If we have room, just copy it and adjust
392e1e1452dSArchie Cobbs 	 */
393e1e1452dSArchie Cobbs 	if (room >= sizeof(*eh)) {
394e1e1452dSArchie Cobbs 		m->m_len += sizeof(*eh);
395e1e1452dSArchie Cobbs 		m->m_data -= sizeof(*eh);
396e1e1452dSArchie Cobbs 		m->m_pkthdr.len += sizeof(*eh);
397e1e1452dSArchie Cobbs 		goto copy;
398e1e1452dSArchie Cobbs 	}
399e1e1452dSArchie Cobbs 
400e1e1452dSArchie Cobbs 	/*
401e1e1452dSArchie Cobbs 	 * Doing anything more is likely to get more
402e1e1452dSArchie Cobbs 	 * expensive than it's worth..
403e1e1452dSArchie Cobbs 	 * it's probable that everything else is in one
404e1e1452dSArchie Cobbs 	 * big lump. The next node will do an m_pullup()
405e1e1452dSArchie Cobbs 	 * for exactly the amount of data it needs and
406e1e1452dSArchie Cobbs 	 * hopefully everything after that will not
407e1e1452dSArchie Cobbs 	 * need one. So let's just use M_PREPEND.
408e1e1452dSArchie Cobbs 	 */
409e1e1452dSArchie Cobbs 	M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
410e1e1452dSArchie Cobbs 	if (m == NULL) {
411e1e1452dSArchie Cobbs 		error = ENOBUFS;
412e1e1452dSArchie Cobbs 		goto done;
413e1e1452dSArchie Cobbs 	}
414e1e1452dSArchie Cobbs 
415e1e1452dSArchie Cobbs copy:
416e1e1452dSArchie Cobbs 	/* Copy header and return (possibly new) mbuf */
417e1e1452dSArchie Cobbs 	bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
418e1e1452dSArchie Cobbs done:
419e1e1452dSArchie Cobbs 	*mp = m;
420e1e1452dSArchie Cobbs 	return error;
421e1e1452dSArchie Cobbs }
422e1e1452dSArchie Cobbs 
423e1e1452dSArchie Cobbs /******************************************************************
424e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
425e1e1452dSArchie Cobbs ******************************************************************/
426e1e1452dSArchie Cobbs 
427e1e1452dSArchie Cobbs /*
428e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
429e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
430e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
431e1e1452dSArchie Cobbs  */
432e1e1452dSArchie Cobbs static int
433069154d5SJulian Elischer ng_ether_constructor(node_p node)
434e1e1452dSArchie Cobbs {
435e1e1452dSArchie Cobbs 	return (EINVAL);
436e1e1452dSArchie Cobbs }
437e1e1452dSArchie Cobbs 
438e1e1452dSArchie Cobbs /*
439e1e1452dSArchie Cobbs  * Check for attaching a new hook.
440e1e1452dSArchie Cobbs  */
441e1e1452dSArchie Cobbs static	int
442e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
443e1e1452dSArchie Cobbs {
444e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
445e1e1452dSArchie Cobbs 	u_char orphan = priv->lowerOrphan;
446e1e1452dSArchie Cobbs 	hook_p *hookptr;
447e1e1452dSArchie Cobbs 
448e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
449e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
450e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
451e1e1452dSArchie Cobbs 
452e1e1452dSArchie Cobbs 	/* Which hook? */
453e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
454e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
455e1e1452dSArchie Cobbs 	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
456e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
457e1e1452dSArchie Cobbs 		orphan = 0;
458e1e1452dSArchie Cobbs 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
459e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
460e1e1452dSArchie Cobbs 		orphan = 1;
461e1e1452dSArchie Cobbs 	} else
462e1e1452dSArchie Cobbs 		return (EINVAL);
463e1e1452dSArchie Cobbs 
464e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
465e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
466e1e1452dSArchie Cobbs 		return (EISCONN);
467e1e1452dSArchie Cobbs 
468e1e1452dSArchie Cobbs 	/* OK */
469e1e1452dSArchie Cobbs 	*hookptr = hook;
470e1e1452dSArchie Cobbs 	priv->lowerOrphan = orphan;
471e1e1452dSArchie Cobbs 	return (0);
472e1e1452dSArchie Cobbs }
473e1e1452dSArchie Cobbs 
474e1e1452dSArchie Cobbs /*
475859a4d16SJulian Elischer  * Hooks are attached, adjust to force queueing.
476859a4d16SJulian Elischer  * We don't really care which hook it is.
477859a4d16SJulian Elischer  * they should all be queuing for outgoing data.
478859a4d16SJulian Elischer  */
479859a4d16SJulian Elischer static	int
480859a4d16SJulian Elischer ng_ether_connect(hook_p hook)
481859a4d16SJulian Elischer {
482859a4d16SJulian Elischer 	hook->peer->flags |= HK_QUEUE;
483859a4d16SJulian Elischer 	return (0);
484859a4d16SJulian Elischer }
485859a4d16SJulian Elischer 
486859a4d16SJulian Elischer /*
487e1e1452dSArchie Cobbs  * Receive an incoming control message.
488e1e1452dSArchie Cobbs  */
489e1e1452dSArchie Cobbs static int
490069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
491e1e1452dSArchie Cobbs {
492e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
493e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
494e1e1452dSArchie Cobbs 	int error = 0;
495069154d5SJulian Elischer 	struct ng_mesg *msg;
496e1e1452dSArchie Cobbs 
497069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
498e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
499e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
500e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
501e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
502e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
503e1e1452dSArchie Cobbs 			if (resp == NULL) {
504e1e1452dSArchie Cobbs 				error = ENOMEM;
505e1e1452dSArchie Cobbs 				break;
506e1e1452dSArchie Cobbs 			}
507e1e1452dSArchie Cobbs 			snprintf(resp->data, IFNAMSIZ + 1,
508e1e1452dSArchie Cobbs 			    "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
509e1e1452dSArchie Cobbs 			break;
510e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
511e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
512e1e1452dSArchie Cobbs 			if (resp == NULL) {
513e1e1452dSArchie Cobbs 				error = ENOMEM;
514e1e1452dSArchie Cobbs 				break;
515e1e1452dSArchie Cobbs 			}
516e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
517e1e1452dSArchie Cobbs 			break;
5184b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
5194b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
5204b39c3c7SArchie Cobbs 			if (resp == NULL) {
5214b39c3c7SArchie Cobbs 				error = ENOMEM;
5224b39c3c7SArchie Cobbs 				break;
5234b39c3c7SArchie Cobbs 			}
5244b39c3c7SArchie Cobbs 			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
5254b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
5264b39c3c7SArchie Cobbs 			break;
52756045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
52856045c66SArchie Cobbs 		    {
52956045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
53056045c66SArchie Cobbs 				error = EINVAL;
53156045c66SArchie Cobbs 				break;
53256045c66SArchie Cobbs 			}
53356045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
53456045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
53556045c66SArchie Cobbs 			break;
53656045c66SArchie Cobbs 		    }
53756045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
53856045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
53956045c66SArchie Cobbs 			if (resp == NULL) {
54056045c66SArchie Cobbs 				error = ENOMEM;
54156045c66SArchie Cobbs 				break;
54256045c66SArchie Cobbs 			}
54356045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
54456045c66SArchie Cobbs 			break;
5454b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
5464b39c3c7SArchie Cobbs 		    {
5474b39c3c7SArchie Cobbs 			u_char want;
5484b39c3c7SArchie Cobbs 
5494b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5504b39c3c7SArchie Cobbs 				error = EINVAL;
5514b39c3c7SArchie Cobbs 				break;
5524b39c3c7SArchie Cobbs 			}
5534b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
5544b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
5554b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
5564b39c3c7SArchie Cobbs 					break;
5574b39c3c7SArchie Cobbs 				priv->promisc = want;
5584b39c3c7SArchie Cobbs 			}
5594b39c3c7SArchie Cobbs 			break;
5604b39c3c7SArchie Cobbs 		    }
56156045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
56256045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
56356045c66SArchie Cobbs 			if (resp == NULL) {
56456045c66SArchie Cobbs 				error = ENOMEM;
56556045c66SArchie Cobbs 				break;
56656045c66SArchie Cobbs 			}
56756045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
56856045c66SArchie Cobbs 			break;
5694b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
5704b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5714b39c3c7SArchie Cobbs 				error = EINVAL;
5724b39c3c7SArchie Cobbs 				break;
5734b39c3c7SArchie Cobbs 			}
5744b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5754b39c3c7SArchie Cobbs 			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.
592e1e1452dSArchie Cobbs  */
593e1e1452dSArchie Cobbs static int
594069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
595e1e1452dSArchie Cobbs {
596e1e1452dSArchie Cobbs 	const node_p node = hook->node;
597e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
598069154d5SJulian Elischer 	struct mbuf *m;
599069154d5SJulian Elischer 	meta_p meta;
600e1e1452dSArchie Cobbs 
601069154d5SJulian Elischer 	NGI_GET_M(item, m);
602069154d5SJulian Elischer 	NGI_GET_META(item, meta);
603069154d5SJulian Elischer 	NG_FREE_ITEM(item);
604e1e1452dSArchie Cobbs 	if (hook == priv->lower)
605e1e1452dSArchie Cobbs 		return ng_ether_rcv_lower(node, m, meta);
606e1e1452dSArchie Cobbs 	if (hook == priv->upper)
607e1e1452dSArchie Cobbs 		return ng_ether_rcv_upper(node, m, meta);
608e1e1452dSArchie Cobbs 	panic("%s: weird hook", __FUNCTION__);
609e1e1452dSArchie Cobbs }
610e1e1452dSArchie Cobbs 
611e1e1452dSArchie Cobbs /*
612e1e1452dSArchie Cobbs  * Handle an mbuf received on the "lower" hook.
613e1e1452dSArchie Cobbs  */
614e1e1452dSArchie Cobbs static int
615e1e1452dSArchie Cobbs ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
616e1e1452dSArchie Cobbs {
617e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
618e1e1452dSArchie Cobbs 
619e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
620e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
621069154d5SJulian Elischer 		NG_FREE_M(m);
622069154d5SJulian Elischer 		NG_FREE_META(meta);
623e1e1452dSArchie Cobbs 		return (EINVAL);
624e1e1452dSArchie Cobbs 	}
625e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
626e1e1452dSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
627e1e1452dSArchie Cobbs 		NG_FREE_META(meta);
628e1e1452dSArchie Cobbs 		return (ENOBUFS);
629e1e1452dSArchie Cobbs 	}
630e1e1452dSArchie Cobbs 
6314b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6324b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6334b39c3c7SArchie Cobbs 		bcopy((IFP2AC(priv->ifp))->ac_enaddr,
6344b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
6354b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
6364b39c3c7SArchie Cobbs 	}
637561e4fb9SJulian Elischer 
638e1e1452dSArchie Cobbs 	/* Send it on its way */
639e1e1452dSArchie Cobbs 	NG_FREE_META(meta);
640e1e1452dSArchie Cobbs 	return ether_output_frame(priv->ifp, m);
641e1e1452dSArchie Cobbs }
642e1e1452dSArchie Cobbs 
643e1e1452dSArchie Cobbs /*
644e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
645e1e1452dSArchie Cobbs  */
646e1e1452dSArchie Cobbs static int
647e1e1452dSArchie Cobbs ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
648e1e1452dSArchie Cobbs {
649e1e1452dSArchie Cobbs 	const priv_p priv = node->private;
650e1e1452dSArchie Cobbs 	struct ether_header *eh;
651e1e1452dSArchie Cobbs 
652e1e1452dSArchie Cobbs 	/* Check length and pull off header */
653e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(*eh)) {
654069154d5SJulian Elischer 		NG_FREE_M(m);
655069154d5SJulian Elischer 		NG_FREE_META(meta);
656e1e1452dSArchie Cobbs 		return (EINVAL);
657e1e1452dSArchie Cobbs 	}
658e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
659e1e1452dSArchie Cobbs 		NG_FREE_META(meta);
660e1e1452dSArchie Cobbs 		return (ENOBUFS);
661e1e1452dSArchie Cobbs 	}
662e1e1452dSArchie Cobbs 	eh = mtod(m, struct ether_header *);
663e1e1452dSArchie Cobbs 	m->m_data += sizeof(*eh);
664e1e1452dSArchie Cobbs 	m->m_len -= sizeof(*eh);
665e1e1452dSArchie Cobbs 	m->m_pkthdr.len -= sizeof(*eh);
666a62b20c4SJulian Elischer 	m->m_pkthdr.rcvif = priv->ifp;
667e1e1452dSArchie Cobbs 
668e1e1452dSArchie Cobbs 	/* Route packet back in */
669e1e1452dSArchie Cobbs 	NG_FREE_META(meta);
670e1e1452dSArchie Cobbs 	ether_demux(priv->ifp, eh, m);
671e1e1452dSArchie Cobbs 	return (0);
672e1e1452dSArchie Cobbs }
673e1e1452dSArchie Cobbs 
674e1e1452dSArchie Cobbs /*
675e1e1452dSArchie Cobbs  * Shutdown node. This resets the node but does not remove it.
676069154d5SJulian Elischer  * Actually it produces a new node. XXX The problem  is what to do when
677069154d5SJulian Elischer  * the node really DOES need to go away,
678069154d5SJulian Elischer  * or if our re-make of the node fails.
679e1e1452dSArchie Cobbs  */
680e1e1452dSArchie Cobbs static int
681069154d5SJulian Elischer ng_ether_shutdown(node_p node)
682e1e1452dSArchie Cobbs {
683069154d5SJulian Elischer 	char name[IFNAMSIZ + 1];
6844b39c3c7SArchie Cobbs 	const priv_p priv = node->private;
6854b39c3c7SArchie Cobbs 
6864b39c3c7SArchie Cobbs 	if (priv->promisc) {		/* disable promiscuous mode */
6874b39c3c7SArchie Cobbs 		(void)ifpromisc(priv->ifp, 0);
6884b39c3c7SArchie Cobbs 		priv->promisc = 0;
6894b39c3c7SArchie Cobbs 	}
690069154d5SJulian Elischer 	ng_unref(node);
691069154d5SJulian Elischer 	snprintf(name, sizeof(name), "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
692069154d5SJulian Elischer 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
693069154d5SJulian Elischer 		log(LOG_ERR, "%s: can't %s for %s\n",
694069154d5SJulian Elischer 		    __FUNCTION__, "create node", name);
695069154d5SJulian Elischer 		return (ENOMEM);
696069154d5SJulian Elischer 	}
697069154d5SJulian Elischer 
698069154d5SJulian Elischer 	/* Allocate private data */
699069154d5SJulian Elischer 	node->private = priv;
700069154d5SJulian Elischer 	IFP2NG(priv->ifp) = node;
7014b39c3c7SArchie Cobbs 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
702069154d5SJulian Elischer 
703069154d5SJulian Elischer 	/* Try to give the node the same name as the interface */
704069154d5SJulian Elischer 	if (ng_name_node(node, name) != 0) {
705069154d5SJulian Elischer 		log(LOG_WARNING, "%s: can't name node %s\n",
706069154d5SJulian Elischer 		    __FUNCTION__, name);
707069154d5SJulian Elischer 	}
708e1e1452dSArchie Cobbs 	return (0);
709e1e1452dSArchie Cobbs }
710e1e1452dSArchie Cobbs 
711e1e1452dSArchie Cobbs /*
712e1e1452dSArchie Cobbs  * Hook disconnection.
713e1e1452dSArchie Cobbs  */
714e1e1452dSArchie Cobbs static int
715e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
716e1e1452dSArchie Cobbs {
717e1e1452dSArchie Cobbs 	const priv_p priv = hook->node->private;
718e1e1452dSArchie Cobbs 
719e1e1452dSArchie Cobbs 	if (hook == priv->upper)
720e1e1452dSArchie Cobbs 		priv->upper = NULL;
721e1e1452dSArchie Cobbs 	else if (hook == priv->lower) {
722e1e1452dSArchie Cobbs 		priv->lower = NULL;
723e1e1452dSArchie Cobbs 		priv->lowerOrphan = 0;
724e1e1452dSArchie Cobbs 	} else
725e1e1452dSArchie Cobbs 		panic("%s: weird hook", __FUNCTION__);
726069154d5SJulian Elischer 	if ((hook->node->numhooks == 0)
727069154d5SJulian Elischer 	&& ((hook->node->flags & NG_INVALID) == 0))
728069154d5SJulian Elischer 		ng_rmnode_self(hook->node);	/* reset node */
729e1e1452dSArchie Cobbs 	return (0);
730e1e1452dSArchie Cobbs }
731e1e1452dSArchie Cobbs 
73256045c66SArchie Cobbs static int
73356045c66SArchie Cobbs ng_enaddr_parse(const struct ng_parse_type *type,
73456045c66SArchie Cobbs 	const char *s, int *const off, const u_char *const start,
73556045c66SArchie Cobbs 	u_char *const buf, int *const buflen)
73656045c66SArchie Cobbs {
73756045c66SArchie Cobbs 	char *eptr;
73856045c66SArchie Cobbs 	u_long val;
73956045c66SArchie Cobbs 	int i;
74056045c66SArchie Cobbs 
74156045c66SArchie Cobbs 	if (*buflen < ETHER_ADDR_LEN)
74256045c66SArchie Cobbs 		return (ERANGE);
74356045c66SArchie Cobbs 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
74456045c66SArchie Cobbs 		val = strtoul(s + *off, &eptr, 16);
74556045c66SArchie Cobbs 		if (val > 0xff || eptr == s + *off)
74656045c66SArchie Cobbs 			return (EINVAL);
74756045c66SArchie Cobbs 		buf[i] = (u_char)val;
74856045c66SArchie Cobbs 		*off = (eptr - s);
74956045c66SArchie Cobbs 		if (i < ETHER_ADDR_LEN - 1) {
75056045c66SArchie Cobbs 			if (*eptr != ':')
75156045c66SArchie Cobbs 				return (EINVAL);
75256045c66SArchie Cobbs 			(*off)++;
75356045c66SArchie Cobbs 		}
75456045c66SArchie Cobbs 	}
75556045c66SArchie Cobbs 	*buflen = ETHER_ADDR_LEN;
75656045c66SArchie Cobbs 	return (0);
75756045c66SArchie Cobbs }
75856045c66SArchie Cobbs 
75956045c66SArchie Cobbs static int
76056045c66SArchie Cobbs ng_enaddr_unparse(const struct ng_parse_type *type,
76156045c66SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
76256045c66SArchie Cobbs {
76356045c66SArchie Cobbs 	int len;
76456045c66SArchie Cobbs 
76556045c66SArchie Cobbs 	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
7667f98dc04SArchie Cobbs 	    data[*off], data[*off + 1], data[*off + 2],
7677f98dc04SArchie Cobbs 	    data[*off + 3], data[*off + 4], data[*off + 5]);
76856045c66SArchie Cobbs 	if (len >= cbuflen)
76956045c66SArchie Cobbs 		return (ERANGE);
77056045c66SArchie Cobbs 	*off += ETHER_ADDR_LEN;
77156045c66SArchie Cobbs 	return (0);
77256045c66SArchie Cobbs }
77356045c66SArchie Cobbs 
774e1e1452dSArchie Cobbs /******************************************************************
775e1e1452dSArchie Cobbs 		    	INITIALIZATION
776e1e1452dSArchie Cobbs ******************************************************************/
777e1e1452dSArchie Cobbs 
778e1e1452dSArchie Cobbs /*
779e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
780e1e1452dSArchie Cobbs  */
781e1e1452dSArchie Cobbs static int
782e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
783e1e1452dSArchie Cobbs {
784e1e1452dSArchie Cobbs 	struct ifnet *ifp;
785e1e1452dSArchie Cobbs 	int error = 0;
786e1e1452dSArchie Cobbs 	int s;
787e1e1452dSArchie Cobbs 
788e1e1452dSArchie Cobbs 	s = splnet();
789e1e1452dSArchie Cobbs 	switch (event) {
790e1e1452dSArchie Cobbs 	case MOD_LOAD:
791e1e1452dSArchie Cobbs 
792e1e1452dSArchie Cobbs 		/* Register function hooks */
793e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
794e1e1452dSArchie Cobbs 			error = EEXIST;
795e1e1452dSArchie Cobbs 			break;
796e1e1452dSArchie Cobbs 		}
797e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
798e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
799e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
800e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
801e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
802e1e1452dSArchie Cobbs 
803e1e1452dSArchie Cobbs 		/* Create nodes for any already-existing Ethernet interfaces */
804e1e1452dSArchie Cobbs 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
805e1e1452dSArchie Cobbs 			if (ifp->if_type == IFT_ETHER)
806e1e1452dSArchie Cobbs 				ng_ether_attach(ifp);
807e1e1452dSArchie Cobbs 		}
808e1e1452dSArchie Cobbs 		break;
809e1e1452dSArchie Cobbs 
810e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
811e1e1452dSArchie Cobbs 
812e1e1452dSArchie Cobbs 		/*
813e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
814e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
815e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
816e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
817e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
818e1e1452dSArchie Cobbs 		 */
819e1e1452dSArchie Cobbs 
820e1e1452dSArchie Cobbs 		/* Unregister function hooks */
821e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
822e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
823e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
824e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
825e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
826e1e1452dSArchie Cobbs 		break;
827e1e1452dSArchie Cobbs 
828e1e1452dSArchie Cobbs 	default:
829e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
830e1e1452dSArchie Cobbs 		break;
831e1e1452dSArchie Cobbs 	}
832e1e1452dSArchie Cobbs 	splx(s);
833e1e1452dSArchie Cobbs 	return (error);
834e1e1452dSArchie Cobbs }
835e1e1452dSArchie Cobbs 
836