Commit | Line | Data |
---|---|---|
70288420 TJ |
1 | /* |
2 | * This is a module which is used for counting packets. | |
3 | */ | |
4 | #include <linux/module.h> | |
5 | #include <linux/skbuff.h> | |
6 | #include <linux/ip.h> | |
7 | #include <linux/spinlock.h> | |
8 | #include <net/icmp.h> | |
9 | #include <net/udp.h> | |
10 | #include <net/tcp.h> | |
11 | #include <linux/netfilter_ipv4/ip_tables.h> | |
12 | ||
13 | struct in_device; | |
14 | #include <net/route.h> | |
15 | #include <linux/netfilter_ipv4/ipt_ACCOUNT.h> | |
16 | ||
17 | //#if 0 | |
18 | #define DEBUGP printk | |
19 | //#else | |
20 | //#define DEBUGP(format, args...) | |
21 | //#endif | |
22 | ||
23 | struct ipt_account_table *ipt_account_tables = NULL; | |
24 | struct ipt_account_handle *ipt_account_handles = NULL; | |
25 | ||
26 | static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED; | |
27 | ||
70288420 | 28 | /* Recursive free of all data structures */ |
850cece6 | 29 | void ipt_account_data_free(void *data, unsigned char depth) |
70288420 TJ |
30 | { |
31 | // Empty data set | |
32 | if (!data) | |
33 | return; | |
34 | ||
850cece6 TJ |
35 | // Free for 8 bit network |
36 | if (depth == 0) | |
70288420 | 37 | { |
850cece6 | 38 | free_page((unsigned long)data); |
70288420 TJ |
39 | data = NULL; |
40 | return; | |
41 | } | |
42 | ||
43 | // Free for 16 bit network | |
850cece6 | 44 | if (depth == 1) |
70288420 TJ |
45 | { |
46 | struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data; | |
47 | unsigned char b; | |
48 | for (b=0; b < 255; b++) | |
49 | { | |
50 | if (mask_16->mask_24[b] != 0) | |
51 | { | |
850cece6 | 52 | free_page((unsigned long)mask_16->mask_24[b]); |
70288420 TJ |
53 | mask_16->mask_24[b] = NULL; |
54 | } | |
55 | } | |
850cece6 | 56 | free_page((unsigned long)data); |
70288420 TJ |
57 | data = NULL; |
58 | return; | |
59 | } | |
60 | ||
61 | // Free for 24 bit network | |
850cece6 | 62 | if (depth == 3) |
70288420 TJ |
63 | { |
64 | unsigned char a, b; | |
65 | for (a=0; a < 255; a++) | |
66 | { | |
67 | if (((struct ipt_account_mask_8 *)data)->mask_16[a]) | |
68 | { | |
69 | struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16*)((struct ipt_account_mask_8 *)data)->mask_16[a]; | |
70 | for (b=0; b < 255; b++) | |
71 | { | |
72 | if (mask_16->mask_24[b]) { | |
850cece6 | 73 | free_page((unsigned long)mask_16->mask_24[b]); |
70288420 TJ |
74 | mask_16->mask_24[b] = NULL; |
75 | } | |
76 | } | |
850cece6 | 77 | free_page((unsigned long)mask_16); |
70288420 TJ |
78 | mask_16 = NULL; |
79 | } | |
80 | } | |
850cece6 | 81 | free_page((unsigned long)data); |
70288420 TJ |
82 | data = NULL; |
83 | return; | |
84 | } | |
85 | ||
850cece6 | 86 | printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth); |
70288420 TJ |
87 | return; |
88 | } | |
89 | ||
90 | /* Look for existing table / insert new one. Return internal ID or -1 on error */ | |
91 | int ipt_account_table_insert(char *name, unsigned int ip, unsigned int netmask) | |
92 | { | |
93 | unsigned int i; | |
94 | ||
850cece6 | 95 | DEBUGP("ACCOUNT: ipt_account_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", name, NIPQUAD(ip), NIPQUAD(netmask)); |
70288420 TJ |
96 | |
97 | // Look for existing table | |
98 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) | |
99 | { | |
bea3921b | 100 | if (strncmp(ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN) == 0) |
70288420 | 101 | { |
850cece6 TJ |
102 | DEBUGP("ACCOUNT: Found existing slot: %d - %u.%u.%u.%u/%u.%u.%u.%u\n", i, |
103 | NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask)); | |
70288420 TJ |
104 | |
105 | if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask) | |
106 | { | |
850cece6 TJ |
107 | printk("ACCOUNT: Table %s found, but IP/netmask mismatch. IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n", |
108 | name, NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask)); | |
70288420 TJ |
109 | return -1; |
110 | } | |
111 | ||
112 | ipt_account_tables[i].refcount++; | |
113 | DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount); | |
114 | return i; | |
115 | } | |
116 | } | |
117 | ||
118 | // Insert new table | |
119 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) | |
120 | { | |
121 | // Found free slot | |
122 | if (ipt_account_tables[i].name[0] == 0) | |
123 | { | |
124 | DEBUGP("ACCOUNT: Found free slot: %d\n", i); | |
125 | ||
126 | strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1); | |
127 | ||
128 | ipt_account_tables[i].ip = ip; | |
129 | ipt_account_tables[i].netmask = netmask; | |
130 | ||
850cece6 TJ |
131 | // Calculate netsize |
132 | unsigned int j, calc_mask, netsize=0; | |
70288420 TJ |
133 | calc_mask = htonl(netmask); |
134 | for (j = 31; j > 0; j--) | |
135 | { | |
136 | if (calc_mask&(1<<j)) | |
850cece6 | 137 | netsize++; |
70288420 TJ |
138 | else |
139 | break; | |
140 | } | |
850cece6 TJ |
141 | |
142 | // Calculate depth from netsize | |
143 | if (netsize >= 24) | |
144 | ipt_account_tables[i].depth = 0; | |
145 | else if (netsize >= 16) | |
146 | ipt_account_tables[i].depth = 1; | |
147 | else if(netsize >= 8) | |
148 | ipt_account_tables[i].depth = 2; | |
149 | ||
150 | printk("ACCOUNT: calculated netsize: %u -> ipt_account_table depth %u\n", netsize, ipt_account_tables[i].depth); | |
70288420 TJ |
151 | |
152 | ipt_account_tables[i].refcount++; | |
153 | if (!(ipt_account_tables[i].data = (void *)get_zeroed_page(GFP_KERNEL))) | |
154 | { | |
850cece6 | 155 | printk("ACCOUNT: out of memory for data of table: %s\n", name); |
70288420 TJ |
156 | memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table)); |
157 | return -1; | |
158 | } | |
850cece6 | 159 | |
70288420 TJ |
160 | return i; |
161 | } | |
162 | } | |
163 | ||
164 | // No free slot found | |
165 | printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES); | |
166 | return -1; | |
167 | } | |
168 | ||
169 | static int ipt_account_checkentry(const char *tablename, | |
170 | const struct ipt_entry *e, | |
171 | void *targinfo, | |
172 | unsigned int targinfosize, | |
173 | unsigned int hook_mask) | |
174 | { | |
175 | struct ipt_account_info *info = targinfo; | |
176 | ||
177 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) { | |
178 | DEBUGP("ACCOUNT: targinfosize %u != %u\n", | |
179 | targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info))); | |
180 | return 0; | |
181 | } | |
182 | ||
850cece6 | 183 | spin_lock_bh(&ipt_account_lock); |
70288420 TJ |
184 | int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask); |
185 | if (table_nr == -1) | |
186 | { | |
187 | printk("ACCOUNT: Table insert problem. Aborting\n"); | |
850cece6 | 188 | spin_unlock_bh(&ipt_account_lock); |
70288420 TJ |
189 | return 0; |
190 | } | |
70288420 TJ |
191 | // Table nr caching so we don't have to do an extra string compare for every packet |
192 | info->table_nr = table_nr; | |
193 | ||
850cece6 TJ |
194 | spin_unlock_bh(&ipt_account_lock); |
195 | ||
70288420 TJ |
196 | return 1; |
197 | } | |
198 | ||
850cece6 TJ |
199 | void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize) |
200 | { | |
201 | unsigned int i; | |
202 | struct ipt_account_info *info = targinfo; | |
203 | ||
204 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) { | |
205 | DEBUGP("ACCOUNT: targinfosize %u != %u\n", | |
206 | targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info))); | |
207 | } | |
208 | ||
209 | spin_lock_bh(&ipt_account_lock); | |
210 | ||
211 | DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr); | |
212 | ||
213 | info->table_nr = -1; // Set back to original state | |
214 | ||
215 | // Look for table | |
216 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) | |
217 | { | |
bea3921b | 218 | if (strncmp(ipt_account_tables[i].name, info->table_name, ACCOUNT_TABLE_NAME_LEN) == 0) |
850cece6 TJ |
219 | { |
220 | DEBUGP("ACCOUNT: Found table at slot: %d\n", i); | |
221 | ||
222 | ipt_account_tables[i].refcount--; | |
223 | DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount); | |
224 | ||
225 | // Table not needed anymore? | |
226 | if (ipt_account_tables[i].refcount == 0) | |
227 | { | |
228 | DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i); | |
229 | ipt_account_data_free(ipt_account_tables[i].data, ipt_account_tables[i].depth); | |
230 | memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table)); | |
231 | } | |
232 | ||
233 | spin_unlock_bh(&ipt_account_lock); | |
234 | return; | |
235 | } | |
236 | } | |
237 | ||
238 | // Table not found | |
239 | printk("ACCOUNT: Table %s not found for destroy\n", info->table_name); | |
240 | spin_unlock_bh(&ipt_account_lock); | |
241 | } | |
242 | ||
4068e14d TJ |
243 | void ipt_account_depth0_insert(struct ipt_account_mask_24 *mask_24, unsigned int net_ip, unsigned int netmask, |
244 | unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) | |
245 | { | |
246 | unsigned char is_src = 0, is_dst = 0; | |
247 | ||
248 | DEBUGP("ACCOUNT: ipt_account_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", | |
249 | NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size); | |
250 | ||
251 | // Check if src/dst is inside our network. | |
252 | // Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 | |
253 | if (!netmask) | |
254 | src_ip = 0; | |
255 | if ((net_ip&netmask) == (src_ip&netmask)) | |
256 | is_src = 1; | |
257 | if ((net_ip&netmask) == (dst_ip&netmask) && netmask) | |
258 | is_dst = 1; | |
259 | ||
260 | if (!is_src && !is_dst) | |
261 | { | |
262 | DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u\n", | |
263 | NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask)); | |
264 | return; | |
265 | } | |
266 | ||
267 | // Check if this entry is new | |
fa9124c2 | 268 | char is_src_new_ip = 0, is_dst_new_ip = 0; |
4068e14d TJ |
269 | |
270 | // Increase size counters | |
271 | if (is_src) | |
272 | { | |
273 | // Calculate network slot | |
274 | unsigned char slot = (unsigned char)((src_ip&0xFF000000) >> 24); | |
275 | DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", slot); | |
276 | if (!mask_24->ip[slot].src_packets && !mask_24->ip[slot].dst_packets) | |
fa9124c2 | 277 | is_src_new_ip = 1; |
4068e14d TJ |
278 | |
279 | mask_24->ip[slot].src_packets++; | |
280 | mask_24->ip[slot].src_bytes+=size; | |
281 | } | |
282 | if (is_dst) | |
283 | { | |
284 | unsigned char slot = (unsigned char)((dst_ip&0xFF000000) >> 24); | |
285 | DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", slot); | |
286 | if (!mask_24->ip[slot].src_packets && !mask_24->ip[slot].dst_packets) | |
fa9124c2 | 287 | is_dst_new_ip = 1; |
4068e14d TJ |
288 | |
289 | mask_24->ip[slot].dst_packets++; | |
290 | mask_24->ip[slot].dst_bytes+=size; | |
291 | } | |
292 | ||
fa9124c2 TJ |
293 | // Increase itemcounter |
294 | if (src_ip == dst_ip) | |
295 | { | |
296 | if (is_src_new_ip || is_dst_new_ip) | |
297 | (*itemcount)++; | |
298 | } else { | |
299 | if (is_src_new_ip) | |
300 | (*itemcount)++; | |
301 | if (is_dst_new_ip) | |
302 | (*itemcount)++; | |
303 | } | |
4068e14d TJ |
304 | } |
305 | ||
306 | void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask, | |
307 | unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) | |
308 | { | |
309 | // Do we need to process src IP? | |
310 | if ((net_ip&netmask) == (src_ip&netmask)) | |
311 | { | |
312 | unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16); | |
313 | DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot); | |
314 | ||
315 | // Do we need to create a new mask_24 bucket? | |
316 | if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL))) | |
317 | { | |
318 | printk("ACCOUNT: Can't process packet because out of memory!\n"); | |
319 | return; | |
320 | } | |
321 | ||
322 | ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask, | |
323 | src_ip, dst_ip, size, itemcount); | |
324 | } | |
325 | ||
326 | // Do we need to process dst IP? | |
327 | if ((net_ip&netmask) == (dst_ip&netmask)) | |
328 | { | |
329 | unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16); | |
330 | DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot); | |
331 | ||
332 | // Do we need to create a new mask_24 bucket? | |
333 | if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL))) | |
334 | { | |
335 | printk("ACCOUT: Can't process packet because out of memory!\n"); | |
336 | return; | |
337 | } | |
338 | ||
339 | ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask, | |
340 | src_ip, dst_ip, size, itemcount); | |
341 | } | |
342 | } | |
343 | ||
344 | void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask, | |
345 | unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) | |
346 | { | |
347 | // Do we need to process src IP? | |
348 | if ((net_ip&netmask) == (src_ip&netmask)) | |
349 | { | |
350 | unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8); | |
351 | DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot); | |
352 | ||
353 | // Do we need to create a new mask_24 bucket? | |
354 | if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL))) | |
355 | { | |
356 | printk("ACCOUNT: Can't process packet because out of memory!\n"); | |
357 | return; | |
358 | } | |
359 | ||
360 | ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask, | |
361 | src_ip, dst_ip, size, itemcount); | |
362 | } | |
363 | ||
364 | // Do we need to process dst IP? | |
365 | if ((net_ip&netmask) == (dst_ip&netmask)) | |
366 | { | |
367 | unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8); | |
368 | DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot); | |
369 | ||
370 | // Do we need to create a new mask_24 bucket? | |
371 | if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL))) | |
372 | { | |
373 | printk("ACCOUNT: Can't process packet because out of memory!\n"); | |
374 | return; | |
375 | } | |
376 | ||
377 | ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask, | |
378 | src_ip, dst_ip, size, itemcount); | |
379 | } | |
380 | } | |
381 | ||
382 | static unsigned int ipt_account_target(struct sk_buff **pskb, | |
383 | unsigned int hooknum, | |
384 | const struct net_device *in, | |
385 | const struct net_device *out, | |
386 | const void *targinfo, | |
387 | void *userinfo) | |
388 | { | |
389 | const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo; | |
390 | unsigned int src_ip = (*pskb)->nh.iph->saddr; | |
391 | unsigned int dst_ip = (*pskb)->nh.iph->daddr; | |
392 | unsigned int size = ntohs((*pskb)->nh.iph->tot_len); | |
393 | ||
394 | spin_lock_bh(&ipt_account_lock); | |
395 | ||
396 | if (ipt_account_tables[info->table_nr].name[0] == 0) | |
397 | { | |
398 | printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n", | |
399 | info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip)); | |
400 | spin_unlock_bh(&ipt_account_lock); | |
401 | return IPT_CONTINUE; | |
402 | } | |
403 | ||
404 | // 8 bit network or "any" network | |
405 | if (ipt_account_tables[info->table_nr].depth == 0) | |
406 | { | |
407 | // Count packet and check if the IP is new | |
408 | ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data, | |
409 | ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask, | |
410 | src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount); | |
411 | spin_unlock_bh(&ipt_account_lock); | |
412 | return IPT_CONTINUE; | |
413 | } | |
414 | ||
415 | // 16 bit network | |
416 | if (ipt_account_tables[info->table_nr].depth == 1) | |
417 | { | |
418 | ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data, | |
419 | ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask, | |
420 | src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount); | |
421 | spin_unlock_bh(&ipt_account_lock); | |
422 | return IPT_CONTINUE; | |
423 | } | |
424 | ||
425 | // 24 bit network | |
426 | if (ipt_account_tables[info->table_nr].depth == 2) | |
427 | { | |
428 | ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data, | |
429 | ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask, | |
430 | src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount); | |
431 | spin_unlock_bh(&ipt_account_lock); | |
432 | return IPT_CONTINUE; | |
433 | } | |
434 | ||
435 | printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n", | |
436 | info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip)); | |
437 | ||
438 | spin_unlock_bh(&ipt_account_lock); | |
439 | return IPT_CONTINUE; | |
440 | } | |
441 | ||
bea3921b TJ |
442 | /* |
443 | Functions dealing with "handles": | |
444 | Handles are snapshots of a accounting state. | |
445 | ||
446 | read snapshots are only for debugging the code | |
447 | and are very expensive concerning speed/memory | |
448 | compared to read_and_flush. | |
449 | ||
450 | The functions aren't protected by spinlocks themselves | |
451 | as this is done in the ioctl part of the code. | |
452 | */ | |
453 | ||
454 | /* | |
455 | Find a free handle slot. Normally only one should be used, | |
456 | but there could be two or more applications accessing the data | |
457 | at the same time. | |
458 | */ | |
459 | int ipt_account_handle_find_slot(void) | |
460 | { | |
461 | unsigned int i; | |
462 | // Insert new table | |
463 | for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) | |
464 | { | |
465 | // Found free slot | |
466 | if (ipt_account_handles[i].data == NULL) | |
467 | { | |
468 | // Don't "mark" data as used as we are protected by a spinlock by the calling function. | |
469 | // handle_find_slot() is only a function to prevent code duplication. | |
470 | return i; | |
471 | } | |
472 | } | |
473 | ||
474 | // No free slot found | |
475 | printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES); | |
476 | return -1; | |
477 | } | |
478 | ||
479 | int ipt_account_handle_free(unsigned int handle) | |
480 | { | |
481 | if (handle >= ACCOUNT_MAX_HANDLES) | |
482 | { | |
483 | printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle); | |
484 | return -EINVAL; | |
485 | } | |
486 | ||
487 | ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth); | |
488 | memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle)); | |
489 | return 0; | |
490 | } | |
491 | ||
492 | /* Prepare data for read without flush. Use only for debugging! | |
493 | Real applications should use read&flush as it's way more efficent */ | |
494 | int ipt_account_handle_prepare_read(char *tablename, unsigned int *count) | |
495 | { | |
496 | int handle, i, table_nr=-1; | |
497 | ||
498 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) | |
499 | if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) | |
500 | { | |
501 | table_nr = i; | |
502 | break; | |
503 | } | |
504 | ||
505 | if (table_nr == -1) | |
506 | { | |
507 | printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename); | |
508 | return -1; | |
509 | } | |
510 | ||
511 | // Can't find a free handle slot? | |
512 | if ((handle = ipt_account_handle_find_slot()) == -1) | |
513 | return -1; | |
514 | ||
515 | // Fill up handle structure | |
516 | ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip; | |
517 | ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth; | |
518 | ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount; | |
519 | ||
520 | // allocate "root" table | |
521 | if (!(ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL))) | |
522 | { | |
523 | printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n"); | |
524 | memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle)); | |
525 | return -1; | |
526 | } | |
527 | ||
528 | // Recursive copy of complete data structure | |
529 | unsigned int depth = ipt_account_handles[handle].depth; | |
530 | if (depth == 0) | |
531 | { | |
532 | memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24)); | |
533 | } else if (depth == 1) { | |
534 | struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data; | |
535 | struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data; | |
536 | unsigned char b; | |
537 | ||
538 | for (b = 0; b < 255; b++) | |
539 | { | |
540 | if (src_16->mask_24[b]) | |
541 | { | |
542 | if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL))) | |
543 | { | |
544 | printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n"); | |
545 | ipt_account_data_free(ipt_account_handles[handle].data, depth); | |
546 | memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle)); | |
547 | return -1; | |
548 | } | |
549 | ||
550 | memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24)); | |
551 | } | |
552 | } | |
553 | } else if(depth == 2) { | |
554 | struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data; | |
555 | struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data; | |
556 | unsigned char a; | |
557 | ||
558 | for (a = 0; a < 255; a++) | |
559 | { | |
560 | if (src_8->mask_16[a]) | |
561 | { | |
562 | if (!(network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL))) | |
563 | { | |
564 | printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n"); | |
565 | ipt_account_data_free(ipt_account_handles[handle].data, depth); | |
566 | memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle)); | |
567 | return -1; | |
568 | } | |
569 | ||
570 | memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16)); | |
571 | ||
572 | struct ipt_account_mask_16 *src_16 = src_8->mask_16[a]; | |
573 | struct ipt_account_mask_16 *network_16 = network_8->mask_16[a]; | |
574 | unsigned char b; | |
575 | ||
576 | for (b = 0; b < 255; b++) | |
577 | { | |
578 | if (src_16->mask_24[b]) | |
579 | { | |
580 | if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL))) | |
581 | { | |
582 | printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n"); | |
583 | ipt_account_data_free(ipt_account_handles[handle].data, depth); | |
584 | memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle)); | |
585 | return -1; | |
586 | } | |
587 | ||
588 | memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24)); | |
589 | } | |
590 | } | |
591 | } | |
592 | } | |
593 | } | |
594 | ||
595 | *count = ipt_account_tables[table_nr].itemcount; | |
596 | return handle; | |
597 | } | |
598 | ||
599 | /* Prepare data for read and flush it */ | |
600 | int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count) | |
601 | { | |
602 | int handle, i, table_nr=-1; | |
603 | ||
604 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) | |
605 | if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) | |
606 | { | |
607 | table_nr = i; | |
608 | break; | |
609 | } | |
610 | ||
611 | if (table_nr == -1) | |
612 | { | |
613 | printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename); | |
614 | return -1; | |
615 | } | |
616 | ||
617 | // Can't find a free handle slot? | |
618 | if ((handle = ipt_account_handle_find_slot()) == -1) | |
619 | return -1; | |
620 | ||
621 | // Fill up handle structure | |
622 | ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip; | |
623 | ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth; | |
624 | ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount; | |
625 | ipt_account_handles[handle].data = ipt_account_tables[table_nr].data; | |
626 | *count = ipt_account_tables[table_nr].itemcount; | |
627 | ||
628 | // "Flush" table data | |
629 | ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL); | |
630 | ipt_account_tables[table_nr].itemcount = 0; | |
631 | ||
632 | return handle; | |
633 | } | |
634 | ||
635 | /* Copy the actual that into a prepared buffer. | |
636 | We only copy entries != 0 to increase performance. | |
637 | The memory gets freed again in ipt_account_handle_free(). | |
638 | */ | |
639 | int ipt_account_handle_get_data(unsigned int handle, void *buffer) | |
640 | { | |
641 | struct ipt_account_handle_ip handle_ip; | |
642 | unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip); | |
643 | unsigned char i; | |
644 | ||
645 | if (handle >= ACCOUNT_MAX_HANDLES) | |
646 | { | |
647 | printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle); | |
648 | return -1; | |
649 | } | |
650 | ||
651 | if (ipt_account_handles[handle].data == NULL) | |
652 | { | |
653 | printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle); | |
654 | return -1; | |
655 | } | |
656 | ||
657 | // Check if at least packet src/dst matches ip/netmask | |
658 | unsigned int net_ip = ipt_account_handles[handle].ip; | |
659 | unsigned int depth = ipt_account_handles[handle].depth; | |
660 | ||
661 | // 8 bit network | |
662 | if (depth == 0) | |
663 | { | |
664 | struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data; | |
665 | for (i = 0; i < 255; i++) | |
666 | { | |
667 | if (network->ip[i].src_packets || network->ip[i].dst_packets) | |
668 | { | |
669 | handle_ip.ip = net_ip | (i<<24); | |
670 | handle_ip.src_packets = network->ip[i].src_packets; | |
671 | handle_ip.src_bytes = network->ip[i].src_bytes; | |
672 | handle_ip.dst_packets = network->ip[i].dst_packets; | |
673 | handle_ip.dst_bytes = network->ip[i].dst_bytes; | |
674 | ||
675 | copy_to_user(buffer, &handle_ip, handle_ip_size); | |
676 | buffer += handle_ip_size; | |
677 | } | |
678 | } | |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
683 | // 16 bit network | |
684 | if (depth == 1) | |
685 | { | |
686 | struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data; | |
687 | unsigned char b; | |
688 | for (b = 0; b < 255; b++) | |
689 | { | |
690 | if (network_16->mask_24[b]) | |
691 | { | |
692 | struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b]; | |
693 | for (i = 0; i < 255; i++) | |
694 | { | |
695 | if (network->ip[i].src_packets || network->ip[i].dst_packets) | |
696 | { | |
697 | handle_ip.ip = net_ip | (b << 16) | (i<<24); | |
698 | handle_ip.src_packets = network->ip[i].src_packets; | |
699 | handle_ip.src_bytes = network->ip[i].src_bytes; | |
700 | handle_ip.dst_packets = network->ip[i].dst_packets; | |
701 | handle_ip.dst_bytes = network->ip[i].dst_bytes; | |
702 | ||
703 | copy_to_user(buffer, &handle_ip, handle_ip_size); | |
704 | buffer += handle_ip_size; | |
705 | } | |
706 | } | |
707 | } | |
708 | } | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
713 | // 24 bit network | |
714 | if (depth == 2) | |
715 | { | |
716 | struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data; | |
717 | unsigned char a, b; | |
718 | for (a = 0; a < 255; a++) | |
719 | { | |
720 | if (network_8->mask_16[a]) | |
721 | { | |
722 | struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a]; | |
723 | for (b = 0; b < 255; b++) | |
724 | { | |
725 | if (network_16->mask_24[b]) | |
726 | { | |
727 | struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b]; | |
728 | for (i = 0; i < 255; i++) | |
729 | { | |
730 | if (network->ip[i].src_packets || network->ip[i].dst_packets) | |
731 | { | |
732 | handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24); | |
733 | handle_ip.src_packets = network->ip[i].src_packets; | |
734 | handle_ip.src_bytes = network->ip[i].src_bytes; | |
735 | handle_ip.dst_packets = network->ip[i].dst_packets; | |
736 | handle_ip.dst_bytes = network->ip[i].dst_bytes; | |
737 | ||
738 | copy_to_user(buffer, &handle_ip, handle_ip_size); | |
739 | buffer += handle_ip_size; | |
740 | } | |
741 | } | |
742 | } | |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
750 | return -1; | |
751 | } | |
752 | ||
753 | static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len) | |
754 | { | |
755 | struct ipt_account_handle_sockopt handle; | |
756 | int ret = -EINVAL; | |
757 | ||
758 | if (!capable(CAP_NET_ADMIN)) | |
759 | return -EPERM; | |
760 | ||
761 | switch (cmd) | |
762 | { | |
763 | case IPT_SO_SET_ACCOUNT_HANDLE_FREE: | |
764 | if (len != sizeof(struct ipt_account_handle_sockopt)) | |
765 | { | |
766 | printk("ACCOUNT: ipt_account_set_ctl: wrong data size (%u != %u) for IPT_SO_SET_HANDLE_FREE\n", len, sizeof(struct ipt_account_handle_sockopt)); | |
767 | break; | |
768 | } | |
769 | ||
770 | if (copy_from_user (&handle, user, len)) | |
771 | { | |
772 | printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n"); | |
773 | break; | |
774 | } | |
775 | ||
776 | spin_lock_bh(&ipt_account_lock); | |
777 | ret = ipt_account_handle_free(handle.handle_nr); | |
778 | spin_unlock_bh(&ipt_account_lock); | |
779 | break; | |
780 | default: | |
781 | printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd); | |
782 | } | |
783 | ||
784 | return ret; | |
785 | } | |
786 | ||
787 | static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len) | |
788 | { | |
789 | struct ipt_account_handle_sockopt handle; | |
790 | int ret = -EINVAL; | |
791 | ||
792 | if (!capable(CAP_NET_ADMIN)) | |
793 | return -EPERM; | |
794 | ||
795 | switch (cmd) | |
796 | { | |
797 | case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH: | |
798 | case IPT_SO_GET_ACCOUNT_PREPARE_READ: | |
799 | if (*len < sizeof(struct ipt_account_handle_sockopt)) | |
800 | { | |
801 | printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n", | |
802 | *len, sizeof(struct ipt_account_handle_sockopt)); | |
803 | break; | |
804 | } | |
805 | ||
806 | if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) | |
807 | { | |
808 | printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n"); | |
809 | break; | |
810 | } | |
811 | ||
812 | spin_lock_bh(&ipt_account_lock); | |
813 | if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH) | |
814 | handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount); | |
815 | else | |
816 | handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount); | |
817 | spin_unlock_bh(&ipt_account_lock); | |
818 | ||
819 | if (handle.handle_nr == -1) | |
820 | { | |
821 | printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n"); | |
822 | break; | |
823 | } | |
824 | ||
825 | if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt))) | |
826 | { | |
827 | printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n"); | |
828 | break; | |
829 | } | |
830 | ret = 0; | |
831 | break; | |
832 | case IPT_SO_GET_ACCOUNT_GET_DATA: | |
833 | if (*len < sizeof(struct ipt_account_handle_sockopt)) | |
834 | { | |
835 | printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n", | |
836 | *len, sizeof(struct ipt_account_handle_sockopt)); | |
837 | break; | |
838 | } | |
839 | ||
840 | if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) | |
841 | { | |
842 | printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n"); | |
843 | break; | |
844 | } | |
845 | ||
846 | if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) | |
847 | { | |
848 | printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr); | |
849 | break; | |
850 | } | |
851 | ||
852 | if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip)) | |
853 | { | |
854 | printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n", | |
855 | *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip)); | |
856 | break; | |
857 | } | |
858 | ||
859 | spin_lock_bh(&ipt_account_lock); | |
860 | ret = ipt_account_handle_get_data(handle.handle_nr, user); | |
861 | spin_unlock_bh(&ipt_account_lock); | |
862 | if (ret) | |
863 | { | |
864 | printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr); | |
865 | break; | |
866 | } | |
867 | ||
868 | ret = 0; | |
869 | break; | |
870 | ||
871 | default: | |
872 | printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd); | |
873 | } | |
874 | ||
875 | return ret; | |
876 | } | |
877 | ||
70288420 | 878 | static struct ipt_target ipt_account_reg |
850cece6 | 879 | = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry, |
70288420 TJ |
880 | THIS_MODULE }; |
881 | ||
bea3921b TJ |
882 | static struct nf_sockopt_ops ipt_account_sockopts |
883 | = { { NULL, NULL }, PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl, | |
884 | IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL }; | |
885 | ||
70288420 TJ |
886 | static int __init init(void) |
887 | { | |
bea3921b TJ |
888 | if (PAGE_SIZE < 4096) |
889 | { | |
890 | printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE); | |
891 | return -EINVAL; | |
892 | } | |
893 | ||
850cece6 | 894 | if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL))) |
70288420 | 895 | { |
bea3921b TJ |
896 | printk("ACCOUNT: Out of memory allocating account_tables structure"); |
897 | return -EINVAL; | |
70288420 | 898 | } |
850cece6 | 899 | memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table)); |
70288420 | 900 | |
850cece6 | 901 | if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL))) |
70288420 | 902 | { |
bea3921b TJ |
903 | printk("ACCOUNT: Out of memory allocating account_handles structure"); |
904 | kfree (ipt_account_tables); | |
905 | ipt_account_tables = NULL; | |
906 | return -EINVAL; | |
70288420 | 907 | } |
850cece6 | 908 | memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle)); |
bea3921b TJ |
909 | |
910 | /* Register setsockopt */ | |
911 | if (nf_register_sockopt(&ipt_account_sockopts) < 0) | |
912 | { | |
913 | printk("ACCOUNT: Can't register sockopts. Aborting\n"); | |
914 | ||
915 | kfree (ipt_account_tables); | |
916 | ipt_account_tables = NULL; | |
917 | kfree(ipt_account_handles); | |
918 | ipt_account_handles = NULL; | |
919 | ||
920 | return -EINVAL; | |
921 | } | |
70288420 TJ |
922 | |
923 | if (ipt_register_target(&ipt_account_reg)) | |
924 | return -EINVAL; | |
925 | ||
926 | return 0; | |
927 | } | |
928 | ||
929 | static void __exit fini(void) | |
930 | { | |
931 | ipt_unregister_target(&ipt_account_reg); | |
932 | ||
bea3921b TJ |
933 | nf_unregister_sockopt(&ipt_account_sockopts); |
934 | ||
70288420 TJ |
935 | kfree(ipt_account_tables); |
936 | ipt_account_tables = NULL; | |
937 | ||
938 | kfree(ipt_account_handles); | |
939 | ipt_account_handles = NULL; | |
940 | } | |
941 | ||
942 | module_init(init); | |
943 | module_exit(fini); | |
944 | MODULE_LICENSE("GPL"); |