act_ipt.c 8.0 KB

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