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