act_ipt.c 7.4 KB

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