The following commit has been merged in the master branch: commit 73eca6e18e0a76af4f8d9baff45f7576c774ba8c Author: Stefan Sperling stsp@stsp.in-berlin.de Date: Wed Sep 20 20:36:47 2006 +0200
Move current IP-IP tunnel code into linux.c. Add fake placeholder implementation of IP-IP tunnel inferface for FreeBSD.
diff --git a/freebsd.c b/freebsd.c index 90f4b7d..c490df1 100644 --- a/freebsd.c +++ b/freebsd.c @@ -183,3 +183,13 @@ void add_del_route(unsigned int dest, unsigned int router, int del, } }
+ +int del_ipip_tun(int fd) { + return 1; +} + +int add_ipip_tun( struct batman_if *batman_if, unsigned int dest_addr, char *tun_dev, int *fd ) +{ + return 0; +} + diff --git a/linux.c b/linux.c index 9ea92ee..54309f8 100755 --- a/linux.c +++ b/linux.c @@ -32,6 +32,10 @@ #include <signal.h> #include <stdlib.h> #include <linux/if.h> +#include <netinet/ip.h> +#include <asm/types.h> +#include <linux/if_tun.h> +#include <linux/if_tunnel.h>
#include "os.h" #include "batman.h" @@ -109,3 +113,121 @@ void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, i } }
+int del_ipip_tun( int fd ) { + + if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) { + + perror("TUNSETPERSIST"); + return -1; + + } + + close( fd ); + + return 1; + +} + +int add_ipip_tun( struct batman_if *batman_if, unsigned int dest_addr, char *tun_dev, int *fd ) { + + int tmp_fd; + struct ifreq ifr; + struct sockaddr_in addr; + + /* set up tunnel device */ + memset( &ifr, 0, sizeof(ifr) ); + ifr.ifr_flags = IFF_TUN; + + if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) { + + perror("/dev/net/tun"); + return -1; + + } + + if ( ( ioctl( *fd, TUNSETIFF, (void *) &ifr ) ) < 0 ) { + + perror("TUNSETIFF"); + close(*fd); + return -1; + + } + + if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) { + + perror("TUNSETPERSIST"); + close(*fd); + return -1; + + } + + + tmp_fd = socket(AF_INET, SOCK_DGRAM, 0); + + if ( tmp_fd < 0 ) { + fprintf(stderr, "Cannot create send socket: %s", strerror(errno)); + del_ipip_tun( *fd ); + return -1; + } + + + /* set ip of this end point of tunnel */ + memset( &addr, 0, sizeof(addr) ); + addr.sin_addr.s_addr = batman_if->addr.sin_addr.s_addr; + addr.sin_family = AF_INET; + memcpy( &ifr.ifr_addr, &addr, sizeof(struct sockaddr) ); + + + if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr) < 0 ) { + + perror("SIOCSIFADDR"); + del_ipip_tun( *fd ); + close( tmp_fd ); + return -1; + + } + + /* set ip of this remote point of tunnel */ + memset( &addr, 0, sizeof(addr) ); + addr.sin_addr.s_addr = dest_addr; + addr.sin_family = AF_INET; + memcpy( &ifr.ifr_addr, &addr, sizeof(struct sockaddr) ); + + if ( ioctl( tmp_fd, SIOCSIFDSTADDR, &ifr) < 0 ) { + + perror("SIOCSIFDSTADDR"); + del_ipip_tun( *fd ); + close( tmp_fd ); + return -1; + + } + + if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr) < 0 ) { + + perror("SIOCGIFFLAGS"); + del_ipip_tun( *fd ); + close( tmp_fd ); + return -1; + + } + + ifr.ifr_flags |= IFF_UP; + ifr.ifr_flags |= IFF_RUNNING; + + if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr) < 0 ) { + + perror("SIOCSIFFLAGS"); + del_ipip_tun( *fd ); + close( tmp_fd ); + return -1; + + } + + close( tmp_fd ); + strncpy( tun_dev, ifr.ifr_name, IFNAMSIZ ); + + return 1; + +} + + diff --git a/posix.c b/posix.c index 59977a1..67f9d45 100644 --- a/posix.c +++ b/posix.c @@ -32,10 +32,6 @@ #include <unistd.h> #include <signal.h> #include <stdlib.h> -#include <asm/types.h> -#include <netinet/ip.h> -#include <linux/if_tun.h> -#include <linux/if_tunnel.h> #include <sys/stat.h> #include <fcntl.h>
@@ -129,129 +125,6 @@ void *keep_tun_alive( void *unused ) {
}
- - -int del_ipip_tun( int fd ) { - - if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) { - - perror("TUNSETPERSIST"); - return -1; - - } - - close( fd ); - - return 1; - -} - - - -int add_ipip_tun( struct batman_if *batman_if, unsigned int dest_addr, char *tun_dev, int *fd ) { - - int tmp_fd; - struct ifreq ifr; - struct sockaddr_in addr; - - /* set up tunnel device */ - memset( &ifr, 0, sizeof(ifr) ); - ifr.ifr_flags = IFF_TUN; - - if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) { - - perror("/dev/net/tun"); - return -1; - - } - - if ( ( ioctl( *fd, TUNSETIFF, (void *) &ifr ) ) < 0 ) { - - perror("TUNSETIFF"); - close(*fd); - return -1; - - } - - if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) { - - perror("TUNSETPERSIST"); - close(*fd); - return -1; - - } - - - tmp_fd = socket(AF_INET, SOCK_DGRAM, 0); - - if ( tmp_fd < 0 ) { - fprintf(stderr, "Cannot create send socket: %s", strerror(errno)); - del_ipip_tun( *fd ); - return -1; - } - - - /* set ip of this end point of tunnel */ - memset( &addr, 0, sizeof(addr) ); - addr.sin_addr.s_addr = batman_if->addr.sin_addr.s_addr; - addr.sin_family = AF_INET; - memcpy( &ifr.ifr_addr, &addr, sizeof(struct sockaddr) ); - - - if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr) < 0 ) { - - perror("SIOCSIFADDR"); - del_ipip_tun( *fd ); - close( tmp_fd ); - return -1; - - } - - /* set ip of this remote point of tunnel */ - memset( &addr, 0, sizeof(addr) ); - addr.sin_addr.s_addr = dest_addr; - addr.sin_family = AF_INET; - memcpy( &ifr.ifr_addr, &addr, sizeof(struct sockaddr) ); - - if ( ioctl( tmp_fd, SIOCSIFDSTADDR, &ifr) < 0 ) { - - perror("SIOCSIFDSTADDR"); - del_ipip_tun( *fd ); - close( tmp_fd ); - return -1; - - } - - if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr) < 0 ) { - - perror("SIOCGIFFLAGS"); - del_ipip_tun( *fd ); - close( tmp_fd ); - return -1; - - } - - ifr.ifr_flags |= IFF_UP; - ifr.ifr_flags |= IFF_RUNNING; - - if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr) < 0 ) { - - perror("SIOCSIFFLAGS"); - del_ipip_tun( *fd ); - close( tmp_fd ); - return -1; - - } - - close( tmp_fd ); - strncpy( tun_dev, ifr.ifr_name, IFNAMSIZ ); - - return 1; - -} - - - void del_default_route() {
curr_gateway = NULL;