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 *
6 * Copyright (C) 2004-2006 by Intra2net AG *
7 * opensource@intra2net.com *
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; *
13 ***************************************************************************/
15 #include <linux/module.h>
16 #include <linux/version.h>
17 #include <linux/skbuff.h>
22 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <asm/semaphore.h>
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <asm/uaccess.h>
30 #include <net/route.h>
31 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
36 #define DEBUGP(format, args...)
39 #if (PAGE_SIZE < 4096)
40 #error "ipt_ACCOUNT needs at least a PAGE_SIZE of 4096"
43 static struct ipt_acc_table *ipt_acc_tables = NULL;
44 static struct ipt_acc_handle *ipt_acc_handles = NULL;
45 static void *ipt_acc_tmpbuf = NULL;
47 /* Spinlock used for manipulating the current accounting tables/data */
48 static DEFINE_SPINLOCK(ipt_acc_lock);
49 /* Mutex (semaphore) used for manipulating userspace handles/snapshot data */
50 static struct semaphore ipt_acc_userspace_mutex;
52 /* Allocates a page and clears it */
53 static void *ipt_acc_zalloc_page(void)
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);
59 memset (mem, 0, PAGE_SIZE);
65 /* Recursive free of all data structures */
66 static void ipt_acc_data_free(void *data, unsigned char depth)
72 /* Free for 8 bit network */
74 free_page((unsigned long)data);
78 /* Free for 16 bit network */
80 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16 *)data;
82 for (b=0; b <= 255; b++) {
83 if (mask_16->mask_24[b]) {
84 free_page((unsigned long)mask_16->mask_24[b]);
87 free_page((unsigned long)data);
91 /* Free for 24 bit network */
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];
99 for (b=0; b <= 255; b++) {
100 if (mask_16->mask_24[b]) {
101 free_page((unsigned long)mask_16->mask_24[b]);
104 free_page((unsigned long)mask_16);
107 free_page((unsigned long)data);
111 printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n",
116 /* Look for existing table / insert new one.
117 Return internal ID or -1 on error */
118 static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
122 DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n",
123 name, NIPQUAD(ip), NIPQUAD(netmask));
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));
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));
143 ipt_acc_tables[i].refcount++;
144 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount);
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;
155 int j; /* needs to be signed, otherwise we risk endless loop */
157 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
158 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
160 ipt_acc_tables[i].ip = ip;
161 ipt_acc_tables[i].netmask = netmask;
163 /* Calculate netsize */
164 calc_mask = htonl(netmask);
165 for (j = 31; j >= 0; j--) {
166 if (calc_mask&(1<<j))
172 /* Calculate depth from netsize */
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;
180 DEBUGP("ACCOUNT: calculated netsize: %u -> "
181 "ipt_acc_table depth %u\n", netsize,
182 ipt_acc_tables[i].depth);
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));
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);
203 static int ipt_acc_checkentry(const char *tablename,
204 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
207 const struct ipt_entry *e,
209 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
210 const struct xt_target *target,
213 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
214 unsigned int targinfosize,
216 unsigned int hook_mask)
218 struct ipt_acc_info *info = targinfo;
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)));
229 spin_lock_bh(&ipt_acc_lock);
230 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
232 spin_unlock_bh(&ipt_acc_lock);
234 if (table_nr == -1) {
235 printk("ACCOUNT: Table insert problem. Aborting\n");
238 /* Table nr caching so we don't have to do an extra string compare
240 info->table_nr = table_nr;
245 static void ipt_acc_destroy(
246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
247 const struct xt_target *target,
249 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
253 unsigned int targinfosize)
257 struct ipt_acc_info *info = targinfo;
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)));
266 spin_lock_bh(&ipt_acc_lock);
268 DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n",
269 info->table_name, info->table_nr);
271 info->table_nr = -1; /* Set back to original state */
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);
279 ipt_acc_tables[i].refcount--;
280 DEBUGP("ACCOUNT: Refcount left: %d\n",
281 ipt_acc_tables[i].refcount);
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));
292 spin_unlock_bh(&ipt_acc_lock);
297 /* Table not found */
298 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
299 spin_unlock_bh(&ipt_acc_lock);
302 static 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)
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 */
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);
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 */
318 if ((net_ip&netmask) == (src_ip&netmask))
320 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
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));
330 /* Calculate array positions */
331 src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
332 dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
334 /* Increase size counters */
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)
342 mask_24->ip[src_slot].src_packets++;
343 mask_24->ip[src_slot].src_bytes+=size;
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)
351 mask_24->ip[dst_slot].dst_packets++;
352 mask_24->ip[dst_slot].dst_bytes+=size;
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);
365 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
369 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
373 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
376 static 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)
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);
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");
393 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
394 net_ip, netmask, src_ip, 0, size, itemcount);
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);
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");
409 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
410 net_ip, netmask, 0, dst_ip, size, itemcount);
414 static 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)
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);
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");
431 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
432 net_ip, netmask, src_ip, 0, size, itemcount);
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);
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");
447 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
448 net_ip, netmask, 0, dst_ip, size, itemcount);
452 static 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,
459 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
460 const void *targinfo)
462 const void *targinfo,
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);
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);
478 spin_lock_bh(&ipt_acc_lock);
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);
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);
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);
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);
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));
526 spin_unlock_bh(&ipt_acc_lock);
531 Functions dealing with "handles":
532 Handles are snapshots of a accounting state.
534 read snapshots are only for debugging the code
535 and are very expensive concerning speed/memory
536 compared to read_and_flush.
538 The functions aren't protected by spinlocks themselves
539 as this is done in the ioctl part of the code.
543 Find a free handle slot. Normally only one should be used,
544 but there could be two or more applications accessing the data
547 static int ipt_acc_handle_find_slot(void)
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. */
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);
567 static int ipt_acc_handle_free(unsigned int handle)
569 if (handle >= ACCOUNT_MAX_HANDLES) {
570 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
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));
581 /* Prepare data for read without flush. Use only for debugging!
582 Real applications should use read&flush as it's way more efficent */
583 static int ipt_acc_handle_prepare_read(char *tablename,
584 struct ipt_acc_handle *dest, u_int32_t *count)
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)
594 if (table_nr == ACCOUNT_MAX_TABLES) {
595 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
596 "Table %s not found\n", tablename);
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;
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");
612 /* Recursive copy of complete data structure */
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;
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);
635 memcpy(network_16->mask_24[b], src_16->mask_24[b],
636 sizeof(struct ipt_acc_mask_24));
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;
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);
657 memcpy(network_8->mask_16[a], src_8->mask_16[a],
658 sizeof(struct ipt_acc_mask_16));
660 src_16 = src_8->mask_16[a];
661 network_16 = network_8->mask_16[a];
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);
673 memcpy(network_16->mask_24[b], src_16->mask_24[b],
674 sizeof(struct ipt_acc_mask_24));
681 *count = ipt_acc_tables[table_nr].itemcount;
686 /* Prepare data for read and flush it */
687 static int ipt_acc_handle_prepare_read_flush(char *tablename,
688 struct ipt_acc_handle *dest, u_int32_t *count)
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)
698 if (table_nr == ACCOUNT_MAX_TABLES) {
699 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
700 "Table %s not found\n", tablename);
704 /* Try to allocate memory */
705 if (!(new_data_page = ipt_acc_zalloc_page())) {
706 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
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;
718 /* "Flush" table data */
719 ipt_acc_tables[table_nr].data = new_data_page;
720 ipt_acc_tables[table_nr].itemcount = 0;
725 /* Copy 8 bit network data into a prepared buffer.
726 We only copy entries != 0 to increase performance.
728 static 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)
733 struct ipt_acc_handle_ip handle_ip;
734 size_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
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);
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;
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,
751 *to_user_pos = *to_user_pos + *tmpbuf_pos;
754 memcpy(ipt_acc_tmpbuf+*tmpbuf_pos, &handle_ip, handle_ip_size);
755 *tmpbuf_pos += handle_ip_size;
762 /* Copy the data from our internal structure
763 We only copy entries != 0 to increase performance.
764 Overwrites ipt_acc_tmpbuf.
766 static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
768 unsigned long to_user_pos=0, tmpbuf_pos=0;
772 if (handle >= ACCOUNT_MAX_HANDLES) {
773 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
774 "specified: %u\n", handle);
778 if (ipt_acc_handles[handle].data == NULL) {
779 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
783 net_ip = ipt_acc_handles[handle].ip;
784 depth = ipt_acc_handles[handle].depth;
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,
794 /* Flush remaining data to userspace */
796 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
804 struct ipt_acc_mask_16 *network_16 =
805 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
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)))
817 /* Flush remaining data to userspace */
819 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
827 struct ipt_acc_mask_8 *network_8 =
828 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
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)))
847 /* Flush remaining data to userspace */
849 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
858 static int ipt_acc_set_ctl(struct sock *sk, int cmd,
859 void *user, unsigned int len)
861 struct ipt_acc_handle_sockopt handle;
864 if (!capable(CAP_NET_ADMIN))
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));
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");
882 down(&ipt_acc_userspace_mutex);
883 ret = ipt_acc_handle_free(handle.handle_nr);
884 up(&ipt_acc_userspace_mutex);
886 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
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);
896 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
902 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
904 struct ipt_acc_handle_sockopt handle;
907 if (!capable(CAP_NET_ADMIN))
911 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
912 case IPT_SO_GET_ACCOUNT_PREPARE_READ: {
913 struct ipt_acc_handle dest;
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));
922 if (copy_from_user (&handle, user,
923 sizeof(struct ipt_acc_handle_sockopt))) {
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);
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?
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);
947 memcpy(&ipt_acc_handles[handle.handle_nr], &dest,
948 sizeof(struct ipt_acc_handle));
949 up(&ipt_acc_userspace_mutex);
951 if (copy_to_user(user, &handle,
952 sizeof(struct ipt_acc_handle_sockopt))) {
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));
967 if (copy_from_user (&handle, user,
968 sizeof(struct ipt_acc_handle_sockopt))) {
973 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
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));
988 down(&ipt_acc_userspace_mutex);
989 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
990 up(&ipt_acc_userspace_mutex);
992 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
993 " failed for handle %u\n", handle.handle_nr);
999 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
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));
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)
1014 up(&ipt_acc_userspace_mutex);
1016 if (copy_to_user(user, &handle,
1017 sizeof(struct ipt_acc_handle_sockopt))) {
1024 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
1025 u_int32_t size = 0, i, name_len;
1028 spin_lock_bh(&ipt_acc_lock);
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;
1035 size += 1; /* Terminating NULL character */
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);
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);
1053 spin_unlock_bh(&ipt_acc_lock);
1055 /* Terminating NULL character */
1058 /* Transfer to userspace */
1059 if (copy_to_user(user, ipt_acc_tmpbuf, size))
1066 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1072 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1073 static struct xt_target xt_acc_reg = {
1075 static struct ipt_target ipt_acc_reg = {
1078 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1081 .target = ipt_acc_target,
1082 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
1083 .targetsize = sizeof(struct ipt_acc_info),
1085 .checkentry = ipt_acc_checkentry,
1086 .destroy = ipt_acc_destroy,
1090 static struct nf_sockopt_ops ipt_acc_sockopts = {
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
1100 static int __init init(void)
1102 init_MUTEX(&ipt_acc_userspace_mutex);
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");
1110 memset(ipt_acc_tables, 0,
1111 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
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");
1119 memset(ipt_acc_handles, 0,
1120 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
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");
1128 /* Register setsockopt */
1129 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1130 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1134 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1135 if (xt_register_target(&xt_acc_reg))
1137 if (ipt_register_target(&ipt_acc_reg))
1145 kfree(ipt_acc_tables);
1147 kfree(ipt_acc_handles);
1149 free_page((unsigned long)ipt_acc_tmpbuf);
1154 static void __exit fini(void)
1156 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1157 xt_unregister_target(&xt_acc_reg);
1159 ipt_unregister_target(&ipt_acc_reg);
1162 nf_unregister_sockopt(&ipt_acc_sockopts);
1164 kfree(ipt_acc_tables);
1165 kfree(ipt_acc_handles);
1166 free_page((unsigned long)ipt_acc_tmpbuf);
1171 MODULE_LICENSE("GPL");