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-2005 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/skbuff.h>
18 #include <linux/spinlock.h>
22 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <asm/uaccess.h>
28 #include <net/route.h>
29 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
34 #define DEBUGP(format, args...)
37 #if (PAGE_SIZE < 4096)
38 #error "ipt_ACCOUNT needs at least a PAGE_SIZE of 4096"
41 static struct ipt_acc_table *ipt_acc_tables = NULL;
42 static struct ipt_acc_handle *ipt_acc_handles = NULL;
43 static void *ipt_acc_tmpbuf = NULL;
45 /* Spinlock used for manipulating the current accounting tables/data */
46 static spinlock_t ipt_acc_lock = SPIN_LOCK_UNLOCKED;
47 /* Spinlock used for manipulating userspace handles/snapshot data */
48 static spinlock_t ipt_acc_userspace_lock = SPIN_LOCK_UNLOCKED;
51 /* Recursive free of all data structures */
52 static void ipt_acc_data_free(void *data, unsigned char depth)
58 /* Free for 8 bit network */
60 free_page((unsigned long)data);
64 /* Free for 16 bit network */
66 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16 *)data;
68 for (b=0; b <= 255; b++) {
69 if (mask_16->mask_24[b] != 0) {
70 free_page((unsigned long)mask_16->mask_24[b]);
73 free_page((unsigned long)data);
77 /* Free for 24 bit network */
80 for (a=0; a <= 255; a++) {
81 if (((struct ipt_acc_mask_8 *)data)->mask_16[a]) {
82 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16*)
83 ((struct ipt_acc_mask_8 *)data)->mask_16[a];
85 for (b=0; b <= 255; b++) {
86 if (mask_16->mask_24[b]) {
87 free_page((unsigned long)mask_16->mask_24[b]);
90 free_page((unsigned long)mask_16);
93 free_page((unsigned long)data);
97 printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n",
102 /* Look for existing table / insert new one.
103 Return internal ID or -1 on error */
104 static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
108 DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n",
109 name, NIPQUAD(ip), NIPQUAD(netmask));
111 /* Look for existing table */
112 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
113 if (strncmp(ipt_acc_tables[i].name, name,
114 ACCOUNT_TABLE_NAME_LEN) == 0) {
115 DEBUGP("ACCOUNT: Found existing slot: %d - "
116 "%u.%u.%u.%u/%u.%u.%u.%u\n", i,
117 NIPQUAD(ipt_acc_tables[i].ip),
118 NIPQUAD(ipt_acc_tables[i].netmask));
120 if (ipt_acc_tables[i].ip != ip
121 || ipt_acc_tables[i].netmask != netmask) {
122 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. "
123 "IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
124 name, NIPQUAD(ipt_acc_tables[i].ip),
125 NIPQUAD(ipt_acc_tables[i].netmask));
129 ipt_acc_tables[i].refcount++;
130 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount);
135 /* Insert new table */
136 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
137 /* Found free slot */
138 if (ipt_acc_tables[i].name[0] == 0) {
139 u_int32_t j, calc_mask, netsize=0;
141 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
142 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
144 ipt_acc_tables[i].ip = ip;
145 ipt_acc_tables[i].netmask = netmask;
147 /* Calculate netsize */
148 calc_mask = htonl(netmask);
149 for (j = 31; j >= 0; j--) {
150 if (calc_mask&(1<<j))
156 /* Calculate depth from netsize */
158 ipt_acc_tables[i].depth = 0;
159 else if (netsize >= 16)
160 ipt_acc_tables[i].depth = 1;
161 else if(netsize >= 8)
162 ipt_acc_tables[i].depth = 2;
164 DEBUGP("ACCOUNT: calculated netsize: %u -> "
165 "ipt_acc_table depth %u\n", netsize,
166 ipt_acc_tables[i].depth);
168 ipt_acc_tables[i].refcount++;
169 if ((ipt_acc_tables[i].data
170 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
171 printk("ACCOUNT: out of memory for data of table: %s\n", name);
172 memset(&ipt_acc_tables[i], 0,
173 sizeof(struct ipt_acc_table));
181 /* No free slot found */
182 printk("ACCOUNT: No free table slot found (max: %d). "
183 "Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
187 static int ipt_acc_checkentry(const char *tablename,
188 const struct ipt_entry *e,
190 unsigned int targinfosize,
191 unsigned int hook_mask)
193 struct ipt_acc_info *info = targinfo;
196 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
197 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
198 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
202 spin_lock_bh(&ipt_acc_lock);
203 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
205 if (table_nr == -1) {
206 printk("ACCOUNT: Table insert problem. Aborting\n");
207 spin_unlock_bh(&ipt_acc_lock);
210 /* Table nr caching so we don't have to do an extra string compare
212 info->table_nr = table_nr;
214 spin_unlock_bh(&ipt_acc_lock);
219 static void ipt_acc_deleteentry(void *targinfo, unsigned int targinfosize)
222 struct ipt_acc_info *info = targinfo;
224 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
225 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
226 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
229 spin_lock_bh(&ipt_acc_lock);
231 DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n",
232 info->table_name, info->table_nr);
234 info->table_nr = -1; /* Set back to original state */
237 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
238 if (strncmp(ipt_acc_tables[i].name, info->table_name,
239 ACCOUNT_TABLE_NAME_LEN) == 0) {
240 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
242 ipt_acc_tables[i].refcount--;
243 DEBUGP("ACCOUNT: Refcount left: %d\n",
244 ipt_acc_tables[i].refcount);
246 /* Table not needed anymore? */
247 if (ipt_acc_tables[i].refcount == 0) {
248 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
249 ipt_acc_data_free(ipt_acc_tables[i].data,
250 ipt_acc_tables[i].depth);
251 memset(&ipt_acc_tables[i], 0,
252 sizeof(struct ipt_acc_table));
255 spin_unlock_bh(&ipt_acc_lock);
260 /* Table not found */
261 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
262 spin_unlock_bh(&ipt_acc_lock);
265 static void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24,
266 u_int32_t net_ip, u_int32_t netmask,
267 u_int32_t src_ip, u_int32_t dst_ip,
268 u_int32_t size, u_int32_t *itemcount)
270 unsigned char is_src = 0, is_dst = 0, src_slot, dst_slot;
271 char is_src_new_ip = 0, is_dst_new_ip = 0; /* Check if this entry is new */
273 DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
274 "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip),
275 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
277 /* Check if src/dst is inside our network. */
278 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
281 if ((net_ip&netmask) == (src_ip&netmask))
283 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
286 if (!is_src && !is_dst) {
287 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
288 "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip),
289 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
293 /* Calculate array positions */
294 src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
295 dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
297 /* Increase size counters */
299 /* Calculate network slot */
300 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
301 if (!mask_24->ip[src_slot].src_packets
302 && !mask_24->ip[src_slot].dst_packets)
305 mask_24->ip[src_slot].src_packets++;
306 mask_24->ip[src_slot].src_bytes+=size;
309 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
310 if (!mask_24->ip[dst_slot].src_packets
311 && !mask_24->ip[dst_slot].dst_packets)
314 mask_24->ip[dst_slot].dst_packets++;
315 mask_24->ip[dst_slot].dst_bytes+=size;
318 /* Increase itemcounter */
319 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
320 if (src_slot == dst_slot) {
321 if (is_src_new_ip || is_dst_new_ip) {
322 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n",
323 is_src_new_ip, is_dst_new_ip);
328 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
332 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
336 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
339 static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
340 u_int32_t net_ip, u_int32_t netmask,
341 u_int32_t src_ip, u_int32_t dst_ip,
342 u_int32_t size, u_int32_t *itemcount)
344 /* Do we need to process src IP? */
345 if ((net_ip&netmask) == (src_ip&netmask)) {
346 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
347 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
349 /* Do we need to create a new mask_24 bucket? */
350 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] =
351 (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
352 printk("ACCOUNT: Can't process packet because out of memory!\n");
356 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
357 net_ip, netmask, src_ip, 0, size, itemcount);
360 /* Do we need to process dst IP? */
361 if ((net_ip&netmask) == (dst_ip&netmask)) {
362 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
363 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
365 /* Do we need to create a new mask_24 bucket? */
366 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot]
367 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
368 printk("ACCOUT: Can't process packet because out of memory!\n");
372 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
373 net_ip, netmask, 0, dst_ip, size, itemcount);
377 static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
378 u_int32_t net_ip, u_int32_t netmask,
379 u_int32_t src_ip, u_int32_t dst_ip,
380 u_int32_t size, u_int32_t *itemcount)
382 /* Do we need to process src IP? */
383 if ((net_ip&netmask) == (src_ip&netmask)) {
384 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
385 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
387 /* Do we need to create a new mask_24 bucket? */
388 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
389 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
390 printk("ACCOUNT: Can't process packet because out of memory!\n");
394 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
395 net_ip, netmask, src_ip, 0, size, itemcount);
398 /* Do we need to process dst IP? */
399 if ((net_ip&netmask) == (dst_ip&netmask)) {
400 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
401 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
403 /* Do we need to create a new mask_24 bucket? */
404 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
405 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
406 printk("ACCOUNT: Can't process packet because out of memory!\n");
410 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
411 net_ip, netmask, 0, dst_ip, size, itemcount);
415 static unsigned int ipt_acc_target(struct sk_buff **pskb,
416 unsigned int hooknum,
417 const struct net_device *in,
418 const struct net_device *out,
419 const void *targinfo,
422 const struct ipt_acc_info *info =
423 (const struct ipt_acc_info *)targinfo;
424 u_int32_t src_ip = (*pskb)->nh.iph->saddr;
425 u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
426 u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
428 spin_lock_bh(&ipt_acc_lock);
430 if (ipt_acc_tables[info->table_nr].name[0] == 0) {
431 printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
432 "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr,
433 NIPQUAD(src_ip), NIPQUAD(dst_ip));
434 spin_unlock_bh(&ipt_acc_lock);
438 /* 8 bit network or "any" network */
439 if (ipt_acc_tables[info->table_nr].depth == 0) {
440 /* Count packet and check if the IP is new */
441 ipt_acc_depth0_insert(
442 (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
443 ipt_acc_tables[info->table_nr].ip,
444 ipt_acc_tables[info->table_nr].netmask,
445 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
446 spin_unlock_bh(&ipt_acc_lock);
451 if (ipt_acc_tables[info->table_nr].depth == 1) {
452 ipt_acc_depth1_insert(
453 (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
454 ipt_acc_tables[info->table_nr].ip,
455 ipt_acc_tables[info->table_nr].netmask,
456 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
457 spin_unlock_bh(&ipt_acc_lock);
462 if (ipt_acc_tables[info->table_nr].depth == 2) {
463 ipt_acc_depth2_insert(
464 (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
465 ipt_acc_tables[info->table_nr].ip,
466 ipt_acc_tables[info->table_nr].netmask,
467 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
468 spin_unlock_bh(&ipt_acc_lock);
472 printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
473 "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
474 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
476 spin_unlock_bh(&ipt_acc_lock);
481 Functions dealing with "handles":
482 Handles are snapshots of a accounting state.
484 read snapshots are only for debugging the code
485 and are very expensive concerning speed/memory
486 compared to read_and_flush.
488 The functions aren't protected by spinlocks themselves
489 as this is done in the ioctl part of the code.
493 Find a free handle slot. Normally only one should be used,
494 but there could be two or more applications accessing the data
497 static int ipt_acc_handle_find_slot(void)
500 /* Insert new table */
501 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
502 /* Found free slot */
503 if (ipt_acc_handles[i].data == NULL) {
504 /* Don't "mark" data as used as we are protected by a spinlock
505 by the calling function. handle_find_slot() is only a function
506 to prevent code duplication. */
511 /* No free slot found */
512 printk("ACCOUNT: No free handle slot found (max: %u). "
513 "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
517 static int ipt_acc_handle_free(u_int32_t handle)
519 if (handle >= ACCOUNT_MAX_HANDLES) {
520 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
525 ipt_acc_data_free(ipt_acc_handles[handle].data,
526 ipt_acc_handles[handle].depth);
527 memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
531 /* Prepare data for read without flush. Use only for debugging!
532 Real applications should use read&flush as it's way more efficent */
533 static int ipt_acc_handle_prepare_read(char *tablename, u_int32_t *count)
535 int handle, i, table_nr=-1;
538 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
539 if (strncmp(ipt_acc_tables[i].name, tablename,
540 ACCOUNT_TABLE_NAME_LEN) == 0) {
545 if (table_nr == -1) {
546 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
547 "Table %s not found\n", tablename);
551 /* Can't find a free handle slot? */
552 if ((handle = ipt_acc_handle_find_slot()) == -1)
555 /* Fill up handle structure */
556 ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
557 ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
558 ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
560 /* allocate "root" table */
561 if ((ipt_acc_handles[handle].data =
562 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
563 printk("ACCOUNT: out of memory for root table "
564 "in ipt_acc_handle_prepare_read()\n");
565 memset (&ipt_acc_handles[handle], 0,
566 sizeof(struct ipt_acc_handle));
570 /* Recursive copy of complete data structure */
571 depth = ipt_acc_handles[handle].depth;
573 memcpy(ipt_acc_handles[handle].data,
574 ipt_acc_tables[table_nr].data,
575 sizeof(struct ipt_acc_mask_24));
576 } else if (depth == 1) {
577 struct ipt_acc_mask_16 *src_16 =
578 (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data;
579 struct ipt_acc_mask_16 *network_16 =
580 (struct ipt_acc_mask_16 *)ipt_acc_handles[handle].data;
583 for (b = 0; b <= 255; b++) {
584 if (src_16->mask_24[b]) {
585 if ((network_16->mask_24[b] =
586 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
587 printk("ACCOUNT: out of memory during copy of 16 bit "
588 "network in ipt_acc_handle_prepare_read()\n");
589 ipt_acc_data_free(ipt_acc_handles[handle].data, depth);
590 memset (&ipt_acc_handles[handle], 0,
591 sizeof(struct ipt_acc_handle));
595 memcpy(network_16->mask_24[b], src_16->mask_24[b],
596 sizeof(struct ipt_acc_mask_24));
599 } else if(depth == 2) {
600 struct ipt_acc_mask_8 *src_8 =
601 (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data;
602 struct ipt_acc_mask_8 *network_8 =
603 (struct ipt_acc_mask_8 *)ipt_acc_handles[handle].data;
606 for (a = 0; a <= 255; a++) {
607 if (src_8->mask_16[a]) {
608 if ((network_8->mask_16[a] =
609 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
610 printk("ACCOUNT: out of memory during copy of 24 bit network"
611 " in ipt_acc_handle_prepare_read()\n");
612 ipt_acc_data_free(ipt_acc_handles[handle].data, depth);
613 memset (&ipt_acc_handles[handle], 0,
614 sizeof(struct ipt_acc_handle));
618 memcpy(network_8->mask_16[a], src_8->mask_16[a],
619 sizeof(struct ipt_acc_mask_16));
621 struct ipt_acc_mask_16 *src_16 = src_8->mask_16[a];
622 struct ipt_acc_mask_16 *network_16 = network_8->mask_16[a];
625 for (b = 0; b <= 255; b++) {
626 if (src_16->mask_24[b]) {
627 if ((network_16->mask_24[b] =
628 (void*)get_zeroed_page(GFP_ATOMIC)) == 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(ipt_acc_handles[handle].data,
633 memset (&ipt_acc_handles[handle], 0,
634 sizeof(struct ipt_acc_handle));
638 memcpy(network_16->mask_24[b], src_16->mask_24[b],
639 sizeof(struct ipt_acc_mask_24));
646 *count = ipt_acc_tables[table_nr].itemcount;
650 /* Prepare data for read and flush it */
651 static int ipt_acc_handle_prepare_read_flush(char *tablename, u_int32_t *count)
653 int handle, i, table_nr=-1;
656 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
657 if (strncmp(ipt_acc_tables[i].name, tablename,
658 ACCOUNT_TABLE_NAME_LEN) == 0) {
663 if (table_nr == -1) {
664 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
665 "Table %s not found\n", tablename);
669 /* Can't find a free handle slot? */
670 if ((handle = ipt_acc_handle_find_slot()) == -1)
673 /* Try to allocate memory */
674 if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
675 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
680 /* Fill up handle structure */
681 ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
682 ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
683 ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
684 ipt_acc_handles[handle].data = ipt_acc_tables[table_nr].data;
685 *count = ipt_acc_tables[table_nr].itemcount;
687 /* "Flush" table data */
688 ipt_acc_tables[table_nr].data = new_data_page;
689 ipt_acc_tables[table_nr].itemcount = 0;
694 /* Copy 8 bit network data into a prepared buffer.
695 We only copy entries != 0 to increase performance.
697 static int ipt_acc_handle_copy_data(void *to_user, u_int32_t *to_user_pos,
698 u_int32_t *tmpbuf_pos,
699 struct ipt_acc_mask_24 *data,
700 u_int32_t net_ip, u_int32_t net_OR_mask)
702 struct ipt_acc_handle_ip handle_ip;
703 u_int32_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
706 for (i = 0; i <= 255; i++) {
707 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
708 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
710 handle_ip.src_packets = data->ip[i].src_packets;
711 handle_ip.src_bytes = data->ip[i].src_bytes;
712 handle_ip.dst_packets = data->ip[i].dst_packets;
713 handle_ip.dst_bytes = data->ip[i].dst_bytes;
715 /* Temporary buffer full? Flush to userspace */
716 if (*tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
717 if (copy_to_user(to_user + *to_user_pos, ipt_acc_tmpbuf,
720 *to_user_pos = *to_user_pos + *tmpbuf_pos;
723 memcpy(ipt_acc_tmpbuf+*tmpbuf_pos, &handle_ip, handle_ip_size);
724 *tmpbuf_pos += handle_ip_size;
731 /* Copy the data from our internal structure
732 We only copy entries != 0 to increase performance.
733 Overwrites ipt_acc_tmpbuf.
735 static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
737 u_int32_t to_user_pos=0, tmpbuf_pos=0, net_ip;
740 if (handle >= ACCOUNT_MAX_HANDLES) {
741 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
742 "specified: %u\n", handle);
746 if (ipt_acc_handles[handle].data == NULL) {
747 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
751 net_ip = ipt_acc_handles[handle].ip;
752 depth = ipt_acc_handles[handle].depth;
756 struct ipt_acc_mask_24 *network =
757 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
758 if (ipt_acc_handle_copy_data(to_user, &to_user_pos, &tmpbuf_pos,
762 /* Flush remaining data to userspace */
764 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
772 struct ipt_acc_mask_16 *network_16 =
773 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
775 for (b = 0; b <= 255; b++) {
776 if (network_16->mask_24[b]) {
777 struct ipt_acc_mask_24 *network =
778 (struct ipt_acc_mask_24*)network_16->mask_24[b];
779 if (ipt_acc_handle_copy_data(to_user, &to_user_pos,
780 &tmpbuf_pos, network, net_ip, (b << 16)))
785 /* Flush remaining data to userspace */
787 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
795 struct ipt_acc_mask_8 *network_8 =
796 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
798 for (a = 0; a <= 255; a++) {
799 if (network_8->mask_16[a]) {
800 struct ipt_acc_mask_16 *network_16 =
801 (struct ipt_acc_mask_16*)network_8->mask_16[a];
802 for (b = 0; b <= 255; b++) {
803 if (network_16->mask_24[b]) {
804 struct ipt_acc_mask_24 *network =
805 (struct ipt_acc_mask_24*)network_16->mask_24[b];
806 if (ipt_acc_handle_copy_data(to_user,
807 &to_user_pos, &tmpbuf_pos,
808 network, net_ip, (a << 8) | (b << 16)))
815 /* Flush remaining data to userspace */
817 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
826 static int ipt_acc_set_ctl(struct sock *sk, int cmd,
827 void *user, u_int32_t len)
829 struct ipt_acc_handle_sockopt handle;
832 if (!capable(CAP_NET_ADMIN))
836 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
837 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
838 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
839 "for IPT_SO_SET_HANDLE_FREE\n",
840 len, sizeof(struct ipt_acc_handle_sockopt));
844 if (copy_from_user (&handle, user, len)) {
845 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
846 "IPT_SO_SET_HANDLE_FREE\n");
850 spin_lock_bh(&ipt_acc_userspace_lock);
851 ret = ipt_acc_handle_free(handle.handle_nr);
852 spin_unlock_bh(&ipt_acc_userspace_lock);
854 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
856 spin_lock_bh(&ipt_acc_userspace_lock);
857 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
858 ipt_acc_handle_free(i);
859 spin_unlock_bh(&ipt_acc_userspace_lock);
864 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
870 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
872 struct ipt_acc_handle_sockopt handle;
875 if (!capable(CAP_NET_ADMIN))
879 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
880 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
881 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
882 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
883 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
884 *len, sizeof(struct ipt_acc_handle_sockopt));
888 if (copy_from_user (&handle, user,
889 sizeof(struct ipt_acc_handle_sockopt))) {
894 spin_lock_bh(&ipt_acc_lock);
895 spin_lock_bh(&ipt_acc_userspace_lock);
896 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
897 handle.handle_nr = ipt_acc_handle_prepare_read_flush(
898 handle.name, &handle.itemcount);
900 handle.handle_nr = ipt_acc_handle_prepare_read(
901 handle.name, &handle.itemcount);
902 spin_unlock_bh(&ipt_acc_userspace_lock);
903 spin_unlock_bh(&ipt_acc_lock);
905 if (handle.handle_nr == -1) {
910 if (copy_to_user(user, &handle,
911 sizeof(struct ipt_acc_handle_sockopt))) {
917 case IPT_SO_GET_ACCOUNT_GET_DATA:
918 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
919 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
920 " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
921 *len, sizeof(struct ipt_acc_handle_sockopt));
925 if (copy_from_user (&handle, user,
926 sizeof(struct ipt_acc_handle_sockopt))) {
931 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
936 if (*len < ipt_acc_handles[handle.handle_nr].itemcount
937 * sizeof(struct ipt_acc_handle_ip)) {
938 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
939 " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
940 *len, ipt_acc_handles[handle.handle_nr].itemcount
941 * sizeof(struct ipt_acc_handle_ip));
946 spin_lock_bh(&ipt_acc_userspace_lock);
947 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
948 spin_unlock_bh(&ipt_acc_userspace_lock);
950 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
951 " failed for handle %u\n", handle.handle_nr);
957 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
959 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
960 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
961 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
962 *len, sizeof(struct ipt_acc_handle_sockopt));
966 /* Find out how many handles are in use */
967 handle.itemcount = 0;
968 spin_lock_bh(&ipt_acc_userspace_lock);
969 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
970 if (ipt_acc_handles[i].data)
972 spin_unlock_bh(&ipt_acc_userspace_lock);
974 if (copy_to_user(user, &handle,
975 sizeof(struct ipt_acc_handle_sockopt))) {
982 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
983 u_int32_t size = 0, i;
986 spin_lock_bh(&ipt_acc_lock);
988 /* Determine size of table names */
989 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
990 if (ipt_acc_tables[i].name[0] != 0)
991 size += strlen (ipt_acc_tables[i].name) + 1;
993 size += 1; /* Terminating NULL character */
996 spin_unlock_bh(&ipt_acc_lock);
997 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
998 " to store table names\n", *len, size);
1002 /* Copy table names to userspace */
1004 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1005 if (ipt_acc_tables[i].name[0] != 0) {
1006 int len = strlen (ipt_acc_tables[i].name) + 1;
1007 /* copy string + terminating zero */
1008 if (copy_to_user(tnames, ipt_acc_tables[i].name, len)) {
1009 spin_unlock_bh(&ipt_acc_lock);
1015 /* Append terminating zero */
1017 ret = copy_to_user(tnames, &i, 1);
1018 spin_unlock_bh(&ipt_acc_lock);
1025 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1031 static struct ipt_target ipt_acc_reg = {
1033 .target = ipt_acc_target,
1034 .checkentry = ipt_acc_checkentry,
1035 .destroy = ipt_acc_deleteentry,
1039 static struct nf_sockopt_ops ipt_acc_sockopts = {
1041 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1042 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1043 .set = ipt_acc_set_ctl,
1044 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1045 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1046 .get = ipt_acc_get_ctl
1049 static int __init init(void)
1051 if ((ipt_acc_tables =
1052 kmalloc(ACCOUNT_MAX_TABLES *
1053 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1054 printk("ACCOUNT: Out of memory allocating account_tables structure");
1057 memset(ipt_acc_tables, 0,
1058 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1060 if ((ipt_acc_handles =
1061 kmalloc(ACCOUNT_MAX_HANDLES *
1062 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1063 printk("ACCOUNT: Out of memory allocating account_handles structure");
1066 memset(ipt_acc_handles, 0,
1067 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1069 /* Allocate one page as temporary storage */
1070 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1071 printk("ACCOUNT: Out of memory for temporary buffer page\n");
1075 /* Register setsockopt */
1076 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1077 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1081 if (ipt_register_target(&ipt_acc_reg))
1088 kfree(ipt_acc_tables);
1090 kfree(ipt_acc_handles);
1092 free_page((unsigned long)ipt_acc_tmpbuf);
1097 static void __exit fini(void)
1099 ipt_unregister_target(&ipt_acc_reg);
1101 nf_unregister_sockopt(&ipt_acc_sockopts);
1103 kfree(ipt_acc_tables);
1104 kfree(ipt_acc_handles);
1105 free_page((unsigned long)ipt_acc_tmpbuf);
1110 MODULE_LICENSE("GPL");