- added support for Mac OSX (10.3 Panther and 10.4 Tiger). For tuntap support
[vde.git] / vde-2 / tuntap.c
blob8467bca4882a090889242bc36a67bb95501b6cb1
1 /* Copyright 2005 Renzo Davoli - VDE-2
2 * --pidfile/-p and cleanup management by Mattia Belletti (C) 2004.
3 * Licensed under the GPLv2
4 * Modified by Ludovico Gardenghi 2005 (OSX tuntap support)
5 */
7 #include <config.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <syslog.h>
14 #include <stdlib.h>
15 #include <libgen.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19 #include <stdarg.h>
20 #define _GNU_SOURCE
21 #include <getopt.h>
23 #ifdef VDE_LINUX
24 #include <net/if.h>
25 #include <linux/if_tun.h>
26 #endif
28 #ifdef VDE_DARWIN
29 #define TAP_PREFIX "/dev/"
30 #endif
32 #include <port.h>
33 #include <switch.h>
34 #include <consmgmt.h>
37 #define MAXCMD 128
38 #define MODULENAME "tuntap"
40 static struct swmodule swmi;
41 static struct mod_support modfun;
42 static unsigned int tap_type;
44 struct init_tap {
45 char *tap_dev;
46 struct init_tap *next;
49 struct init_tap *hinit_tap=NULL;
51 static void send_tap(int fd, int ctl_fd, void *packet, int len, void *unused, int port)
53 int n;
55 n = write(ctl_fd, packet, len);
56 if(n != len){
57 if(errno != EAGAIN) printlog(LOG_WARNING,"send_tap port %d: %s",port,strerror(errno));
61 static void closeport(int fd, int portno)
63 if (fd>0)
64 remove_fd(fd);
67 static void handle_input(unsigned char type,int fd,int revents,int *arg)
69 struct bipacket packet;
70 int len=read(fd, &(packet.p), sizeof(struct packet));
72 if(len < 0){
73 if(errno != EAGAIN) printlog(LOG_WARNING,"Reading tap data: %s",strerror(errno));
75 else if(len == 0) {
76 if(errno != EAGAIN) printlog(LOG_WARNING,"EOF tap data port: %s",strerror(errno));
77 /* close tap! */
78 } else if (len >= ETH_HEADER_SIZE)
79 handle_in_packet(*arg, &(packet.p), len);
83 static void cleanup(unsigned char type,int fd,int arg)
85 if (fd >= 0)
86 close(fd);
89 static struct option long_options[] = {
90 {"tap", 1, 0, 't'},
92 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
94 static void usage(void)
96 printf(
97 "(opts from tuntap module)\n"
98 " -t, --tap TAP Enable routing through TAP tap interface\n"
99 #ifdef VDE_DARWIN
100 " TAP can be an absolute file name or a relative\n"
101 " one (and will be prefixed with %s). The TAP\n"
102 " device must already exist.\n", TAP_PREFIX
103 #endif
107 static struct init_tap *add_init_tap(struct init_tap *p,char *arg)
109 if (p == NULL) {
110 p=malloc(sizeof(struct init_tap));
111 if (p==NULL)
112 printlog(LOG_WARNING,"Malloc Tap init:%s\n",strerror(errno));
113 else {
114 p->tap_dev=strdup(optarg);
115 p->next=NULL;
117 } else
118 p->next=add_init_tap(p->next,arg);
119 return(p);
122 static struct init_tap *free_init_tap(struct init_tap *p)
124 if (p != NULL) {
125 free_init_tap(p->next);
126 free(p);
128 return NULL;
131 static int parseopt(int c, char *optarg)
133 int outc=0;
134 switch (c) {
135 case 't':
136 hinit_tap=add_init_tap(hinit_tap,optarg);
137 break;
138 default:
139 outc=c;
141 return outc;
144 #ifdef VDE_LINUX
145 int open_tap(char *dev)
147 struct ifreq ifr;
148 int fd;
150 if((fd = open("/dev/net/tun", O_RDWR)) < 0){
151 printlog(LOG_ERR,"Failed to open /dev/net/tun %s",strerror(errno));
152 return(-1);
154 memset(&ifr, 0, sizeof(ifr));
155 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
156 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1);
157 /*printf("dev=\"%s\", ifr.ifr_name=\"%s\"\n", ifr.ifr_name, dev);*/
158 if(ioctl(fd, TUNSETIFF, (void *) &ifr) < 0){
159 printlog(LOG_ERR,"TUNSETIFF failed %s",strerror(errno));
160 close(fd);
161 return(-1);
163 return(fd);
165 #endif
167 #ifdef VDE_DARWIN
168 int open_tap(char *dev)
170 int fd;
171 int prefixlen = strlen(TAP_PREFIX);
172 char *path = NULL;
173 if (*dev == '/')
174 fd=open(dev, O_RDWR);
175 else
177 path = malloc(strlen(dev) + prefixlen + 1);
178 if (path != NULL)
180 snprintf(path, strlen(dev) + prefixlen + 1, "%s%s", TAP_PREFIX, dev);
181 fd=open(path, O_RDWR);
182 free(path);
184 else
185 fd = -1;
188 if (fd < 0)
190 printlog(LOG_ERR,"Failed to open tap device %s: %s", (*dev == '/') ? dev : path, strerror(errno));
191 return(-1);
193 return fd;
195 #endif
197 static int newport(int fd, int portno)
199 return fd;
202 static int newtap(char *dev)
204 int tap_fd;
205 tap_fd = open_tap(dev);
206 if (tap_fd>0) {
207 int portno=setup_ep(0,tap_fd,NULL,&modfun);
208 if (portno >= 0) {
209 setup_description(portno,tap_fd,dev);
210 add_fd(tap_fd,tap_type,portno);
211 return portno;
212 } else
213 return -1;
214 } else
215 return -1;
218 static void init(void)
220 if(hinit_tap != NULL) {
221 struct init_tap *p;
222 tap_type=add_type(&swmi,1);
223 for(p=hinit_tap;p != NULL;p=p->next) {
224 if (newtap(p->tap_dev) < 0)
225 printlog(LOG_ERR,"ERROR OPENING tap interface: %s",p->tap_dev);
227 hinit_tap=free_init_tap(hinit_tap);
231 static void delep (int fd, void* data, void *descr)
233 if (descr) free(descr);
236 void start_tuntap(void)
238 modfun.modname=swmi.swmname=MODULENAME;
239 swmi.swmnopts=Nlong_options;
240 swmi.swmopts=long_options;
241 swmi.usage=usage;
242 swmi.parseopt=parseopt;
243 swmi.init=init;
244 swmi.handle_input=handle_input;
245 swmi.cleanup=cleanup;
246 modfun.sender=send_tap;
247 modfun.newport=newport;
248 modfun.delep=delep;
249 modfun.delport=closeport;
250 add_swm(&swmi);