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