act_ipt.c 8.0 KB

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