act_ipt.c 8.0 KB

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