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