b9addf2ab482af730f38614b18b99d88099fca98
[ipt_ACCOUNT] / linux / net / ipv4 / netfilter / ipt_ACCOUNT.c
1 /*
2  * This is a module which is used for counting packets.
3  */
4 #include <linux/module.h>
5 #include <linux/skbuff.h>
6 #include <linux/ip.h>
7 #include <linux/spinlock.h>
8 #include <net/icmp.h>
9 #include <net/udp.h>
10 #include <net/tcp.h>
11 #include <linux/netfilter_ipv4/ip_tables.h>
12
13 struct in_device;
14 #include <net/route.h>
15 #include <linux/netfilter_ipv4/ipt_ACCOUNT.h>
16
17 //#if 0
18 #define DEBUGP printk
19 //#else
20 //#define DEBUGP(format, args...)
21 //#endif
22
23 struct ipt_account_table *ipt_account_tables = NULL;
24 struct ipt_account_handle *ipt_account_handles = NULL;
25
26 static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED;
27
28 /* Recursive free of all data structures */
29 void ipt_account_data_free(void *data, unsigned char depth)
30 {
31     // Empty data set
32     if (!data)
33         return;
34         
35     // Free for 8 bit network
36     if (depth == 0)
37     {
38         free_page((unsigned long)data);
39         data = NULL;
40         return;
41     }
42     
43     // Free for 16 bit network
44     if (depth == 1)
45     {
46         struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data;
47         unsigned char b;
48         for (b=0; b < 255; b++)
49         {
50             if (mask_16->mask_24[b] != 0)
51             {
52                 free_page((unsigned long)mask_16->mask_24[b]);
53                 mask_16->mask_24[b] = NULL;
54             }
55         }
56         free_page((unsigned long)data);
57         data = NULL;
58         return;
59     } 
60    
61     // Free for 24 bit network
62     if (depth == 3)
63     {
64         unsigned char a, b;
65         for (a=0; a < 255; a++)
66         {
67             if (((struct ipt_account_mask_8 *)data)->mask_16[a])
68             {
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++)
71                 {
72                     if (mask_16->mask_24[b]) {
73                         free_page((unsigned long)mask_16->mask_24[b]);
74                         mask_16->mask_24[b] = NULL;
75                     }
76                 }
77                 free_page((unsigned long)mask_16);
78                 mask_16 = NULL;
79             }
80         }
81         free_page((unsigned long)data);
82         data = NULL;
83         return;
84     }
85     
86     printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth);
87     return;
88 }
89
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)
92 {
93     unsigned int i;
94
95     DEBUGP("ACCOUNT: ipt_account_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", name, NIPQUAD(ip), NIPQUAD(netmask));
96
97     // Look for existing table
98     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
99     {
100         if (strcmp(ipt_account_tables[i].name, name) == 0)
101         {
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));
104             
105             if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask)
106             {
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));
109                 return -1;
110             }
111
112             ipt_account_tables[i].refcount++;
113             DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount);
114             return i;
115         }
116     }
117
118     // Insert new table
119     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
120     {
121         // Found free slot
122         if (ipt_account_tables[i].name[0] == 0)
123         {
124             DEBUGP("ACCOUNT: Found free slot: %d\n", i);
125         
126             strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
127             
128             ipt_account_tables[i].ip = ip;
129             ipt_account_tables[i].netmask = netmask;
130             
131             // Calculate netsize
132             unsigned int j, calc_mask, netsize=0;
133             calc_mask = htonl(netmask);
134             for (j = 31; j > 0; j--)
135             {
136                 if (calc_mask&(1<<j))
137                     netsize++;
138                 else
139                     break;
140             }
141             
142             // Calculate depth from netsize
143             if (netsize >= 24)
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;
149             
150             printk("ACCOUNT: calculated netsize: %u -> ipt_account_table depth %u\n", netsize, ipt_account_tables[i].depth);
151                         
152             ipt_account_tables[i].refcount++;
153             if (!(ipt_account_tables[i].data = (void *)get_zeroed_page(GFP_KERNEL)))
154             {
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));
157                 return -1;
158             }
159             
160             return i;
161         }
162     }
163         
164     // No free slot found
165     printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
166     return -1;
167 }
168
169 static int ipt_account_checkentry(const char *tablename,
170     const struct ipt_entry *e,
171     void *targinfo,
172     unsigned int targinfosize,
173     unsigned int hook_mask)
174 {
175     struct ipt_account_info *info = targinfo;
176     
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)));
180         return 0;
181     }
182
183     spin_lock_bh(&ipt_account_lock);
184     int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask);
185     if (table_nr == -1)
186     {
187         printk("ACCOUNT: Table insert problem. Aborting\n");
188         spin_unlock_bh(&ipt_account_lock);
189         return 0;
190     }
191     // Table nr caching so we don't have to do an extra string compare for every packet
192     info->table_nr = table_nr;
193     
194     spin_unlock_bh(&ipt_account_lock);
195
196     return 1;
197 }
198
199 void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize)
200 {
201     unsigned int i;
202     struct ipt_account_info *info = targinfo;
203     
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)));
207     }
208
209     spin_lock_bh(&ipt_account_lock);
210     
211     DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr);
212     
213     info->table_nr = -1;    // Set back to original state
214     
215     // Look for table
216     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
217     {
218         if (strcmp(ipt_account_tables[i].name, info->table_name) == 0)
219         {
220             DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
221             
222             ipt_account_tables[i].refcount--;
223             DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount);
224
225             // Table not needed anymore?
226             if (ipt_account_tables[i].refcount == 0)
227             {
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));
231             }
232             
233             spin_unlock_bh(&ipt_account_lock);
234             return;
235         }
236     }
237
238     // Table not found
239     printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
240     spin_unlock_bh(&ipt_account_lock);
241 }
242
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)
245 {
246     unsigned char is_src = 0, is_dst = 0;
247     
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);
250         
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
253     if (!netmask)
254         src_ip = 0;
255     if ((net_ip&netmask) == (src_ip&netmask))
256         is_src = 1;
257     if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
258         is_dst = 1;
259     
260     if (!is_src && !is_dst)
261     {
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));
264         return;
265     }
266         
267     // Check if this entry is new
268     char is_new_ip = 0;
269     
270     // Increase size counters
271     if (is_src)
272     {
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)
277             is_new_ip = 1;
278         
279         mask_24->ip[slot].src_packets++;
280         mask_24->ip[slot].src_bytes+=size;
281     }
282     if (is_dst)
283     {
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)
287             is_new_ip = 1;
288         
289         mask_24->ip[slot].dst_packets++;
290         mask_24->ip[slot].dst_bytes+=size;
291     }
292     
293     if (is_new_ip)
294         (*itemcount)++;
295 }
296
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)
299 {
300     // Do we need to process src IP?
301     if ((net_ip&netmask) == (src_ip&netmask))
302     {
303         unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
304         DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
305         
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)))
308         {
309             printk("ACCOUNT: Can't process packet because out of memory!\n");
310             return;
311         }
312         
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);
315     }
316     
317     // Do we need to process dst IP?
318     if ((net_ip&netmask) == (dst_ip&netmask))
319     {
320         unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
321         DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
322         
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)))
325         {
326             printk("ACCOUT: Can't process packet because out of memory!\n");
327             return;
328         }
329         
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);
332     }
333 }
334
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)
337 {
338     // Do we need to process src IP?
339     if ((net_ip&netmask) == (src_ip&netmask))
340     {
341         unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
342         DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
343                 
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)))
346         {
347             printk("ACCOUNT: Can't process packet because out of memory!\n");
348             return;
349         }
350         
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);
353     }
354     
355     // Do we need to process dst IP?
356     if ((net_ip&netmask) == (dst_ip&netmask))
357     {
358         unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
359         DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
360         
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)))
363         {
364             printk("ACCOUNT: Can't process packet because out of memory!\n");
365             return;
366         }
367         
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);
370     }
371 }
372
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,
378     void *userinfo)
379 {
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);
384     
385     spin_lock_bh(&ipt_account_lock);
386     
387     if (ipt_account_tables[info->table_nr].name[0] == 0)
388     {
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);
392         return IPT_CONTINUE;
393     }
394     
395     // 8 bit network or "any" network
396     if (ipt_account_tables[info->table_nr].depth == 0)
397     {
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);
403         return IPT_CONTINUE;
404     }    
405     
406     // 16 bit network
407     if (ipt_account_tables[info->table_nr].depth == 1)
408     {
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);
413         return IPT_CONTINUE;
414     }
415     
416     // 24 bit network
417     if (ipt_account_tables[info->table_nr].depth == 2)
418     {
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);
423         return IPT_CONTINUE;
424     }
425     
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));
428     
429     spin_unlock_bh(&ipt_account_lock);
430     return IPT_CONTINUE;
431 }
432
433 static struct ipt_target ipt_account_reg
434 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry, 
435     THIS_MODULE };
436
437 static int __init init(void)
438 {
439     if (!(ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)))
440     {
441             printk("ACCOUNT: Out of memory allocating account_tables structure");
442             return -EINVAL;
443     }
444     memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
445                             
446     if (!(ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)))
447     {
448             printk("ACCOUNT: Out of memory allocating account_handles structure");
449             kfree (ipt_account_tables);
450             ipt_account_tables = NULL;
451             return -EINVAL;
452     }
453     memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
454     
455     if (ipt_register_target(&ipt_account_reg))
456             return -EINVAL;
457
458     return 0;
459 }
460
461 static void __exit fini(void)
462 {
463     ipt_unregister_target(&ipt_account_reg);
464
465     kfree(ipt_account_tables);
466     ipt_account_tables = NULL;
467         
468     kfree(ipt_account_handles);
469     ipt_account_handles = NULL;
470 }
471
472 module_init(init);
473 module_exit(fini);
474 MODULE_LICENSE("GPL");