act_ipt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * net/sched/act_ipt.c iptables target interface
  3. *
  4. *TODO: Add other tables. For now we only support the ipv4 table targets
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Copyright: Jamal Hadi Salim (2002-13)
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <net/netlink.h>
  23. #include <net/pkt_sched.h>
  24. #include <linux/tc_act/tc_ipt.h>
  25. #include <net/tc_act/tc_ipt.h>
  26. #include <linux/netfilter_ipv4/ip_tables.h>
  27. #define IPT_TAB_MASK 15
  28. static int ipt_net_id;
  29. static struct tc_action_ops act_ipt_ops;
  30. static int xt_net_id;
  31. static struct tc_action_ops act_xt_ops;
  32. static int ipt_init_target(struct xt_entry_target *t, char *table,
  33. unsigned int hook)
  34. {
  35. struct xt_tgchk_param par;
  36. struct xt_target *target;
  37. struct ipt_entry e = {};
  38. int ret = 0;
  39. target = xt_request_find_target(AF_INET, t->u.user.name,
  40. t->u.user.revision);
  41. if (IS_ERR(target))
  42. return PTR_ERR(target);
  43. t->u.kernel.target = target;
  44. memset(&par, 0, sizeof(par));
  45. par.table = table;
  46. par.entryinfo = &e;
  47. par.target = target;
  48. par.targinfo = t->data;
  49. par.hook_mask = hook;
  50. par.family = NFPROTO_IPV4;
  51. ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
  52. if (ret < 0) {
  53. module_put(t->u.kernel.target->me);
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static void ipt_destroy_target(struct xt_entry_target *t)
  59. {
  60. struct xt_tgdtor_param par = {
  61. .target = t->u.kernel.target,
  62. .targinfo = t->data,
  63. .family = NFPROTO_IPV4,
  64. };
  65. if (par.target->destroy != NULL)
  66. par.target->destroy(&par);
  67. module_put(par.target->me);
  68. }
  69. static void tcf_ipt_release(struct tc_action *a, int bind)
  70. {
  71. struct tcf_ipt *ipt = to_ipt(a);
  72. ipt_destroy_target(ipt->tcfi_t);
  73. kfree(ipt->tcfi_tname);
  74. kfree(ipt->tcfi_t);
  75. }
  76. static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
  77. [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
  78. [TCA_IPT_HOOK] = { .type = NLA_U32 },
  79. [TCA_IPT_INDEX] = { .type = NLA_U32 },
  80. [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
  81. };
  82. static int __tcf_ipt_init(struct tc_action_net *tn, struct nlattr *nla,
  83. struct nlattr *est, struct tc_action **a,
  84. const struct tc_action_ops *ops, int ovr, int bind)
  85. {
  86. struct nlattr *tb[TCA_IPT_MAX + 1];
  87. struct tcf_ipt *ipt;
  88. struct xt_entry_target *td, *t;
  89. char *tname;
  90. bool exists = false;
  91. int ret = 0, err;
  92. u32 hook = 0;
  93. u32 index = 0;
  94. if (nla == NULL)
  95. return -EINVAL;
  96. err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy);
  97. if (err < 0)
  98. return err;
  99. if (tb[TCA_IPT_INDEX] != NULL)
  100. index = nla_get_u32(tb[TCA_IPT_INDEX]);
  101. exists = tcf_hash_check(tn, index, a, bind);
  102. if (exists && bind)
  103. return 0;
  104. if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) {
  105. if (exists)
  106. tcf_hash_release(*a, bind);
  107. return -EINVAL;
  108. }
  109. td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
  110. if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size) {
  111. if (exists)
  112. tcf_hash_release(*a, bind);
  113. return -EINVAL;
  114. }
  115. if (!exists) {
  116. ret = tcf_hash_create(tn, index, est, a, ops, bind,
  117. false);
  118. if (ret)
  119. return ret;
  120. ret = ACT_P_CREATED;
  121. } else {
  122. if (bind)/* dont override defaults */
  123. return 0;
  124. tcf_hash_release(*a, bind);
  125. if (!ovr)
  126. return -EEXIST;
  127. }
  128. hook = nla_get_u32(tb[TCA_IPT_HOOK]);
  129. err = -ENOMEM;
  130. tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
  131. if (unlikely(!tname))
  132. goto err1;
  133. if (tb[TCA_IPT_TABLE] == NULL ||
  134. nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
  135. strcpy(tname, "mangle");
  136. t = kmemdup(td, td->u.target_size, GFP_KERNEL);
  137. if (unlikely(!t))
  138. goto err2;
  139. err = ipt_init_target(t, tname, hook);
  140. if (err < 0)
  141. goto err3;
  142. ipt = to_ipt(*a);
  143. spin_lock_bh(&ipt->tcf_lock);
  144. if (ret != ACT_P_CREATED) {
  145. ipt_destroy_target(ipt->tcfi_t);
  146. kfree(ipt->tcfi_tname);
  147. kfree(ipt->tcfi_t);
  148. }
  149. ipt->tcfi_tname = tname;
  150. ipt->tcfi_t = t;
  151. ipt->tcfi_hook = hook;
  152. spin_unlock_bh(&ipt->tcf_lock);
  153. if (ret == ACT_P_CREATED)
  154. tcf_hash_insert(tn, *a);
  155. return ret;
  156. err3:
  157. kfree(t);
  158. err2:
  159. kfree(tname);
  160. err1:
  161. if (ret == ACT_P_CREATED)
  162. tcf_hash_cleanup(*a, est);
  163. return err;
  164. }
  165. static int tcf_ipt_init(struct net *net, struct nlattr *nla,
  166. struct nlattr *est, struct tc_action **a, int ovr,
  167. int bind)
  168. {
  169. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  170. return __tcf_ipt_init(tn, nla, est, a, &act_ipt_ops, ovr, bind);
  171. }
  172. static int tcf_xt_init(struct net *net, struct nlattr *nla,
  173. struct nlattr *est, struct tc_action **a, int ovr,
  174. int bind)
  175. {
  176. struct tc_action_net *tn = net_generic(net, xt_net_id);
  177. return __tcf_ipt_init(tn, nla, est, a, &act_xt_ops, ovr, bind);
  178. }
  179. static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a,
  180. struct tcf_result *res)
  181. {
  182. int ret = 0, result = 0;
  183. struct tcf_ipt *ipt = to_ipt(a);
  184. struct xt_action_param par;
  185. if (skb_unclone(skb, GFP_ATOMIC))
  186. return TC_ACT_UNSPEC;
  187. spin_lock(&ipt->tcf_lock);
  188. tcf_lastuse_update(&ipt->tcf_tm);
  189. bstats_update(&ipt->tcf_bstats, skb);
  190. /* yes, we have to worry about both in and out dev
  191. * worry later - danger - this API seems to have changed
  192. * from earlier kernels
  193. */
  194. par.net = dev_net(skb->dev);
  195. par.in = skb->dev;
  196. par.out = NULL;
  197. par.hooknum = ipt->tcfi_hook;
  198. par.target = ipt->tcfi_t->u.kernel.target;
  199. par.targinfo = ipt->tcfi_t->data;
  200. par.family = NFPROTO_IPV4;
  201. ret = par.target->target(skb, &par);
  202. switch (ret) {
  203. case NF_ACCEPT:
  204. result = TC_ACT_OK;
  205. break;
  206. case NF_DROP:
  207. result = TC_ACT_SHOT;
  208. ipt->tcf_qstats.drops++;
  209. break;
  210. case XT_CONTINUE:
  211. result = TC_ACT_PIPE;
  212. break;
  213. default:
  214. net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
  215. ret);
  216. result = TC_ACT_OK;
  217. break;
  218. }
  219. spin_unlock(&ipt->tcf_lock);
  220. return result;
  221. }
  222. static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  223. int ref)
  224. {
  225. unsigned char *b = skb_tail_pointer(skb);
  226. struct tcf_ipt *ipt = to_ipt(a);
  227. struct xt_entry_target *t;
  228. struct tcf_t tm;
  229. struct tc_cnt c;
  230. /* for simple targets kernel size == user size
  231. * user name = target name
  232. * for foolproof you need to not assume this
  233. */
  234. t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
  235. if (unlikely(!t))
  236. goto nla_put_failure;
  237. c.bindcnt = ipt->tcf_bindcnt - bind;
  238. c.refcnt = ipt->tcf_refcnt - ref;
  239. strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
  240. if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
  241. nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
  242. nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
  243. nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
  244. nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
  245. goto nla_put_failure;
  246. tcf_tm_dump(&tm, &ipt->tcf_tm);
  247. if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD))
  248. goto nla_put_failure;
  249. kfree(t);
  250. return skb->len;
  251. nla_put_failure:
  252. nlmsg_trim(skb, b);
  253. kfree(t);
  254. return -1;
  255. }
  256. static int tcf_ipt_walker(struct net *net, struct sk_buff *skb,
  257. struct netlink_callback *cb, int type,
  258. const struct tc_action_ops *ops)
  259. {
  260. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  261. return tcf_generic_walker(tn, skb, cb, type, ops);
  262. }
  263. static int tcf_ipt_search(struct net *net, struct tc_action **a, u32 index)
  264. {
  265. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  266. return tcf_hash_search(tn, a, index);
  267. }
  268. static struct tc_action_ops act_ipt_ops = {
  269. .kind = "ipt",
  270. .type = TCA_ACT_IPT,
  271. .owner = THIS_MODULE,
  272. .act = tcf_ipt,
  273. .dump = tcf_ipt_dump,
  274. .cleanup = tcf_ipt_release,
  275. .init = tcf_ipt_init,
  276. .walk = tcf_ipt_walker,
  277. .lookup = tcf_ipt_search,
  278. .size = sizeof(struct tcf_ipt),
  279. };
  280. static __net_init int ipt_init_net(struct net *net)
  281. {
  282. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  283. return tc_action_net_init(tn, &act_ipt_ops, IPT_TAB_MASK);
  284. }
  285. static void __net_exit ipt_exit_net(struct net *net)
  286. {
  287. struct tc_action_net *tn = net_generic(net, ipt_net_id);
  288. tc_action_net_exit(tn);
  289. }
  290. static struct pernet_operations ipt_net_ops = {
  291. .init = ipt_init_net,
  292. .exit = ipt_exit_net,
  293. .id = &ipt_net_id,
  294. .size = sizeof(struct tc_action_net),
  295. };
  296. static int tcf_xt_walker(struct net *net, struct sk_buff *skb,
  297. struct netlink_callback *cb, int type,
  298. const struct tc_action_ops *ops)
  299. {
  300. struct tc_action_net *tn = net_generic(net, xt_net_id);
  301. return tcf_generic_walker(tn, skb, cb, type, ops);
  302. }
  303. static int tcf_xt_search(struct net *net, struct tc_action **a, u32 index)
  304. {
  305. struct tc_action_net *tn = net_generic(net, xt_net_id);
  306. return tcf_hash_search(tn, a, index);
  307. }
  308. static struct tc_action_ops act_xt_ops = {
  309. .kind = "xt",
  310. .type = TCA_ACT_XT,
  311. .owner = THIS_MODULE,
  312. .act = tcf_ipt,
  313. .dump = tcf_ipt_dump,
  314. .cleanup = tcf_ipt_release,
  315. .init = tcf_xt_init,
  316. .walk = tcf_xt_walker,
  317. .lookup = tcf_xt_search,
  318. .size = sizeof(struct tcf_ipt),
  319. };
  320. static __net_init int xt_init_net(struct net *net)
  321. {
  322. struct tc_action_net *tn = net_generic(net, xt_net_id);
  323. return tc_action_net_init(tn, &act_xt_ops, IPT_TAB_MASK);
  324. }
  325. static void __net_exit xt_exit_net(struct net *net)
  326. {
  327. struct tc_action_net *tn = net_generic(net, xt_net_id);
  328. tc_action_net_exit(tn);
  329. }
  330. static struct pernet_operations xt_net_ops = {
  331. .init = xt_init_net,
  332. .exit = xt_exit_net,
  333. .id = &xt_net_id,
  334. .size = sizeof(struct tc_action_net),
  335. };
  336. MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
  337. MODULE_DESCRIPTION("Iptables target actions");
  338. MODULE_LICENSE("GPL");
  339. MODULE_ALIAS("act_xt");
  340. static int __init ipt_init_module(void)
  341. {
  342. int ret1, ret2;
  343. ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops);
  344. if (ret1 < 0)
  345. pr_err("Failed to load xt action\n");
  346. ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops);
  347. if (ret2 < 0)
  348. pr_err("Failed to load ipt action\n");
  349. if (ret1 < 0 && ret2 < 0) {
  350. return ret1;
  351. } else
  352. return 0;
  353. }
  354. static void __exit ipt_cleanup_module(void)
  355. {
  356. tcf_unregister_action(&act_ipt_ops, &ipt_net_ops);
  357. tcf_unregister_action(&act_xt_ops, &xt_net_ops);
  358. }
  359. module_init(ipt_init_module);
  360. module_exit(ipt_cleanup_module);