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