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