119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
#include "util.h"
|
|
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
#include <sys/socket.h>
|
|
#include <linux/if.h>
|
|
#include <linux/if_tun.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/ip.h>
|
|
|
|
void* xmalloc( size_t bytes )
|
|
{
|
|
void* result = malloc( bytes );
|
|
|
|
if ( bytes > 0 && result == NULL ) {
|
|
warn( "Couldn't allocate memory, exiting!" );
|
|
exit(2);
|
|
|
|
}
|
|
|
|
memset( result, 0, bytes );
|
|
return result;
|
|
}
|
|
|
|
int create_tun( const char* name )
|
|
{
|
|
int fd, err;
|
|
struct ifreq ifr;
|
|
|
|
if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
|
|
warn( "Error %s opening tun to create %s", strerror(errno), name );
|
|
return -1;
|
|
}
|
|
|
|
memset( &ifr, 0, sizeof( struct ifreq ) );
|
|
|
|
ifr.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_UP;
|
|
strncpy( ifr.ifr_name, name, IFNAMSIZ );
|
|
|
|
if ( (err = ioctl( fd, TUNSETIFF, (void*) &ifr ) ) < 0 ) {
|
|
warn( "Error creating tun device %s: %s", name, strerror(errno) );
|
|
close( fd );
|
|
return -1;
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
|
|
// shamelessly copied from:
|
|
// http://www.roman10.net/how-to-calculate-iptcpudp-checksumpart-2-implementation/
|
|
unsigned short compute_checksum(unsigned short *addr, unsigned int count) {
|
|
|
|
unsigned long sum = 0;
|
|
|
|
while (count > 1) {
|
|
sum += * addr++;
|
|
count -= 2;
|
|
}
|
|
|
|
//if any bytes left, pad the bytes and add
|
|
if(count > 0) {
|
|
sum += ((*addr)&htons(0xFF00));
|
|
}
|
|
|
|
//Fold sum to 16 bits: add carrier to result
|
|
while (sum>>16) {
|
|
sum = (sum & 0xffff) + (sum >> 16);
|
|
}
|
|
|
|
//one's complement
|
|
sum = ~sum;
|
|
|
|
return ((unsigned short)sum);
|
|
|
|
}
|
|
|
|
void compute_ip_checksum(struct iphdr* pkt)
|
|
{
|
|
pkt->check = 0x0000;
|
|
pkt->check = compute_checksum( (unsigned short*) pkt, pkt->ihl * 4 );
|
|
}
|
|
|
|
|
|
|
|
int link_set_up( char *link_name, int state )
|
|
{
|
|
int fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_IP );
|
|
struct ifreq ifr;
|
|
|
|
|
|
memset( &ifr, 0, sizeof( struct ifreq ) );
|
|
strncpy( ifr.ifr_name, link_name, IFNAMSIZ );
|
|
|
|
if ( ioctl( fd, SIOCGIFFLAGS, (void*)&ifr ) < 0 ) {
|
|
warn( "Failed to read interface flags for %s: %s", link_name, strerror(errno) );
|
|
return 0;
|
|
}
|
|
|
|
if ( state ) {
|
|
ifr.ifr_flags |= IFF_UP;
|
|
} else {
|
|
ifr.ifr_flags = ( ifr.ifr_flags & ~IFF_UP );
|
|
}
|
|
|
|
if ( ioctl( fd, SIOCSIFFLAGS, (void*)&ifr ) < 0 ) {
|
|
warn( "Failed to set link state for interface %s to %i: %s", link_name, state, strerror(errno) );
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
|
|
} |