act_mirred.c 6.1 KB

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