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 struct ipt_account_table *ipt_account_tables = NULL;
35 struct ipt_account_handle *ipt_account_handles = NULL;
36 void *ipt_account_tmpbuf = NULL;
38 /* Spinlock used for manipulating the current accounting tables/data */
39 static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED;
40 /* Spinlock used for manipulating userspace handles/snapshot data */
41 static spinlock_t ipt_account_userspace_lock = SPIN_LOCK_UNLOCKED;
44 /* Recursive free of all data structures */
45 void ipt_account_data_free(void *data, unsigned char depth) {
50 // Free for 8 bit network
52 free_page((unsigned long)data);
57 // Free for 16 bit network
59 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data;
61 for (b=0; b <= 255; b++) {
62 if (mask_16->mask_24[b] != 0) {
63 free_page((unsigned long)mask_16->mask_24[b]);
64 mask_16->mask_24[b] = NULL;
67 free_page((unsigned long)data);
72 // Free for 24 bit network
75 for (a=0; a <= 255; a++) {
76 if (((struct ipt_account_mask_8 *)data)->mask_16[a]) {
77 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16*)((struct ipt_account_mask_8 *)data)->mask_16[a];
78 for (b=0; b <= 255; b++) {
79 if (mask_16->mask_24[b]) {
80 free_page((unsigned long)mask_16->mask_24[b]);
81 mask_16->mask_24[b] = NULL;
84 free_page((unsigned long)mask_16);
88 free_page((unsigned long)data);
93 printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth);
97 /* Look for existing table / insert new one. Return internal ID or -1 on error */
98 int ipt_account_table_insert(char *name, unsigned int ip, unsigned int netmask) {
101 DEBUGP("ACCOUNT: ipt_account_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", name, NIPQUAD(ip), NIPQUAD(netmask));
103 // Look for existing table
104 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
105 if (strncmp(ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN) == 0) {
106 DEBUGP("ACCOUNT: Found existing slot: %d - %u.%u.%u.%u/%u.%u.%u.%u\n", i,
107 NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
109 if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask) {
110 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
111 name, NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
115 ipt_account_tables[i].refcount++;
116 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount);
122 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
124 if (ipt_account_tables[i].name[0] == 0) {
125 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
127 strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
129 ipt_account_tables[i].ip = ip;
130 ipt_account_tables[i].netmask = netmask;
133 unsigned int j, calc_mask, netsize=0;
134 calc_mask = htonl(netmask);
135 for (j = 31; j > 0; j--) {
136 if (calc_mask&(1<<j))
142 // Calculate depth from netsize
144 ipt_account_tables[i].depth = 0;
145 else if (netsize >= 16)
146 ipt_account_tables[i].depth = 1;
147 else if(netsize >= 8)
148 ipt_account_tables[i].depth = 2;
150 printk("ACCOUNT: calculated netsize: %u -> ipt_account_table depth %u\n", netsize, ipt_account_tables[i].depth);
152 ipt_account_tables[i].refcount++;
153 if ((ipt_account_tables[i].data = (void *)get_zeroed_page(GFP_KERNEL)) == NULL) {
154 printk("ACCOUNT: out of memory for data of table: %s\n", name);
155 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
163 // No free slot found
164 printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
168 static int ipt_account_checkentry(const char *tablename,
169 const struct ipt_entry *e,
171 unsigned int targinfosize,
172 unsigned int hook_mask) {
173 struct ipt_account_info *info = targinfo;
175 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
176 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
177 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
181 spin_lock_bh(&ipt_account_lock);
182 int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask);
183 if (table_nr == -1) {
184 printk("ACCOUNT: Table insert problem. Aborting\n");
185 spin_unlock_bh(&ipt_account_lock);
188 // Table nr caching so we don't have to do an extra string compare for every packet
189 info->table_nr = table_nr;
191 spin_unlock_bh(&ipt_account_lock);
196 void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize) {
198 struct ipt_account_info *info = targinfo;
200 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
201 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
202 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
205 spin_lock_bh(&ipt_account_lock);
207 DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr);
209 info->table_nr = -1; // Set back to original state
212 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
213 if (strncmp(ipt_account_tables[i].name, info->table_name, ACCOUNT_TABLE_NAME_LEN) == 0) {
214 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
216 ipt_account_tables[i].refcount--;
217 DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount);
219 // Table not needed anymore?
220 if (ipt_account_tables[i].refcount == 0) {
221 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
222 ipt_account_data_free(ipt_account_tables[i].data, ipt_account_tables[i].depth);
223 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
226 spin_unlock_bh(&ipt_account_lock);
232 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
233 spin_unlock_bh(&ipt_account_lock);
236 void ipt_account_depth0_insert(struct ipt_account_mask_24 *mask_24, unsigned int net_ip, unsigned int netmask,
237 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) {
238 unsigned char is_src = 0, is_dst = 0;
240 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",
241 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
243 // Check if src/dst is inside our network.
244 // Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0
247 if ((net_ip&netmask) == (src_ip&netmask))
249 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
252 if (!is_src && !is_dst) {
253 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u\n",
254 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
258 // Check if this entry is new
259 char is_src_new_ip = 0, is_dst_new_ip = 0;
261 // Calculate array positions
262 unsigned char src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
263 unsigned char dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
265 // Increase size counters
267 // Calculate network slot
268 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
269 if (!mask_24->ip[src_slot].src_packets && !mask_24->ip[src_slot].dst_packets)
272 mask_24->ip[src_slot].src_packets++;
273 mask_24->ip[src_slot].src_bytes+=size;
276 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
277 if (!mask_24->ip[dst_slot].src_packets && !mask_24->ip[dst_slot].dst_packets)
280 mask_24->ip[dst_slot].dst_packets++;
281 mask_24->ip[dst_slot].dst_bytes+=size;
284 // Increase itemcounter
285 DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
286 if (src_slot == dst_slot) {
287 if (is_src_new_ip || is_dst_new_ip) {
288 DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n", is_src_new_ip, is_dst_new_ip);
293 DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
297 DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
301 DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
304 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask,
305 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) {
306 // Do we need to process src IP?
307 if ((net_ip&netmask) == (src_ip&netmask)) {
308 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
309 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
311 // Do we need to create a new mask_24 bucket?
312 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL) {
313 printk("ACCOUNT: Can't process packet because out of memory!\n");
317 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
318 src_ip, 0, size, itemcount);
321 // Do we need to process dst IP?
322 if ((net_ip&netmask) == (dst_ip&netmask)) {
323 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
324 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
326 // Do we need to create a new mask_24 bucket?
327 if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL) {
328 printk("ACCOUT: Can't process packet because out of memory!\n");
332 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
333 0, dst_ip, size, itemcount);
337 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask,
338 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount) {
339 // Do we need to process src IP?
340 if ((net_ip&netmask) == (src_ip&netmask)) {
341 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
342 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
344 // Do we need to create a new mask_24 bucket?
345 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL) {
346 printk("ACCOUNT: Can't process packet because out of memory!\n");
350 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
351 src_ip, 0, size, itemcount);
354 // Do we need to process dst IP?
355 if ((net_ip&netmask) == (dst_ip&netmask)) {
356 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
357 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
359 // Do we need to create a new mask_24 bucket?
360 if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL) {
361 printk("ACCOUNT: Can't process packet because out of memory!\n");
365 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
366 0, dst_ip, size, itemcount);
370 static unsigned int ipt_account_target(struct sk_buff **pskb,
371 unsigned int hooknum,
372 const struct net_device *in,
373 const struct net_device *out,
374 const void *targinfo,
376 const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
377 unsigned int src_ip = (*pskb)->nh.iph->saddr;
378 unsigned int dst_ip = (*pskb)->nh.iph->daddr;
379 unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
381 spin_lock_bh(&ipt_account_lock);
383 if (ipt_account_tables[info->table_nr].name[0] == 0) {
384 printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
385 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
386 spin_unlock_bh(&ipt_account_lock);
390 // 8 bit network or "any" network
391 if (ipt_account_tables[info->table_nr].depth == 0) {
392 // Count packet and check if the IP is new
393 ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
394 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
395 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
396 spin_unlock_bh(&ipt_account_lock);
401 if (ipt_account_tables[info->table_nr].depth == 1) {
402 ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
403 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
404 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
405 spin_unlock_bh(&ipt_account_lock);
410 if (ipt_account_tables[info->table_nr].depth == 2) {
411 ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
412 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
413 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
414 spin_unlock_bh(&ipt_account_lock);
418 printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
419 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
421 spin_unlock_bh(&ipt_account_lock);
426 Functions dealing with "handles":
427 Handles are snapshots of a accounting state.
429 read snapshots are only for debugging the code
430 and are very expensive concerning speed/memory
431 compared to read_and_flush.
433 The functions aren't protected by spinlocks themselves
434 as this is done in the ioctl part of the code.
438 Find a free handle slot. Normally only one should be used,
439 but there could be two or more applications accessing the data
442 int ipt_account_handle_find_slot(void) {
445 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
447 if (ipt_account_handles[i].data == NULL) {
448 // Don't "mark" data as used as we are protected by a spinlock by the calling function.
449 // handle_find_slot() is only a function to prevent code duplication.
454 // No free slot found
455 printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
459 int ipt_account_handle_free(unsigned int handle) {
460 if (handle >= ACCOUNT_MAX_HANDLES) {
461 printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle);
465 ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth);
466 memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle));
470 /* Prepare data for read without flush. Use only for debugging!
471 Real applications should use read&flush as it's way more efficent */
472 int ipt_account_handle_prepare_read(char *tablename, unsigned int *count) {
473 int handle, i, table_nr=-1;
475 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
476 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) {
481 if (table_nr == -1) {
482 printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
486 // Can't find a free handle slot?
487 if ((handle = ipt_account_handle_find_slot()) == -1)
490 // Fill up handle structure
491 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
492 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
493 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
495 // allocate "root" table
496 if ((ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL)) == NULL) {
497 printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
498 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
502 // Recursive copy of complete data structure
503 unsigned int depth = ipt_account_handles[handle].depth;
505 memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
506 } else if (depth == 1) {
507 struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
508 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
511 for (b = 0; b <= 255; b++) {
512 if (src_16->mask_24[b]) {
513 if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL) {
514 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
515 ipt_account_data_free(ipt_account_handles[handle].data, depth);
516 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
520 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
523 } else if(depth == 2) {
524 struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
525 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
528 for (a = 0; a <= 255; a++) {
529 if (src_8->mask_16[a]) {
530 if ((network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL) {
531 printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
532 ipt_account_data_free(ipt_account_handles[handle].data, depth);
533 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
537 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
539 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
540 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
543 for (b = 0; b <= 255; b++) {
544 if (src_16->mask_24[b]) {
545 if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL) {
546 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
547 ipt_account_data_free(ipt_account_handles[handle].data, depth);
548 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
552 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
559 *count = ipt_account_tables[table_nr].itemcount;
563 /* Prepare data for read and flush it */
564 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count) {
565 int handle, i, table_nr=-1;
567 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
568 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0) {
573 if (table_nr == -1) {
574 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
578 // Can't find a free handle slot?
579 if ((handle = ipt_account_handle_find_slot()) == -1)
582 // Fill up handle structure
583 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
584 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
585 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
586 ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
587 *count = ipt_account_tables[table_nr].itemcount;
589 // "Flush" table data
590 ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL);
591 ipt_account_tables[table_nr].itemcount = 0;
596 /* Copy the actual that into a prepared buffer.
597 We only copy entries != 0 to increase performance.
598 The memory gets freed again in ipt_account_handle_free().
600 int ipt_account_handle_get_data(unsigned int handle, void *buffer) {
601 struct ipt_account_handle_ip handle_ip;
602 unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
603 unsigned int i, tmpbuf_pos=0;
605 if (handle >= ACCOUNT_MAX_HANDLES) {
606 printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
610 if (ipt_account_handles[handle].data == NULL) {
611 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
615 unsigned int net_ip = ipt_account_handles[handle].ip;
616 unsigned int depth = ipt_account_handles[handle].depth;
620 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
621 for (i = 0; i <= 255; i++) {
622 if (network->ip[i].src_packets || network->ip[i].dst_packets) {
623 handle_ip.ip = net_ip | (i<<24);
624 handle_ip.src_packets = network->ip[i].src_packets;
625 handle_ip.src_bytes = network->ip[i].src_bytes;
626 handle_ip.dst_packets = network->ip[i].dst_packets;
627 handle_ip.dst_bytes = network->ip[i].dst_bytes;
629 // Temporary buffer full? Flush to userspace
630 if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
631 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
634 memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
635 tmpbuf_pos += handle_ip_size;
639 // Flush remaining data to userspace
641 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
648 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
650 for (b = 0; b <= 255; b++) {
651 if (network_16->mask_24[b]) {
652 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
653 for (i = 0; i <= 255; i++) {
654 if (network->ip[i].src_packets || network->ip[i].dst_packets) {
655 handle_ip.ip = net_ip | (b << 16) | (i<<24);
656 handle_ip.src_packets = network->ip[i].src_packets;
657 handle_ip.src_bytes = network->ip[i].src_bytes;
658 handle_ip.dst_packets = network->ip[i].dst_packets;
659 handle_ip.dst_bytes = network->ip[i].dst_bytes;
661 // Temporary buffer full? Flush to userspace
662 if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
663 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
666 memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
667 tmpbuf_pos += handle_ip_size;
673 // Flush remaining data to userspace
675 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
682 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
684 for (a = 0; a <= 255; a++) {
685 if (network_8->mask_16[a]) {
686 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
687 for (b = 0; b <= 255; b++) {
688 if (network_16->mask_24[b]) {
689 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
690 for (i = 0; i <= 255; i++) {
691 if (network->ip[i].src_packets || network->ip[i].dst_packets) {
692 handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24);
693 handle_ip.src_packets = network->ip[i].src_packets;
694 handle_ip.src_bytes = network->ip[i].src_bytes;
695 handle_ip.dst_packets = network->ip[i].dst_packets;
696 handle_ip.dst_bytes = network->ip[i].dst_bytes;
698 // Temporary buffer full? Flush to userspace
699 if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
700 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
703 memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
704 tmpbuf_pos += handle_ip_size;
712 // Flush remaining data to userspace
714 copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
722 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len) {
723 struct ipt_account_handle_sockopt handle;
726 if (!capable(CAP_NET_ADMIN))
730 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
731 if (len != sizeof(struct ipt_account_handle_sockopt)) {
732 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));
736 if (copy_from_user (&handle, user, len)) {
737 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
741 spin_lock_bh(&ipt_account_userspace_lock);
742 ret = ipt_account_handle_free(handle.handle_nr);
743 spin_unlock_bh(&ipt_account_userspace_lock);
745 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
747 spin_lock_bh(&ipt_account_userspace_lock);
748 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
749 ipt_account_handle_free(i);
750 spin_unlock_bh(&ipt_account_userspace_lock);
755 printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
761 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len) {
762 struct ipt_account_handle_sockopt handle;
765 if (!capable(CAP_NET_ADMIN))
769 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
770 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
771 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
772 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
773 *len, sizeof(struct ipt_account_handle_sockopt));
777 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) {
778 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
782 spin_lock_bh(&ipt_account_lock);
783 spin_lock_bh(&ipt_account_userspace_lock);
784 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
785 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
787 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
788 spin_unlock_bh(&ipt_account_userspace_lock);
789 spin_unlock_bh(&ipt_account_lock);
791 if (handle.handle_nr == -1) {
792 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n");
796 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt))) {
797 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
802 case IPT_SO_GET_ACCOUNT_GET_DATA:
803 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
804 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
805 *len, sizeof(struct ipt_account_handle_sockopt));
809 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt))) {
810 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
814 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
815 printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr);
819 if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip)) {
820 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
821 *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
826 spin_lock_bh(&ipt_account_userspace_lock);
827 ret = ipt_account_handle_get_data(handle.handle_nr, user);
828 spin_unlock_bh(&ipt_account_userspace_lock);
830 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
836 case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
837 if (*len < sizeof(struct ipt_account_handle_sockopt)) {
838 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
839 *len, sizeof(struct ipt_account_handle_sockopt));
843 // Find out how many handles are in use
845 handle.itemcount = 0;
846 spin_lock_bh(&ipt_account_userspace_lock);
847 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
848 if (ipt_account_handles[i].data)
850 spin_unlock_bh(&ipt_account_userspace_lock);
852 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt))) {
853 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n");
859 case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
860 spin_lock_bh(&ipt_account_lock);
862 // Determine size of table names
863 unsigned int size = 0, i;
864 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
865 if (ipt_account_tables[i].name[0] != 0)
866 size += strlen (ipt_account_tables[i].name) + 1;
868 size += 1; // Terminating NULL character
871 spin_unlock_bh(&ipt_account_lock);
872 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store table names\n", *len, size);
876 // Copy table names to userspace
878 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
879 if (ipt_account_tables[i].name[0] != 0) {
880 int len = strlen (ipt_account_tables[i].name) + 1;
881 copy_to_user(tnames, ipt_account_tables[i].name, len); // copy string + terminating zero
885 // Append terminating zero
887 copy_to_user(tnames, &i, 1);
888 spin_unlock_bh(&ipt_account_lock);
893 printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
899 static struct ipt_target ipt_account_reg
903 , "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
907 static struct nf_sockopt_ops ipt_account_sockopts
911 , PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
912 IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL
915 static int __init init(void) {
916 if (PAGE_SIZE < 4096) {
917 printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE);
921 if ((ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)) == NULL) {
922 printk("ACCOUNT: Out of memory allocating account_tables structure");
925 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
927 if ((ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)) == NULL) {
928 printk("ACCOUNT: Out of memory allocating account_handles structure");
929 kfree (ipt_account_tables);
930 ipt_account_tables = NULL;
933 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
935 // Allocate one page as temporary storage
936 if ((ipt_account_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
937 printk("ACCOUNT: Out of memory for temporary buffer page\n");
938 kfree(ipt_account_tables);
939 kfree(ipt_account_handles);
940 ipt_account_tables = NULL;
941 ipt_account_handles = NULL;
945 /* Register setsockopt */
946 if (nf_register_sockopt(&ipt_account_sockopts) < 0) {
947 printk("ACCOUNT: Can't register sockopts. Aborting\n");
949 kfree(ipt_account_tables);
950 kfree(ipt_account_handles);
951 free_page((unsigned long)ipt_account_tmpbuf);
952 ipt_account_tables = NULL;
953 ipt_account_handles = NULL;
954 ipt_account_tmpbuf = NULL;
959 if (ipt_register_target(&ipt_account_reg))
965 static void __exit fini(void) {
966 ipt_unregister_target(&ipt_account_reg);
968 nf_unregister_sockopt(&ipt_account_sockopts);
970 kfree(ipt_account_tables);
971 kfree(ipt_account_handles);
972 free_page((unsigned long)ipt_account_tmpbuf);
974 ipt_account_tables = NULL;
975 ipt_account_handles = NULL;
976 ipt_account_tmpbuf = NULL;
981 MODULE_LICENSE("GPL");