ng_eiface.c revision 155554
1285101Semaste/*-
2285101Semaste *
3285101Semaste * Copyright (c) 1999-2001, Vitaly V Belekhov
4285101Semaste * All rights reserved.
5285101Semaste *
6285101Semaste * Redistribution and use in source and binary forms, with or without
7285101Semaste * modification, are permitted provided that the following conditions
8285101Semaste * are met:
9285101Semaste * 1. Redistributions of source code must retain the above copyright
10285101Semaste *    notice unmodified, this list of conditions, and the following
11285101Semaste *    disclaimer.
12285101Semaste * 2. Redistributions in binary form must reproduce the above copyright
13285101Semaste *    notice, this list of conditions and the following disclaimer in the
14285101Semaste *    documentation and/or other materials provided with the distribution.
15285101Semaste *
16285101Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17285101Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18285101Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19285101Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20285101Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21285101Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22285101Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23285101Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24285101Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25285101Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26285101Semaste * SUCH DAMAGE.
27285101Semaste *
28285101Semaste * $FreeBSD: head/sys/netgraph/ng_eiface.c 155554 2006-02-11 20:25:00Z ru $
29285101Semaste */
30285101Semaste
31285101Semaste#include <sys/param.h>
32285101Semaste#include <sys/systm.h>
33285101Semaste#include <sys/errno.h>
34285101Semaste#include <sys/kernel.h>
35285101Semaste#include <sys/malloc.h>
36285101Semaste#include <sys/mbuf.h>
37285101Semaste#include <sys/errno.h>
38285101Semaste#include <sys/sockio.h>
39285101Semaste#include <sys/socket.h>
40285101Semaste#include <sys/syslog.h>
41285101Semaste
42285101Semaste#include <net/if.h>
43285101Semaste#include <net/if_types.h>
44285101Semaste#include <net/netisr.h>
45285101Semaste
46296417Sdim#include <netgraph/ng_message.h>
47285101Semaste#include <netgraph/netgraph.h>
48285101Semaste#include <netgraph/ng_parse.h>
49285101Semaste#include <netgraph/ng_eiface.h>
50285101Semaste
51285101Semaste#include <net/bpf.h>
52285101Semaste#include <net/ethernet.h>
53285101Semaste#include <net/if_arp.h>
54285101Semaste
55285101Semastestatic const struct ng_cmdlist ng_eiface_cmdlist[] = {
56285101Semaste	{
57285101Semaste	  NGM_EIFACE_COOKIE,
58285101Semaste	  NGM_EIFACE_GET_IFNAME,
59285101Semaste	  "getifname",
60285101Semaste	  NULL,
61285101Semaste	  &ng_parse_string_type
62285101Semaste	},
63285101Semaste	{
64285101Semaste	  NGM_EIFACE_COOKIE,
65285101Semaste	  NGM_EIFACE_SET,
66285101Semaste	  "set",
67285101Semaste	  &ng_parse_enaddr_type,
68285101Semaste	  NULL
69285101Semaste	},
70285101Semaste	{ 0 }
71285101Semaste};
72285101Semaste
73285101Semaste/* Node private data */
74285101Semastestruct ng_eiface_private {
75285101Semaste	struct ifnet	*ifp;		/* per-interface network data */
76285101Semaste	int		unit;		/* Interface unit number */
77285101Semaste	node_p		node;		/* Our netgraph node */
78285101Semaste	hook_p		ether;		/* Hook for ethernet stream */
79285101Semaste};
80285101Semastetypedef struct ng_eiface_private *priv_p;
81285101Semaste
82285101Semaste/* Interface methods */
83285101Semastestatic void	ng_eiface_init(void *xsc);
84285101Semastestatic void	ng_eiface_start(struct ifnet *ifp);
85285101Semastestatic int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
86285101Semaste#ifdef DEBUG
87285101Semastestatic void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
88285101Semaste#endif
89285101Semaste
90285101Semaste/* Netgraph methods */
91285101Semastestatic int		ng_eiface_mod_event(module_t, int, void *);
92285101Semastestatic ng_constructor_t	ng_eiface_constructor;
93285101Semastestatic ng_rcvmsg_t	ng_eiface_rcvmsg;
94285101Semastestatic ng_shutdown_t	ng_eiface_rmnode;
95285101Semastestatic ng_newhook_t	ng_eiface_newhook;
96285101Semastestatic ng_rcvdata_t	ng_eiface_rcvdata;
97285101Semastestatic ng_disconnect_t	ng_eiface_disconnect;
98285101Semaste
99285101Semaste/* Node type descriptor */
100285101Semastestatic struct ng_type typestruct = {
101285101Semaste	.version =	NG_ABI_VERSION,
102285101Semaste	.name =		NG_EIFACE_NODE_TYPE,
103285101Semaste	.mod_event =	ng_eiface_mod_event,
104285101Semaste	.constructor =	ng_eiface_constructor,
105285101Semaste	.rcvmsg =	ng_eiface_rcvmsg,
106285101Semaste	.shutdown =	ng_eiface_rmnode,
107285101Semaste	.newhook =	ng_eiface_newhook,
108285101Semaste	.rcvdata =	ng_eiface_rcvdata,
109285101Semaste	.disconnect =	ng_eiface_disconnect,
110285101Semaste	.cmdlist =	ng_eiface_cmdlist
111285101Semaste};
112285101SemasteNETGRAPH_INIT(eiface, &typestruct);
113285101Semaste
114285101Semastestatic struct unrhdr	*ng_eiface_unit;
115285101Semaste
116285101Semaste/************************************************************************
117285101Semaste			INTERFACE STUFF
118285101Semaste ************************************************************************/
119285101Semaste
120285101Semaste/*
121285101Semaste * Process an ioctl for the virtual interface
122285101Semaste */
123285101Semastestatic int
124285101Semasteng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
125285101Semaste{
126285101Semaste	struct ifreq *const ifr = (struct ifreq *)data;
127285101Semaste	int s, error = 0;
128285101Semaste
129285101Semaste#ifdef DEBUG
130285101Semaste	ng_eiface_print_ioctl(ifp, command, data);
131285101Semaste#endif
132285101Semaste	s = splimp();
133285101Semaste	switch (command) {
134285101Semaste
135285101Semaste	/* These two are mostly handled at a higher layer */
136285101Semaste	case SIOCSIFADDR:
137285101Semaste		error = ether_ioctl(ifp, command, data);
138285101Semaste		break;
139285101Semaste	case SIOCGIFADDR:
140285101Semaste		break;
141285101Semaste
142285101Semaste	/* Set flags */
143285101Semaste	case SIOCSIFFLAGS:
144285101Semaste		/*
145285101Semaste		 * 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 (ifr->ifr_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	ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
281}
282
283#ifdef DEBUG
284/*
285 * Display an ioctl to the virtual interface
286 */
287
288static void
289ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
290{
291	char *str;
292
293	switch (command & IOC_DIRMASK) {
294	case IOC_VOID:
295		str = "IO";
296		break;
297	case IOC_OUT:
298		str = "IOR";
299		break;
300	case IOC_IN:
301		str = "IOW";
302		break;
303	case IOC_INOUT:
304		str = "IORW";
305		break;
306	default:
307		str = "IO??";
308	}
309	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
310	    ifp->if_xname,
311	    str,
312	    IOCGROUP(command),
313	    command & 0xff,
314	    IOCPARM_LEN(command));
315}
316#endif /* DEBUG */
317
318/************************************************************************
319			NETGRAPH NODE STUFF
320 ************************************************************************/
321
322/*
323 * Constructor for a node
324 */
325static int
326ng_eiface_constructor(node_p node)
327{
328	struct ifnet *ifp;
329	priv_p priv;
330	u_char eaddr[6] = {0,0,0,0,0,0};
331
332	/* Allocate node and interface private structures */
333	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
334	if (priv == NULL)
335		return (ENOMEM);
336
337	ifp = priv->ifp = if_alloc(IFT_ETHER);
338	if (ifp == NULL) {
339		free(priv, M_NETGRAPH);
340		return (ENOSPC);
341	}
342
343	/* Link them together */
344	ifp->if_softc = priv;
345
346	/* Get an interface unit number */
347	priv->unit = alloc_unr(ng_eiface_unit);
348
349	/* Link together node and private info */
350	NG_NODE_SET_PRIVATE(node, priv);
351	priv->node = node;
352
353	/* Initialize interface structure */
354	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
355	ifp->if_init = ng_eiface_init;
356	ifp->if_output = ether_output;
357	ifp->if_start = ng_eiface_start;
358	ifp->if_ioctl = ng_eiface_ioctl;
359	ifp->if_watchdog = NULL;
360	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
361	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
362
363#if 0
364	/* Give this node name */
365	bzero(ifname, sizeof(ifname));
366	sprintf(ifname, "if%s", ifp->if_xname);
367	(void)ng_name_node(node, ifname);
368#endif
369
370	/* Attach the interface */
371	ether_ifattach(ifp, eaddr);
372
373	/* Done */
374	return (0);
375}
376
377/*
378 * Give our ok for a hook to be added
379 */
380static int
381ng_eiface_newhook(node_p node, hook_p hook, const char *name)
382{
383	priv_p priv = NG_NODE_PRIVATE(node);
384	struct ifnet *ifp = priv->ifp;
385
386	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
387		return (EPFNOSUPPORT);
388	if (priv->ether != NULL)
389		return (EISCONN);
390	priv->ether = hook;
391	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
392
393	if_link_state_change(ifp, LINK_STATE_UP);
394
395	return (0);
396}
397
398/*
399 * Receive a control message
400 */
401static int
402ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
403{
404	const priv_p priv = NG_NODE_PRIVATE(node);
405	struct ifnet *const ifp = priv->ifp;
406	struct ng_mesg *resp = NULL;
407	int error = 0;
408	struct ng_mesg *msg;
409
410	NGI_GET_MSG(item, msg);
411	switch (msg->header.typecookie) {
412	case NGM_EIFACE_COOKIE:
413		switch (msg->header.cmd) {
414
415		case NGM_EIFACE_SET:
416		    {
417			if (msg->header.arglen != ETHER_ADDR_LEN) {
418				error = EINVAL;
419				break;
420			}
421			error = if_setlladdr(priv->ifp,
422			    (u_char *)msg->data, ETHER_ADDR_LEN);
423			break;
424		    }
425
426		case NGM_EIFACE_GET_IFNAME:
427			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
428			if (resp == NULL) {
429				error = ENOMEM;
430				break;
431			}
432			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
433			break;
434
435		case NGM_EIFACE_GET_IFADDRS:
436		    {
437			struct ifaddr *ifa;
438			caddr_t ptr;
439			int buflen;
440
441#define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
442
443			/* Determine size of response and allocate it */
444			buflen = 0;
445			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
446				buflen += SA_SIZE(ifa->ifa_addr);
447			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
448			if (resp == NULL) {
449				error = ENOMEM;
450				break;
451			}
452
453			/* Add addresses */
454			ptr = resp->data;
455			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
456				const int len = SA_SIZE(ifa->ifa_addr);
457
458				if (buflen < len) {
459					log(LOG_ERR, "%s: len changed?\n",
460					    ifp->if_xname);
461					break;
462				}
463				bcopy(ifa->ifa_addr, ptr, len);
464				ptr += len;
465				buflen -= len;
466			}
467			break;
468#undef SA_SIZE
469		    }
470
471		default:
472			error = EINVAL;
473			break;
474		} /* end of inner switch() */
475		break;
476	case NGM_FLOW_COOKIE:
477		switch (msg->header.cmd) {
478		case NGM_LINK_IS_UP:
479			if_link_state_change(ifp, LINK_STATE_UP);
480			break;
481		case NGM_LINK_IS_DOWN:
482			if_link_state_change(ifp, LINK_STATE_DOWN);
483			break;
484		default:
485			break;
486		}
487		break;
488	default:
489		error = EINVAL;
490		break;
491	}
492	NG_RESPOND_MSG(error, node, item, resp);
493	NG_FREE_MSG(msg);
494	return (error);
495}
496
497/*
498 * Receive data from a hook. Pass the packet to the ether_input routine.
499 */
500static int
501ng_eiface_rcvdata(hook_p hook, item_p item)
502{
503	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
504	struct ifnet *const ifp = priv->ifp;
505	struct mbuf *m;
506
507	NGI_GET_M(item, m);
508	NG_FREE_ITEM(item);
509
510	if (!((ifp->if_flags & IFF_UP) &&
511	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
512		NG_FREE_M(m);
513		return (ENETDOWN);
514	}
515
516	if (m->m_len < ETHER_HDR_LEN) {
517		m = m_pullup(m, ETHER_HDR_LEN);
518		if (m == NULL)
519			return (EINVAL);
520	}
521
522	/* Note receiving interface */
523	m->m_pkthdr.rcvif = ifp;
524
525	/* Update interface stats */
526	ifp->if_ipackets++;
527
528	(*ifp->if_input)(ifp, m);
529
530	/* Done */
531	return (0);
532}
533
534/*
535 * Shutdown processing.
536 */
537static int
538ng_eiface_rmnode(node_p node)
539{
540	const priv_p priv = NG_NODE_PRIVATE(node);
541	struct ifnet *const ifp = priv->ifp;
542
543	ether_ifdetach(ifp);
544	if_free(ifp);
545	free_unr(ng_eiface_unit, priv->unit);
546	FREE(priv, M_NETGRAPH);
547	NG_NODE_SET_PRIVATE(node, NULL);
548	NG_NODE_UNREF(node);
549	return (0);
550}
551
552/*
553 * Hook disconnection
554 */
555static int
556ng_eiface_disconnect(hook_p hook)
557{
558	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
559
560	priv->ether = NULL;
561	return (0);
562}
563
564/*
565 * Handle loading and unloading for this node type.
566 */
567static int
568ng_eiface_mod_event(module_t mod, int event, void *data)
569{
570	int error = 0;
571
572	switch (event) {
573	case MOD_LOAD:
574		ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
575		break;
576	case MOD_UNLOAD:
577		delete_unrhdr(ng_eiface_unit);
578		break;
579	default:
580		error = EOPNOTSUPP;
581		break;
582	}
583	return (error);
584}
585