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