ng_eiface.c revision 195727
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 195727 2009-07-16 21:13:04Z rwatson $
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#include <sys/vimage.h>
42
43#include <net/if.h>
44#include <net/if_types.h>
45#include <net/netisr.h>
46#include <net/route.h>
47
48#include <netgraph/ng_message.h>
49#include <netgraph/netgraph.h>
50#include <netgraph/ng_parse.h>
51#include <netgraph/ng_eiface.h>
52
53#include <net/bpf.h>
54#include <net/ethernet.h>
55#include <net/if_arp.h>
56
57static const struct ng_cmdlist ng_eiface_cmdlist[] = {
58	{
59	  NGM_EIFACE_COOKIE,
60	  NGM_EIFACE_GET_IFNAME,
61	  "getifname",
62	  NULL,
63	  &ng_parse_string_type
64	},
65	{
66	  NGM_EIFACE_COOKIE,
67	  NGM_EIFACE_SET,
68	  "set",
69	  &ng_parse_enaddr_type,
70	  NULL
71	},
72	{ 0 }
73};
74
75/* Node private data */
76struct ng_eiface_private {
77	struct ifnet	*ifp;		/* per-interface network data */
78	int		unit;		/* Interface unit number */
79	node_p		node;		/* Our netgraph node */
80	hook_p		ether;		/* Hook for ethernet stream */
81};
82typedef struct ng_eiface_private *priv_p;
83
84/* Interface methods */
85static void	ng_eiface_init(void *xsc);
86static void	ng_eiface_start(struct ifnet *ifp);
87static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
88#ifdef DEBUG
89static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
90#endif
91
92/* Netgraph methods */
93static int		ng_eiface_mod_event(module_t, int, void *);
94static ng_constructor_t	ng_eiface_constructor;
95static ng_rcvmsg_t	ng_eiface_rcvmsg;
96static ng_shutdown_t	ng_eiface_rmnode;
97static ng_newhook_t	ng_eiface_newhook;
98static ng_rcvdata_t	ng_eiface_rcvdata;
99static ng_disconnect_t	ng_eiface_disconnect;
100
101/* Node type descriptor */
102static struct ng_type typestruct = {
103	.version =	NG_ABI_VERSION,
104	.name =		NG_EIFACE_NODE_TYPE,
105	.mod_event =	ng_eiface_mod_event,
106	.constructor =	ng_eiface_constructor,
107	.rcvmsg =	ng_eiface_rcvmsg,
108	.shutdown =	ng_eiface_rmnode,
109	.newhook =	ng_eiface_newhook,
110	.rcvdata =	ng_eiface_rcvdata,
111	.disconnect =	ng_eiface_disconnect,
112	.cmdlist =	ng_eiface_cmdlist
113};
114NETGRAPH_INIT(eiface, &typestruct);
115
116static vnet_attach_fn ng_eiface_iattach;
117static vnet_detach_fn ng_eiface_idetach;
118
119static VNET_DEFINE(struct unrhdr *, ng_eiface_unit);
120#define	V_ng_eiface_unit		VNET(ng_eiface_unit)
121
122#ifdef VIMAGE
123static vnet_modinfo_t vnet_ng_eiface_modinfo = {
124	.vmi_id		= VNET_MOD_NG_EIFACE,
125	.vmi_name	= "ng_eiface",
126	.vmi_dependson	= VNET_MOD_NETGRAPH,
127	.vmi_iattach	= ng_eiface_iattach,
128	.vmi_idetach	= ng_eiface_idetach
129};
130#endif
131
132/************************************************************************
133			INTERFACE STUFF
134 ************************************************************************/
135
136/*
137 * Process an ioctl for the virtual interface
138 */
139static int
140ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
141{
142	struct ifreq *const ifr = (struct ifreq *)data;
143	int s, error = 0;
144
145#ifdef DEBUG
146	ng_eiface_print_ioctl(ifp, command, data);
147#endif
148	s = splimp();
149	switch (command) {
150
151	/* These two are mostly handled at a higher layer */
152	case SIOCSIFADDR:
153		error = ether_ioctl(ifp, command, data);
154		break;
155	case SIOCGIFADDR:
156		break;
157
158	/* Set flags */
159	case SIOCSIFFLAGS:
160		/*
161		 * If the interface is marked up and stopped, then start it.
162		 * If it is marked down and running, then stop it.
163		 */
164		if (ifp->if_flags & IFF_UP) {
165			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
166				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
167				ifp->if_drv_flags |= IFF_DRV_RUNNING;
168			}
169		} else {
170			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
171				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
172				    IFF_DRV_OACTIVE);
173		}
174		break;
175
176	/* Set the interface MTU */
177	case SIOCSIFMTU:
178		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
179		    ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
180			error = EINVAL;
181		else
182			ifp->if_mtu = ifr->ifr_mtu;
183		break;
184
185	/* Stuff that's not supported */
186	case SIOCADDMULTI:
187	case SIOCDELMULTI:
188		error = 0;
189		break;
190	case SIOCSIFPHYS:
191		error = EOPNOTSUPP;
192		break;
193
194	default:
195		error = EINVAL;
196		break;
197	}
198	splx(s);
199	return (error);
200}
201
202static void
203ng_eiface_init(void *xsc)
204{
205	priv_p sc = xsc;
206	struct ifnet *ifp = sc->ifp;
207	int s;
208
209	s = splimp();
210
211	ifp->if_drv_flags |= IFF_DRV_RUNNING;
212	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
213
214	splx(s);
215}
216
217/*
218 * We simply relay the packet to the "ether" hook, if it is connected.
219 * We have been through the netgraph locking and are guaranteed to
220 * be the only code running in this node at this time.
221 */
222static void
223ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
224{
225	struct ifnet *ifp = arg1;
226	const priv_p priv = (priv_p)ifp->if_softc;
227	int error = 0;
228	struct mbuf *m;
229
230	/* Check interface flags */
231
232	if (!((ifp->if_flags & IFF_UP) &&
233	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
234		return;
235
236	for (;;) {
237		/*
238		 * Grab a packet to transmit.
239		 */
240		IF_DEQUEUE(&ifp->if_snd, m);
241
242		/* If there's nothing to send, break. */
243		if (m == NULL)
244			break;
245
246		/*
247		 * Berkeley packet filter.
248		 * Pass packet to bpf if there is a listener.
249		 * XXX is this safe? locking?
250		 */
251		BPF_MTAP(ifp, m);
252
253		if (ifp->if_flags & IFF_MONITOR) {
254			ifp->if_ipackets++;
255			m_freem(m);
256			continue;
257		}
258
259		/*
260		 * Send packet; if hook is not connected, mbuf will get
261		 * freed.
262		 */
263		NG_OUTBOUND_THREAD_REF();
264		NG_SEND_DATA_ONLY(error, priv->ether, m);
265		NG_OUTBOUND_THREAD_UNREF();
266
267		/* Update stats */
268		if (error == 0)
269			ifp->if_opackets++;
270		else
271			ifp->if_oerrors++;
272	}
273
274	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
275
276	return;
277}
278
279/*
280 * This routine is called to deliver a packet out the interface.
281 * We simply queue the netgraph version to be called when netgraph locking
282 * allows it to happen.
283 * Until we know what the rest of the networking code is doing for
284 * locking, we don't know how we will interact with it.
285 * Take comfort from the fact that the ifnet struct is part of our
286 * private info and can't go away while we are queued.
287 * [Though we don't know it is still there now....]
288 * it is possible we don't gain anything from this because
289 * we would like to get the mbuf and queue it as data
290 * somehow, but we can't and if we did would we solve anything?
291 */
292static void
293ng_eiface_start(struct ifnet *ifp)
294{
295
296	const priv_p priv = (priv_p)ifp->if_softc;
297
298	/* Don't do anything if output is active */
299	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
300		return;
301
302	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
303
304	if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
305		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
306}
307
308#ifdef DEBUG
309/*
310 * Display an ioctl to the virtual interface
311 */
312
313static void
314ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
315{
316	char *str;
317
318	switch (command & IOC_DIRMASK) {
319	case IOC_VOID:
320		str = "IO";
321		break;
322	case IOC_OUT:
323		str = "IOR";
324		break;
325	case IOC_IN:
326		str = "IOW";
327		break;
328	case IOC_INOUT:
329		str = "IORW";
330		break;
331	default:
332		str = "IO??";
333	}
334	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
335	    ifp->if_xname,
336	    str,
337	    IOCGROUP(command),
338	    command & 0xff,
339	    IOCPARM_LEN(command));
340}
341#endif /* DEBUG */
342
343/************************************************************************
344			NETGRAPH NODE STUFF
345 ************************************************************************/
346
347/*
348 * Constructor for a node
349 */
350static int
351ng_eiface_constructor(node_p node)
352{
353	struct ifnet *ifp;
354	priv_p priv;
355	u_char eaddr[6] = {0,0,0,0,0,0};
356
357	/* Allocate node and interface private structures */
358	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
359	if (priv == NULL)
360		return (ENOMEM);
361
362	ifp = priv->ifp = if_alloc(IFT_ETHER);
363	if (ifp == NULL) {
364		free(priv, M_NETGRAPH);
365		return (ENOSPC);
366	}
367
368	/* Link them together */
369	ifp->if_softc = priv;
370
371	/* Get an interface unit number */
372	priv->unit = alloc_unr(V_ng_eiface_unit);
373
374	/* Link together node and private info */
375	NG_NODE_SET_PRIVATE(node, priv);
376	priv->node = node;
377
378	/* Initialize interface structure */
379	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
380	ifp->if_init = ng_eiface_init;
381	ifp->if_output = ether_output;
382	ifp->if_start = ng_eiface_start;
383	ifp->if_ioctl = ng_eiface_ioctl;
384	ifp->if_watchdog = NULL;
385	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
386	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
387
388	/* Give this node the same name as the interface (if possible) */
389	if (ng_name_node(node, ifp->if_xname) != 0)
390		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
391		    ifp->if_xname);
392
393	/* Attach the interface */
394	ether_ifattach(ifp, eaddr);
395
396	/* Done */
397	return (0);
398}
399
400/*
401 * Give our ok for a hook to be added
402 */
403static int
404ng_eiface_newhook(node_p node, hook_p hook, const char *name)
405{
406	priv_p priv = NG_NODE_PRIVATE(node);
407	struct ifnet *ifp = priv->ifp;
408
409	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
410		return (EPFNOSUPPORT);
411	if (priv->ether != NULL)
412		return (EISCONN);
413	priv->ether = hook;
414	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
415	NG_HOOK_SET_TO_INBOUND(hook);
416
417	if_link_state_change(ifp, LINK_STATE_UP);
418
419	return (0);
420}
421
422/*
423 * Receive a control message
424 */
425static int
426ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
427{
428	const priv_p priv = NG_NODE_PRIVATE(node);
429	struct ifnet *const ifp = priv->ifp;
430	struct ng_mesg *resp = NULL;
431	int error = 0;
432	struct ng_mesg *msg;
433
434	NGI_GET_MSG(item, msg);
435	switch (msg->header.typecookie) {
436	case NGM_EIFACE_COOKIE:
437		switch (msg->header.cmd) {
438
439		case NGM_EIFACE_SET:
440		    {
441			if (msg->header.arglen != ETHER_ADDR_LEN) {
442				error = EINVAL;
443				break;
444			}
445			error = if_setlladdr(priv->ifp,
446			    (u_char *)msg->data, ETHER_ADDR_LEN);
447			break;
448		    }
449
450		case NGM_EIFACE_GET_IFNAME:
451			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
452			if (resp == NULL) {
453				error = ENOMEM;
454				break;
455			}
456			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
457			break;
458
459		case NGM_EIFACE_GET_IFADDRS:
460		    {
461			struct ifaddr *ifa;
462			caddr_t ptr;
463			int buflen;
464
465			/* Determine size of response and allocate it */
466			buflen = 0;
467			if_addr_rlock(ifp);
468			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
469				buflen += SA_SIZE(ifa->ifa_addr);
470			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
471			if (resp == NULL) {
472				if_addr_runlock(ifp);
473				error = ENOMEM;
474				break;
475			}
476
477			/* Add addresses */
478			ptr = resp->data;
479			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
480				const int len = SA_SIZE(ifa->ifa_addr);
481
482				if (buflen < len) {
483					log(LOG_ERR, "%s: len changed?\n",
484					    ifp->if_xname);
485					break;
486				}
487				bcopy(ifa->ifa_addr, ptr, len);
488				ptr += len;
489				buflen -= len;
490			}
491			if_addr_runlock(ifp);
492			break;
493		    }
494
495		default:
496			error = EINVAL;
497			break;
498		} /* end of inner switch() */
499		break;
500	case NGM_FLOW_COOKIE:
501		switch (msg->header.cmd) {
502		case NGM_LINK_IS_UP:
503			if_link_state_change(ifp, LINK_STATE_UP);
504			break;
505		case NGM_LINK_IS_DOWN:
506			if_link_state_change(ifp, LINK_STATE_DOWN);
507			break;
508		default:
509			break;
510		}
511		break;
512	default:
513		error = EINVAL;
514		break;
515	}
516	NG_RESPOND_MSG(error, node, item, resp);
517	NG_FREE_MSG(msg);
518	return (error);
519}
520
521/*
522 * Receive data from a hook. Pass the packet to the ether_input routine.
523 */
524static int
525ng_eiface_rcvdata(hook_p hook, item_p item)
526{
527	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
528	struct ifnet *const ifp = priv->ifp;
529	struct mbuf *m;
530
531	NGI_GET_M(item, m);
532	NG_FREE_ITEM(item);
533
534	if (!((ifp->if_flags & IFF_UP) &&
535	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
536		NG_FREE_M(m);
537		return (ENETDOWN);
538	}
539
540	if (m->m_len < ETHER_HDR_LEN) {
541		m = m_pullup(m, ETHER_HDR_LEN);
542		if (m == NULL)
543			return (EINVAL);
544	}
545
546	/* Note receiving interface */
547	m->m_pkthdr.rcvif = ifp;
548
549	/* Update interface stats */
550	ifp->if_ipackets++;
551
552	(*ifp->if_input)(ifp, m);
553
554	/* Done */
555	return (0);
556}
557
558/*
559 * Shutdown processing.
560 */
561static int
562ng_eiface_rmnode(node_p node)
563{
564	const priv_p priv = NG_NODE_PRIVATE(node);
565	struct ifnet *const ifp = priv->ifp;
566
567	/*
568	 * the ifnet may be in a different vnet than the netgraph node,
569	 * hence we have to change the current vnet context here.
570	 */
571	CURVNET_SET_QUIET(ifp->if_vnet);
572	ether_ifdetach(ifp);
573	if_free(ifp);
574	CURVNET_RESTORE();
575	free_unr(V_ng_eiface_unit, priv->unit);
576	free(priv, M_NETGRAPH);
577	NG_NODE_SET_PRIVATE(node, NULL);
578	NG_NODE_UNREF(node);
579	return (0);
580}
581
582/*
583 * Hook disconnection
584 */
585static int
586ng_eiface_disconnect(hook_p hook)
587{
588	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
589
590	priv->ether = NULL;
591	return (0);
592}
593
594/*
595 * Handle loading and unloading for this node type.
596 */
597static int
598ng_eiface_mod_event(module_t mod, int event, void *data)
599{
600	int error = 0;
601
602	switch (event) {
603	case MOD_LOAD:
604#ifdef VIMAGE
605		vnet_mod_register(&vnet_ng_eiface_modinfo);
606#else
607		ng_eiface_iattach(NULL);
608#endif
609		break;
610	case MOD_UNLOAD:
611#ifdef VIMAGE
612		vnet_mod_deregister(&vnet_ng_eiface_modinfo);
613#else
614		ng_eiface_idetach(NULL);
615#endif
616		break;
617	default:
618		error = EOPNOTSUPP;
619		break;
620	}
621	return (error);
622}
623
624static int ng_eiface_iattach(const void *unused)
625{
626
627	V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
628
629	return (0);
630}
631
632static int ng_eiface_idetach(const void *unused)
633{
634
635	delete_unrhdr(V_ng_eiface_unit);
636
637	return (0);
638}
639