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
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;
297 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask,
298 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
300 // Do we need to process src IP?
301 if ((net_ip&netmask) == (src_ip&netmask))
303 unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
304 DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
306 // Do we need to create a new mask_24 bucket?
307 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
309 printk("ACCOUNT: Can't process packet because out of memory!\n");
313 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
314 src_ip, dst_ip, size, itemcount);
317 // Do we need to process dst IP?
318 if ((net_ip&netmask) == (dst_ip&netmask))
320 unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
321 DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
323 // Do we need to create a new mask_24 bucket?
324 if (!mask_16->mask_24[slot] && !(mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
326 printk("ACCOUT: Can't process packet because out of memory!\n");
330 ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
331 src_ip, dst_ip, size, itemcount);
335 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask,
336 unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
338 // Do we need to process src IP?
339 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)))
347 printk("ACCOUNT: Can't process packet because out of memory!\n");
351 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
352 src_ip, dst_ip, size, itemcount);
355 // Do we need to process dst IP?
356 if ((net_ip&netmask) == (dst_ip&netmask))
358 unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
359 DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
361 // Do we need to create a new mask_24 bucket?
362 if (!mask_8->mask_16[slot] && !(mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)))
364 printk("ACCOUNT: Can't process packet because out of memory!\n");
368 ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
369 src_ip, dst_ip, size, itemcount);
373 static unsigned int ipt_account_target(struct sk_buff **pskb,
374 unsigned int hooknum,
375 const struct net_device *in,
376 const struct net_device *out,
377 const void *targinfo,
380 const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
381 unsigned int src_ip = (*pskb)->nh.iph->saddr;
382 unsigned int dst_ip = (*pskb)->nh.iph->daddr;
383 unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
385 spin_lock_bh(&ipt_account_lock);
387 if (ipt_account_tables[info->table_nr].name[0] == 0)
389 printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
390 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
391 spin_unlock_bh(&ipt_account_lock);
395 // 8 bit network or "any" network
396 if (ipt_account_tables[info->table_nr].depth == 0)
398 // Count packet and check if the IP is new
399 ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
400 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
401 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
402 spin_unlock_bh(&ipt_account_lock);
407 if (ipt_account_tables[info->table_nr].depth == 1)
409 ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
410 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
411 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
412 spin_unlock_bh(&ipt_account_lock);
417 if (ipt_account_tables[info->table_nr].depth == 2)
419 ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
420 ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
421 src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
422 spin_unlock_bh(&ipt_account_lock);
426 printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
427 info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
429 spin_unlock_bh(&ipt_account_lock);
434 Functions dealing with "handles":
435 Handles are snapshots of a accounting state.
437 read snapshots are only for debugging the code
438 and are very expensive concerning speed/memory
439 compared to read_and_flush.
441 The functions aren't protected by spinlocks themselves
442 as this is done in the ioctl part of the code.
446 Find a free handle slot. Normally only one should be used,
447 but there could be two or more applications accessing the data
450 int ipt_account_handle_find_slot(void)
454 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
457 if (ipt_account_handles[i].data == NULL)
459 // Don't "mark" data as used as we are protected by a spinlock by the calling function.
460 // handle_find_slot() is only a function to prevent code duplication.
465 // No free slot found
466 printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
470 int ipt_account_handle_free(unsigned int handle)
472 if (handle >= ACCOUNT_MAX_HANDLES)
474 printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle);
478 ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth);
479 memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle));
483 /* Prepare data for read without flush. Use only for debugging!
484 Real applications should use read&flush as it's way more efficent */
485 int ipt_account_handle_prepare_read(char *tablename, unsigned int *count)
487 int handle, i, table_nr=-1;
489 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
490 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
498 printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
502 // Can't find a free handle slot?
503 if ((handle = ipt_account_handle_find_slot()) == -1)
506 // Fill up handle structure
507 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
508 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
509 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
511 // allocate "root" table
512 if (!(ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL)))
514 printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
515 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
519 // Recursive copy of complete data structure
520 unsigned int depth = ipt_account_handles[handle].depth;
523 memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
524 } else if (depth == 1) {
525 struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
526 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
529 for (b = 0; b < 255; b++)
531 if (src_16->mask_24[b])
533 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
535 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
536 ipt_account_data_free(ipt_account_handles[handle].data, depth);
537 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
541 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
544 } else if(depth == 2) {
545 struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
546 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
549 for (a = 0; a < 255; a++)
551 if (src_8->mask_16[a])
553 if (!(network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL)))
555 printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
556 ipt_account_data_free(ipt_account_handles[handle].data, depth);
557 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
561 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
563 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
564 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
567 for (b = 0; b < 255; b++)
569 if (src_16->mask_24[b])
571 if (!(network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)))
573 printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
574 ipt_account_data_free(ipt_account_handles[handle].data, depth);
575 memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
579 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
586 *count = ipt_account_tables[table_nr].itemcount;
590 /* Prepare data for read and flush it */
591 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count)
593 int handle, i, table_nr=-1;
595 for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
596 if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
604 printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
608 // Can't find a free handle slot?
609 if ((handle = ipt_account_handle_find_slot()) == -1)
612 // Fill up handle structure
613 ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
614 ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
615 ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
616 ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
617 *count = ipt_account_tables[table_nr].itemcount;
619 // "Flush" table data
620 ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL);
621 ipt_account_tables[table_nr].itemcount = 0;
626 /* Copy the actual that into a prepared buffer.
627 We only copy entries != 0 to increase performance.
628 The memory gets freed again in ipt_account_handle_free().
630 int ipt_account_handle_get_data(unsigned int handle, void *buffer)
632 struct ipt_account_handle_ip handle_ip;
633 unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
636 if (handle >= ACCOUNT_MAX_HANDLES)
638 printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
642 if (ipt_account_handles[handle].data == NULL)
644 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
648 // Check if at least packet src/dst matches ip/netmask
649 unsigned int net_ip = ipt_account_handles[handle].ip;
650 unsigned int depth = ipt_account_handles[handle].depth;
655 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
656 for (i = 0; i < 255; i++)
658 if (network->ip[i].src_packets || network->ip[i].dst_packets)
660 handle_ip.ip = net_ip | (i<<24);
661 handle_ip.src_packets = network->ip[i].src_packets;
662 handle_ip.src_bytes = network->ip[i].src_bytes;
663 handle_ip.dst_packets = network->ip[i].dst_packets;
664 handle_ip.dst_bytes = network->ip[i].dst_bytes;
666 copy_to_user(buffer, &handle_ip, handle_ip_size);
667 buffer += handle_ip_size;
677 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
679 for (b = 0; b < 255; b++)
681 if (network_16->mask_24[b])
683 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
684 for (i = 0; i < 255; i++)
686 if (network->ip[i].src_packets || network->ip[i].dst_packets)
688 handle_ip.ip = net_ip | (b << 16) | (i<<24);
689 handle_ip.src_packets = network->ip[i].src_packets;
690 handle_ip.src_bytes = network->ip[i].src_bytes;
691 handle_ip.dst_packets = network->ip[i].dst_packets;
692 handle_ip.dst_bytes = network->ip[i].dst_bytes;
694 copy_to_user(buffer, &handle_ip, handle_ip_size);
695 buffer += handle_ip_size;
707 struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
709 for (a = 0; a < 255; a++)
711 if (network_8->mask_16[a])
713 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
714 for (b = 0; b < 255; b++)
716 if (network_16->mask_24[b])
718 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
719 for (i = 0; i < 255; i++)
721 if (network->ip[i].src_packets || network->ip[i].dst_packets)
723 handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24);
724 handle_ip.src_packets = network->ip[i].src_packets;
725 handle_ip.src_bytes = network->ip[i].src_bytes;
726 handle_ip.dst_packets = network->ip[i].dst_packets;
727 handle_ip.dst_bytes = network->ip[i].dst_bytes;
729 copy_to_user(buffer, &handle_ip, handle_ip_size);
730 buffer += handle_ip_size;
744 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
746 struct ipt_account_handle_sockopt handle;
749 if (!capable(CAP_NET_ADMIN))
754 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
755 if (len != sizeof(struct ipt_account_handle_sockopt))
757 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));
761 if (copy_from_user (&handle, user, len))
763 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
767 spin_lock_bh(&ipt_account_lock);
768 ret = ipt_account_handle_free(handle.handle_nr);
769 spin_unlock_bh(&ipt_account_lock);
772 printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
778 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len)
780 struct ipt_account_handle_sockopt handle;
783 if (!capable(CAP_NET_ADMIN))
788 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
789 case IPT_SO_GET_ACCOUNT_PREPARE_READ:
790 if (*len < sizeof(struct ipt_account_handle_sockopt))
792 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
793 *len, sizeof(struct ipt_account_handle_sockopt));
797 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
799 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
803 spin_lock_bh(&ipt_account_lock);
804 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
805 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
807 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
808 spin_unlock_bh(&ipt_account_lock);
810 if (handle.handle_nr == -1)
812 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n");
816 if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt)))
818 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
823 case IPT_SO_GET_ACCOUNT_GET_DATA:
824 if (*len < sizeof(struct ipt_account_handle_sockopt))
826 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
827 *len, sizeof(struct ipt_account_handle_sockopt));
831 if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
833 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
837 if (handle.handle_nr >= ACCOUNT_MAX_HANDLES)
839 printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr);
843 if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip))
845 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
846 *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
850 spin_lock_bh(&ipt_account_lock);
851 ret = ipt_account_handle_get_data(handle.handle_nr, user);
852 spin_unlock_bh(&ipt_account_lock);
855 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
863 printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
869 static struct ipt_target ipt_account_reg
870 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
873 static struct nf_sockopt_ops ipt_account_sockopts
874 = { { NULL, NULL }, PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
875 IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL };
877 static int __init init(void)
879 if (PAGE_SIZE < 4096)
881 printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE);
885 if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)))
887 printk("ACCOUNT: Out of memory allocating account_tables structure");
890 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
892 if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)))
894 printk("ACCOUNT: Out of memory allocating account_handles structure");
895 kfree (ipt_account_tables);
896 ipt_account_tables = NULL;
899 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
901 /* Register setsockopt */
902 if (nf_register_sockopt(&ipt_account_sockopts) < 0)
904 printk("ACCOUNT: Can't register sockopts. Aborting\n");
906 kfree (ipt_account_tables);
907 ipt_account_tables = NULL;
908 kfree(ipt_account_handles);
909 ipt_account_handles = NULL;
914 if (ipt_register_target(&ipt_account_reg))
920 static void __exit fini(void)
922 ipt_unregister_target(&ipt_account_reg);
924 nf_unregister_sockopt(&ipt_account_sockopts);
926 kfree(ipt_account_tables);
927 ipt_account_tables = NULL;
929 kfree(ipt_account_handles);
930 ipt_account_handles = NULL;
935 MODULE_LICENSE("GPL");