act_ipt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_init_target(struct xt_entry_target *t, char *table, unsigned int hook)
  29. {
  30. struct xt_tgchk_param par;
  31. struct xt_target *target;
  32. struct ipt_entry e = {};
  33. int ret = 0;
  34. target = xt_request_find_target(AF_INET, t->u.user.name,
  35. t->u.user.revision);
  36. if (IS_ERR(target))
  37. return PTR_ERR(target);
  38. t->u.kernel.target = target;
  39. memset(&par, 0, sizeof(par));
  40. par.table = table;
  41. par.entryinfo = &e;
  42. par.target = target;
  43. par.targinfo = t->data;
  44. par.hook_mask = hook;
  45. par.family = NFPROTO_IPV4;
  46. ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
  47. if (ret < 0) {
  48. module_put(t->u.kernel.target->me);
  49. return ret;
  50. }
  51. return 0;
  52. }
  53. static void ipt_destroy_target(struct xt_entry_target *t)
  54. {
  55. struct xt_tgdtor_param par = {
  56. .target = t->u.kernel.target,
  57. .targinfo = t->data,
  58. };
  59. if (par.target->destroy != NULL)
  60. par.target->destroy(&par);
  61. module_put(par.target->me);
  62. }
  63. static void tcf_ipt_release(struct tc_action *a, int bind)
  64. {
  65. struct tcf_ipt *ipt = to_ipt(a);
  66. ipt_destroy_target(ipt->tcfi_t);
  67. kfree(ipt->tcfi_tname);
  68. kfree(ipt->tcfi_t);
  69. }
  70. static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
  71. [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
  72. [TCA_IPT_HOOK] = { .type = NLA_U32 },
  73. [TCA_IPT_INDEX] = { .type = NLA_U32 },
  74. [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
  75. };
  76. static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est,
  77. struct tc_action *a, int ovr, int bind)
  78. {
  79. struct nlattr *tb[TCA_IPT_MAX + 1];
  80. struct tcf_ipt *ipt;
  81. struct xt_entry_target *td, *t;
  82. char *tname;
  83. int ret = 0, err;
  84. u32 hook = 0;
  85. u32 index = 0;
  86. if (nla == NULL)
  87. return -EINVAL;
  88. err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy);
  89. if (err < 0)
  90. return err;
  91. if (tb[TCA_IPT_HOOK] == NULL)
  92. return -EINVAL;
  93. if (tb[TCA_IPT_TARG] == NULL)
  94. return -EINVAL;
  95. td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
  96. if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size)
  97. return -EINVAL;
  98. if (tb[TCA_IPT_INDEX] != NULL)
  99. index = nla_get_u32(tb[TCA_IPT_INDEX]);
  100. if (!tcf_hash_check(index, a, bind) ) {
  101. ret = tcf_hash_create(index, est, a, sizeof(*ipt), bind, false);
  102. if (ret)
  103. return ret;
  104. ret = ACT_P_CREATED;
  105. } else {
  106. if (bind)/* dont override defaults */
  107. return 0;
  108. tcf_hash_release(a, bind);
  109. if (!ovr)
  110. return -EEXIST;
  111. }
  112. ipt = to_ipt(a);
  113. hook = nla_get_u32(tb[TCA_IPT_HOOK]);
  114. err = -ENOMEM;
  115. tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
  116. if (unlikely(!tname))
  117. goto err1;
  118. if (tb[TCA_IPT_TABLE] == NULL ||
  119. nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
  120. strcpy(tname, "mangle");
  121. t = kmemdup(td, td->u.target_size, GFP_KERNEL);
  122. if (unlikely(!t))
  123. goto err2;
  124. err = ipt_init_target(t, tname, hook);
  125. if (err < 0)
  126. goto err3;
  127. spin_lock_bh(&ipt->tcf_lock);
  128. if (ret != ACT_P_CREATED) {
  129. ipt_destroy_target(ipt->tcfi_t);
  130. kfree(ipt->tcfi_tname);
  131. kfree(ipt->tcfi_t);
  132. }
  133. ipt->tcfi_tname = tname;
  134. ipt->tcfi_t = t;
  135. ipt->tcfi_hook = hook;
  136. spin_unlock_bh(&ipt->tcf_lock);
  137. if (ret == ACT_P_CREATED)
  138. tcf_hash_insert(a);
  139. return ret;
  140. err3:
  141. kfree(t);
  142. err2:
  143. kfree(tname);
  144. err1:
  145. if (ret == ACT_P_CREATED)
  146. tcf_hash_cleanup(a, est);
  147. return err;
  148. }
  149. static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a,
  150. struct tcf_result *res)
  151. {
  152. int ret = 0, result = 0;
  153. struct tcf_ipt *ipt = a->priv;
  154. struct xt_action_param par;
  155. if (skb_unclone(skb, GFP_ATOMIC))
  156. return TC_ACT_UNSPEC;
  157. spin_lock(&ipt->tcf_lock);
  158. ipt->tcf_tm.lastuse = jiffies;
  159. bstats_update(&ipt->tcf_bstats, skb);
  160. /* yes, we have to worry about both in and out dev
  161. * worry later - danger - this API seems to have changed
  162. * from earlier kernels
  163. */
  164. par.net = dev_net(skb->dev);
  165. par.in = skb->dev;
  166. par.out = NULL;
  167. par.hooknum = ipt->tcfi_hook;
  168. par.target = ipt->tcfi_t->u.kernel.target;
  169. par.targinfo = ipt->tcfi_t->data;
  170. ret = par.target->target(skb, &par);
  171. switch (ret) {
  172. case NF_ACCEPT:
  173. result = TC_ACT_OK;
  174. break;
  175. case NF_DROP:
  176. result = TC_ACT_SHOT;
  177. ipt->tcf_qstats.drops++;
  178. break;
  179. case XT_CONTINUE:
  180. result = TC_ACT_PIPE;
  181. break;
  182. default:
  183. net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
  184. ret);
  185. result = TC_POLICE_OK;
  186. break;
  187. }
  188. spin_unlock(&ipt->tcf_lock);
  189. return result;
  190. }
  191. static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  192. {
  193. unsigned char *b = skb_tail_pointer(skb);
  194. struct tcf_ipt *ipt = a->priv;
  195. struct xt_entry_target *t;
  196. struct tcf_t tm;
  197. struct tc_cnt c;
  198. /* for simple targets kernel size == user size
  199. * user name = target name
  200. * for foolproof you need to not assume this
  201. */
  202. t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
  203. if (unlikely(!t))
  204. goto nla_put_failure;
  205. c.bindcnt = ipt->tcf_bindcnt - bind;
  206. c.refcnt = ipt->tcf_refcnt - ref;
  207. strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
  208. if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
  209. nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
  210. nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
  211. nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
  212. nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
  213. goto nla_put_failure;
  214. tm.install = jiffies_to_clock_t(jiffies - ipt->tcf_tm.install);
  215. tm.lastuse = jiffies_to_clock_t(jiffies - ipt->tcf_tm.lastuse);
  216. tm.expires = jiffies_to_clock_t(ipt->tcf_tm.expires);
  217. if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm))
  218. goto nla_put_failure;
  219. kfree(t);
  220. return skb->len;
  221. nla_put_failure:
  222. nlmsg_trim(skb, b);
  223. kfree(t);
  224. return -1;
  225. }
  226. static struct tc_action_ops act_ipt_ops = {
  227. .kind = "ipt",
  228. .type = TCA_ACT_IPT,
  229. .owner = THIS_MODULE,
  230. .act = tcf_ipt,
  231. .dump = tcf_ipt_dump,
  232. .cleanup = tcf_ipt_release,
  233. .init = tcf_ipt_init,
  234. };
  235. static struct tc_action_ops act_xt_ops = {
  236. .kind = "xt",
  237. .type = TCA_ACT_XT,
  238. .owner = THIS_MODULE,
  239. .act = tcf_ipt,
  240. .dump = tcf_ipt_dump,
  241. .cleanup = tcf_ipt_release,
  242. .init = tcf_ipt_init,
  243. };
  244. MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
  245. MODULE_DESCRIPTION("Iptables target actions");
  246. MODULE_LICENSE("GPL");
  247. MODULE_ALIAS("act_xt");
  248. static int __init ipt_init_module(void)
  249. {
  250. int ret1, ret2;
  251. ret1 = tcf_register_action(&act_xt_ops, IPT_TAB_MASK);
  252. if (ret1 < 0)
  253. printk("Failed to load xt action\n");
  254. ret2 = tcf_register_action(&act_ipt_ops, IPT_TAB_MASK);
  255. if (ret2 < 0)
  256. printk("Failed to load ipt action\n");
  257. if (ret1 < 0 && ret2 < 0) {
  258. return ret1;
  259. } else
  260. return 0;
  261. }
  262. static void __exit ipt_cleanup_module(void)
  263. {
  264. tcf_unregister_action(&act_xt_ops);
  265. tcf_unregister_action(&act_ipt_ops);
  266. }
  267. module_init(ipt_init_module);
  268. module_exit(ipt_cleanup_module);