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 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 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 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 u_int32_t j, calc_mask, netsize=0;
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;
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 int 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 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 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;
272 DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
273 "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip),
274 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
276 /* Check if src/dst is inside our network. */
277 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
280 if ((net_ip&netmask) == (src_ip&netmask))
282 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
285 if (!is_src && !is_dst) {
286 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
287 "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip),
288 NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
292 /* Check if this entry is new */
293 char is_src_new_ip = 0, is_dst_new_ip = 0;
295 /* Calculate array positions */
296 unsigned char src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
297 unsigned char dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
299 /* Increase size counters */
301 /* Calculate network slot */
302 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
303 if (!mask_24->ip[src_slot].src_packets
304 && !mask_24->ip[src_slot].dst_packets)
307 mask_24->ip[src_slot].src_packets++;
308 mask_24->ip[src_slot].src_bytes+=size;
311 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
312 if (!mask_24->ip[dst_slot].src_packets
313 && !mask_24->ip[dst_slot].dst_packets)
316 mask_24->ip[dst_slot].dst_packets++;
317 mask_24->ip[dst_slot].dst_bytes+=size;
320 /* Increase itemcounter */
321 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
322 if (src_slot == dst_slot) {
323 if (is_src_new_ip || is_dst_new_ip) {
324 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n",
325 is_src_new_ip, is_dst_new_ip);
330 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
334 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
338 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
341 void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
342 u_int32_t net_ip, u_int32_t netmask,
343 u_int32_t src_ip, u_int32_t dst_ip,
344 u_int32_t size, u_int32_t *itemcount)
346 /* Do we need to process src IP? */
347 if ((net_ip&netmask) == (src_ip&netmask)) {
348 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
349 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
351 /* Do we need to create a new mask_24 bucket? */
352 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] =
353 (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
354 printk("ACCOUNT: Can't process packet because out of memory!\n");
358 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
359 net_ip, netmask, src_ip, 0, size, itemcount);
362 /* Do we need to process dst IP? */
363 if ((net_ip&netmask) == (dst_ip&netmask)) {
364 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
365 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
367 /* Do we need to create a new mask_24 bucket? */
368 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot]
369 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
370 printk("ACCOUT: Can't process packet because out of memory!\n");
374 ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
375 net_ip, netmask, 0, dst_ip, size, itemcount);
379 void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
380 u_int32_t net_ip, u_int32_t netmask,
381 u_int32_t src_ip, u_int32_t dst_ip,
382 u_int32_t size, u_int32_t *itemcount)
384 /* Do we need to process src IP? */
385 if ((net_ip&netmask) == (src_ip&netmask)) {
386 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
387 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
389 /* Do we need to create a new mask_24 bucket? */
390 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
391 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
392 printk("ACCOUNT: Can't process packet because out of memory!\n");
396 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
397 net_ip, netmask, src_ip, 0, size, itemcount);
400 /* Do we need to process dst IP? */
401 if ((net_ip&netmask) == (dst_ip&netmask)) {
402 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
403 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
405 /* Do we need to create a new mask_24 bucket? */
406 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot]
407 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
408 printk("ACCOUNT: Can't process packet because out of memory!\n");
412 ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
413 net_ip, netmask, 0, dst_ip, size, itemcount);
417 static unsigned int ipt_acc_target(struct sk_buff **pskb,
418 unsigned int hooknum,
419 const struct net_device *in,
420 const struct net_device *out,
421 const void *targinfo,
424 const struct ipt_acc_info *info =
425 (const struct ipt_acc_info *)targinfo;
426 u_int32_t src_ip = (*pskb)->nh.iph->saddr;
427 u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
428 u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
430 spin_lock_bh(&ipt_acc_lock);
432 if (ipt_acc_tables[info->table_nr].name[0] == 0) {
433 printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
434 "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr,
435 NIPQUAD(src_ip), NIPQUAD(dst_ip));
436 spin_unlock_bh(&ipt_acc_lock);
440 /* 8 bit network or "any" network */
441 if (ipt_acc_tables[info->table_nr].depth == 0) {
442 /* Count packet and check if the IP is new */
443 ipt_acc_depth0_insert(
444 (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
445 ipt_acc_tables[info->table_nr].ip,
446 ipt_acc_tables[info->table_nr].netmask,
447 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
448 spin_unlock_bh(&ipt_acc_lock);
453 if (ipt_acc_tables[info->table_nr].depth == 1) {
454 ipt_acc_depth1_insert(
455 (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
456 ipt_acc_tables[info->table_nr].ip,
457 ipt_acc_tables[info->table_nr].netmask,
458 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
459 spin_unlock_bh(&ipt_acc_lock);
464 if (ipt_acc_tables[info->table_nr].depth == 2) {
465 ipt_acc_depth2_insert(
466 (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
467 ipt_acc_tables[info->table_nr].ip,
468 ipt_acc_tables[info->table_nr].netmask,
469 src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
470 spin_unlock_bh(&ipt_acc_lock);
474 printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
475 "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
476 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
478 spin_unlock_bh(&ipt_acc_lock);
483 Functions dealing with "handles":
484 Handles are snapshots of a accounting state.
486 read snapshots are only for debugging the code
487 and are very expensive concerning speed/memory
488 compared to read_and_flush.
490 The functions aren't protected by spinlocks themselves
491 as this is done in the ioctl part of the code.
495 Find a free handle slot. Normally only one should be used,
496 but there could be two or more applications accessing the data
499 int ipt_acc_handle_find_slot(void)
502 /* Insert new table */
503 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
504 /* Found free slot */
505 if (ipt_acc_handles[i].data == NULL) {
506 /* Don't "mark" data as used as we are protected by a spinlock
507 by the calling function. handle_find_slot() is only a function
508 to prevent code duplication. */
513 /* No free slot found */
514 printk("ACCOUNT: No free handle slot found (max: %u). "
515 "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
519 int ipt_acc_handle_free(u_int32_t handle)
521 if (handle >= ACCOUNT_MAX_HANDLES) {
522 printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
527 ipt_acc_data_free(ipt_acc_handles[handle].data,
528 ipt_acc_handles[handle].depth);
529 memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
533 /* Prepare data for read without flush. Use only for debugging!
534 Real applications should use read&flush as it's way more efficent */
535 int ipt_acc_handle_prepare_read(char *tablename, u_int32_t *count)
537 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 u_int32_t 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 int ipt_acc_handle_prepare_read_flush(char *tablename, u_int32_t *count)
654 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 void *new_data_page = (void*)get_zeroed_page(GFP_ATOMIC);
677 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
682 /* Fill up handle structure */
683 ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
684 ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
685 ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
686 ipt_acc_handles[handle].data = ipt_acc_tables[table_nr].data;
687 *count = ipt_acc_tables[table_nr].itemcount;
689 /* "Flush" table data */
690 ipt_acc_tables[table_nr].data = new_data_page;
691 ipt_acc_tables[table_nr].itemcount = 0;
696 /* Copy 8 bit network data into a prepared buffer.
697 We only copy entries != 0 to increase performance.
699 void ipt_acc_handle_copy_data(void *to_user, int *pos,
700 struct ipt_acc_mask_24 *data,
701 u_int32_t net_ip, u_int32_t net_OR_mask)
703 struct ipt_acc_handle_ip handle_ip;
704 u_int32_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
708 for (i = 0; i <= 255; i++) {
709 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
710 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
712 handle_ip.src_packets = data->ip[i].src_packets;
713 handle_ip.src_bytes = data->ip[i].src_bytes;
714 handle_ip.dst_packets = data->ip[i].dst_packets;
715 handle_ip.dst_bytes = data->ip[i].dst_bytes;
717 /* Temporary buffer full? Flush to userspace */
718 if (*pos+handle_ip_size >= PAGE_SIZE) {
720 if (copy_to_user(to_user, ipt_acc_tmpbuf, *pos))
723 memcpy(ipt_acc_tmpbuf+*pos, &handle_ip, handle_ip_size);
724 *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 int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
735 u_int32_t tmpbuf_pos=0;
737 if (handle >= ACCOUNT_MAX_HANDLES) {
738 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
739 "specified: %u\n", handle);
743 if (ipt_acc_handles[handle].data == NULL) {
744 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
748 u_int32_t net_ip = ipt_acc_handles[handle].ip;
749 u_int32_t depth = ipt_acc_handles[handle].depth;
753 struct ipt_acc_mask_24 *network =
754 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
755 ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, 0);
757 /* Flush remaining data to userspace */
759 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
767 struct ipt_acc_mask_16 *network_16 =
768 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
770 for (b = 0; b <= 255; b++) {
771 if (network_16->mask_24[b]) {
772 struct ipt_acc_mask_24 *network =
773 (struct ipt_acc_mask_24*)network_16->mask_24[b];
774 ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network,
779 /* Flush remaining data to userspace */
781 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
789 struct ipt_acc_mask_8 *network_8 =
790 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
792 for (a = 0; a <= 255; a++) {
793 if (network_8->mask_16[a]) {
794 struct ipt_acc_mask_16 *network_16 =
795 (struct ipt_acc_mask_16*)network_8->mask_16[a];
796 for (b = 0; b <= 255; b++) {
797 if (network_16->mask_24[b]) {
798 struct ipt_acc_mask_24 *network =
799 (struct ipt_acc_mask_24*)network_16->mask_24[b];
800 ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network,
801 net_ip, (a << 8) | (b << 16));
807 /* Flush remaining data to userspace */
809 if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
818 static int ipt_acc_set_ctl(struct sock *sk, int cmd,
819 void *user, u_int32_t len)
821 struct ipt_acc_handle_sockopt handle;
824 if (!capable(CAP_NET_ADMIN))
828 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
829 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
830 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
831 "for IPT_SO_SET_HANDLE_FREE\n",
832 len, sizeof(struct ipt_acc_handle_sockopt));
836 if (copy_from_user (&handle, user, len)) {
837 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
838 "IPT_SO_SET_HANDLE_FREE\n");
842 spin_lock_bh(&ipt_acc_userspace_lock);
843 ret = ipt_acc_handle_free(handle.handle_nr);
844 spin_unlock_bh(&ipt_acc_userspace_lock);
846 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
848 spin_lock_bh(&ipt_acc_userspace_lock);
849 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
850 ipt_acc_handle_free(i);
851 spin_unlock_bh(&ipt_acc_userspace_lock);
856 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
862 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
864 struct ipt_acc_handle_sockopt handle;
867 if (!capable(CAP_NET_ADMIN))
871 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
872 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
873 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
874 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
875 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
876 *len, sizeof(struct ipt_acc_handle_sockopt));
880 if (copy_from_user (&handle, user,
881 sizeof(struct ipt_acc_handle_sockopt))) {
886 spin_lock_bh(&ipt_acc_lock);
887 spin_lock_bh(&ipt_acc_userspace_lock);
888 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
889 handle.handle_nr = ipt_acc_handle_prepare_read_flush(
890 handle.name, &handle.itemcount);
892 handle.handle_nr = ipt_acc_handle_prepare_read(
893 handle.name, &handle.itemcount);
894 spin_unlock_bh(&ipt_acc_userspace_lock);
895 spin_unlock_bh(&ipt_acc_lock);
897 if (handle.handle_nr == -1) {
902 if (copy_to_user(user, &handle,
903 sizeof(struct ipt_acc_handle_sockopt))) {
909 case IPT_SO_GET_ACCOUNT_GET_DATA:
910 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
911 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
912 " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
913 *len, sizeof(struct ipt_acc_handle_sockopt));
917 if (copy_from_user (&handle, user,
918 sizeof(struct ipt_acc_handle_sockopt))) {
923 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
928 if (*len < ipt_acc_handles[handle.handle_nr].itemcount
929 * sizeof(struct ipt_acc_handle_ip)) {
930 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
931 " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
932 *len, ipt_acc_handles[handle.handle_nr].itemcount
933 * sizeof(struct ipt_acc_handle_ip));
938 spin_lock_bh(&ipt_acc_userspace_lock);
939 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
940 spin_unlock_bh(&ipt_acc_userspace_lock);
942 printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
943 " failed for handle %u\n", handle.handle_nr);
949 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
950 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
951 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
952 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
953 *len, sizeof(struct ipt_acc_handle_sockopt));
957 /* Find out how many handles are in use */
959 handle.itemcount = 0;
960 spin_lock_bh(&ipt_acc_userspace_lock);
961 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
962 if (ipt_acc_handles[i].data)
964 spin_unlock_bh(&ipt_acc_userspace_lock);
966 if (copy_to_user(user, &handle,
967 sizeof(struct ipt_acc_handle_sockopt))) {
974 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
975 spin_lock_bh(&ipt_acc_lock);
977 /* Determine size of table names */
978 u_int32_t size = 0, i;
979 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
980 if (ipt_acc_tables[i].name[0] != 0)
981 size += strlen (ipt_acc_tables[i].name) + 1;
983 size += 1; /* Terminating NULL character */
986 spin_unlock_bh(&ipt_acc_lock);
987 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
988 " to store table names\n", *len, size);
992 /* Copy table names to userspace */
994 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
995 if (ipt_acc_tables[i].name[0] != 0) {
996 int len = strlen (ipt_acc_tables[i].name) + 1;
997 /* copy string + terminating zero */
998 if (copy_to_user(tnames, ipt_acc_tables[i].name, len)) {
999 spin_unlock_bh(&ipt_acc_lock);
1005 /* Append terminating zero */
1007 ret = copy_to_user(tnames, &i, 1);
1008 spin_unlock_bh(&ipt_acc_lock);
1015 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1021 static struct ipt_target ipt_acc_reg = {
1023 .target = ipt_acc_target,
1024 .checkentry = ipt_acc_checkentry,
1025 .destroy = ipt_acc_deleteentry,
1029 static struct nf_sockopt_ops ipt_acc_sockopts = {
1031 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1032 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1033 .set = ipt_acc_set_ctl,
1034 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1035 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1036 .get = ipt_acc_get_ctl
1039 static int __init init(void)
1041 if ((ipt_acc_tables =
1042 kmalloc(ACCOUNT_MAX_TABLES *
1043 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1044 printk("ACCOUNT: Out of memory allocating account_tables structure");
1047 memset(ipt_acc_tables, 0,
1048 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1050 if ((ipt_acc_handles =
1051 kmalloc(ACCOUNT_MAX_HANDLES *
1052 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1053 printk("ACCOUNT: Out of memory allocating account_handles structure");
1056 memset(ipt_acc_handles, 0,
1057 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1059 /* Allocate one page as temporary storage */
1060 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1061 printk("ACCOUNT: Out of memory for temporary buffer page\n");
1065 /* Register setsockopt */
1066 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1067 printk("ACCOUNT: Can't register sockopts. Aborting\n");
1071 if (ipt_register_target(&ipt_acc_reg))
1078 kfree(ipt_acc_tables);
1080 kfree(ipt_acc_handles);
1082 free_page((unsigned long)ipt_acc_tmpbuf);
1087 static void __exit fini(void)
1089 ipt_unregister_target(&ipt_acc_reg);
1091 nf_unregister_sockopt(&ipt_acc_sockopts);
1093 kfree(ipt_acc_tables);
1094 kfree(ipt_acc_handles);
1095 free_page((unsigned long)ipt_acc_tmpbuf);
1100 MODULE_LICENSE("GPL");