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