full pom-ng suite for ipt_ACCOUNT
[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 void *ipt_account_tmpbuf = NULL;
26
27 // Spinlock used for manipulating the current accounting tables/data
28 static spinlock_t ipt_account_lock = SPIN_LOCK_UNLOCKED;
29 // Spinlock used for manipulating userspace handles/snapshot data
30 static spinlock_t ipt_account_userspace_lock = SPIN_LOCK_UNLOCKED;
31
32
33 /* Recursive free of all data structures */
34 void ipt_account_data_free(void *data, unsigned char depth)
35 {
36     // Empty data set
37     if (!data)
38         return;
39         
40     // Free for 8 bit network
41     if (depth == 0)
42     {
43         free_page((unsigned long)data);
44         data = NULL;
45         return;
46     }
47     
48     // Free for 16 bit network
49     if (depth == 1)
50     {
51         struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16 *)data;
52         unsigned int b;
53         for (b=0; b <= 255; b++)
54         {
55             if (mask_16->mask_24[b] != 0)
56             {
57                 free_page((unsigned long)mask_16->mask_24[b]);
58                 mask_16->mask_24[b] = NULL;
59             }
60         }
61         free_page((unsigned long)data);
62         data = NULL;
63         return;
64     } 
65    
66     // Free for 24 bit network
67     if (depth == 3)
68     {
69         unsigned int a, b;
70         for (a=0; a <= 255; a++)
71         {
72             if (((struct ipt_account_mask_8 *)data)->mask_16[a])
73             {
74                 struct ipt_account_mask_16 *mask_16 = (struct ipt_account_mask_16*)((struct ipt_account_mask_8 *)data)->mask_16[a];
75                 for (b=0; b <= 255; b++)
76                 {
77                     if (mask_16->mask_24[b]) {
78                         free_page((unsigned long)mask_16->mask_24[b]);
79                         mask_16->mask_24[b] = NULL;
80                     }
81                 }
82                 free_page((unsigned long)mask_16);
83                 mask_16 = NULL;
84             }
85         }
86         free_page((unsigned long)data);
87         data = NULL;
88         return;
89     }
90     
91     printk("ACCOUNT: ipt_account_data_free called with unknown depth: %d\n", depth);
92     return;
93 }
94
95 /* Look for existing table / insert new one. Return internal ID or -1 on error */
96 int ipt_account_table_insert(char *name, unsigned int ip, unsigned int netmask)
97 {
98     unsigned int i;
99
100     DEBUGP("ACCOUNT: ipt_account_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", name, NIPQUAD(ip), NIPQUAD(netmask));
101
102     // Look for existing table
103     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
104     {
105         if (strncmp(ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN) == 0)
106         {
107             DEBUGP("ACCOUNT: Found existing slot: %d - %u.%u.%u.%u/%u.%u.%u.%u\n", i,
108                    NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
109             
110             if (ipt_account_tables[i].ip != ip || ipt_account_tables[i].netmask != netmask)
111             {
112                 printk("ACCOUNT: Table %s found, but IP/netmask mismatch. IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n",
113                         name, NIPQUAD(ipt_account_tables[i].ip), NIPQUAD(ipt_account_tables[i].netmask));
114                 return -1;
115             }
116
117             ipt_account_tables[i].refcount++;
118             DEBUGP("ACCOUNT: Refcount: %d\n", ipt_account_tables[i].refcount);
119             return i;
120         }
121     }
122
123     // Insert new table
124     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
125     {
126         // Found free slot
127         if (ipt_account_tables[i].name[0] == 0)
128         {
129             DEBUGP("ACCOUNT: Found free slot: %d\n", i);
130         
131             strncpy (ipt_account_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
132             
133             ipt_account_tables[i].ip = ip;
134             ipt_account_tables[i].netmask = netmask;
135             
136             // Calculate netsize
137             unsigned int j, calc_mask, netsize=0;
138             calc_mask = htonl(netmask);
139             for (j = 31; j > 0; j--)
140             {
141                 if (calc_mask&(1<<j))
142                     netsize++;
143                 else
144                     break;
145             }
146             
147             // Calculate depth from netsize
148             if (netsize >= 24)
149                 ipt_account_tables[i].depth = 0;
150             else if (netsize >= 16)
151                 ipt_account_tables[i].depth = 1;
152             else if(netsize >= 8)
153                 ipt_account_tables[i].depth = 2;
154             
155             printk("ACCOUNT: calculated netsize: %u -> ipt_account_table depth %u\n", netsize, ipt_account_tables[i].depth);
156                         
157             ipt_account_tables[i].refcount++;
158             if ((ipt_account_tables[i].data = (void *)get_zeroed_page(GFP_KERNEL)) == NULL)
159             {
160                 printk("ACCOUNT: out of memory for data of table: %s\n", name);
161                 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
162                 return -1;
163             }
164             
165             return i;
166         }
167     }
168         
169     // No free slot found
170     printk("ACCOUNT: No free table slot found (max: %d). Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
171     return -1;
172 }
173
174 static int ipt_account_checkentry(const char *tablename,
175     const struct ipt_entry *e,
176     void *targinfo,
177     unsigned int targinfosize,
178     unsigned int hook_mask)
179 {
180     struct ipt_account_info *info = targinfo;
181     
182     if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
183         DEBUGP("ACCOUNT: targinfosize %u != %u\n",
184                 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
185         return 0;
186     }
187
188     spin_lock_bh(&ipt_account_lock);
189     int table_nr = ipt_account_table_insert(info->table_name, info->net_ip, info->net_mask);
190     if (table_nr == -1)
191     {
192         printk("ACCOUNT: Table insert problem. Aborting\n");
193         spin_unlock_bh(&ipt_account_lock);
194         return 0;
195     }
196     // Table nr caching so we don't have to do an extra string compare for every packet
197     info->table_nr = table_nr;
198     
199     spin_unlock_bh(&ipt_account_lock);
200
201     return 1;
202 }
203
204 void ipt_account_deleteentry(void *targinfo, unsigned int targinfosize)
205 {
206     unsigned int i;
207     struct ipt_account_info *info = targinfo;
208     
209     if (targinfosize != IPT_ALIGN(sizeof(struct ipt_account_info))) {
210         DEBUGP("ACCOUNT: targinfosize %u != %u\n",
211                 targinfosize, IPT_ALIGN(sizeof(struct ipt_account_info)));
212     }
213
214     spin_lock_bh(&ipt_account_lock);
215     
216     DEBUGP("ACCOUNT: ipt_account_deleteentry called for table: %s (#%d)\n", info->table_name, info->table_nr);
217     
218     info->table_nr = -1;    // Set back to original state
219     
220     // Look for table
221     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
222     {
223         if (strncmp(ipt_account_tables[i].name, info->table_name, ACCOUNT_TABLE_NAME_LEN) == 0)
224         {
225             DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
226             
227             ipt_account_tables[i].refcount--;
228             DEBUGP("ACCOUNT: Refcount left: %d\n", ipt_account_tables[i].refcount);
229
230             // Table not needed anymore?
231             if (ipt_account_tables[i].refcount == 0)
232             {
233                 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
234                 ipt_account_data_free(ipt_account_tables[i].data, ipt_account_tables[i].depth);
235                 memset(&ipt_account_tables[i], 0, sizeof(struct ipt_account_table));
236             }
237             
238             spin_unlock_bh(&ipt_account_lock);
239             return;
240         }
241     }
242
243     // Table not found
244     printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
245     spin_unlock_bh(&ipt_account_lock);
246 }
247
248 void ipt_account_depth0_insert(struct ipt_account_mask_24 *mask_24, unsigned int net_ip, unsigned int netmask,
249                                unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
250 {
251     unsigned char is_src = 0, is_dst = 0;
252     
253     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",
254             NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
255         
256     // Check if src/dst is inside our network.
257     // Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0
258     if (!netmask)
259         src_ip = 0;
260     if ((net_ip&netmask) == (src_ip&netmask))
261         is_src = 1;
262     if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
263         is_dst = 1;
264     
265     if (!is_src && !is_dst)
266     {
267         DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u for net %u.%u.%u.%u/%u.%u.%u.%u\n",
268                 NIPQUAD(src_ip), NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
269         return;
270     }
271         
272     // Check if this entry is new
273     char is_src_new_ip = 0, is_dst_new_ip = 0;
274
275     // Calculate array positions
276     unsigned char src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
277     unsigned char dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
278     
279     // Increase size counters
280     if (is_src)
281     {
282         // Calculate network slot
283         DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
284         if (!mask_24->ip[src_slot].src_packets && !mask_24->ip[src_slot].dst_packets)
285             is_src_new_ip = 1;
286         
287         mask_24->ip[src_slot].src_packets++;
288         mask_24->ip[src_slot].src_bytes+=size;
289     }
290     if (is_dst)
291     {
292         DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
293         if (!mask_24->ip[dst_slot].src_packets && !mask_24->ip[dst_slot].dst_packets)
294             is_dst_new_ip = 1;
295         
296         mask_24->ip[dst_slot].dst_packets++;
297         mask_24->ip[dst_slot].dst_bytes+=size;
298     }
299     
300     // Increase itemcounter
301     DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
302     if (src_slot == dst_slot)
303     {
304         if (is_src_new_ip || is_dst_new_ip) {
305             DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n", is_src_new_ip, is_dst_new_ip);
306             (*itemcount)++;
307         }
308     } else {
309         if (is_src_new_ip) {
310             DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
311             (*itemcount)++;
312         }
313         if (is_dst_new_ip) {
314             DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
315             (*itemcount)++;
316         }
317     }
318     DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
319 }
320
321 void ipt_account_depth1_insert(struct ipt_account_mask_16 *mask_16, unsigned int net_ip, unsigned int netmask,
322                                unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
323 {
324     // Do we need to process src IP?
325     if ((net_ip&netmask) == (src_ip&netmask))
326     {
327         unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
328         DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
329         
330         // Do we need to create a new mask_24 bucket?
331         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL)
332         {
333             printk("ACCOUNT: Can't process packet because out of memory!\n");
334             return;
335         }
336         
337         ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
338                                     src_ip, 0, size, itemcount);
339     }
340     
341     // Do we need to process dst IP?
342     if ((net_ip&netmask) == (dst_ip&netmask))
343     {
344         unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
345         DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
346         
347         // Do we need to create a new mask_24 bucket?
348         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL)
349         {
350             printk("ACCOUT: Can't process packet because out of memory!\n");
351             return;
352         }
353         
354         ipt_account_depth0_insert((struct ipt_account_mask_24 *)mask_16->mask_24[slot], net_ip, netmask,
355                                     0, dst_ip, size, itemcount);
356     }
357 }
358
359 void ipt_account_depth2_insert(struct ipt_account_mask_8 *mask_8, unsigned int net_ip, unsigned int netmask,
360                                unsigned int src_ip, unsigned int dst_ip, unsigned int size, unsigned int *itemcount)
361 {
362     // Do we need to process src IP?
363     if ((net_ip&netmask) == (src_ip&netmask))
364     {
365         unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
366         DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
367                 
368         // Do we need to create a new mask_24 bucket?
369         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL)
370         {
371             printk("ACCOUNT: Can't process packet because out of memory!\n");
372             return;
373         }
374         
375         ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
376                                     src_ip, 0, size, itemcount);
377     }
378     
379     // Do we need to process dst IP?
380     if ((net_ip&netmask) == (dst_ip&netmask))
381     {
382         unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
383         DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
384         
385         // Do we need to create a new mask_24 bucket?
386         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] = (void *)get_zeroed_page(GFP_KERNEL)) == NULL)
387         {
388             printk("ACCOUNT: Can't process packet because out of memory!\n");
389             return;
390         }
391         
392         ipt_account_depth1_insert((struct ipt_account_mask_16 *)mask_8->mask_16[slot], net_ip, netmask,
393                                     0, dst_ip, size, itemcount);
394     }
395 }
396
397 static unsigned int ipt_account_target(struct sk_buff **pskb,
398     unsigned int hooknum,
399     const struct net_device *in,
400     const struct net_device *out,
401     const void *targinfo,
402     void *userinfo)
403 {
404     const struct ipt_account_info *info = (const struct ipt_account_info *)targinfo;
405     unsigned int src_ip = (*pskb)->nh.iph->saddr;
406     unsigned int dst_ip = (*pskb)->nh.iph->daddr;
407     unsigned int size = ntohs((*pskb)->nh.iph->tot_len);
408     
409     spin_lock_bh(&ipt_account_lock);
410     
411     if (ipt_account_tables[info->table_nr].name[0] == 0)
412     {
413         printk("ACCOUNT: ipt_account_target: Invalid table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
414                info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
415         spin_unlock_bh(&ipt_account_lock);
416         return IPT_CONTINUE;
417     }
418     
419     // 8 bit network or "any" network
420     if (ipt_account_tables[info->table_nr].depth == 0)
421     {
422         // Count packet and check if the IP is new
423         ipt_account_depth0_insert((struct ipt_account_mask_24 *)ipt_account_tables[info->table_nr].data,
424                                   ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
425                                   src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
426         spin_unlock_bh(&ipt_account_lock);
427         return IPT_CONTINUE;
428     }    
429     
430     // 16 bit network
431     if (ipt_account_tables[info->table_nr].depth == 1)
432     {
433         ipt_account_depth1_insert((struct ipt_account_mask_16 *)ipt_account_tables[info->table_nr].data,
434                                   ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
435                                   src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
436         spin_unlock_bh(&ipt_account_lock);
437         return IPT_CONTINUE;
438     }
439     
440     // 24 bit network
441     if (ipt_account_tables[info->table_nr].depth == 2)
442     {
443         ipt_account_depth2_insert((struct ipt_account_mask_8 *)ipt_account_tables[info->table_nr].data,
444                                   ipt_account_tables[info->table_nr].ip, ipt_account_tables[info->table_nr].netmask,
445                                   src_ip, dst_ip, size, &ipt_account_tables[info->table_nr].itemcount);
446         spin_unlock_bh(&ipt_account_lock);
447         return IPT_CONTINUE;
448     }
449     
450     printk("ACCOUNT: ipt_account_target: Unable to process packet. Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n",
451             info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
452     
453     spin_unlock_bh(&ipt_account_lock);
454     return IPT_CONTINUE;
455 }
456
457 /*
458     Functions dealing with "handles":
459     Handles are snapshots of a accounting state.
460     
461     read snapshots are only for debugging the code
462     and are very expensive concerning speed/memory
463     compared to read_and_flush.
464     
465     The functions aren't protected by spinlocks themselves
466     as this is done in the ioctl part of the code.
467 */
468
469 /*
470     Find a free handle slot. Normally only one should be used,
471     but there could be two or more applications accessing the data
472     at the same time.
473 */
474 int ipt_account_handle_find_slot(void)
475 {
476     unsigned int i;
477     // Insert new table
478     for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
479     {
480         // Found free slot
481         if (ipt_account_handles[i].data == NULL)
482         {
483             // Don't "mark" data as used as we are protected by a spinlock by the calling function.
484             // handle_find_slot() is only a function to prevent code duplication.
485             return i;
486         }
487     }
488         
489     // No free slot found
490     printk("ACCOUNT: No free handle slot found (max: %u). Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
491     return -1;
492 }
493
494 int ipt_account_handle_free(unsigned int handle)
495 {
496     if (handle >= ACCOUNT_MAX_HANDLES)
497     {
498         printk("ACCOUNT: Invalid handle for ipt_account_handle_free() specified: %u\n", handle);
499         return -EINVAL;
500     }
501     
502     ipt_account_data_free(ipt_account_handles[handle].data, ipt_account_handles[handle].depth);
503     memset (&ipt_account_handles[handle], 0, sizeof (struct ipt_account_handle));
504     return 0;
505 }
506
507 /* Prepare data for read without flush. Use only for debugging!
508    Real applications should use read&flush as it's way more efficent */
509 int ipt_account_handle_prepare_read(char *tablename, unsigned int *count)
510 {
511     int handle, i, table_nr=-1;
512     
513     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
514         if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
515         {
516             table_nr = i;
517             break;
518         }
519
520     if (table_nr == -1)
521     {
522         printk("ACCOUNT: ipt_account_handle_prepare_read(): Table %s not found\n", tablename);
523         return -1;
524     }
525             
526     // Can't find a free handle slot?
527     if ((handle = ipt_account_handle_find_slot()) == -1)
528         return -1;
529     
530     // Fill up handle structure
531     ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
532     ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
533     ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
534     
535     // allocate "root" table
536     if ((ipt_account_handles[handle].data = (void*)get_zeroed_page(GFP_KERNEL)) == NULL)
537     {
538         printk("ACCOUNT: out of memory for root table in ipt_account_handle_prepare_read()\n");
539         memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
540         return -1;        
541     }
542     
543     // Recursive copy of complete data structure
544     unsigned int depth = ipt_account_handles[handle].depth;
545     if (depth == 0)
546     {
547         memcpy(ipt_account_handles[handle].data, ipt_account_tables[table_nr].data, sizeof(struct ipt_account_mask_24));
548     } else if (depth == 1) {
549         struct ipt_account_mask_16 *src_16 = (struct ipt_account_mask_16 *)ipt_account_tables[table_nr].data;
550         struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16 *)ipt_account_handles[handle].data;
551         unsigned int b;
552         
553         for (b = 0; b <= 255; b++)
554         {
555             if (src_16->mask_24[b])
556             {
557                 if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL)
558                 {
559                     printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
560                     ipt_account_data_free(ipt_account_handles[handle].data, depth);
561                     memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
562                     return -1;                    
563                 }
564                 
565                 memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
566             }
567         }
568     } else if(depth == 2) {
569         struct ipt_account_mask_8 *src_8 = (struct ipt_account_mask_8 *)ipt_account_tables[table_nr].data;
570         struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8 *)ipt_account_handles[handle].data;
571         unsigned int a;
572         
573         for (a = 0; a <= 255; a++)
574         {
575             if (src_8->mask_16[a])
576             {
577                 if ((network_8->mask_16[a] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL)
578                 {
579                     printk("ACCOUNT: out of memory during copy of 24 bit network in ipt_account_handle_prepare_read()\n");
580                     ipt_account_data_free(ipt_account_handles[handle].data, depth);
581                     memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
582                     return -1;                    
583                 }
584
585                 memcpy(network_8->mask_16[a], src_8->mask_16[a], sizeof(struct ipt_account_mask_16));
586                     
587                 struct ipt_account_mask_16 *src_16 = src_8->mask_16[a];
588                 struct ipt_account_mask_16 *network_16 = network_8->mask_16[a];
589                 unsigned int b;
590                 
591                 for (b = 0; b <= 255; b++)
592                 {
593                     if (src_16->mask_24[b])
594                     {
595                         if ((network_16->mask_24[b] = (void*)get_zeroed_page(GFP_KERNEL)) == NULL)
596                         {
597                             printk("ACCOUNT: out of memory during copy of 16 bit network in ipt_account_handle_prepare_read()\n");
598                             ipt_account_data_free(ipt_account_handles[handle].data, depth);
599                             memset (&ipt_account_handles[handle], 0, sizeof(struct ipt_account_handle));
600                             return -1;                    
601                         }
602                         
603                         memcpy(network_16->mask_24[b], src_16->mask_24[b], sizeof(struct ipt_account_mask_24));
604                     }
605                 }
606             }
607         }
608     }
609
610     *count = ipt_account_tables[table_nr].itemcount;
611     return handle;
612 }
613
614 /* Prepare data for read and flush it */
615 int ipt_account_handle_prepare_read_flush(char *tablename, unsigned int *count)
616 {
617     int handle, i, table_nr=-1;
618     
619     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
620         if (strncmp(ipt_account_tables[i].name, tablename, ACCOUNT_TABLE_NAME_LEN) == 0)
621         {
622             table_nr = i;
623             break;
624         }
625
626     if (table_nr == -1)
627     {
628         printk("ACCOUNT: ipt_account_handle_prepare_read_flush(): Table %s not found\n", tablename);
629         return -1;
630     }
631             
632     // Can't find a free handle slot?
633     if ((handle = ipt_account_handle_find_slot()) == -1)
634         return -1;
635     
636     // Fill up handle structure
637     ipt_account_handles[handle].ip = ipt_account_tables[table_nr].ip;
638     ipt_account_handles[handle].depth = ipt_account_tables[table_nr].depth;
639     ipt_account_handles[handle].itemcount = ipt_account_tables[table_nr].itemcount;
640     ipt_account_handles[handle].data = ipt_account_tables[table_nr].data;
641     *count = ipt_account_tables[table_nr].itemcount;
642
643     // "Flush" table data
644     ipt_account_tables[table_nr].data = (void*)get_zeroed_page(GFP_KERNEL);
645     ipt_account_tables[table_nr].itemcount = 0;
646
647     return handle;
648 }
649
650 /* Copy the actual that into a prepared buffer.
651    We only copy entries != 0 to increase performance.
652    The memory gets freed again in ipt_account_handle_free().
653 */
654 int ipt_account_handle_get_data(unsigned int handle, void *buffer)
655 {
656     struct ipt_account_handle_ip handle_ip;    
657     unsigned int handle_ip_size = sizeof (struct ipt_account_handle_ip);
658     unsigned int i, tmpbuf_pos=0;
659
660     if (handle >= ACCOUNT_MAX_HANDLES)
661     {
662         printk("ACCOUNT: invalid handle for ipt_account_handle_get_data() specified: %u\n", handle);
663         return -1;
664     }
665     
666     if (ipt_account_handles[handle].data == NULL)
667     {
668         printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
669         return -1;
670     }
671     
672     unsigned int net_ip = ipt_account_handles[handle].ip;
673     unsigned int depth = ipt_account_handles[handle].depth;
674     
675     // 8 bit network
676     if (depth == 0)
677     {
678         struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)ipt_account_handles[handle].data;
679         for (i = 0; i <= 255; i++)
680         {
681             if (network->ip[i].src_packets || network->ip[i].dst_packets)
682             {
683                 handle_ip.ip = net_ip | (i<<24);
684                 handle_ip.src_packets = network->ip[i].src_packets;
685                 handle_ip.src_bytes = network->ip[i].src_bytes;
686                 handle_ip.dst_packets = network->ip[i].dst_packets;
687                 handle_ip.dst_bytes = network->ip[i].dst_bytes;
688
689                 // Temporary buffer full? Flush to userspace
690                 if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE)
691                 {
692                     copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
693                     tmpbuf_pos = 0;
694                 }
695                 memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
696                 tmpbuf_pos += handle_ip_size;
697             }
698         }
699
700         // Flush remaining data to userspace
701         if (tmpbuf_pos)
702             copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
703         
704         return 0;
705     }
706     
707     // 16 bit network
708     if (depth == 1)
709     {
710         struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)ipt_account_handles[handle].data;
711         unsigned int b;
712         for (b = 0; b <= 255; b++)
713         {
714             if (network_16->mask_24[b])
715             {
716                 struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
717                 for (i = 0; i <= 255; i++)
718                 {
719                     if (network->ip[i].src_packets || network->ip[i].dst_packets)
720                     {
721                         handle_ip.ip = net_ip | (b << 16) | (i<<24);
722                         handle_ip.src_packets = network->ip[i].src_packets;
723                         handle_ip.src_bytes = network->ip[i].src_bytes;
724                         handle_ip.dst_packets = network->ip[i].dst_packets;
725                         handle_ip.dst_bytes = network->ip[i].dst_bytes;
726         
727                         // Temporary buffer full? Flush to userspace
728                         if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE)
729                         {
730                             copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
731                             tmpbuf_pos = 0;
732                         }
733                         memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
734                         tmpbuf_pos += handle_ip_size;
735                     }
736                 }
737             }
738         }
739         
740         // Flush remaining data to userspace
741         if (tmpbuf_pos)
742             copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
743         
744         return 0;
745     }
746     
747     // 24 bit network
748     if (depth == 2)
749     {
750         struct ipt_account_mask_8 *network_8 = (struct ipt_account_mask_8*)ipt_account_handles[handle].data;
751         unsigned int a, b;
752         for (a = 0; a <= 255; a++)
753         {
754             if (network_8->mask_16[a])
755             {
756                 struct ipt_account_mask_16 *network_16 = (struct ipt_account_mask_16*)network_8->mask_16[a];
757                 for (b = 0; b <= 255; b++)
758                 {
759                     if (network_16->mask_24[b])
760                     {
761                         struct ipt_account_mask_24 *network = (struct ipt_account_mask_24*)network_16->mask_24[b];
762                         for (i = 0; i <= 255; i++)
763                         {
764                             if (network->ip[i].src_packets || network->ip[i].dst_packets)
765                             {
766                                 handle_ip.ip = net_ip | (a << 8) | (b << 16) | (i<<24);
767                                 handle_ip.src_packets = network->ip[i].src_packets;
768                                 handle_ip.src_bytes = network->ip[i].src_bytes;
769                                 handle_ip.dst_packets = network->ip[i].dst_packets;
770                                 handle_ip.dst_bytes = network->ip[i].dst_bytes;
771                 
772                                 // Temporary buffer full? Flush to userspace
773                                 if (tmpbuf_pos+handle_ip_size >= PAGE_SIZE)
774                                 {
775                                     copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
776                                     tmpbuf_pos = 0;
777                                 }
778                                 memcpy(ipt_account_tmpbuf+tmpbuf_pos, &handle_ip, handle_ip_size);
779                                 tmpbuf_pos += handle_ip_size;
780                             }
781                         }
782                     }
783                 }
784             }
785         }
786         
787         // Flush remaining data to userspace
788         if (tmpbuf_pos)
789             copy_to_user(buffer, ipt_account_tmpbuf, tmpbuf_pos);
790         
791         return 0;
792     }
793     
794     return -1;
795 }
796
797 static int ipt_account_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
798 {
799     struct ipt_account_handle_sockopt handle;
800     int ret = -EINVAL;
801
802     if (!capable(CAP_NET_ADMIN))
803             return -EPERM;
804             
805     switch (cmd)
806     {
807         case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
808             if (len != sizeof(struct ipt_account_handle_sockopt))
809             {
810                 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));
811                 break;
812             }   
813             
814             if (copy_from_user (&handle, user, len))
815             {
816                 printk("ACCOUNT: ipt_account_set_ctl: copy_from_user failed for IPT_SO_SET_HANDLE_FREE\n");
817                 break;
818             }
819             
820             spin_lock_bh(&ipt_account_userspace_lock);
821             ret = ipt_account_handle_free(handle.handle_nr);
822             spin_unlock_bh(&ipt_account_userspace_lock);
823             break;
824         case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL:
825         {
826             unsigned int i;
827             spin_lock_bh(&ipt_account_userspace_lock);
828             for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
829                 ipt_account_handle_free(i);
830             spin_unlock_bh(&ipt_account_userspace_lock);
831             ret = 0;
832             break;
833         }
834         default:
835             printk("ACCOUNT: ipt_account_set_ctl: unknown request %i\n", cmd);
836     }
837
838     return ret;
839 }
840
841 static int ipt_account_get_ctl(struct sock *sk, int cmd, void *user, int *len)
842 {
843     struct ipt_account_handle_sockopt handle;
844     int ret = -EINVAL;
845
846     if (!capable(CAP_NET_ADMIN))
847             return -EPERM;
848
849     switch (cmd)
850     {
851         case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
852         case IPT_SO_GET_ACCOUNT_PREPARE_READ:
853             if (*len < sizeof(struct ipt_account_handle_sockopt))
854             {
855                 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
856                        *len, sizeof(struct ipt_account_handle_sockopt));
857                 break;
858             }   
859             
860             if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
861             {
862                 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
863                 break;
864             }
865             
866             spin_lock_bh(&ipt_account_lock);
867             spin_lock_bh(&ipt_account_userspace_lock);
868             if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
869                 handle.handle_nr = ipt_account_handle_prepare_read_flush(handle.name, &handle.itemcount);
870             else
871                 handle.handle_nr = ipt_account_handle_prepare_read(handle.name, &handle.itemcount);
872             spin_unlock_bh(&ipt_account_userspace_lock);
873             spin_unlock_bh(&ipt_account_lock);
874                 
875             if (handle.handle_nr == -1)
876             {
877                 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_prepare_read failed\n");
878                 break;
879             }
880             
881             if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt)))
882             {
883                 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
884                 break;
885             }
886             ret = 0;
887             break;
888         case IPT_SO_GET_ACCOUNT_GET_DATA:
889             if (*len < sizeof(struct ipt_account_handle_sockopt))
890             {
891                 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
892                        *len, sizeof(struct ipt_account_handle_sockopt));
893                 break;
894             }   
895             
896             if (copy_from_user (&handle, user, sizeof(struct ipt_account_handle_sockopt)))
897             {
898                 printk("ACCOUNT: ipt_account_get_ctl: copy_from_user failed for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n");
899                 break;
900             }
901             
902             if (handle.handle_nr >= ACCOUNT_MAX_HANDLES)
903             {
904                 printk("ACCOUNT: Invalid handle for IPT_SO_GET_ACCOUNT_GET_DATA: %u\n", handle.handle_nr);
905                 break;
906             }
907             
908             if (*len < ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip))
909             {
910                 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
911                        *len, ipt_account_handles[handle.handle_nr].itemcount*sizeof(struct ipt_account_handle_ip));
912                 ret = -ENOMEM;
913                 break;
914             }   
915             
916             spin_lock_bh(&ipt_account_userspace_lock);
917             ret = ipt_account_handle_get_data(handle.handle_nr, user);
918             spin_unlock_bh(&ipt_account_userspace_lock);
919             if (ret)
920             {
921                 printk("ACCOUNT: ipt_account_get_ctl: ipt_account_handle_get_data failed for handle %u\n", handle.handle_nr);
922                 break;
923             }
924             
925             ret = 0;
926             break;
927         case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE:
928         {
929             if (*len < sizeof(struct ipt_account_handle_sockopt))
930             {
931                 printk("ACCOUNT: ipt_account_get_ctl: wrong data size (%u != %u) for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
932                        *len, sizeof(struct ipt_account_handle_sockopt));
933                 break;
934             }   
935             
936             // Find out how many handles are in use
937             unsigned int i;
938             handle.itemcount = 0;
939             spin_lock_bh(&ipt_account_userspace_lock);
940             for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
941                 if (ipt_account_handles[i].data)
942                     handle.itemcount++;
943             spin_unlock_bh(&ipt_account_userspace_lock);
944             
945             if (copy_to_user(user, &handle, sizeof(struct ipt_account_handle_sockopt)))
946             {
947                 printk("ACCOUNT: ipt_account_set_ctl: copy_to_user failed for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n");
948                 break;
949             }
950             ret = 0;
951             break;
952         }
953         case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES:
954         {
955             spin_lock_bh(&ipt_account_lock);
956             
957             // Determine size of table names
958             unsigned int size = 0, i;
959             for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
960             {
961                 if (ipt_account_tables[i].name[0] != 0)
962                     size += strlen (ipt_account_tables[i].name) + 1;
963             }
964             size += 1;    // Terminating NULL character
965             
966             if (*len < size)
967             {
968                 spin_unlock_bh(&ipt_account_lock);
969                 printk("ACCOUNT: ipt_account_get_ctl: not enough space (%u < %u) to store table names\n", *len, size);
970                 ret = -ENOMEM;
971                 break;
972             }
973             // Copy table names to userspace
974             char *tnames = user;
975             for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
976             {
977                 if (ipt_account_tables[i].name[0] != 0)
978                 {
979                     int len = strlen (ipt_account_tables[i].name) + 1;
980                     copy_to_user(tnames, ipt_account_tables[i].name, len);    // copy string + terminating zero
981                     tnames += len;
982                 }
983             }
984             // Append terminating zero
985             i = 0;
986             copy_to_user(tnames, &i, 1);    
987             spin_unlock_bh(&ipt_account_lock);
988             ret = 0;
989             break;
990         }
991         default:
992             printk("ACCOUNT: ipt_account_get_ctl: unknown request %i\n", cmd);
993     }
994
995     return ret;
996 }
997
998 static struct ipt_target ipt_account_reg
999 = { { NULL, NULL }, "ACCOUNT", ipt_account_target, ipt_account_checkentry, ipt_account_deleteentry, 
1000     THIS_MODULE };
1001
1002 static struct nf_sockopt_ops ipt_account_sockopts
1003 = { { NULL, NULL }, PF_INET, IPT_SO_SET_ACCOUNT_HANDLE_FREE, IPT_SO_SET_ACCOUNT_MAX+1, ipt_account_set_ctl,
1004     IPT_SO_GET_ACCOUNT_PREPARE_READ, IPT_SO_GET_ACCOUNT_MAX+1, ipt_account_get_ctl, 0, NULL  };
1005     
1006 static int __init init(void)
1007 {
1008     if (PAGE_SIZE < 4096)
1009     {
1010         printk("ACCOUNT: Sorry we need at least a PAGE_SIZE of 4096. Found: %lu\n", PAGE_SIZE);
1011         return -EINVAL;
1012     }
1013
1014     if ((ipt_account_tables = kmalloc(ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table), GFP_KERNEL)) == NULL)
1015     {
1016         printk("ACCOUNT: Out of memory allocating account_tables structure");
1017         return -EINVAL;
1018     }
1019     memset(ipt_account_tables, 0, ACCOUNT_MAX_TABLES*sizeof(struct ipt_account_table));
1020                             
1021     if ((ipt_account_handles = kmalloc(ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle), GFP_KERNEL)) == NULL)
1022     {
1023         printk("ACCOUNT: Out of memory allocating account_handles structure");
1024         kfree (ipt_account_tables);
1025         ipt_account_tables = NULL;
1026         return -EINVAL;
1027     }
1028     memset(ipt_account_handles, 0, ACCOUNT_MAX_HANDLES*sizeof(struct ipt_account_handle));
1029
1030     // Allocate one page as temporary storage
1031     if ((ipt_account_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL)
1032     {
1033         printk("ACCOUNT: Out of memory for temporary buffer page\n");
1034         kfree(ipt_account_tables);
1035         kfree(ipt_account_handles);
1036         ipt_account_tables = NULL;
1037         ipt_account_handles = NULL;
1038         return -EINVAL;
1039     }
1040     
1041     /* Register setsockopt */
1042     if (nf_register_sockopt(&ipt_account_sockopts) < 0)
1043     {
1044         printk("ACCOUNT: Can't register sockopts. Aborting\n");
1045         
1046         kfree(ipt_account_tables);
1047         kfree(ipt_account_handles);
1048         free_page((unsigned long)ipt_account_tmpbuf);
1049         ipt_account_tables = NULL;
1050         ipt_account_handles = NULL;
1051         ipt_account_tmpbuf = NULL;
1052         
1053         return -EINVAL;
1054     }
1055     
1056     if (ipt_register_target(&ipt_account_reg))
1057             return -EINVAL;
1058
1059     return 0;
1060 }
1061
1062 static void __exit fini(void)
1063 {
1064     ipt_unregister_target(&ipt_account_reg);
1065
1066     nf_unregister_sockopt(&ipt_account_sockopts);
1067     
1068     kfree(ipt_account_tables);
1069     kfree(ipt_account_handles);
1070     free_page((unsigned long)ipt_account_tmpbuf);
1071     
1072     ipt_account_tables = NULL;
1073     ipt_account_handles = NULL;
1074     ipt_account_tmpbuf = NULL;
1075 }
1076
1077 module_init(init);
1078 module_exit(fini);
1079 MODULE_LICENSE("GPL");