-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsrc_list.c
More file actions
executable file
·237 lines (188 loc) · 5.37 KB
/
src_list.c
File metadata and controls
executable file
·237 lines (188 loc) · 5.37 KB
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
/***************************************************************************
Created by Miguel Catalan Cid - miguel.catcid@gmail.com - Version: Mon Jan 1 2010
***************************************************************************/
//Traffic sources lists, Route tables identificators
#include "src_list.h"
extern u_int32_t g_null_ip;
src_list_entry *src_list;
extern u_int32_t g_mesh_ip;
extern u_int32_t g_null_ip;
//rwlock_t src_lock = RW_LOCK_UNLOCKED;
DEFINE_RWLOCK(src_lock);
inline void src_read_lock(void) {
read_lock_bh(&src_lock);
}
inline void src_read_unlock(void) {
read_unlock_bh(&src_lock);
}
inline void src_write_lock(void) {
write_lock_bh(&src_lock);
}
inline void src_write_unlock(void) {
write_unlock_bh(&src_lock);
}
void init_src_list() {
src_list=NULL;
}
void flush_src_list(void) {
src_list_entry *tmp_entry = src_list;
src_list_entry *prev_entry = NULL;
while (tmp_entry != NULL) {
//Deleting associated source rules
if (tmp_entry->ip != g_null_ip) {
int error = rpdb_rule(RTM_DELRULE, tmp_entry->rt_table,
tmp_entry->ip, g_null_ip);
if (error < 0)
printk("Error sending with rtnetlink - Add ROUTE - err no: %dn",
error);
}
prev_entry = tmp_entry;
tmp_entry = tmp_entry->next;
kfree(prev_entry);
}
}
void delete_src_list_entry(u_int32_t ip) {
src_list_entry *tmp_entry = src_list;
src_list_entry *prev_entry = NULL;
src_write_lock(); //avoid conflicts with read_proc
while (tmp_entry != NULL) {
if (tmp_entry->ip == ip) {
if (prev_entry != NULL)
prev_entry->next = tmp_entry->next;
else
src_list = tmp_entry->next;
src_write_unlock();
if (tmp_entry->ip != g_null_ip) {
int error = rpdb_rule(RTM_DELRULE, tmp_entry->rt_table,
tmp_entry->ip, g_null_ip);
if (error < 0)
printk("Error sending with rtnetlink - Add ROUTE - err no: %dn",
error);
}
kfree(tmp_entry);
return;
}
prev_entry = tmp_entry;
tmp_entry = tmp_entry->next;
}
src_write_unlock();
}
src_list_entry * insert_src_list_entry(u_int32_t ip) {
int error =1;
int n_table;
src_list_entry *new_entry = NULL;
new_entry = kmalloc(sizeof(src_list_entry), GFP_ATOMIC);
if (new_entry == NULL) {
printk("SRC_LIST: Can't allocate new entry\n");
return NULL;
}
src_write_lock(); //avoid conflicts with read_proc
new_entry->ip = ip;
new_entry->num_routes = 0;
new_entry->next = NULL;
if (ip == g_mesh_ip)
new_entry->num_routes = 1;
if (new_entry->ip != g_null_ip) {
n_table = find_first_rttable();
if (n_table > MAX_ROUTE_TABLES) {
printk("There are no more empty route tables! New Traffic can't be routed\n");
src_write_unlock();
kfree(new_entry);
return NULL;
}
new_entry->rt_table = n_table;
src_write_unlock();
error = rpdb_rule(RTM_NEWRULE, new_entry->rt_table, new_entry->ip,
g_null_ip);
if (error < 0) {
printk("Error sending with rtnetlink - Add ROUTE - err no: %dn",
error);
kfree(new_entry);
return NULL;
}
src_write_lock();
} else {
new_entry->rt_table = DEFAULT_ROUTE_TABLE;
}
if (src_list == NULL) {
src_list = new_entry;
} else {
new_entry->next = src_list;
src_list = new_entry;
}
src_write_unlock();
#ifdef DEBUG
printk("inserting %s in src_table\n", inet_ntoa(ip));
#endif
return new_entry;
}
src_list_entry * find_src_list_entry(u_int32_t ip) {
src_list_entry *tmp_entry = src_list;
while (tmp_entry != NULL) {
if (tmp_entry->ip == ip) {
return tmp_entry;
}
tmp_entry = tmp_entry->next;
}
return NULL;
}
int find_empty_rttable(int index) {
src_list_entry *tmp_entry = src_list;
while (tmp_entry != NULL) {
if (tmp_entry->rt_table == index)
return 0;
tmp_entry = tmp_entry->next;
}
return 1;
}
int find_first_rttable() {
int rttable;
for (rttable = 1; rttable <= MAX_ROUTE_TABLES; rttable++) {
if (find_empty_rttable(rttable))
return rttable;
}
return rttable;
}
int read_src_list_proc(char *buffer, char **buffer_location, off_t offset,
int buffer_length, int *eof, void *data) {
src_list_entry *tmp_entry;
static char *my_buffer;
char temp_buffer[400];
char src[16];
int len;
tmp_entry = src_list;
my_buffer = buffer;
src_read_lock();
sprintf(
my_buffer,
"\n Sources Table \n"
"-------------------------------------------------------------------------------------------\n");
sprintf(temp_buffer,
" IP | ROUTE TABLE | TOTAL ROUTES | QoS-D ROUTES | BG ROUTES |\n");
strcat(my_buffer, temp_buffer);
sprintf(
temp_buffer,
"-------------------------------------------------------------------------------------------\n");
strcat(my_buffer, temp_buffer);
while (tmp_entry != NULL) {
strcpy(src, inet_ntoa(tmp_entry->ip));
sprintf(
temp_buffer,
" %-16s %3d %3d\n",
src, tmp_entry->rt_table, tmp_entry->num_routes);
strcat(my_buffer, temp_buffer);
tmp_entry = tmp_entry->next;
}
strcat(
my_buffer,
"\n-------------------------------------------------------------------------------------------\n\n");
len = strlen(my_buffer);
*buffer_location = my_buffer + offset;
len -= offset;
if (len > buffer_length)
len = buffer_length;
else if (len < 0)
len = 0;
src_read_unlock();
return len;
}