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