1/* (C) 1999-2001 Paul `Rusty' Russell 2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 9#include <linux/types.h> 10#include <linux/netfilter.h> 11#include <linux/module.h> 12#include <linux/skbuff.h> 13#include <linux/proc_fs.h> 14#include <linux/seq_file.h> 15#include <linux/percpu.h> 16#include <linux/netdevice.h> 17#ifdef CONFIG_SYSCTL 18#include <linux/sysctl.h> 19#endif 20 21#include <net/netfilter/nf_conntrack.h> 22#include <net/netfilter/nf_conntrack_core.h> 23#include <net/netfilter/nf_conntrack_l3proto.h> 24#include <net/netfilter/nf_conntrack_l4proto.h> 25#include <net/netfilter/nf_conntrack_expect.h> 26#include <net/netfilter/nf_conntrack_helper.h> 27 28#define DEBUGP(format, args...) 29 30MODULE_LICENSE("GPL"); 31 32#ifdef CONFIG_PROC_FS 33int 34print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple, 35 struct nf_conntrack_l3proto *l3proto, 36 struct nf_conntrack_l4proto *l4proto) 37{ 38 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple); 39} 40EXPORT_SYMBOL_GPL(print_tuple); 41 42#ifdef CONFIG_NF_CT_ACCT 43static unsigned int 44seq_print_counters(struct seq_file *s, 45 const struct ip_conntrack_counter *counter) 46{ 47 return seq_printf(s, "packets=%llu bytes=%llu ", 48 (unsigned long long)counter->packets, 49 (unsigned long long)counter->bytes); 50} 51#else 52#define seq_print_counters(x, y) 0 53#endif 54 55struct ct_iter_state { 56 unsigned int bucket; 57}; 58 59static struct list_head *ct_get_first(struct seq_file *seq) 60{ 61 struct ct_iter_state *st = seq->private; 62 63 for (st->bucket = 0; 64 st->bucket < nf_conntrack_htable_size; 65 st->bucket++) { 66 if (!list_empty(&nf_conntrack_hash[st->bucket])) 67 return nf_conntrack_hash[st->bucket].next; 68 } 69 return NULL; 70} 71 72static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head) 73{ 74 struct ct_iter_state *st = seq->private; 75 76 head = head->next; 77 while (head == &nf_conntrack_hash[st->bucket]) { 78 if (++st->bucket >= nf_conntrack_htable_size) 79 return NULL; 80 head = nf_conntrack_hash[st->bucket].next; 81 } 82 return head; 83} 84 85static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos) 86{ 87 struct list_head *head = ct_get_first(seq); 88 89 if (head) 90 while (pos && (head = ct_get_next(seq, head))) 91 pos--; 92 return pos ? NULL : head; 93} 94 95static void *ct_seq_start(struct seq_file *seq, loff_t *pos) 96{ 97 read_lock_bh(&nf_conntrack_lock); 98 return ct_get_idx(seq, *pos); 99} 100 101static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) 102{ 103 (*pos)++; 104 return ct_get_next(s, v); 105} 106 107static void ct_seq_stop(struct seq_file *s, void *v) 108{ 109 read_unlock_bh(&nf_conntrack_lock); 110} 111 112/* return 0 on success, 1 in case of error */ 113static int ct_seq_show(struct seq_file *s, void *v) 114{ 115 const struct nf_conntrack_tuple_hash *hash = v; 116 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash); 117 struct nf_conntrack_l3proto *l3proto; 118 struct nf_conntrack_l4proto *l4proto; 119 120 NF_CT_ASSERT(conntrack); 121 122 /* we only want to print DIR_ORIGINAL */ 123 if (NF_CT_DIRECTION(hash)) 124 return 0; 125 126 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL] 127 .tuple.src.l3num); 128 129 NF_CT_ASSERT(l3proto); 130 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL] 131 .tuple.src.l3num, 132 conntrack->tuplehash[IP_CT_DIR_ORIGINAL] 133 .tuple.dst.protonum); 134 NF_CT_ASSERT(l4proto); 135 136 if (seq_printf(s, "%-8s %u %-8s %u %ld ", 137 l3proto->name, 138 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, 139 l4proto->name, 140 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum, 141 timer_pending(&conntrack->timeout) 142 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0) 143 return -ENOSPC; 144 145 if (l3proto->print_conntrack(s, conntrack)) 146 return -ENOSPC; 147 148 if (l4proto->print_conntrack(s, conntrack)) 149 return -ENOSPC; 150 151 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple, 152 l3proto, l4proto)) 153 return -ENOSPC; 154 155 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL])) 156 return -ENOSPC; 157 158 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status))) 159 if (seq_printf(s, "[UNREPLIED] ")) 160 return -ENOSPC; 161 162 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple, 163 l3proto, l4proto)) 164 return -ENOSPC; 165 166 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY])) 167 return -ENOSPC; 168 169 if (test_bit(IPS_ASSURED_BIT, &conntrack->status)) 170 if (seq_printf(s, "[ASSURED] ")) 171 return -ENOSPC; 172 173#if defined(CONFIG_NF_CONNTRACK_MARK) 174 if (seq_printf(s, "mark=%u ", conntrack->mark)) 175 return -ENOSPC; 176#endif 177 178#ifdef CONFIG_NF_CONNTRACK_SECMARK 179 if (seq_printf(s, "secmark=%u ", conntrack->secmark)) 180 return -ENOSPC; 181#endif 182 183 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use))) 184 return -ENOSPC; 185 186 return 0; 187} 188 189static struct seq_operations ct_seq_ops = { 190 .start = ct_seq_start, 191 .next = ct_seq_next, 192 .stop = ct_seq_stop, 193 .show = ct_seq_show 194}; 195 196static int ct_open(struct inode *inode, struct file *file) 197{ 198 struct seq_file *seq; 199 struct ct_iter_state *st; 200 int ret; 201 202 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL); 203 if (st == NULL) 204 return -ENOMEM; 205 ret = seq_open(file, &ct_seq_ops); 206 if (ret) 207 goto out_free; 208 seq = file->private_data; 209 seq->private = st; 210 memset(st, 0, sizeof(struct ct_iter_state)); 211 return ret; 212out_free: 213 kfree(st); 214 return ret; 215} 216 217static const struct file_operations ct_file_ops = { 218 .owner = THIS_MODULE, 219 .open = ct_open, 220 .read = seq_read, 221 .llseek = seq_lseek, 222 .release = seq_release_private, 223}; 224 225static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) 226{ 227 int cpu; 228 229 if (*pos == 0) 230 return SEQ_START_TOKEN; 231 232 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) { 233 if (!cpu_possible(cpu)) 234 continue; 235 *pos = cpu + 1; 236 return &per_cpu(nf_conntrack_stat, cpu); 237 } 238 239 return NULL; 240} 241 242static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 243{ 244 int cpu; 245 246 for (cpu = *pos; cpu < NR_CPUS; ++cpu) { 247 if (!cpu_possible(cpu)) 248 continue; 249 *pos = cpu + 1; 250 return &per_cpu(nf_conntrack_stat, cpu); 251 } 252 253 return NULL; 254} 255 256static void ct_cpu_seq_stop(struct seq_file *seq, void *v) 257{ 258} 259 260static int ct_cpu_seq_show(struct seq_file *seq, void *v) 261{ 262 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count); 263 struct ip_conntrack_stat *st = v; 264 265 if (v == SEQ_START_TOKEN) { 266 seq_printf(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete\n"); 267 return 0; 268 } 269 270 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " 271 "%08x %08x %08x %08x %08x %08x %08x %08x \n", 272 nr_conntracks, 273 st->searched, 274 st->found, 275 st->new, 276 st->invalid, 277 st->ignore, 278 st->delete, 279 st->delete_list, 280 st->insert, 281 st->insert_failed, 282 st->drop, 283 st->early_drop, 284 st->error, 285 286 st->expect_new, 287 st->expect_create, 288 st->expect_delete 289 ); 290 return 0; 291} 292 293static struct seq_operations ct_cpu_seq_ops = { 294 .start = ct_cpu_seq_start, 295 .next = ct_cpu_seq_next, 296 .stop = ct_cpu_seq_stop, 297 .show = ct_cpu_seq_show, 298}; 299 300static int ct_cpu_seq_open(struct inode *inode, struct file *file) 301{ 302 return seq_open(file, &ct_cpu_seq_ops); 303} 304 305static const struct file_operations ct_cpu_seq_fops = { 306 .owner = THIS_MODULE, 307 .open = ct_cpu_seq_open, 308 .read = seq_read, 309 .llseek = seq_lseek, 310 .release = seq_release_private, 311}; 312#endif /* CONFIG_PROC_FS */ 313 314/* Sysctl support */ 315 316int nf_conntrack_checksum __read_mostly = 1; 317EXPORT_SYMBOL_GPL(nf_conntrack_checksum); 318 319#ifdef CONFIG_SYSCTL 320/* Log invalid packets of a given protocol */ 321static int log_invalid_proto_min = 0; 322static int log_invalid_proto_max = 255; 323 324static struct ctl_table_header *nf_ct_sysctl_header; 325 326static ctl_table nf_ct_sysctl_table[] = { 327 { 328 .ctl_name = NET_NF_CONNTRACK_MAX, 329 .procname = "nf_conntrack_max", 330 .data = &nf_conntrack_max, 331 .maxlen = sizeof(int), 332 .mode = 0644, 333 .proc_handler = &proc_dointvec, 334 }, 335 { 336 .ctl_name = NET_NF_CONNTRACK_COUNT, 337 .procname = "nf_conntrack_count", 338 .data = &nf_conntrack_count, 339 .maxlen = sizeof(int), 340 .mode = 0444, 341 .proc_handler = &proc_dointvec, 342 }, 343 { 344 .ctl_name = NET_NF_CONNTRACK_BUCKETS, 345 .procname = "nf_conntrack_buckets", 346 .data = &nf_conntrack_htable_size, 347 .maxlen = sizeof(unsigned int), 348 .mode = 0444, 349 .proc_handler = &proc_dointvec, 350 }, 351 { 352 .ctl_name = NET_NF_CONNTRACK_CHECKSUM, 353 .procname = "nf_conntrack_checksum", 354 .data = &nf_conntrack_checksum, 355 .maxlen = sizeof(unsigned int), 356 .mode = 0644, 357 .proc_handler = &proc_dointvec, 358 }, 359 { 360 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID, 361 .procname = "nf_conntrack_log_invalid", 362 .data = &nf_ct_log_invalid, 363 .maxlen = sizeof(unsigned int), 364 .mode = 0644, 365 .proc_handler = &proc_dointvec_minmax, 366 .strategy = &sysctl_intvec, 367 .extra1 = &log_invalid_proto_min, 368 .extra2 = &log_invalid_proto_max, 369 }, 370 371 { .ctl_name = 0 } 372}; 373 374#define NET_NF_CONNTRACK_MAX 2089 375 376static ctl_table nf_ct_netfilter_table[] = { 377 { 378 .ctl_name = NET_NETFILTER, 379 .procname = "netfilter", 380 .mode = 0555, 381 .child = nf_ct_sysctl_table, 382 }, 383 { 384 .ctl_name = NET_NF_CONNTRACK_MAX, 385 .procname = "nf_conntrack_max", 386 .data = &nf_conntrack_max, 387 .maxlen = sizeof(int), 388 .mode = 0644, 389 .proc_handler = &proc_dointvec, 390 }, 391 { .ctl_name = 0 } 392}; 393 394static ctl_table nf_ct_net_table[] = { 395 { 396 .ctl_name = CTL_NET, 397 .procname = "net", 398 .mode = 0555, 399 .child = nf_ct_netfilter_table, 400 }, 401 { .ctl_name = 0 } 402}; 403EXPORT_SYMBOL_GPL(nf_ct_log_invalid); 404#endif /* CONFIG_SYSCTL */ 405 406static int __init nf_conntrack_standalone_init(void) 407{ 408#ifdef CONFIG_PROC_FS 409 struct proc_dir_entry *proc, *proc_exp, *proc_stat; 410#endif 411 int ret = 0; 412 413 ret = nf_conntrack_init(); 414 if (ret < 0) 415 return ret; 416 417#ifdef CONFIG_PROC_FS 418 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops); 419 if (!proc) goto cleanup_init; 420 421 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440, 422 &exp_file_ops); 423 if (!proc_exp) goto cleanup_proc; 424 425 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat); 426 if (!proc_stat) 427 goto cleanup_proc_exp; 428 429 proc_stat->proc_fops = &ct_cpu_seq_fops; 430 proc_stat->owner = THIS_MODULE; 431#endif 432#ifdef CONFIG_SYSCTL 433 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table); 434 if (nf_ct_sysctl_header == NULL) { 435 printk("nf_conntrack: can't register to sysctl.\n"); 436 ret = -ENOMEM; 437 goto cleanup_proc_stat; 438 } 439#endif 440 return ret; 441 442#ifdef CONFIG_SYSCTL 443 cleanup_proc_stat: 444#endif 445#ifdef CONFIG_PROC_FS 446 remove_proc_entry("nf_conntrack", proc_net_stat); 447 cleanup_proc_exp: 448 proc_net_remove("nf_conntrack_expect"); 449 cleanup_proc: 450 proc_net_remove("nf_conntrack"); 451 cleanup_init: 452#endif /* CNFIG_PROC_FS */ 453 nf_conntrack_cleanup(); 454 return ret; 455} 456 457static void __exit nf_conntrack_standalone_fini(void) 458{ 459#ifdef CONFIG_SYSCTL 460 unregister_sysctl_table(nf_ct_sysctl_header); 461#endif 462#ifdef CONFIG_PROC_FS 463 remove_proc_entry("nf_conntrack", proc_net_stat); 464 proc_net_remove("nf_conntrack_expect"); 465 proc_net_remove("nf_conntrack"); 466#endif /* CNFIG_PROC_FS */ 467 nf_conntrack_cleanup(); 468} 469 470module_init(nf_conntrack_standalone_init); 471module_exit(nf_conntrack_standalone_fini); 472 473/* Some modules need us, but don't depend directly on any symbol. 474 They should call this. */ 475void need_conntrack(void) 476{ 477} 478EXPORT_SYMBOL_GPL(need_conntrack); 479