act_ipt.c 7.8 KB

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