xref: /dragonfly/sys/netgraph/eiface/ng_eiface.c (revision 52f9f0d9)
1 /*
2  * ng_eiface.c
3  *
4  * Copyright (c) 1999-2000, Vitaly V Belekhov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * 	$Id: ng_eiface.c,v 1.14 2000/03/15 12:28:44 vitaly Exp $
30  * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.4.2.5 2002/12/17 21:47:48 julian Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/errno.h>
40 #include <sys/sockio.h>
41 #include <sys/socket.h>
42 #include <sys/syslog.h>
43 #include <sys/serialize.h>
44 #include <sys/thread2.h>
45 
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/netisr.h>
49 
50 
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 #include <netgraph/ng_parse.h>
54 #include "ng_eiface.h"
55 
56 #include <net/bpf.h>
57 #include <net/ethernet.h>
58 #include <net/if_arp.h>
59 
60 static const struct ng_parse_struct_field ng_eiface_par_fields[]
61 	= NG_EIFACE_PAR_FIELDS;
62 
63 static const struct ng_parse_type ng_eiface_par_type = {
64 	&ng_parse_struct_type,
65 	&ng_eiface_par_fields
66 };
67 
68 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
69 	{
70 	  NGM_EIFACE_COOKIE,
71 	  NGM_EIFACE_SET,
72 	  "set",
73 	  &ng_eiface_par_type,
74 	  NULL
75 	},
76 	{ 0 }
77 };
78 
79 /* Node private data */
80 struct ng_eiface_private {
81 	struct arpcom	arpcom;		/* per-interface network data */
82 	struct	ifnet *ifp;		/* This interface */
83 	node_p	node;			/* Our netgraph node */
84 	hook_p	ether;			/* Hook for ethernet stream */
85 	struct	private *next;		/* When hung on the free list */
86 };
87 typedef struct ng_eiface_private *priv_p;
88 
89 /* Interface methods */
90 static void	ng_eiface_init(void *xsc);
91 static void	ng_eiface_start(struct ifnet *ifp);
92 static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
93 				struct ucred *cr);
94 #ifdef DEBUG
95 static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
96 #endif
97 
98 /* Netgraph methods */
99 static ng_constructor_t	ng_eiface_constructor;
100 static ng_rcvmsg_t	ng_eiface_rcvmsg;
101 static ng_shutdown_t	ng_eiface_rmnode;
102 static ng_newhook_t	ng_eiface_newhook;
103 static ng_rcvdata_t	ng_eiface_rcvdata;
104 static ng_connect_t	ng_eiface_connect;
105 static ng_disconnect_t	ng_eiface_disconnect;
106 
107 /* Node type descriptor */
108 static struct ng_type typestruct = {
109 	NG_VERSION,
110 	NG_EIFACE_NODE_TYPE,
111 	NULL,
112 	ng_eiface_constructor,
113 	ng_eiface_rcvmsg,
114 	ng_eiface_rmnode,
115 	ng_eiface_newhook,
116 	NULL,
117 	ng_eiface_connect,
118 	ng_eiface_rcvdata,
119 	ng_eiface_rcvdata,
120 	ng_eiface_disconnect,
121 	ng_eiface_cmdlist
122 };
123 NETGRAPH_INIT(eiface, &typestruct);
124 
125 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME;
126 static int ng_eiface_next_unit;
127 
128 /************************************************************************
129 			INTERFACE STUFF
130  ************************************************************************/
131 
132 /*
133  * Process an ioctl for the virtual interface
134  */
135 static int
136 ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
137 {
138 	struct ifreq *const ifr = (struct ifreq *) data;
139 	int error = 0;
140 
141 #ifdef DEBUG
142 	ng_eiface_print_ioctl(ifp, cmd, data);
143 #endif
144 	crit_enter();
145 	switch (cmd) {
146 
147 	/* These two are mostly handled at a higher layer */
148 	case SIOCSIFADDR:
149 		error = ether_ioctl(ifp, cmd, data);
150 		break;
151 	case SIOCGIFADDR:
152 		break;
153 
154 	/* Set flags */
155 	case SIOCSIFFLAGS:
156 		/*
157 		 * If the interface is marked up and stopped, then start it.
158 		 * If it is marked down and running, then stop it.
159 		 */
160 		if (ifr->ifr_flags & IFF_UP) {
161 			if (!(ifp->if_flags & IFF_RUNNING)) {
162 				ifp->if_flags &= ~(IFF_OACTIVE);
163 				ifp->if_flags |= IFF_RUNNING;
164 			}
165 		} else {
166 			if (ifp->if_flags & IFF_RUNNING)
167 				ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
168 		}
169 		break;
170 
171 	/* Set the interface MTU */
172 	case SIOCSIFMTU:
173 		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX
174 		    || ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
175 			error = EINVAL;
176 		else
177 			ifp->if_mtu = ifr->ifr_mtu;
178 		break;
179 
180 	/* Stuff that's not supported */
181 	case SIOCADDMULTI:
182 	case SIOCDELMULTI:
183 		error = 0;
184 		break;
185 	case SIOCSIFPHYS:
186 		error = EOPNOTSUPP;
187 		break;
188 
189 	default:
190 		error = EINVAL;
191 		break;
192 	}
193 	crit_exit();
194 	return (error);
195 }
196 
197 static void
198 ng_eiface_init(void *xsc)
199 {
200 	priv_p sc = xsc;
201 	struct ifnet *ifp = sc->ifp;
202 
203 	crit_enter();
204 
205 	ifp->if_flags |= IFF_RUNNING;
206 	ifp->if_flags &= ~IFF_OACTIVE;
207 
208 	crit_exit();
209 
210 }
211 
212 /*
213  * This routine is called to deliver a packet out the interface.
214  * We simply relay the packet to
215  * the ether hook, if it is connected.
216  */
217 
218 static void
219 ng_eiface_start(struct ifnet *ifp)
220 {
221 	const priv_p priv = (priv_p) ifp->if_softc;
222 	meta_p meta = NULL;
223 	int len, error = 0;
224 	struct mbuf *m;
225 
226 	/* Check interface flags */
227 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
228 		return;
229 
230 	/* Don't do anything if output is active */
231 	if( ifp->if_flags & IFF_OACTIVE )
232 		return;
233 
234 	ifp->if_flags |= IFF_OACTIVE;
235 
236 	/*
237 	 * Grab a packet to transmit.
238 	 */
239 	IF_DEQUEUE(&ifp->if_snd, m);
240 
241 	/* If there's nothing to send, return. */
242 	if(m == NULL)
243 	{
244 		ifp->if_flags &= ~IFF_OACTIVE;
245 		return;
246 	}
247 
248 	BPF_MTAP(ifp, m);
249 
250 	/* Copy length before the mbuf gets invalidated */
251 	len = m->m_pkthdr.len;
252 
253 	/* Send packet; if hook is not connected, mbuf will get freed. */
254 	NG_SEND_DATA(error, priv->ether, m, meta);
255 
256 	/* Update stats */
257 	if (error == 0) {
258 		ifp->if_obytes += len;
259 		ifp->if_opackets++;
260 	}
261 
262 	ifp->if_flags &= ~IFF_OACTIVE;
263 
264 	return;
265 }
266 
267 #ifdef DEBUG
268 /*
269  * Display an ioctl to the virtual interface
270  */
271 
272 static void
273 ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data)
274 {
275 	char   *str;
276 
277 	switch (cmd & IOC_DIRMASK) {
278 	case IOC_VOID:
279 		str = "IO";
280 		break;
281 	case IOC_OUT:
282 		str = "IOR";
283 		break;
284 	case IOC_IN:
285 		str = "IOW";
286 		break;
287 	case IOC_INOUT:
288 		str = "IORW";
289 		break;
290 	default:
291 		str = "IO??";
292 	}
293 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
294 	       ifp->if_xname,
295 	       str,
296 	       IOCGROUP(cmd),
297 	       cmd & 0xff,
298 	       IOCPARM_LEN(cmd));
299 }
300 #endif /* DEBUG */
301 
302 /************************************************************************
303 			NETGRAPH NODE STUFF
304  ************************************************************************/
305 
306 /*
307  * Constructor for a node
308  */
309 static int
310 ng_eiface_constructor(node_p *nodep)
311 {
312 	struct ifnet *ifp;
313 	node_p node;
314 	priv_p priv;
315 	int error = 0;
316 
317 	/* Allocate node and interface private structures */
318 	priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
319 
320 	ifp = &(priv->arpcom.ac_if);
321 
322 	/* Link them together */
323 	ifp->if_softc = priv;
324 	priv->ifp = ifp;
325 
326 	/* Call generic node constructor */
327 	if ((error = ng_make_node_common(&typestruct, nodep))) {
328 		kfree(priv, M_NETGRAPH);
329 		return (error);
330 	}
331 	node = *nodep;
332 
333 	/* Link together node and private info */
334 	node->private = priv;
335 	priv->node = node;
336 
337 	/* Initialize interface structure */
338 	if_initname(ifp, ng_eiface_ifname, ng_eiface_next_unit++);
339 	ifp->if_init = ng_eiface_init;
340 	ifp->if_start = ng_eiface_start;
341 	ifp->if_ioctl = ng_eiface_ioctl;
342 	ifp->if_watchdog = NULL;
343 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
344 	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
345 
346 	/* Give this node name *
347 	bzero(ifname, sizeof(ifname));
348 	ksprintf(ifname, "if%s", ifp->if_xname);
349 	ng_name_node(node, ifname);
350 	*/
351 
352 	/* Attach the interface */
353 	ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
354 
355 	/* Done */
356 	return (0);
357 }
358 
359 /*
360  * Give our ok for a hook to be added
361  */
362 static int
363 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
364 {
365 	priv_p priv = node->private;
366 
367 	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
368 		return (EPFNOSUPPORT);
369 	if (priv->ether != NULL)
370 		return (EISCONN);
371 	priv->ether = hook;
372 	hook->private = &priv->ether;
373 
374 	return (0);
375 }
376 
377 /*
378  * Receive a control message
379  */
380 static int
381 ng_eiface_rcvmsg(node_p node, struct ng_mesg *msg,
382 		const char *retaddr, struct ng_mesg **rptr)
383 {
384 	const priv_p priv = node->private;
385 	struct ifnet *const ifp = priv->ifp;
386 	struct ng_mesg *resp = NULL;
387 	int error = 0;
388 
389 	switch (msg->header.typecookie) {
390 	case NGM_EIFACE_COOKIE:
391 		switch (msg->header.cmd) {
392 
393 		case NGM_EIFACE_SET:
394 		    {
395 		      struct ng_eiface_par *eaddr;
396 
397 		      if (msg->header.arglen != sizeof(struct ng_eiface_par))
398 			{
399 			  error = EINVAL;
400 			  break;
401 			}
402 		      eaddr = (struct ng_eiface_par *)(msg->data);
403 
404 		      priv->arpcom.ac_enaddr[0] = eaddr->oct0;
405 		      priv->arpcom.ac_enaddr[1] = eaddr->oct1;
406 		      priv->arpcom.ac_enaddr[2] = eaddr->oct2;
407 		      priv->arpcom.ac_enaddr[3] = eaddr->oct3;
408 		      priv->arpcom.ac_enaddr[4] = eaddr->oct4;
409 		      priv->arpcom.ac_enaddr[5] = eaddr->oct5;
410 
411 		      break;
412 		    }
413 
414 		case NGM_EIFACE_GET_IFNAME:
415 		    {
416 			struct ng_eiface_ifname *arg;
417 
418 			NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
419 			if (resp == NULL) {
420 				error = ENOMEM;
421 				break;
422 			}
423 			arg = (struct ng_eiface_ifname *) resp->data;
424 			ksprintf(arg->ngif_name,
425 			    "%s", ifp->if_xname); /* XXX: strings */
426 			break;
427 		    }
428 
429 		case NGM_EIFACE_GET_IFADDRS:
430 		    {
431 			struct ifaddr_container *ifac;
432 			caddr_t ptr;
433 			int buflen;
434 
435 #define SA_SIZE(s)	((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
436 
437 			/* Determine size of response and allocate it */
438 			buflen = 0;
439 			TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
440 				      ifa_link)
441 				buflen += SA_SIZE(ifac->ifa->ifa_addr);
442 			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
443 			if (resp == NULL) {
444 				error = ENOMEM;
445 				break;
446 			}
447 
448 			/* Add addresses */
449 			ptr = resp->data;
450 			TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
451 				      ifa_link) {
452 				struct ifaddr *ifa = ifac->ifa;
453 				const int len = SA_SIZE(ifa->ifa_addr);
454 
455 				if (buflen < len) {
456 					log(LOG_ERR, "%s: len changed?\n",
457 					    ifp->if_xname);
458 					break;
459 				}
460 				bcopy(ifa->ifa_addr, ptr, len);
461 				ptr += len;
462 				buflen -= len;
463 			}
464 			break;
465 #undef SA_SIZE
466 		    }
467 
468 		default:
469 			error = EINVAL;
470 			break;
471 		}
472 		break;
473 	default:
474 		error = EINVAL;
475 		break;
476 	}
477 	if (rptr)
478 		*rptr = resp;
479 	else if (resp)
480 		kfree(resp, M_NETGRAPH);
481 	kfree(msg, M_NETGRAPH);
482 	return (error);
483 }
484 
485 /*
486  * Recive data from a hook. Pass the packet to the ether_input routine.
487  */
488 static int
489 ng_eiface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
490 {
491 	const priv_p priv = hook->node->private;
492 	struct ifnet *const ifp = priv->ifp;
493 	int error = 0;
494 
495 	/* Meta-data is end its life here... */
496 	NG_FREE_META(meta);
497 
498 	if (m == NULL) {
499 	    kprintf("ng_eiface: mbuf is null.\n");
500 	    return (EINVAL);
501 	}
502 
503 	if ( !(ifp->if_flags & IFF_UP) )
504 		return (ENETDOWN);
505 
506 	/* Note receiving interface */
507 	m->m_pkthdr.rcvif = ifp;
508 
509 	/* Update interface stats */
510 	ifp->if_ipackets++;
511 
512 	BPF_MTAP(ifp, m);
513 
514 	ifp->if_input(ifp, m);
515 
516 	/* Done */
517 	return (error);
518 }
519 
520 /*
521  * Because the BSD networking code doesn't support the removal of
522  * networking interfaces, iface nodes (once created) are persistent.
523  * So this method breaks all connections and marks the interface
524  * down, but does not remove the node.
525  */
526 static int
527 ng_eiface_rmnode(node_p node)
528 {
529 	const priv_p priv = node->private;
530 	struct ifnet *const ifp = priv->ifp;
531 
532 	ng_cutlinks(node);
533 	node->flags &= ~NG_INVALID;
534 	ifnet_serialize_all(ifp);
535 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE);
536 	ifnet_deserialize_all(ifp);
537 	return (0);
538 }
539 
540 
541 /*
542  * This is called once we've already connected a new hook to the other node.
543  * It gives us a chance to balk at the last minute.
544  */
545 static int
546 ng_eiface_connect(hook_p hook)
547 {
548 	/* be really amiable and just say "YUP that's OK by me! " */
549 	return (0);
550 }
551 
552 /*
553  * Hook disconnection
554  */
555 static int
556 ng_eiface_disconnect(hook_p hook)
557 {
558 	const priv_p priv = hook->node->private;
559 
560 	priv->ether = NULL;
561 	return (0);
562 }
563