Copy over 2.6.36 files to 3.0 - so patch-o-matic finds them
[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 * *
7f30e02e 6 * Copyright (C) 2004-2005 by Intra2net AG *
54756114
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
70288420
TJ
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/ip.h>
70288420
TJ
18#include <net/icmp.h>
19#include <net/udp.h>
20#include <net/tcp.h>
21#include <linux/netfilter_ipv4/ip_tables.h>
b8041594
TJ
22#include <linux/netfilter_ipv4/lockhelp.h>
23#include <asm/semaphore.h>
6fe5ced9
TJ
24#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/string.h>
27#include <asm/uaccess.h>
70288420 28
70288420
TJ
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
491d7fff
TJ
42static struct ipt_acc_table *ipt_acc_tables = NULL;
43static struct ipt_acc_handle *ipt_acc_handles = NULL;
44static void *ipt_acc_tmpbuf = NULL;
70288420 45
2ea7a43c 46/* Spinlock used for manipulating the current accounting tables/data */
b8041594
TJ
47DECLARE_LOCK(ipt_acc_lock);
48/* Mutex (semaphore) used for manipulating userspace handles/snapshot data */
49static struct semaphore ipt_acc_userspace_mutex;
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) {
8bea9bf9 140 u_int32_t calc_mask, netsize=0;
c6620097 141 int j; /* needs to be signed, otherwise we risk endless loop */
07eb98b4 142
70288420 143 DEBUGP("ACCOUNT: Found free slot: %d\n", i);
6803acfc 144 strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1);
2ea7a43c 145
6803acfc
TJ
146 ipt_acc_tables[i].ip = ip;
147 ipt_acc_tables[i].netmask = netmask;
2ea7a43c 148
06817fca 149 /* Calculate netsize */
70288420 150 calc_mask = htonl(netmask);
6fe5ced9 151 for (j = 31; j >= 0; j--) {
70288420 152 if (calc_mask&(1<<j))
850cece6 153 netsize++;
70288420
TJ
154 else
155 break;
156 }
2ea7a43c 157
06817fca 158 /* Calculate depth from netsize */
850cece6 159 if (netsize >= 24)
6803acfc 160 ipt_acc_tables[i].depth = 0;
850cece6 161 else if (netsize >= 16)
6803acfc 162 ipt_acc_tables[i].depth = 1;
850cece6 163 else if(netsize >= 8)
6803acfc 164 ipt_acc_tables[i].depth = 2;
2ea7a43c 165
013fcc80 166 DEBUGP("ACCOUNT: calculated netsize: %u -> "
6803acfc
TJ
167 "ipt_acc_table depth %u\n", netsize,
168 ipt_acc_tables[i].depth);
2ea7a43c 169
6803acfc
TJ
170 ipt_acc_tables[i].refcount++;
171 if ((ipt_acc_tables[i].data
013fcc80 172 = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
850cece6 173 printk("ACCOUNT: out of memory for data of table: %s\n", name);
6803acfc
TJ
174 memset(&ipt_acc_tables[i], 0,
175 sizeof(struct ipt_acc_table));
70288420
TJ
176 return -1;
177 }
2ea7a43c 178
70288420
TJ
179 return i;
180 }
181 }
2ea7a43c 182
06817fca 183 /* No free slot found */
013fcc80
GE
184 printk("ACCOUNT: No free table slot found (max: %d). "
185 "Please increase ACCOUNT_MAX_TABLES.\n", ACCOUNT_MAX_TABLES);
70288420
TJ
186 return -1;
187}
188
6803acfc 189static int ipt_acc_checkentry(const char *tablename,
2ea7a43c
TJ
190 const struct ipt_entry *e,
191 void *targinfo,
192 unsigned int targinfosize,
e1b66101
TJ
193 unsigned int hook_mask)
194{
6803acfc 195 struct ipt_acc_info *info = targinfo;
07eb98b4 196 int table_nr;
2ea7a43c 197
6803acfc 198 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) {
70288420 199 DEBUGP("ACCOUNT: targinfosize %u != %u\n",
6803acfc 200 targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info)));
70288420
TJ
201 return 0;
202 }
203
b8041594 204 LOCK_BH(&ipt_acc_lock);
07eb98b4
TJ
205 table_nr = ipt_acc_table_insert(info->table_name, info->net_ip,
206 info->net_mask);
b8041594
TJ
207 UNLOCK_BH(&ipt_acc_lock);
208
2ea7a43c 209 if (table_nr == -1) {
70288420
TJ
210 printk("ACCOUNT: Table insert problem. Aborting\n");
211 return 0;
212 }
013fcc80
GE
213 /* Table nr caching so we don't have to do an extra string compare
214 for every packet */
70288420 215 info->table_nr = table_nr;
2ea7a43c 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
b8041594 230 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
b8041594 256 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);
b8041594 263 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 const struct net_device *in,
418 const struct net_device *out,
2b0e6504 419 unsigned int hooknum,
2ea7a43c 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
b8041594 429 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));
b8041594 435 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);
b8041594 447 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);
b8041594 458 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);
b8041594 469 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
b8041594 477 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 */
b8041594
TJ
534static int ipt_acc_handle_prepare_read(char *tablename,
535 struct ipt_acc_handle *dest, u_int32_t *count)
e1b66101 536{
b8041594 537 int table_nr=-1;
07eb98b4 538 unsigned char depth;
2ea7a43c 539
b8041594
TJ
540 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
541 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
542 ACCOUNT_TABLE_NAME_LEN) == 0)
543 break;
bea3921b 544
b8041594 545 if (table_nr == ACCOUNT_MAX_TABLES) {
6803acfc 546 printk("ACCOUNT: ipt_acc_handle_prepare_read(): "
013fcc80 547 "Table %s not found\n", tablename);
bea3921b
TJ
548 return -1;
549 }
2ea7a43c 550
06817fca 551 /* Fill up handle structure */
b8041594
TJ
552 dest->ip = ipt_acc_tables[table_nr].ip;
553 dest->depth = ipt_acc_tables[table_nr].depth;
554 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
2ea7a43c 555
06817fca 556 /* allocate "root" table */
b8041594 557 if ((dest->data = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
013fcc80 558 printk("ACCOUNT: out of memory for root table "
6803acfc 559 "in ipt_acc_handle_prepare_read()\n");
2ea7a43c 560 return -1;
bea3921b 561 }
2ea7a43c 562
06817fca 563 /* Recursive copy of complete data structure */
b8041594 564 depth = dest->depth;
2ea7a43c 565 if (depth == 0) {
b8041594 566 memcpy(dest->data,
6803acfc
TJ
567 ipt_acc_tables[table_nr].data,
568 sizeof(struct ipt_acc_mask_24));
bea3921b 569 } else if (depth == 1) {
6803acfc
TJ
570 struct ipt_acc_mask_16 *src_16 =
571 (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data;
572 struct ipt_acc_mask_16 *network_16 =
b8041594 573 (struct ipt_acc_mask_16 *)dest->data;
6fe5ced9 574 u_int32_t b;
2ea7a43c
TJ
575
576 for (b = 0; b <= 255; b++) {
577 if (src_16->mask_24[b]) {
013fcc80
GE
578 if ((network_16->mask_24[b] =
579 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
580 printk("ACCOUNT: out of memory during copy of 16 bit "
6803acfc 581 "network in ipt_acc_handle_prepare_read()\n");
b8041594 582 ipt_acc_data_free(dest->data, depth);
2ea7a43c 583 return -1;
bea3921b 584 }
2ea7a43c 585
013fcc80 586 memcpy(network_16->mask_24[b], src_16->mask_24[b],
6803acfc 587 sizeof(struct ipt_acc_mask_24));
bea3921b
TJ
588 }
589 }
590 } else if(depth == 2) {
6803acfc
TJ
591 struct ipt_acc_mask_8 *src_8 =
592 (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data;
593 struct ipt_acc_mask_8 *network_8 =
b8041594 594 (struct ipt_acc_mask_8 *)dest->data;
2b0e6504
TJ
595 struct ipt_acc_mask_16 *src_16, *network_16;
596 u_int32_t a, b;
2ea7a43c
TJ
597
598 for (a = 0; a <= 255; a++) {
599 if (src_8->mask_16[a]) {
013fcc80
GE
600 if ((network_8->mask_16[a] =
601 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
602 printk("ACCOUNT: out of memory during copy of 24 bit network"
6803acfc 603 " in ipt_acc_handle_prepare_read()\n");
b8041594 604 ipt_acc_data_free(dest->data, depth);
2ea7a43c 605 return -1;
bea3921b
TJ
606 }
607
013fcc80 608 memcpy(network_8->mask_16[a], src_8->mask_16[a],
6803acfc 609 sizeof(struct ipt_acc_mask_16));
2ea7a43c 610
2b0e6504
TJ
611 src_16 = src_8->mask_16[a];
612 network_16 = network_8->mask_16[a];
2ea7a43c
TJ
613
614 for (b = 0; b <= 255; b++) {
615 if (src_16->mask_24[b]) {
013fcc80
GE
616 if ((network_16->mask_24[b] =
617 (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
618 printk("ACCOUNT: out of memory during copy of 16 bit"
6803acfc 619 " network in ipt_acc_handle_prepare_read()\n");
b8041594 620 ipt_acc_data_free(dest->data, depth);
2ea7a43c 621 return -1;
bea3921b 622 }
2ea7a43c 623
013fcc80 624 memcpy(network_16->mask_24[b], src_16->mask_24[b],
6803acfc 625 sizeof(struct ipt_acc_mask_24));
bea3921b
TJ
626 }
627 }
628 }
629 }
630 }
631
6803acfc 632 *count = ipt_acc_tables[table_nr].itemcount;
b8041594
TJ
633
634 return 0;
bea3921b
TJ
635}
636
637/* Prepare data for read and flush it */
b8041594
TJ
638static int ipt_acc_handle_prepare_read_flush(char *tablename,
639 struct ipt_acc_handle *dest, u_int32_t *count)
e1b66101 640{
b8041594 641 int table_nr;
07eb98b4 642 void *new_data_page;
2ea7a43c 643
b8041594
TJ
644 for (table_nr = 0; table_nr < ACCOUNT_MAX_TABLES; table_nr++)
645 if (strncmp(ipt_acc_tables[table_nr].name, tablename,
646 ACCOUNT_TABLE_NAME_LEN) == 0)
647 break;
bea3921b 648
b8041594 649 if (table_nr == ACCOUNT_MAX_TABLES) {
6803acfc 650 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
013fcc80 651 "Table %s not found\n", tablename);
bea3921b
TJ
652 return -1;
653 }
2ea7a43c 654
ad773892 655 /* Try to allocate memory */
07eb98b4 656 if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
6803acfc 657 printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
013fcc80 658 "Out of memory!\n");
ad773892
TJ
659 return -1;
660 }
661
06817fca 662 /* Fill up handle structure */
b8041594
TJ
663 dest->ip = ipt_acc_tables[table_nr].ip;
664 dest->depth = ipt_acc_tables[table_nr].depth;
665 dest->itemcount = ipt_acc_tables[table_nr].itemcount;
666 dest->data = ipt_acc_tables[table_nr].data;
6803acfc 667 *count = ipt_acc_tables[table_nr].itemcount;
bea3921b 668
06817fca 669 /* "Flush" table data */
6803acfc
TJ
670 ipt_acc_tables[table_nr].data = new_data_page;
671 ipt_acc_tables[table_nr].itemcount = 0;
bea3921b 672
b8041594 673 return 0;
bea3921b
TJ
674}
675
e1b66101 676/* Copy 8 bit network data into a prepared buffer.
bea3921b 677 We only copy entries != 0 to increase performance.
bea3921b 678*/
32cd84a6
TJ
679static int ipt_acc_handle_copy_data(void *to_user, u_int32_t *to_user_pos,
680 u_int32_t *tmpbuf_pos,
6803acfc 681 struct ipt_acc_mask_24 *data,
6fe5ced9 682 u_int32_t net_ip, u_int32_t net_OR_mask)
e1b66101 683{
6803acfc 684 struct ipt_acc_handle_ip handle_ip;
6fe5ced9 685 u_int32_t handle_ip_size = sizeof (struct ipt_acc_handle_ip);
6fe5ced9 686 u_int32_t i;
e1b66101
TJ
687
688 for (i = 0; i <= 255; i++) {
689 if (data->ip[i].src_packets || data->ip[i].dst_packets) {
690 handle_ip.ip = net_ip | net_OR_mask | (i<<24);
691
692 handle_ip.src_packets = data->ip[i].src_packets;
693 handle_ip.src_bytes = data->ip[i].src_bytes;
694 handle_ip.dst_packets = data->ip[i].dst_packets;
695 handle_ip.dst_bytes = data->ip[i].dst_bytes;
696
697 /* Temporary buffer full? Flush to userspace */
32cd84a6
TJ
698 if (*tmpbuf_pos+handle_ip_size >= PAGE_SIZE) {
699 if (copy_to_user(to_user + *to_user_pos, ipt_acc_tmpbuf,
700 *tmpbuf_pos))
dc668ad3 701 return -EFAULT;
32cd84a6
TJ
702 *to_user_pos = *to_user_pos + *tmpbuf_pos;
703 *tmpbuf_pos = 0;
e1b66101 704 }
32cd84a6
TJ
705 memcpy(ipt_acc_tmpbuf+*tmpbuf_pos, &handle_ip, handle_ip_size);
706 *tmpbuf_pos += handle_ip_size;
e1b66101
TJ
707 }
708 }
dc668ad3
TJ
709
710 return 0;
e1b66101
TJ
711}
712
713/* Copy the data from our internal structure
714 We only copy entries != 0 to increase performance.
6803acfc 715 Overwrites ipt_acc_tmpbuf.
e1b66101 716*/
0f549f69 717static int ipt_acc_handle_get_data(u_int32_t handle, void *to_user)
e1b66101 718{
39f2f255 719 u_int32_t to_user_pos=0, tmpbuf_pos=0, net_ip;
07eb98b4 720 unsigned char depth;
bea3921b 721
2ea7a43c 722 if (handle >= ACCOUNT_MAX_HANDLES) {
6803acfc 723 printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() "
013fcc80 724 "specified: %u\n", handle);
bea3921b
TJ
725 return -1;
726 }
2ea7a43c 727
6803acfc 728 if (ipt_acc_handles[handle].data == NULL) {
bea3921b
TJ
729 printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle);
730 return -1;
731 }
2ea7a43c 732
07eb98b4
TJ
733 net_ip = ipt_acc_handles[handle].ip;
734 depth = ipt_acc_handles[handle].depth;
2ea7a43c 735
06817fca 736 /* 8 bit network */
2ea7a43c 737 if (depth == 0) {
6803acfc
TJ
738 struct ipt_acc_mask_24 *network =
739 (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data;
32cd84a6
TJ
740 if (ipt_acc_handle_copy_data(to_user, &to_user_pos, &tmpbuf_pos,
741 network, net_ip, 0))
dc668ad3 742 return -1;
e1b66101 743
06817fca 744 /* Flush remaining data to userspace */
9c44d30e 745 if (tmpbuf_pos)
32cd84a6 746 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
6fe5ced9 747 return -1;
2ea7a43c 748
bea3921b
TJ
749 return 0;
750 }
2ea7a43c 751
06817fca 752 /* 16 bit network */
2ea7a43c 753 if (depth == 1) {
6803acfc
TJ
754 struct ipt_acc_mask_16 *network_16 =
755 (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data;
6fe5ced9 756 u_int32_t b;
2ea7a43c
TJ
757 for (b = 0; b <= 255; b++) {
758 if (network_16->mask_24[b]) {
6803acfc
TJ
759 struct ipt_acc_mask_24 *network =
760 (struct ipt_acc_mask_24*)network_16->mask_24[b];
39f2f255 761 if (ipt_acc_handle_copy_data(to_user, &to_user_pos,
32cd84a6 762 &tmpbuf_pos, network, net_ip, (b << 16)))
dc668ad3 763 return -1;
bea3921b
TJ
764 }
765 }
2ea7a43c 766
06817fca 767 /* Flush remaining data to userspace */
9c44d30e 768 if (tmpbuf_pos)
32cd84a6 769 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
6fe5ced9 770 return -1;
2ea7a43c 771
bea3921b
TJ
772 return 0;
773 }
2ea7a43c 774
06817fca 775 /* 24 bit network */
2ea7a43c 776 if (depth == 2) {
6803acfc
TJ
777 struct ipt_acc_mask_8 *network_8 =
778 (struct ipt_acc_mask_8*)ipt_acc_handles[handle].data;
6fe5ced9 779 u_int32_t a, b;
2ea7a43c
TJ
780 for (a = 0; a <= 255; a++) {
781 if (network_8->mask_16[a]) {
6803acfc
TJ
782 struct ipt_acc_mask_16 *network_16 =
783 (struct ipt_acc_mask_16*)network_8->mask_16[a];
2ea7a43c
TJ
784 for (b = 0; b <= 255; b++) {
785 if (network_16->mask_24[b]) {
6803acfc
TJ
786 struct ipt_acc_mask_24 *network =
787 (struct ipt_acc_mask_24*)network_16->mask_24[b];
39f2f255 788 if (ipt_acc_handle_copy_data(to_user,
32cd84a6
TJ
789 &to_user_pos, &tmpbuf_pos,
790 network, net_ip, (a << 8) | (b << 16)))
dc668ad3 791 return -1;
bea3921b
TJ
792 }
793 }
794 }
795 }
2ea7a43c 796
06817fca 797 /* Flush remaining data to userspace */
9c44d30e 798 if (tmpbuf_pos)
32cd84a6 799 if (copy_to_user(to_user+to_user_pos, ipt_acc_tmpbuf, tmpbuf_pos))
6fe5ced9 800 return -1;
2ea7a43c 801
bea3921b
TJ
802 return 0;
803 }
e1b66101 804
bea3921b
TJ
805 return -1;
806}
807
6803acfc 808static int ipt_acc_set_ctl(struct sock *sk, int cmd,
6fe5ced9 809 void *user, u_int32_t len)
e1b66101 810{
6803acfc 811 struct ipt_acc_handle_sockopt handle;
bea3921b
TJ
812 int ret = -EINVAL;
813
814 if (!capable(CAP_NET_ADMIN))
2ea7a43c
TJ
815 return -EPERM;
816
817 switch (cmd) {
818 case IPT_SO_SET_ACCOUNT_HANDLE_FREE:
6803acfc
TJ
819 if (len != sizeof(struct ipt_acc_handle_sockopt)) {
820 printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) "
013fcc80 821 "for IPT_SO_SET_HANDLE_FREE\n",
6803acfc 822 len, sizeof(struct ipt_acc_handle_sockopt));
2ea7a43c
TJ
823 break;
824 }
825
826 if (copy_from_user (&handle, user, len)) {
6803acfc 827 printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for "
013fcc80 828 "IPT_SO_SET_HANDLE_FREE\n");
bea3921b 829 break;
2ea7a43c
TJ
830 }
831
b8041594 832 down(&ipt_acc_userspace_mutex);
6803acfc 833 ret = ipt_acc_handle_free(handle.handle_nr);
b8041594 834 up(&ipt_acc_userspace_mutex);
2ea7a43c
TJ
835 break;
836 case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: {
6fe5ced9 837 u_int32_t i;
b8041594 838 down(&ipt_acc_userspace_mutex);
5b15c288 839 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
6803acfc 840 ipt_acc_handle_free(i);
b8041594 841 up(&ipt_acc_userspace_mutex);
5b15c288
TJ
842 ret = 0;
843 break;
844 }
2ea7a43c 845 default:
6803acfc 846 printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd);
bea3921b
TJ
847 }
848
849 return ret;
850}
851
6803acfc 852static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
e1b66101 853{
6803acfc 854 struct ipt_acc_handle_sockopt handle;
bea3921b
TJ
855 int ret = -EINVAL;
856
857 if (!capable(CAP_NET_ADMIN))
2ea7a43c
TJ
858 return -EPERM;
859
860 switch (cmd) {
861 case IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH:
b8041594
TJ
862 case IPT_SO_GET_ACCOUNT_PREPARE_READ: {
863 struct ipt_acc_handle dest;
864
865 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
866 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) "
867 "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n",
868 *len, sizeof(struct ipt_acc_handle_sockopt));
869 break;
870 }
871
872 if (copy_from_user (&handle, user,
873 sizeof(struct ipt_acc_handle_sockopt))) {
874 return -EFAULT;
875 break;
876 }
877
878 LOCK_BH(&ipt_acc_lock);
879 if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH)
880 ret = ipt_acc_handle_prepare_read_flush(
881 handle.name, &dest, &handle.itemcount);
882 else
883 ret = ipt_acc_handle_prepare_read(
884 handle.name, &dest, &handle.itemcount);
885 UNLOCK_BH(&ipt_acc_lock);
886 // Error occured during prepare_read?
887 if (ret == -1)
888 return -EINVAL;
889
890 /* Allocate a userspace handle */
891 down(&ipt_acc_userspace_mutex);
892 if ((handle.handle_nr = ipt_acc_handle_find_slot()) == -1) {
893 ipt_acc_data_free(dest.data, dest.depth);
8bea9bf9 894 up(&ipt_acc_userspace_mutex);
b8041594
TJ
895 return -EINVAL;
896 }
897 memcpy(&ipt_acc_handles[handle.handle_nr], &dest,
898 sizeof(struct ipt_acc_handle));
899 up(&ipt_acc_userspace_mutex);
900
901 if (copy_to_user(user, &handle,
6803acfc 902 sizeof(struct ipt_acc_handle_sockopt))) {
b8041594
TJ
903 return -EFAULT;
904 break;
905 }
906 ret = 0;
2ea7a43c
TJ
907 break;
908 }
2ea7a43c 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
b8041594 938 down(&ipt_acc_userspace_mutex);
6803acfc 939 ret = ipt_acc_handle_get_data(handle.handle_nr, user);
b8041594 940 up(&ipt_acc_userspace_mutex);
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: {
07eb98b4 950 u_int32_t i;
6803acfc
TJ
951 if (*len < sizeof(struct ipt_acc_handle_sockopt)) {
952 printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)"
013fcc80 953 " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n",
6803acfc 954 *len, sizeof(struct ipt_acc_handle_sockopt));
5b15c288 955 break;
2ea7a43c
TJ
956 }
957
06817fca 958 /* Find out how many handles are in use */
5b15c288 959 handle.itemcount = 0;
b8041594 960 down(&ipt_acc_userspace_mutex);
5b15c288 961 for (i = 0; i < ACCOUNT_MAX_HANDLES; i++)
6803acfc 962 if (ipt_acc_handles[i].data)
5b15c288 963 handle.itemcount++;
b8041594 964 up(&ipt_acc_userspace_mutex);
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: {
b8041594 975 u_int32_t size = 0, i, name_len;
07eb98b4 976 char *tnames;
b8041594
TJ
977
978 LOCK_BH(&ipt_acc_lock);
2ea7a43c 979
06817fca 980 /* Determine size of table names */
2ea7a43c 981 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
6803acfc
TJ
982 if (ipt_acc_tables[i].name[0] != 0)
983 size += strlen (ipt_acc_tables[i].name) + 1;
5b15c288 984 }
06817fca 985 size += 1; /* Terminating NULL character */
2ea7a43c 986
b8041594
TJ
987 if (*len < size || size > PAGE_SIZE) {
988 UNLOCK_BH(&ipt_acc_lock);
989 printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u < %lu)"
990 " to store table names\n", *len, size, PAGE_SIZE);
5b15c288
TJ
991 ret = -ENOMEM;
992 break;
993 }
06817fca 994 /* Copy table names to userspace */
b8041594 995 tnames = ipt_acc_tmpbuf;
2ea7a43c 996 for (i = 0; i < ACCOUNT_MAX_TABLES; i++) {
6803acfc 997 if (ipt_acc_tables[i].name[0] != 0) {
b8041594
TJ
998 name_len = strlen (ipt_acc_tables[i].name) + 1;
999 memcpy(tnames, ipt_acc_tables[i].name, name_len);
1000 tnames += name_len;
5b15c288
TJ
1001 }
1002 }
b8041594
TJ
1003 UNLOCK_BH(&ipt_acc_lock);
1004
1005 /* Terminating NULL character */
1006 *tnames = 0;
1007
1008 /* Transfer to userspace */
1009 if (copy_to_user(user, ipt_acc_tmpbuf, size))
6fe5ced9 1010 return -EFAULT;
b8041594 1011
5b15c288
TJ
1012 ret = 0;
1013 break;
1014 }
2ea7a43c 1015 default:
6803acfc 1016 printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd);
bea3921b
TJ
1017 }
1018
1019 return ret;
1020}
1021
6803acfc 1022static struct ipt_target ipt_acc_reg = {
6fe5ced9
TJ
1023 .name = "ACCOUNT",
1024 .target = ipt_acc_target,
1025 .checkentry = ipt_acc_checkentry,
1026 .destroy = ipt_acc_deleteentry,
1027 .me = THIS_MODULE
013fcc80
GE
1028};
1029
6803acfc 1030static struct nf_sockopt_ops ipt_acc_sockopts = {
6fe5ced9
TJ
1031 .pf = PF_INET,
1032 .set_optmin = IPT_SO_SET_ACCOUNT_HANDLE_FREE,
1033 .set_optmax = IPT_SO_SET_ACCOUNT_MAX+1,
1034 .set = ipt_acc_set_ctl,
1035 .get_optmin = IPT_SO_GET_ACCOUNT_PREPARE_READ,
1036 .get_optmax = IPT_SO_GET_ACCOUNT_MAX+1,
1037 .get = ipt_acc_get_ctl
013fcc80 1038};
2ea7a43c 1039
e1b66101
TJ
1040static int __init init(void)
1041{
b8041594
TJ
1042 init_MUTEX(&ipt_acc_userspace_mutex);
1043
6803acfc 1044 if ((ipt_acc_tables =
013fcc80 1045 kmalloc(ACCOUNT_MAX_TABLES *
6803acfc 1046 sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) {
bea3921b 1047 printk("ACCOUNT: Out of memory allocating account_tables structure");
6fe5ced9 1048 goto error_cleanup;
70288420 1049 }
6803acfc
TJ
1050 memset(ipt_acc_tables, 0,
1051 ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
2ea7a43c 1052
6803acfc 1053 if ((ipt_acc_handles =
013fcc80 1054 kmalloc(ACCOUNT_MAX_HANDLES *
6803acfc 1055 sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) {
bea3921b 1056 printk("ACCOUNT: Out of memory allocating account_handles structure");
6fe5ced9 1057 goto error_cleanup;
70288420 1058 }
6803acfc
TJ
1059 memset(ipt_acc_handles, 0,
1060 ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
bea3921b 1061
06817fca 1062 /* Allocate one page as temporary storage */
6803acfc 1063 if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) {
9c44d30e 1064 printk("ACCOUNT: Out of memory for temporary buffer page\n");
6fe5ced9 1065 goto error_cleanup;
9c44d30e 1066 }
2ea7a43c 1067
bea3921b 1068 /* Register setsockopt */
6803acfc 1069 if (nf_register_sockopt(&ipt_acc_sockopts) < 0) {
bea3921b 1070 printk("ACCOUNT: Can't register sockopts. Aborting\n");
6fe5ced9 1071 goto error_cleanup;
bea3921b 1072 }
2ea7a43c 1073
6803acfc 1074 if (ipt_register_target(&ipt_acc_reg))
6fe5ced9
TJ
1075 goto error_cleanup;
1076
70288420 1077 return 0;
6fe5ced9
TJ
1078
1079error_cleanup:
1080 if(ipt_acc_tables)
1081 kfree(ipt_acc_tables);
1082 if(ipt_acc_handles)
1083 kfree(ipt_acc_handles);
1084 if (ipt_acc_tmpbuf)
1085 free_page((unsigned long)ipt_acc_tmpbuf);
1086
1087 return -EINVAL;
70288420
TJ
1088}
1089
e1b66101
TJ
1090static void __exit fini(void)
1091{
6803acfc 1092 ipt_unregister_target(&ipt_acc_reg);
70288420 1093
6803acfc 1094 nf_unregister_sockopt(&ipt_acc_sockopts);
2ea7a43c 1095
6803acfc
TJ
1096 kfree(ipt_acc_tables);
1097 kfree(ipt_acc_handles);
1098 free_page((unsigned long)ipt_acc_tmpbuf);
70288420
TJ
1099}
1100
1101module_init(init);
1102module_exit(fini);
1103MODULE_LICENSE("GPL");