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 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>
29 #include <net/route.h>
30 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
35 #define DEBUGP(format, args...)
38 #if (PAGE_SIZE < 4096)
39 #error "ipt_ACCOUNT needs at least a PAGE_SIZE of 4096"
42 struct ipt_acc_table *ipt_acc_tables = NULL;
43 struct ipt_acc_handle *ipt_acc_handles = NULL;
44 void *ipt_acc_tmpbuf = NULL;
46 /* Spinlock used for manipulating the current accounting tables/data */
47 static spinlock_t ipt_acc_lock = SPIN_LOCK_UNLOCKED;
48 /* Spinlock used for manipulating userspace handles/snapshot data */
49 static spinlock_t ipt_acc_userspace_lock = SPIN_LOCK_UNLOCKED;
52 /* Recursive free of all data structures */
53 static void ipt_acc_data_free(void *data, unsigned char depth)
59 /* Free for 8 bit network */
61 free_page((unsigned long)data);
65 /* Free for 16 bit network */
67 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16 *)data;
69 for (b=0; b <= 255; b++) {
70 if (mask_16->mask_24[b] != 0) {
71 free_page((unsigned long)mask_16->mask_24[b]);
74 free_page((unsigned long)data);
78 /* Free for 24 bit network */
81 for (a=0; a <= 255; a++) {
82 if (((struct ipt_acc_mask_8 *)data)->mask_16[a]) {
83 struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16*)
84 ((struct ipt_acc_mask_8 *)data)->mask_16[a];
86 for (b=0; b <= 255; b++) {
87 if (mask_16->mask_24[b]) {
88 free_page((unsigned long)mask_16->mask_24[b]);
91 free_page((unsigned long)mask_16);
94 free_page((unsigned long)data);
98 printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n",
103 /* Look for existing table / insert new one.
104 Return internal ID or -1 on error */
105 static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
109 DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n",
110 name, NIPQUAD(ip), NIPQUAD(netmask));
112 /* Look for existing table */
113 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
114 if (strncmp(ipt_acc_tables[i].name, name,
115 ACCOUNT_TABLE_NAME_LEN) == 0) {
116 DEBUGP("ACCOUNT: Found existing slot: %d - "
117 "%u.%u.%u.%u/%u.%u.%u.%u\n", i,
118 NIPQUAD(ipt_acc_tables[i].ip),
119 NIPQUAD(ipt_acc_tables[i].netmask));
121 if (ipt_acc_tables[i].ip != ip
122 || ipt_acc_tables[i].netmask != netmask) {
123 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. "
124 "IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
125 name, NIPQUAD(ipt_acc_tables[i].ip),
126 NIPQUAD(ipt_acc_tables[i].netmask));
130 ipt_acc_tables[i].refcount++;
131 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount);
136 /* Insert new table */
137 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
138 /* Found free slot */
139 if (ipt_acc_tables[i].name[0] == 0) {
140 u_int32_t j, calc_mask, netsize=0;
142 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
143 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
145 ipt_acc_tables[i].ip = ip;
146 ipt_acc_tables[i].netmask = netmask;
148 /* Calculate netsize */
149 calc_mask = htonl(netmask);
150 for (j = 31; j >= 0; j--) {
151 if (calc_mask&(1<<j))
157 /* Calculate depth from netsize */
159 ipt_acc_tables[i].depth = 0;
160 else if (netsize >= 16)
161 ipt_acc_tables[i].depth = 1;
162 else if(netsize >= 8)
163 ipt_acc_tables[i].depth = 2;
165 DEBUGP("ACCOUNT: calculated netsize: %u -> "
166 "ipt_acc_table depth %u\n", netsize,
167 ipt_acc_tables[i].depth);
169 ipt_acc_tables[i].refcount++;
170 if ((ipt_acc_tables[i].data
171 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
172 printk("ACCOUNT: out of memory for data of table: %s\n", name);
173 memset(&ipt_acc_tables[i], 0,
174 sizeof(struct ipt_acc_table));
182 /* No free slot found */
183 printk("ACCOUNT: No free table slot found (max: %d). "
184 "Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
188 static int ipt_acc_checkentry(const char *tablename,
189 const struct ipt_entry *e,
191 unsigned int targinfosize,
192 unsigned int hook_mask)
194 struct ipt_acc_info *info = targinfo;
197 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
198 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
199 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
203 spin_lock_bh(&ipt_acc_lock);
204 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
206 if (table_nr == -1) {
207 printk("ACCOUNT: Table insert problem. Aborting\n");
208 spin_unlock_bh(&ipt_acc_lock);
211 /* Table nr caching so we don't have to do an extra string compare
213 info->table_nr = table_nr;
215 spin_unlock_bh(&ipt_acc_lock);
220 static void ipt_acc_deleteentry(void *targinfo, unsigned int targinfosize)
223 struct ipt_acc_info *info = targinfo;
225 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
226 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
227 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
230 spin_lock_bh(&ipt_acc_lock);
232 DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n",
233 info->table_name, info->table_nr);
235 info->table_nr = -1; /* Set back to original state */
238 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
239 if (strncmp(ipt_acc_tables[i].name, info->table_name,
240 ACCOUNT_TABLE_NAME_LEN) == 0) {
241 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
243 ipt_acc_tables[i].refcount--;
244 DEBUGP("ACCOUNT: Refcount left: %d\n",
245 ipt_acc_tables[i].refcount);
247 /* Table not needed anymore? */
248 if (ipt_acc_tables[i].refcount == 0) {
249 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
250 ipt_acc_data_free(ipt_acc_tables[i].data,
251 ipt_acc_tables[i].depth);
252 memset(&ipt_acc_tables[i], 0,
253 sizeof(struct ipt_acc_table));
256 spin_unlock_bh(&ipt_acc_lock);
261 /* Table not found */
262 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
263 spin_unlock_bh(&ipt_acc_lock);
266 static void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24,
267 u_int32_t net_ip, u_int32_t netmask,
268 u_int32_t src_ip, u_int32_t dst_ip,
269 u_int32_t size, u_int32_t *itemcount)
271 unsigned char is_src = 0, is_dst = 0, src_slot, dst_slot;
272 char is_src_new_ip = 0, is_dst_new_ip = 0; /* Check if this entry is new */
274 DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
275 "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip),
276 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
278 /* Check if src/dst is inside our network. */
279 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
282 if ((net_ip&netmask) == (src_ip&netmask))
284 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
287 if (!is_src && !is_dst) {
288 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
289 "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip),
290 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
294 /* Calculate array positions */
295 src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
296 dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
298 /* Increase size counters */
300 /* Calculate network slot */
301 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
302 if (!mask_24->ip[src_slot].src_packets
303 && !mask_24->ip[src_slot].dst_packets)
306 mask_24->ip[src_slot].src_packets++;
307 mask_24->ip[src_slot].src_bytes+=size;
310 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
311 if (!mask_24->ip[dst_slot].src_packets
312 && !mask_24->ip[dst_slot].dst_packets)
315 mask_24->ip[dst_slot].dst_packets++;
316 mask_24->ip[dst_slot].dst_bytes+=size;
319 /* Increase itemcounter */
320 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
321 if (src_slot == dst_slot) {
322 if (is_src_new_ip || is_dst_new_ip) {
323 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n",
324 is_src_new_ip, is_dst_new_ip);
329 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
333 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
337 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
340 static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
341 u_int32_t net_ip, u_int32_t netmask,
342 u_int32_t src_ip, u_int32_t dst_ip,
343 u_int32_t size, u_int32_t *itemcount)
345 /* Do we need to process src IP? */
346 if ((net_ip&netmask) == (src_ip&netmask)) {
347 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
348 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
350 /* Do we need to create a new mask_24 bucket? */
351 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] =
352 (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
353 printk("ACCOUNT: Can't process packet because out of memory!\n");
357 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
358 net_ip, netmask, src_ip, 0, size, itemcount);
361 /* Do we need to process dst IP? */
362 if ((net_ip&netmask) == (dst_ip&netmask)) {
363 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
364 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
366 /* Do we need to create a new mask_24 bucket? */
367 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot]
368 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
369 printk("ACCOUT: Can't process packet because out of memory!\n");
373 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
374 net_ip, netmask, 0, dst_ip, size, itemcount);
378 static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
379 u_int32_t net_ip, u_int32_t netmask,
380 u_int32_t src_ip, u_int32_t dst_ip,
381 u_int32_t size, u_int32_t *itemcount)
383 /* Do we need to process src IP? */
384 if ((net_ip&netmask) == (src_ip&netmask)) {
385 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
386 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
388 /* Do we need to create a new mask_24 bucket? */
389 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
390 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
391 printk("ACCOUNT: Can't process packet because out of memory!\n");
395 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
396 net_ip, netmask, src_ip, 0, size, itemcount);
399 /* Do we need to process dst IP? */
400 if ((net_ip&netmask) == (dst_ip&netmask)) {
401 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
402 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
404 /* Do we need to create a new mask_24 bucket? */
405 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
406 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
407 printk("ACCOUNT: Can't process packet because out of memory!\n");
411 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
412 net_ip, netmask, 0, dst_ip, size, itemcount);
416 static unsigned int ipt_acc_target(struct sk_buff **pskb,
417 unsigned int hooknum,
418 const struct net_device *in,
419 const struct net_device *out,
420 const void *targinfo,
423 const struct ipt_acc_info *info =
424 (const struct ipt_acc_info *)targinfo;
425 u_int32_t src_ip = (*pskb)->nh.iph->saddr;
426 u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
427 u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
429 spin_lock_bh(&ipt_acc_lock);
431 if (ipt_acc_tables[info->table_nr].name[0] == 0) {
432 printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
433 "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr,
434 NIPQUAD(src_ip), NIPQUAD(dst_ip));
435 spin_unlock_bh(&ipt_acc_lock);
439 /* 8 bit network or "any" network */
440 if (ipt_acc_tables[info->table_nr].depth == 0) {
441 /* Count packet and check if the IP is new */
442 ipt_acc_depth0_insert(
443 (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
444 ipt_acc_tables[info->table_nr].ip,
445 ipt_acc_tables[info->table_nr].netmask,
446 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
447 spin_unlock_bh(&ipt_acc_lock);
452 if (ipt_acc_tables[info->table_nr].depth == 1) {
453 ipt_acc_depth1_insert(
454 (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
455 ipt_acc_tables[info->table_nr].ip,
456 ipt_acc_tables[info->table_nr].netmask,
457 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
458 spin_unlock_bh(&ipt_acc_lock);
463 if (ipt_acc_tables[info->table_nr].depth == 2) {
464 ipt_acc_depth2_insert(
465 (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
466 ipt_acc_tables[info->table_nr].ip,
467 ipt_acc_tables[info->table_nr].netmask,
468 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
469 spin_unlock_bh(&ipt_acc_lock);
473 printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
474 "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
475 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
477 spin_unlock_bh(&ipt_acc_lock);
482 Functions dealing with "handles":
483 Handles are snapshots of a accounting state.
485 read snapshots are only for debugging the code
486 and are very expensive concerning speed/memory
487 compared to read_and_flush.
489 The functions aren't protected by spinlocks themselves
490 as this is done in the ioctl part of the code.
494 Find a free handle slot. Normally only one should be used,
495 but there could be two or more applications accessing the data
498 static int ipt_acc_handle_find_slot(void)
501 /* Insert new table */
502 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
503 /* Found free slot */
504 if (ipt_acc_handles[i].data == NULL) {
505 /* Don't "mark" data as used as we are protected by a spinlock
506 by the calling function. handle_find_slot() is only a function
507 to prevent code duplication. */
512 /* No free slot found */
513 printk("ACCOUNT: No free handle slot found (max: %u). "
514 "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
518 static int ipt_acc_handle_free(u_int32_t handle)
520 if (handle >= ACCOUNT_MAX_HANDLES) {
521 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
526 ipt_acc_data_free(ipt_acc_handles[handle].data,
527 ipt_acc_handles[handle].depth);
528 memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
532 /* Prepare data for read without flush. Use only for debugging!
533 Real applications should use read&flush as it's way more efficent */
534 static int ipt_acc_handle_prepare_read(char *tablename, u_int32_t *count)
536 int handle, i, table_nr=-1;
539 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
540 if (strncmp(ipt_acc_tables[i].name, tablename,
541 ACCOUNT_TABLE_NAME_LEN) == 0) {
546 if (table_nr == -1) {
547 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
548 "Table %s not found\n", tablename);
552 /* Can't find a free handle slot? */
553 if ((handle = ipt_acc_handle_find_slot()) == -1)
556 /* Fill up handle structure */
557 ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
558 ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
559 ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
561 /* allocate "root" table */
562 if ((ipt_acc_handles[handle].data =
563 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
564 printk("ACCOUNT: out of memory for root table "
565 "in ipt_acc_handle_prepare_read()\n");
566 memset (&ipt_acc_handles[handle], 0,
567 sizeof(struct ipt_acc_handle));
571 /* Recursive copy of complete data structure */
572 depth = ipt_acc_handles[handle].depth;
574 memcpy(ipt_acc_handles[handle].data,
575 ipt_acc_tables[table_nr].data,
576 sizeof(struct ipt_acc_mask_24));
577 } else if (depth == 1) {
578 struct ipt_acc_mask_16 *src_16 =
579 (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data;
580 struct ipt_acc_mask_16 *network_16 =
581 (struct ipt_acc_mask_16 *)ipt_acc_handles[handle].data;
584 for (b = 0; b <= 255; b++) {
585 if (src_16->mask_24[b]) {
586 if ((network_16->mask_24[b] =
587 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
588 printk("ACCOUNT: out of memory during copy of 16 bit "
589 "network in ipt_acc_handle_prepare_read()\n");
590 ipt_acc_data_free(ipt_acc_handles[handle].data, depth);
591 memset (&ipt_acc_handles[handle], 0,
592 sizeof(struct ipt_acc_handle));
596 memcpy(network_16->mask_24[b], src_16->mask_24[b],
597 sizeof(struct ipt_acc_mask_24));
600 } else if(depth == 2) {
601 struct ipt_acc_mask_8 *src_8 =
602 (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data;
603 struct ipt_acc_mask_8 *network_8 =
604 (struct ipt_acc_mask_8 *)ipt_acc_handles[handle].data;
607 for (a = 0; a <= 255; a++) {
608 if (src_8->mask_16[a]) {
609 if ((network_8->mask_16[a] =
610 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
611 printk("ACCOUNT: out of memory during copy of 24 bit network"
612 " in ipt_acc_handle_prepare_read()\n");
613 ipt_acc_data_free(ipt_acc_handles[handle].data, depth);
614 memset (&ipt_acc_handles[handle], 0,
615 sizeof(struct ipt_acc_handle));
619 memcpy(network_8->mask_16[a], src_8->mask_16[a],
620 sizeof(struct ipt_acc_mask_16));
622 struct ipt_acc_mask_16 *src_16 = src_8->mask_16[a];
623 struct ipt_acc_mask_16 *network_16 = network_8->mask_16[a];
626 for (b = 0; b <= 255; b++) {
627 if (src_16->mask_24[b]) {
628 if ((network_16->mask_24[b] =
629 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
630 printk("ACCOUNT: out of memory during copy of 16 bit"
631 " network in ipt_acc_handle_prepare_read()\n");
632 ipt_acc_data_free(ipt_acc_handles[handle].data,
634 memset (&ipt_acc_handles[handle], 0,
635 sizeof(struct ipt_acc_handle));
639 memcpy(network_16->mask_24[b], src_16->mask_24[b],
640 sizeof(struct ipt_acc_mask_24));
647 *count = ipt_acc_tables[table_nr].itemcount;
651 /* Prepare data for read and flush it */
652 static int ipt_acc_handle_prepare_read_flush(char *tablename, u_int32_t *count)
654 int handle, i, table_nr=-1;
657 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
658 if (strncmp(ipt_acc_tables[i].name, tablename,
659 ACCOUNT_TABLE_NAME_LEN) == 0) {
664 if (table_nr == -1) {
665 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
666 "Table %s not found\n", tablename);
670 /* Can't find a free handle slot? */
671 if ((handle = ipt_acc_handle_find_slot()) == -1)
674 /* Try to allocate memory */
675 if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
676 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
681 /* Fill up handle structure */
682 ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
683 ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
684 ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
685 ipt_acc_handles[handle].data = ipt_acc_tables[table_nr].data;
686 *count = ipt_acc_tables[table_nr].itemcount;
688 /* "Flush" table data */
689 ipt_acc_tables[table_nr].data = new_data_page;
690 ipt_acc_tables[table_nr].itemcount = 0;
695 /* Copy 8 bit network data into a prepared buffer.
696 We only copy entries != 0 to increase performance.
698 static int ipt_acc_handle_copy_data(void *to_user, int *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 (*pos+handle_ip_size >= PAGE_SIZE) {
717 if (copy_to_user(to_user, ipt_acc_tmpbuf, *pos))
721 memcpy(ipt_acc_tmpbuf+*pos, &handle_ip, handle_ip_size);
722 *pos += handle_ip_size;
729 /* Copy the data from our internal structure
730 We only copy entries != 0 to increase performance.
731 Overwrites ipt_acc_tmpbuf.
733 static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
735 u_int32_t tmpbuf_pos=0, net_ip;
738 if (handle >= ACCOUNT_MAX_HANDLES) {
739 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
740 "specified: %u\n", handle);
744 if (ipt_acc_handles[handle].data == NULL) {
745 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
749 net_ip = ipt_acc_handles[handle].ip;
750 depth = ipt_acc_handles[handle].depth;
754 struct ipt_acc_mask_24 *network =
755 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
756 if (ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, 0))
759 /* Flush remaining data to userspace */
761 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
769 struct ipt_acc_mask_16 *network_16 =
770 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
772 for (b = 0; b <= 255; b++) {
773 if (network_16->mask_24[b]) {
774 struct ipt_acc_mask_24 *network =
775 (struct ipt_acc_mask_24*)network_16->mask_24[b];
776 if (ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network,
782 /* Flush remaining data to userspace */
784 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
792 struct ipt_acc_mask_8 *network_8 =
793 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
795 for (a = 0; a <= 255; a++) {
796 if (network_8->mask_16[a]) {
797 struct ipt_acc_mask_16 *network_16 =
798 (struct ipt_acc_mask_16*)network_8->mask_16[a];
799 for (b = 0; b <= 255; b++) {
800 if (network_16->mask_24[b]) {
801 struct ipt_acc_mask_24 *network =
802 (struct ipt_acc_mask_24*)network_16->mask_24[b];
803 if (ipt_acc_handle_copy_data(to_user, &tmpbuf_pos,
804 network, net_ip, (a << 8) | (b << 16)))
811 /* Flush remaining data to userspace */
813 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
822 static int ipt_acc_set_ctl(struct sock *sk, int cmd,
823 void *user, u_int32_t len)
825 struct ipt_acc_handle_sockopt handle;
828 if (!capable(CAP_NET_ADMIN))
832 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
833 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
834 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
835 "for IPT_SO_SET_HANDLE_FREE\n",
836 len, sizeof(struct ipt_acc_handle_sockopt));
840 if (copy_from_user (&handle, user, len)) {
841 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
842 "IPT_SO_SET_HANDLE_FREE\n");
846 spin_lock_bh(&ipt_acc_userspace_lock);
847 ret = ipt_acc_handle_free(handle.handle_nr);
848 spin_unlock_bh(&ipt_acc_userspace_lock);
850 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
852 spin_lock_bh(&ipt_acc_userspace_lock);
853 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
854 ipt_acc_handle_free(i);
855 spin_unlock_bh(&ipt_acc_userspace_lock);
860 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
866 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
868 struct ipt_acc_handle_sockopt handle;
871 if (!capable(CAP_NET_ADMIN))
875 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
876 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
877 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
878 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
879 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
880 *len, sizeof(struct ipt_acc_handle_sockopt));
884 if (copy_from_user (&handle, user,
885 sizeof(struct ipt_acc_handle_sockopt))) {
890 spin_lock_bh(&ipt_acc_lock);
891 spin_lock_bh(&ipt_acc_userspace_lock);
892 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
893 handle.handle_nr = ipt_acc_handle_prepare_read_flush(
894 handle.name, &handle.itemcount);
896 handle.handle_nr = ipt_acc_handle_prepare_read(
897 handle.name, &handle.itemcount);
898 spin_unlock_bh(&ipt_acc_userspace_lock);
899 spin_unlock_bh(&ipt_acc_lock);
901 if (handle.handle_nr == -1) {
906 if (copy_to_user(user, &handle,
907 sizeof(struct ipt_acc_handle_sockopt))) {
913 case IPT_SO_GET_ACCOUNT_GET_DATA:
914 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
915 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
916 " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
917 *len, sizeof(struct ipt_acc_handle_sockopt));
921 if (copy_from_user (&handle, user,
922 sizeof(struct ipt_acc_handle_sockopt))) {
927 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
932 if (*len < ipt_acc_handles[handle.handle_nr].itemcount
933 * sizeof(struct ipt_acc_handle_ip)) {
934 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
935 " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
936 *len, ipt_acc_handles[handle.handle_nr].itemcount
937 * sizeof(struct ipt_acc_handle_ip));
942 spin_lock_bh(&ipt_acc_userspace_lock);
943 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
944 spin_unlock_bh(&ipt_acc_userspace_lock);
946 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
947 " failed for handle %u\n", handle.handle_nr);
953 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
955 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
956 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
957 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
958 *len, sizeof(struct ipt_acc_handle_sockopt));
962 /* Find out how many handles are in use */
963 handle.itemcount = 0;
964 spin_lock_bh(&ipt_acc_userspace_lock);
965 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
966 if (ipt_acc_handles[i].data)
968 spin_unlock_bh(&ipt_acc_userspace_lock);
970 if (copy_to_user(user, &handle,
971 sizeof(struct ipt_acc_handle_sockopt))) {
978 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
979 u_int32_t size = 0, i;
982 spin_lock_bh(&ipt_acc_lock);
984 /* Determine size of table names */
985 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
986 if (ipt_acc_tables[i].name[0] != 0)
987 size += strlen (ipt_acc_tables[i].name) + 1;
989 size += 1; /* Terminating NULL character */
992 spin_unlock_bh(&ipt_acc_lock);
993 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
994 " to store table names\n", *len, size);
998 /* Copy table names to userspace */
1000 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
1001 if (ipt_acc_tables[i].name[0] != 0) {
1002 int len = strlen (ipt_acc_tables[i].name) + 1;
1003 /* copy string + terminating zero */
1004 if (copy_to_user(tnames, ipt_acc_tables[i].name, len)) {
1005 spin_unlock_bh(&ipt_acc_lock);
1011 /* Append terminating zero */
1013 ret = copy_to_user(tnames, &i, 1);
1014 spin_unlock_bh(&ipt_acc_lock);
1021 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1027 static struct ipt_target ipt_acc_reg = {
1029 .target = ipt_acc_target,
1030 .checkentry = ipt_acc_checkentry,
1031 .destroy = ipt_acc_deleteentry,
1035 static struct nf_sockopt_ops ipt_acc_sockopts = {
1037 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1038 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1039 .set = ipt_acc_set_ctl,
1040 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1041 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1042 .get = ipt_acc_get_ctl
1045 static int __init init(void)
1047 if ((ipt_acc_tables =
1048 kmalloc(ACCOUNT_MAX_TABLES *
1049 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1050 printk("ACCOUNT: Out of memory allocating account_tables structure");
1053 memset(ipt_acc_tables, 0,
1054 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1056 if ((ipt_acc_handles =
1057 kmalloc(ACCOUNT_MAX_HANDLES *
1058 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1059 printk("ACCOUNT: Out of memory allocating account_handles structure");
1062 memset(ipt_acc_handles, 0,
1063 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1065 /* Allocate one page as temporary storage */
1066 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1067 printk("ACCOUNT: Out of memory for temporary buffer page\n");
1071 /* Register setsockopt */
1072 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1073 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1077 if (ipt_register_target(&ipt_acc_reg))
1084 kfree(ipt_acc_tables);
1086 kfree(ipt_acc_handles);
1088 free_page((unsigned long)ipt_acc_tmpbuf);
1093 static void __exit fini(void)
1095 ipt_unregister_target(&ipt_acc_reg);
1097 nf_unregister_sockopt(&ipt_acc_sockopts);
1099 kfree(ipt_acc_tables);
1100 kfree(ipt_acc_handles);
1101 free_page((unsigned long)ipt_acc_tmpbuf);
1106 MODULE_LICENSE("GPL");