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 | ||
6803acfc TJ |
38 | struct ipt_acc_table *ipt_acc_tables = NULL; |
39 | struct ipt_acc_handle *ipt_acc_handles = NULL; | |
40 | void *ipt_acc_tmpbuf = NULL; | |
70288420 | 41 | |
2ea7a43c | 42 | /* Spinlock used for manipulating the current accounting tables/data */ |
6803acfc | 43 | static spinlock_t ipt_acc_lock = SPIN_LOCK_UNLOCKED; |
2ea7a43c | 44 | /* Spinlock used for manipulating userspace handles/snapshot data */ |
6803acfc | 45 | static spinlock_t ipt_acc_userspace_lock = SPIN_LOCK_UNLOCKED; |
9c44d30e | 46 | |
70288420 | 47 | |
70288420 | 48 | /* Recursive free of all data structures */ |
6803acfc | 49 | void ipt_acc_data_free(void *data, unsigned char depth) |
e1b66101 | 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) { |
6803acfc | 64 | struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_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 | 80 | for (a=0; a <= 255; a++) { |
6803acfc TJ |
81 | if (((struct ipt_acc_mask_8 *)data)->mask_16[a]) { |
82 | struct ipt_acc_mask_16 *mask_16 = (struct ipt_acc_mask_16*) | |
83 | ((struct ipt_acc_mask_8 *)data)->mask_16[a]; | |
e1b66101 | 84 | |
2ea7a43c | 85 | for (b=0; b <= 255; b++) { |
70288420 | 86 | if (mask_16->mask_24[b]) { |
850cece6 | 87 | free_page((unsigned long)mask_16->mask_24[b]); |
70288420 TJ |
88 | 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 | |
6803acfc | 100 | printk("ACCOUNT: ipt_acc_data_free called with unknown depth: %d\n", |
013fcc80 | 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 */ | |
6803acfc | 107 | int ipt_acc_table_insert(char *name, unsigned int ip, unsigned int netmask) |
e1b66101 | 108 | { |
70288420 TJ |
109 | unsigned int i; |
110 | ||
6803acfc | 111 | DEBUGP("ACCOUNT: ipt_acc_table_insert: %s, %u.%u.%u.%u/%u.%u.%u.%u\n", |
e1b66101 | 112 | name, NIPQUAD(ip), NIPQUAD(netmask)); |
70288420 | 113 | |
06817fca | 114 | /* Look for existing table */ |
2ea7a43c | 115 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) { |
6803acfc | 116 | if (strncmp(ipt_acc_tables[i].name, name, |
013fcc80 GE |
117 | ACCOUNT_TABLE_NAME_LEN) == 0) { |
118 | DEBUGP("ACCOUNT: Found existing slot: %d - " | |
119 | "%u.%u.%u.%u/%u.%u.%u.%u\n", i, | |
6803acfc TJ |
120 | NIPQUAD(ipt_acc_tables[i].ip), |
121 | NIPQUAD(ipt_acc_tables[i].netmask)); | |
013fcc80 | 122 | |
6803acfc TJ |
123 | if (ipt_acc_tables[i].ip != ip |
124 | || ipt_acc_tables[i].netmask != netmask) { | |
013fcc80 GE |
125 | printk("ACCOUNT: Table %s found, but IP/netmask mismatch. " |
126 | "IP/netmask found: %u.%u.%u.%u/%u.%u.%u.%u\n", | |
6803acfc TJ |
127 | name, NIPQUAD(ipt_acc_tables[i].ip), |
128 | NIPQUAD(ipt_acc_tables[i].netmask)); | |
70288420 TJ |
129 | return -1; |
130 | } | |
131 | ||
6803acfc TJ |
132 | ipt_acc_tables[i].refcount++; |
133 | DEBUGP("ACCOUNT: Refcount: %d\n", ipt_acc_tables[i].refcount); | |
70288420 TJ |
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 */ |
6803acfc | 141 | if (ipt_acc_tables[i].name[0] == 0) { |
70288420 | 142 | DEBUGP("ACCOUNT: Found free slot: %d\n", i); |
2ea7a43c | 143 | |
6803acfc | 144 | strncpy (ipt_acc_tables[i].name, name, ACCOUNT_TABLE_NAME_LEN-1); |
2ea7a43c | 145 | |
6803acfc TJ |
146 | ipt_acc_tables[i].ip = ip; |
147 | ipt_acc_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 | 160 | if (netsize >= 24) |
6803acfc | 161 | ipt_acc_tables[i].depth = 0; |
850cece6 | 162 | else if (netsize >= 16) |
6803acfc | 163 | ipt_acc_tables[i].depth = 1; |
850cece6 | 164 | else if(netsize >= 8) |
6803acfc | 165 | ipt_acc_tables[i].depth = 2; |
2ea7a43c | 166 | |
013fcc80 | 167 | DEBUGP("ACCOUNT: calculated netsize: %u -> " |
6803acfc TJ |
168 | "ipt_acc_table depth %u\n", netsize, |
169 | ipt_acc_tables[i].depth); | |
2ea7a43c | 170 | |
6803acfc TJ |
171 | ipt_acc_tables[i].refcount++; |
172 | if ((ipt_acc_tables[i].data | |
013fcc80 | 173 | = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) { |
850cece6 | 174 | printk("ACCOUNT: out of memory for data of table: %s\n", name); |
6803acfc TJ |
175 | memset(&ipt_acc_tables[i], 0, |
176 | sizeof(struct ipt_acc_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 | ||
6803acfc | 190 | static int ipt_acc_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 | { | |
6803acfc | 196 | struct ipt_acc_info *info = targinfo; |
2ea7a43c | 197 | |
6803acfc | 198 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) { |
70288420 | 199 | DEBUGP("ACCOUNT: targinfosize %u != %u\n", |
6803acfc | 200 | targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info))); |
70288420 TJ |
201 | return 0; |
202 | } | |
203 | ||
6803acfc TJ |
204 | spin_lock_bh(&ipt_acc_lock); |
205 | int table_nr = ipt_acc_table_insert(info->table_name, info->net_ip, | |
013fcc80 | 206 | info->net_mask); |
2ea7a43c | 207 | if (table_nr == -1) { |
70288420 | 208 | printk("ACCOUNT: Table insert problem. Aborting\n"); |
6803acfc | 209 | spin_unlock_bh(&ipt_acc_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 | |
6803acfc | 216 | spin_unlock_bh(&ipt_acc_lock); |
850cece6 | 217 | |
70288420 TJ |
218 | return 1; |
219 | } | |
220 | ||
6803acfc | 221 | void ipt_acc_deleteentry(void *targinfo, unsigned int targinfosize) |
e1b66101 | 222 | { |
850cece6 | 223 | unsigned int i; |
6803acfc | 224 | struct ipt_acc_info *info = targinfo; |
2ea7a43c | 225 | |
6803acfc | 226 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_acc_info))) { |
850cece6 | 227 | DEBUGP("ACCOUNT: targinfosize %u != %u\n", |
6803acfc | 228 | targinfosize, IPT_ALIGN(sizeof(struct ipt_acc_info))); |
850cece6 TJ |
229 | } |
230 | ||
6803acfc | 231 | spin_lock_bh(&ipt_acc_lock); |
2ea7a43c | 232 | |
6803acfc | 233 | DEBUGP("ACCOUNT: ipt_acc_deleteentry called for table: %s (#%d)\n", |
013fcc80 | 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++) { |
6803acfc | 240 | if (strncmp(ipt_acc_tables[i].name, info->table_name, |
013fcc80 | 241 | ACCOUNT_TABLE_NAME_LEN) == 0) { |
850cece6 | 242 | DEBUGP("ACCOUNT: Found table at slot: %d\n", i); |
2ea7a43c | 243 | |
6803acfc | 244 | ipt_acc_tables[i].refcount--; |
013fcc80 | 245 | DEBUGP("ACCOUNT: Refcount left: %d\n", |
6803acfc | 246 | ipt_acc_tables[i].refcount); |
850cece6 | 247 | |
06817fca | 248 | /* Table not needed anymore? */ |
6803acfc | 249 | if (ipt_acc_tables[i].refcount == 0) { |
850cece6 | 250 | DEBUGP("ACCOUNT: Destroying table at slot: %d\n", i); |
6803acfc TJ |
251 | ipt_acc_data_free(ipt_acc_tables[i].data, |
252 | ipt_acc_tables[i].depth); | |
253 | memset(&ipt_acc_tables[i], 0, | |
254 | sizeof(struct ipt_acc_table)); | |
850cece6 | 255 | } |
2ea7a43c | 256 | |
6803acfc | 257 | spin_unlock_bh(&ipt_acc_lock); |
850cece6 TJ |
258 | return; |
259 | } | |
260 | } | |
261 | ||
06817fca | 262 | /* Table not found */ |
850cece6 | 263 | printk("ACCOUNT: Table %s not found for destroy\n", info->table_name); |
6803acfc | 264 | spin_unlock_bh(&ipt_acc_lock); |
850cece6 TJ |
265 | } |
266 | ||
6803acfc | 267 | void ipt_acc_depth0_insert(struct ipt_acc_mask_24 *mask_24, |
e1b66101 TJ |
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 | |
6803acfc | 274 | DEBUGP("ACCOUNT: ipt_acc_depth0_insert: %u.%u.%u.%u/%u.%u.%u.%u " |
013fcc80 GE |
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 | ||
6803acfc | 343 | void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16, |
013fcc80 GE |
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 | |
6803acfc | 360 | ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot], |
013fcc80 | 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 | |
6803acfc | 376 | ipt_acc_depth0_insert((struct ipt_acc_mask_24 *)mask_16->mask_24[slot], |
013fcc80 | 377 | net_ip, netmask, 0, dst_ip, size, itemcount); |
4068e14d TJ |
378 | } |
379 | } | |
380 | ||
6803acfc | 381 | void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8, |
013fcc80 GE |
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 | |
6803acfc | 398 | ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot], |
013fcc80 | 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 | |
6803acfc | 414 | ipt_acc_depth1_insert((struct ipt_acc_mask_16 *)mask_8->mask_16[slot], |
013fcc80 | 415 | net_ip, netmask, 0, dst_ip, size, itemcount); |
4068e14d TJ |
416 | } |
417 | } | |
418 | ||
6803acfc | 419 | static unsigned int ipt_acc_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 | { | |
6803acfc TJ |
426 | const struct ipt_acc_info *info = |
427 | (const struct ipt_acc_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 | |
6803acfc | 432 | spin_lock_bh(&ipt_acc_lock); |
2ea7a43c | 433 | |
6803acfc TJ |
434 | if (ipt_acc_tables[info->table_nr].name[0] == 0) { |
435 | printk("ACCOUNT: ipt_acc_target: Invalid table id %u. " | |
013fcc80 GE |
436 | "IPs %u.%u.%u.%u/%u.%u.%u.%u\n", info->table_nr, |
437 | NIPQUAD(src_ip), NIPQUAD(dst_ip)); | |
6803acfc | 438 | spin_unlock_bh(&ipt_acc_lock); |
4068e14d TJ |
439 | return IPT_CONTINUE; |
440 | } | |
2ea7a43c | 441 | |
06817fca | 442 | /* 8 bit network or "any" network */ |
6803acfc | 443 | if (ipt_acc_tables[info->table_nr].depth == 0) { |
06817fca | 444 | /* Count packet and check if the IP is new */ |
6803acfc TJ |
445 | ipt_acc_depth0_insert( |
446 | (struct ipt_acc_mask_24 *)ipt_acc_tables[info->table_nr].data, | |
447 | ipt_acc_tables[info->table_nr].ip, | |
448 | ipt_acc_tables[info->table_nr].netmask, | |
449 | src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount); | |
450 | spin_unlock_bh(&ipt_acc_lock); | |
4068e14d | 451 | return IPT_CONTINUE; |
2ea7a43c TJ |
452 | } |
453 | ||
06817fca | 454 | /* 16 bit network */ |
6803acfc TJ |
455 | if (ipt_acc_tables[info->table_nr].depth == 1) { |
456 | ipt_acc_depth1_insert( | |
457 | (struct ipt_acc_mask_16 *)ipt_acc_tables[info->table_nr].data, | |
458 | ipt_acc_tables[info->table_nr].ip, | |
459 | ipt_acc_tables[info->table_nr].netmask, | |
460 | src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount); | |
461 | spin_unlock_bh(&ipt_acc_lock); | |
4068e14d TJ |
462 | return IPT_CONTINUE; |
463 | } | |
2ea7a43c | 464 | |
06817fca | 465 | /* 24 bit network */ |
6803acfc TJ |
466 | if (ipt_acc_tables[info->table_nr].depth == 2) { |
467 | ipt_acc_depth2_insert( | |
468 | (struct ipt_acc_mask_8 *)ipt_acc_tables[info->table_nr].data, | |
469 | ipt_acc_tables[info->table_nr].ip, | |
470 | ipt_acc_tables[info->table_nr].netmask, | |
471 | src_ip, dst_ip, size, &ipt_acc_tables[info->table_nr].itemcount); | |
472 | spin_unlock_bh(&ipt_acc_lock); | |
4068e14d TJ |
473 | return IPT_CONTINUE; |
474 | } | |
2ea7a43c | 475 | |
6803acfc | 476 | printk("ACCOUNT: ipt_acc_target: Unable to process packet. " |
013fcc80 | 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 | ||
6803acfc | 480 | spin_unlock_bh(&ipt_acc_lock); |
4068e14d TJ |
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 | */ | |
6803acfc | 501 | int ipt_acc_handle_find_slot(void) |
e1b66101 | 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 */ |
6803acfc | 507 | if (ipt_acc_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 | ||
6803acfc | 521 | int ipt_acc_handle_free(unsigned int handle) |
e1b66101 | 522 | { |
2ea7a43c | 523 | if (handle >= ACCOUNT_MAX_HANDLES) { |
6803acfc | 524 | printk("ACCOUNT: Invalid handle for ipt_acc_handle_free() specified:" |
013fcc80 | 525 | " %u\n", handle); |
bea3921b TJ |
526 | return -EINVAL; |
527 | } | |
2ea7a43c | 528 | |
6803acfc TJ |
529 | ipt_acc_data_free(ipt_acc_handles[handle].data, |
530 | ipt_acc_handles[handle].depth); | |
531 | memset (&ipt_acc_handles[handle], 0, sizeof (struct ipt_acc_handle)); | |
bea3921b TJ |
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 */ | |
6803acfc | 537 | int ipt_acc_handle_prepare_read(char *tablename, unsigned int *count) |
e1b66101 | 538 | { |
bea3921b | 539 | int handle, i, table_nr=-1; |
2ea7a43c | 540 | |
bea3921b | 541 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) |
6803acfc | 542 | if (strncmp(ipt_acc_tables[i].name, tablename, |
013fcc80 | 543 | ACCOUNT_TABLE_NAME_LEN) == 0) { |
bea3921b TJ |
544 | table_nr = i; |
545 | break; | |
546 | } | |
547 | ||
2ea7a43c | 548 | if (table_nr == -1) { |
6803acfc | 549 | printk("ACCOUNT: ipt_acc_handle_prepare_read(): " |
013fcc80 | 550 | "Table %s not found\n", tablename); |
bea3921b TJ |
551 | return -1; |
552 | } | |
2ea7a43c | 553 | |
06817fca | 554 | /* Can't find a free handle slot? */ |
6803acfc | 555 | if ((handle = ipt_acc_handle_find_slot()) == -1) |
bea3921b | 556 | return -1; |
2ea7a43c | 557 | |
06817fca | 558 | /* Fill up handle structure */ |
6803acfc TJ |
559 | ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip; |
560 | ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth; | |
561 | ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount; | |
2ea7a43c | 562 | |
06817fca | 563 | /* allocate "root" table */ |
6803acfc | 564 | if ((ipt_acc_handles[handle].data = |
013fcc80 GE |
565 | (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) { |
566 | printk("ACCOUNT: out of memory for root table " | |
6803acfc TJ |
567 | "in ipt_acc_handle_prepare_read()\n"); |
568 | memset (&ipt_acc_handles[handle], 0, | |
569 | sizeof(struct ipt_acc_handle)); | |
2ea7a43c | 570 | return -1; |
bea3921b | 571 | } |
2ea7a43c | 572 | |
06817fca | 573 | /* Recursive copy of complete data structure */ |
6803acfc | 574 | unsigned int depth = ipt_acc_handles[handle].depth; |
2ea7a43c | 575 | if (depth == 0) { |
6803acfc TJ |
576 | memcpy(ipt_acc_handles[handle].data, |
577 | ipt_acc_tables[table_nr].data, | |
578 | sizeof(struct ipt_acc_mask_24)); | |
bea3921b | 579 | } else if (depth == 1) { |
6803acfc TJ |
580 | struct ipt_acc_mask_16 *src_16 = |
581 | (struct ipt_acc_mask_16 *)ipt_acc_tables[table_nr].data; | |
582 | struct ipt_acc_mask_16 *network_16 = | |
583 | (struct ipt_acc_mask_16 *)ipt_acc_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 " | |
6803acfc TJ |
591 | "network in ipt_acc_handle_prepare_read()\n"); |
592 | ipt_acc_data_free(ipt_acc_handles[handle].data, depth); | |
593 | memset (&ipt_acc_handles[handle], 0, | |
594 | sizeof(struct ipt_acc_handle)); | |
2ea7a43c | 595 | return -1; |
bea3921b | 596 | } |
2ea7a43c | 597 | |
013fcc80 | 598 | memcpy(network_16->mask_24[b], src_16->mask_24[b], |
6803acfc | 599 | sizeof(struct ipt_acc_mask_24)); |
bea3921b TJ |
600 | } |
601 | } | |
602 | } else if(depth == 2) { | |
6803acfc TJ |
603 | struct ipt_acc_mask_8 *src_8 = |
604 | (struct ipt_acc_mask_8 *)ipt_acc_tables[table_nr].data; | |
605 | struct ipt_acc_mask_8 *network_8 = | |
606 | (struct ipt_acc_mask_8 *)ipt_acc_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" | |
6803acfc TJ |
614 | " in ipt_acc_handle_prepare_read()\n"); |
615 | ipt_acc_data_free(ipt_acc_handles[handle].data, depth); | |
616 | memset (&ipt_acc_handles[handle], 0, | |
617 | sizeof(struct ipt_acc_handle)); | |
2ea7a43c | 618 | return -1; |
bea3921b TJ |
619 | } |
620 | ||
013fcc80 | 621 | memcpy(network_8->mask_16[a], src_8->mask_16[a], |
6803acfc | 622 | sizeof(struct ipt_acc_mask_16)); |
2ea7a43c | 623 | |
6803acfc TJ |
624 | struct ipt_acc_mask_16 *src_16 = src_8->mask_16[a]; |
625 | struct ipt_acc_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" | |
6803acfc TJ |
633 | " network in ipt_acc_handle_prepare_read()\n"); |
634 | ipt_acc_data_free(ipt_acc_handles[handle].data, | |
013fcc80 | 635 | depth); |
6803acfc TJ |
636 | memset (&ipt_acc_handles[handle], 0, |
637 | sizeof(struct ipt_acc_handle)); | |
2ea7a43c | 638 | return -1; |
bea3921b | 639 | } |
2ea7a43c | 640 | |
013fcc80 | 641 | memcpy(network_16->mask_24[b], src_16->mask_24[b], |
6803acfc | 642 | sizeof(struct ipt_acc_mask_24)); |
bea3921b TJ |
643 | } |
644 | } | |
645 | } | |
646 | } | |
647 | } | |
648 | ||
6803acfc | 649 | *count = ipt_acc_tables[table_nr].itemcount; |
bea3921b TJ |
650 | return handle; |
651 | } | |
652 | ||
653 | /* Prepare data for read and flush it */ | |
6803acfc | 654 | int ipt_acc_handle_prepare_read_flush(char *tablename, unsigned int *count) |
e1b66101 | 655 | { |
bea3921b | 656 | int handle, i, table_nr=-1; |
2ea7a43c | 657 | |
bea3921b | 658 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) |
6803acfc | 659 | if (strncmp(ipt_acc_tables[i].name, tablename, |
013fcc80 | 660 | ACCOUNT_TABLE_NAME_LEN) == 0) { |
bea3921b TJ |
661 | table_nr = i; |
662 | break; | |
663 | } | |
664 | ||
2ea7a43c | 665 | if (table_nr == -1) { |
6803acfc | 666 | printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): " |
013fcc80 | 667 | "Table %s not found\n", tablename); |
bea3921b TJ |
668 | return -1; |
669 | } | |
2ea7a43c | 670 | |
06817fca | 671 | /* Can't find a free handle slot? */ |
6803acfc | 672 | if ((handle = ipt_acc_handle_find_slot()) == -1) |
bea3921b | 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 | { | |
6803acfc | 679 | printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): " |
013fcc80 | 680 | "Out of memory!\n"); |
ad773892 TJ |
681 | return -1; |
682 | } | |
683 | ||
06817fca | 684 | /* Fill up handle structure */ |
6803acfc TJ |
685 | ipt_acc_handles[handle].ip = ipt_acc_tables[table_nr].ip; |
686 | ipt_acc_handles[handle].depth = ipt_acc_tables[table_nr].depth; | |
687 | ipt_acc_handles[handle].itemcount = ipt_acc_tables[table_nr].itemcount; | |
688 | ipt_acc_handles[handle].data = ipt_acc_tables[table_nr].data; | |
689 | *count = ipt_acc_tables[table_nr].itemcount; | |
bea3921b | 690 | |
06817fca | 691 | /* "Flush" table data */ |
6803acfc TJ |
692 | ipt_acc_tables[table_nr].data = new_data_page; |
693 | ipt_acc_tables[table_nr].itemcount = 0; | |
bea3921b TJ |
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 | */ |
6803acfc TJ |
701 | void ipt_acc_handle_copy_data(void *to_user, int *pos, |
702 | struct ipt_acc_mask_24 *data, | |
e1b66101 TJ |
703 | unsigned int net_ip, unsigned int net_OR_mask) |
704 | { | |
6803acfc TJ |
705 | struct ipt_acc_handle_ip handle_ip; |
706 | unsigned int handle_ip_size = sizeof (struct ipt_acc_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) { | |
6803acfc | 721 | copy_to_user(to_user, ipt_acc_tmpbuf, *pos); |
e1b66101 TJ |
722 | *pos = 0; |
723 | } | |
6803acfc | 724 | memcpy(ipt_acc_tmpbuf+*pos, &handle_ip, handle_ip_size); |
e1b66101 TJ |
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. | |
6803acfc | 732 | Overwrites ipt_acc_tmpbuf. |
e1b66101 | 733 | */ |
6803acfc | 734 | int ipt_acc_handle_get_data(unsigned int handle, void *to_user) |
e1b66101 TJ |
735 | { |
736 | unsigned int tmpbuf_pos=0; | |
bea3921b | 737 | |
2ea7a43c | 738 | if (handle >= ACCOUNT_MAX_HANDLES) { |
6803acfc | 739 | printk("ACCOUNT: invalid handle for ipt_acc_handle_get_data() " |
013fcc80 | 740 | "specified: %u\n", handle); |
bea3921b TJ |
741 | return -1; |
742 | } | |
2ea7a43c | 743 | |
6803acfc | 744 | if (ipt_acc_handles[handle].data == NULL) { |
bea3921b TJ |
745 | printk("ACCOUNT: handle %u is BROKEN: Contains no data\n", handle); |
746 | return -1; | |
747 | } | |
2ea7a43c | 748 | |
6803acfc TJ |
749 | unsigned int net_ip = ipt_acc_handles[handle].ip; |
750 | unsigned int depth = ipt_acc_handles[handle].depth; | |
2ea7a43c | 751 | |
06817fca | 752 | /* 8 bit network */ |
2ea7a43c | 753 | if (depth == 0) { |
6803acfc TJ |
754 | struct ipt_acc_mask_24 *network = |
755 | (struct ipt_acc_mask_24*)ipt_acc_handles[handle].data; | |
756 | ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, net_ip, 0); | |
e1b66101 | 757 | |
06817fca | 758 | /* Flush remaining data to userspace */ |
9c44d30e | 759 | if (tmpbuf_pos) |
6803acfc | 760 | copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos); |
2ea7a43c | 761 | |
bea3921b TJ |
762 | return 0; |
763 | } | |
2ea7a43c | 764 | |
06817fca | 765 | /* 16 bit network */ |
2ea7a43c | 766 | if (depth == 1) { |
6803acfc TJ |
767 | struct ipt_acc_mask_16 *network_16 = |
768 | (struct ipt_acc_mask_16*)ipt_acc_handles[handle].data; | |
4adc8355 | 769 | unsigned int b; |
2ea7a43c TJ |
770 | for (b = 0; b <= 255; b++) { |
771 | if (network_16->mask_24[b]) { | |
6803acfc TJ |
772 | struct ipt_acc_mask_24 *network = |
773 | (struct ipt_acc_mask_24*)network_16->mask_24[b]; | |
774 | ipt_acc_handle_copy_data(to_user, &tmpbuf_pos, network, | |
013fcc80 | 775 | net_ip, (b << 16)); |
bea3921b TJ |
776 | } |
777 | } | |
2ea7a43c | 778 | |
06817fca | 779 | /* Flush remaining data to userspace */ |
9c44d30e | 780 | if (tmpbuf_pos) |
6803acfc | 781 | copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos); |
2ea7a43c | 782 | |
bea3921b TJ |
783 | return 0; |
784 | } | |
2ea7a43c | 785 | |
06817fca | 786 | /* 24 bit network */ |
2ea7a43c | 787 | if (depth == 2) { |
6803acfc TJ |
788 | struct ipt_acc_mask_8 *network_8 = |
789 | (struct ipt_acc_mask_8*)ipt_acc_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]) { | |
6803acfc TJ |
793 | struct ipt_acc_mask_16 *network_16 = |
794 | (struct ipt_acc_mask_16*)network_8->mask_16[a]; | |
2ea7a43c TJ |
795 | for (b = 0; b <= 255; b++) { |
796 | if (network_16->mask_24[b]) { | |
6803acfc TJ |
797 | struct ipt_acc_mask_24 *network = |
798 | (struct ipt_acc_mask_24*)network_16->mask_24[b]; | |
799 | ipt_acc_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) |
6803acfc | 808 | copy_to_user(to_user, ipt_acc_tmpbuf, tmpbuf_pos); |
2ea7a43c | 809 | |
bea3921b TJ |
810 | return 0; |
811 | } | |
e1b66101 | 812 | |
bea3921b TJ |
813 | return -1; |
814 | } | |
815 | ||
6803acfc | 816 | static int ipt_acc_set_ctl(struct sock *sk, int cmd, |
013fcc80 | 817 | void *user, unsigned int len) |
e1b66101 | 818 | { |
6803acfc | 819 | struct ipt_acc_handle_sockopt handle; |
bea3921b TJ |
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: | |
6803acfc TJ |
827 | if (len != sizeof(struct ipt_acc_handle_sockopt)) { |
828 | printk("ACCOUNT: ipt_acc_set_ctl: wrong data size (%u != %u) " | |
013fcc80 | 829 | "for IPT_SO_SET_HANDLE_FREE\n", |
6803acfc | 830 | len, sizeof(struct ipt_acc_handle_sockopt)); |
2ea7a43c TJ |
831 | break; |
832 | } | |
833 | ||
834 | if (copy_from_user (&handle, user, len)) { | |
6803acfc | 835 | printk("ACCOUNT: ipt_acc_set_ctl: copy_from_user failed for " |
013fcc80 | 836 | "IPT_SO_SET_HANDLE_FREE\n"); |
bea3921b | 837 | break; |
2ea7a43c TJ |
838 | } |
839 | ||
6803acfc TJ |
840 | spin_lock_bh(&ipt_acc_userspace_lock); |
841 | ret = ipt_acc_handle_free(handle.handle_nr); | |
842 | spin_unlock_bh(&ipt_acc_userspace_lock); | |
2ea7a43c TJ |
843 | break; |
844 | case IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL: { | |
5b15c288 | 845 | unsigned int i; |
6803acfc | 846 | spin_lock_bh(&ipt_acc_userspace_lock); |
5b15c288 | 847 | for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) |
6803acfc TJ |
848 | ipt_acc_handle_free(i); |
849 | spin_unlock_bh(&ipt_acc_userspace_lock); | |
5b15c288 TJ |
850 | ret = 0; |
851 | break; | |
852 | } | |
2ea7a43c | 853 | default: |
6803acfc | 854 | printk("ACCOUNT: ipt_acc_set_ctl: unknown request %i\n", cmd); |
bea3921b TJ |
855 | } |
856 | ||
857 | return ret; | |
858 | } | |
859 | ||
6803acfc | 860 | static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len) |
e1b66101 | 861 | { |
6803acfc | 862 | struct ipt_acc_handle_sockopt handle; |
bea3921b TJ |
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: | |
6803acfc TJ |
871 | if (*len < sizeof(struct ipt_acc_handle_sockopt)) { |
872 | printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u) " | |
013fcc80 | 873 | "for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n", |
6803acfc | 874 | *len, sizeof(struct ipt_acc_handle_sockopt)); |
bea3921b | 875 | break; |
2ea7a43c TJ |
876 | } |
877 | ||
013fcc80 | 878 | if (copy_from_user (&handle, user, |
6803acfc | 879 | sizeof(struct ipt_acc_handle_sockopt))) { |
230248c5 | 880 | return -EFAULT; |
2ea7a43c TJ |
881 | break; |
882 | } | |
883 | ||
6803acfc TJ |
884 | spin_lock_bh(&ipt_acc_lock); |
885 | spin_lock_bh(&ipt_acc_userspace_lock); | |
2ea7a43c | 886 | if (cmd == IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH) |
6803acfc | 887 | handle.handle_nr = ipt_acc_handle_prepare_read_flush( |
013fcc80 | 888 | handle.name, &handle.itemcount); |
2ea7a43c | 889 | else |
6803acfc | 890 | handle.handle_nr = ipt_acc_handle_prepare_read( |
013fcc80 | 891 | handle.name, &handle.itemcount); |
6803acfc TJ |
892 | spin_unlock_bh(&ipt_acc_userspace_lock); |
893 | spin_unlock_bh(&ipt_acc_lock); | |
2ea7a43c TJ |
894 | |
895 | if (handle.handle_nr == -1) { | |
230248c5 | 896 | return -EINVAL; |
2ea7a43c TJ |
897 | break; |
898 | } | |
899 | ||
013fcc80 | 900 | if (copy_to_user(user, &handle, |
6803acfc | 901 | sizeof(struct ipt_acc_handle_sockopt))) { |
230248c5 | 902 | return -EFAULT; |
2ea7a43c TJ |
903 | break; |
904 | } | |
905 | ret = 0; | |
906 | break; | |
907 | case IPT_SO_GET_ACCOUNT_GET_DATA: | |
6803acfc TJ |
908 | if (*len < sizeof(struct ipt_acc_handle_sockopt)) { |
909 | printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)" | |
013fcc80 | 910 | " for IPT_SO_GET_ACCOUNT_PREPARE_READ/READ_FLUSH\n", |
6803acfc | 911 | *len, sizeof(struct ipt_acc_handle_sockopt)); |
2ea7a43c TJ |
912 | break; |
913 | } | |
914 | ||
013fcc80 | 915 | if (copy_from_user (&handle, user, |
6803acfc | 916 | sizeof(struct ipt_acc_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 | ||
6803acfc TJ |
926 | if (*len < ipt_acc_handles[handle.handle_nr].itemcount |
927 | * sizeof(struct ipt_acc_handle_ip)) { | |
928 | printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)" | |
013fcc80 | 929 | " to store data from IPT_SO_GET_ACCOUNT_GET_DATA\n", |
6803acfc TJ |
930 | *len, ipt_acc_handles[handle.handle_nr].itemcount |
931 | * sizeof(struct ipt_acc_handle_ip)); | |
2ea7a43c | 932 | ret = -ENOMEM; |
bea3921b | 933 | break; |
2ea7a43c TJ |
934 | } |
935 | ||
6803acfc TJ |
936 | spin_lock_bh(&ipt_acc_userspace_lock); |
937 | ret = ipt_acc_handle_get_data(handle.handle_nr, user); | |
938 | spin_unlock_bh(&ipt_acc_userspace_lock); | |
2ea7a43c | 939 | if (ret) { |
6803acfc | 940 | printk("ACCOUNT: ipt_acc_get_ctl: ipt_acc_handle_get_data" |
013fcc80 | 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: { | |
6803acfc TJ |
948 | if (*len < sizeof(struct ipt_acc_handle_sockopt)) { |
949 | printk("ACCOUNT: ipt_acc_get_ctl: wrong data size (%u != %u)" | |
013fcc80 | 950 | " for IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE\n", |
6803acfc | 951 | *len, sizeof(struct ipt_acc_handle_sockopt)); |
5b15c288 | 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; | |
6803acfc | 958 | spin_lock_bh(&ipt_acc_userspace_lock); |
5b15c288 | 959 | for (i = 0; i < ACCOUNT_MAX_HANDLES; i++) |
6803acfc | 960 | if (ipt_acc_handles[i].data) |
5b15c288 | 961 | handle.itemcount++; |
6803acfc | 962 | spin_unlock_bh(&ipt_acc_userspace_lock); |
2ea7a43c | 963 | |
013fcc80 | 964 | if (copy_to_user(user, &handle, |
6803acfc | 965 | sizeof(struct ipt_acc_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: { |
6803acfc | 973 | spin_lock_bh(&ipt_acc_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++) { |
6803acfc TJ |
978 | if (ipt_acc_tables[i].name[0] != 0) |
979 | size += strlen (ipt_acc_tables[i].name) + 1; | |
5b15c288 | 980 | } |
06817fca | 981 | size += 1; /* Terminating NULL character */ |
2ea7a43c TJ |
982 | |
983 | if (*len < size) { | |
6803acfc TJ |
984 | spin_unlock_bh(&ipt_acc_lock); |
985 | printk("ACCOUNT: ipt_acc_get_ctl: not enough space (%u < %u)" | |
013fcc80 | 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 | 992 | for (i = 0; i < ACCOUNT_MAX_TABLES; i++) { |
6803acfc TJ |
993 | if (ipt_acc_tables[i].name[0] != 0) { |
994 | int len = strlen (ipt_acc_tables[i].name) + 1; | |
013fcc80 | 995 | /* copy string + terminating zero */ |
6803acfc | 996 | copy_to_user(tnames, ipt_acc_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); |
6803acfc | 1003 | spin_unlock_bh(&ipt_acc_lock); |
5b15c288 TJ |
1004 | ret = 0; |
1005 | break; | |
1006 | } | |
2ea7a43c | 1007 | default: |
6803acfc | 1008 | printk("ACCOUNT: ipt_acc_get_ctl: unknown request %i\n", cmd); |
bea3921b TJ |
1009 | } |
1010 | ||
1011 | return ret; | |
1012 | } | |
1013 | ||
6803acfc | 1014 | static struct ipt_target ipt_acc_reg = { |
013fcc80 GE |
1015 | { |
1016 | NULL, NULL | |
1017 | }, | |
1018 | "ACCOUNT", | |
6803acfc TJ |
1019 | ipt_acc_target, |
1020 | ipt_acc_checkentry, | |
1021 | ipt_acc_deleteentry, | |
013fcc80 GE |
1022 | THIS_MODULE |
1023 | }; | |
1024 | ||
6803acfc | 1025 | static struct nf_sockopt_ops ipt_acc_sockopts = { |
013fcc80 GE |
1026 | { |
1027 | NULL, NULL | |
1028 | }, | |
1029 | PF_INET, | |
1030 | IPT_SO_SET_ACCOUNT_HANDLE_FREE, | |
1031 | IPT_SO_SET_ACCOUNT_MAX+1, | |
6803acfc | 1032 | ipt_acc_set_ctl, |
013fcc80 GE |
1033 | IPT_SO_GET_ACCOUNT_PREPARE_READ, |
1034 | IPT_SO_GET_ACCOUNT_MAX+1, | |
6803acfc | 1035 | ipt_acc_get_ctl, |
013fcc80 GE |
1036 | 0, |
1037 | NULL | |
1038 | }; | |
2ea7a43c | 1039 | |
e1b66101 TJ |
1040 | static int __init init(void) |
1041 | { | |
6803acfc | 1042 | if ((ipt_acc_tables = |
013fcc80 | 1043 | kmalloc(ACCOUNT_MAX_TABLES * |
6803acfc | 1044 | sizeof(struct ipt_acc_table), GFP_KERNEL)) == NULL) { |
bea3921b TJ |
1045 | printk("ACCOUNT: Out of memory allocating account_tables structure"); |
1046 | return -EINVAL; | |
70288420 | 1047 | } |
6803acfc TJ |
1048 | memset(ipt_acc_tables, 0, |
1049 | ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table)); | |
2ea7a43c | 1050 | |
6803acfc | 1051 | if ((ipt_acc_handles = |
013fcc80 | 1052 | kmalloc(ACCOUNT_MAX_HANDLES * |
6803acfc | 1053 | sizeof(struct ipt_acc_handle), GFP_KERNEL)) == NULL) { |
bea3921b | 1054 | printk("ACCOUNT: Out of memory allocating account_handles structure"); |
6803acfc TJ |
1055 | kfree (ipt_acc_tables); |
1056 | ipt_acc_tables = NULL; | |
bea3921b | 1057 | return -EINVAL; |
70288420 | 1058 | } |
6803acfc TJ |
1059 | memset(ipt_acc_handles, 0, |
1060 | ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle)); | |
bea3921b | 1061 | |
06817fca | 1062 | /* Allocate one page as temporary storage */ |
6803acfc | 1063 | if ((ipt_acc_tmpbuf = (void*)__get_free_page(GFP_KERNEL)) == NULL) { |
9c44d30e | 1064 | printk("ACCOUNT: Out of memory for temporary buffer page\n"); |
6803acfc TJ |
1065 | kfree(ipt_acc_tables); |
1066 | kfree(ipt_acc_handles); | |
1067 | ipt_acc_tables = NULL; | |
1068 | ipt_acc_handles = NULL; | |
9c44d30e TJ |
1069 | return -EINVAL; |
1070 | } | |
2ea7a43c | 1071 | |
bea3921b | 1072 | /* Register setsockopt */ |
6803acfc | 1073 | if (nf_register_sockopt(&ipt_acc_sockopts) < 0) { |
bea3921b | 1074 | printk("ACCOUNT: Can't register sockopts. Aborting\n"); |
2ea7a43c | 1075 | |
6803acfc TJ |
1076 | kfree(ipt_acc_tables); |
1077 | kfree(ipt_acc_handles); | |
1078 | free_page((unsigned long)ipt_acc_tmpbuf); | |
1079 | ipt_acc_tables = NULL; | |
1080 | ipt_acc_handles = NULL; | |
1081 | ipt_acc_tmpbuf = NULL; | |
2ea7a43c | 1082 | |
bea3921b TJ |
1083 | return -EINVAL; |
1084 | } | |
2ea7a43c | 1085 | |
6803acfc | 1086 | if (ipt_register_target(&ipt_acc_reg)) |
2ea7a43c | 1087 | return -EINVAL; |
70288420 TJ |
1088 | |
1089 | return 0; | |
1090 | } | |
1091 | ||
e1b66101 TJ |
1092 | static void __exit fini(void) |
1093 | { | |
6803acfc | 1094 | ipt_unregister_target(&ipt_acc_reg); |
70288420 | 1095 | |
6803acfc | 1096 | nf_unregister_sockopt(&ipt_acc_sockopts); |
2ea7a43c | 1097 | |
6803acfc TJ |
1098 | kfree(ipt_acc_tables); |
1099 | kfree(ipt_acc_handles); | |
1100 | free_page((unsigned long)ipt_acc_tmpbuf); | |
2ea7a43c | 1101 | |
6803acfc TJ |
1102 | ipt_acc_tables = NULL; |
1103 | ipt_acc_handles = NULL; | |
1104 | ipt_acc_tmpbuf = NULL; | |
70288420 TJ |
1105 | } |
1106 | ||
1107 | module_init(init); | |
1108 | module_exit(fini); | |
1109 | MODULE_LICENSE("GPL"); |