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