act_ipt.c 7.4 KB

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