xref: /freebsd/sys/netgraph/ng_ether.c (revision 30400f03aa3c764fffcb7d3158634778a035a183)
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);
21330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
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);
23230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
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 {
25430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
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);
27430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
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);
31130400f03SJulian Elischer 		NG_NODE_UNREF(node);
312e1e1452dSArchie Cobbs 		return;
313e1e1452dSArchie Cobbs 	}
31430400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, 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 */
339e1e1452dSArchie Cobbs 	IFP2NG(ifp) = NULL;		/* detach node from interface */
34030400f03SJulian Elischer 	priv = NG_NODE_PRIVATE(node);		/* free node private info */
341e1e1452dSArchie Cobbs 	bzero(priv, sizeof(*priv));
342e1e1452dSArchie Cobbs 	FREE(priv, M_NETGRAPH);
34330400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
34430400f03SJulian Elischer 	NG_NODE_UNREF(node);			/* free node itself */
345e1e1452dSArchie Cobbs }
346e1e1452dSArchie Cobbs 
347e1e1452dSArchie Cobbs /*
348e1e1452dSArchie Cobbs  * Optimization for gluing the Ethernet header back onto
349e1e1452dSArchie Cobbs  * the front of an incoming packet.
350e1e1452dSArchie Cobbs  */
351e1e1452dSArchie Cobbs static int
352e1e1452dSArchie Cobbs ng_ether_glueback_header(struct mbuf **mp, struct ether_header *eh)
353e1e1452dSArchie Cobbs {
354e1e1452dSArchie Cobbs 	struct mbuf *m = *mp;
355e1e1452dSArchie Cobbs 	uintfptr_t room;
356e1e1452dSArchie Cobbs 	int error = 0;
357e1e1452dSArchie Cobbs 
358e1e1452dSArchie Cobbs 	/*
359e1e1452dSArchie Cobbs 	 * Possibly the header is already on the front.
360e1e1452dSArchie Cobbs 	 * If this is the case so just move the markers back
361e1e1452dSArchie Cobbs 	 * to re-include it. We lucked out.
362e1e1452dSArchie Cobbs 	 * This allows us to avoid a yucky m_pullup
363e1e1452dSArchie Cobbs 	 * in later nodes if it works.
364e1e1452dSArchie Cobbs 	 */
365e1e1452dSArchie Cobbs 	if (eh == mtod(m, struct ether_header *) - 1) {
366e1e1452dSArchie Cobbs 		m->m_len += sizeof(*eh);
367e1e1452dSArchie Cobbs 		m->m_data -= sizeof(*eh);
368e1e1452dSArchie Cobbs 		m->m_pkthdr.len += sizeof(*eh);
369e1e1452dSArchie Cobbs 		goto done;
370e1e1452dSArchie Cobbs 	}
371e1e1452dSArchie Cobbs 
372e1e1452dSArchie Cobbs 	/*
373e1e1452dSArchie Cobbs 	 * Alternatively there may be room even though
374e1e1452dSArchie Cobbs 	 * it is stored somewhere else. If so, copy it in.
375e1e1452dSArchie Cobbs 	 * This only safe because we KNOW that this packet has
376e1e1452dSArchie Cobbs 	 * just been generated by an ethernet card, so there are
377e1e1452dSArchie Cobbs 	 * no aliases to the buffer (not so for outgoing packets).
378e1e1452dSArchie Cobbs 	 * Nearly all ethernet cards will end up producing mbufs
379e1e1452dSArchie Cobbs 	 * that fall into these cases. So we are not optimizing
380e1e1452dSArchie Cobbs 	 * contorted cases.
381e1e1452dSArchie Cobbs 	 */
382e1e1452dSArchie Cobbs 	if ((m->m_flags & M_EXT) != 0) {
383e1e1452dSArchie Cobbs 		room = mtod(m, caddr_t) - m->m_ext.ext_buf;
384e1e1452dSArchie Cobbs 		if (room > m->m_ext.ext_size)	/* garbage, fail immediately */
385e1e1452dSArchie Cobbs 			room = 0;
386e1e1452dSArchie Cobbs 	} else
387e1e1452dSArchie Cobbs 		room = mtod(m, caddr_t) - m->m_pktdat;
388e1e1452dSArchie Cobbs 
389e1e1452dSArchie Cobbs 	/*
390e1e1452dSArchie Cobbs 	 * If we have room, just copy it and adjust
391e1e1452dSArchie Cobbs 	 */
392e1e1452dSArchie Cobbs 	if (room >= sizeof(*eh)) {
393e1e1452dSArchie Cobbs 		m->m_len += sizeof(*eh);
394e1e1452dSArchie Cobbs 		m->m_data -= sizeof(*eh);
395e1e1452dSArchie Cobbs 		m->m_pkthdr.len += sizeof(*eh);
396e1e1452dSArchie Cobbs 		goto copy;
397e1e1452dSArchie Cobbs 	}
398e1e1452dSArchie Cobbs 
399e1e1452dSArchie Cobbs 	/*
400e1e1452dSArchie Cobbs 	 * Doing anything more is likely to get more
401e1e1452dSArchie Cobbs 	 * expensive than it's worth..
402e1e1452dSArchie Cobbs 	 * it's probable that everything else is in one
403e1e1452dSArchie Cobbs 	 * big lump. The next node will do an m_pullup()
404e1e1452dSArchie Cobbs 	 * for exactly the amount of data it needs and
405e1e1452dSArchie Cobbs 	 * hopefully everything after that will not
406e1e1452dSArchie Cobbs 	 * need one. So let's just use M_PREPEND.
407e1e1452dSArchie Cobbs 	 */
408e1e1452dSArchie Cobbs 	M_PREPEND(m, sizeof (*eh), M_DONTWAIT);
409e1e1452dSArchie Cobbs 	if (m == NULL) {
410e1e1452dSArchie Cobbs 		error = ENOBUFS;
411e1e1452dSArchie Cobbs 		goto done;
412e1e1452dSArchie Cobbs 	}
413e1e1452dSArchie Cobbs 
414e1e1452dSArchie Cobbs copy:
415e1e1452dSArchie Cobbs 	/* Copy header and return (possibly new) mbuf */
416e1e1452dSArchie Cobbs 	bcopy((caddr_t)eh, mtod(m, struct ether_header *), sizeof(*eh));
417e1e1452dSArchie Cobbs done:
418e1e1452dSArchie Cobbs 	*mp = m;
419e1e1452dSArchie Cobbs 	return error;
420e1e1452dSArchie Cobbs }
421e1e1452dSArchie Cobbs 
422e1e1452dSArchie Cobbs /******************************************************************
423e1e1452dSArchie Cobbs 		    NETGRAPH NODE METHODS
424e1e1452dSArchie Cobbs ******************************************************************/
425e1e1452dSArchie Cobbs 
426e1e1452dSArchie Cobbs /*
427e1e1452dSArchie Cobbs  * It is not possible or allowable to create a node of this type.
428e1e1452dSArchie Cobbs  * Nodes get created when the interface is attached (or, when
429e1e1452dSArchie Cobbs  * this node type's KLD is loaded).
430e1e1452dSArchie Cobbs  */
431e1e1452dSArchie Cobbs static int
432069154d5SJulian Elischer ng_ether_constructor(node_p node)
433e1e1452dSArchie Cobbs {
434e1e1452dSArchie Cobbs 	return (EINVAL);
435e1e1452dSArchie Cobbs }
436e1e1452dSArchie Cobbs 
437e1e1452dSArchie Cobbs /*
438e1e1452dSArchie Cobbs  * Check for attaching a new hook.
439e1e1452dSArchie Cobbs  */
440e1e1452dSArchie Cobbs static	int
441e1e1452dSArchie Cobbs ng_ether_newhook(node_p node, hook_p hook, const char *name)
442e1e1452dSArchie Cobbs {
44330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
444e1e1452dSArchie Cobbs 	u_char orphan = priv->lowerOrphan;
445e1e1452dSArchie Cobbs 	hook_p *hookptr;
446e1e1452dSArchie Cobbs 
447e1e1452dSArchie Cobbs 	/* Divert hook is an alias for lower */
448e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_DIVERT) == 0)
449e1e1452dSArchie Cobbs 		name = NG_ETHER_HOOK_LOWER;
450e1e1452dSArchie Cobbs 
451e1e1452dSArchie Cobbs 	/* Which hook? */
452e1e1452dSArchie Cobbs 	if (strcmp(name, NG_ETHER_HOOK_UPPER) == 0)
453e1e1452dSArchie Cobbs 		hookptr = &priv->upper;
454e1e1452dSArchie Cobbs 	else if (strcmp(name, NG_ETHER_HOOK_LOWER) == 0) {
455e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
456e1e1452dSArchie Cobbs 		orphan = 0;
457e1e1452dSArchie Cobbs 	} else if (strcmp(name, NG_ETHER_HOOK_ORPHAN) == 0) {
458e1e1452dSArchie Cobbs 		hookptr = &priv->lower;
459e1e1452dSArchie Cobbs 		orphan = 1;
460e1e1452dSArchie Cobbs 	} else
461e1e1452dSArchie Cobbs 		return (EINVAL);
462e1e1452dSArchie Cobbs 
463e1e1452dSArchie Cobbs 	/* Check if already connected (shouldn't be, but doesn't hurt) */
464e1e1452dSArchie Cobbs 	if (*hookptr != NULL)
465e1e1452dSArchie Cobbs 		return (EISCONN);
466e1e1452dSArchie Cobbs 
467e1e1452dSArchie Cobbs 	/* OK */
468e1e1452dSArchie Cobbs 	*hookptr = hook;
469e1e1452dSArchie Cobbs 	priv->lowerOrphan = orphan;
470e1e1452dSArchie Cobbs 	return (0);
471e1e1452dSArchie Cobbs }
472e1e1452dSArchie Cobbs 
473e1e1452dSArchie Cobbs /*
474859a4d16SJulian Elischer  * Hooks are attached, adjust to force queueing.
475859a4d16SJulian Elischer  * We don't really care which hook it is.
476859a4d16SJulian Elischer  * they should all be queuing for outgoing data.
477859a4d16SJulian Elischer  */
478859a4d16SJulian Elischer static	int
479859a4d16SJulian Elischer ng_ether_connect(hook_p hook)
480859a4d16SJulian Elischer {
48130400f03SJulian Elischer 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
482859a4d16SJulian Elischer 	return (0);
483859a4d16SJulian Elischer }
484859a4d16SJulian Elischer 
485859a4d16SJulian Elischer /*
486e1e1452dSArchie Cobbs  * Receive an incoming control message.
487e1e1452dSArchie Cobbs  */
488e1e1452dSArchie Cobbs static int
489069154d5SJulian Elischer ng_ether_rcvmsg(node_p node, item_p item, hook_p lasthook)
490e1e1452dSArchie Cobbs {
49130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
492e1e1452dSArchie Cobbs 	struct ng_mesg *resp = NULL;
493e1e1452dSArchie Cobbs 	int error = 0;
494069154d5SJulian Elischer 	struct ng_mesg *msg;
495e1e1452dSArchie Cobbs 
496069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
497e1e1452dSArchie Cobbs 	switch (msg->header.typecookie) {
498e1e1452dSArchie Cobbs 	case NGM_ETHER_COOKIE:
499e1e1452dSArchie Cobbs 		switch (msg->header.cmd) {
500e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFNAME:
501e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, IFNAMSIZ + 1, M_NOWAIT);
502e1e1452dSArchie Cobbs 			if (resp == NULL) {
503e1e1452dSArchie Cobbs 				error = ENOMEM;
504e1e1452dSArchie Cobbs 				break;
505e1e1452dSArchie Cobbs 			}
506e1e1452dSArchie Cobbs 			snprintf(resp->data, IFNAMSIZ + 1,
507e1e1452dSArchie Cobbs 			    "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
508e1e1452dSArchie Cobbs 			break;
509e1e1452dSArchie Cobbs 		case NGM_ETHER_GET_IFINDEX:
510e1e1452dSArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
511e1e1452dSArchie Cobbs 			if (resp == NULL) {
512e1e1452dSArchie Cobbs 				error = ENOMEM;
513e1e1452dSArchie Cobbs 				break;
514e1e1452dSArchie Cobbs 			}
515e1e1452dSArchie Cobbs 			*((u_int32_t *)resp->data) = priv->ifp->if_index;
516e1e1452dSArchie Cobbs 			break;
5174b39c3c7SArchie Cobbs 		case NGM_ETHER_GET_ENADDR:
5184b39c3c7SArchie Cobbs 			NG_MKRESPONSE(resp, msg, ETHER_ADDR_LEN, M_NOWAIT);
5194b39c3c7SArchie Cobbs 			if (resp == NULL) {
5204b39c3c7SArchie Cobbs 				error = ENOMEM;
5214b39c3c7SArchie Cobbs 				break;
5224b39c3c7SArchie Cobbs 			}
5234b39c3c7SArchie Cobbs 			bcopy((IFP2AC(priv->ifp))->ac_enaddr,
5244b39c3c7SArchie Cobbs 			    resp->data, ETHER_ADDR_LEN);
5254b39c3c7SArchie Cobbs 			break;
52656045c66SArchie Cobbs 		case NGM_ETHER_SET_ENADDR:
52756045c66SArchie Cobbs 		    {
52856045c66SArchie Cobbs 			if (msg->header.arglen != ETHER_ADDR_LEN) {
52956045c66SArchie Cobbs 				error = EINVAL;
53056045c66SArchie Cobbs 				break;
53156045c66SArchie Cobbs 			}
53256045c66SArchie Cobbs 			error = if_setlladdr(priv->ifp,
53356045c66SArchie Cobbs 			    (u_char *)msg->data, ETHER_ADDR_LEN);
53456045c66SArchie Cobbs 			break;
53556045c66SArchie Cobbs 		    }
53656045c66SArchie Cobbs 		case NGM_ETHER_GET_PROMISC:
53756045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
53856045c66SArchie Cobbs 			if (resp == NULL) {
53956045c66SArchie Cobbs 				error = ENOMEM;
54056045c66SArchie Cobbs 				break;
54156045c66SArchie Cobbs 			}
54256045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->promisc;
54356045c66SArchie Cobbs 			break;
5444b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_PROMISC:
5454b39c3c7SArchie Cobbs 		    {
5464b39c3c7SArchie Cobbs 			u_char want;
5474b39c3c7SArchie Cobbs 
5484b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5494b39c3c7SArchie Cobbs 				error = EINVAL;
5504b39c3c7SArchie Cobbs 				break;
5514b39c3c7SArchie Cobbs 			}
5524b39c3c7SArchie Cobbs 			want = !!*((u_int32_t *)msg->data);
5534b39c3c7SArchie Cobbs 			if (want ^ priv->promisc) {
5544b39c3c7SArchie Cobbs 				if ((error = ifpromisc(priv->ifp, want)) != 0)
5554b39c3c7SArchie Cobbs 					break;
5564b39c3c7SArchie Cobbs 				priv->promisc = want;
5574b39c3c7SArchie Cobbs 			}
5584b39c3c7SArchie Cobbs 			break;
5594b39c3c7SArchie Cobbs 		    }
56056045c66SArchie Cobbs 		case NGM_ETHER_GET_AUTOSRC:
56156045c66SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(u_int32_t), M_NOWAIT);
56256045c66SArchie Cobbs 			if (resp == NULL) {
56356045c66SArchie Cobbs 				error = ENOMEM;
56456045c66SArchie Cobbs 				break;
56556045c66SArchie Cobbs 			}
56656045c66SArchie Cobbs 			*((u_int32_t *)resp->data) = priv->autoSrcAddr;
56756045c66SArchie Cobbs 			break;
5684b39c3c7SArchie Cobbs 		case NGM_ETHER_SET_AUTOSRC:
5694b39c3c7SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
5704b39c3c7SArchie Cobbs 				error = EINVAL;
5714b39c3c7SArchie Cobbs 				break;
5724b39c3c7SArchie Cobbs 			}
5734b39c3c7SArchie Cobbs 			priv->autoSrcAddr = !!*((u_int32_t *)msg->data);
5744b39c3c7SArchie Cobbs 			break;
575e1e1452dSArchie Cobbs 		default:
576e1e1452dSArchie Cobbs 			error = EINVAL;
577e1e1452dSArchie Cobbs 			break;
578e1e1452dSArchie Cobbs 		}
579e1e1452dSArchie Cobbs 		break;
580e1e1452dSArchie Cobbs 	default:
581e1e1452dSArchie Cobbs 		error = EINVAL;
582e1e1452dSArchie Cobbs 		break;
583e1e1452dSArchie Cobbs 	}
584069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
585069154d5SJulian Elischer 	NG_FREE_MSG(msg);
586e1e1452dSArchie Cobbs 	return (error);
587e1e1452dSArchie Cobbs }
588e1e1452dSArchie Cobbs 
589e1e1452dSArchie Cobbs /*
590e1e1452dSArchie Cobbs  * Receive data on a hook.
591e1e1452dSArchie Cobbs  */
592e1e1452dSArchie Cobbs static int
593069154d5SJulian Elischer ng_ether_rcvdata(hook_p hook, item_p item)
594e1e1452dSArchie Cobbs {
59530400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
59630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
597069154d5SJulian Elischer 	struct mbuf *m;
598069154d5SJulian Elischer 	meta_p meta;
599e1e1452dSArchie Cobbs 
600069154d5SJulian Elischer 	NGI_GET_M(item, m);
601069154d5SJulian Elischer 	NGI_GET_META(item, meta);
602069154d5SJulian Elischer 	NG_FREE_ITEM(item);
603e1e1452dSArchie Cobbs 	if (hook == priv->lower)
604e1e1452dSArchie Cobbs 		return ng_ether_rcv_lower(node, m, meta);
605e1e1452dSArchie Cobbs 	if (hook == priv->upper)
606e1e1452dSArchie Cobbs 		return ng_ether_rcv_upper(node, m, meta);
607e1e1452dSArchie Cobbs 	panic("%s: weird hook", __FUNCTION__);
608e1e1452dSArchie Cobbs }
609e1e1452dSArchie Cobbs 
610e1e1452dSArchie Cobbs /*
611e1e1452dSArchie Cobbs  * Handle an mbuf received on the "lower" hook.
612e1e1452dSArchie Cobbs  */
613e1e1452dSArchie Cobbs static int
614e1e1452dSArchie Cobbs ng_ether_rcv_lower(node_p node, struct mbuf *m, meta_p meta)
615e1e1452dSArchie Cobbs {
61630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
617e1e1452dSArchie Cobbs 
618e1e1452dSArchie Cobbs 	/* Make sure header is fully pulled up */
619e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(struct ether_header)) {
620069154d5SJulian Elischer 		NG_FREE_M(m);
621069154d5SJulian Elischer 		NG_FREE_META(meta);
622e1e1452dSArchie Cobbs 		return (EINVAL);
623e1e1452dSArchie Cobbs 	}
624e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(struct ether_header)
625e1e1452dSArchie Cobbs 	    && (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
626e1e1452dSArchie Cobbs 		NG_FREE_META(meta);
627e1e1452dSArchie Cobbs 		return (ENOBUFS);
628e1e1452dSArchie Cobbs 	}
629e1e1452dSArchie Cobbs 
6304b39c3c7SArchie Cobbs 	/* Drop in the MAC address if desired */
6314b39c3c7SArchie Cobbs 	if (priv->autoSrcAddr) {
6324b39c3c7SArchie Cobbs 		bcopy((IFP2AC(priv->ifp))->ac_enaddr,
6334b39c3c7SArchie Cobbs 		    mtod(m, struct ether_header *)->ether_shost,
6344b39c3c7SArchie Cobbs 		    ETHER_ADDR_LEN);
6354b39c3c7SArchie Cobbs 	}
636561e4fb9SJulian Elischer 
637e1e1452dSArchie Cobbs 	/* Send it on its way */
638e1e1452dSArchie Cobbs 	NG_FREE_META(meta);
639e1e1452dSArchie Cobbs 	return ether_output_frame(priv->ifp, m);
640e1e1452dSArchie Cobbs }
641e1e1452dSArchie Cobbs 
642e1e1452dSArchie Cobbs /*
643e1e1452dSArchie Cobbs  * Handle an mbuf received on the "upper" hook.
644e1e1452dSArchie Cobbs  */
645e1e1452dSArchie Cobbs static int
646e1e1452dSArchie Cobbs ng_ether_rcv_upper(node_p node, struct mbuf *m, meta_p meta)
647e1e1452dSArchie Cobbs {
64830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
649e1e1452dSArchie Cobbs 	struct ether_header *eh;
650e1e1452dSArchie Cobbs 
651e1e1452dSArchie Cobbs 	/* Check length and pull off header */
652e1e1452dSArchie Cobbs 	if (m->m_pkthdr.len < sizeof(*eh)) {
653069154d5SJulian Elischer 		NG_FREE_M(m);
654069154d5SJulian Elischer 		NG_FREE_META(meta);
655e1e1452dSArchie Cobbs 		return (EINVAL);
656e1e1452dSArchie Cobbs 	}
657e1e1452dSArchie Cobbs 	if (m->m_len < sizeof(*eh) && (m = m_pullup(m, sizeof(*eh))) == NULL) {
658e1e1452dSArchie Cobbs 		NG_FREE_META(meta);
659e1e1452dSArchie Cobbs 		return (ENOBUFS);
660e1e1452dSArchie Cobbs 	}
661e1e1452dSArchie Cobbs 	eh = mtod(m, struct ether_header *);
662e1e1452dSArchie Cobbs 	m->m_data += sizeof(*eh);
663e1e1452dSArchie Cobbs 	m->m_len -= sizeof(*eh);
664e1e1452dSArchie Cobbs 	m->m_pkthdr.len -= sizeof(*eh);
665a62b20c4SJulian Elischer 	m->m_pkthdr.rcvif = priv->ifp;
666e1e1452dSArchie Cobbs 
667e1e1452dSArchie Cobbs 	/* Route packet back in */
668e1e1452dSArchie Cobbs 	NG_FREE_META(meta);
669e1e1452dSArchie Cobbs 	ether_demux(priv->ifp, eh, m);
670e1e1452dSArchie Cobbs 	return (0);
671e1e1452dSArchie Cobbs }
672e1e1452dSArchie Cobbs 
673e1e1452dSArchie Cobbs /*
674e1e1452dSArchie Cobbs  * Shutdown node. This resets the node but does not remove it.
675069154d5SJulian Elischer  * Actually it produces a new node. XXX The problem  is what to do when
676069154d5SJulian Elischer  * the node really DOES need to go away,
677069154d5SJulian Elischer  * or if our re-make of the node fails.
678e1e1452dSArchie Cobbs  */
679e1e1452dSArchie Cobbs static int
680069154d5SJulian Elischer ng_ether_shutdown(node_p node)
681e1e1452dSArchie Cobbs {
682069154d5SJulian Elischer 	char name[IFNAMSIZ + 1];
68330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6844b39c3c7SArchie Cobbs 
6854b39c3c7SArchie Cobbs 	if (priv->promisc) {		/* disable promiscuous mode */
6864b39c3c7SArchie Cobbs 		(void)ifpromisc(priv->ifp, 0);
6874b39c3c7SArchie Cobbs 		priv->promisc = 0;
6884b39c3c7SArchie Cobbs 	}
68930400f03SJulian Elischer 	NG_NODE_UNREF(node);
690069154d5SJulian Elischer 	snprintf(name, sizeof(name), "%s%d", priv->ifp->if_name, priv->ifp->if_unit);
691069154d5SJulian Elischer 	if (ng_make_node_common(&ng_ether_typestruct, &node) != 0) {
692069154d5SJulian Elischer 		log(LOG_ERR, "%s: can't %s for %s\n",
693069154d5SJulian Elischer 		    __FUNCTION__, "create node", name);
694069154d5SJulian Elischer 		return (ENOMEM);
695069154d5SJulian Elischer 	}
696069154d5SJulian Elischer 
697069154d5SJulian Elischer 	/* Allocate private data */
69830400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
699069154d5SJulian Elischer 	IFP2NG(priv->ifp) = node;
7004b39c3c7SArchie Cobbs 	priv->autoSrcAddr = 1;		/* reset auto-src-addr flag */
701069154d5SJulian Elischer 
702069154d5SJulian Elischer 	/* Try to give the node the same name as the interface */
703069154d5SJulian Elischer 	if (ng_name_node(node, name) != 0) {
704069154d5SJulian Elischer 		log(LOG_WARNING, "%s: can't name node %s\n",
705069154d5SJulian Elischer 		    __FUNCTION__, name);
706069154d5SJulian Elischer 	}
707e1e1452dSArchie Cobbs 	return (0);
708e1e1452dSArchie Cobbs }
709e1e1452dSArchie Cobbs 
710e1e1452dSArchie Cobbs /*
711e1e1452dSArchie Cobbs  * Hook disconnection.
712e1e1452dSArchie Cobbs  */
713e1e1452dSArchie Cobbs static int
714e1e1452dSArchie Cobbs ng_ether_disconnect(hook_p hook)
715e1e1452dSArchie Cobbs {
71630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
717e1e1452dSArchie Cobbs 
718e1e1452dSArchie Cobbs 	if (hook == priv->upper)
719e1e1452dSArchie Cobbs 		priv->upper = NULL;
720e1e1452dSArchie Cobbs 	else if (hook == priv->lower) {
721e1e1452dSArchie Cobbs 		priv->lower = NULL;
722e1e1452dSArchie Cobbs 		priv->lowerOrphan = 0;
723e1e1452dSArchie Cobbs 	} else
724e1e1452dSArchie Cobbs 		panic("%s: weird hook", __FUNCTION__);
72530400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
72630400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
72730400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));	/* reset node */
728e1e1452dSArchie Cobbs 	return (0);
729e1e1452dSArchie Cobbs }
730e1e1452dSArchie Cobbs 
73156045c66SArchie Cobbs static int
73256045c66SArchie Cobbs ng_enaddr_parse(const struct ng_parse_type *type,
73356045c66SArchie Cobbs 	const char *s, int *const off, const u_char *const start,
73456045c66SArchie Cobbs 	u_char *const buf, int *const buflen)
73556045c66SArchie Cobbs {
73656045c66SArchie Cobbs 	char *eptr;
73756045c66SArchie Cobbs 	u_long val;
73856045c66SArchie Cobbs 	int i;
73956045c66SArchie Cobbs 
74056045c66SArchie Cobbs 	if (*buflen < ETHER_ADDR_LEN)
74156045c66SArchie Cobbs 		return (ERANGE);
74256045c66SArchie Cobbs 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
74356045c66SArchie Cobbs 		val = strtoul(s + *off, &eptr, 16);
74456045c66SArchie Cobbs 		if (val > 0xff || eptr == s + *off)
74556045c66SArchie Cobbs 			return (EINVAL);
74656045c66SArchie Cobbs 		buf[i] = (u_char)val;
74756045c66SArchie Cobbs 		*off = (eptr - s);
74856045c66SArchie Cobbs 		if (i < ETHER_ADDR_LEN - 1) {
74956045c66SArchie Cobbs 			if (*eptr != ':')
75056045c66SArchie Cobbs 				return (EINVAL);
75156045c66SArchie Cobbs 			(*off)++;
75256045c66SArchie Cobbs 		}
75356045c66SArchie Cobbs 	}
75456045c66SArchie Cobbs 	*buflen = ETHER_ADDR_LEN;
75556045c66SArchie Cobbs 	return (0);
75656045c66SArchie Cobbs }
75756045c66SArchie Cobbs 
75856045c66SArchie Cobbs static int
75956045c66SArchie Cobbs ng_enaddr_unparse(const struct ng_parse_type *type,
76056045c66SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
76156045c66SArchie Cobbs {
76256045c66SArchie Cobbs 	int len;
76356045c66SArchie Cobbs 
76456045c66SArchie Cobbs 	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
7657f98dc04SArchie Cobbs 	    data[*off], data[*off + 1], data[*off + 2],
7667f98dc04SArchie Cobbs 	    data[*off + 3], data[*off + 4], data[*off + 5]);
76756045c66SArchie Cobbs 	if (len >= cbuflen)
76856045c66SArchie Cobbs 		return (ERANGE);
76956045c66SArchie Cobbs 	*off += ETHER_ADDR_LEN;
77056045c66SArchie Cobbs 	return (0);
77156045c66SArchie Cobbs }
77256045c66SArchie Cobbs 
773e1e1452dSArchie Cobbs /******************************************************************
774e1e1452dSArchie Cobbs 		    	INITIALIZATION
775e1e1452dSArchie Cobbs ******************************************************************/
776e1e1452dSArchie Cobbs 
777e1e1452dSArchie Cobbs /*
778e1e1452dSArchie Cobbs  * Handle loading and unloading for this node type.
779e1e1452dSArchie Cobbs  */
780e1e1452dSArchie Cobbs static int
781e1e1452dSArchie Cobbs ng_ether_mod_event(module_t mod, int event, void *data)
782e1e1452dSArchie Cobbs {
783e1e1452dSArchie Cobbs 	struct ifnet *ifp;
784e1e1452dSArchie Cobbs 	int error = 0;
785e1e1452dSArchie Cobbs 	int s;
786e1e1452dSArchie Cobbs 
787e1e1452dSArchie Cobbs 	s = splnet();
788e1e1452dSArchie Cobbs 	switch (event) {
789e1e1452dSArchie Cobbs 	case MOD_LOAD:
790e1e1452dSArchie Cobbs 
791e1e1452dSArchie Cobbs 		/* Register function hooks */
792e1e1452dSArchie Cobbs 		if (ng_ether_attach_p != NULL) {
793e1e1452dSArchie Cobbs 			error = EEXIST;
794e1e1452dSArchie Cobbs 			break;
795e1e1452dSArchie Cobbs 		}
796e1e1452dSArchie Cobbs 		ng_ether_attach_p = ng_ether_attach;
797e1e1452dSArchie Cobbs 		ng_ether_detach_p = ng_ether_detach;
798e1e1452dSArchie Cobbs 		ng_ether_output_p = ng_ether_output;
799e1e1452dSArchie Cobbs 		ng_ether_input_p = ng_ether_input;
800e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = ng_ether_input_orphan;
801e1e1452dSArchie Cobbs 
802e1e1452dSArchie Cobbs 		/* Create nodes for any already-existing Ethernet interfaces */
803e1e1452dSArchie Cobbs 		TAILQ_FOREACH(ifp, &ifnet, if_link) {
804e1e1452dSArchie Cobbs 			if (ifp->if_type == IFT_ETHER)
805e1e1452dSArchie Cobbs 				ng_ether_attach(ifp);
806e1e1452dSArchie Cobbs 		}
807e1e1452dSArchie Cobbs 		break;
808e1e1452dSArchie Cobbs 
809e1e1452dSArchie Cobbs 	case MOD_UNLOAD:
810e1e1452dSArchie Cobbs 
811e1e1452dSArchie Cobbs 		/*
812e1e1452dSArchie Cobbs 		 * Note that the base code won't try to unload us until
813e1e1452dSArchie Cobbs 		 * all nodes have been removed, and that can't happen
814e1e1452dSArchie Cobbs 		 * until all Ethernet interfaces are removed. In any
815e1e1452dSArchie Cobbs 		 * case, we know there are no nodes left if the action
816e1e1452dSArchie Cobbs 		 * is MOD_UNLOAD, so there's no need to detach any nodes.
817e1e1452dSArchie Cobbs 		 */
818e1e1452dSArchie Cobbs 
819e1e1452dSArchie Cobbs 		/* Unregister function hooks */
820e1e1452dSArchie Cobbs 		ng_ether_attach_p = NULL;
821e1e1452dSArchie Cobbs 		ng_ether_detach_p = NULL;
822e1e1452dSArchie Cobbs 		ng_ether_output_p = NULL;
823e1e1452dSArchie Cobbs 		ng_ether_input_p = NULL;
824e1e1452dSArchie Cobbs 		ng_ether_input_orphan_p = NULL;
825e1e1452dSArchie Cobbs 		break;
826e1e1452dSArchie Cobbs 
827e1e1452dSArchie Cobbs 	default:
828e1e1452dSArchie Cobbs 		error = EOPNOTSUPP;
829e1e1452dSArchie Cobbs 		break;
830e1e1452dSArchie Cobbs 	}
831e1e1452dSArchie Cobbs 	splx(s);
832e1e1452dSArchie Cobbs 	return (error);
833e1e1452dSArchie Cobbs }
834e1e1452dSArchie Cobbs 
835