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