ipt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 = ipt_find_target(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 **pskb, struct tc_action *a)
  179. {
  180. int ret = 0, result = 0;
  181. struct tcf_ipt *p = PRIV(a, ipt);
  182. struct sk_buff *skb = *pskb;
  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. ret = p->t->u.kernel.target->target(&skb, skb->dev, NULL,
  195. p->hook, p->t->data, NULL);
  196. switch (ret) {
  197. case NF_ACCEPT:
  198. result = TC_ACT_OK;
  199. break;
  200. case NF_DROP:
  201. result = TC_ACT_SHOT;
  202. p->qstats.drops++;
  203. break;
  204. case IPT_CONTINUE:
  205. result = TC_ACT_PIPE;
  206. break;
  207. default:
  208. if (net_ratelimit())
  209. printk("Bogus netfilter code %d assume ACCEPT\n", ret);
  210. result = TC_POLICE_OK;
  211. break;
  212. }
  213. spin_unlock(&p->lock);
  214. return result;
  215. }
  216. static int
  217. tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  218. {
  219. struct ipt_entry_target *t;
  220. struct tcf_t tm;
  221. struct tc_cnt c;
  222. unsigned char *b = skb->tail;
  223. struct tcf_ipt *p = PRIV(a, ipt);
  224. /* for simple targets kernel size == user size
  225. ** user name = target name
  226. ** for foolproof you need to not assume this
  227. */
  228. t = kmalloc(p->t->u.user.target_size, GFP_ATOMIC);
  229. if (t == NULL)
  230. goto rtattr_failure;
  231. c.bindcnt = p->bindcnt - bind;
  232. c.refcnt = p->refcnt - ref;
  233. memcpy(t, p->t, p->t->u.user.target_size);
  234. strcpy(t->u.user.name, p->t->u.kernel.target->name);
  235. DPRINTK("\ttcf_ipt_dump tablename %s length %d\n", p->tname,
  236. strlen(p->tname));
  237. DPRINTK("\tdump target name %s size %d size user %d "
  238. "data[0] %x data[1] %x\n", p->t->u.kernel.target->name,
  239. p->t->u.target_size, p->t->u.user.target_size,
  240. p->t->data[0], p->t->data[1]);
  241. RTA_PUT(skb, TCA_IPT_TARG, p->t->u.user.target_size, t);
  242. RTA_PUT(skb, TCA_IPT_INDEX, 4, &p->index);
  243. RTA_PUT(skb, TCA_IPT_HOOK, 4, &p->hook);
  244. RTA_PUT(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c);
  245. RTA_PUT(skb, TCA_IPT_TABLE, IFNAMSIZ, p->tname);
  246. tm.install = jiffies_to_clock_t(jiffies - p->tm.install);
  247. tm.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
  248. tm.expires = jiffies_to_clock_t(p->tm.expires);
  249. RTA_PUT(skb, TCA_IPT_TM, sizeof (tm), &tm);
  250. kfree(t);
  251. return skb->len;
  252. rtattr_failure:
  253. skb_trim(skb, b - skb->data);
  254. kfree(t);
  255. return -1;
  256. }
  257. static struct tc_action_ops act_ipt_ops = {
  258. .kind = "ipt",
  259. .type = TCA_ACT_IPT,
  260. .capab = TCA_CAP_NONE,
  261. .owner = THIS_MODULE,
  262. .act = tcf_ipt,
  263. .dump = tcf_ipt_dump,
  264. .cleanup = tcf_ipt_cleanup,
  265. .lookup = tcf_hash_search,
  266. .init = tcf_ipt_init,
  267. .walk = tcf_generic_walker
  268. };
  269. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  270. MODULE_DESCRIPTION("Iptables target actions");
  271. MODULE_LICENSE("GPL");
  272. static int __init
  273. ipt_init_module(void)
  274. {
  275. return tcf_register_action(&act_ipt_ops);
  276. }
  277. static void __exit
  278. ipt_cleanup_module(void)
  279. {
  280. tcf_unregister_action(&act_ipt_ops);
  281. }
  282. module_init(ipt_init_module);
  283. module_exit(ipt_cleanup_module);