xref: /freebsd/sys/netgraph/ng_eiface.c (revision ebccf1e3a6b11b97cbf5f813dd76636e892a9035)
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_dl.h>
44 #include <net/if_types.h>
45 #include <net/netisr.h>
46 
47 #include <netgraph/ng_message.h>
48 #include <netgraph/netgraph.h>
49 #include <netgraph/ng_parse.h>
50 #include <netgraph/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 arpcom	arpcom;		/* per-interface network data */
77 	struct ifnet	*ifp;		/* This interface */
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 struct unrhdr	*ng_eiface_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)
127 {
128 	struct ifreq *const ifr = (struct ifreq *)data;
129 	int s, error = 0;
130 
131 #ifdef DEBUG
132 	ng_eiface_print_ioctl(ifp, command, data);
133 #endif
134 	s = splimp();
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 (ifr->ifr_flags & IFF_UP) {
151 			if (!(ifp->if_flags & IFF_RUNNING)) {
152 				ifp->if_flags &= ~(IFF_OACTIVE);
153 				ifp->if_flags |= IFF_RUNNING;
154 			}
155 		} else {
156 			if (ifp->if_flags & IFF_RUNNING)
157 				ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
158 		}
159 		break;
160 
161 	/* Set the interface MTU */
162 	case SIOCSIFMTU:
163 		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
164 		    ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
165 			error = EINVAL;
166 		else
167 			ifp->if_mtu = ifr->ifr_mtu;
168 		break;
169 
170 	/* Stuff that's not supported */
171 	case SIOCADDMULTI:
172 	case SIOCDELMULTI:
173 		error = 0;
174 		break;
175 	case SIOCSIFPHYS:
176 		error = EOPNOTSUPP;
177 		break;
178 
179 	default:
180 		error = EINVAL;
181 		break;
182 	}
183 	splx(s);
184 	return (error);
185 }
186 
187 static void
188 ng_eiface_init(void *xsc)
189 {
190 	priv_p sc = xsc;
191 	struct ifnet *ifp = sc->ifp;
192 	int s;
193 
194 	s = splimp();
195 
196 	ifp->if_flags |= IFF_RUNNING;
197 	ifp->if_flags &= ~IFF_OACTIVE;
198 
199 	splx(s);
200 }
201 
202 /*
203  * We simply relay the packet to the "ether" hook, if it is connected.
204  * We have been through the netgraph locking and are guaranteed to
205  * be the only code running in this node at this time.
206  */
207 static void
208 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
209 {
210 	struct ifnet *ifp = arg1;
211 	const priv_p priv = (priv_p)ifp->if_softc;
212 	int len, error = 0;
213 	struct mbuf *m;
214 
215 	/* Check interface flags */
216 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
217 		return;
218 
219 	/* Don't do anything if output is active */
220 	if (ifp->if_flags & IFF_OACTIVE)
221 		return;
222 
223 	ifp->if_flags |= IFF_OACTIVE;
224 
225 	/*
226 	 * Grab a packet to transmit.
227 	 */
228 	IF_DEQUEUE(&ifp->if_snd, m);
229 
230 	/* If there's nothing to send, return. */
231 	if (m == NULL) {
232 		ifp->if_flags &= ~IFF_OACTIVE;
233 		return;
234 	}
235 
236 	/*
237 	 * Berkeley packet filter.
238 	 * Pass packet to bpf if there is a listener.
239 	 * XXX is this safe? locking?
240 	 */
241 	BPF_MTAP(ifp, m);
242 
243 	/* Copy length before the mbuf gets invalidated */
244 	len = m->m_pkthdr.len;
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_obytes += len;
255 		ifp->if_opackets++;
256 	}
257 
258 	ifp->if_flags &= ~IFF_OACTIVE;
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)
278 {
279 
280 	const priv_p priv = (priv_p)ifp->if_softc;
281 
282 	ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
283 }
284 
285 #ifdef DEBUG
286 /*
287  * Display an ioctl to the virtual interface
288  */
289 
290 static void
291 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
292 {
293 	char *str;
294 
295 	switch (command & IOC_DIRMASK) {
296 	case IOC_VOID:
297 		str = "IO";
298 		break;
299 	case IOC_OUT:
300 		str = "IOR";
301 		break;
302 	case IOC_IN:
303 		str = "IOW";
304 		break;
305 	case IOC_INOUT:
306 		str = "IORW";
307 		break;
308 	default:
309 		str = "IO??";
310 	}
311 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
312 	    ifp->if_xname,
313 	    str,
314 	    IOCGROUP(command),
315 	    command & 0xff,
316 	    IOCPARM_LEN(command));
317 }
318 #endif /* DEBUG */
319 
320 /************************************************************************
321 			NETGRAPH NODE STUFF
322  ************************************************************************/
323 
324 /*
325  * Constructor for a node
326  */
327 static int
328 ng_eiface_constructor(node_p node)
329 {
330 	struct ifnet *ifp;
331 	priv_p priv;
332 
333 	/* Allocate node and interface private structures */
334 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
335 	if (priv == NULL)
336 		return (ENOMEM);
337 
338 	ifp = &(priv->arpcom.ac_if);
339 
340 	/* Link them together */
341 	ifp->if_softc = priv;
342 	priv->ifp = ifp;
343 
344 	/* Get an interface unit number */
345 	priv->unit = alloc_unr(ng_eiface_unit);
346 
347 	/* Link together node and private info */
348 	NG_NODE_SET_PRIVATE(node, priv);
349 	priv->node = node;
350 
351 	/* Initialize interface structure */
352 	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
353 	ifp->if_init = ng_eiface_init;
354 	ifp->if_output = ether_output;
355 	ifp->if_start = ng_eiface_start;
356 	ifp->if_ioctl = ng_eiface_ioctl;
357 	ifp->if_watchdog = NULL;
358 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
359 	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
360 
361 #if 0
362 	/* Give this node name */
363 	bzero(ifname, sizeof(ifname));
364 	sprintf(ifname, "if%s", ifp->if_xname);
365 	(void)ng_name_node(node, ifname);
366 #endif
367 
368 	/* Attach the interface */
369 	ether_ifattach(ifp, priv->arpcom.ac_enaddr);
370 
371 	/* Done */
372 	return (0);
373 }
374 
375 /*
376  * Give our ok for a hook to be added
377  */
378 static int
379 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
380 {
381 	priv_p priv = NG_NODE_PRIVATE(node);
382 
383 	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
384 		return (EPFNOSUPPORT);
385 	if (priv->ether != NULL)
386 		return (EISCONN);
387 	priv->ether = hook;
388 	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
389 
390 	return (0);
391 }
392 
393 /*
394  * Receive a control message
395  */
396 static int
397 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
398 {
399 	const priv_p priv = NG_NODE_PRIVATE(node);
400 	struct ifnet *const ifp = priv->ifp;
401 	struct ng_mesg *resp = NULL;
402 	int error = 0;
403 	struct ng_mesg *msg;
404 
405 	NGI_GET_MSG(item, msg);
406 	switch (msg->header.typecookie) {
407 	case NGM_EIFACE_COOKIE:
408 		switch (msg->header.cmd) {
409 
410 		case NGM_EIFACE_SET:
411 		    {
412 			struct ether_addr *eaddr;
413 			struct ifaddr *ifa;
414 			struct sockaddr_dl *sdl;
415 
416 			if (msg->header.arglen != sizeof(struct ether_addr)) {
417 				error = EINVAL;
418 				break;
419 			}
420 			eaddr = (struct ether_addr *)(msg->data);
421 			bcopy(eaddr, priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
422 
423 			/* And put it in the ifaddr list */
424 			TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) {
425 				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
426 				if (sdl->sdl_type == IFT_ETHER) {
427 					bcopy((IFP2AC(ifp))->ac_enaddr,
428 						LLADDR(sdl), ifp->if_addrlen);
429 					break;
430 				}
431 			}
432 			break;
433 		    }
434 
435 		case NGM_EIFACE_GET_IFNAME:
436 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
437 			if (resp == NULL) {
438 				error = ENOMEM;
439 				break;
440 			}
441 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
442 			break;
443 
444 		case NGM_EIFACE_GET_IFADDRS:
445 		    {
446 			struct ifaddr *ifa;
447 			caddr_t ptr;
448 			int buflen;
449 
450 #define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
451 
452 			/* Determine size of response and allocate it */
453 			buflen = 0;
454 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
455 				buflen += SA_SIZE(ifa->ifa_addr);
456 			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
457 			if (resp == NULL) {
458 				error = ENOMEM;
459 				break;
460 			}
461 
462 			/* Add addresses */
463 			ptr = resp->data;
464 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
465 				const int len = SA_SIZE(ifa->ifa_addr);
466 
467 				if (buflen < len) {
468 					log(LOG_ERR, "%s: len changed?\n",
469 					    ifp->if_xname);
470 					break;
471 				}
472 				bcopy(ifa->ifa_addr, ptr, len);
473 				ptr += len;
474 				buflen -= len;
475 			}
476 			break;
477 #undef SA_SIZE
478 		    }
479 
480 		default:
481 			error = EINVAL;
482 			break;
483 		} /* end of inner switch() */
484 		break;
485 	case NGM_FLOW_COOKIE:
486 		switch (msg->header.cmd) {
487 		case NGM_LINK_IS_UP:
488 			ifp->if_flags |= IFF_RUNNING;
489 			break;
490 		case NGM_LINK_IS_DOWN:
491 			ifp->if_flags &= ~IFF_RUNNING;
492 			break;
493 		default:
494 			break;
495 		}
496 		break;
497 	default:
498 		error = EINVAL;
499 		break;
500 	}
501 	NG_RESPOND_MSG(error, node, item, resp);
502 	NG_FREE_MSG(msg);
503 	return (error);
504 }
505 
506 /*
507  * Receive data from a hook. Pass the packet to the ether_input routine.
508  */
509 static int
510 ng_eiface_rcvdata(hook_p hook, item_p item)
511 {
512 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
513 	struct ifnet *const ifp = priv->ifp;
514 	struct mbuf *m;
515 
516 	NGI_GET_M(item, m);
517 	NG_FREE_ITEM(item);
518 
519 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
520 	    (IFF_UP | IFF_RUNNING)) {
521 		NG_FREE_M(m);
522 		return (ENETDOWN);
523 	}
524 
525 	if (m->m_len < ETHER_HDR_LEN) {
526 		m = m_pullup(m, ETHER_HDR_LEN);
527 		if (m == NULL)
528 			return (EINVAL);
529 	}
530 
531 	/* Note receiving interface */
532 	m->m_pkthdr.rcvif = ifp;
533 
534 	/* Update interface stats */
535 	ifp->if_ipackets++;
536 
537 	(*ifp->if_input)(ifp, m);
538 
539 	/* Done */
540 	return (0);
541 }
542 
543 /*
544  * Shutdown processing.
545  */
546 static int
547 ng_eiface_rmnode(node_p node)
548 {
549 	const priv_p priv = NG_NODE_PRIVATE(node);
550 	struct ifnet *const ifp = priv->ifp;
551 
552 	ether_ifdetach(ifp);
553 	free_unr(ng_eiface_unit, priv->unit);
554 	FREE(priv, M_NETGRAPH);
555 	NG_NODE_SET_PRIVATE(node, NULL);
556 	NG_NODE_UNREF(node);
557 	return (0);
558 }
559 
560 /*
561  * Hook disconnection
562  */
563 static int
564 ng_eiface_disconnect(hook_p hook)
565 {
566 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
567 
568 	priv->ether = NULL;
569 	return (0);
570 }
571 
572 /*
573  * Handle loading and unloading for this node type.
574  */
575 static int
576 ng_eiface_mod_event(module_t mod, int event, void *data)
577 {
578 	int error = 0;
579 
580 	switch (event) {
581 	case MOD_LOAD:
582 		ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
583 		break;
584 	case MOD_UNLOAD:
585 		delete_unrhdr(ng_eiface_unit);
586 		break;
587 	default:
588 		error = EOPNOTSUPP;
589 		break;
590 	}
591 	return (error);
592 }
593