2 * This is a module which is used for counting packets.
4 #include <linux/module.h>
5 #include <linux/skbuff.h>
7 #include <linux/spinlock.h>
11 #include <linux/netfilter_ipv4/ip_tables.h>
14 #include <net/route.h>
15 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
20 //#define DEBUGP(format, args...)
23 struct ipt_account_table *ipt_account_tables = NULL;
24 struct ipt_account_handle *ipt_account_handles = NULL;
26 static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED;
28 /* Recursive free of all data structures */
29 void ipt_account_data_free(void *data, unsigned char depth)
35 // Free for 8 bit network
38 free_page((unsigned long)data);
43 // Free for 16 bit network
46 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data;
48 for (b=0; b < 255; b++)
50 if (mask_16->mask_24[b] != 0)
52 free_page((unsigned long)mask_16->mask_24[b]);
53 mask_16->mask_24[b] = NULL;
56 free_page((unsigned long)data);
61 // Free for 24 bit network
65 for (a=0; a < 255; a++)
67 if (((struct ipt_account_mask_8 *)data)->mask_16[a])
69 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16*)((struct ipt_account_mask_8 *)data)->mask_16[a];
70 for (b=0; b < 255; b++)
72 if (mask_16->mask_24[b]) {
73 free_page((unsigned long)mask_16->mask_24[b]);
74 mask_16->mask_24[b] = NULL;
77 free_page((unsigned long)mask_16);
81 free_page((unsigned long)data);
86 printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth);
90 /* Look for existing table / insert new one. Return internal ID or -1 on error */
91 int ipt_account_table_insert(char *name, unsigned int ip, unsigned int netmask)
95 DEBUGP("ACCOUNT: ipt_account_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", name, NIPQUAD(ip), NIPQUAD(netmask));
97 // Look for existing table
98 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
100 if (strncmp(ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN) == 0)
102 DEBUGP("ACCOUNT: Found existing slot: %d - %u.%u.%u.%u/%u.%u.%u.%u\n", i,
103 NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
105 if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask)
107 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
108 name, NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
112 ipt_account_tables[i].refcount++;
113 DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount);
119 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
122 if (ipt_account_tables[i].name[0] == 0)
124 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
126 strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
128 ipt_account_tables[i].ip = ip;
129 ipt_account_tables[i].netmask = netmask;
132 unsigned int j, calc_mask, netsize=0;
133 calc_mask = htonl(netmask);
134 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)))
155 printk("ACCOUNT: out of memory for data of table: %s\n", name);
156 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
164 // No free slot found
165 printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
169 static int ipt_account_checkentry(const char *tablename,
170 const struct ipt_entry *e,
172 unsigned int targinfosize,
173 unsigned int hook_mask)
175 struct ipt_account_info *info = targinfo;
177 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
178 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
179 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
183 spin_lock_bh(&ipt_account_lock);
184 int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask);
187 printk("ACCOUNT: Table insert problem. Aborting\n");
188 spin_unlock_bh(&ipt_account_lock);
191 // Table nr caching so we don't have to do an extra string compare for every packet
192 info->table_nr = table_nr;
194 spin_unlock_bh(&ipt_account_lock);
199 void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize)
202 struct ipt_account_info *info = targinfo;
204 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
205 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
206 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
209 spin_lock_bh(&ipt_account_lock);
211 DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr);
213 info->table_nr = -1; // Set back to original state
216 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
218 if (strncmp(ipt_account_tables[i].name, info->table_name, ACCOUNT_TABLE_NAME_LEN) == 0)
220 DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
222 ipt_account_tables[i].refcount--;
223 DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount);
225 // Table not needed anymore?
226 if (ipt_account_tables[i].refcount == 0)
228 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
229 ipt_account_data_free(ipt_account_tables[i].data, ipt_account_tables[i].depth);
230 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
233 spin_unlock_bh(&ipt_account_lock);
239 printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
240 spin_unlock_bh(&ipt_account_lock);
243 void ipt_account_depth0_insert(struct ipt_account_mask_24 *mask_24, unsigned int net_ip, unsigned int netmask,
244 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
246 unsigned char is_src = 0, is_dst = 0;
248 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",
249 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
251 // Check if src/dst is inside our network.
252 // Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0
255 if ((net_ip&netmask) == (src_ip&netmask))
257 if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
260 if (!is_src && !is_dst)
262 DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u\n",
263 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
267 // Check if this entry is new
268 char is_src_new_ip = 0, is_dst_new_ip = 0;
270 // Calculate array positions
271 unsigned char src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
272 unsigned char dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
274 // Increase size counters
277 // Calculate network slot
278 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", slot);
279 if (!mask_24->ip[src_slot].src_packets && !mask_24->ip[src_slot].dst_packets)
282 mask_24->ip[src_slot].src_packets++;
283 mask_24->ip[src_slot].src_bytes+=size;
287 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", slot);
288 if (!mask_24->ip[dst_slot].src_packets && !mask_24->ip[dst_slot].dst_packets)
291 mask_24->ip[dst_slot].dst_packets++;
292 mask_24->ip[dst_slot].dst_bytes+=size;
295 // Increase itemcounter
296 if (src_slot == dst_slot)
298 if (is_src_new_ip || is_dst_new_ip)
308 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask,
309 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
311 // Do we need to process src IP?
312 if ((net_ip&netmask) == (src_ip&netmask))
314 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
315 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
317 // Do we need to create a new mask_24 bucket?
318 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
320 printk("ACCOUNT: Can't process packet because out of memory!\n");
324 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
325 src_ip, dst_ip, size, itemcount);
328 // Do we need to process dst IP?
329 if ((net_ip&netmask) == (dst_ip&netmask))
331 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
332 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
334 // Do we need to create a new mask_24 bucket?
335 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
337 printk("ACCOUT: Can't process packet because out of memory!\n");
341 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
342 src_ip, dst_ip, size, itemcount);
346 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask,
347 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
349 // Do we need to process src IP?
350 if ((net_ip&netmask) == (src_ip&netmask))
352 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
353 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
355 // Do we need to create a new mask_24 bucket?
356 if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
358 printk("ACCOUNT: Can't process packet because out of memory!\n");
362 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
363 src_ip, dst_ip, size, itemcount);
366 // Do we need to process dst IP?
367 if ((net_ip&netmask) == (dst_ip&netmask))
369 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
370 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
372 // Do we need to create a new mask_24 bucket?
373 if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
375 printk("ACCOUNT: Can't process packet because out of memory!\n");
379 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
380 src_ip, dst_ip, size, itemcount);
384 static unsigned int ipt_account_target(struct sk_buff **pskb,
385 unsigned int hooknum,
386 const struct net_device *in,
387 const struct net_device *out,
388 const void *targinfo,
391 const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
392 unsigned int src_ip = (*pskb)->nh.iph->saddr;
393 unsigned int dst_ip = (*pskb)->nh.iph->daddr;
394 unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
396 spin_lock_bh(&ipt_account_lock);
398 if (ipt_account_tables[info->table_nr].name[0] == 0)
400 printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
401 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
402 spin_unlock_bh(&ipt_account_lock);
406 // 8 bit network or "any" network
407 if (ipt_account_tables[info->table_nr].depth == 0)
409 // Count packet and check if the IP is new
410 ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
411 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
412 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
413 spin_unlock_bh(&ipt_account_lock);
418 if (ipt_account_tables[info->table_nr].depth == 1)
420 ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
421 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
422 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
423 spin_unlock_bh(&ipt_account_lock);
428 if (ipt_account_tables[info->table_nr].depth == 2)
430 ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
431 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
432 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
433 spin_unlock_bh(&ipt_account_lock);
437 printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
438 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
440 spin_unlock_bh(&ipt_account_lock);
445 Functions dealing with "handles":
446 Handles are snapshots of a accounting state.
448 read snapshots are only for debugging the code
449 and are very expensive concerning speed/memory
450 compared to read_and_flush.
452 The functions aren't protected by spinlocks themselves
453 as this is done in the ioctl part of the code.
457 Find a free handle slot. Normally only one should be used,
458 but there could be two or more applications accessing the data
461 int ipt_account_handle_find_slot(void)
465 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
468 if (ipt_account_handles[i].data == NULL)
470 // Don't "mark" data as used as we are protected by a spinlock by the calling function.
471 // handle_find_slot() is only a function to prevent code duplication.
476 // No free slot found
477 printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
481 int ipt_account_handle_free(unsigned int handle)
483 if (handle >= ACCOUNT_MAX_HANDLES)
485 printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle);
489 ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth);
490 memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle));
494 /* Prepare data for read without flush. Use only for debugging!
495 Real applications should use read&flush as it's way more efficent */
496 int ipt_account_handle_prepare_read(char *tablename, unsigned int *count)
498 int handle, i, table_nr=-1;
500 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
501 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
509 printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
513 // Can't find a free handle slot?
514 if ((handle = ipt_account_handle_find_slot()) == -1)
517 // Fill up handle structure
518 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
519 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
520 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
522 // allocate "root" table
523 if (!(ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL)))
525 printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
526 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
530 // Recursive copy of complete data structure
531 unsigned int depth = ipt_account_handles[handle].depth;
534 memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
535 } else if (depth == 1) {
536 struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
537 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
540 for (b = 0; b < 255; b++)
542 if (src_16->mask_24[b])
544 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
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));
555 } else if(depth == 2) {
556 struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
557 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
560 for (a = 0; a < 255; a++)
562 if (src_8->mask_16[a])
564 if (!(network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL)))
566 printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
567 ipt_account_data_free(ipt_account_handles[handle].data, depth);
568 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
572 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
574 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
575 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
578 for (b = 0; b < 255; b++)
580 if (src_16->mask_24[b])
582 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
584 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
585 ipt_account_data_free(ipt_account_handles[handle].data, depth);
586 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
590 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
597 *count = ipt_account_tables[table_nr].itemcount;
601 /* Prepare data for read and flush it */
602 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count)
604 int handle, i, table_nr=-1;
606 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
607 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
615 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
619 // Can't find a free handle slot?
620 if ((handle = ipt_account_handle_find_slot()) == -1)
623 // Fill up handle structure
624 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
625 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
626 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
627 ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
628 *count = ipt_account_tables[table_nr].itemcount;
630 // "Flush" table data
631 ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL);
632 ipt_account_tables[table_nr].itemcount = 0;
637 /* Copy the actual that into a prepared buffer.
638 We only copy entries != 0 to increase performance.
639 The memory gets freed again in ipt_account_handle_free().
641 int ipt_account_handle_get_data(unsigned int handle, void *buffer)
643 struct ipt_account_handle_ip handle_ip;
644 unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
647 if (handle >= ACCOUNT_MAX_HANDLES)
649 printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
653 if (ipt_account_handles[handle].data == NULL)
655 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
659 // Check if at least packet src/dst matches ip/netmask
660 unsigned int net_ip = ipt_account_handles[handle].ip;
661 unsigned int depth = ipt_account_handles[handle].depth;
666 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
667 for (i = 0; i < 255; i++)
669 if (network->ip[i].src_packets || network->ip[i].dst_packets)
671 handle_ip.ip = net_ip | (i<<24);
672 handle_ip.src_packets = network->ip[i].src_packets;
673 handle_ip.src_bytes = network->ip[i].src_bytes;
674 handle_ip.dst_packets = network->ip[i].dst_packets;
675 handle_ip.dst_bytes = network->ip[i].dst_bytes;
677 copy_to_user(buffer, &handle_ip, handle_ip_size);
678 buffer += handle_ip_size;
688 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
690 for (b = 0; b < 255; b++)
692 if (network_16->mask_24[b])
694 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
695 for (i = 0; i < 255; i++)
697 if (network->ip[i].src_packets || network->ip[i].dst_packets)
699 handle_ip.ip = net_ip | (b << 16) | (i<<24);
700 handle_ip.src_packets = network->ip[i].src_packets;
701 handle_ip.src_bytes = network->ip[i].src_bytes;
702 handle_ip.dst_packets = network->ip[i].dst_packets;
703 handle_ip.dst_bytes = network->ip[i].dst_bytes;
705 copy_to_user(buffer, &handle_ip, handle_ip_size);
706 buffer += handle_ip_size;
718 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
720 for (a = 0; a < 255; a++)
722 if (network_8->mask_16[a])
724 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
725 for (b = 0; b < 255; b++)
727 if (network_16->mask_24[b])
729 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
730 for (i = 0; i < 255; i++)
732 if (network->ip[i].src_packets || network->ip[i].dst_packets)
734 handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24);
735 handle_ip.src_packets = network->ip[i].src_packets;
736 handle_ip.src_bytes = network->ip[i].src_bytes;
737 handle_ip.dst_packets = network->ip[i].dst_packets;
738 handle_ip.dst_bytes = network->ip[i].dst_bytes;
740 copy_to_user(buffer, &handle_ip, handle_ip_size);
741 buffer += handle_ip_size;
755 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
757 struct ipt_account_handle_sockopt handle;
760 if (!capable(CAP_NET_ADMIN))
765 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
766 if (len != sizeof(struct ipt_account_handle_sockopt))
768 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));
772 if (copy_from_user (&handle, user, len))
774 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
778 spin_lock_bh(&ipt_account_lock);
779 ret = ipt_account_handle_free(handle.handle_nr);
780 spin_unlock_bh(&ipt_account_lock);
783 printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
789 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len)
791 struct ipt_account_handle_sockopt handle;
794 if (!capable(CAP_NET_ADMIN))
799 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
800 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
801 if (*len < sizeof(struct ipt_account_handle_sockopt))
803 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
804 *len, sizeof(struct ipt_account_handle_sockopt));
808 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 spin_lock_bh(&ipt_account_lock);
815 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
816 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
818 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
819 spin_unlock_bh(&ipt_account_lock);
821 if (handle.handle_nr == -1)
823 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n");
827 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt)))
829 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
834 case IPT_SO_GET_ACCOUNT_GET_DATA:
835 if (*len < sizeof(struct ipt_account_handle_sockopt))
837 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
838 *len, sizeof(struct ipt_account_handle_sockopt));
842 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
844 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
848 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES)
850 printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr);
854 if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip))
856 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
857 *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
861 spin_lock_bh(&ipt_account_lock);
862 ret = ipt_account_handle_get_data(handle.handle_nr, user);
863 spin_unlock_bh(&ipt_account_lock);
866 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
874 printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
880 static struct ipt_target ipt_account_reg
881 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
884 static struct nf_sockopt_ops ipt_account_sockopts
885 = { { NULL, NULL }, PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
886 IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL };
888 static int __init init(void)
890 if (PAGE_SIZE < 4096)
892 printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE);
896 if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)))
898 printk("ACCOUNT: Out of memory allocating account_tables structure");
901 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
903 if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)))
905 printk("ACCOUNT: Out of memory allocating account_handles structure");
906 kfree (ipt_account_tables);
907 ipt_account_tables = NULL;
910 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
912 /* Register setsockopt */
913 if (nf_register_sockopt(&ipt_account_sockopts) < 0)
915 printk("ACCOUNT: Can't register sockopts. Aborting\n");
917 kfree (ipt_account_tables);
918 ipt_account_tables = NULL;
919 kfree(ipt_account_handles);
920 ipt_account_handles = NULL;
925 if (ipt_register_target(&ipt_account_reg))
931 static void __exit fini(void)
933 ipt_unregister_target(&ipt_account_reg);
935 nf_unregister_sockopt(&ipt_account_sockopts);
937 kfree(ipt_account_tables);
938 ipt_account_tables = NULL;
940 kfree(ipt_account_handles);
941 ipt_account_handles = NULL;
946 MODULE_LICENSE("GPL");