2
* Copyright (C) 2006-2009 BATMAN contributors:
4
* Thomas Lopatic, Marek Lindner
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of version 2 of the GNU General Public
8
* License as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32
#include <sys/ioctl.h>
33
#include <sys/socket.h>
35
#include <arpa/inet.h>
41
#include "../batman.h"
50
static int my_daemon(void) {
70
/* Make certain we are not a session leader, or else we might reacquire a controlling terminal */
76
if ( ( fd = open(_PATH_DEVNULL, O_RDWR, 0) ) != -1 ) {
78
dup2(fd, STDIN_FILENO);
79
dup2(fd, STDOUT_FILENO);
80
dup2(fd, STDERR_FILENO);
91
static void create_routing_pipe(void)
96
printf("Could not create a pipe to '%s': %s\n", policy_routing_script, strerror(errno));
100
if ((policy_routing_script_pid = fork()) < 0) {
102
printf("Could not fork to execute '%s': %s\n", policy_routing_script, strerror(errno));
106
} else if (policy_routing_script_pid > 0) {
109
policy_routing_pipe = fd[1];
111
/* make pipe non blocking */
112
pipe_opts = fcntl(policy_routing_pipe, F_GETFL, 0);
113
fcntl(policy_routing_pipe, F_SETFL, pipe_opts | O_NONBLOCK);
120
if (fd[0] != STDIN_FILENO) {
122
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) {
123
printf("Could not dup2 to redirect stdin to '%s'\n", policy_routing_script);
131
signal(SIGINT, SIG_IGN);
132
signal(SIGTERM, SIG_IGN);
133
signal(SIGPIPE, SIG_IGN);
135
if (execl("/bin/sh", "/bin/sh", "-c", policy_routing_script, NULL) < 0)
136
printf("Could not execute '%s': %s\n", policy_routing_script, strerror(errno));
141
void apply_init_args( int argc, char *argv[] ) {
143
struct in_addr tmp_ip_holder;
144
struct batman_if *batman_if;
145
struct hna_task *hna_task;
146
struct debug_level_info *debug_level_info;
147
uint8_t found_args = 1, batch_mode = 0, info_output = 0, was_hna = 0;
150
int32_t optchar, option_index, recv_buff_len, bytes_written, download_speed = 0, upload_speed = 0;
151
char str1[16], str2[16], *slash_ptr, *unix_buff, *buff_ptr, *cr_ptr;
152
char routing_class_opt = 0, gateway_class_opt = 0, pref_gw_opt = 0;
153
char hop_penalty_opt = 0, purge_timeout_opt = 0;
154
uint32_t vis_server = 0;
155
struct option long_options[] =
157
{"policy-routing-script", required_argument, 0, 'n'},
158
{"hop-penalty", required_argument, 0, 'm'},
159
{"purge-timeout", required_argument, 0, 'q'},
160
{"disable-aggregation", no_argument, 0, 'x'},
161
{"disable-client-nat", no_argument, 0, 'z'},
165
memset( &tmp_ip_holder, 0, sizeof (struct in_addr) );
169
if ( strstr( SOURCE_VERSION, "-" ) != NULL )
170
printf( "WARNING: You are using the unstable batman branch. If you are interested in *using* batman get the latest stable release !\n" );
172
while ( ( optchar = getopt_long( argc, argv, "a:A:bcd:hHio:g:p:r:s:vV", long_options, &option_index ) ) != -1 ) {
177
hna_local_task_add_str(optarg, ROUTE_ADD, 0);
179
/* increment found_args only by one if optarg and optchar are directly following each other
180
increment found_args by two if there is some space between the arguments */
181
found_args += ((*((char*)(optarg - 1)) == optchar ) ? 1 : 2);
185
hna_local_task_add_str(optarg, ROUTE_DEL, 0);
187
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
204
debug_level = strtol( optarg, NULL, 10 );
206
if ( ( errno == ERANGE ) || ( errno != 0 && debug_level == 0 ) ) {
213
if ( debug_level > debug_level_max ) {
215
printf( "Invalid debug level: %i\nDebug level has to be between 0 and %i.\n", debug_level, debug_level_max );
220
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
225
if ( ( slash_ptr = strchr( optarg, '/' ) ) != NULL )
230
download_speed = strtol( optarg, NULL, 10 );
232
if ( ( errno == ERANGE ) || ( errno != 0 && download_speed == 0 ) ) {
239
if ( ( strlen( optarg ) > 4 ) && ( ( strncmp( optarg + strlen( optarg ) - 4, "MBit", 4 ) == 0 ) || ( strncmp( optarg + strlen( optarg ) - 4, "mbit", 4 ) == 0 ) || ( strncmp( optarg + strlen( optarg ) - 4, "Mbit", 4 ) == 0 ) ) )
240
download_speed *= 1024;
242
if ( slash_ptr != NULL ) {
246
upload_speed = strtol( slash_ptr + 1, NULL, 10 );
248
if ( ( errno == ERANGE ) || ( errno != 0 && upload_speed == 0 ) ) {
253
if ( ( strlen( slash_ptr + 1 ) > 4 ) && ( ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "MBit", 4 ) == 0 ) || ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "mbit", 4 ) == 0 ) || ( strncmp( slash_ptr + 1 + strlen( slash_ptr + 1 ) - 4, "Mbit", 4 ) == 0 ) ) )
254
upload_speed *= 1024;
260
gateway_class_opt = 1;
262
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
275
policy_routing_script = optarg;
277
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
284
hop_penalty = strtol( optarg, NULL, 10 );
287
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
294
purge_timeout = strtol(optarg, NULL, 10);
295
purge_timeout_opt = 1;
297
found_args += ((*((char*)( optarg - 1)) == optchar ) ? 1 : 2);
304
originator_interval = strtol( optarg, NULL, 10 );
306
if ( originator_interval < 1 ) {
308
printf( "Invalid originator interval specified: %i.\nThe Interval has to be greater than 0.\n", originator_interval );
313
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
320
if ( inet_pton( AF_INET, optarg, &tmp_ip_holder ) < 1 ) {
322
printf( "Invalid preferred gateway IP specified: %s\n", optarg );
327
pref_gateway = tmp_ip_holder.s_addr;
331
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
338
routing_class = strtol(optarg, NULL, 10);
339
routing_class_opt = 1;
341
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
347
if ( inet_pton( AF_INET, optarg, &tmp_ip_holder ) < 1 ) {
349
printf( "Invalid preferred visualation server IP specified: %s\n", optarg );
354
vis_server = tmp_ip_holder.s_addr;
357
found_args += ((*((char*)( optarg - 1)) == optchar) ? 1 : 2);
362
printf("B.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
369
printf("\x1B[0;0HB.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
370
printf("\x1B[9;0H \t May the bat guide your path ...\n\n\n");
375
aggregation_enabled = 0;
380
disable_client_nat = 1;
393
if (!unix_client && info_output) {
400
if ( ( download_speed > 0 ) && ( upload_speed == 0 ) )
401
upload_speed = download_speed / 5;
403
if (download_speed > 0) {
405
gateway_class = get_gw_class(download_speed, upload_speed);
406
get_gw_speeds(gateway_class, &download_speed, &upload_speed);
410
if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) {
411
fprintf( stderr, "Error - routing class can't be set while gateway class is in use !\n" );
416
if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) {
417
fprintf( stderr, "Error - preferred gateway can't be set while gateway class is in use !\n" );
422
/* use routing class 1 if none specified */
423
if ( ( routing_class == 0 ) && ( pref_gateway != 0 ) )
424
routing_class = DEFAULT_ROUTING_CLASS;
426
if ( ( ( routing_class != 0 ) || ( gateway_class != 0 ) ) && ( !probe_tun(1) ) )
431
if (argc <= found_args) {
432
fprintf(stderr, "Error - no interface specified\n");
437
if ((disable_client_nat) && (routing_class == 0))
438
fprintf(stderr, "Warning - the activated option '--disable-client-nat' has no effect without setting a routing class.\n");
440
nat_tool_avail = probe_nat_tool();
442
if (policy_routing_script != NULL)
443
create_routing_pipe();
445
signal( SIGINT, handler );
446
signal( SIGTERM, handler );
447
signal( SIGPIPE, SIG_IGN );
448
signal( SIGSEGV, segmentation_fault );
450
debug_clients.fd_list = debugMalloc( sizeof(struct list_head_first *) * debug_level_max, 203 );
451
debug_clients.mutex = debugMalloc( sizeof(pthread_mutex_t *) * debug_level_max, 209 );
452
debug_clients.clients_num = debugMalloc( sizeof(int16_t) * debug_level_max, 209 );
454
for (res = 0; res < debug_level_max; res++) {
456
debug_clients.fd_list[res] = debugMalloc(sizeof(struct list_head_first), 204);
457
((struct list_head_first *)debug_clients.fd_list[res])->next = debug_clients.fd_list[res];
458
((struct list_head_first *)debug_clients.fd_list[res])->prev = debug_clients.fd_list[res];
460
debug_clients.mutex[res] = debugMalloc(sizeof(pthread_mutex_t), 209);
461
pthread_mutex_init((pthread_mutex_t *)debug_clients.mutex[res], NULL);
463
debug_clients.clients_num[res] = 0;
466
if ( flush_routes_rules(0) < 0 )
469
if ( flush_routes_rules(1) < 0 )
472
while (argc > found_args) {
474
if (argv[found_args][0] == '-') {
475
fprintf(stderr, "%s: invalid option -- %s\n", argv[0], argv[found_args]);
480
batman_if = debugMalloc(sizeof(struct batman_if), 206);
481
memset(batman_if, 0, sizeof(struct batman_if));
482
INIT_LIST_HEAD( &batman_if->list );
484
batman_if->dev = argv[found_args];
485
batman_if->if_num = found_ifs;
487
batman_if->if_rp_filter_old = -1;
488
batman_if->if_send_redirects_old = -1;
490
list_add_tail(&batman_if->list, &if_list);
492
init_interface(batman_if);
494
if (batman_if->if_num > 0)
495
hna_local_task_add_ip(batman_if->addr.sin_addr.s_addr, 32, ROUTE_ADD);
497
if (batman_if->if_active) {
499
addr_to_string(batman_if->addr.sin_addr.s_addr, str1, sizeof (str1));
500
addr_to_string(batman_if->broad.sin_addr.s_addr, str2, sizeof (str2));
502
printf("Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2);
506
printf("Not using interface %s (retrying later): interface not active\n", batman_if->dev);
517
unix_if.unix_sock = socket( AF_LOCAL, SOCK_STREAM, 0 );
519
memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
520
unix_if.addr.sun_family = AF_LOCAL;
521
strcpy( unix_if.addr.sun_path, UNIX_PATH );
523
if ( bind ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof (struct sockaddr_un) ) < 0 ) {
525
printf( "Error - can't bind unix socket '%s': %s\n", UNIX_PATH, strerror(errno) );
531
if ( listen( unix_if.unix_sock, 10 ) < 0 ) {
533
printf( "Error - can't listen unix socket '%s': %s\n", UNIX_PATH, strerror(errno) );
540
if (debug_level == 0) {
542
if (my_daemon() < 0) {
544
printf("Error - can't fork to background: %s\n", strerror(errno));
550
openlog("batmand", LOG_PID, LOG_DAEMON);
554
printf("B.A.T.M.A.N. %s%s (compatibility version %i)\n", SOURCE_VERSION, (strlen(REVISION_VERSION) > 3 ? REVISION_VERSION : ""), COMPAT_VERSION);
556
debug_clients.clients_num[ debug_level - 1 ]++;
557
debug_level_info = debugMalloc( sizeof(struct debug_level_info), 205 );
558
INIT_LIST_HEAD( &debug_level_info->list );
559
debug_level_info->fd = 2;
560
list_add( &debug_level_info->list, (struct list_head_first *)debug_clients.fd_list[debug_level - 1] );
564
log_facility_active = 1;
566
pthread_create( &unix_if.listen_thread_id, NULL, &unix_listen, NULL );
568
/* add rule for hna networks */
569
add_del_rule(0, 0, BATMAN_RT_TABLE_NETWORKS, BATMAN_RT_PRIO_UNREACH - 1, 0, RULE_TYPE_DST, RULE_ADD);
571
/* add unreachable routing table entry */
572
add_del_route(0, 0, 0, 0, 0, "unknown", BATMAN_RT_TABLE_UNREACH, ROUTE_TYPE_UNREACHABLE, ROUTE_ADD);
574
if (routing_class > 0) {
575
if (add_del_interface_rules(RULE_ADD) < 0) {
581
memset(&vis_if, 0, sizeof(vis_if));
584
vis_if.addr.sin_family = AF_INET;
585
vis_if.addr.sin_port = htons(PORT + 2);
586
vis_if.addr.sin_addr.s_addr = vis_server;
587
vis_if.sock = socket( PF_INET, SOCK_DGRAM, 0 );
590
if (gateway_class != 0)
593
if ( debug_level > 0 ) {
595
printf( "debug level: %i\n", debug_level );
597
if ( originator_interval != 1000 )
598
printf( "originator interval: %i\n", originator_interval );
600
if ( gateway_class > 0 )
601
printf( "gateway class: %i -> propagating: %i%s/%i%s\n", gateway_class, ( download_speed > 2048 ? download_speed / 1024 : download_speed ), ( download_speed > 2048 ? "MBit" : "KBit" ), ( upload_speed > 2048 ? upload_speed / 1024 : upload_speed ), ( upload_speed > 2048 ? "MBit" : "KBit" ) );
603
if ( routing_class > 0 )
604
printf( "routing class: %i\n", routing_class );
606
if ( pref_gateway > 0 ) {
607
addr_to_string( pref_gateway, str1, sizeof(str1) );
608
printf( "preferred gateway: %s\n", str1 );
611
if ( vis_server > 0 ) {
612
addr_to_string( vis_server, str1, sizeof(str1) );
613
printf( "visualisation server: %s\n", str1 );
618
/* connect to running batmand via unix socket */
622
unix_if.unix_sock = socket( AF_LOCAL, SOCK_STREAM, 0 );
624
memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
625
unix_if.addr.sun_family = AF_LOCAL;
626
strcpy( unix_if.addr.sun_path, UNIX_PATH );
628
if ( connect ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof(struct sockaddr_un) ) < 0 ) {
630
printf( "Error - can't connect to unix socket '%s': %s ! Is batmand running on this host ?\n", UNIX_PATH, strerror(errno) );
631
close( unix_if.unix_sock );
636
unix_buff = debugMalloc( 1501, 5001 );
638
if ( debug_level > 0 ) {
640
if ( debug_level <= debug_level_max ) {
642
snprintf( unix_buff, 10, "d:%c", debug_level );
644
if ( ( debug_level > 2 ) && ( batch_mode ) )
645
printf( "WARNING: Your chosen debug level (%i) does not support batch mode !\n", debug_level );
649
} else if ( routing_class_opt ) {
652
snprintf( unix_buff, 10, "r:%c", routing_class );
654
} else if ( pref_gw_opt ) {
657
addr_to_string( pref_gateway, str1, sizeof(str1) );
658
snprintf( unix_buff, 20, "p:%s", str1 );
660
} else if ( gateway_class_opt ) {
663
snprintf( unix_buff, 10, "g:%c", gateway_class );
665
} else if (hop_penalty_opt) {
668
snprintf(unix_buff, 10, "m:%c", hop_penalty);
670
} else if (purge_timeout_opt) {
673
snprintf(unix_buff, 20, "q:%u", purge_timeout);
675
} else if (info_output) {
678
snprintf( unix_buff, 10, "i" );
680
} else if (!list_empty(&hna_chg_list)) {
682
batch_mode = was_hna = 1;
683
hna_task = (struct hna_task *)hna_chg_list.next;
684
addr_to_string(hna_task->addr, str1, sizeof(str1));
685
snprintf(unix_buff, 30, "%c:%s/%i",
686
(hna_task->route_action == ROUTE_ADD ? 'a' : 'A'),
687
str1, hna_task->netmask);
689
list_del((struct list_head *)&hna_chg_list, &hna_task->list, &hna_chg_list);
690
debugFree(hna_task, 1298);
695
snprintf(unix_buff, 10, "y");
699
if ( write( unix_if.unix_sock, unix_buff, 30 ) < 0 ) {
701
printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
702
close( unix_if.unix_sock );
703
debugFree( unix_buff, 5101 );
708
while ( ( recv_buff_len = read( unix_if.unix_sock, unix_buff, 1500 ) ) > 0 ) {
710
unix_buff[recv_buff_len] = '\0';
712
buff_ptr = unix_buff;
715
while ( ( cr_ptr = strchr( buff_ptr, '\n' ) ) != NULL ) {
719
if ( strncmp( buff_ptr, "EOD", 3 ) == 0 ) {
724
} else if ( strncmp( buff_ptr, "BOD", 3 ) == 0 ) {
731
printf( "%s\n", buff_ptr );
735
bytes_written += strlen( buff_ptr ) + 1;
736
buff_ptr = cr_ptr + 1;
740
if ( bytes_written != recv_buff_len )
741
printf( "%s", buff_ptr );
746
close( unix_if.unix_sock );
747
debugFree( unix_buff, 5102 );
749
if ( recv_buff_len < 0 ) {
751
printf( "Error - can't read from unix socket: %s\n", strerror(errno) );
757
printf( "Connection terminated by remote host\n" );
761
if ((was_hna) && (!list_empty(&hna_chg_list)))
770
void interface_listen_sockets(void)
772
struct list_head *list_pos;
773
struct batman_if *batman_if;
775
FD_ZERO(&receive_wait_set);
776
receive_max_sock = 0;
778
list_for_each(list_pos, &if_list) {
779
batman_if = list_entry(list_pos, struct batman_if, list);
781
if (batman_if->if_active) {
782
if (batman_if->udp_recv_sock > receive_max_sock)
783
receive_max_sock = batman_if->udp_recv_sock;
785
FD_SET(batman_if->udp_recv_sock, &receive_wait_set);
790
static int is_interface_up(char *dev)
792
struct ifreq int_req;
795
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
798
memset(&int_req, 0, sizeof (struct ifreq));
799
strncpy(int_req.ifr_name, dev, IFNAMSIZ - 1);
801
if (ioctl(sock, SIOCGIFFLAGS, &int_req) < 0)
804
if (!(int_req.ifr_flags & IFF_UP))
807
if (ioctl(sock, SIOCGIFADDR, &int_req) < 0)
818
void deactivate_interface(struct batman_if *batman_if)
820
if (batman_if->udp_recv_sock != 0)
821
close(batman_if->udp_recv_sock);
823
if (batman_if->udp_send_sock != 0)
824
close(batman_if->udp_send_sock);
826
batman_if->udp_recv_sock = 0;
827
batman_if->udp_send_sock = 0;
829
if ((batman_if->netaddr > 0) && (batman_if->netmask > 0)) {
831
add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_HOSTS, BATMAN_RT_PRIO_DEFAULT + batman_if->if_num, 0, RULE_TYPE_DST, RULE_DEL);
832
add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_UNREACH, BATMAN_RT_PRIO_UNREACH + batman_if->if_num, 0, RULE_TYPE_DST, RULE_DEL);
836
batman_if->if_active = 0;
839
if (batman_if->if_rp_filter_old > -1)
840
set_rp_filter(batman_if->if_rp_filter_old, batman_if->dev);
842
if (batman_if->if_send_redirects_old > -1)
843
set_send_redirects(batman_if->if_send_redirects_old, batman_if->dev);
845
batman_if->if_rp_filter_old = -1;
846
batman_if->if_send_redirects_old = -1;
848
interface_listen_sockets();
849
debug_output(3, "Interface deactivated: %s\n", batman_if->dev);
852
static void activate_interface(struct batman_if *batman_if)
854
struct ifreq int_req;
855
int on = 1, sock_opts;
857
if ( ( batman_if->udp_recv_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {
859
debug_output(3, "Error - can't create receive socket: %s\n", strerror(errno) );
864
memset( &int_req, 0, sizeof (struct ifreq) );
865
strncpy( int_req.ifr_name, batman_if->dev, IFNAMSIZ - 1 );
867
if ( ioctl( batman_if->udp_recv_sock, SIOCGIFADDR, &int_req ) < 0 ) {
869
debug_output(3, "Error - can't get IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
874
batman_if->addr.sin_family = AF_INET;
875
batman_if->addr.sin_port = htons(PORT);
876
batman_if->addr.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr;
878
if (batman_if->addr.sin_addr.s_addr == 0) {
880
debug_output(3, "Error - invalid ip address detected (0.0.0.0): %s\n", batman_if->dev);
885
if ( ioctl( batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req ) < 0 ) {
887
debug_output(3, "Error - can't get broadcast IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
892
batman_if->broad.sin_family = AF_INET;
893
batman_if->broad.sin_port = htons(PORT);
894
batman_if->broad.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_broadaddr)->sin_addr.s_addr;
896
if ( batman_if->broad.sin_addr.s_addr == 0 ) {
898
debug_output(3, "Error - invalid broadcast address detected (0.0.0.0): %s\n", batman_if->dev );
905
/* The SIOCGIFINDEX ioctl is Linux specific, but I am not yet sure if the
906
* equivalent exists on *BSD. There is a function called if_nametoindex()
907
* on both Linux and BSD.
908
* Maybe it does the same as this code and we can simply call it instead?
911
if ( ioctl( batman_if->udp_recv_sock, SIOCGIFINDEX, &int_req ) < 0 ) {
913
debug_output(3, "Error - can't get index of interface %s: %s\n", batman_if->dev, strerror(errno) );
918
batman_if->if_index = int_req.ifr_ifindex;
920
batman_if->if_index = 0;
922
batman_if->wifi_if = is_wifi_interface(batman_if->dev, batman_if->udp_recv_sock);
924
if (ioctl(batman_if->udp_recv_sock, SIOCGIFNETMASK, &int_req) < 0) {
926
debug_output(3, "Error - can't get netmask address of interface %s: %s\n", batman_if->dev, strerror(errno));
931
batman_if->netaddr = (((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr & batman_if->addr.sin_addr.s_addr);
932
batman_if->netmask = bit_count(((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr);
934
add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_HOSTS, BATMAN_RT_PRIO_DEFAULT + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);
935
add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_UNREACH, BATMAN_RT_PRIO_UNREACH + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);
937
if ( ( batman_if->udp_send_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {
939
debug_output(3, "Error - can't create send socket: %s\n", strerror(errno) );
944
if ( setsockopt( batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on) ) < 0 ) {
946
debug_output(3, "Error - can't enable broadcasts: %s\n", strerror(errno) );
951
if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) {
953
debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
958
if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {
960
debug_output(3, "Error - can't bind send socket: %s\n", strerror(errno) );
965
/* make udp socket non blocking */
966
sock_opts = fcntl(batman_if->udp_send_sock, F_GETFL, 0);
967
fcntl(batman_if->udp_send_sock, F_SETFL, sock_opts | O_NONBLOCK);
969
if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) {
971
debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
976
if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof(struct sockaddr_in) ) < 0 ) {
978
debug_output(3, "Error - can't bind receive socket: %s\n", strerror(errno));
983
batman_if->out.orig = batman_if->addr.sin_addr.s_addr;
984
batman_if->out.prev_sender = batman_if->addr.sin_addr.s_addr;
986
batman_if->if_active = 1;
989
interface_listen_sockets();
990
debug_output(3, "Interface activated: %s\n", batman_if->dev);
992
batman_if->if_rp_filter_old = get_rp_filter(batman_if->dev);
993
set_rp_filter(0, batman_if->dev);
995
batman_if->if_send_redirects_old = get_send_redirects(batman_if->dev);
996
set_send_redirects(0, batman_if->dev);
1001
deactivate_interface(batman_if);
1004
void init_interface(struct batman_if *batman_if)
1006
if (strlen( batman_if->dev ) > IFNAMSIZ - 1) {
1007
printf("Error - interface name too long: %s\n", batman_if->dev);
1012
if (is_interface_up(batman_if->dev))
1013
activate_interface(batman_if);
1016
void check_inactive_interfaces(void)
1018
struct list_head *list_pos;
1019
struct batman_if *batman_if;
1021
/* all available interfaces are active */
1022
if (found_ifs == active_ifs)
1025
list_for_each(list_pos, &if_list) {
1026
batman_if = list_entry(list_pos, struct batman_if, list);
1028
if ((!batman_if->if_active) && (is_interface_up(batman_if->dev)))
1029
activate_interface(batman_if);
1033
void check_active_interfaces(void)
1035
struct list_head *list_pos;
1036
struct batman_if *batman_if;
1038
/* all available interfaces are deactive */
1039
if (active_ifs == 0)
1042
list_for_each(list_pos, &if_list) {
1043
batman_if = list_entry(list_pos, struct batman_if, list);
1045
if ((batman_if->if_active) && (!is_interface_up(batman_if->dev)))
1046
deactivate_interface(batman_if);
1053
void init_interface_gw (void) {
1055
int32_t sock_opts, err, skfd;
1057
struct sockaddr_in sin;
1058
struct batgat_ioc_args ioc;
1059
struct batman_if *batman_if = (struct batman_if *)if_list.next;
1062
if ((batman_if->udp_tunnel_sock = use_gateway_module()) < 0) {
1064
batman_if->addr.sin_port = htons(GW_PORT);
1066
batman_if->udp_tunnel_sock = socket(PF_INET, SOCK_DGRAM, 0);
1068
if (batman_if->udp_tunnel_sock < 0) {
1070
debug_output( 0, "Error - can't create tunnel socket: %s\n", strerror(errno) );
1076
if ( bind( batman_if->udp_tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {
1078
debug_output( 0, "Error - can't bind tunnel socket: %s\n", strerror(errno) );
1084
/* make udp socket non blocking */
1085
sock_opts = fcntl( batman_if->udp_tunnel_sock, F_GETFL, 0 );
1086
fcntl( batman_if->udp_tunnel_sock, F_SETFL, sock_opts | O_NONBLOCK );
1088
batman_if->addr.sin_port = htons(PORT);
1090
pthread_create( &batman_if->listen_thread_id, NULL, &gw_listen, batman_if );
1094
ioc.universal = strlen(batman_if->dev);
1096
strncpy(ioc.dev_name, batman_if->dev, IFNAMSIZ - 1);
1098
if (ioctl(batman_if->udp_tunnel_sock, IOCSETDEV, &ioc) < 0) {
1099
debug_output(0, "Error - can't add device %s: %s\n", batman_if->dev,strerror(errno));
1100
batman_if->dev = NULL;
1105
/* set ip address of gate device */
1106
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1107
debug_output( 0, "Error - can't create socket to gate\n" );
1109
exit( EXIT_FAILURE );
1112
memset(&ifr, 0, sizeof(ifr));
1113
memset(&sin, 0, sizeof(struct sockaddr));
1115
strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
1117
sin.sin_family = AF_INET;
1118
sin.sin_addr.s_addr = ioc.universal;
1119
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
1121
if ((err = ioctl(skfd, SIOCSIFADDR, &ifr)) < 0) {
1122
debug_output(0, "Error - can't set IFADDR %s: %s\n", ioc.dev_name, strerror(err));
1125
exit( EXIT_FAILURE );
1128
memset(&ifr, 0, sizeof(ifr));
1129
strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
1130
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
1131
debug_output(0, "Error - can't get IFFLAGS for %s: %s\n", ioc.dev_name, strerror(errno));
1134
exit( EXIT_FAILURE );
1137
strncpy(ifr.ifr_name, ioc.dev_name, IFNAMSIZ - 1);
1138
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
1140
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
1141
debug_output(0, "Error - can't set IFFLAGS for %s: %s\n", ioc.dev_name, strerror(errno));
1144
exit( EXIT_FAILURE );
1150
add_del_route(ioc.universal, 16, 0, 0, ioc.ifindex, ioc.dev_name, 254, ROUTE_TYPE_UNICAST, ROUTE_ADD);