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