1/* ip_conntrack proc compat - based on ip_conntrack_standalone.c 2 * 3 * (C) 1999-2001 Paul `Rusty' Russell 4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10#include <linux/types.h> 11#include <linux/proc_fs.h> 12#include <linux/seq_file.h> 13#include <linux/percpu.h> 14 15#include <linux/netfilter.h> 16#include <net/netfilter/nf_conntrack_core.h> 17#include <net/netfilter/nf_conntrack_l3proto.h> 18#include <net/netfilter/nf_conntrack_l4proto.h> 19#include <net/netfilter/nf_conntrack_expect.h> 20 21#define DEBUGP(format, args...) 22 23#ifdef CONFIG_NF_CT_ACCT 24static unsigned int 25seq_print_counters(struct seq_file *s, 26 const struct ip_conntrack_counter *counter) 27{ 28 return seq_printf(s, "packets=%llu bytes=%llu ", 29 (unsigned long long)counter->packets, 30 (unsigned long long)counter->bytes); 31} 32#else 33#define seq_print_counters(x, y) 0 34#endif 35 36struct ct_iter_state { 37 unsigned int bucket; 38}; 39 40static struct list_head *ct_get_first(struct seq_file *seq) 41{ 42 struct ct_iter_state *st = seq->private; 43 44 for (st->bucket = 0; 45 st->bucket < nf_conntrack_htable_size; 46 st->bucket++) { 47 if (!list_empty(&nf_conntrack_hash[st->bucket])) 48 return nf_conntrack_hash[st->bucket].next; 49 } 50 return NULL; 51} 52 53static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head) 54{ 55 struct ct_iter_state *st = seq->private; 56 57 head = head->next; 58 while (head == &nf_conntrack_hash[st->bucket]) { 59 if (++st->bucket >= nf_conntrack_htable_size) 60 return NULL; 61 head = nf_conntrack_hash[st->bucket].next; 62 } 63 return head; 64} 65 66static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos) 67{ 68 struct list_head *head = ct_get_first(seq); 69 70 if (head) 71 while (pos && (head = ct_get_next(seq, head))) 72 pos--; 73 return pos ? NULL : head; 74} 75 76static void *ct_seq_start(struct seq_file *seq, loff_t *pos) 77{ 78 read_lock_bh(&nf_conntrack_lock); 79 return ct_get_idx(seq, *pos); 80} 81 82static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) 83{ 84 (*pos)++; 85 return ct_get_next(s, v); 86} 87 88static void ct_seq_stop(struct seq_file *s, void *v) 89{ 90 read_unlock_bh(&nf_conntrack_lock); 91} 92 93static int ct_seq_show(struct seq_file *s, void *v) 94{ 95 const struct nf_conntrack_tuple_hash *hash = v; 96 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash); 97 struct nf_conntrack_l3proto *l3proto; 98 struct nf_conntrack_l4proto *l4proto; 99 100 NF_CT_ASSERT(ct); 101 102 /* we only want to print DIR_ORIGINAL */ 103 if (NF_CT_DIRECTION(hash)) 104 return 0; 105 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET) 106 return 0; 107 108 l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL] 109 .tuple.src.l3num); 110 NF_CT_ASSERT(l3proto); 111 l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL] 112 .tuple.src.l3num, 113 ct->tuplehash[IP_CT_DIR_ORIGINAL] 114 .tuple.dst.protonum); 115 NF_CT_ASSERT(l4proto); 116 117 if (seq_printf(s, "%-8s %u %ld ", 118 l4proto->name, 119 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum, 120 timer_pending(&ct->timeout) 121 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0) 122 return -ENOSPC; 123 124 if (l3proto->print_conntrack(s, ct)) 125 return -ENOSPC; 126 127 if (l4proto->print_conntrack(s, ct)) 128 return -ENOSPC; 129 130 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, 131 l3proto, l4proto)) 132 return -ENOSPC; 133 134 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL])) 135 return -ENOSPC; 136 137 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) 138 if (seq_printf(s, "[UNREPLIED] ")) 139 return -ENOSPC; 140 141 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, 142 l3proto, l4proto)) 143 return -ENOSPC; 144 145 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY])) 146 return -ENOSPC; 147 148 if (test_bit(IPS_ASSURED_BIT, &ct->status)) 149 if (seq_printf(s, "[ASSURED] ")) 150 return -ENOSPC; 151 152#ifdef CONFIG_NF_CONNTRACK_MARK 153 if (seq_printf(s, "mark=%u ", ct->mark)) 154 return -ENOSPC; 155#endif 156 157#ifdef CONFIG_NF_CONNTRACK_SECMARK 158 if (seq_printf(s, "secmark=%u ", ct->secmark)) 159 return -ENOSPC; 160#endif 161 162 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use))) 163 return -ENOSPC; 164 165 return 0; 166} 167 168static struct seq_operations ct_seq_ops = { 169 .start = ct_seq_start, 170 .next = ct_seq_next, 171 .stop = ct_seq_stop, 172 .show = ct_seq_show 173}; 174 175static int ct_open(struct inode *inode, struct file *file) 176{ 177 struct seq_file *seq; 178 struct ct_iter_state *st; 179 int ret; 180 181 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL); 182 if (st == NULL) 183 return -ENOMEM; 184 ret = seq_open(file, &ct_seq_ops); 185 if (ret) 186 goto out_free; 187 seq = file->private_data; 188 seq->private = st; 189 memset(st, 0, sizeof(struct ct_iter_state)); 190 return ret; 191out_free: 192 kfree(st); 193 return ret; 194} 195 196static const struct file_operations ct_file_ops = { 197 .owner = THIS_MODULE, 198 .open = ct_open, 199 .read = seq_read, 200 .llseek = seq_lseek, 201 .release = seq_release_private, 202}; 203 204/* expects */ 205static void *exp_seq_start(struct seq_file *s, loff_t *pos) 206{ 207 struct list_head *e = &nf_conntrack_expect_list; 208 loff_t i; 209 210 /* strange seq_file api calls stop even if we fail, 211 * thus we need to grab lock since stop unlocks */ 212 read_lock_bh(&nf_conntrack_lock); 213 214 if (list_empty(e)) 215 return NULL; 216 217 for (i = 0; i <= *pos; i++) { 218 e = e->next; 219 if (e == &nf_conntrack_expect_list) 220 return NULL; 221 } 222 return e; 223} 224 225static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos) 226{ 227 struct list_head *e = v; 228 229 ++*pos; 230 e = e->next; 231 232 if (e == &nf_conntrack_expect_list) 233 return NULL; 234 235 return e; 236} 237 238static void exp_seq_stop(struct seq_file *s, void *v) 239{ 240 read_unlock_bh(&nf_conntrack_lock); 241} 242 243static int exp_seq_show(struct seq_file *s, void *v) 244{ 245 struct nf_conntrack_expect *exp = v; 246 247 if (exp->tuple.src.l3num != AF_INET) 248 return 0; 249 250 if (exp->timeout.function) 251 seq_printf(s, "%ld ", timer_pending(&exp->timeout) 252 ? (long)(exp->timeout.expires - jiffies)/HZ : 0); 253 else 254 seq_printf(s, "- "); 255 256 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum); 257 258 print_tuple(s, &exp->tuple, 259 __nf_ct_l3proto_find(exp->tuple.src.l3num), 260 __nf_ct_l4proto_find(exp->tuple.src.l3num, 261 exp->tuple.dst.protonum)); 262 return seq_putc(s, '\n'); 263} 264 265static struct seq_operations exp_seq_ops = { 266 .start = exp_seq_start, 267 .next = exp_seq_next, 268 .stop = exp_seq_stop, 269 .show = exp_seq_show 270}; 271 272static int exp_open(struct inode *inode, struct file *file) 273{ 274 return seq_open(file, &exp_seq_ops); 275} 276 277static const struct file_operations ip_exp_file_ops = { 278 .owner = THIS_MODULE, 279 .open = exp_open, 280 .read = seq_read, 281 .llseek = seq_lseek, 282 .release = seq_release 283}; 284 285static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) 286{ 287 int cpu; 288 289 if (*pos == 0) 290 return SEQ_START_TOKEN; 291 292 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) { 293 if (!cpu_possible(cpu)) 294 continue; 295 *pos = cpu+1; 296 return &per_cpu(nf_conntrack_stat, cpu); 297 } 298 299 return NULL; 300} 301 302static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 303{ 304 int cpu; 305 306 for (cpu = *pos; cpu < NR_CPUS; ++cpu) { 307 if (!cpu_possible(cpu)) 308 continue; 309 *pos = cpu+1; 310 return &per_cpu(nf_conntrack_stat, cpu); 311 } 312 313 return NULL; 314} 315 316static void ct_cpu_seq_stop(struct seq_file *seq, void *v) 317{ 318} 319 320static int ct_cpu_seq_show(struct seq_file *seq, void *v) 321{ 322 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count); 323 struct ip_conntrack_stat *st = v; 324 325 if (v == SEQ_START_TOKEN) { 326 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"); 327 return 0; 328 } 329 330 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " 331 "%08x %08x %08x %08x %08x %08x %08x %08x \n", 332 nr_conntracks, 333 st->searched, 334 st->found, 335 st->new, 336 st->invalid, 337 st->ignore, 338 st->delete, 339 st->delete_list, 340 st->insert, 341 st->insert_failed, 342 st->drop, 343 st->early_drop, 344 st->error, 345 346 st->expect_new, 347 st->expect_create, 348 st->expect_delete 349 ); 350 return 0; 351} 352 353static struct seq_operations ct_cpu_seq_ops = { 354 .start = ct_cpu_seq_start, 355 .next = ct_cpu_seq_next, 356 .stop = ct_cpu_seq_stop, 357 .show = ct_cpu_seq_show, 358}; 359 360static int ct_cpu_seq_open(struct inode *inode, struct file *file) 361{ 362 return seq_open(file, &ct_cpu_seq_ops); 363} 364 365static const struct file_operations ct_cpu_seq_fops = { 366 .owner = THIS_MODULE, 367 .open = ct_cpu_seq_open, 368 .read = seq_read, 369 .llseek = seq_lseek, 370 .release = seq_release_private, 371}; 372 373int __init nf_conntrack_ipv4_compat_init(void) 374{ 375 struct proc_dir_entry *proc, *proc_exp, *proc_stat; 376 377 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops); 378 if (!proc) 379 goto err1; 380 381 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440, 382 &ip_exp_file_ops); 383 if (!proc_exp) 384 goto err2; 385 386 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat); 387 if (!proc_stat) 388 goto err3; 389 390 proc_stat->proc_fops = &ct_cpu_seq_fops; 391 proc_stat->owner = THIS_MODULE; 392 393 return 0; 394 395err3: 396 proc_net_remove("ip_conntrack_expect"); 397err2: 398 proc_net_remove("ip_conntrack"); 399err1: 400 return -ENOMEM; 401} 402 403void __exit nf_conntrack_ipv4_compat_fini(void) 404{ 405 remove_proc_entry("ip_conntrack", proc_net_stat); 406 proc_net_remove("ip_conntrack_expect"); 407 proc_net_remove("ip_conntrack"); 408} 409