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