File: kernel.c

package info (click to toggle)
batmand 0.3.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 616 kB
  • ctags: 857
  • sloc: ansic: 7,677; makefile: 218; sh: 47
file content (200 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2006-2009 B.A.T.M.A.N. contributors:
 *
 * Thomas Lopatic
 *
 * 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 <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <inttypes.h>
#include <net/if.h>

#include "../os.h"
#include "../batman.h"


#define IOCGETNWDEV 1

#ifndef SIOCGIWNAME
#define SIOCGIWNAME 0x8B01
#endif


static int get_integer_file(const char* filename)
{
	FILE *f;
	int32_t integer = 0;
	int n;

	if((f = fopen(filename, "r")) == NULL)
		return 0;

	n = fscanf(f, "%"SCNd32, &integer);
	fclose(f);

	if (n == 0 || n == EOF)
		integer = 0;

	return integer;
}

static void set_integer_file(const char* filename, int32_t integer)
{
	FILE *f;

	if ((f = fopen(filename, "w")) == NULL)
		return;

	fprintf(f, "%"PRId32, integer);
	fclose(f);
}

void set_rp_filter(int32_t state, char* dev)
{
	char filename[100], *colon_ptr;

	/* if given interface is an alias use parent interface */
	if ((colon_ptr = strchr(dev, ':')) != NULL)
		*colon_ptr = '\0';

	sprintf(filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
	set_integer_file(filename, state);

	if (colon_ptr != NULL)
		*colon_ptr = ':';
}

int32_t get_rp_filter(char *dev)
{
	int32_t state = 0;
	char filename[100], *colon_ptr;

	/* if given interface is an alias use parent interface */
	if ((colon_ptr = strchr(dev, ':')) != NULL)
		*colon_ptr = '\0';

	sprintf(filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
	state = get_integer_file(filename);

	if (colon_ptr != NULL)
		*colon_ptr = ':';

	return state;
}

void set_send_redirects(int32_t state, char* dev)
{
	char filename[100], *colon_ptr;

	/* if given interface is an alias use parent interface */
	if ((colon_ptr = strchr(dev, ':')) != NULL)
		*colon_ptr = '\0';

	sprintf(filename, "/proc/sys/net/ipv4/conf/%s/send_redirects", dev);
	set_integer_file(filename, state);

	if (colon_ptr != NULL)
		*colon_ptr = ':';
}

int32_t get_send_redirects(char *dev)
{
	int32_t state = 0;
	char filename[100], *colon_ptr;

	/* if given interface is an alias use parent interface */
	if ((colon_ptr = strchr(dev, ':')) != NULL)
		*colon_ptr = '\0';

	sprintf(filename, "/proc/sys/net/ipv4/conf/%s/send_redirects", dev);
	state = get_integer_file(filename);

	if (colon_ptr != NULL)
		*colon_ptr = ':';

	return state;
}

void set_forwarding(int32_t state)
{
	set_integer_file("/proc/sys/net/ipv4/ip_forward", state);
}

int32_t get_forwarding(void)
{
	return get_integer_file("/proc/sys/net/ipv4/ip_forward");
}

int8_t bind_to_iface(int32_t sock, char *dev)
{
	char *colon_ptr;

	/* if given interface is an alias bind to parent interface */
	if ((colon_ptr = strchr(dev, ':')) != NULL)
		*colon_ptr = '\0';

	if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev) + 1) < 0) {

		if (colon_ptr != NULL)
			*colon_ptr = ':';

		return -1;

	}

	if (colon_ptr != NULL)
		*colon_ptr = ':';

	return 1;
}

int32_t use_gateway_module(void)
{
	int32_t fd;

	if ((fd = open("/dev/batgat", O_WRONLY)) < 0) {
		debug_output(0, "Warning - batgat kernel modul interface (/dev/batgat) not usable: %s\nThis may decrease the performance of batman!\n", strerror(errno));

		return -1;
	}

	return fd;
}

int is_wifi_interface(char *dev, int fd)
{
	struct ifreq int_req;
	char *colon_ptr;

	memset(&int_req, 0, sizeof (struct ifreq));
	strncpy(int_req.ifr_name, dev, IFNAMSIZ - 1);

	if ((colon_ptr = strchr(int_req.ifr_name, ':')) != NULL)
		*colon_ptr = '\0';

	if (ioctl(fd, SIOCGIWNAME, &int_req) >= 0)
		return 1;

	return 0;
}