act_mirred.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * net/sched/mirred.c packet mirroring and redirect actions
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Jamal Hadi Salim (2002-4)
  10. *
  11. * TODO: Add ingress support (and socket redirect support)
  12. *
  13. */
  14. #include <asm/uaccess.h>
  15. #include <asm/system.h>
  16. #include <asm/bitops.h>
  17. #include <linux/config.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/in.h>
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/proc_fs.h>
  34. #include <net/sock.h>
  35. #include <net/pkt_sched.h>
  36. #include <linux/tc_act/tc_mirred.h>
  37. #include <net/tc_act/tc_mirred.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/if_arp.h>
  40. /* use generic hash table */
  41. #define MY_TAB_SIZE 8
  42. #define MY_TAB_MASK (MY_TAB_SIZE - 1)
  43. static u32 idx_gen;
  44. static struct tcf_mirred *tcf_mirred_ht[MY_TAB_SIZE];
  45. static DEFINE_RWLOCK(mirred_lock);
  46. /* ovewrride the defaults */
  47. #define tcf_st tcf_mirred
  48. #define tc_st tc_mirred
  49. #define tcf_t_lock mirred_lock
  50. #define tcf_ht tcf_mirred_ht
  51. #define CONFIG_NET_ACT_INIT 1
  52. #include <net/pkt_act.h>
  53. static inline int
  54. tcf_mirred_release(struct tcf_mirred *p, int bind)
  55. {
  56. if (p) {
  57. if (bind)
  58. p->bindcnt--;
  59. p->refcnt--;
  60. if(!p->bindcnt && p->refcnt <= 0) {
  61. dev_put(p->dev);
  62. tcf_hash_destroy(p);
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }
  68. static int
  69. tcf_mirred_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a,
  70. int ovr, int bind)
  71. {
  72. struct rtattr *tb[TCA_MIRRED_MAX];
  73. struct tc_mirred *parm;
  74. struct tcf_mirred *p;
  75. struct net_device *dev = NULL;
  76. int ret = 0;
  77. int ok_push = 0;
  78. if (rta == NULL || rtattr_parse_nested(tb, TCA_MIRRED_MAX, rta) < 0)
  79. return -EINVAL;
  80. if (tb[TCA_MIRRED_PARMS-1] == NULL ||
  81. RTA_PAYLOAD(tb[TCA_MIRRED_PARMS-1]) < sizeof(*parm))
  82. return -EINVAL;
  83. parm = RTA_DATA(tb[TCA_MIRRED_PARMS-1]);
  84. if (parm->ifindex) {
  85. dev = __dev_get_by_index(parm->ifindex);
  86. if (dev == NULL)
  87. return -ENODEV;
  88. switch (dev->type) {
  89. case ARPHRD_TUNNEL:
  90. case ARPHRD_TUNNEL6:
  91. case ARPHRD_SIT:
  92. case ARPHRD_IPGRE:
  93. case ARPHRD_VOID:
  94. case ARPHRD_NONE:
  95. ok_push = 0;
  96. break;
  97. default:
  98. ok_push = 1;
  99. break;
  100. }
  101. }
  102. p = tcf_hash_check(parm->index, a, ovr, bind);
  103. if (p == NULL) {
  104. if (!parm->ifindex)
  105. return -EINVAL;
  106. p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind);
  107. if (p == NULL)
  108. return -ENOMEM;
  109. ret = ACT_P_CREATED;
  110. } else {
  111. if (!ovr) {
  112. tcf_mirred_release(p, bind);
  113. return -EEXIST;
  114. }
  115. }
  116. spin_lock_bh(&p->lock);
  117. p->action = parm->action;
  118. p->eaction = parm->eaction;
  119. if (parm->ifindex) {
  120. p->ifindex = parm->ifindex;
  121. if (ret != ACT_P_CREATED)
  122. dev_put(p->dev);
  123. p->dev = dev;
  124. dev_hold(dev);
  125. p->ok_push = ok_push;
  126. }
  127. spin_unlock_bh(&p->lock);
  128. if (ret == ACT_P_CREATED)
  129. tcf_hash_insert(p);
  130. DPRINTK("tcf_mirred_init index %d action %d eaction %d device %s "
  131. "ifindex %d\n", parm->index, parm->action, parm->eaction,
  132. dev->name, parm->ifindex);
  133. return ret;
  134. }
  135. static int
  136. tcf_mirred_cleanup(struct tc_action *a, int bind)
  137. {
  138. struct tcf_mirred *p = PRIV(a, mirred);
  139. if (p != NULL)
  140. return tcf_mirred_release(p, bind);
  141. return 0;
  142. }
  143. static int
  144. tcf_mirred(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  145. {
  146. struct tcf_mirred *p = PRIV(a, mirred);
  147. struct net_device *dev;
  148. struct sk_buff *skb2 = NULL;
  149. u32 at = G_TC_AT(skb->tc_verd);
  150. spin_lock(&p->lock);
  151. dev = p->dev;
  152. p->tm.lastuse = jiffies;
  153. if (!(dev->flags&IFF_UP) ) {
  154. if (net_ratelimit())
  155. printk("mirred to Houston: device %s is gone!\n",
  156. dev->name);
  157. bad_mirred:
  158. if (skb2 != NULL)
  159. kfree_skb(skb2);
  160. p->qstats.overlimits++;
  161. p->bstats.bytes += skb->len;
  162. p->bstats.packets++;
  163. spin_unlock(&p->lock);
  164. /* should we be asking for packet to be dropped?
  165. * may make sense for redirect case only
  166. */
  167. return TC_ACT_SHOT;
  168. }
  169. skb2 = skb_clone(skb, GFP_ATOMIC);
  170. if (skb2 == NULL)
  171. goto bad_mirred;
  172. if (p->eaction != TCA_EGRESS_MIRROR && p->eaction != TCA_EGRESS_REDIR) {
  173. if (net_ratelimit())
  174. printk("tcf_mirred unknown action %d\n", p->eaction);
  175. goto bad_mirred;
  176. }
  177. p->bstats.bytes += skb2->len;
  178. p->bstats.packets++;
  179. if (!(at & AT_EGRESS))
  180. if (p->ok_push)
  181. skb_push(skb2, skb2->dev->hard_header_len);
  182. /* mirror is always swallowed */
  183. if (p->eaction != TCA_EGRESS_MIRROR)
  184. skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
  185. skb2->dev = dev;
  186. skb2->input_dev = skb->dev;
  187. dev_queue_xmit(skb2);
  188. spin_unlock(&p->lock);
  189. return p->action;
  190. }
  191. static int
  192. tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  193. {
  194. unsigned char *b = skb->tail;
  195. struct tc_mirred opt;
  196. struct tcf_mirred *p = PRIV(a, mirred);
  197. struct tcf_t t;
  198. opt.index = p->index;
  199. opt.action = p->action;
  200. opt.refcnt = p->refcnt - ref;
  201. opt.bindcnt = p->bindcnt - bind;
  202. opt.eaction = p->eaction;
  203. opt.ifindex = p->ifindex;
  204. DPRINTK("tcf_mirred_dump index %d action %d eaction %d ifindex %d\n",
  205. p->index, p->action, p->eaction, p->ifindex);
  206. RTA_PUT(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt);
  207. t.install = jiffies_to_clock_t(jiffies - p->tm.install);
  208. t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse);
  209. t.expires = jiffies_to_clock_t(p->tm.expires);
  210. RTA_PUT(skb, TCA_MIRRED_TM, sizeof(t), &t);
  211. return skb->len;
  212. rtattr_failure:
  213. skb_trim(skb, b - skb->data);
  214. return -1;
  215. }
  216. static struct tc_action_ops act_mirred_ops = {
  217. .kind = "mirred",
  218. .type = TCA_ACT_MIRRED,
  219. .capab = TCA_CAP_NONE,
  220. .owner = THIS_MODULE,
  221. .act = tcf_mirred,
  222. .dump = tcf_mirred_dump,
  223. .cleanup = tcf_mirred_cleanup,
  224. .lookup = tcf_hash_search,
  225. .init = tcf_mirred_init,
  226. .walk = tcf_generic_walker
  227. };
  228. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  229. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  230. MODULE_LICENSE("GPL");
  231. static int __init
  232. mirred_init_module(void)
  233. {
  234. printk("Mirror/redirect action on\n");
  235. return tcf_register_action(&act_mirred_ops);
  236. }
  237. static void __exit
  238. mirred_cleanup_module(void)
  239. {
  240. tcf_unregister_action(&act_mirred_ops);
  241. }
  242. module_init(mirred_init_module);
  243. module_exit(mirred_cleanup_module);