act_mirred.c 6.0 KB

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