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 // Increase size counters
273 // Calculate network slot
274 unsigned char slot = (unsigned char)((src_ip&0xFF000000) >> 24);
275 DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", slot);
276 if (!mask_24->ip[slot].src_packets && !mask_24->ip[slot].dst_packets)
279 mask_24->ip[slot].src_packets++;
280 mask_24->ip[slot].src_bytes+=size;
284 unsigned char slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
285 DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", slot);
286 if (!mask_24->ip[slot].src_packets && !mask_24->ip[slot].dst_packets)
289 mask_24->ip[slot].dst_packets++;
290 mask_24->ip[slot].dst_bytes+=size;
293 // Increase itemcounter
294 if (src_ip == dst_ip)
296 if (is_src_new_ip || is_dst_new_ip)
306 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask,
307 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
309 // Do we need to process src IP?
310 if ((net_ip&netmask) == (src_ip&netmask))
312 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
313 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
315 // Do we need to create a new mask_24 bucket?
316 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
318 printk("ACCOUNT: Can't process packet because out of memory!\n");
322 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
323 src_ip, dst_ip, size, itemcount);
326 // Do we need to process dst IP?
327 if ((net_ip&netmask) == (dst_ip&netmask))
329 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
330 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
332 // Do we need to create a new mask_24 bucket?
333 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
335 printk("ACCOUT: Can't process packet because out of memory!\n");
339 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
340 src_ip, dst_ip, size, itemcount);
344 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask,
345 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
347 // Do we need to process src IP?
348 if ((net_ip&netmask) == (src_ip&netmask))
350 unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
351 DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
353 // Do we need to create a new mask_24 bucket?
354 if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
356 printk("ACCOUNT: Can't process packet because out of memory!\n");
360 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
361 src_ip, dst_ip, size, itemcount);
364 // Do we need to process dst IP?
365 if ((net_ip&netmask) == (dst_ip&netmask))
367 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
368 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
370 // Do we need to create a new mask_24 bucket?
371 if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
373 printk("ACCOUNT: Can't process packet because out of memory!\n");
377 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
378 src_ip, dst_ip, size, itemcount);
382 static unsigned int ipt_account_target(struct sk_buff **pskb,
383 unsigned int hooknum,
384 const struct net_device *in,
385 const struct net_device *out,
386 const void *targinfo,
389 const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
390 unsigned int src_ip = (*pskb)->nh.iph->saddr;
391 unsigned int dst_ip = (*pskb)->nh.iph->daddr;
392 unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
394 spin_lock_bh(&ipt_account_lock);
396 if (ipt_account_tables[info->table_nr].name[0] == 0)
398 printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
399 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
400 spin_unlock_bh(&ipt_account_lock);
404 // 8 bit network or "any" network
405 if (ipt_account_tables[info->table_nr].depth == 0)
407 // Count packet and check if the IP is new
408 ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
409 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
410 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
411 spin_unlock_bh(&ipt_account_lock);
416 if (ipt_account_tables[info->table_nr].depth == 1)
418 ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
419 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
420 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
421 spin_unlock_bh(&ipt_account_lock);
426 if (ipt_account_tables[info->table_nr].depth == 2)
428 ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
429 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
430 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
431 spin_unlock_bh(&ipt_account_lock);
435 printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
436 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
438 spin_unlock_bh(&ipt_account_lock);
443 Functions dealing with "handles":
444 Handles are snapshots of a accounting state.
446 read snapshots are only for debugging the code
447 and are very expensive concerning speed/memory
448 compared to read_and_flush.
450 The functions aren't protected by spinlocks themselves
451 as this is done in the ioctl part of the code.
455 Find a free handle slot. Normally only one should be used,
456 but there could be two or more applications accessing the data
459 int ipt_account_handle_find_slot(void)
463 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
466 if (ipt_account_handles[i].data == NULL)
468 // Don't "mark" data as used as we are protected by a spinlock by the calling function.
469 // handle_find_slot() is only a function to prevent code duplication.
474 // No free slot found
475 printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
479 int ipt_account_handle_free(unsigned int handle)
481 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)
507 printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
511 // Can't find a free handle slot?
512 if ((handle = ipt_account_handle_find_slot()) == -1)
515 // Fill up handle structure
516 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
517 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
518 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
520 // allocate "root" table
521 if (!(ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL)))
523 printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
524 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
528 // Recursive copy of complete data structure
529 unsigned int depth = ipt_account_handles[handle].depth;
532 memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
533 } else if (depth == 1) {
534 struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
535 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
538 for (b = 0; b < 255; b++)
540 if (src_16->mask_24[b])
542 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
544 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
545 ipt_account_data_free(ipt_account_handles[handle].data, depth);
546 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
550 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
553 } else if(depth == 2) {
554 struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
555 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
558 for (a = 0; a < 255; a++)
560 if (src_8->mask_16[a])
562 if (!(network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL)))
564 printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
565 ipt_account_data_free(ipt_account_handles[handle].data, depth);
566 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
570 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
572 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
573 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
576 for (b = 0; b < 255; b++)
578 if (src_16->mask_24[b])
580 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
582 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
583 ipt_account_data_free(ipt_account_handles[handle].data, depth);
584 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
588 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
595 *count = ipt_account_tables[table_nr].itemcount;
599 /* Prepare data for read and flush it */
600 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count)
602 int handle, i, table_nr=-1;
604 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
605 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
613 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
617 // Can't find a free handle slot?
618 if ((handle = ipt_account_handle_find_slot()) == -1)
621 // Fill up handle structure
622 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
623 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
624 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
625 ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
626 *count = ipt_account_tables[table_nr].itemcount;
628 // "Flush" table data
629 ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL);
630 ipt_account_tables[table_nr].itemcount = 0;
635 /* Copy the actual that into a prepared buffer.
636 We only copy entries != 0 to increase performance.
637 The memory gets freed again in ipt_account_handle_free().
639 int ipt_account_handle_get_data(unsigned int handle, void *buffer)
641 struct ipt_account_handle_ip handle_ip;
642 unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
645 if (handle >= ACCOUNT_MAX_HANDLES)
647 printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
651 if (ipt_account_handles[handle].data == NULL)
653 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
657 // Check if at least packet src/dst matches ip/netmask
658 unsigned int net_ip = ipt_account_handles[handle].ip;
659 unsigned int depth = ipt_account_handles[handle].depth;
664 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
665 for (i = 0; i < 255; i++)
667 if (network->ip[i].src_packets || network->ip[i].dst_packets)
669 handle_ip.ip = net_ip | (i<<24);
670 handle_ip.src_packets = network->ip[i].src_packets;
671 handle_ip.src_bytes = network->ip[i].src_bytes;
672 handle_ip.dst_packets = network->ip[i].dst_packets;
673 handle_ip.dst_bytes = network->ip[i].dst_bytes;
675 copy_to_user(buffer, &handle_ip, handle_ip_size);
676 buffer += handle_ip_size;
686 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
688 for (b = 0; b < 255; b++)
690 if (network_16->mask_24[b])
692 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
693 for (i = 0; i < 255; i++)
695 if (network->ip[i].src_packets || network->ip[i].dst_packets)
697 handle_ip.ip = net_ip | (b << 16) | (i<<24);
698 handle_ip.src_packets = network->ip[i].src_packets;
699 handle_ip.src_bytes = network->ip[i].src_bytes;
700 handle_ip.dst_packets = network->ip[i].dst_packets;
701 handle_ip.dst_bytes = network->ip[i].dst_bytes;
703 copy_to_user(buffer, &handle_ip, handle_ip_size);
704 buffer += handle_ip_size;
716 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
718 for (a = 0; a < 255; a++)
720 if (network_8->mask_16[a])
722 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
723 for (b = 0; b < 255; b++)
725 if (network_16->mask_24[b])
727 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
728 for (i = 0; i < 255; i++)
730 if (network->ip[i].src_packets || network->ip[i].dst_packets)
732 handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24);
733 handle_ip.src_packets = network->ip[i].src_packets;
734 handle_ip.src_bytes = network->ip[i].src_bytes;
735 handle_ip.dst_packets = network->ip[i].dst_packets;
736 handle_ip.dst_bytes = network->ip[i].dst_bytes;
738 copy_to_user(buffer, &handle_ip, handle_ip_size);
739 buffer += handle_ip_size;
753 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
755 struct ipt_account_handle_sockopt handle;
758 if (!capable(CAP_NET_ADMIN))
763 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
764 if (len != sizeof(struct ipt_account_handle_sockopt))
766 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));
770 if (copy_from_user (&handle, user, len))
772 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
776 spin_lock_bh(&ipt_account_lock);
777 ret = ipt_account_handle_free(handle.handle_nr);
778 spin_unlock_bh(&ipt_account_lock);
781 printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
787 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len)
789 struct ipt_account_handle_sockopt handle;
792 if (!capable(CAP_NET_ADMIN))
797 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
798 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
799 if (*len < sizeof(struct ipt_account_handle_sockopt))
801 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
802 *len, sizeof(struct ipt_account_handle_sockopt));
806 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
808 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
812 spin_lock_bh(&ipt_account_lock);
813 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
814 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
816 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
817 spin_unlock_bh(&ipt_account_lock);
819 if (handle.handle_nr == -1)
821 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n");
825 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt)))
827 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
832 case IPT_SO_GET_ACCOUNT_GET_DATA:
833 if (*len < sizeof(struct ipt_account_handle_sockopt))
835 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
836 *len, sizeof(struct ipt_account_handle_sockopt));
840 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
842 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
846 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES)
848 printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr);
852 if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip))
854 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
855 *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
859 spin_lock_bh(&ipt_account_lock);
860 ret = ipt_account_handle_get_data(handle.handle_nr, user);
861 spin_unlock_bh(&ipt_account_lock);
864 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
872 printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
878 static struct ipt_target ipt_account_reg
879 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
882 static struct nf_sockopt_ops ipt_account_sockopts
883 = { { NULL, NULL }, PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
884 IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL };
886 static int __init init(void)
888 if (PAGE_SIZE < 4096)
890 printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE);
894 if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)))
896 printk("ACCOUNT: Out of memory allocating account_tables structure");
899 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
901 if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)))
903 printk("ACCOUNT: Out of memory allocating account_handles structure");
904 kfree (ipt_account_tables);
905 ipt_account_tables = NULL;
908 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
910 /* Register setsockopt */
911 if (nf_register_sockopt(&ipt_account_sockopts) < 0)
913 printk("ACCOUNT: Can't register sockopts. Aborting\n");
915 kfree (ipt_account_tables);
916 ipt_account_tables = NULL;
917 kfree(ipt_account_handles);
918 ipt_account_handles = NULL;
923 if (ipt_register_target(&ipt_account_reg))
929 static void __exit fini(void)
931 ipt_unregister_target(&ipt_account_reg);
933 nf_unregister_sockopt(&ipt_account_sockopts);
935 kfree(ipt_account_tables);
936 ipt_account_tables = NULL;
938 kfree(ipt_account_handles);
939 ipt_account_handles = NULL;
944 MODULE_LICENSE("GPL");