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