ng_eiface.c revision 141190
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 141190 2005-02-03 11:28: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/lock.h>
36#include <sys/malloc.h>
37#include <sys/mbuf.h>
38#include <sys/mutex.h>
39#include <sys/errno.h>
40#include <sys/sockio.h>
41#include <sys/socket.h>
42#include <sys/syslog.h>
43
44#include <net/if.h>
45#include <net/if_dl.h>
46#include <net/if_types.h>
47#include <net/netisr.h>
48
49#include <netgraph/ng_message.h>
50#include <netgraph/netgraph.h>
51#include <netgraph/ng_parse.h>
52#include <netgraph/ng_eiface.h>
53
54#include <net/bpf.h>
55#include <net/ethernet.h>
56#include <net/if_arp.h>
57
58static const struct ng_cmdlist ng_eiface_cmdlist[] = {
59	{
60	  NGM_EIFACE_COOKIE,
61	  NGM_EIFACE_SET,
62	  "set",
63	  &ng_parse_enaddr_type,
64	  NULL
65	},
66	{ 0 }
67};
68
69/* Node private data */
70struct ng_eiface_private {
71	struct arpcom	arpcom;		/* per-interface network data */
72	struct ifnet	*ifp;		/* This interface */
73	int		unit;		/* Interface unit number */
74	node_p		node;		/* Our netgraph node */
75	hook_p		ether;		/* Hook for ethernet stream */
76};
77typedef struct ng_eiface_private *priv_p;
78
79/* Interface methods */
80static void	ng_eiface_init(void *xsc);
81static void	ng_eiface_start(struct ifnet *ifp);
82static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
83#ifdef DEBUG
84static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
85#endif
86
87/* Netgraph methods */
88static ng_constructor_t	ng_eiface_constructor;
89static ng_rcvmsg_t	ng_eiface_rcvmsg;
90static ng_shutdown_t	ng_eiface_rmnode;
91static ng_newhook_t	ng_eiface_newhook;
92static ng_rcvdata_t	ng_eiface_rcvdata;
93static ng_connect_t	ng_eiface_connect;
94static ng_disconnect_t	ng_eiface_disconnect;
95
96/* Node type descriptor */
97static struct ng_type typestruct = {
98	.version =	NG_ABI_VERSION,
99	.name =		NG_EIFACE_NODE_TYPE,
100	.constructor =	ng_eiface_constructor,
101	.rcvmsg =	ng_eiface_rcvmsg,
102	.shutdown =	ng_eiface_rmnode,
103	.newhook =	ng_eiface_newhook,
104	.connect =	ng_eiface_connect,
105	.rcvdata =	ng_eiface_rcvdata,
106	.disconnect =	ng_eiface_disconnect,
107	.cmdlist =	ng_eiface_cmdlist
108};
109NETGRAPH_INIT(eiface, &typestruct);
110
111/* We keep a bitmap indicating which unit numbers are free.
112   One means the unit number is free, zero means it's taken. */
113static int	*ng_eiface_units = NULL;
114static int	ng_eiface_units_len = 0;
115static int	ng_units_in_use = 0;
116
117#define UNITS_BITSPERWORD	(sizeof(*ng_eiface_units) * NBBY)
118
119static struct mtx	ng_eiface_mtx;
120MTX_SYSINIT(ng_eiface, &ng_eiface_mtx, "ng_eiface", MTX_DEF);
121
122/************************************************************************
123			HELPER STUFF
124 ************************************************************************/
125/*
126 * Find the first free unit number for a new interface.
127 * Increase the size of the unit bitmap as necessary.
128 */
129static __inline int
130ng_eiface_get_unit(int *unit)
131{
132	int index, bit;
133
134	mtx_lock(&ng_eiface_mtx);
135	for (index = 0; index < ng_eiface_units_len
136	    && ng_eiface_units[index] == 0; index++);
137	if (index == ng_eiface_units_len) {		/* extend array */
138		int i, *newarray, newlen;
139
140		newlen = (2 * ng_eiface_units_len) + 4;
141		MALLOC(newarray, int *, newlen * sizeof(*ng_eiface_units),
142		    M_NETGRAPH, M_NOWAIT);
143		if (newarray == NULL) {
144			mtx_unlock(&ng_eiface_mtx);
145			return (ENOMEM);
146		}
147		bcopy(ng_eiface_units, newarray,
148		    ng_eiface_units_len * sizeof(*ng_eiface_units));
149		for (i = ng_eiface_units_len; i < newlen; i++)
150			newarray[i] = ~0;
151		if (ng_eiface_units != NULL)
152			FREE(ng_eiface_units, M_NETGRAPH);
153		ng_eiface_units = newarray;
154		ng_eiface_units_len = newlen;
155	}
156	bit = ffs(ng_eiface_units[index]) - 1;
157	KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
158	    ("%s: word=%d bit=%d", __func__, ng_eiface_units[index], bit));
159	ng_eiface_units[index] &= ~(1 << bit);
160	*unit = (index * UNITS_BITSPERWORD) + bit;
161	ng_units_in_use++;
162	mtx_unlock(&ng_eiface_mtx);
163	return (0);
164}
165
166/*
167 * Free a no longer needed unit number.
168 */
169static __inline void
170ng_eiface_free_unit(int unit)
171{
172	int index, bit;
173
174	index = unit / UNITS_BITSPERWORD;
175	bit = unit % UNITS_BITSPERWORD;
176	mtx_lock(&ng_eiface_mtx);
177	KASSERT(index < ng_eiface_units_len,
178	    ("%s: unit=%d len=%d", __func__, unit, ng_eiface_units_len));
179	KASSERT((ng_eiface_units[index] & (1 << bit)) == 0,
180	    ("%s: unit=%d is free", __func__, unit));
181	ng_eiface_units[index] |= (1 << bit);
182	/*
183	 * XXX We could think about reducing the size of ng_eiface_units[]
184	 * XXX here if the last portion is all ones
185	 * XXX At least free it if no more units.
186	 * Needed if we are to eventually be able to unload.
187	 */
188	ng_units_in_use--;
189	if (ng_units_in_use == 0) { /* XXX make SMP safe */
190		FREE(ng_eiface_units, M_NETGRAPH);
191		ng_eiface_units_len = 0;
192		ng_eiface_units = NULL;
193	}
194	mtx_unlock(&ng_eiface_mtx);
195}
196
197/************************************************************************
198			INTERFACE STUFF
199 ************************************************************************/
200
201/*
202 * Process an ioctl for the virtual interface
203 */
204static int
205ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
206{
207	struct ifreq *const ifr = (struct ifreq *)data;
208	int s, error = 0;
209
210#ifdef DEBUG
211	ng_eiface_print_ioctl(ifp, command, data);
212#endif
213	s = splimp();
214	switch (command) {
215
216	/* These two are mostly handled at a higher layer */
217	case SIOCSIFADDR:
218		error = ether_ioctl(ifp, command, data);
219		break;
220	case SIOCGIFADDR:
221		break;
222
223	/* Set flags */
224	case SIOCSIFFLAGS:
225		/*
226		 * If the interface is marked up and stopped, then start it.
227		 * If it is marked down and running, then stop it.
228		 */
229		if (ifr->ifr_flags & IFF_UP) {
230			if (!(ifp->if_flags & IFF_RUNNING)) {
231				ifp->if_flags &= ~(IFF_OACTIVE);
232				ifp->if_flags |= IFF_RUNNING;
233			}
234		} else {
235			if (ifp->if_flags & IFF_RUNNING)
236				ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
237		}
238		break;
239
240	/* Set the interface MTU */
241	case SIOCSIFMTU:
242		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
243		    ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
244			error = EINVAL;
245		else
246			ifp->if_mtu = ifr->ifr_mtu;
247		break;
248
249	/* Stuff that's not supported */
250	case SIOCADDMULTI:
251	case SIOCDELMULTI:
252		error = 0;
253		break;
254	case SIOCSIFPHYS:
255		error = EOPNOTSUPP;
256		break;
257
258	default:
259		error = EINVAL;
260		break;
261	}
262	splx(s);
263	return (error);
264}
265
266static void
267ng_eiface_init(void *xsc)
268{
269	priv_p sc = xsc;
270	struct ifnet *ifp = sc->ifp;
271	int s;
272
273	s = splimp();
274
275	ifp->if_flags |= IFF_RUNNING;
276	ifp->if_flags &= ~IFF_OACTIVE;
277
278	splx(s);
279}
280
281/*
282 * We simply relay the packet to the "ether" hook, if it is connected.
283 * We have been through the netgraph locking and are guaranteed to
284 * be the only code running in this node at this time.
285 */
286static void
287ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
288{
289	struct ifnet *ifp = arg1;
290	const priv_p priv = (priv_p)ifp->if_softc;
291	int len, error = 0;
292	struct mbuf *m;
293
294	/* Check interface flags */
295	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
296		return;
297
298	/* Don't do anything if output is active */
299	if (ifp->if_flags & IFF_OACTIVE)
300		return;
301
302	ifp->if_flags |= IFF_OACTIVE;
303
304	/*
305	 * Grab a packet to transmit.
306	 */
307	IF_DEQUEUE(&ifp->if_snd, m);
308
309	/* If there's nothing to send, return. */
310	if (m == NULL) {
311		ifp->if_flags &= ~IFF_OACTIVE;
312		return;
313	}
314
315	/*
316	 * Berkeley packet filter.
317	 * Pass packet to bpf if there is a listener.
318	 * XXX is this safe? locking?
319	 */
320	BPF_MTAP(ifp, m);
321
322	/* Copy length before the mbuf gets invalidated */
323	len = m->m_pkthdr.len;
324
325	/*
326	 * Send packet; if hook is not connected, mbuf will get
327	 * freed.
328	 */
329	NG_SEND_DATA_ONLY(error, priv->ether, m);
330
331	/* Update stats */
332	if (error == 0) {
333		ifp->if_obytes += len;
334		ifp->if_opackets++;
335	}
336
337	ifp->if_flags &= ~IFF_OACTIVE;
338
339	return;
340}
341
342/*
343 * This routine is called to deliver a packet out the interface.
344 * We simply queue the netgraph version to be called when netgraph locking
345 * allows it to happen.
346 * Until we know what the rest of the networking code is doing for
347 * locking, we don't know how we will interact with it.
348 * Take comfort from the fact that the ifnet struct is part of our
349 * private info and can't go away while we are queued.
350 * [Though we don't know it is still there now....]
351 * it is possible we don't gain anything from this because
352 * we would like to get the mbuf and queue it as data
353 * somehow, but we can't and if we did would we solve anything?
354 */
355static void
356ng_eiface_start(struct ifnet *ifp)
357{
358
359	const priv_p priv = (priv_p)ifp->if_softc;
360
361	ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
362}
363
364#ifdef DEBUG
365/*
366 * Display an ioctl to the virtual interface
367 */
368
369static void
370ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
371{
372	char *str;
373
374	switch (command & IOC_DIRMASK) {
375	case IOC_VOID:
376		str = "IO";
377		break;
378	case IOC_OUT:
379		str = "IOR";
380		break;
381	case IOC_IN:
382		str = "IOW";
383		break;
384	case IOC_INOUT:
385		str = "IORW";
386		break;
387	default:
388		str = "IO??";
389	}
390	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
391	    ifp->if_xname,
392	    str,
393	    IOCGROUP(command),
394	    command & 0xff,
395	    IOCPARM_LEN(command));
396}
397#endif /* DEBUG */
398
399/************************************************************************
400			NETGRAPH NODE STUFF
401 ************************************************************************/
402
403/*
404 * Constructor for a node
405 */
406static int
407ng_eiface_constructor(node_p node)
408{
409	struct ifnet *ifp;
410	priv_p priv;
411	int error = 0;
412
413	/* Allocate node and interface private structures */
414	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
415	if (priv == NULL)
416		return (ENOMEM);
417	bzero(priv, sizeof(*priv));
418
419	ifp = &(priv->arpcom.ac_if);
420
421	/* Link them together */
422	ifp->if_softc = priv;
423	priv->ifp = ifp;
424
425	/* Get an interface unit number */
426	if ((error = ng_eiface_get_unit(&priv->unit)) != 0) {
427		FREE(priv, M_NETGRAPH);
428		return (error);
429	}
430
431	/* Link together node and private info */
432	NG_NODE_SET_PRIVATE(node, priv);
433	priv->node = node;
434
435	/* Initialize interface structure */
436	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
437	ifp->if_init = ng_eiface_init;
438	ifp->if_output = ether_output;
439	ifp->if_start = ng_eiface_start;
440	ifp->if_ioctl = ng_eiface_ioctl;
441	ifp->if_watchdog = NULL;
442	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
443	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
444
445#if 0
446	/* Give this node name */
447	bzero(ifname, sizeof(ifname));
448	sprintf(ifname, "if%s", ifp->if_xname);
449	(void)ng_name_node(node, ifname);
450#endif
451
452	/* Attach the interface */
453	ether_ifattach(ifp, priv->arpcom.ac_enaddr);
454
455	/* Done */
456	return (0);
457}
458
459/*
460 * Give our ok for a hook to be added
461 */
462static int
463ng_eiface_newhook(node_p node, hook_p hook, const char *name)
464{
465	priv_p priv = NG_NODE_PRIVATE(node);
466
467	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
468		return (EPFNOSUPPORT);
469	if (priv->ether != NULL)
470		return (EISCONN);
471	priv->ether = hook;
472	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
473
474	return (0);
475}
476
477/*
478 * Receive a control message
479 */
480static int
481ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
482{
483	const priv_p priv = NG_NODE_PRIVATE(node);
484	struct ifnet *const ifp = priv->ifp;
485	struct ng_mesg *resp = NULL;
486	int error = 0;
487	struct ng_mesg *msg;
488
489	NGI_GET_MSG(item, msg);
490	switch (msg->header.typecookie) {
491	case NGM_EIFACE_COOKIE:
492		switch (msg->header.cmd) {
493
494		case NGM_EIFACE_SET:
495		    {
496			struct ether_addr *eaddr;
497			struct ifaddr *ifa;
498			struct sockaddr_dl *sdl;
499
500			if (msg->header.arglen != sizeof(struct ether_addr)) {
501				error = EINVAL;
502				break;
503			}
504			eaddr = (struct ether_addr *)(msg->data);
505			bcopy(eaddr, priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
506
507			/* And put it in the ifaddr list */
508			TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) {
509				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
510				if (sdl->sdl_type == IFT_ETHER) {
511					bcopy((IFP2AC(ifp))->ac_enaddr,
512						LLADDR(sdl), ifp->if_addrlen);
513					break;
514				}
515			}
516			break;
517		    }
518
519		case NGM_EIFACE_GET_IFNAME:
520		    {
521			struct ng_eiface_ifname *arg;
522
523			NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
524			if (resp == NULL) {
525				error = ENOMEM;
526				break;
527			}
528			arg = (struct ng_eiface_ifname *)resp->data;
529			strlcpy(arg->ngif_name, ifp->if_xname,
530			    sizeof(arg->ngif_name));
531			break;
532		    }
533
534		case NGM_EIFACE_GET_IFADDRS:
535		    {
536			struct ifaddr *ifa;
537			caddr_t ptr;
538			int buflen;
539
540#define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
541
542			/* Determine size of response and allocate it */
543			buflen = 0;
544			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
545				buflen += SA_SIZE(ifa->ifa_addr);
546			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
547			if (resp == NULL) {
548				error = ENOMEM;
549				break;
550			}
551
552			/* Add addresses */
553			ptr = resp->data;
554			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
555				const int len = SA_SIZE(ifa->ifa_addr);
556
557				if (buflen < len) {
558					log(LOG_ERR, "%s: len changed?\n",
559					    ifp->if_xname);
560					break;
561				}
562				bcopy(ifa->ifa_addr, ptr, len);
563				ptr += len;
564				buflen -= len;
565			}
566			break;
567#undef SA_SIZE
568		    }
569
570		default:
571			error = EINVAL;
572			break;
573		} /* end of inner switch() */
574		break;
575	case NGM_FLOW_COOKIE:
576		switch (msg->header.cmd) {
577		case NGM_LINK_IS_UP:
578			ifp->if_flags |= IFF_RUNNING;
579			break;
580		case NGM_LINK_IS_DOWN:
581			ifp->if_flags &= ~IFF_RUNNING;
582			break;
583		default:
584			break;
585		}
586		break;
587	default:
588		error = EINVAL;
589		break;
590	}
591	NG_RESPOND_MSG(error, node, item, resp);
592	NG_FREE_MSG(msg);
593	return (error);
594}
595
596/*
597 * Receive data from a hook. Pass the packet to the ether_input routine.
598 */
599static int
600ng_eiface_rcvdata(hook_p hook, item_p item)
601{
602	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
603	struct ifnet *const ifp = priv->ifp;
604	struct mbuf *m;
605
606	NGI_GET_M(item, m);
607	NG_FREE_ITEM(item);
608
609	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
610	    (IFF_UP | IFF_RUNNING)) {
611		NG_FREE_M(m);
612		return (ENETDOWN);
613	}
614
615	/* Note receiving interface */
616	m->m_pkthdr.rcvif = ifp;
617
618	/* Update interface stats */
619	ifp->if_ipackets++;
620
621	(*ifp->if_input)(ifp, m);
622
623	/* Done */
624	return (0);
625}
626
627/*
628 * Shutdown processing.
629 */
630static int
631ng_eiface_rmnode(node_p node)
632{
633	const priv_p priv = NG_NODE_PRIVATE(node);
634	struct ifnet *const ifp = priv->ifp;
635
636	ether_ifdetach(ifp);
637	ng_eiface_free_unit(priv->unit);
638	FREE(priv, M_NETGRAPH);
639	NG_NODE_SET_PRIVATE(node, NULL);
640	NG_NODE_UNREF(node);
641	return (0);
642}
643
644
645/*
646 * This is called once we've already connected a new hook to the other node.
647 * It gives us a chance to balk at the last minute.
648 */
649static int
650ng_eiface_connect(hook_p hook)
651{
652	/* be really amiable and just say "YUP that's OK by me! " */
653	return (0);
654}
655
656/*
657 * Hook disconnection
658 */
659static int
660ng_eiface_disconnect(hook_p hook)
661{
662	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
663
664	priv->ether = NULL;
665	return (0);
666}
667