1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
* Copyright (C) 2006-2009 B.A.T.M.A.N. contributors:
*
* Marek Lindner
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <unistd.h> /* close() */
#include <fcntl.h> /* open(), O_RDWR */
#include <errno.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> /* inet_ntop() */
#include <netinet/ip.h> /* iph */
#include <linux/if_tun.h> /* TUNSETPERSIST, ... */
#include <linux/if.h> /* ifr_if, ifr_tun */
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h> /* system() */
#include <sys/wait.h> /* WEXITSTATUS */
#include "../os.h"
#include "../batman.h"
#define IPTABLES_ADD_MASQ "iptables -t nat -A POSTROUTING -o %s -j MASQUERADE"
#define IPTABLES_DEL_MASQ "iptables -t nat -D POSTROUTING -o %s -j MASQUERADE"
#define IPTABLES_ADD_ACC "iptables -t nat -I POSTROUTING -s %s/%i -j ACCEPT"
#define IPTABLES_DEL_ACC "iptables -t nat -D POSTROUTING -s %s/%i -j ACCEPT"
int run_cmd(char *cmd) {
int error, pipes[2], stderr = -1, ret = 0;
char error_log[256];
if (pipe(pipes) < 0) {
debug_output(3, "Warning - could not create a pipe to '%s': %s\n", cmd, strerror(errno));
return -1;
}
memset(error_log, 0, 256);
/* save stderr */
stderr = dup(STDERR_FILENO);
/* connect the commands output with the pipe for later logging */
dup2(pipes[1], STDERR_FILENO);
close(pipes[1]);
error = system(cmd);
/* copy stderr back */
dup2(stderr, STDERR_FILENO);
close(stderr);
if ((error < 0) || (WEXITSTATUS(error) != 0)) {
ret = read(pipes[0], error_log, sizeof(error_log));
debug_output(3, "Warning - command '%s' returned an error\n", cmd);
if (ret > 0)
debug_output(3, " %s\n", error_log);
ret = -1;
}
close(pipes[0]);
return ret;
}
/* Probe for iptables binary availability */
int probe_nat_tool(void) {
return run_cmd("which iptables > /dev/null");
}
void exec_iptables_rule(char *cmd, int8_t route_action) {
if (disable_client_nat)
return;
if (nat_tool_avail == -1) {
debug_output(3, "Warning - could not %sactivate NAT: iptables binary not found!\n", (route_action == ROUTE_ADD ? "" : "de"));
debug_output(3, " You may need to run this command: %s\n", cmd);
return;
}
run_cmd(cmd);
}
void add_nat_rule(char *dev) {
char cmd[100];
sprintf(cmd, IPTABLES_ADD_MASQ, dev);
exec_iptables_rule(cmd, ROUTE_ADD);
}
void del_nat_rule(char *dev) {
char cmd[100];
sprintf(cmd, IPTABLES_DEL_MASQ, dev);
exec_iptables_rule(cmd, ROUTE_DEL);
}
void hna_local_update_nat(uint32_t hna_ip, uint8_t netmask, int8_t route_action) {
char cmd[100], ip_addr[16];
inet_ntop(AF_INET, &hna_ip, ip_addr, sizeof(ip_addr));
if (route_action == ROUTE_DEL)
sprintf(cmd, IPTABLES_DEL_ACC, ip_addr, netmask);
else
sprintf(cmd, IPTABLES_ADD_ACC, ip_addr, netmask);
exec_iptables_rule(cmd, route_action);
}
/* Probe for tun interface availability */
int8_t probe_tun(uint8_t print_to_stderr) {
int32_t fd;
if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
if (print_to_stderr)
fprintf( stderr, "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n" );
else
debug_output( 0, "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n" );
return 0;
}
close( fd );
return 1;
}
int8_t del_dev_tun( int32_t fd ) {
if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) {
debug_output( 0, "Error - can't delete tun device: %s\n", strerror(errno) );
return -1;
}
close( fd );
return 1;
}
int8_t add_dev_tun( struct batman_if *batman_if, uint32_t tun_addr, char *tun_dev, size_t tun_dev_size, int32_t *fd, int32_t *ifi ) {
int32_t tmp_fd, sock_opts;
struct ifreq ifr_tun, ifr_if;
struct sockaddr_in addr;
/* set up tunnel device */
memset( &ifr_tun, 0, sizeof(ifr_tun) );
memset( &ifr_if, 0, sizeof(ifr_if) );
ifr_tun.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy( ifr_tun.ifr_name, "gate%d", IFNAMSIZ );
if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
debug_output( 0, "Error - can't create tun device (/dev/net/tun): %s\n", strerror(errno) );
return -1;
}
if ( ( ioctl( *fd, TUNSETIFF, (void *)&ifr_tun ) ) < 0 ) {
debug_output( 0, "Error - can't create tun device (TUNSETIFF): %s\n", strerror(errno) );
close(*fd);
return -1;
}
if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) {
debug_output( 0, "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) );
close(*fd);
return -1;
}
tmp_fd = socket( AF_INET, SOCK_DGRAM, 0 );
if ( tmp_fd < 0 ) {
debug_output( 0, "Error - can't create tun device (udp socket): %s\n", strerror(errno) );
del_dev_tun( *fd );
return -1;
}
/* set ip of this end point of tunnel */
memset( &addr, 0, sizeof(addr) );
addr.sin_addr.s_addr = tun_addr;
addr.sin_family = AF_INET;
memcpy( &ifr_tun.ifr_addr, &addr, sizeof(struct sockaddr) );
if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr_tun) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
if ( ioctl( tmp_fd, SIOCGIFINDEX, &ifr_tun ) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCGIFINDEX): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
*ifi = ifr_tun.ifr_ifindex;
if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr_tun) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
ifr_tun.ifr_flags |= IFF_UP;
ifr_tun.ifr_flags |= IFF_RUNNING;
if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr_tun) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
/* get MTU from real interface */
strncpy( ifr_if.ifr_name, batman_if->dev, IFNAMSIZ - 1 );
if ( ioctl( tmp_fd, SIOCGIFMTU, &ifr_if ) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
/* set MTU of tun interface: real MTU - 29 */
if ( ifr_if.ifr_mtu < 100 ) {
debug_output( 0, "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n" );
} else {
ifr_tun.ifr_mtu = ifr_if.ifr_mtu - 29;
if ( ioctl( tmp_fd, SIOCSIFMTU, &ifr_tun ) < 0 ) {
debug_output( 0, "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
}
}
/* make tun socket non blocking */
sock_opts = fcntl( *fd, F_GETFL, 0 );
fcntl( *fd, F_SETFL, sock_opts | O_NONBLOCK );
strncpy( tun_dev, ifr_tun.ifr_name, tun_dev_size - 1 );
close( tmp_fd );
return 1;
}
int8_t set_tun_addr( int32_t fd, uint32_t tun_addr, char *tun_dev ) {
struct sockaddr_in addr;
struct ifreq ifr_tun;
memset( &ifr_tun, 0, sizeof(ifr_tun) );
memset( &addr, 0, sizeof(addr) );
addr.sin_addr.s_addr = tun_addr;
addr.sin_family = AF_INET;
memcpy( &ifr_tun.ifr_addr, &addr, sizeof(struct sockaddr) );
strncpy( ifr_tun.ifr_name, tun_dev, IFNAMSIZ - 1 );
if ( ioctl( fd, SIOCSIFADDR, &ifr_tun) < 0 ) {
debug_output( 0, "Error - can't set tun address (SIOCSIFADDR): %s\n", strerror(errno) );
return -1;
}
return 1;
}
|