act_mirred.c 6.0 KB

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