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