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>
25 #include <net/route.h>
26 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
31 #define DEBUGP(format, args...)
34 #if (PAGE_SIZE < 4096)
35 #error "ipt_ACCOUNT needs at least a PAGE_SIZE of 4096"
38 struct ipt_account_table *ipt_account_tables = NULL;
39 struct ipt_account_handle *ipt_account_handles = NULL;
40 void *ipt_account_tmpbuf = NULL;
42 /* Spinlock used for manipulating the current accounting tables/data */
43 static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED;
44 /* Spinlock used for manipulating userspace handles/snapshot data */
45 static spinlock_t ipt_account_userspace_lock = SPIN_LOCK_UNLOCKED;
48 /* Recursive free of all data structures */
49 void ipt_account_data_free(void *data, unsigned char depth)
55 /* Free for 8 bit network */
57 free_page((unsigned long)data);
62 /* Free for 16 bit network */
64 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data;
66 for (b=0; b <= 255; b++) {
67 if (mask_16->mask_24[b] != 0) {
68 free_page((unsigned long)mask_16->mask_24[b]);
69 mask_16->mask_24[b] = NULL;
72 free_page((unsigned long)data);
77 /* Free for 24 bit network */
80 for (a=0; a <= 255; a++) {
81 if (((struct ipt_account_mask_8 *)data)->mask_16[a]) {
82 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16*)
83 ((struct ipt_account_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]);
88 mask_16->mask_24[b] = NULL;
91 free_page((unsigned long)mask_16);
95 free_page((unsigned long)data);
100 printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth);
104 /* Look for existing table / insert new one. Return internal ID or -1 on error */
105 int ipt_account_table_insert(char *name, unsigned int ip, unsigned int netmask)
109 DEBUGP("ACCOUNT: ipt_account_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_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN) == 0) {
115 DEBUGP("ACCOUNT: Found existing slot: %d - %u.%u.%u.%u/%u.%u.%u.%u\n", i,
116 NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
118 if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask) {
119 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
120 name, NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
124 ipt_account_tables[i].refcount++;
125 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount);
130 /* Insert new table */
131 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
132 /* Found free slot */
133 if (ipt_account_tables[i].name[0] == 0) {
134 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
136 strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
138 ipt_account_tables[i].ip = ip;
139 ipt_account_tables[i].netmask = netmask;
141 /* Calculate netsize */
142 unsigned int j, calc_mask, netsize=0;
143 calc_mask = htonl(netmask);
144 for (j = 31; j > 0; j--) {
145 if (calc_mask&(1<<j))
151 /* Calculate depth from netsize */
153 ipt_account_tables[i].depth = 0;
154 else if (netsize >= 16)
155 ipt_account_tables[i].depth = 1;
156 else if(netsize >= 8)
157 ipt_account_tables[i].depth = 2;
159 DEBUGP("ACCOUNT: calculated netsize: %u -> ipt_account_table depth %u\n",
160 netsize, ipt_account_tables[i].depth);
162 ipt_account_tables[i].refcount++;
163 if ((ipt_account_tables[i].data = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
164 printk("ACCOUNT: out of memory for data of table: %s\n", name);
165 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
173 /* No free slot found */
174 printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
178 static int ipt_account_checkentry(const char *tablename,
179 const struct ipt_entry *e,
181 unsigned int targinfosize,
182 unsigned int hook_mask)
184 struct ipt_account_info *info = targinfo;
186 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
187 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
188 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
192 spin_lock_bh(&ipt_account_lock);
193 int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask);
194 if (table_nr == -1) {
195 printk("ACCOUNT: Table insert problem. Aborting\n");
196 spin_unlock_bh(&ipt_account_lock);
199 /* Table nr caching so we don't have to do an extra string compare for every packet */
200 info->table_nr = table_nr;
202 spin_unlock_bh(&ipt_account_lock);
207 void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize)
210 struct ipt_account_info *info = targinfo;
212 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
213 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
214 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
217 spin_lock_bh(&ipt_account_lock);
219 DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr);
221 info->table_nr = -1; /* Set back to original state */
224 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
225 if (strncmp(ipt_account_tables[i].name, info->table_name, ACCOUNT_TABLE_NAME_LEN) == 0) {
226 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
228 ipt_account_tables[i].refcount--;
229 DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount);
231 /* Table not needed anymore? */
232 if (ipt_account_tables[i].refcount == 0) {
233 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
234 ipt_account_data_free(ipt_account_tables[i].data, ipt_account_tables[i].depth);
235 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
238 spin_unlock_bh(&ipt_account_lock);
243 /* Table not found */
244 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
245 spin_unlock_bh(&ipt_account_lock);
248 void ipt_account_depth0_insert(struct ipt_account_mask_24 *mask_24,
249 unsigned int net_ip, unsigned int netmask,
250 unsigned int src_ip, unsigned int dst_ip,
251 unsigned int size, unsigned int *itemcount)
253 unsigned char is_src = 0, is_dst = 0;
255 DEBUGP("ACCOUNT: ipt_account_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n",
256 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
258 /* Check if src/dst is inside our network. */
259 /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
262 if ((net_ip&netmask) == (src_ip&netmask))
264 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
267 if (!is_src && !is_dst) {
268 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u\n",
269 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
273 /* Check if this entry is new */
274 char is_src_new_ip = 0, is_dst_new_ip = 0;
276 /* Calculate array positions */
277 unsigned char src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
278 unsigned char dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
280 /* Increase size counters */
282 /* Calculate network slot */
283 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
284 if (!mask_24->ip[src_slot].src_packets && !mask_24->ip[src_slot].dst_packets)
287 mask_24->ip[src_slot].src_packets++;
288 mask_24->ip[src_slot].src_bytes+=size;
291 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
292 if (!mask_24->ip[dst_slot].src_packets && !mask_24->ip[dst_slot].dst_packets)
295 mask_24->ip[dst_slot].dst_packets++;
296 mask_24->ip[dst_slot].dst_bytes+=size;
299 /* Increase itemcounter */
300 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
301 if (src_slot == dst_slot) {
302 if (is_src_new_ip || is_dst_new_ip) {
303 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n", is_src_new_ip, is_dst_new_ip);
308 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
312 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
316 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
319 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip,
320 unsigned int netmask, unsigned int src_ip, unsigned int dst_ip,
321 unsigned int size, unsigned int *itemcount)
323 /* Do we need to process src IP? */
324 if ((net_ip&netmask) == (src_ip&netmask)) {
325 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
326 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
328 /* Do we need to create a new mask_24 bucket? */
329 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
330 printk("ACCOUNT: Can't process packet because out of memory!\n");
334 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
335 src_ip, 0, size, itemcount);
338 /* Do we need to process dst IP? */
339 if ((net_ip&netmask) == (dst_ip&netmask)) {
340 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
341 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
343 /* Do we need to create a new mask_24 bucket? */
344 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
345 printk("ACCOUT: Can't process packet because out of memory!\n");
349 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
350 0, dst_ip, size, itemcount);
354 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip,
355 unsigned int netmask, unsigned int src_ip, unsigned int dst_ip,
356 unsigned int size, unsigned int *itemcount)
358 /* Do we need to process src IP? */
359 if ((net_ip&netmask) == (src_ip&netmask)) {
360 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
361 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
363 /* Do we need to create a new mask_24 bucket? */
364 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
365 printk("ACCOUNT: Can't process packet because out of memory!\n");
369 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
370 src_ip, 0, size, itemcount);
373 /* Do we need to process dst IP? */
374 if ((net_ip&netmask) == (dst_ip&netmask)) {
375 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
376 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
378 /* Do we need to create a new mask_24 bucket? */
379 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
380 printk("ACCOUNT: Can't process packet because out of memory!\n");
384 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
385 0, dst_ip, size, itemcount);
389 static unsigned int ipt_account_target(struct sk_buff **pskb,
390 unsigned int hooknum,
391 const struct net_device *in,
392 const struct net_device *out,
393 const void *targinfo,
396 const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
397 unsigned int src_ip = (*pskb)->nh.iph->saddr;
398 unsigned int dst_ip = (*pskb)->nh.iph->daddr;
399 unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
401 spin_lock_bh(&ipt_account_lock);
403 if (ipt_account_tables[info->table_nr].name[0] == 0) {
404 printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
405 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
406 spin_unlock_bh(&ipt_account_lock);
410 /* 8 bit network or "any" network */
411 if (ipt_account_tables[info->table_nr].depth == 0) {
412 /* Count packet and check if the IP is new */
413 ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
414 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
415 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
416 spin_unlock_bh(&ipt_account_lock);
421 if (ipt_account_tables[info->table_nr].depth == 1) {
422 ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
423 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
424 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
425 spin_unlock_bh(&ipt_account_lock);
430 if (ipt_account_tables[info->table_nr].depth == 2) {
431 ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
432 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
433 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
434 spin_unlock_bh(&ipt_account_lock);
438 printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
439 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
441 spin_unlock_bh(&ipt_account_lock);
446 Functions dealing with "handles":
447 Handles are snapshots of a accounting state.
449 read snapshots are only for debugging the code
450 and are very expensive concerning speed/memory
451 compared to read_and_flush.
453 The functions aren't protected by spinlocks themselves
454 as this is done in the ioctl part of the code.
458 Find a free handle slot. Normally only one should be used,
459 but there could be two or more applications accessing the data
462 int ipt_account_handle_find_slot(void)
465 /* Insert new table */
466 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
467 /* Found free slot */
468 if (ipt_account_handles[i].data == NULL) {
469 /* Don't "mark" data as used as we are protected by a spinlock by the calling function. */
470 /* handle_find_slot() is only a function to prevent code duplication. */
475 /* No free slot found */
476 printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
480 int ipt_account_handle_free(unsigned int handle)
482 if (handle >= ACCOUNT_MAX_HANDLES) {
483 printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle);
487 ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth);
488 memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle));
492 /* Prepare data for read without flush. Use only for debugging!
493 Real applications should use read&flush as it's way more efficent */
494 int ipt_account_handle_prepare_read(char *tablename, unsigned int *count)
496 int handle, i, table_nr=-1;
498 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
499 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) {
504 if (table_nr == -1) {
505 printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
509 /* Can't find a free handle slot? */
510 if ((handle = ipt_account_handle_find_slot()) == -1)
513 /* Fill up handle structure */
514 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
515 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
516 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
518 /* allocate "root" table */
519 if ((ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
520 printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
521 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
525 /* Recursive copy of complete data structure */
526 unsigned int depth = ipt_account_handles[handle].depth;
528 memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
529 } else if (depth == 1) {
530 struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
531 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
534 for (b = 0; b <= 255; b++) {
535 if (src_16->mask_24[b]) {
536 if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
537 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
538 ipt_account_data_free(ipt_account_handles[handle].data, depth);
539 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
543 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
546 } else if(depth == 2) {
547 struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
548 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
551 for (a = 0; a <= 255; a++) {
552 if (src_8->mask_16[a]) {
553 if ((network_8->mask_16[a] = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
554 printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
555 ipt_account_data_free(ipt_account_handles[handle].data, depth);
556 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
560 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
562 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
563 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
566 for (b = 0; b <= 255; b++) {
567 if (src_16->mask_24[b]) {
568 if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
569 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
570 ipt_account_data_free(ipt_account_handles[handle].data, depth);
571 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
575 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
582 *count = ipt_account_tables[table_nr].itemcount;
586 /* Prepare data for read and flush it */
587 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count)
589 int handle, i, table_nr=-1;
591 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
592 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) {
597 if (table_nr == -1) {
598 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
602 /* Can't find a free handle slot? */
603 if ((handle = ipt_account_handle_find_slot()) == -1)
606 /* Try to allocate memory */
607 void *new_data_page = (void*)get_zeroed_page(GFP_ATOMIC);
610 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Out of memory!\n");
614 /* Fill up handle structure */
615 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
616 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
617 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
618 ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
619 *count = ipt_account_tables[table_nr].itemcount;
621 /* "Flush" table data */
622 ipt_account_tables[table_nr].data = new_data_page;
623 ipt_account_tables[table_nr].itemcount = 0;
628 /* Copy 8 bit network data into a prepared buffer.
629 We only copy entries != 0 to increase performance.
631 void ipt_account_handle_copy_data(void *to_user, int *pos, struct ipt_account_mask_24 *data,
632 unsigned int net_ip, unsigned int net_OR_mask)
634 struct ipt_account_handle_ip handle_ip;
635 unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
639 for (i = 0; i <= 255; i++) {
640 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
641 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
643 handle_ip.src_packets = data->ip[i].src_packets;
644 handle_ip.src_bytes = data->ip[i].src_bytes;
645 handle_ip.dst_packets = data->ip[i].dst_packets;
646 handle_ip.dst_bytes = data->ip[i].dst_bytes;
648 /* Temporary buffer full? Flush to userspace */
649 if (*pos+handle_ip_size >= PAGE_SIZE) {
650 copy_to_user(to_user, ipt_account_tmpbuf, *pos);
653 memcpy(ipt_account_tmpbuf+*pos, &handle_ip, handle_ip_size);
654 *pos += handle_ip_size;
659 /* Copy the data from our internal structure
660 We only copy entries != 0 to increase performance.
661 Overwrites ipt_account_tmpbuf.
663 int ipt_account_handle_get_data(unsigned int handle, void *to_user)
665 unsigned int tmpbuf_pos=0;
667 if (handle >= ACCOUNT_MAX_HANDLES) {
668 printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
672 if (ipt_account_handles[handle].data == NULL) {
673 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
677 unsigned int net_ip = ipt_account_handles[handle].ip;
678 unsigned int depth = ipt_account_handles[handle].depth;
682 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
683 ipt_account_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, 0);
685 /* Flush remaining data to userspace */
687 copy_to_user(to_user, ipt_account_tmpbuf, tmpbuf_pos);
694 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
696 for (b = 0; b <= 255; b++) {
697 if (network_16->mask_24[b]) {
698 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
699 ipt_account_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, (b << 16));
703 /* Flush remaining data to userspace */
705 copy_to_user(to_user, ipt_account_tmpbuf, tmpbuf_pos);
712 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
714 for (a = 0; a <= 255; a++) {
715 if (network_8->mask_16[a]) {
716 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
717 for (b = 0; b <= 255; b++) {
718 if (network_16->mask_24[b]) {
719 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
720 ipt_account_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, (a << 8) | (b << 16));
726 /* Flush remaining data to userspace */
728 copy_to_user(to_user, ipt_account_tmpbuf, tmpbuf_pos);
736 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
738 struct ipt_account_handle_sockopt handle;
741 if (!capable(CAP_NET_ADMIN))
745 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
746 if (len != sizeof(struct ipt_account_handle_sockopt)) {
747 printk("ACCOUNT: ipt_account_set_ctl: wrong data size (%u != %u) for IPT_SO_SET_HANDLE_FREE\n", len, sizeof(struct ipt_account_handle_sockopt));
751 if (copy_from_user (&handle, user, len)) {
752 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
756 spin_lock_bh(&ipt_account_userspace_lock);
757 ret = ipt_account_handle_free(handle.handle_nr);
758 spin_unlock_bh(&ipt_account_userspace_lock);
760 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
762 spin_lock_bh(&ipt_account_userspace_lock);
763 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
764 ipt_account_handle_free(i);
765 spin_unlock_bh(&ipt_account_userspace_lock);
770 printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
776 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len)
778 struct ipt_account_handle_sockopt handle;
781 if (!capable(CAP_NET_ADMIN))
785 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
786 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
787 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
788 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
789 *len, sizeof(struct ipt_account_handle_sockopt));
793 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) {
798 spin_lock_bh(&ipt_account_lock);
799 spin_lock_bh(&ipt_account_userspace_lock);
800 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
801 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
803 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
804 spin_unlock_bh(&ipt_account_userspace_lock);
805 spin_unlock_bh(&ipt_account_lock);
807 if (handle.handle_nr == -1) {
812 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt))) {
818 case IPT_SO_GET_ACCOUNT_GET_DATA:
819 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
820 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
821 *len, sizeof(struct ipt_account_handle_sockopt));
825 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) {
830 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
835 if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip)) {
836 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
837 *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
842 spin_lock_bh(&ipt_account_userspace_lock);
843 ret = ipt_account_handle_get_data(handle.handle_nr, user);
844 spin_unlock_bh(&ipt_account_userspace_lock);
846 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
852 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
853 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
854 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
855 *len, sizeof(struct ipt_account_handle_sockopt));
859 /* Find out how many handles are in use */
861 handle.itemcount = 0;
862 spin_lock_bh(&ipt_account_userspace_lock);
863 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
864 if (ipt_account_handles[i].data)
866 spin_unlock_bh(&ipt_account_userspace_lock);
868 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt))) {
875 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
876 spin_lock_bh(&ipt_account_lock);
878 /* Determine size of table names */
879 unsigned int size = 0, i;
880 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
881 if (ipt_account_tables[i].name[0] != 0)
882 size += strlen (ipt_account_tables[i].name) + 1;
884 size += 1; /* Terminating NULL character */
887 spin_unlock_bh(&ipt_account_lock);
888 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store table names\n", *len, size);
892 /* Copy table names to userspace */
894 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
895 if (ipt_account_tables[i].name[0] != 0) {
896 int len = strlen (ipt_account_tables[i].name) + 1;
897 copy_to_user(tnames, ipt_account_tables[i].name, len); /* copy string + terminating zero */
901 /* Append terminating zero */
903 copy_to_user(tnames, &i, 1);
904 spin_unlock_bh(&ipt_account_lock);
909 printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
915 static struct ipt_target ipt_account_reg
919 , "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
923 static struct nf_sockopt_ops ipt_account_sockopts
927 , PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
928 IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL
931 static int __init init(void)
933 if ((ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)) == NULL) {
934 printk("ACCOUNT: Out of memory allocating account_tables structure");
937 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
939 if ((ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)) == NULL) {
940 printk("ACCOUNT: Out of memory allocating account_handles structure");
941 kfree (ipt_account_tables);
942 ipt_account_tables = NULL;
945 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
947 /* Allocate one page as temporary storage */
948 if ((ipt_account_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
949 printk("ACCOUNT: Out of memory for temporary buffer page\n");
950 kfree(ipt_account_tables);
951 kfree(ipt_account_handles);
952 ipt_account_tables = NULL;
953 ipt_account_handles = NULL;
957 /* Register setsockopt */
958 if (nf_register_sockopt(&ipt_account_sockopts) < 0) {
959 printk("ACCOUNT: Can't register sockopts. Aborting\n");
961 kfree(ipt_account_tables);
962 kfree(ipt_account_handles);
963 free_page((unsigned long)ipt_account_tmpbuf);
964 ipt_account_tables = NULL;
965 ipt_account_handles = NULL;
966 ipt_account_tmpbuf = NULL;
971 if (ipt_register_target(&ipt_account_reg))
977 static void __exit fini(void)
979 ipt_unregister_target(&ipt_account_reg);
981 nf_unregister_sockopt(&ipt_account_sockopts);
983 kfree(ipt_account_tables);
984 kfree(ipt_account_handles);
985 free_page((unsigned long)ipt_account_tmpbuf);
987 ipt_account_tables = NULL;
988 ipt_account_handles = NULL;
989 ipt_account_tmpbuf = NULL;
994 MODULE_LICENSE("GPL");