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