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