xref: /freebsd/sys/netgraph/ng_eiface.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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 
42 #include <net/if.h>
43 #include <net/if_types.h>
44 #include <net/netisr.h>
45 
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
48 #include <netgraph/ng_parse.h>
49 #include <netgraph/ng_eiface.h>
50 
51 #include <net/bpf.h>
52 #include <net/ethernet.h>
53 #include <net/if_arp.h>
54 
55 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
56 	{
57 	  NGM_EIFACE_COOKIE,
58 	  NGM_EIFACE_GET_IFNAME,
59 	  "getifname",
60 	  NULL,
61 	  &ng_parse_string_type
62 	},
63 	{
64 	  NGM_EIFACE_COOKIE,
65 	  NGM_EIFACE_SET,
66 	  "set",
67 	  &ng_parse_enaddr_type,
68 	  NULL
69 	},
70 	{ 0 }
71 };
72 
73 /* Node private data */
74 struct ng_eiface_private {
75 	struct ifnet	*ifp;		/* per-interface network data */
76 	int		unit;		/* Interface unit number */
77 	node_p		node;		/* Our netgraph node */
78 	hook_p		ether;		/* Hook for ethernet stream */
79 };
80 typedef struct ng_eiface_private *priv_p;
81 
82 /* Interface methods */
83 static void	ng_eiface_init(void *xsc);
84 static void	ng_eiface_start(struct ifnet *ifp);
85 static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
86 #ifdef DEBUG
87 static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
88 #endif
89 
90 /* Netgraph methods */
91 static int		ng_eiface_mod_event(module_t, int, void *);
92 static ng_constructor_t	ng_eiface_constructor;
93 static ng_rcvmsg_t	ng_eiface_rcvmsg;
94 static ng_shutdown_t	ng_eiface_rmnode;
95 static ng_newhook_t	ng_eiface_newhook;
96 static ng_rcvdata_t	ng_eiface_rcvdata;
97 static ng_disconnect_t	ng_eiface_disconnect;
98 
99 /* Node type descriptor */
100 static struct ng_type typestruct = {
101 	.version =	NG_ABI_VERSION,
102 	.name =		NG_EIFACE_NODE_TYPE,
103 	.mod_event =	ng_eiface_mod_event,
104 	.constructor =	ng_eiface_constructor,
105 	.rcvmsg =	ng_eiface_rcvmsg,
106 	.shutdown =	ng_eiface_rmnode,
107 	.newhook =	ng_eiface_newhook,
108 	.rcvdata =	ng_eiface_rcvdata,
109 	.disconnect =	ng_eiface_disconnect,
110 	.cmdlist =	ng_eiface_cmdlist
111 };
112 NETGRAPH_INIT(eiface, &typestruct);
113 
114 static struct unrhdr	*ng_eiface_unit;
115 
116 /************************************************************************
117 			INTERFACE STUFF
118  ************************************************************************/
119 
120 /*
121  * Process an ioctl for the virtual interface
122  */
123 static int
124 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
125 {
126 	struct ifreq *const ifr = (struct ifreq *)data;
127 	int s, error = 0;
128 
129 #ifdef DEBUG
130 	ng_eiface_print_ioctl(ifp, command, data);
131 #endif
132 	s = splimp();
133 	switch (command) {
134 
135 	/* These two are mostly handled at a higher layer */
136 	case SIOCSIFADDR:
137 		error = ether_ioctl(ifp, command, data);
138 		break;
139 	case SIOCGIFADDR:
140 		break;
141 
142 	/* Set flags */
143 	case SIOCSIFFLAGS:
144 		/*
145 		 * 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 
186 static void
187 ng_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  */
206 static void
207 ng_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 len, 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 	/* Don't do anything if output is active */
221 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
222 		return;
223 
224 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
225 
226 	/*
227 	 * Grab a packet to transmit.
228 	 */
229 	IF_DEQUEUE(&ifp->if_snd, m);
230 
231 	/* If there's nothing to send, return. */
232 	if (m == NULL) {
233 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
234 		return;
235 	}
236 
237 	/*
238 	 * Berkeley packet filter.
239 	 * Pass packet to bpf if there is a listener.
240 	 * XXX is this safe? locking?
241 	 */
242 	BPF_MTAP(ifp, m);
243 
244 	/* Copy length before the mbuf gets invalidated */
245 	len = m->m_pkthdr.len;
246 
247 	/*
248 	 * Send packet; if hook is not connected, mbuf will get
249 	 * freed.
250 	 */
251 	NG_SEND_DATA_ONLY(error, priv->ether, m);
252 
253 	/* Update stats */
254 	if (error == 0) {
255 		ifp->if_obytes += len;
256 		ifp->if_opackets++;
257 	}
258 
259 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
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)
279 {
280 
281 	const priv_p priv = (priv_p)ifp->if_softc;
282 
283 	ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
284 }
285 
286 #ifdef DEBUG
287 /*
288  * Display an ioctl to the virtual interface
289  */
290 
291 static void
292 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
293 {
294 	char *str;
295 
296 	switch (command & IOC_DIRMASK) {
297 	case IOC_VOID:
298 		str = "IO";
299 		break;
300 	case IOC_OUT:
301 		str = "IOR";
302 		break;
303 	case IOC_IN:
304 		str = "IOW";
305 		break;
306 	case IOC_INOUT:
307 		str = "IORW";
308 		break;
309 	default:
310 		str = "IO??";
311 	}
312 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
313 	    ifp->if_xname,
314 	    str,
315 	    IOCGROUP(command),
316 	    command & 0xff,
317 	    IOCPARM_LEN(command));
318 }
319 #endif /* DEBUG */
320 
321 /************************************************************************
322 			NETGRAPH NODE STUFF
323  ************************************************************************/
324 
325 /*
326  * Constructor for a node
327  */
328 static int
329 ng_eiface_constructor(node_p node)
330 {
331 	struct ifnet *ifp;
332 	priv_p priv;
333 	u_char eaddr[6] = {0,0,0,0,0,0};
334 
335 	/* Allocate node and interface private structures */
336 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
337 	if (priv == NULL)
338 		return (ENOMEM);
339 
340 	ifp = priv->ifp = if_alloc(IFT_ETHER);
341 	if (ifp == NULL) {
342 		free(priv, M_NETGRAPH);
343 		return (ENOSPC);
344 	}
345 
346 	/* Link them together */
347 	ifp->if_softc = priv;
348 
349 	/* Get an interface unit number */
350 	priv->unit = alloc_unr(ng_eiface_unit);
351 
352 	/* Link together node and private info */
353 	NG_NODE_SET_PRIVATE(node, priv);
354 	priv->node = node;
355 
356 	/* Initialize interface structure */
357 	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
358 	ifp->if_init = ng_eiface_init;
359 	ifp->if_output = ether_output;
360 	ifp->if_start = ng_eiface_start;
361 	ifp->if_ioctl = ng_eiface_ioctl;
362 	ifp->if_watchdog = NULL;
363 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
364 	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
365 
366 #if 0
367 	/* Give this node name */
368 	bzero(ifname, sizeof(ifname));
369 	sprintf(ifname, "if%s", ifp->if_xname);
370 	(void)ng_name_node(node, ifname);
371 #endif
372 
373 	/* Attach the interface */
374 	ether_ifattach(ifp, eaddr);
375 
376 	/* Done */
377 	return (0);
378 }
379 
380 /*
381  * Give our ok for a hook to be added
382  */
383 static int
384 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
385 {
386 	priv_p priv = NG_NODE_PRIVATE(node);
387 	struct ifnet *ifp = priv->ifp;
388 
389 	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
390 		return (EPFNOSUPPORT);
391 	if (priv->ether != NULL)
392 		return (EISCONN);
393 	priv->ether = hook;
394 	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
395 
396 	if_link_state_change(ifp, LINK_STATE_UP);
397 
398 	return (0);
399 }
400 
401 /*
402  * Receive a control message
403  */
404 static int
405 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
406 {
407 	const priv_p priv = NG_NODE_PRIVATE(node);
408 	struct ifnet *const ifp = priv->ifp;
409 	struct ng_mesg *resp = NULL;
410 	int error = 0;
411 	struct ng_mesg *msg;
412 
413 	NGI_GET_MSG(item, msg);
414 	switch (msg->header.typecookie) {
415 	case NGM_EIFACE_COOKIE:
416 		switch (msg->header.cmd) {
417 
418 		case NGM_EIFACE_SET:
419 		    {
420 			if (msg->header.arglen != ETHER_ADDR_LEN) {
421 				error = EINVAL;
422 				break;
423 			}
424 			error = if_setlladdr(priv->ifp,
425 			    (u_char *)msg->data, ETHER_ADDR_LEN);
426 			break;
427 		    }
428 
429 		case NGM_EIFACE_GET_IFNAME:
430 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
431 			if (resp == NULL) {
432 				error = ENOMEM;
433 				break;
434 			}
435 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
436 			break;
437 
438 		case NGM_EIFACE_GET_IFADDRS:
439 		    {
440 			struct ifaddr *ifa;
441 			caddr_t ptr;
442 			int buflen;
443 
444 #define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
445 
446 			/* Determine size of response and allocate it */
447 			buflen = 0;
448 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
449 				buflen += SA_SIZE(ifa->ifa_addr);
450 			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
451 			if (resp == NULL) {
452 				error = ENOMEM;
453 				break;
454 			}
455 
456 			/* Add addresses */
457 			ptr = resp->data;
458 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
459 				const int len = SA_SIZE(ifa->ifa_addr);
460 
461 				if (buflen < len) {
462 					log(LOG_ERR, "%s: len changed?\n",
463 					    ifp->if_xname);
464 					break;
465 				}
466 				bcopy(ifa->ifa_addr, ptr, len);
467 				ptr += len;
468 				buflen -= len;
469 			}
470 			break;
471 #undef SA_SIZE
472 		    }
473 
474 		default:
475 			error = EINVAL;
476 			break;
477 		} /* end of inner switch() */
478 		break;
479 	case NGM_FLOW_COOKIE:
480 		switch (msg->header.cmd) {
481 		case NGM_LINK_IS_UP:
482 			if_link_state_change(ifp, LINK_STATE_UP);
483 			break;
484 		case NGM_LINK_IS_DOWN:
485 			if_link_state_change(ifp, LINK_STATE_DOWN);
486 			break;
487 		default:
488 			break;
489 		}
490 		break;
491 	default:
492 		error = EINVAL;
493 		break;
494 	}
495 	NG_RESPOND_MSG(error, node, item, resp);
496 	NG_FREE_MSG(msg);
497 	return (error);
498 }
499 
500 /*
501  * Receive data from a hook. Pass the packet to the ether_input routine.
502  */
503 static int
504 ng_eiface_rcvdata(hook_p hook, item_p item)
505 {
506 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
507 	struct ifnet *const ifp = priv->ifp;
508 	struct mbuf *m;
509 
510 	NGI_GET_M(item, m);
511 	NG_FREE_ITEM(item);
512 
513 	if (!((ifp->if_flags & IFF_UP) &&
514 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
515 		NG_FREE_M(m);
516 		return (ENETDOWN);
517 	}
518 
519 	if (m->m_len < ETHER_HDR_LEN) {
520 		m = m_pullup(m, ETHER_HDR_LEN);
521 		if (m == NULL)
522 			return (EINVAL);
523 	}
524 
525 	/* Note receiving interface */
526 	m->m_pkthdr.rcvif = ifp;
527 
528 	/* Update interface stats */
529 	ifp->if_ipackets++;
530 
531 	(*ifp->if_input)(ifp, m);
532 
533 	/* Done */
534 	return (0);
535 }
536 
537 /*
538  * Shutdown processing.
539  */
540 static int
541 ng_eiface_rmnode(node_p node)
542 {
543 	const priv_p priv = NG_NODE_PRIVATE(node);
544 	struct ifnet *const ifp = priv->ifp;
545 
546 	ether_ifdetach(ifp);
547 	if_free(ifp);
548 	free_unr(ng_eiface_unit, priv->unit);
549 	FREE(priv, M_NETGRAPH);
550 	NG_NODE_SET_PRIVATE(node, NULL);
551 	NG_NODE_UNREF(node);
552 	return (0);
553 }
554 
555 /*
556  * Hook disconnection
557  */
558 static int
559 ng_eiface_disconnect(hook_p hook)
560 {
561 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
562 
563 	priv->ether = NULL;
564 	return (0);
565 }
566 
567 /*
568  * Handle loading and unloading for this node type.
569  */
570 static int
571 ng_eiface_mod_event(module_t mod, int event, void *data)
572 {
573 	int error = 0;
574 
575 	switch (event) {
576 	case MOD_LOAD:
577 		ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
578 		break;
579 	case MOD_UNLOAD:
580 		delete_unrhdr(ng_eiface_unit);
581 		break;
582 	default:
583 		error = EOPNOTSUPP;
584 		break;
585 	}
586 	return (error);
587 }
588