c1d89fc13ccaa59785c52cbc2280a1064edc570f
[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             u_int32_t j, calc_mask, netsize=0;
141             
142             DEBUGP("ACCOUNT: Found free slot: %d\n", i);
143             strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
144
145             ipt_acc_tables[i].ip = ip;
146             ipt_acc_tables[i].netmask = netmask;
147
148             /* Calculate netsize */
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     int table_nr;
196
197     if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
198         DEBUGP("ACCOUNT: targinfosize %u != %u\n",
199                targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
200         return 0;
201     }
202
203     spin_lock_bh(&ipt_acc_lock);
204     table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
205                                                       info->net_mask);
206     if (table_nr == -1) {
207         printk("ACCOUNT: Table insert problem. Aborting\n");
208         spin_unlock_bh(&ipt_acc_lock);
209         return 0;
210     }
211     /* Table nr caching so we don't have to do an extra string compare 
212        for every packet */
213     info->table_nr = table_nr;
214
215     spin_unlock_bh(&ipt_acc_lock);
216
217     return 1;
218 }
219
220 void ipt_acc_deleteentry(void *targinfo, unsigned int targinfosize)
221 {
222     u_int32_t i;
223     struct ipt_acc_info *info = targinfo;
224
225     if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
226         DEBUGP("ACCOUNT: targinfosize %u != %u\n",
227                targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
228     }
229
230     spin_lock_bh(&ipt_acc_lock);
231
232     DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n", 
233            info->table_name, info->table_nr);
234
235     info->table_nr = -1;    /* Set back to original state */
236
237     /* Look for table */
238     for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
239         if (strncmp(ipt_acc_tables[i].name, info->table_name, 
240                     ACCOUNT_TABLE_NAME_LEN) == 0) {
241             DEBUGP("ACCOUNT: Found table at slot: %d\n", i);
242
243             ipt_acc_tables[i].refcount--;
244             DEBUGP("ACCOUNT: Refcount left: %d\n", 
245                    ipt_acc_tables[i].refcount);
246
247             /* Table not needed anymore? */
248             if (ipt_acc_tables[i].refcount == 0) {
249                 DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i);
250                 ipt_acc_data_free(ipt_acc_tables[i].data, 
251                                       ipt_acc_tables[i].depth);
252                 memset(&ipt_acc_tables[i], 0, 
253                        sizeof(struct ipt_acc_table));
254             }
255
256             spin_unlock_bh(&ipt_acc_lock);
257             return;
258         }
259     }
260
261     /* Table not found */
262     printk("ACCOUNT: Table %s not found for destroy\n", info->table_name);
263     spin_unlock_bh(&ipt_acc_lock);
264 }
265
266 void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24,
267                                u_int32_t net_ip, u_int32_t netmask,
268                                u_int32_t src_ip, u_int32_t dst_ip,
269                                u_int32_t size, u_int32_t *itemcount)
270 {
271     unsigned char is_src = 0, is_dst = 0, src_slot, dst_slot;
272     char is_src_new_ip = 0, is_dst_new_ip = 0; /* Check if this entry is new */
273
274     DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u "
275            "for net %u.%u.%u.%u/%u.%u.%u.%u, size: %u\n", NIPQUAD(src_ip), 
276            NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask), size);
277
278     /* Check if src/dst is inside our network. */
279     /* Special: net_ip = 0.0.0.0/0 gets stored as src in slot 0 */
280     if (!netmask)
281         src_ip = 0;
282     if ((net_ip&netmask) == (src_ip&netmask))
283         is_src = 1;
284     if ((net_ip&netmask) == (dst_ip&netmask) && netmask)
285         is_dst = 1;
286
287     if (!is_src && !is_dst) {
288         DEBUGP("ACCOUNT: Skipping packet %u.%u.%u.%u/%u.%u.%u.%u "
289                "for net %u.%u.%u.%u/%u.%u.%u.%u\n", NIPQUAD(src_ip), 
290                NIPQUAD(dst_ip), NIPQUAD(net_ip), NIPQUAD(netmask));
291         return;
292     }
293
294     /* Calculate array positions */
295     src_slot = (unsigned char)((src_ip&0xFF000000) >> 24);
296     dst_slot = (unsigned char)((dst_ip&0xFF000000) >> 24);
297
298     /* Increase size counters */
299     if (is_src) {
300         /* Calculate network slot */
301         DEBUGP("ACCOUNT: Calculated SRC 8 bit network slot: %d\n", src_slot);
302         if (!mask_24->ip[src_slot].src_packets 
303             && !mask_24->ip[src_slot].dst_packets)
304             is_src_new_ip = 1;
305
306         mask_24->ip[src_slot].src_packets++;
307         mask_24->ip[src_slot].src_bytes+=size;
308     }
309     if (is_dst) {
310         DEBUGP("ACCOUNT: Calculated DST 8 bit network slot: %d\n", dst_slot);
311         if (!mask_24->ip[dst_slot].src_packets 
312             && !mask_24->ip[dst_slot].dst_packets)
313             is_dst_new_ip = 1;
314
315         mask_24->ip[dst_slot].dst_packets++;
316         mask_24->ip[dst_slot].dst_bytes+=size;
317     }
318
319     /* Increase itemcounter */
320     DEBUGP("ACCOUNT: Itemcounter before: %d\n", *itemcount);
321     if (src_slot == dst_slot) {
322         if (is_src_new_ip || is_dst_new_ip) {
323             DEBUGP("ACCOUNT: src_slot == dst_slot: %d, %d\n", 
324                    is_src_new_ip, is_dst_new_ip);
325             (*itemcount)++;
326         }
327     } else {
328         if (is_src_new_ip) {
329             DEBUGP("ACCOUNT: New src_ip: %u.%u.%u.%u\n", NIPQUAD(src_ip));
330             (*itemcount)++;
331         }
332         if (is_dst_new_ip) {
333             DEBUGP("ACCOUNT: New dst_ip: %u.%u.%u.%u\n", NIPQUAD(dst_ip));
334             (*itemcount)++;
335         }
336     }
337     DEBUGP("ACCOUNT: Itemcounter after: %d\n", *itemcount);
338 }
339
340 void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16, 
341                                u_int32_t net_ip, u_int32_t netmask, 
342                                u_int32_t src_ip, u_int32_t dst_ip,
343                                u_int32_t size, u_int32_t *itemcount)
344 {
345     /* Do we need to process src IP? */
346     if ((net_ip&netmask) == (src_ip&netmask)) {
347         unsigned char slot = (unsigned char)((src_ip&0x00FF0000) >> 16);
348         DEBUGP("ACCOUNT: Calculated SRC 16 bit network slot: %d\n", slot);
349
350         /* Do we need to create a new mask_24 bucket? */
351         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = 
352              (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
353             printk("ACCOUNT: Can't process packet because out of memory!\n");
354             return;
355         }
356
357         ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
358                                   net_ip, netmask, src_ip, 0, size, itemcount);
359     }
360
361     /* Do we need to process dst IP? */
362     if ((net_ip&netmask) == (dst_ip&netmask)) {
363         unsigned char slot = (unsigned char)((dst_ip&0x00FF0000) >> 16);
364         DEBUGP("ACCOUNT: Calculated DST 16 bit network slot: %d\n", slot);
365
366         /* Do we need to create a new mask_24 bucket? */
367         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] 
368             = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
369             printk("ACCOUT: Can't process packet because out of memory!\n");
370             return;
371         }
372
373         ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot],
374                                   net_ip, netmask, 0, dst_ip, size, itemcount);
375     }
376 }
377
378 void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8, 
379                                u_int32_t net_ip, u_int32_t netmask,
380                                u_int32_t src_ip, u_int32_t dst_ip,
381                                u_int32_t size, u_int32_t *itemcount)
382 {
383     /* Do we need to process src IP? */
384     if ((net_ip&netmask) == (src_ip&netmask)) {
385         unsigned char slot = (unsigned char)((src_ip&0x0000FF00) >> 8);
386         DEBUGP("ACCOUNT: Calculated SRC 24 bit network slot: %d\n", slot);
387
388         /* Do we need to create a new mask_24 bucket? */
389         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] 
390             = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
391             printk("ACCOUNT: Can't process packet because out of memory!\n");
392             return;
393         }
394
395         ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
396                                   net_ip, netmask, src_ip, 0, size, itemcount);
397     }
398
399     /* Do we need to process dst IP? */
400     if ((net_ip&netmask) == (dst_ip&netmask)) {
401         unsigned char slot = (unsigned char)((dst_ip&0x0000FF00) >> 8);
402         DEBUGP("ACCOUNT: Calculated DST 24 bit network slot: %d\n", slot);
403
404         /* Do we need to create a new mask_24 bucket? */
405         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] 
406             = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
407             printk("ACCOUNT: Can't process packet because out of memory!\n");
408             return;
409         }
410
411         ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot],
412                                   net_ip, netmask, 0, dst_ip, size, itemcount);
413     }
414 }
415
416 static unsigned int ipt_acc_target(struct sk_buff **pskb,
417                                        unsigned int hooknum,
418                                        const struct net_device *in,
419                                        const struct net_device *out,
420                                        const void *targinfo,
421                                        void *userinfo)
422 {
423     const struct ipt_acc_info *info = 
424         (const struct ipt_acc_info *)targinfo;
425     u_int32_t src_ip = (*pskb)->nh.iph->saddr;
426     u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
427     u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
428
429     spin_lock_bh(&ipt_acc_lock);
430
431     if (ipt_acc_tables[info->table_nr].name[0] == 0) {
432         printk("ACCOUNT: ipt_acc_target: Invalid table id %u. "
433                "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr, 
434                NIPQUAD(src_ip), NIPQUAD(dst_ip));
435         spin_unlock_bh(&ipt_acc_lock);
436         return IPT_CONTINUE;
437     }
438
439     /* 8 bit network or "any" network */
440     if (ipt_acc_tables[info->table_nr].depth == 0) {
441         /* Count packet and check if the IP is new */
442         ipt_acc_depth0_insert(
443             (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data,
444             ipt_acc_tables[info->table_nr].ip, 
445             ipt_acc_tables[info->table_nr].netmask,
446             src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
447         spin_unlock_bh(&ipt_acc_lock);
448         return IPT_CONTINUE;
449     }
450
451     /* 16 bit network */
452     if (ipt_acc_tables[info->table_nr].depth == 1) {
453         ipt_acc_depth1_insert(
454             (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data,
455             ipt_acc_tables[info->table_nr].ip, 
456             ipt_acc_tables[info->table_nr].netmask,
457             src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
458         spin_unlock_bh(&ipt_acc_lock);
459         return IPT_CONTINUE;
460     }
461
462     /* 24 bit network */
463     if (ipt_acc_tables[info->table_nr].depth == 2) {
464         ipt_acc_depth2_insert(
465             (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data,
466             ipt_acc_tables[info->table_nr].ip, 
467             ipt_acc_tables[info->table_nr].netmask,
468             src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount);
469         spin_unlock_bh(&ipt_acc_lock);
470         return IPT_CONTINUE;
471     }
472
473     printk("ACCOUNT: ipt_acc_target: Unable to process packet. "
474            "Table id %u. IPs %u.%u.%u.%u/%u.%u.%u.%u\n", 
475            info->table_nr, NIPQUAD(src_ip), NIPQUAD(dst_ip));
476
477     spin_unlock_bh(&ipt_acc_lock);
478     return IPT_CONTINUE;
479 }
480
481 /*
482     Functions dealing with "handles":
483     Handles are snapshots of a accounting state.
484     
485     read snapshots are only for debugging the code
486     and are very expensive concerning speed/memory
487     compared to read_and_flush.
488     
489     The functions aren't protected by spinlocks themselves
490     as this is done in the ioctl part of the code.
491 */
492
493 /*
494     Find a free handle slot. Normally only one should be used,
495     but there could be two or more applications accessing the data
496     at the same time.
497 */
498 int ipt_acc_handle_find_slot(void)
499 {
500     u_int32_t i;
501     /* Insert new table */
502     for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) {
503         /* Found free slot */
504         if (ipt_acc_handles[i].data == NULL) {
505             /* Don't "mark" data as used as we are protected by a spinlock 
506                by the calling function. handle_find_slot() is only a function
507                to prevent code duplication. */
508             return i;
509         }
510     }
511
512     /* No free slot found */
513     printk("ACCOUNT: No free handle slot found (max: %u). "
514            "Please increase ACCOUNT_MAX_HANDLES.\n", ACCOUNT_MAX_HANDLES);
515     return -1;
516 }
517
518 int ipt_acc_handle_free(u_int32_t handle)
519 {
520     if (handle >= ACCOUNT_MAX_HANDLES) {
521         printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:"
522                " %u\n", handle);
523         return -EINVAL;
524     }
525
526     ipt_acc_data_free(ipt_acc_handles[handle].data, 
527                           ipt_acc_handles[handle].depth);
528     memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle));
529     return 0;
530 }
531
532 /* Prepare data for read without flush. Use only for debugging!
533    Real applications should use read&flush as it's way more efficent */
534 int ipt_acc_handle_prepare_read(char *tablename, u_int32_t *count)
535 {
536     int handle, i, table_nr=-1;
537     unsigned char depth;
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     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     void *new_data_page;
656
657     for (i = 0; i < ACCOUNT_MAX_TABLES; i++)
658         if (strncmp(ipt_acc_tables[i].name, tablename, 
659                     ACCOUNT_TABLE_NAME_LEN) == 0) {
660             table_nr = i;
661             break;
662         }
663
664     if (table_nr == -1) {
665         printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
666                "Table %s not found\n", tablename);
667         return -1;
668     }
669
670     /* Can't find a free handle slot? */
671     if ((handle = ipt_acc_handle_find_slot()) == -1)
672         return -1;
673
674     /* Try to allocate memory */
675     if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
676         printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
677                "Out of memory!\n");
678         return -1;
679     }
680
681     /* Fill up handle structure */
682     ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip;
683     ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth;
684     ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount;
685     ipt_acc_handles[handle].data = ipt_acc_tables[table_nr].data;
686     *count = ipt_acc_tables[table_nr].itemcount;
687
688     /* "Flush" table data */
689     ipt_acc_tables[table_nr].data = new_data_page;
690     ipt_acc_tables[table_nr].itemcount = 0;
691
692     return handle;
693 }
694
695 /* Copy 8 bit network data into a prepared buffer.
696    We only copy entries != 0 to increase performance.
697 */
698 void ipt_acc_handle_copy_data(void *to_user, int *pos, 
699                                   struct ipt_acc_mask_24 *data,
700                                   u_int32_t net_ip, u_int32_t net_OR_mask)
701 {
702     struct ipt_acc_handle_ip handle_ip;
703     u_int32_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
704     u_int32_t i;
705     
706     for (i = 0; i <= 255; i++) {
707         if (data->ip[i].src_packets || data->ip[i].dst_packets) {
708             handle_ip.ip = net_ip | net_OR_mask | (i<<24);
709             
710             handle_ip.src_packets = data->ip[i].src_packets;
711             handle_ip.src_bytes = data->ip[i].src_bytes;
712             handle_ip.dst_packets = data->ip[i].dst_packets;
713             handle_ip.dst_bytes = data->ip[i].dst_bytes;
714
715             /* Temporary buffer full? Flush to userspace */
716             if (*pos+handle_ip_size >= PAGE_SIZE) {
717                 *pos = 0;
718                 if (copy_to_user(to_user, ipt_acc_tmpbuf, *pos))
719                     return;
720             }
721             memcpy(ipt_acc_tmpbuf+*pos, &handle_ip, handle_ip_size);
722             *pos += handle_ip_size;
723         }
724     }
725 }
726    
727 /* Copy the data from our internal structure 
728    We only copy entries != 0 to increase performance.
729    Overwrites ipt_acc_tmpbuf.
730 */
731 int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
732 {
733     u_int32_t tmpbuf_pos=0, net_ip;
734     unsigned char depth;
735
736     if (handle >= ACCOUNT_MAX_HANDLES) {
737         printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
738                "specified: %u\n", handle);
739         return -1;
740     }
741
742     if (ipt_acc_handles[handle].data == NULL) {
743         printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
744         return -1;
745     }
746
747     net_ip = ipt_acc_handles[handle].ip;
748     depth = ipt_acc_handles[handle].depth;
749
750     /* 8 bit network */
751     if (depth == 0) {
752         struct ipt_acc_mask_24 *network = 
753             (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
754         ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, 0);
755         
756         /* Flush remaining data to userspace */
757         if (tmpbuf_pos)
758             if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
759                 return -1;
760
761         return 0;
762     }
763
764     /* 16 bit network */
765     if (depth == 1) {
766         struct ipt_acc_mask_16 *network_16 = 
767             (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
768         u_int32_t b;
769         for (b = 0; b <= 255; b++) {
770             if (network_16->mask_24[b]) {
771                 struct ipt_acc_mask_24 *network = 
772                     (struct ipt_acc_mask_24*)network_16->mask_24[b];
773                 ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, 
774                                              net_ip, (b << 16));
775             }
776         }
777
778         /* Flush remaining data to userspace */
779         if (tmpbuf_pos)
780             if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
781                 return -1;
782
783         return 0;
784     }
785
786     /* 24 bit network */
787     if (depth == 2) {
788         struct ipt_acc_mask_8 *network_8 = 
789             (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
790         u_int32_t a, b;
791         for (a = 0; a <= 255; a++) {
792             if (network_8->mask_16[a]) {
793                 struct ipt_acc_mask_16 *network_16 = 
794                     (struct ipt_acc_mask_16*)network_8->mask_16[a];
795                 for (b = 0; b <= 255; b++) {
796                     if (network_16->mask_24[b]) {
797                         struct ipt_acc_mask_24 *network = 
798                             (struct ipt_acc_mask_24*)network_16->mask_24[b];
799                         ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network,
800                                                  net_ip, (a << 8) | (b << 16));
801                     }
802                 }
803             }
804         }
805
806         /* Flush remaining data to userspace */
807         if (tmpbuf_pos)
808             if (copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos))
809                 return -1;
810
811         return 0;
812     }
813     
814     return -1;
815 }
816
817 static int ipt_acc_set_ctl(struct sock *sk, int cmd, 
818                                void *user, u_int32_t len)
819 {
820     struct ipt_acc_handle_sockopt handle;
821     int ret = -EINVAL;
822
823     if (!capable(CAP_NET_ADMIN))
824         return -EPERM;
825
826     switch (cmd) {
827     case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
828         if (len != sizeof(struct ipt_acc_handle_sockopt)) {
829             printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
830                    "for IPT_SO_SET_HANDLE_FREE\n", 
831                    len, sizeof(struct ipt_acc_handle_sockopt));
832             break;
833         }
834
835         if (copy_from_user (&handle, user, len)) {
836             printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
837                    "IPT_SO_SET_HANDLE_FREE\n");
838             break;
839         }
840
841         spin_lock_bh(&ipt_acc_userspace_lock);
842         ret = ipt_acc_handle_free(handle.handle_nr);
843         spin_unlock_bh(&ipt_acc_userspace_lock);
844         break;
845     case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
846             u_int32_t i;
847             spin_lock_bh(&ipt_acc_userspace_lock);
848             for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
849                 ipt_acc_handle_free(i);
850             spin_unlock_bh(&ipt_acc_userspace_lock);
851             ret = 0;
852             break;
853         }
854     default:
855         printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
856     }
857
858     return ret;
859 }
860
861 static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
862 {
863     struct ipt_acc_handle_sockopt handle;
864     int ret = -EINVAL;
865
866     if (!capable(CAP_NET_ADMIN))
867         return -EPERM;
868
869     switch (cmd) {
870     case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
871     case IPT_SO_GET_ACCOUNT_PREPARE_READ:
872         if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
873             printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
874                    "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
875                    *len, sizeof(struct ipt_acc_handle_sockopt));
876             break;
877         }
878
879         if (copy_from_user (&handle, user, 
880                             sizeof(struct ipt_acc_handle_sockopt))) {
881             return -EFAULT;
882             break;
883         }
884
885         spin_lock_bh(&ipt_acc_lock);
886         spin_lock_bh(&ipt_acc_userspace_lock);
887         if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
888             handle.handle_nr = ipt_acc_handle_prepare_read_flush(
889                                    handle.name, &handle.itemcount);
890         else
891             handle.handle_nr = ipt_acc_handle_prepare_read(
892                                    handle.name, &handle.itemcount);
893         spin_unlock_bh(&ipt_acc_userspace_lock);
894         spin_unlock_bh(&ipt_acc_lock);
895
896         if (handle.handle_nr == -1) {
897             return -EINVAL;
898             break;
899         }
900
901         if (copy_to_user(user, &handle, 
902                          sizeof(struct ipt_acc_handle_sockopt))) {
903             return -EFAULT;
904             break;
905         }
906         ret = 0;
907         break;
908     case IPT_SO_GET_ACCOUNT_GET_DATA:
909         if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
910             printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
911                    " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
912                    *len, sizeof(struct ipt_acc_handle_sockopt));
913             break;
914         }
915
916         if (copy_from_user (&handle, user, 
917                             sizeof(struct ipt_acc_handle_sockopt))) {
918             return -EFAULT;
919             break;
920         }
921
922         if (handle.handle_nr >= ACCOUNT_MAX_HANDLES) {
923             return -EINVAL;
924             break;
925         }
926
927         if (*len < ipt_acc_handles[handle.handle_nr].itemcount
928                    * sizeof(struct ipt_acc_handle_ip)) {
929             printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
930                    " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n",
931                    *len, ipt_acc_handles[handle.handle_nr].itemcount
932                    * sizeof(struct ipt_acc_handle_ip));
933             ret = -ENOMEM;
934             break;
935         }
936
937         spin_lock_bh(&ipt_acc_userspace_lock);
938         ret = ipt_acc_handle_get_data(handle.handle_nr, user);
939         spin_unlock_bh(&ipt_acc_userspace_lock);
940         if (ret) {
941             printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data"
942                    " failed for handle %u\n", handle.handle_nr);
943             break;
944         }
945
946         ret = 0;
947         break;
948     case IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE: {
949             u_int32_t i;
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             handle.itemcount = 0;
959             spin_lock_bh(&ipt_acc_userspace_lock);
960             for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
961                 if (ipt_acc_handles[i].data)
962                     handle.itemcount++;
963             spin_unlock_bh(&ipt_acc_userspace_lock);
964
965             if (copy_to_user(user, &handle, 
966                              sizeof(struct ipt_acc_handle_sockopt))) {
967                 return -EFAULT;
968                 break;
969             }
970             ret = 0;
971             break;
972         }
973     case IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES: {
974             u_int32_t size = 0, i;
975             char *tnames;
976             
977             spin_lock_bh(&ipt_acc_lock);
978
979             /* Determine size of table names */
980             for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
981                 if (ipt_acc_tables[i].name[0] != 0)
982                     size += strlen (ipt_acc_tables[i].name) + 1;
983             }
984             size += 1;    /* Terminating NULL character */
985
986             if (*len < size) {
987                 spin_unlock_bh(&ipt_acc_lock);
988                 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)"
989                        " to store table names\n", *len, size);
990                 ret = -ENOMEM;
991                 break;
992             }
993             /* Copy table names to userspace */
994             tnames = user;
995             for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
996                 if (ipt_acc_tables[i].name[0] != 0) {
997                     int len = strlen (ipt_acc_tables[i].name) + 1;
998                     /* copy string + terminating zero */
999                     if (copy_to_user(tnames, ipt_acc_tables[i].name, len)) {
1000                         spin_unlock_bh(&ipt_acc_lock);
1001                         return -EFAULT;
1002                     }
1003                     tnames += len;
1004                 }
1005             }
1006             /* Append terminating zero */
1007             i = 0;
1008             ret = copy_to_user(tnames, &i, 1);
1009             spin_unlock_bh(&ipt_acc_lock);
1010             if (ret)
1011                 return -EFAULT;
1012             ret = 0;
1013             break;
1014         }
1015     default:
1016         printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
1017     }
1018
1019     return ret;
1020 }
1021
1022 static struct ipt_target ipt_acc_reg = {
1023     .name = "ACCOUNT",
1024     .target = ipt_acc_target,
1025     .checkentry = ipt_acc_checkentry,
1026     .destroy = ipt_acc_deleteentry,
1027     .me = THIS_MODULE
1028 };
1029
1030 static struct nf_sockopt_ops ipt_acc_sockopts = {
1031     .pf = PF_INET,
1032     .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1033     .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1034     .set = ipt_acc_set_ctl,
1035     .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1036     .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1037     .get = ipt_acc_get_ctl
1038 };
1039
1040 static int __init init(void)
1041 {
1042     if ((ipt_acc_tables = 
1043          kmalloc(ACCOUNT_MAX_TABLES * 
1044                  sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
1045         printk("ACCOUNT: Out of memory allocating account_tables structure");
1046         goto error_cleanup;
1047     }
1048     memset(ipt_acc_tables, 0, 
1049            ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
1050
1051     if ((ipt_acc_handles = 
1052          kmalloc(ACCOUNT_MAX_HANDLES * 
1053                  sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
1054         printk("ACCOUNT: Out of memory allocating account_handles structure");
1055         goto error_cleanup;
1056     }
1057     memset(ipt_acc_handles, 0, 
1058            ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
1059
1060     /* Allocate one page as temporary storage */
1061     if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
1062         printk("ACCOUNT: Out of memory for temporary buffer page\n");
1063         goto error_cleanup;
1064     }
1065
1066     /* Register setsockopt */
1067     if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
1068         printk("ACCOUNT: Can't register sockopts. Aborting\n");
1069         goto error_cleanup;
1070     }
1071
1072     if (ipt_register_target(&ipt_acc_reg))
1073         goto error_cleanup;
1074         
1075     return 0;
1076         
1077 error_cleanup:
1078     if(ipt_acc_tables)
1079         kfree(ipt_acc_tables);
1080     if(ipt_acc_handles)
1081         kfree(ipt_acc_handles);
1082     if (ipt_acc_tmpbuf)
1083         free_page((unsigned long)ipt_acc_tmpbuf);
1084         
1085     return -EINVAL;
1086 }
1087
1088 static void __exit fini(void)
1089 {
1090     ipt_unregister_target(&ipt_acc_reg);
1091
1092     nf_unregister_sockopt(&ipt_acc_sockopts);
1093
1094     kfree(ipt_acc_tables);
1095     kfree(ipt_acc_handles);
1096     free_page((unsigned long)ipt_acc_tmpbuf);
1097 }
1098
1099 module_init(init);
1100 module_exit(fini);
1101 MODULE_LICENSE("GPL");