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;
53 /* Recursive free of all data structures */
54 static void ipt_acc_data_free(void *data, unsigned char depth)
60 /* Free for 8 bit network */
62 free_page((unsigned long)data);
66 /* Free for 16 bit network */
68 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16 *)data;
70 for (b=0; b <= 255; b++) {
71 if (mask_16->mask_24[b]) {
72 free_page((unsigned long)mask_16->mask_24[b]);
75 free_page((unsigned long)data);
79 /* Free for 24 bit network */
82 for (a=0; a <= 255; a++) {
83 if (((struct ipt_acc_mask_8 *)data)->mask_16[a]) {
84 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16*)
85 ((struct ipt_acc_mask_8 *)data)->mask_16[a];
87 for (b=0; b <= 255; b++) {
88 if (mask_16->mask_24[b]) {
89 free_page((unsigned long)mask_16->mask_24[b]);
92 free_page((unsigned long)mask_16);
95 free_page((unsigned long)data);
99 printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n",
104 /* Look for existing table / insert new one.
105 Return internal ID or -1 on error */
106 static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
110 DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n",
111 name, NIPQUAD(ip), NIPQUAD(netmask));
113 /* Look for existing table */
114 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
115 if (strncmp(ipt_acc_tables[i].name, name,
116 ACCOUNT_TABLE_NAME_LEN) == 0) {
117 DEBUGP("ACCOUNT: Found existing slot: %d - "
118 "%u.%u.%u.%u/%u.%u.%u.%u\n", i,
119 NIPQUAD(ipt_acc_tables[i].ip),
120 NIPQUAD(ipt_acc_tables[i].netmask));
122 if (ipt_acc_tables[i].ip != ip
123 || ipt_acc_tables[i].netmask != netmask) {
124 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. "
125 "IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
126 name, NIPQUAD(ipt_acc_tables[i].ip),
127 NIPQUAD(ipt_acc_tables[i].netmask));
131 ipt_acc_tables[i].refcount++;
132 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount);
137 /* Insert new table */
138 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
139 /* Found free slot */
140 if (ipt_acc_tables[i].name[0] == 0) {
141 unsigned int netsize=0;
143 int j; /* needs to be signed, otherwise we risk endless loop */
145 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
146 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
148 ipt_acc_tables[i].ip = ip;
149 ipt_acc_tables[i].netmask = netmask;
151 /* Calculate netsize */
152 calc_mask = htonl(netmask);
153 for (j = 31; j >= 0; j--) {
154 if (calc_mask&(1<<j))
160 /* Calculate depth from netsize */
162 ipt_acc_tables[i].depth = 0;
163 else if (netsize >= 16)
164 ipt_acc_tables[i].depth = 1;
165 else if(netsize >= 8)
166 ipt_acc_tables[i].depth = 2;
168 DEBUGP("ACCOUNT: calculated netsize: %u -> "
169 "ipt_acc_table depth %u\n", netsize,
170 ipt_acc_tables[i].depth);
172 ipt_acc_tables[i].refcount++;
173 if ((ipt_acc_tables[i].data
174 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
175 printk("ACCOUNT: out of memory for data of table: %s\n", name);
176 memset(&ipt_acc_tables[i], 0,
177 sizeof(struct ipt_acc_table));
185 /* No free slot found */
186 printk("ACCOUNT: No free table slot found (max: %d). "
187 "Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
191 static int ipt_acc_checkentry(const char *tablename,
192 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
195 const struct ipt_entry *e,
197 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
198 const struct xt_target *target,
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
202 unsigned int targinfosize,
204 unsigned int hook_mask)
206 struct ipt_acc_info *info = targinfo;
209 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
210 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
211 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
212 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
217 spin_lock_bh(&ipt_acc_lock);
218 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
220 spin_unlock_bh(&ipt_acc_lock);
222 if (table_nr == -1) {
223 printk("ACCOUNT: Table insert problem. Aborting\n");
226 /* Table nr caching so we don't have to do an extra string compare
228 info->table_nr = table_nr;
233 static void ipt_acc_destroy(
234 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
235 const struct xt_target *target,
237 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
241 unsigned int targinfosize)
245 struct ipt_acc_info *info = targinfo;
247 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
248 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
249 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
250 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
254 spin_lock_bh(&ipt_acc_lock);
256 DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n",
257 info->table_name, info->table_nr);
259 info->table_nr = -1; /* Set back to original state */
262 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
263 if (strncmp(ipt_acc_tables[i].name, info->table_name,
264 ACCOUNT_TABLE_NAME_LEN) == 0) {
265 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
267 ipt_acc_tables[i].refcount--;
268 DEBUGP("ACCOUNT: Refcount left: %d\n",
269 ipt_acc_tables[i].refcount);
271 /* Table not needed anymore? */
272 if (ipt_acc_tables[i].refcount == 0) {
273 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
274 ipt_acc_data_free(ipt_acc_tables[i].data,
275 ipt_acc_tables[i].depth);
276 memset(&ipt_acc_tables[i], 0,
277 sizeof(struct ipt_acc_table));
280 spin_unlock_bh(&ipt_acc_lock);
285 /* Table not found */
286 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
287 spin_unlock_bh(&ipt_acc_lock);
290 static void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24,
291 u_int32_t net_ip, u_int32_t netmask,
292 u_int32_t src_ip, u_int32_t dst_ip,
293 u_int32_t size, u_int32_t *itemcount)
295 unsigned char is_src = 0, is_dst = 0, src_slot, dst_slot;
296 char is_src_new_ip = 0, is_dst_new_ip = 0; /* Check if this entry is new */
298 DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
299 "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip),
300 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
302 /* Check if src/dst is inside our network. */
303 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
306 if ((net_ip&netmask) == (src_ip&netmask))
308 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
311 if (!is_src && !is_dst) {
312 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
313 "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip),
314 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
318 /* Calculate array positions */
319 src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
320 dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
322 /* Increase size counters */
324 /* Calculate network slot */
325 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
326 if (!mask_24->ip[src_slot].src_packets
327 && !mask_24->ip[src_slot].dst_packets)
330 mask_24->ip[src_slot].src_packets++;
331 mask_24->ip[src_slot].src_bytes+=size;
334 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
335 if (!mask_24->ip[dst_slot].src_packets
336 && !mask_24->ip[dst_slot].dst_packets)
339 mask_24->ip[dst_slot].dst_packets++;
340 mask_24->ip[dst_slot].dst_bytes+=size;
343 /* Increase itemcounter */
344 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
345 if (src_slot == dst_slot) {
346 if (is_src_new_ip || is_dst_new_ip) {
347 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n",
348 is_src_new_ip, is_dst_new_ip);
353 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
357 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
361 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
364 static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
365 u_int32_t net_ip, u_int32_t netmask,
366 u_int32_t src_ip, u_int32_t dst_ip,
367 u_int32_t size, u_int32_t *itemcount)
369 /* Do we need to process src IP? */
370 if ((net_ip&netmask) == (src_ip&netmask)) {
371 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
372 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
374 /* Do we need to create a new mask_24 bucket? */
375 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] =
376 (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
377 printk("ACCOUNT: Can't process packet because out of memory!\n");
381 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
382 net_ip, netmask, src_ip, 0, size, itemcount);
385 /* Do we need to process dst IP? */
386 if ((net_ip&netmask) == (dst_ip&netmask)) {
387 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
388 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
390 /* Do we need to create a new mask_24 bucket? */
391 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot]
392 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
393 printk("ACCOUT: Can't process packet because out of memory!\n");
397 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
398 net_ip, netmask, 0, dst_ip, size, itemcount);
402 static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
403 u_int32_t net_ip, u_int32_t netmask,
404 u_int32_t src_ip, u_int32_t dst_ip,
405 u_int32_t size, u_int32_t *itemcount)
407 /* Do we need to process src IP? */
408 if ((net_ip&netmask) == (src_ip&netmask)) {
409 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
410 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
412 /* Do we need to create a new mask_24 bucket? */
413 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
414 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
415 printk("ACCOUNT: Can't process packet because out of memory!\n");
419 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
420 net_ip, netmask, src_ip, 0, size, itemcount);
423 /* Do we need to process dst IP? */
424 if ((net_ip&netmask) == (dst_ip&netmask)) {
425 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
426 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
428 /* Do we need to create a new mask_24 bucket? */
429 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
430 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
431 printk("ACCOUNT: Can't process packet because out of memory!\n");
435 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
436 net_ip, netmask, 0, dst_ip, size, itemcount);
440 static unsigned int ipt_acc_target(struct sk_buff **pskb,
441 const struct net_device *in,
442 const struct net_device *out,
443 unsigned int hooknum,
444 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
445 const struct xt_target *target,
447 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
448 const void *targinfo)
450 const void *targinfo,
454 const struct ipt_acc_info *info =
455 (const struct ipt_acc_info *)targinfo;
456 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
457 u_int32_t src_ip = ip_hdr(*pskb)->saddr;
458 u_int32_t dst_ip = ip_hdr(*pskb)->daddr;
459 u_int32_t size = ntohs(ip_hdr(*pskb)->tot_len);
461 u_int32_t src_ip = (*pskb)->nh.iph->saddr;
462 u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
463 u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
466 spin_lock_bh(&ipt_acc_lock);
468 if (ipt_acc_tables[info->table_nr].name[0] == 0) {
469 printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
470 "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr,
471 NIPQUAD(src_ip), NIPQUAD(dst_ip));
472 spin_unlock_bh(&ipt_acc_lock);
476 /* 8 bit network or "any" network */
477 if (ipt_acc_tables[info->table_nr].depth == 0) {
478 /* Count packet and check if the IP is new */
479 ipt_acc_depth0_insert(
480 (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
481 ipt_acc_tables[info->table_nr].ip,
482 ipt_acc_tables[info->table_nr].netmask,
483 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
484 spin_unlock_bh(&ipt_acc_lock);
489 if (ipt_acc_tables[info->table_nr].depth == 1) {
490 ipt_acc_depth1_insert(
491 (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
492 ipt_acc_tables[info->table_nr].ip,
493 ipt_acc_tables[info->table_nr].netmask,
494 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
495 spin_unlock_bh(&ipt_acc_lock);
500 if (ipt_acc_tables[info->table_nr].depth == 2) {
501 ipt_acc_depth2_insert(
502 (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
503 ipt_acc_tables[info->table_nr].ip,
504 ipt_acc_tables[info->table_nr].netmask,
505 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
506 spin_unlock_bh(&ipt_acc_lock);
510 printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
511 "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
512 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
514 spin_unlock_bh(&ipt_acc_lock);
519 Functions dealing with "handles":
520 Handles are snapshots of a accounting state.
522 read snapshots are only for debugging the code
523 and are very expensive concerning speed/memory
524 compared to read_and_flush.
526 The functions aren't protected by spinlocks themselves
527 as this is done in the ioctl part of the code.
531 Find a free handle slot. Normally only one should be used,
532 but there could be two or more applications accessing the data
535 static int ipt_acc_handle_find_slot(void)
538 /* Insert new table */
539 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
540 /* Found free slot */
541 if (ipt_acc_handles[i].data == NULL) {
542 /* Don't "mark" data as used as we are protected by a spinlock
543 by the calling function. handle_find_slot() is only a function
544 to prevent code duplication. */
549 /* No free slot found */
550 printk("ACCOUNT: No free handle slot found (max: %u). "
551 "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
555 static int ipt_acc_handle_free(unsigned int handle)
557 if (handle >= ACCOUNT_MAX_HANDLES) {
558 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
563 ipt_acc_data_free(ipt_acc_handles[handle].data,
564 ipt_acc_handles[handle].depth);
565 memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
569 /* Prepare data for read without flush. Use only for debugging!
570 Real applications should use read&flush as it's way more efficent */
571 static int ipt_acc_handle_prepare_read(char *tablename,
572 struct ipt_acc_handle *dest, u_int32_t *count)
577 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
578 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
579 ACCOUNT_TABLE_NAME_LEN) == 0)
582 if (table_nr == ACCOUNT_MAX_TABLES) {
583 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
584 "Table %s not found\n", tablename);
588 /* Fill up handle structure */
589 dest->ip = ipt_acc_tables[table_nr].ip;
590 dest->depth = ipt_acc_tables[table_nr].depth;
591 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
593 /* allocate "root" table */
594 if ((dest->data = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
595 printk("ACCOUNT: out of memory for root table "
596 "in ipt_acc_handle_prepare_read()\n");
600 /* Recursive copy of complete data structure */
604 ipt_acc_tables[table_nr].data,
605 sizeof(struct ipt_acc_mask_24));
606 } else if (depth == 1) {
607 struct ipt_acc_mask_16 *src_16 =
608 (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data;
609 struct ipt_acc_mask_16 *network_16 =
610 (struct ipt_acc_mask_16 *)dest->data;
613 for (b = 0; b <= 255; b++) {
614 if (src_16->mask_24[b]) {
615 if ((network_16->mask_24[b] =
616 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
617 printk("ACCOUNT: out of memory during copy of 16 bit "
618 "network in ipt_acc_handle_prepare_read()\n");
619 ipt_acc_data_free(dest->data, depth);
623 memcpy(network_16->mask_24[b], src_16->mask_24[b],
624 sizeof(struct ipt_acc_mask_24));
627 } else if(depth == 2) {
628 struct ipt_acc_mask_8 *src_8 =
629 (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data;
630 struct ipt_acc_mask_8 *network_8 =
631 (struct ipt_acc_mask_8 *)dest->data;
632 struct ipt_acc_mask_16 *src_16, *network_16;
635 for (a = 0; a <= 255; a++) {
636 if (src_8->mask_16[a]) {
637 if ((network_8->mask_16[a] =
638 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
639 printk("ACCOUNT: out of memory during copy of 24 bit network"
640 " in ipt_acc_handle_prepare_read()\n");
641 ipt_acc_data_free(dest->data, depth);
645 memcpy(network_8->mask_16[a], src_8->mask_16[a],
646 sizeof(struct ipt_acc_mask_16));
648 src_16 = src_8->mask_16[a];
649 network_16 = network_8->mask_16[a];
651 for (b = 0; b <= 255; b++) {
652 if (src_16->mask_24[b]) {
653 if ((network_16->mask_24[b] =
654 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
655 printk("ACCOUNT: out of memory during copy of 16 bit"
656 " network in ipt_acc_handle_prepare_read()\n");
657 ipt_acc_data_free(dest->data, depth);
661 memcpy(network_16->mask_24[b], src_16->mask_24[b],
662 sizeof(struct ipt_acc_mask_24));
669 *count = ipt_acc_tables[table_nr].itemcount;
674 /* Prepare data for read and flush it */
675 static int ipt_acc_handle_prepare_read_flush(char *tablename,
676 struct ipt_acc_handle *dest, u_int32_t *count)
681 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
682 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
683 ACCOUNT_TABLE_NAME_LEN) == 0)
686 if (table_nr == ACCOUNT_MAX_TABLES) {
687 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
688 "Table %s not found\n", tablename);
692 /* Try to allocate memory */
693 if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
694 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
699 /* Fill up handle structure */
700 dest->ip = ipt_acc_tables[table_nr].ip;
701 dest->depth = ipt_acc_tables[table_nr].depth;
702 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
703 dest->data = ipt_acc_tables[table_nr].data;
704 *count = ipt_acc_tables[table_nr].itemcount;
706 /* "Flush" table data */
707 ipt_acc_tables[table_nr].data = new_data_page;
708 ipt_acc_tables[table_nr].itemcount = 0;
713 /* Copy 8 bit network data into a prepared buffer.
714 We only copy entries != 0 to increase performance.
716 static int ipt_acc_handle_copy_data(void *to_user, unsigned long *to_user_pos,
717 unsigned long *tmpbuf_pos,
718 struct ipt_acc_mask_24 *data,
719 u_int32_t net_ip, u_int32_t net_OR_mask)
721 struct ipt_acc_handle_ip handle_ip;
722 size_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
725 for (i = 0; i <= 255; i++) {
726 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
727 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
729 handle_ip.src_packets = data->ip[i].src_packets;
730 handle_ip.src_bytes = data->ip[i].src_bytes;
731 handle_ip.dst_packets = data->ip[i].dst_packets;
732 handle_ip.dst_bytes = data->ip[i].dst_bytes;
734 /* Temporary buffer full? Flush to userspace */
735 if (*tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
736 if (copy_to_user(to_user + *to_user_pos, ipt_acc_tmpbuf,
739 *to_user_pos = *to_user_pos + *tmpbuf_pos;
742 memcpy(ipt_acc_tmpbuf+*tmpbuf_pos, &handle_ip, handle_ip_size);
743 *tmpbuf_pos += handle_ip_size;
750 /* Copy the data from our internal structure
751 We only copy entries != 0 to increase performance.
752 Overwrites ipt_acc_tmpbuf.
754 static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
756 unsigned long to_user_pos=0, tmpbuf_pos=0;
760 if (handle >= ACCOUNT_MAX_HANDLES) {
761 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
762 "specified: %u\n", handle);
766 if (ipt_acc_handles[handle].data == NULL) {
767 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
771 net_ip = ipt_acc_handles[handle].ip;
772 depth = ipt_acc_handles[handle].depth;
776 struct ipt_acc_mask_24 *network =
777 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
778 if (ipt_acc_handle_copy_data(to_user, &to_user_pos, &tmpbuf_pos,
782 /* Flush remaining data to userspace */
784 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
792 struct ipt_acc_mask_16 *network_16 =
793 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
795 for (b = 0; b <= 255; b++) {
796 if (network_16->mask_24[b]) {
797 struct ipt_acc_mask_24 *network =
798 (struct ipt_acc_mask_24*)network_16->mask_24[b];
799 if (ipt_acc_handle_copy_data(to_user, &to_user_pos,
800 &tmpbuf_pos, network, net_ip, (b << 16)))
805 /* Flush remaining data to userspace */
807 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
815 struct ipt_acc_mask_8 *network_8 =
816 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
818 for (a = 0; a <= 255; a++) {
819 if (network_8->mask_16[a]) {
820 struct ipt_acc_mask_16 *network_16 =
821 (struct ipt_acc_mask_16*)network_8->mask_16[a];
822 for (b = 0; b <= 255; b++) {
823 if (network_16->mask_24[b]) {
824 struct ipt_acc_mask_24 *network =
825 (struct ipt_acc_mask_24*)network_16->mask_24[b];
826 if (ipt_acc_handle_copy_data(to_user,
827 &to_user_pos, &tmpbuf_pos,
828 network, net_ip, (a << 8) | (b << 16)))
835 /* Flush remaining data to userspace */
837 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
846 static int ipt_acc_set_ctl(struct sock *sk, int cmd,
847 void *user, unsigned int len)
849 struct ipt_acc_handle_sockopt handle;
852 if (!capable(CAP_NET_ADMIN))
856 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
857 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
858 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
859 "for IPT_SO_SET_HANDLE_FREE\n",
860 len, sizeof(struct ipt_acc_handle_sockopt));
864 if (copy_from_user (&handle, user, len)) {
865 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
866 "IPT_SO_SET_HANDLE_FREE\n");
870 down(&ipt_acc_userspace_mutex);
871 ret = ipt_acc_handle_free(handle.handle_nr);
872 up(&ipt_acc_userspace_mutex);
874 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
876 down(&ipt_acc_userspace_mutex);
877 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
878 ipt_acc_handle_free(i);
879 up(&ipt_acc_userspace_mutex);
884 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
890 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
892 struct ipt_acc_handle_sockopt handle;
895 if (!capable(CAP_NET_ADMIN))
899 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
900 case IPT_SO_GET_ACCOUNT_PREPARE_READ: {
901 struct ipt_acc_handle dest;
903 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
904 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
905 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
906 *len, sizeof(struct ipt_acc_handle_sockopt));
910 if (copy_from_user (&handle, user,
911 sizeof(struct ipt_acc_handle_sockopt))) {
916 spin_lock_bh(&ipt_acc_lock);
917 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
918 ret = ipt_acc_handle_prepare_read_flush(
919 handle.name, &dest, &handle.itemcount);
921 ret = ipt_acc_handle_prepare_read(
922 handle.name, &dest, &handle.itemcount);
923 spin_unlock_bh(&ipt_acc_lock);
924 // Error occured during prepare_read?
928 /* Allocate a userspace handle */
929 down(&ipt_acc_userspace_mutex);
930 if ((handle.handle_nr = ipt_acc_handle_find_slot()) == -1) {
931 ipt_acc_data_free(dest.data, dest.depth);
932 up(&ipt_acc_userspace_mutex);
935 memcpy(&ipt_acc_handles[handle.handle_nr], &dest,
936 sizeof(struct ipt_acc_handle));
937 up(&ipt_acc_userspace_mutex);
939 if (copy_to_user(user, &handle,
940 sizeof(struct ipt_acc_handle_sockopt))) {
947 case IPT_SO_GET_ACCOUNT_GET_DATA:
948 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
949 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
950 " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
951 *len, sizeof(struct ipt_acc_handle_sockopt));
955 if (copy_from_user (&handle, user,
956 sizeof(struct ipt_acc_handle_sockopt))) {
961 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
966 if (*len < ipt_acc_handles[handle.handle_nr].itemcount
967 * sizeof(struct ipt_acc_handle_ip)) {
968 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
969 " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
970 *len, ipt_acc_handles[handle.handle_nr].itemcount
971 * sizeof(struct ipt_acc_handle_ip));
976 down(&ipt_acc_userspace_mutex);
977 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
978 up(&ipt_acc_userspace_mutex);
980 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
981 " failed for handle %u\n", handle.handle_nr);
987 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
989 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
990 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
991 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
992 *len, sizeof(struct ipt_acc_handle_sockopt));
996 /* Find out how many handles are in use */
997 handle.itemcount = 0;
998 down(&ipt_acc_userspace_mutex);
999 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
1000 if (ipt_acc_handles[i].data)
1002 up(&ipt_acc_userspace_mutex);
1004 if (copy_to_user(user, &handle,
1005 sizeof(struct ipt_acc_handle_sockopt))) {
1012 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
1013 u_int32_t size = 0, i, name_len;
1016 spin_lock_bh(&ipt_acc_lock);
1018 /* Determine size of table names */
1019 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1020 if (ipt_acc_tables[i].name[0] != 0)
1021 size += strlen (ipt_acc_tables[i].name) + 1;
1023 size += 1; /* Terminating NULL character */
1025 if (*len < size || size > PAGE_SIZE) {
1026 spin_unlock_bh(&ipt_acc_lock);
1027 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u < %lu)"
1028 " to store table names\n", *len, size, PAGE_SIZE);
1032 /* Copy table names to userspace */
1033 tnames = ipt_acc_tmpbuf;
1034 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1035 if (ipt_acc_tables[i].name[0] != 0) {
1036 name_len = strlen (ipt_acc_tables[i].name) + 1;
1037 memcpy(tnames, ipt_acc_tables[i].name, name_len);
1041 spin_unlock_bh(&ipt_acc_lock);
1043 /* Terminating NULL character */
1046 /* Transfer to userspace */
1047 if (copy_to_user(user, ipt_acc_tmpbuf, size))
1054 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1060 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1061 static struct xt_target xt_acc_reg = {
1063 static struct ipt_target ipt_acc_reg = {
1066 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1069 .target = ipt_acc_target,
1070 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
1071 .targetsize = sizeof(struct ipt_acc_info),
1073 .checkentry = ipt_acc_checkentry,
1074 .destroy = ipt_acc_destroy,
1078 static struct nf_sockopt_ops ipt_acc_sockopts = {
1080 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1081 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1082 .set = ipt_acc_set_ctl,
1083 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1084 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1085 .get = ipt_acc_get_ctl
1088 static int __init init(void)
1090 init_MUTEX(&ipt_acc_userspace_mutex);
1092 if ((ipt_acc_tables =
1093 kmalloc(ACCOUNT_MAX_TABLES *
1094 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1095 printk("ACCOUNT: Out of memory allocating account_tables structure");
1098 memset(ipt_acc_tables, 0,
1099 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1101 if ((ipt_acc_handles =
1102 kmalloc(ACCOUNT_MAX_HANDLES *
1103 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1104 printk("ACCOUNT: Out of memory allocating account_handles structure");
1107 memset(ipt_acc_handles, 0,
1108 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1110 /* Allocate one page as temporary storage */
1111 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1112 printk("ACCOUNT: Out of memory for temporary buffer page\n");
1116 /* Register setsockopt */
1117 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1118 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1122 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1123 if (xt_register_target(&xt_acc_reg))
1125 if (ipt_register_target(&ipt_acc_reg))
1133 kfree(ipt_acc_tables);
1135 kfree(ipt_acc_handles);
1137 free_page((unsigned long)ipt_acc_tmpbuf);
1142 static void __exit fini(void)
1144 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
1145 xt_unregister_target(&xt_acc_reg);
1147 ipt_unregister_target(&ipt_acc_reg);
1150 nf_unregister_sockopt(&ipt_acc_sockopts);
1152 kfree(ipt_acc_tables);
1153 kfree(ipt_acc_handles);
1154 free_page((unsigned long)ipt_acc_tmpbuf);
1159 MODULE_LICENSE("GPL");