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 (strcmp(ipt_account_tables[i].name, name) == 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 (strcmp(ipt_account_tables[i].name, info->table_name) == 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);
433 static struct ipt_target ipt_account_reg
434 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry,
437 static int __init init(void)
439 if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)))
441 printk("ACCOUNT: Out of memory allocating account_tables structure");
444 memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
446 if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)))
448 printk("ACCOUNT: Out of memory allocating account_handles structure");
449 kfree (ipt_account_tables);
450 ipt_account_tables = NULL;
453 memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
455 if (ipt_register_target(&ipt_account_reg))
461 static void __exit fini(void)
463 ipt_unregister_target(&ipt_account_reg);
465 kfree(ipt_account_tables);
466 ipt_account_tables = NULL;
468 kfree(ipt_account_handles);
469 ipt_account_handles = NULL;
474 MODULE_LICENSE("GPL");