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