act_mirred.c 6.3 KB

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