ipt_ACCOUNT: (tomj) work around kernel 2.6 crash bug
[ipt_ACCOUNT] / linux-2.6 / net / ipv4 / netfilter / ipt_ACCOUNT.c
... / ...
CommitLineData
1/***************************************************************************
2 * This is a module which is used for counting packets. *
3 * See http://www.intra2net.com/opensource/ipt_account *
4 * for further information *
5 * *
6 * Copyright (C) 2004-2006 by Intra2net AG *
7 * opensource@intra2net.com *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License *
11 * version 2 as published by the Free Software Foundation; *
12 * *
13 ***************************************************************************/
14
15#include <linux/module.h>
16#include <linux/version.h>
17#include <linux/skbuff.h>
18#include <linux/ip.h>
19#include <net/icmp.h>
20#include <net/udp.h>
21#include <net/tcp.h>
22#include <linux/netfilter_ipv4/ip_tables.h>
23#include <asm/semaphore.h>
24#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/string.h>
27#include <linux/spinlock.h>
28#include <asm/uaccess.h>
29
30#include <net/route.h>
31#include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
32
33#if 0
34#define DEBUGP printk
35#else
36#define DEBUGP(format, args...)
37#endif
38
39#if (PAGE_SIZE < 4096)
40#error "ipt_ACCOUNT needs at least a PAGE_SIZE of 4096"
41#endif
42
43static struct ipt_acc_table *ipt_acc_tables = NULL;
44static struct ipt_acc_handle *ipt_acc_handles = NULL;
45static void *ipt_acc_tmpbuf = NULL;
46
47/* Spinlock used for manipulating the current accounting tables/data */
48static DEFINE_SPINLOCK(ipt_acc_lock);
49/* Mutex (semaphore) used for manipulating userspace handles/snapshot data */
50static struct semaphore ipt_acc_userspace_mutex;
51
52/* Allocates a page and clears it */
53static void *ipt_acc_zalloc_page(void)
54{
55 // Don't use get_zeroed_page until it's fixed in the kernel.
56 // get_zeroed_page(GFP_ATOMIC)
57 void *mem = (void *)__get_free_page(GFP_ATOMIC);
58 if (mem) {
59 memset (mem, 0, PAGE_SIZE);
60 }
61
62 return mem;
63}
64
65/* Recursive free of all data structures */
66static void ipt_acc_data_free(void *data, unsigned char depth)
67{
68 /* Empty data set */
69 if (!data)
70 return;
71
72 /* Free for 8 bit network */
73 if (depth == 0) {
74 free_page((unsigned long)data);
75 return;
76 }
77
78 /* Free for 16 bit network */
79 if (depth == 1) {
80 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16 *)data;
81 unsigned int b;
82 for (b=0; b <= 255; b++) {
83 if (mask_16->mask_24[b]) {
84 free_page((unsigned long)mask_16->mask_24[b]);
85 }
86 }
87 free_page((unsigned long)data);
88 return;
89 }
90
91 /* Free for 24 bit network */
92 if (depth == 2) {
93 unsigned int a, b;
94 for (a=0; a <= 255; a++) {
95 if (((struct ipt_acc_mask_8 *)data)->mask_16[a]) {
96 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16*)
97 ((struct ipt_acc_mask_8 *)data)->mask_16[a];
98
99 for (b=0; b <= 255; b++) {
100 if (mask_16->mask_24[b]) {
101 free_page((unsigned long)mask_16->mask_24[b]);
102 }
103 }
104 free_page((unsigned long)mask_16);
105 }
106 }
107 free_page((unsigned long)data);
108 return;
109 }
110
111 printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n",
112 depth);
113 return;
114}
115
116/* Look for existing table / insert new one.
117 Return internal ID or -1 on error */
118static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
119{
120 unsigned int i;
121
122 DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n",
123 name, NIPQUAD(ip), NIPQUAD(netmask));
124
125 /* Look for existing table */
126 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
127 if (strncmp(ipt_acc_tables[i].name, name,
128 ACCOUNT_TABLE_NAME_LEN) == 0) {
129 DEBUGP("ACCOUNT: Found existing slot: %d - "
130 "%u.%u.%u.%u/%u.%u.%u.%u\n", i,
131 NIPQUAD(ipt_acc_tables[i].ip),
132 NIPQUAD(ipt_acc_tables[i].netmask));
133
134 if (ipt_acc_tables[i].ip != ip
135 || ipt_acc_tables[i].netmask != netmask) {
136 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. "
137 "IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
138 name, NIPQUAD(ipt_acc_tables[i].ip),
139 NIPQUAD(ipt_acc_tables[i].netmask));
140 return -1;
141 }
142
143 ipt_acc_tables[i].refcount++;
144 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount);
145 return i;
146 }
147 }
148
149 /* Insert new table */
150 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
151 /* Found free slot */
152 if (ipt_acc_tables[i].name[0] == 0) {
153 unsigned int netsize=0;
154 u_int32_t calc_mask;
155 int j; /* needs to be signed, otherwise we risk endless loop */
156
157 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
158 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
159
160 ipt_acc_tables[i].ip = ip;
161 ipt_acc_tables[i].netmask = netmask;
162
163 /* Calculate netsize */
164 calc_mask = htonl(netmask);
165 for (j = 31; j >= 0; j--) {
166 if (calc_mask&(1<<j))
167 netsize++;
168 else
169 break;
170 }
171
172 /* Calculate depth from netsize */
173 if (netsize >= 24)
174 ipt_acc_tables[i].depth = 0;
175 else if (netsize >= 16)
176 ipt_acc_tables[i].depth = 1;
177 else if(netsize >= 8)
178 ipt_acc_tables[i].depth = 2;
179
180 DEBUGP("ACCOUNT: calculated netsize: %u -> "
181 "ipt_acc_table depth %u\n", netsize,
182 ipt_acc_tables[i].depth);
183
184 ipt_acc_tables[i].refcount++;
185 if ((ipt_acc_tables[i].data
186 = ipt_acc_zalloc_page()) == NULL) {
187 printk("ACCOUNT: out of memory for data of table: %s\n", name);
188 memset(&ipt_acc_tables[i], 0,
189 sizeof(struct ipt_acc_table));
190 return -1;
191 }
192
193 return i;
194 }
195 }
196
197 /* No free slot found */
198 printk("ACCOUNT: No free table slot found (max: %d). "
199 "Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
200 return -1;
201}
202
203static int ipt_acc_checkentry(const char *tablename,
204#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
205 const void *e,
206#else
207 const struct ipt_entry *e,
208#endif
209#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
210 const struct xt_target *target,
211#endif
212 void *targinfo,
213#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
214 unsigned int targinfosize,
215#endif
216 unsigned int hook_mask)
217{
218 struct ipt_acc_info *info = targinfo;
219 int table_nr;
220
221#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
222 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
223 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
224 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
225 return 0;
226 }
227#endif
228
229 spin_lock_bh(&ipt_acc_lock);
230 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
231 info->net_mask);
232 spin_unlock_bh(&ipt_acc_lock);
233
234 if (table_nr == -1) {
235 printk("ACCOUNT: Table insert problem. Aborting\n");
236 return 0;
237 }
238 /* Table nr caching so we don't have to do an extra string compare
239 for every packet */
240 info->table_nr = table_nr;
241
242 return 1;
243}
244
245static void ipt_acc_destroy(
246#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
247 const struct xt_target *target,
248#endif
249#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
250 void *targinfo)
251#else
252 void *targinfo,
253 unsigned int targinfosize)
254#endif
255{
256 unsigned int i;
257 struct ipt_acc_info *info = targinfo;
258
259#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
260 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
261 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
262 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
263 }
264#endif
265
266 spin_lock_bh(&ipt_acc_lock);
267
268 DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n",
269 info->table_name, info->table_nr);
270
271 info->table_nr = -1; /* Set back to original state */
272
273 /* Look for table */
274 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
275 if (strncmp(ipt_acc_tables[i].name, info->table_name,
276 ACCOUNT_TABLE_NAME_LEN) == 0) {
277 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
278
279 ipt_acc_tables[i].refcount--;
280 DEBUGP("ACCOUNT: Refcount left: %d\n",
281 ipt_acc_tables[i].refcount);
282
283 /* Table not needed anymore? */
284 if (ipt_acc_tables[i].refcount == 0) {
285 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
286 ipt_acc_data_free(ipt_acc_tables[i].data,
287 ipt_acc_tables[i].depth);
288 memset(&ipt_acc_tables[i], 0,
289 sizeof(struct ipt_acc_table));
290 }
291
292 spin_unlock_bh(&ipt_acc_lock);
293 return;
294 }
295 }
296
297 /* Table not found */
298 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
299 spin_unlock_bh(&ipt_acc_lock);
300}
301
302static void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24,
303 u_int32_t net_ip, u_int32_t netmask,
304 u_int32_t src_ip, u_int32_t dst_ip,
305 u_int32_t size, u_int32_t *itemcount)
306{
307 unsigned char is_src = 0, is_dst = 0, src_slot, dst_slot;
308 char is_src_new_ip = 0, is_dst_new_ip = 0; /* Check if this entry is new */
309
310 DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
311 "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip),
312 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
313
314 /* Check if src/dst is inside our network. */
315 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
316 if (!netmask)
317 src_ip = 0;
318 if ((net_ip&netmask) == (src_ip&netmask))
319 is_src = 1;
320 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
321 is_dst = 1;
322
323 if (!is_src && !is_dst) {
324 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
325 "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip),
326 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
327 return;
328 }
329
330 /* Calculate array positions */
331 src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
332 dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
333
334 /* Increase size counters */
335 if (is_src) {
336 /* Calculate network slot */
337 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
338 if (!mask_24->ip[src_slot].src_packets
339 && !mask_24->ip[src_slot].dst_packets)
340 is_src_new_ip = 1;
341
342 mask_24->ip[src_slot].src_packets++;
343 mask_24->ip[src_slot].src_bytes+=size;
344 }
345 if (is_dst) {
346 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
347 if (!mask_24->ip[dst_slot].src_packets
348 && !mask_24->ip[dst_slot].dst_packets)
349 is_dst_new_ip = 1;
350
351 mask_24->ip[dst_slot].dst_packets++;
352 mask_24->ip[dst_slot].dst_bytes+=size;
353 }
354
355 /* Increase itemcounter */
356 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
357 if (src_slot == dst_slot) {
358 if (is_src_new_ip || is_dst_new_ip) {
359 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n",
360 is_src_new_ip, is_dst_new_ip);
361 (*itemcount)++;
362 }
363 } else {
364 if (is_src_new_ip) {
365 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
366 (*itemcount)++;
367 }
368 if (is_dst_new_ip) {
369 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
370 (*itemcount)++;
371 }
372 }
373 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
374}
375
376static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
377 u_int32_t net_ip, u_int32_t netmask,
378 u_int32_t src_ip, u_int32_t dst_ip,
379 u_int32_t size, u_int32_t *itemcount)
380{
381 /* Do we need to process src IP? */
382 if ((net_ip&netmask) == (src_ip&netmask)) {
383 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
384 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
385
386 /* Do we need to create a new mask_24 bucket? */
387 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] =
388 ipt_acc_zalloc_page()) == NULL) {
389 printk("ACCOUNT: Can't process packet because out of memory!\n");
390 return;
391 }
392
393 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
394 net_ip, netmask, src_ip, 0, size, itemcount);
395 }
396
397 /* Do we need to process dst IP? */
398 if ((net_ip&netmask) == (dst_ip&netmask)) {
399 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
400 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
401
402 /* Do we need to create a new mask_24 bucket? */
403 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot]
404 = ipt_acc_zalloc_page()) == NULL) {
405 printk("ACCOUT: Can't process packet because out of memory!\n");
406 return;
407 }
408
409 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
410 net_ip, netmask, 0, dst_ip, size, itemcount);
411 }
412}
413
414static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
415 u_int32_t net_ip, u_int32_t netmask,
416 u_int32_t src_ip, u_int32_t dst_ip,
417 u_int32_t size, u_int32_t *itemcount)
418{
419 /* Do we need to process src IP? */
420 if ((net_ip&netmask) == (src_ip&netmask)) {
421 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
422 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
423
424 /* Do we need to create a new mask_24 bucket? */
425 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
426 = ipt_acc_zalloc_page()) == NULL) {
427 printk("ACCOUNT: Can't process packet because out of memory!\n");
428 return;
429 }
430
431 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
432 net_ip, netmask, src_ip, 0, size, itemcount);
433 }
434
435 /* Do we need to process dst IP? */
436 if ((net_ip&netmask) == (dst_ip&netmask)) {
437 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
438 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
439
440 /* Do we need to create a new mask_24 bucket? */
441 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
442 = ipt_acc_zalloc_page()) == NULL) {
443 printk("ACCOUNT: Can't process packet because out of memory!\n");
444 return;
445 }
446
447 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
448 net_ip, netmask, 0, dst_ip, size, itemcount);
449 }
450}
451
452static unsigned int ipt_acc_target(struct sk_buff **pskb,
453 const struct net_device *in,
454 const struct net_device *out,
455 unsigned int hooknum,
456#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
457 const struct xt_target *target,
458#endif
459#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
460 const void *targinfo)
461#else
462 const void *targinfo,
463 void *userinfo)
464#endif
465{
466 const struct ipt_acc_info *info =
467 (const struct ipt_acc_info *)targinfo;
468#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
469 u_int32_t src_ip = ip_hdr(*pskb)->saddr;
470 u_int32_t dst_ip = ip_hdr(*pskb)->daddr;
471 u_int32_t size = ntohs(ip_hdr(*pskb)->tot_len);
472#else
473 u_int32_t src_ip = (*pskb)->nh.iph->saddr;
474 u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
475 u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
476#endif
477
478 spin_lock_bh(&ipt_acc_lock);
479
480 if (ipt_acc_tables[info->table_nr].name[0] == 0) {
481 printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
482 "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr,
483 NIPQUAD(src_ip), NIPQUAD(dst_ip));
484 spin_unlock_bh(&ipt_acc_lock);
485 return IPT_CONTINUE;
486 }
487
488 /* 8 bit network or "any" network */
489 if (ipt_acc_tables[info->table_nr].depth == 0) {
490 /* Count packet and check if the IP is new */
491 ipt_acc_depth0_insert(
492 (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
493 ipt_acc_tables[info->table_nr].ip,
494 ipt_acc_tables[info->table_nr].netmask,
495 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
496 spin_unlock_bh(&ipt_acc_lock);
497 return IPT_CONTINUE;
498 }
499
500 /* 16 bit network */
501 if (ipt_acc_tables[info->table_nr].depth == 1) {
502 ipt_acc_depth1_insert(
503 (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
504 ipt_acc_tables[info->table_nr].ip,
505 ipt_acc_tables[info->table_nr].netmask,
506 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
507 spin_unlock_bh(&ipt_acc_lock);
508 return IPT_CONTINUE;
509 }
510
511 /* 24 bit network */
512 if (ipt_acc_tables[info->table_nr].depth == 2) {
513 ipt_acc_depth2_insert(
514 (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
515 ipt_acc_tables[info->table_nr].ip,
516 ipt_acc_tables[info->table_nr].netmask,
517 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
518 spin_unlock_bh(&ipt_acc_lock);
519 return IPT_CONTINUE;
520 }
521
522 printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
523 "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
524 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
525
526 spin_unlock_bh(&ipt_acc_lock);
527 return IPT_CONTINUE;
528}
529
530/*
531 Functions dealing with "handles":
532 Handles are snapshots of a accounting state.
533
534 read snapshots are only for debugging the code
535 and are very expensive concerning speed/memory
536 compared to read_and_flush.
537
538 The functions aren't protected by spinlocks themselves
539 as this is done in the ioctl part of the code.
540*/
541
542/*
543 Find a free handle slot. Normally only one should be used,
544 but there could be two or more applications accessing the data
545 at the same time.
546*/
547static int ipt_acc_handle_find_slot(void)
548{
549 unsigned int i;
550 /* Insert new table */
551 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
552 /* Found free slot */
553 if (ipt_acc_handles[i].data == NULL) {
554 /* Don't "mark" data as used as we are protected by a spinlock
555 by the calling function. handle_find_slot() is only a function
556 to prevent code duplication. */
557 return i;
558 }
559 }
560
561 /* No free slot found */
562 printk("ACCOUNT: No free handle slot found (max: %u). "
563 "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
564 return -1;
565}
566
567static int ipt_acc_handle_free(unsigned int handle)
568{
569 if (handle >= ACCOUNT_MAX_HANDLES) {
570 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
571 " %u\n", handle);
572 return -EINVAL;
573 }
574
575 ipt_acc_data_free(ipt_acc_handles[handle].data,
576 ipt_acc_handles[handle].depth);
577 memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
578 return 0;
579}
580
581/* Prepare data for read without flush. Use only for debugging!
582 Real applications should use read&flush as it's way more efficent */
583static int ipt_acc_handle_prepare_read(char *tablename,
584 struct ipt_acc_handle *dest, u_int32_t *count)
585{
586 int table_nr=-1;
587 unsigned char depth;
588
589 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
590 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
591 ACCOUNT_TABLE_NAME_LEN) == 0)
592 break;
593
594 if (table_nr == ACCOUNT_MAX_TABLES) {
595 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
596 "Table %s not found\n", tablename);
597 return -1;
598 }
599
600 /* Fill up handle structure */
601 dest->ip = ipt_acc_tables[table_nr].ip;
602 dest->depth = ipt_acc_tables[table_nr].depth;
603 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
604
605 /* allocate "root" table */
606 if ((dest->data = ipt_acc_zalloc_page()) == NULL) {
607 printk("ACCOUNT: out of memory for root table "
608 "in ipt_acc_handle_prepare_read()\n");
609 return -1;
610 }
611
612 /* Recursive copy of complete data structure */
613 depth = dest->depth;
614 if (depth == 0) {
615 memcpy(dest->data,
616 ipt_acc_tables[table_nr].data,
617 sizeof(struct ipt_acc_mask_24));
618 } else if (depth == 1) {
619 struct ipt_acc_mask_16 *src_16 =
620 (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data;
621 struct ipt_acc_mask_16 *network_16 =
622 (struct ipt_acc_mask_16 *)dest->data;
623 unsigned int b;
624
625 for (b = 0; b <= 255; b++) {
626 if (src_16->mask_24[b]) {
627 if ((network_16->mask_24[b] =
628 ipt_acc_zalloc_page()) == NULL) {
629 printk("ACCOUNT: out of memory during copy of 16 bit "
630 "network in ipt_acc_handle_prepare_read()\n");
631 ipt_acc_data_free(dest->data, depth);
632 return -1;
633 }
634
635 memcpy(network_16->mask_24[b], src_16->mask_24[b],
636 sizeof(struct ipt_acc_mask_24));
637 }
638 }
639 } else if(depth == 2) {
640 struct ipt_acc_mask_8 *src_8 =
641 (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data;
642 struct ipt_acc_mask_8 *network_8 =
643 (struct ipt_acc_mask_8 *)dest->data;
644 struct ipt_acc_mask_16 *src_16, *network_16;
645 unsigned int a, b;
646
647 for (a = 0; a <= 255; a++) {
648 if (src_8->mask_16[a]) {
649 if ((network_8->mask_16[a] =
650 ipt_acc_zalloc_page()) == NULL) {
651 printk("ACCOUNT: out of memory during copy of 24 bit network"
652 " in ipt_acc_handle_prepare_read()\n");
653 ipt_acc_data_free(dest->data, depth);
654 return -1;
655 }
656
657 memcpy(network_8->mask_16[a], src_8->mask_16[a],
658 sizeof(struct ipt_acc_mask_16));
659
660 src_16 = src_8->mask_16[a];
661 network_16 = network_8->mask_16[a];
662
663 for (b = 0; b <= 255; b++) {
664 if (src_16->mask_24[b]) {
665 if ((network_16->mask_24[b] =
666 ipt_acc_zalloc_page()) == NULL) {
667 printk("ACCOUNT: out of memory during copy of 16 bit"
668 " network in ipt_acc_handle_prepare_read()\n");
669 ipt_acc_data_free(dest->data, depth);
670 return -1;
671 }
672
673 memcpy(network_16->mask_24[b], src_16->mask_24[b],
674 sizeof(struct ipt_acc_mask_24));
675 }
676 }
677 }
678 }
679 }
680
681 *count = ipt_acc_tables[table_nr].itemcount;
682
683 return 0;
684}
685
686/* Prepare data for read and flush it */
687static int ipt_acc_handle_prepare_read_flush(char *tablename,
688 struct ipt_acc_handle *dest, u_int32_t *count)
689{
690 int table_nr;
691 void *new_data_page;
692
693 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
694 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
695 ACCOUNT_TABLE_NAME_LEN) == 0)
696 break;
697
698 if (table_nr == ACCOUNT_MAX_TABLES) {
699 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
700 "Table %s not found\n", tablename);
701 return -1;
702 }
703
704 /* Try to allocate memory */
705 if (!(new_data_page = ipt_acc_zalloc_page())) {
706 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
707 "Out of memory!\n");
708 return -1;
709 }
710
711 /* Fill up handle structure */
712 dest->ip = ipt_acc_tables[table_nr].ip;
713 dest->depth = ipt_acc_tables[table_nr].depth;
714 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
715 dest->data = ipt_acc_tables[table_nr].data;
716 *count = ipt_acc_tables[table_nr].itemcount;
717
718 /* "Flush" table data */
719 ipt_acc_tables[table_nr].data = new_data_page;
720 ipt_acc_tables[table_nr].itemcount = 0;
721
722 return 0;
723}
724
725/* Copy 8 bit network data into a prepared buffer.
726 We only copy entries != 0 to increase performance.
727*/
728static int ipt_acc_handle_copy_data(void *to_user, unsigned long *to_user_pos,
729 unsigned long *tmpbuf_pos,
730 struct ipt_acc_mask_24 *data,
731 u_int32_t net_ip, u_int32_t net_OR_mask)
732{
733 struct ipt_acc_handle_ip handle_ip;
734 size_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
735 unsigned int i;
736
737 for (i = 0; i <= 255; i++) {
738 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
739 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
740
741 handle_ip.src_packets = data->ip[i].src_packets;
742 handle_ip.src_bytes = data->ip[i].src_bytes;
743 handle_ip.dst_packets = data->ip[i].dst_packets;
744 handle_ip.dst_bytes = data->ip[i].dst_bytes;
745
746 /* Temporary buffer full? Flush to userspace */
747 if (*tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
748 if (copy_to_user(to_user + *to_user_pos, ipt_acc_tmpbuf,
749 *tmpbuf_pos))
750 return -EFAULT;
751 *to_user_pos = *to_user_pos + *tmpbuf_pos;
752 *tmpbuf_pos = 0;
753 }
754 memcpy(ipt_acc_tmpbuf+*tmpbuf_pos, &handle_ip, handle_ip_size);
755 *tmpbuf_pos += handle_ip_size;
756 }
757 }
758
759 return 0;
760}
761
762/* Copy the data from our internal structure
763 We only copy entries != 0 to increase performance.
764 Overwrites ipt_acc_tmpbuf.
765*/
766static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
767{
768 unsigned long to_user_pos=0, tmpbuf_pos=0;
769 u_int32_t net_ip;
770 unsigned char depth;
771
772 if (handle >= ACCOUNT_MAX_HANDLES) {
773 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
774 "specified: %u\n", handle);
775 return -1;
776 }
777
778 if (ipt_acc_handles[handle].data == NULL) {
779 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
780 return -1;
781 }
782
783 net_ip = ipt_acc_handles[handle].ip;
784 depth = ipt_acc_handles[handle].depth;
785
786 /* 8 bit network */
787 if (depth == 0) {
788 struct ipt_acc_mask_24 *network =
789 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
790 if (ipt_acc_handle_copy_data(to_user, &to_user_pos, &tmpbuf_pos,
791 network, net_ip, 0))
792 return -1;
793
794 /* Flush remaining data to userspace */
795 if (tmpbuf_pos)
796 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
797 return -1;
798
799 return 0;
800 }
801
802 /* 16 bit network */
803 if (depth == 1) {
804 struct ipt_acc_mask_16 *network_16 =
805 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
806 unsigned int b;
807 for (b = 0; b <= 255; b++) {
808 if (network_16->mask_24[b]) {
809 struct ipt_acc_mask_24 *network =
810 (struct ipt_acc_mask_24*)network_16->mask_24[b];
811 if (ipt_acc_handle_copy_data(to_user, &to_user_pos,
812 &tmpbuf_pos, network, net_ip, (b << 16)))
813 return -1;
814 }
815 }
816
817 /* Flush remaining data to userspace */
818 if (tmpbuf_pos)
819 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
820 return -1;
821
822 return 0;
823 }
824
825 /* 24 bit network */
826 if (depth == 2) {
827 struct ipt_acc_mask_8 *network_8 =
828 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
829 unsigned int a, b;
830 for (a = 0; a <= 255; a++) {
831 if (network_8->mask_16[a]) {
832 struct ipt_acc_mask_16 *network_16 =
833 (struct ipt_acc_mask_16*)network_8->mask_16[a];
834 for (b = 0; b <= 255; b++) {
835 if (network_16->mask_24[b]) {
836 struct ipt_acc_mask_24 *network =
837 (struct ipt_acc_mask_24*)network_16->mask_24[b];
838 if (ipt_acc_handle_copy_data(to_user,
839 &to_user_pos, &tmpbuf_pos,
840 network, net_ip, (a << 8) | (b << 16)))
841 return -1;
842 }
843 }
844 }
845 }
846
847 /* Flush remaining data to userspace */
848 if (tmpbuf_pos)
849 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
850 return -1;
851
852 return 0;
853 }
854
855 return -1;
856}
857
858static int ipt_acc_set_ctl(struct sock *sk, int cmd,
859 void *user, unsigned int len)
860{
861 struct ipt_acc_handle_sockopt handle;
862 int ret = -EINVAL;
863
864 if (!capable(CAP_NET_ADMIN))
865 return -EPERM;
866
867 switch (cmd) {
868 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
869 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
870 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
871 "for IPT_SO_SET_HANDLE_FREE\n",
872 len, sizeof(struct ipt_acc_handle_sockopt));
873 break;
874 }
875
876 if (copy_from_user (&handle, user, len)) {
877 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
878 "IPT_SO_SET_HANDLE_FREE\n");
879 break;
880 }
881
882 down(&ipt_acc_userspace_mutex);
883 ret = ipt_acc_handle_free(handle.handle_nr);
884 up(&ipt_acc_userspace_mutex);
885 break;
886 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
887 unsigned int i;
888 down(&ipt_acc_userspace_mutex);
889 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
890 ipt_acc_handle_free(i);
891 up(&ipt_acc_userspace_mutex);
892 ret = 0;
893 break;
894 }
895 default:
896 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
897 }
898
899 return ret;
900}
901
902static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
903{
904 struct ipt_acc_handle_sockopt handle;
905 int ret = -EINVAL;
906
907 if (!capable(CAP_NET_ADMIN))
908 return -EPERM;
909
910 switch (cmd) {
911 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
912 case IPT_SO_GET_ACCOUNT_PREPARE_READ: {
913 struct ipt_acc_handle dest;
914
915 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
916 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
917 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
918 *len, sizeof(struct ipt_acc_handle_sockopt));
919 break;
920 }
921
922 if (copy_from_user (&handle, user,
923 sizeof(struct ipt_acc_handle_sockopt))) {
924 return -EFAULT;
925 break;
926 }
927
928 spin_lock_bh(&ipt_acc_lock);
929 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
930 ret = ipt_acc_handle_prepare_read_flush(
931 handle.name, &dest, &handle.itemcount);
932 else
933 ret = ipt_acc_handle_prepare_read(
934 handle.name, &dest, &handle.itemcount);
935 spin_unlock_bh(&ipt_acc_lock);
936 // Error occured during prepare_read?
937 if (ret == -1)
938 return -EINVAL;
939
940 /* Allocate a userspace handle */
941 down(&ipt_acc_userspace_mutex);
942 if ((handle.handle_nr = ipt_acc_handle_find_slot()) == -1) {
943 ipt_acc_data_free(dest.data, dest.depth);
944 up(&ipt_acc_userspace_mutex);
945 return -EINVAL;
946 }
947 memcpy(&ipt_acc_handles[handle.handle_nr], &dest,
948 sizeof(struct ipt_acc_handle));
949 up(&ipt_acc_userspace_mutex);
950
951 if (copy_to_user(user, &handle,
952 sizeof(struct ipt_acc_handle_sockopt))) {
953 return -EFAULT;
954 break;
955 }
956 ret = 0;
957 break;
958 }
959 case IPT_SO_GET_ACCOUNT_GET_DATA:
960 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
961 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
962 " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
963 *len, sizeof(struct ipt_acc_handle_sockopt));
964 break;
965 }
966
967 if (copy_from_user (&handle, user,
968 sizeof(struct ipt_acc_handle_sockopt))) {
969 return -EFAULT;
970 break;
971 }
972
973 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
974 return -EINVAL;
975 break;
976 }
977
978 if (*len < ipt_acc_handles[handle.handle_nr].itemcount
979 * sizeof(struct ipt_acc_handle_ip)) {
980 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
981 " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
982 *len, ipt_acc_handles[handle.handle_nr].itemcount
983 * sizeof(struct ipt_acc_handle_ip));
984 ret = -ENOMEM;
985 break;
986 }
987
988 down(&ipt_acc_userspace_mutex);
989 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
990 up(&ipt_acc_userspace_mutex);
991 if (ret) {
992 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
993 " failed for handle %u\n", handle.handle_nr);
994 break;
995 }
996
997 ret = 0;
998 break;
999 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
1000 unsigned int i;
1001 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
1002 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
1003 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
1004 *len, sizeof(struct ipt_acc_handle_sockopt));
1005 break;
1006 }
1007
1008 /* Find out how many handles are in use */
1009 handle.itemcount = 0;
1010 down(&ipt_acc_userspace_mutex);
1011 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
1012 if (ipt_acc_handles[i].data)
1013 handle.itemcount++;
1014 up(&ipt_acc_userspace_mutex);
1015
1016 if (copy_to_user(user, &handle,
1017 sizeof(struct ipt_acc_handle_sockopt))) {
1018 return -EFAULT;
1019 break;
1020 }
1021 ret = 0;
1022 break;
1023 }
1024 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
1025 u_int32_t size = 0, i, name_len;
1026 char *tnames;
1027
1028 spin_lock_bh(&ipt_acc_lock);
1029
1030 /* Determine size of table names */
1031 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1032 if (ipt_acc_tables[i].name[0] != 0)
1033 size += strlen (ipt_acc_tables[i].name) + 1;
1034 }
1035 size += 1; /* Terminating NULL character */
1036
1037 if (*len < size || size > PAGE_SIZE) {
1038 spin_unlock_bh(&ipt_acc_lock);
1039 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u < %lu)"
1040 " to store table names\n", *len, size, PAGE_SIZE);
1041 ret = -ENOMEM;
1042 break;
1043 }
1044 /* Copy table names to userspace */
1045 tnames = ipt_acc_tmpbuf;
1046 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1047 if (ipt_acc_tables[i].name[0] != 0) {
1048 name_len = strlen (ipt_acc_tables[i].name) + 1;
1049 memcpy(tnames, ipt_acc_tables[i].name, name_len);
1050 tnames += name_len;
1051 }
1052 }
1053 spin_unlock_bh(&ipt_acc_lock);
1054
1055 /* Terminating NULL character */
1056 *tnames = 0;
1057
1058 /* Transfer to userspace */
1059 if (copy_to_user(user, ipt_acc_tmpbuf, size))
1060 return -EFAULT;
1061
1062 ret = 0;
1063 break;
1064 }
1065 default:
1066 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1067 }
1068
1069 return ret;
1070}
1071
1072#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1073static struct xt_target xt_acc_reg = {
1074#else
1075static struct ipt_target ipt_acc_reg = {
1076#endif
1077 .name = "ACCOUNT",
1078#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1079 .family = AF_INET,
1080#endif
1081 .target = ipt_acc_target,
1082#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
1083 .targetsize = sizeof(struct ipt_acc_info),
1084#endif
1085 .checkentry = ipt_acc_checkentry,
1086 .destroy = ipt_acc_destroy,
1087 .me = THIS_MODULE
1088};
1089
1090static struct nf_sockopt_ops ipt_acc_sockopts = {
1091 .pf = PF_INET,
1092 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1093 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1094 .set = ipt_acc_set_ctl,
1095 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1096 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1097 .get = ipt_acc_get_ctl
1098};
1099
1100static int __init init(void)
1101{
1102 init_MUTEX(&ipt_acc_userspace_mutex);
1103
1104 if ((ipt_acc_tables =
1105 kmalloc(ACCOUNT_MAX_TABLES *
1106 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1107 printk("ACCOUNT: Out of memory allocating account_tables structure");
1108 goto error_cleanup;
1109 }
1110 memset(ipt_acc_tables, 0,
1111 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1112
1113 if ((ipt_acc_handles =
1114 kmalloc(ACCOUNT_MAX_HANDLES *
1115 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1116 printk("ACCOUNT: Out of memory allocating account_handles structure");
1117 goto error_cleanup;
1118 }
1119 memset(ipt_acc_handles, 0,
1120 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1121
1122 /* Allocate one page as temporary storage */
1123 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1124 printk("ACCOUNT: Out of memory for temporary buffer page\n");
1125 goto error_cleanup;
1126 }
1127
1128 /* Register setsockopt */
1129 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1130 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1131 goto error_cleanup;
1132 }
1133
1134#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1135 if (xt_register_target(&xt_acc_reg))
1136#else
1137 if (ipt_register_target(&ipt_acc_reg))
1138#endif
1139 goto error_cleanup;
1140
1141 return 0;
1142
1143error_cleanup:
1144 if(ipt_acc_tables)
1145 kfree(ipt_acc_tables);
1146 if(ipt_acc_handles)
1147 kfree(ipt_acc_handles);
1148 if (ipt_acc_tmpbuf)
1149 free_page((unsigned long)ipt_acc_tmpbuf);
1150
1151 return -EINVAL;
1152}
1153
1154static void __exit fini(void)
1155{
1156#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1157 xt_unregister_target(&xt_acc_reg);
1158#else
1159 ipt_unregister_target(&ipt_acc_reg);
1160#endif
1161
1162 nf_unregister_sockopt(&ipt_acc_sockopts);
1163
1164 kfree(ipt_acc_tables);
1165 kfree(ipt_acc_handles);
1166 free_page((unsigned long)ipt_acc_tmpbuf);
1167}
1168
1169module_init(init);
1170module_exit(fini);
1171MODULE_LICENSE("GPL");