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