xt_CT.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/gfp.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/netfilter_ipv4/ip_tables.h>
  13. #include <linux/netfilter_ipv6/ip6_tables.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter/xt_CT.h>
  16. #include <net/netfilter/nf_conntrack.h>
  17. #include <net/netfilter/nf_conntrack_l4proto.h>
  18. #include <net/netfilter/nf_conntrack_helper.h>
  19. #include <net/netfilter/nf_conntrack_ecache.h>
  20. #include <net/netfilter/nf_conntrack_timeout.h>
  21. #include <net/netfilter/nf_conntrack_zones.h>
  22. static inline int xt_ct_target(struct sk_buff *skb, struct nf_conn *ct)
  23. {
  24. /* Previously seen (loopback)? Ignore. */
  25. if (skb->nfct != NULL)
  26. return XT_CONTINUE;
  27. /* special case the untracked ct : we want the percpu object */
  28. if (!ct)
  29. ct = nf_ct_untracked_get();
  30. atomic_inc(&ct->ct_general.use);
  31. skb->nfct = &ct->ct_general;
  32. skb->nfctinfo = IP_CT_NEW;
  33. return XT_CONTINUE;
  34. }
  35. static unsigned int xt_ct_target_v0(struct sk_buff *skb,
  36. const struct xt_action_param *par)
  37. {
  38. const struct xt_ct_target_info *info = par->targinfo;
  39. struct nf_conn *ct = info->ct;
  40. return xt_ct_target(skb, ct);
  41. }
  42. static unsigned int xt_ct_target_v1(struct sk_buff *skb,
  43. const struct xt_action_param *par)
  44. {
  45. const struct xt_ct_target_info_v1 *info = par->targinfo;
  46. struct nf_conn *ct = info->ct;
  47. return xt_ct_target(skb, ct);
  48. }
  49. static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
  50. {
  51. if (par->family == NFPROTO_IPV4) {
  52. const struct ipt_entry *e = par->entryinfo;
  53. if (e->ip.invflags & IPT_INV_PROTO)
  54. return 0;
  55. return e->ip.proto;
  56. } else if (par->family == NFPROTO_IPV6) {
  57. const struct ip6t_entry *e = par->entryinfo;
  58. if (e->ipv6.invflags & IP6T_INV_PROTO)
  59. return 0;
  60. return e->ipv6.proto;
  61. } else
  62. return 0;
  63. }
  64. static int
  65. xt_ct_set_helper(struct nf_conn *ct, const char *helper_name,
  66. const struct xt_tgchk_param *par)
  67. {
  68. struct nf_conntrack_helper *helper;
  69. struct nf_conn_help *help;
  70. u8 proto;
  71. proto = xt_ct_find_proto(par);
  72. if (!proto) {
  73. pr_info("You must specify a L4 protocol, and not use "
  74. "inversions on it.\n");
  75. return -ENOENT;
  76. }
  77. helper = nf_conntrack_helper_try_module_get(helper_name, par->family,
  78. proto);
  79. if (helper == NULL) {
  80. pr_info("No such helper \"%s\"\n", helper_name);
  81. return -ENOENT;
  82. }
  83. help = nf_ct_helper_ext_add(ct, helper, GFP_KERNEL);
  84. if (help == NULL) {
  85. module_put(helper->me);
  86. return -ENOMEM;
  87. }
  88. help->helper = helper;
  89. return 0;
  90. }
  91. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  92. static void __xt_ct_tg_timeout_put(struct ctnl_timeout *timeout)
  93. {
  94. typeof(nf_ct_timeout_put_hook) timeout_put;
  95. timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
  96. if (timeout_put)
  97. timeout_put(timeout);
  98. }
  99. #endif
  100. static int
  101. xt_ct_set_timeout(struct nf_conn *ct, const struct xt_tgchk_param *par,
  102. const char *timeout_name)
  103. {
  104. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  105. typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
  106. struct ctnl_timeout *timeout;
  107. struct nf_conn_timeout *timeout_ext;
  108. struct nf_conntrack_l4proto *l4proto;
  109. int ret = 0;
  110. u8 proto;
  111. rcu_read_lock();
  112. timeout_find_get = rcu_dereference(nf_ct_timeout_find_get_hook);
  113. if (timeout_find_get == NULL) {
  114. ret = -ENOENT;
  115. pr_info("Timeout policy base is empty\n");
  116. goto out;
  117. }
  118. proto = xt_ct_find_proto(par);
  119. if (!proto) {
  120. ret = -EINVAL;
  121. pr_info("You must specify a L4 protocol, and not use "
  122. "inversions on it.\n");
  123. goto out;
  124. }
  125. timeout = timeout_find_get(timeout_name);
  126. if (timeout == NULL) {
  127. ret = -ENOENT;
  128. pr_info("No such timeout policy \"%s\"\n", timeout_name);
  129. goto out;
  130. }
  131. if (timeout->l3num != par->family) {
  132. ret = -EINVAL;
  133. pr_info("Timeout policy `%s' can only be used by L3 protocol "
  134. "number %d\n", timeout_name, timeout->l3num);
  135. goto err_put_timeout;
  136. }
  137. /* Make sure the timeout policy matches any existing protocol tracker,
  138. * otherwise default to generic.
  139. */
  140. l4proto = __nf_ct_l4proto_find(par->family, proto);
  141. if (timeout->l4proto->l4proto != l4proto->l4proto) {
  142. ret = -EINVAL;
  143. pr_info("Timeout policy `%s' can only be used by L4 protocol "
  144. "number %d\n",
  145. timeout_name, timeout->l4proto->l4proto);
  146. goto err_put_timeout;
  147. }
  148. timeout_ext = nf_ct_timeout_ext_add(ct, timeout, GFP_ATOMIC);
  149. if (timeout_ext == NULL)
  150. ret = -ENOMEM;
  151. err_put_timeout:
  152. __xt_ct_tg_timeout_put(timeout);
  153. out:
  154. rcu_read_unlock();
  155. return ret;
  156. #else
  157. return -EOPNOTSUPP;
  158. #endif
  159. }
  160. static int xt_ct_tg_check(const struct xt_tgchk_param *par,
  161. struct xt_ct_target_info_v1 *info)
  162. {
  163. struct nf_conntrack_tuple t;
  164. struct nf_conn *ct;
  165. int ret = -EOPNOTSUPP;
  166. if (info->flags & XT_CT_NOTRACK) {
  167. ct = NULL;
  168. goto out;
  169. }
  170. #ifndef CONFIG_NF_CONNTRACK_ZONES
  171. if (info->zone)
  172. goto err1;
  173. #endif
  174. ret = nf_ct_l3proto_try_module_get(par->family);
  175. if (ret < 0)
  176. goto err1;
  177. memset(&t, 0, sizeof(t));
  178. ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
  179. ret = PTR_ERR(ct);
  180. if (IS_ERR(ct))
  181. goto err2;
  182. ret = 0;
  183. if ((info->ct_events || info->exp_events) &&
  184. !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
  185. GFP_KERNEL)) {
  186. ret = -EINVAL;
  187. goto err3;
  188. }
  189. if (info->helper[0]) {
  190. ret = xt_ct_set_helper(ct, info->helper, par);
  191. if (ret < 0)
  192. goto err3;
  193. }
  194. if (info->timeout[0]) {
  195. ret = xt_ct_set_timeout(ct, par, info->timeout);
  196. if (ret < 0)
  197. goto err3;
  198. }
  199. nf_conntrack_tmpl_insert(par->net, ct);
  200. out:
  201. info->ct = ct;
  202. return 0;
  203. err3:
  204. nf_conntrack_free(ct);
  205. err2:
  206. nf_ct_l3proto_module_put(par->family);
  207. err1:
  208. return ret;
  209. }
  210. static int xt_ct_tg_check_v0(const struct xt_tgchk_param *par)
  211. {
  212. struct xt_ct_target_info *info = par->targinfo;
  213. struct xt_ct_target_info_v1 info_v1 = {
  214. .flags = info->flags,
  215. .zone = info->zone,
  216. .ct_events = info->ct_events,
  217. .exp_events = info->exp_events,
  218. };
  219. int ret;
  220. if (info->flags & ~XT_CT_NOTRACK)
  221. return -EINVAL;
  222. memcpy(info_v1.helper, info->helper, sizeof(info->helper));
  223. ret = xt_ct_tg_check(par, &info_v1);
  224. if (ret < 0)
  225. return ret;
  226. info->ct = info_v1.ct;
  227. return ret;
  228. }
  229. static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
  230. {
  231. struct xt_ct_target_info_v1 *info = par->targinfo;
  232. if (info->flags & ~XT_CT_NOTRACK)
  233. return -EINVAL;
  234. return xt_ct_tg_check(par, par->targinfo);
  235. }
  236. static int xt_ct_tg_check_v2(const struct xt_tgchk_param *par)
  237. {
  238. struct xt_ct_target_info_v1 *info = par->targinfo;
  239. if (info->flags & ~XT_CT_MASK)
  240. return -EINVAL;
  241. return xt_ct_tg_check(par, par->targinfo);
  242. }
  243. static void xt_ct_destroy_timeout(struct nf_conn *ct)
  244. {
  245. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  246. struct nf_conn_timeout *timeout_ext;
  247. typeof(nf_ct_timeout_put_hook) timeout_put;
  248. rcu_read_lock();
  249. timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
  250. if (timeout_put) {
  251. timeout_ext = nf_ct_timeout_find(ct);
  252. if (timeout_ext)
  253. timeout_put(timeout_ext->timeout);
  254. }
  255. rcu_read_unlock();
  256. #endif
  257. }
  258. static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,
  259. struct xt_ct_target_info_v1 *info)
  260. {
  261. struct nf_conn *ct = info->ct;
  262. struct nf_conn_help *help;
  263. if (ct && !nf_ct_is_untracked(ct)) {
  264. help = nfct_help(ct);
  265. if (help)
  266. module_put(help->helper->me);
  267. nf_ct_l3proto_module_put(par->family);
  268. xt_ct_destroy_timeout(ct);
  269. nf_ct_put(info->ct);
  270. }
  271. }
  272. static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
  273. {
  274. struct xt_ct_target_info *info = par->targinfo;
  275. struct xt_ct_target_info_v1 info_v1 = {
  276. .flags = info->flags,
  277. .zone = info->zone,
  278. .ct_events = info->ct_events,
  279. .exp_events = info->exp_events,
  280. .ct = info->ct,
  281. };
  282. memcpy(info_v1.helper, info->helper, sizeof(info->helper));
  283. xt_ct_tg_destroy(par, &info_v1);
  284. }
  285. static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
  286. {
  287. xt_ct_tg_destroy(par, par->targinfo);
  288. }
  289. static struct xt_target xt_ct_tg_reg[] __read_mostly = {
  290. {
  291. .name = "CT",
  292. .family = NFPROTO_UNSPEC,
  293. .targetsize = sizeof(struct xt_ct_target_info),
  294. .checkentry = xt_ct_tg_check_v0,
  295. .destroy = xt_ct_tg_destroy_v0,
  296. .target = xt_ct_target_v0,
  297. .table = "raw",
  298. .me = THIS_MODULE,
  299. },
  300. {
  301. .name = "CT",
  302. .family = NFPROTO_UNSPEC,
  303. .revision = 1,
  304. .targetsize = sizeof(struct xt_ct_target_info_v1),
  305. .checkentry = xt_ct_tg_check_v1,
  306. .destroy = xt_ct_tg_destroy_v1,
  307. .target = xt_ct_target_v1,
  308. .table = "raw",
  309. .me = THIS_MODULE,
  310. },
  311. {
  312. .name = "CT",
  313. .family = NFPROTO_UNSPEC,
  314. .revision = 2,
  315. .targetsize = sizeof(struct xt_ct_target_info_v1),
  316. .checkentry = xt_ct_tg_check_v2,
  317. .destroy = xt_ct_tg_destroy_v1,
  318. .target = xt_ct_target_v1,
  319. .table = "raw",
  320. .me = THIS_MODULE,
  321. },
  322. };
  323. static unsigned int
  324. notrack_tg(struct sk_buff *skb, const struct xt_action_param *par)
  325. {
  326. /* Previously seen (loopback)? Ignore. */
  327. if (skb->nfct != NULL)
  328. return XT_CONTINUE;
  329. skb->nfct = &nf_ct_untracked_get()->ct_general;
  330. skb->nfctinfo = IP_CT_NEW;
  331. nf_conntrack_get(skb->nfct);
  332. return XT_CONTINUE;
  333. }
  334. static int notrack_chk(const struct xt_tgchk_param *par)
  335. {
  336. if (!par->net->xt.notrack_deprecated_warning) {
  337. pr_info("netfilter: NOTRACK target is deprecated, "
  338. "use CT instead or upgrade iptables\n");
  339. par->net->xt.notrack_deprecated_warning = true;
  340. }
  341. return 0;
  342. }
  343. static struct xt_target notrack_tg_reg __read_mostly = {
  344. .name = "NOTRACK",
  345. .revision = 0,
  346. .family = NFPROTO_UNSPEC,
  347. .checkentry = notrack_chk,
  348. .target = notrack_tg,
  349. .table = "raw",
  350. .me = THIS_MODULE,
  351. };
  352. static int __init xt_ct_tg_init(void)
  353. {
  354. int ret;
  355. ret = xt_register_target(&notrack_tg_reg);
  356. if (ret < 0)
  357. return ret;
  358. ret = xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
  359. if (ret < 0) {
  360. xt_unregister_target(&notrack_tg_reg);
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. static void __exit xt_ct_tg_exit(void)
  366. {
  367. xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
  368. xt_unregister_target(&notrack_tg_reg);
  369. }
  370. module_init(xt_ct_tg_init);
  371. module_exit(xt_ct_tg_exit);
  372. MODULE_LICENSE("GPL");
  373. MODULE_DESCRIPTION("Xtables: connection tracking target");
  374. MODULE_ALIAS("ipt_CT");
  375. MODULE_ALIAS("ip6t_CT");
  376. MODULE_ALIAS("ipt_NOTRACK");
  377. MODULE_ALIAS("ip6t_NOTRACK");