nf_conntrack_l3proto_ipv4_compat.c 9.2 KB

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