act_ipt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <asm/uaccess.h>
  14. #include <asm/system.h>
  15. #include <asm/bitops.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/in.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/kmod.h>
  32. #include <net/netlink.h>
  33. #include <net/sock.h>
  34. #include <net/pkt_sched.h>
  35. #include <linux/tc_act/tc_ipt.h>
  36. #include <net/tc_act/tc_ipt.h>
  37. #include <linux/netfilter_ipv4/ip_tables.h>
  38. #define IPT_TAB_MASK 15
  39. static struct tcf_common *tcf_ipt_ht[IPT_TAB_MASK + 1];
  40. static u32 ipt_idx_gen;
  41. static DEFINE_RWLOCK(ipt_lock);
  42. static struct tcf_hashinfo ipt_hash_info = {
  43. .htab = tcf_ipt_ht,
  44. .hmask = IPT_TAB_MASK,
  45. .lock = &ipt_lock,
  46. };
  47. static int ipt_init_target(struct ipt_entry_target *t, char *table, unsigned int hook)
  48. {
  49. struct xt_target *target;
  50. int ret = 0;
  51. target = xt_request_find_target(AF_INET, t->u.user.name,
  52. t->u.user.revision);
  53. if (!target)
  54. return -ENOENT;
  55. t->u.kernel.target = target;
  56. ret = xt_check_target(target, AF_INET, t->u.target_size - sizeof(*t),
  57. table, hook, 0, 0);
  58. if (ret) {
  59. module_put(t->u.kernel.target->me);
  60. return ret;
  61. }
  62. if (t->u.kernel.target->checkentry
  63. && !t->u.kernel.target->checkentry(table, NULL,
  64. t->u.kernel.target, t->data,
  65. hook)) {
  66. module_put(t->u.kernel.target->me);
  67. ret = -EINVAL;
  68. }
  69. return ret;
  70. }
  71. static void ipt_destroy_target(struct ipt_entry_target *t)
  72. {
  73. if (t->u.kernel.target->destroy)
  74. t->u.kernel.target->destroy(t->u.kernel.target, t->data);
  75. module_put(t->u.kernel.target->me);
  76. }
  77. static int tcf_ipt_release(struct tcf_ipt *ipt, int bind)
  78. {
  79. int ret = 0;
  80. if (ipt) {
  81. if (bind)
  82. ipt->tcf_bindcnt--;
  83. ipt->tcf_refcnt--;
  84. if (ipt->tcf_bindcnt <= 0 && ipt->tcf_refcnt <= 0) {
  85. ipt_destroy_target(ipt->tcfi_t);
  86. kfree(ipt->tcfi_tname);
  87. kfree(ipt->tcfi_t);
  88. tcf_hash_destroy(&ipt->common, &ipt_hash_info);
  89. ret = ACT_P_DELETED;
  90. }
  91. }
  92. return ret;
  93. }
  94. static int tcf_ipt_init(struct rtattr *rta, struct rtattr *est,
  95. struct tc_action *a, int ovr, int bind)
  96. {
  97. struct rtattr *tb[TCA_IPT_MAX];
  98. struct tcf_ipt *ipt;
  99. struct tcf_common *pc;
  100. struct ipt_entry_target *td, *t;
  101. char *tname;
  102. int ret = 0, err;
  103. u32 hook = 0;
  104. u32 index = 0;
  105. if (rta == NULL || rtattr_parse_nested(tb, TCA_IPT_MAX, rta) < 0)
  106. return -EINVAL;
  107. if (tb[TCA_IPT_HOOK-1] == NULL ||
  108. RTA_PAYLOAD(tb[TCA_IPT_HOOK-1]) < sizeof(u32))
  109. return -EINVAL;
  110. if (tb[TCA_IPT_TARG-1] == NULL ||
  111. RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < sizeof(*t))
  112. return -EINVAL;
  113. td = (struct ipt_entry_target *)RTA_DATA(tb[TCA_IPT_TARG-1]);
  114. if (RTA_PAYLOAD(tb[TCA_IPT_TARG-1]) < td->u.target_size)
  115. return -EINVAL;
  116. if (tb[TCA_IPT_INDEX-1] != NULL &&
  117. RTA_PAYLOAD(tb[TCA_IPT_INDEX-1]) >= sizeof(u32))
  118. index = *(u32 *)RTA_DATA(tb[TCA_IPT_INDEX-1]);
  119. pc = tcf_hash_check(index, a, bind, &ipt_hash_info);
  120. if (!pc) {
  121. pc = tcf_hash_create(index, est, a, sizeof(*ipt), bind,
  122. &ipt_idx_gen, &ipt_hash_info);
  123. if (unlikely(!pc))
  124. return -ENOMEM;
  125. ret = ACT_P_CREATED;
  126. } else {
  127. if (!ovr) {
  128. tcf_ipt_release(to_ipt(pc), bind);
  129. return -EEXIST;
  130. }
  131. }
  132. ipt = to_ipt(pc);
  133. hook = *(u32 *)RTA_DATA(tb[TCA_IPT_HOOK-1]);
  134. err = -ENOMEM;
  135. tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
  136. if (unlikely(!tname))
  137. goto err1;
  138. if (tb[TCA_IPT_TABLE - 1] == NULL ||
  139. rtattr_strlcpy(tname, tb[TCA_IPT_TABLE-1], IFNAMSIZ) >= IFNAMSIZ)
  140. strcpy(tname, "mangle");
  141. t = kmemdup(td, td->u.target_size, GFP_KERNEL);
  142. if (unlikely(!t))
  143. goto err2;
  144. if ((err = ipt_init_target(t, tname, hook)) < 0)
  145. goto err3;
  146. spin_lock_bh(&ipt->tcf_lock);
  147. if (ret != ACT_P_CREATED) {
  148. ipt_destroy_target(ipt->tcfi_t);
  149. kfree(ipt->tcfi_tname);
  150. kfree(ipt->tcfi_t);
  151. }
  152. ipt->tcfi_tname = tname;
  153. ipt->tcfi_t = t;
  154. ipt->tcfi_hook = hook;
  155. spin_unlock_bh(&ipt->tcf_lock);
  156. if (ret == ACT_P_CREATED)
  157. tcf_hash_insert(pc, &ipt_hash_info);
  158. return ret;
  159. err3:
  160. kfree(t);
  161. err2:
  162. kfree(tname);
  163. err1:
  164. kfree(pc);
  165. return err;
  166. }
  167. static int tcf_ipt_cleanup(struct tc_action *a, int bind)
  168. {
  169. struct tcf_ipt *ipt = a->priv;
  170. return tcf_ipt_release(ipt, bind);
  171. }
  172. static int tcf_ipt(struct sk_buff *skb, struct tc_action *a,
  173. struct tcf_result *res)
  174. {
  175. int ret = 0, result = 0;
  176. struct tcf_ipt *ipt = a->priv;
  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. ipt->tcf_bstats.bytes += skb->len;
  184. ipt->tcf_bstats.packets++;
  185. /* yes, we have to worry about both in and out dev
  186. worry later - danger - this API seems to have changed
  187. from earlier kernels */
  188. /* iptables targets take a double skb pointer in case the skb
  189. * needs to be replaced. We don't own the skb, so this must not
  190. * happen. The pskb_expand_head above should make sure of this */
  191. ret = ipt->tcfi_t->u.kernel.target->target(&skb, skb->dev, NULL,
  192. ipt->tcfi_hook,
  193. ipt->tcfi_t->u.kernel.target,
  194. ipt->tcfi_t->data);
  195. switch (ret) {
  196. case NF_ACCEPT:
  197. result = TC_ACT_OK;
  198. break;
  199. case NF_DROP:
  200. result = TC_ACT_SHOT;
  201. ipt->tcf_qstats.drops++;
  202. break;
  203. case IPT_CONTINUE:
  204. result = TC_ACT_PIPE;
  205. break;
  206. default:
  207. if (net_ratelimit())
  208. printk("Bogus netfilter code %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 ipt_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 rtattr_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. RTA_PUT(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t);
  233. RTA_PUT(skb, TCA_IPT_INDEX, 4, &ipt->tcf_index);
  234. RTA_PUT(skb, TCA_IPT_HOOK, 4, &ipt->tcfi_hook);
  235. RTA_PUT(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c);
  236. RTA_PUT(skb, TCA_IPT_TABLE, IFNAMSIZ, 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. RTA_PUT(skb, TCA_IPT_TM, sizeof (tm), &tm);
  241. kfree(t);
  242. return skb->len;
  243. rtattr_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);