xt_CT.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2010 Patrick McHardy <kaber@trash.net>
  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. #include <linux/module.h>
  9. #include <linux/gfp.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netfilter_ipv4/ip_tables.h>
  12. #include <linux/netfilter_ipv6/ip6_tables.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter/xt_CT.h>
  15. #include <net/netfilter/nf_conntrack.h>
  16. #include <net/netfilter/nf_conntrack_helper.h>
  17. #include <net/netfilter/nf_conntrack_ecache.h>
  18. #include <net/netfilter/nf_conntrack_zones.h>
  19. static unsigned int xt_ct_target(struct sk_buff *skb,
  20. const struct xt_action_param *par)
  21. {
  22. const struct xt_ct_target_info *info = par->targinfo;
  23. struct nf_conn *ct = info->ct;
  24. /* Previously seen (loopback)? Ignore. */
  25. if (skb->nfct != NULL)
  26. return XT_CONTINUE;
  27. atomic_inc(&ct->ct_general.use);
  28. skb->nfct = &ct->ct_general;
  29. skb->nfctinfo = IP_CT_NEW;
  30. return XT_CONTINUE;
  31. }
  32. static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
  33. {
  34. if (par->family == NFPROTO_IPV4) {
  35. const struct ipt_entry *e = par->entryinfo;
  36. if (e->ip.invflags & IPT_INV_PROTO)
  37. return 0;
  38. return e->ip.proto;
  39. } else if (par->family == NFPROTO_IPV6) {
  40. const struct ip6t_entry *e = par->entryinfo;
  41. if (e->ipv6.invflags & IP6T_INV_PROTO)
  42. return 0;
  43. return e->ipv6.proto;
  44. } else
  45. return 0;
  46. }
  47. static int xt_ct_tg_check(const struct xt_tgchk_param *par)
  48. {
  49. struct xt_ct_target_info *info = par->targinfo;
  50. struct nf_conntrack_tuple t;
  51. struct nf_conn_help *help;
  52. struct nf_conn *ct;
  53. int ret = 0;
  54. u8 proto;
  55. if (info->flags & ~XT_CT_NOTRACK)
  56. return -EINVAL;
  57. if (info->flags & XT_CT_NOTRACK) {
  58. ct = nf_ct_untracked_get();
  59. atomic_inc(&ct->ct_general.use);
  60. goto out;
  61. }
  62. #ifndef CONFIG_NF_CONNTRACK_ZONES
  63. if (info->zone)
  64. goto err1;
  65. #endif
  66. ret = nf_ct_l3proto_try_module_get(par->family);
  67. if (ret < 0)
  68. goto err1;
  69. memset(&t, 0, sizeof(t));
  70. ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
  71. ret = PTR_ERR(ct);
  72. if (IS_ERR(ct))
  73. goto err2;
  74. ret = 0;
  75. if ((info->ct_events || info->exp_events) &&
  76. !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
  77. GFP_KERNEL))
  78. goto err3;
  79. if (info->helper[0]) {
  80. ret = -ENOENT;
  81. proto = xt_ct_find_proto(par);
  82. if (!proto)
  83. goto err3;
  84. ret = -ENOMEM;
  85. help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
  86. if (help == NULL)
  87. goto err3;
  88. ret = -ENOENT;
  89. help->helper = nf_conntrack_helper_try_module_get(info->helper,
  90. par->family,
  91. proto);
  92. if (help->helper == NULL)
  93. goto err3;
  94. }
  95. __set_bit(IPS_TEMPLATE_BIT, &ct->status);
  96. __set_bit(IPS_CONFIRMED_BIT, &ct->status);
  97. out:
  98. info->ct = ct;
  99. return 0;
  100. err3:
  101. nf_conntrack_free(ct);
  102. err2:
  103. nf_ct_l3proto_module_put(par->family);
  104. err1:
  105. return ret;
  106. }
  107. static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par)
  108. {
  109. struct xt_ct_target_info *info = par->targinfo;
  110. struct nf_conn *ct = info->ct;
  111. struct nf_conn_help *help;
  112. if (!nf_ct_is_untracked(ct)) {
  113. help = nfct_help(ct);
  114. if (help)
  115. module_put(help->helper->me);
  116. nf_ct_l3proto_module_put(par->family);
  117. }
  118. nf_ct_put(info->ct);
  119. }
  120. static struct xt_target xt_ct_tg __read_mostly = {
  121. .name = "CT",
  122. .family = NFPROTO_UNSPEC,
  123. .targetsize = sizeof(struct xt_ct_target_info),
  124. .checkentry = xt_ct_tg_check,
  125. .destroy = xt_ct_tg_destroy,
  126. .target = xt_ct_target,
  127. .table = "raw",
  128. .me = THIS_MODULE,
  129. };
  130. static int __init xt_ct_tg_init(void)
  131. {
  132. return xt_register_target(&xt_ct_tg);
  133. }
  134. static void __exit xt_ct_tg_exit(void)
  135. {
  136. xt_unregister_target(&xt_ct_tg);
  137. }
  138. module_init(xt_ct_tg_init);
  139. module_exit(xt_ct_tg_exit);
  140. MODULE_LICENSE("GPL");
  141. MODULE_DESCRIPTION("Xtables: connection tracking target");
  142. MODULE_ALIAS("ipt_CT");
  143. MODULE_ALIAS("ip6t_CT");