ng_eiface.c revision 167348
1/*-
2 *
3 * Copyright (c) 1999-2001, Vitaly V Belekhov
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/netgraph/ng_eiface.c 167348 2007-03-08 21:10:53Z ru $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/errno.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/errno.h>
38#include <sys/sockio.h>
39#include <sys/socket.h>
40#include <sys/syslog.h>
41
42#include <net/if.h>
43#include <net/if_types.h>
44#include <net/netisr.h>
45
46#include <netgraph/ng_message.h>
47#include <netgraph/netgraph.h>
48#include <netgraph/ng_parse.h>
49#include <netgraph/ng_eiface.h>
50
51#include <net/bpf.h>
52#include <net/ethernet.h>
53#include <net/if_arp.h>
54
55static const struct ng_cmdlist ng_eiface_cmdlist[] = {
56	{
57	  NGM_EIFACE_COOKIE,
58	  NGM_EIFACE_GET_IFNAME,
59	  "getifname",
60	  NULL,
61	  &ng_parse_string_type
62	},
63	{
64	  NGM_EIFACE_COOKIE,
65	  NGM_EIFACE_SET,
66	  "set",
67	  &ng_parse_enaddr_type,
68	  NULL
69	},
70	{ 0 }
71};
72
73/* Node private data */
74struct ng_eiface_private {
75	struct ifnet	*ifp;		/* per-interface network data */
76	int		unit;		/* Interface unit number */
77	node_p		node;		/* Our netgraph node */
78	hook_p		ether;		/* Hook for ethernet stream */
79};
80typedef struct ng_eiface_private *priv_p;
81
82/* Interface methods */
83static void	ng_eiface_init(void *xsc);
84static void	ng_eiface_start(struct ifnet *ifp);
85static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
86#ifdef DEBUG
87static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
88#endif
89
90/* Netgraph methods */
91static int		ng_eiface_mod_event(module_t, int, void *);
92static ng_constructor_t	ng_eiface_constructor;
93static ng_rcvmsg_t	ng_eiface_rcvmsg;
94static ng_shutdown_t	ng_eiface_rmnode;
95static ng_newhook_t	ng_eiface_newhook;
96static ng_rcvdata_t	ng_eiface_rcvdata;
97static ng_disconnect_t	ng_eiface_disconnect;
98
99/* Node type descriptor */
100static struct ng_type typestruct = {
101	.version =	NG_ABI_VERSION,
102	.name =		NG_EIFACE_NODE_TYPE,
103	.mod_event =	ng_eiface_mod_event,
104	.constructor =	ng_eiface_constructor,
105	.rcvmsg =	ng_eiface_rcvmsg,
106	.shutdown =	ng_eiface_rmnode,
107	.newhook =	ng_eiface_newhook,
108	.rcvdata =	ng_eiface_rcvdata,
109	.disconnect =	ng_eiface_disconnect,
110	.cmdlist =	ng_eiface_cmdlist
111};
112NETGRAPH_INIT(eiface, &typestruct);
113
114static struct unrhdr	*ng_eiface_unit;
115
116/************************************************************************
117			INTERFACE STUFF
118 ************************************************************************/
119
120/*
121 * Process an ioctl for the virtual interface
122 */
123static int
124ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
125{
126	struct ifreq *const ifr = (struct ifreq *)data;
127	int s, error = 0;
128
129#ifdef DEBUG
130	ng_eiface_print_ioctl(ifp, command, data);
131#endif
132	s = splimp();
133	switch (command) {
134
135	/* These two are mostly handled at a higher layer */
136	case SIOCSIFADDR:
137		error = ether_ioctl(ifp, command, data);
138		break;
139	case SIOCGIFADDR:
140		break;
141
142	/* Set flags */
143	case SIOCSIFFLAGS:
144		/*
145		 * If the interface is marked up and stopped, then start it.
146		 * If it is marked down and running, then stop it.
147		 */
148		if (ifp->if_flags & IFF_UP) {
149			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
150				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
151				ifp->if_drv_flags |= IFF_DRV_RUNNING;
152			}
153		} else {
154			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
155				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
156				    IFF_DRV_OACTIVE);
157		}
158		break;
159
160	/* Set the interface MTU */
161	case SIOCSIFMTU:
162		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
163		    ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
164			error = EINVAL;
165		else
166			ifp->if_mtu = ifr->ifr_mtu;
167		break;
168
169	/* Stuff that's not supported */
170	case SIOCADDMULTI:
171	case SIOCDELMULTI:
172		error = 0;
173		break;
174	case SIOCSIFPHYS:
175		error = EOPNOTSUPP;
176		break;
177
178	default:
179		error = EINVAL;
180		break;
181	}
182	splx(s);
183	return (error);
184}
185
186static void
187ng_eiface_init(void *xsc)
188{
189	priv_p sc = xsc;
190	struct ifnet *ifp = sc->ifp;
191	int s;
192
193	s = splimp();
194
195	ifp->if_drv_flags |= IFF_DRV_RUNNING;
196	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
197
198	splx(s);
199}
200
201/*
202 * We simply relay the packet to the "ether" hook, if it is connected.
203 * We have been through the netgraph locking and are guaranteed to
204 * be the only code running in this node at this time.
205 */
206static void
207ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
208{
209	struct ifnet *ifp = arg1;
210	const priv_p priv = (priv_p)ifp->if_softc;
211	int error = 0;
212	struct mbuf *m;
213
214	/* Check interface flags */
215
216	if (!((ifp->if_flags & IFF_UP) &&
217	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
218		return;
219
220	for (;;) {
221		/*
222		 * Grab a packet to transmit.
223		 */
224		IF_DEQUEUE(&ifp->if_snd, m);
225
226		/* If there's nothing to send, break. */
227		if (m == NULL)
228			break;
229
230		/*
231		 * Berkeley packet filter.
232		 * Pass packet to bpf if there is a listener.
233		 * XXX is this safe? locking?
234		 */
235		BPF_MTAP(ifp, m);
236
237		/*
238		 * Send packet; if hook is not connected, mbuf will get
239		 * freed.
240		 */
241		NG_SEND_DATA_ONLY(error, priv->ether, m);
242
243		/* Update stats */
244		if (error == 0)
245			ifp->if_opackets++;
246		else
247			ifp->if_oerrors++;
248	}
249
250	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
251
252	return;
253}
254
255/*
256 * This routine is called to deliver a packet out the interface.
257 * We simply queue the netgraph version to be called when netgraph locking
258 * allows it to happen.
259 * Until we know what the rest of the networking code is doing for
260 * locking, we don't know how we will interact with it.
261 * Take comfort from the fact that the ifnet struct is part of our
262 * private info and can't go away while we are queued.
263 * [Though we don't know it is still there now....]
264 * it is possible we don't gain anything from this because
265 * we would like to get the mbuf and queue it as data
266 * somehow, but we can't and if we did would we solve anything?
267 */
268static void
269ng_eiface_start(struct ifnet *ifp)
270{
271
272	const priv_p priv = (priv_p)ifp->if_softc;
273
274	/* Don't do anything if output is active */
275	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
276		return;
277
278	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
279
280	if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
281		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
282}
283
284#ifdef DEBUG
285/*
286 * Display an ioctl to the virtual interface
287 */
288
289static void
290ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
291{
292	char *str;
293
294	switch (command & IOC_DIRMASK) {
295	case IOC_VOID:
296		str = "IO";
297		break;
298	case IOC_OUT:
299		str = "IOR";
300		break;
301	case IOC_IN:
302		str = "IOW";
303		break;
304	case IOC_INOUT:
305		str = "IORW";
306		break;
307	default:
308		str = "IO??";
309	}
310	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
311	    ifp->if_xname,
312	    str,
313	    IOCGROUP(command),
314	    command & 0xff,
315	    IOCPARM_LEN(command));
316}
317#endif /* DEBUG */
318
319/************************************************************************
320			NETGRAPH NODE STUFF
321 ************************************************************************/
322
323/*
324 * Constructor for a node
325 */
326static int
327ng_eiface_constructor(node_p node)
328{
329	struct ifnet *ifp;
330	priv_p priv;
331	u_char eaddr[6] = {0,0,0,0,0,0};
332
333	/* Allocate node and interface private structures */
334	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
335	if (priv == NULL)
336		return (ENOMEM);
337
338	ifp = priv->ifp = if_alloc(IFT_ETHER);
339	if (ifp == NULL) {
340		free(priv, M_NETGRAPH);
341		return (ENOSPC);
342	}
343
344	/* Link them together */
345	ifp->if_softc = priv;
346
347	/* Get an interface unit number */
348	priv->unit = alloc_unr(ng_eiface_unit);
349
350	/* Link together node and private info */
351	NG_NODE_SET_PRIVATE(node, priv);
352	priv->node = node;
353
354	/* Initialize interface structure */
355	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
356	ifp->if_init = ng_eiface_init;
357	ifp->if_output = ether_output;
358	ifp->if_start = ng_eiface_start;
359	ifp->if_ioctl = ng_eiface_ioctl;
360	ifp->if_watchdog = NULL;
361	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
362	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
363
364#if 0
365	/* Give this node name */
366	bzero(ifname, sizeof(ifname));
367	sprintf(ifname, "if%s", ifp->if_xname);
368	(void)ng_name_node(node, ifname);
369#endif
370
371	/* Attach the interface */
372	ether_ifattach(ifp, eaddr);
373
374	/* Done */
375	return (0);
376}
377
378/*
379 * Give our ok for a hook to be added
380 */
381static int
382ng_eiface_newhook(node_p node, hook_p hook, const char *name)
383{
384	priv_p priv = NG_NODE_PRIVATE(node);
385	struct ifnet *ifp = priv->ifp;
386
387	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
388		return (EPFNOSUPPORT);
389	if (priv->ether != NULL)
390		return (EISCONN);
391	priv->ether = hook;
392	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
393
394	if_link_state_change(ifp, LINK_STATE_UP);
395
396	return (0);
397}
398
399/*
400 * Receive a control message
401 */
402static int
403ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
404{
405	const priv_p priv = NG_NODE_PRIVATE(node);
406	struct ifnet *const ifp = priv->ifp;
407	struct ng_mesg *resp = NULL;
408	int error = 0;
409	struct ng_mesg *msg;
410
411	NGI_GET_MSG(item, msg);
412	switch (msg->header.typecookie) {
413	case NGM_EIFACE_COOKIE:
414		switch (msg->header.cmd) {
415
416		case NGM_EIFACE_SET:
417		    {
418			if (msg->header.arglen != ETHER_ADDR_LEN) {
419				error = EINVAL;
420				break;
421			}
422			error = if_setlladdr(priv->ifp,
423			    (u_char *)msg->data, ETHER_ADDR_LEN);
424			break;
425		    }
426
427		case NGM_EIFACE_GET_IFNAME:
428			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
429			if (resp == NULL) {
430				error = ENOMEM;
431				break;
432			}
433			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
434			break;
435
436		case NGM_EIFACE_GET_IFADDRS:
437		    {
438			struct ifaddr *ifa;
439			caddr_t ptr;
440			int buflen;
441
442#define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
443
444			/* Determine size of response and allocate it */
445			buflen = 0;
446			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
447				buflen += SA_SIZE(ifa->ifa_addr);
448			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
449			if (resp == NULL) {
450				error = ENOMEM;
451				break;
452			}
453
454			/* Add addresses */
455			ptr = resp->data;
456			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
457				const int len = SA_SIZE(ifa->ifa_addr);
458
459				if (buflen < len) {
460					log(LOG_ERR, "%s: len changed?\n",
461					    ifp->if_xname);
462					break;
463				}
464				bcopy(ifa->ifa_addr, ptr, len);
465				ptr += len;
466				buflen -= len;
467			}
468			break;
469#undef SA_SIZE
470		    }
471
472		default:
473			error = EINVAL;
474			break;
475		} /* end of inner switch() */
476		break;
477	case NGM_FLOW_COOKIE:
478		switch (msg->header.cmd) {
479		case NGM_LINK_IS_UP:
480			if_link_state_change(ifp, LINK_STATE_UP);
481			break;
482		case NGM_LINK_IS_DOWN:
483			if_link_state_change(ifp, LINK_STATE_DOWN);
484			break;
485		default:
486			break;
487		}
488		break;
489	default:
490		error = EINVAL;
491		break;
492	}
493	NG_RESPOND_MSG(error, node, item, resp);
494	NG_FREE_MSG(msg);
495	return (error);
496}
497
498/*
499 * Receive data from a hook. Pass the packet to the ether_input routine.
500 */
501static int
502ng_eiface_rcvdata(hook_p hook, item_p item)
503{
504	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
505	struct ifnet *const ifp = priv->ifp;
506	struct mbuf *m;
507
508	NGI_GET_M(item, m);
509	NG_FREE_ITEM(item);
510
511	if (!((ifp->if_flags & IFF_UP) &&
512	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
513		NG_FREE_M(m);
514		return (ENETDOWN);
515	}
516
517	if (m->m_len < ETHER_HDR_LEN) {
518		m = m_pullup(m, ETHER_HDR_LEN);
519		if (m == NULL)
520			return (EINVAL);
521	}
522
523	/* Note receiving interface */
524	m->m_pkthdr.rcvif = ifp;
525
526	/* Update interface stats */
527	ifp->if_ipackets++;
528
529	(*ifp->if_input)(ifp, m);
530
531	/* Done */
532	return (0);
533}
534
535/*
536 * Shutdown processing.
537 */
538static int
539ng_eiface_rmnode(node_p node)
540{
541	const priv_p priv = NG_NODE_PRIVATE(node);
542	struct ifnet *const ifp = priv->ifp;
543
544	ether_ifdetach(ifp);
545	if_free(ifp);
546	free_unr(ng_eiface_unit, priv->unit);
547	FREE(priv, M_NETGRAPH);
548	NG_NODE_SET_PRIVATE(node, NULL);
549	NG_NODE_UNREF(node);
550	return (0);
551}
552
553/*
554 * Hook disconnection
555 */
556static int
557ng_eiface_disconnect(hook_p hook)
558{
559	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
560
561	priv->ether = NULL;
562	return (0);
563}
564
565/*
566 * Handle loading and unloading for this node type.
567 */
568static int
569ng_eiface_mod_event(module_t mod, int event, void *data)
570{
571	int error = 0;
572
573	switch (event) {
574	case MOD_LOAD:
575		ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
576		break;
577	case MOD_UNLOAD:
578		delete_unrhdr(ng_eiface_unit);
579		break;
580	default:
581		error = EOPNOTSUPP;
582		break;
583	}
584	return (error);
585}
586