act_mirred.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * net/sched/act_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 LIST_HEAD(mirred_list);
  31. static void tcf_mirred_release(struct tc_action *a, int bind)
  32. {
  33. struct tcf_mirred *m = to_mirred(a);
  34. list_del(&m->tcfm_list);
  35. if (m->tcfm_dev)
  36. dev_put(m->tcfm_dev);
  37. }
  38. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  39. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  40. };
  41. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  42. struct nlattr *est, struct tc_action *a, int ovr,
  43. int bind)
  44. {
  45. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  46. struct tc_mirred *parm;
  47. struct tcf_mirred *m;
  48. struct net_device *dev;
  49. int ret, ok_push = 0;
  50. if (nla == NULL)
  51. return -EINVAL;
  52. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
  53. if (ret < 0)
  54. return ret;
  55. if (tb[TCA_MIRRED_PARMS] == NULL)
  56. return -EINVAL;
  57. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  58. switch (parm->eaction) {
  59. case TCA_EGRESS_MIRROR:
  60. case TCA_EGRESS_REDIR:
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. if (parm->ifindex) {
  66. dev = __dev_get_by_index(net, parm->ifindex);
  67. if (dev == NULL)
  68. return -ENODEV;
  69. switch (dev->type) {
  70. case ARPHRD_TUNNEL:
  71. case ARPHRD_TUNNEL6:
  72. case ARPHRD_SIT:
  73. case ARPHRD_IPGRE:
  74. case ARPHRD_VOID:
  75. case ARPHRD_NONE:
  76. ok_push = 0;
  77. break;
  78. default:
  79. ok_push = 1;
  80. break;
  81. }
  82. } else {
  83. dev = NULL;
  84. }
  85. if (!tcf_hash_check(parm->index, a, bind)) {
  86. if (dev == NULL)
  87. return -EINVAL;
  88. ret = tcf_hash_create(parm->index, est, a, sizeof(*m), bind);
  89. if (ret)
  90. return ret;
  91. ret = ACT_P_CREATED;
  92. } else {
  93. if (!ovr) {
  94. tcf_hash_release(a, bind);
  95. return -EEXIST;
  96. }
  97. }
  98. m = to_mirred(a);
  99. spin_lock_bh(&m->tcf_lock);
  100. m->tcf_action = parm->action;
  101. m->tcfm_eaction = parm->eaction;
  102. if (dev != NULL) {
  103. m->tcfm_ifindex = parm->ifindex;
  104. if (ret != ACT_P_CREATED)
  105. dev_put(m->tcfm_dev);
  106. dev_hold(dev);
  107. m->tcfm_dev = dev;
  108. m->tcfm_ok_push = ok_push;
  109. }
  110. spin_unlock_bh(&m->tcf_lock);
  111. if (ret == ACT_P_CREATED) {
  112. list_add(&m->tcfm_list, &mirred_list);
  113. tcf_hash_insert(a);
  114. }
  115. return ret;
  116. }
  117. static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
  118. struct tcf_result *res)
  119. {
  120. struct tcf_mirred *m = a->priv;
  121. struct net_device *dev;
  122. struct sk_buff *skb2;
  123. u32 at;
  124. int retval, err = 1;
  125. spin_lock(&m->tcf_lock);
  126. m->tcf_tm.lastuse = jiffies;
  127. bstats_update(&m->tcf_bstats, skb);
  128. dev = m->tcfm_dev;
  129. if (!dev) {
  130. printk_once(KERN_NOTICE "tc mirred: target device is gone\n");
  131. goto out;
  132. }
  133. if (!(dev->flags & IFF_UP)) {
  134. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  135. dev->name);
  136. goto out;
  137. }
  138. at = G_TC_AT(skb->tc_verd);
  139. skb2 = skb_clone(skb, GFP_ATOMIC);
  140. if (skb2 == NULL)
  141. goto out;
  142. if (!(at & AT_EGRESS)) {
  143. if (m->tcfm_ok_push)
  144. skb_push(skb2, skb->mac_len);
  145. }
  146. /* mirror is always swallowed */
  147. if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
  148. skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
  149. skb2->skb_iif = skb->dev->ifindex;
  150. skb2->dev = dev;
  151. err = dev_queue_xmit(skb2);
  152. out:
  153. if (err) {
  154. m->tcf_qstats.overlimits++;
  155. if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
  156. retval = TC_ACT_SHOT;
  157. else
  158. retval = m->tcf_action;
  159. } else
  160. retval = m->tcf_action;
  161. spin_unlock(&m->tcf_lock);
  162. return retval;
  163. }
  164. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  165. {
  166. unsigned char *b = skb_tail_pointer(skb);
  167. struct tcf_mirred *m = a->priv;
  168. struct tc_mirred opt = {
  169. .index = m->tcf_index,
  170. .action = m->tcf_action,
  171. .refcnt = m->tcf_refcnt - ref,
  172. .bindcnt = m->tcf_bindcnt - bind,
  173. .eaction = m->tcfm_eaction,
  174. .ifindex = m->tcfm_ifindex,
  175. };
  176. struct tcf_t t;
  177. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  178. goto nla_put_failure;
  179. t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
  180. t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
  181. t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
  182. if (nla_put(skb, TCA_MIRRED_TM, sizeof(t), &t))
  183. goto nla_put_failure;
  184. return skb->len;
  185. nla_put_failure:
  186. nlmsg_trim(skb, b);
  187. return -1;
  188. }
  189. static int mirred_device_event(struct notifier_block *unused,
  190. unsigned long event, void *ptr)
  191. {
  192. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  193. struct tcf_mirred *m;
  194. if (event == NETDEV_UNREGISTER)
  195. list_for_each_entry(m, &mirred_list, tcfm_list) {
  196. spin_lock_bh(&m->tcf_lock);
  197. if (m->tcfm_dev == dev) {
  198. dev_put(dev);
  199. m->tcfm_dev = NULL;
  200. }
  201. spin_unlock_bh(&m->tcf_lock);
  202. }
  203. return NOTIFY_DONE;
  204. }
  205. static struct notifier_block mirred_device_notifier = {
  206. .notifier_call = mirred_device_event,
  207. };
  208. static struct tc_action_ops act_mirred_ops = {
  209. .kind = "mirred",
  210. .type = TCA_ACT_MIRRED,
  211. .owner = THIS_MODULE,
  212. .act = tcf_mirred,
  213. .dump = tcf_mirred_dump,
  214. .cleanup = tcf_mirred_release,
  215. .init = tcf_mirred_init,
  216. };
  217. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  218. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  219. MODULE_LICENSE("GPL");
  220. static int __init mirred_init_module(void)
  221. {
  222. int err = register_netdevice_notifier(&mirred_device_notifier);
  223. if (err)
  224. return err;
  225. pr_info("Mirror/redirect action on\n");
  226. return tcf_register_action(&act_mirred_ops, MIRRED_TAB_MASK);
  227. }
  228. static void __exit mirred_cleanup_module(void)
  229. {
  230. tcf_unregister_action(&act_mirred_ops);
  231. unregister_netdevice_notifier(&mirred_device_notifier);
  232. }
  233. module_init(mirred_init_module);
  234. module_exit(mirred_cleanup_module);