act_mirred.c 6.2 KB

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