act_ipt.c 7.9 KB

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