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