act_ipt.c 7.3 KB

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