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