act_mirred.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. struct net_device *dev = rcu_dereference_protected(m->tcfm_dev, 1);
  35. list_del(&m->tcfm_list);
  36. if (dev)
  37. dev_put(dev);
  38. }
  39. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  40. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  41. };
  42. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  43. struct nlattr *est, struct tc_action *a, int ovr,
  44. int bind)
  45. {
  46. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  47. struct tc_mirred *parm;
  48. struct tcf_mirred *m;
  49. struct net_device *dev;
  50. int ret, ok_push = 0;
  51. if (nla == NULL)
  52. return -EINVAL;
  53. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
  54. if (ret < 0)
  55. return ret;
  56. if (tb[TCA_MIRRED_PARMS] == NULL)
  57. return -EINVAL;
  58. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  59. switch (parm->eaction) {
  60. case TCA_EGRESS_MIRROR:
  61. case TCA_EGRESS_REDIR:
  62. break;
  63. default:
  64. return -EINVAL;
  65. }
  66. if (parm->ifindex) {
  67. dev = __dev_get_by_index(net, 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. } else {
  84. dev = NULL;
  85. }
  86. if (!tcf_hash_check(parm->index, a, bind)) {
  87. if (dev == NULL)
  88. return -EINVAL;
  89. ret = tcf_hash_create(parm->index, est, a, sizeof(*m),
  90. bind, true);
  91. if (ret)
  92. return ret;
  93. ret = ACT_P_CREATED;
  94. } else {
  95. if (bind)
  96. return 0;
  97. if (!ovr) {
  98. tcf_hash_release(a, bind);
  99. return -EEXIST;
  100. }
  101. }
  102. m = to_mirred(a);
  103. ASSERT_RTNL();
  104. m->tcf_action = parm->action;
  105. m->tcfm_eaction = parm->eaction;
  106. if (dev != NULL) {
  107. m->tcfm_ifindex = parm->ifindex;
  108. if (ret != ACT_P_CREATED)
  109. dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
  110. dev_hold(dev);
  111. rcu_assign_pointer(m->tcfm_dev, dev);
  112. m->tcfm_ok_push = ok_push;
  113. }
  114. if (ret == ACT_P_CREATED) {
  115. list_add(&m->tcfm_list, &mirred_list);
  116. tcf_hash_insert(a);
  117. }
  118. return ret;
  119. }
  120. static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
  121. struct tcf_result *res)
  122. {
  123. struct tcf_mirred *m = a->priv;
  124. struct net_device *dev;
  125. struct sk_buff *skb2;
  126. int retval, err;
  127. u32 at;
  128. tcf_lastuse_update(&m->tcf_tm);
  129. bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
  130. rcu_read_lock();
  131. retval = READ_ONCE(m->tcf_action);
  132. dev = rcu_dereference(m->tcfm_dev);
  133. if (unlikely(!dev)) {
  134. pr_notice_once("tc mirred: target device is gone\n");
  135. goto out;
  136. }
  137. if (unlikely(!(dev->flags & IFF_UP))) {
  138. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  139. dev->name);
  140. goto out;
  141. }
  142. at = G_TC_AT(skb->tc_verd);
  143. skb2 = skb_clone(skb, GFP_ATOMIC);
  144. if (!skb2)
  145. goto out;
  146. if (!(at & AT_EGRESS)) {
  147. if (m->tcfm_ok_push)
  148. skb_push(skb2, skb->mac_len);
  149. }
  150. /* mirror is always swallowed */
  151. if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
  152. skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
  153. skb2->skb_iif = skb->dev->ifindex;
  154. skb2->dev = dev;
  155. err = dev_queue_xmit(skb2);
  156. if (err) {
  157. out:
  158. qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
  159. if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
  160. retval = TC_ACT_SHOT;
  161. }
  162. rcu_read_unlock();
  163. return retval;
  164. }
  165. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  166. {
  167. unsigned char *b = skb_tail_pointer(skb);
  168. struct tcf_mirred *m = a->priv;
  169. struct tc_mirred opt = {
  170. .index = m->tcf_index,
  171. .action = m->tcf_action,
  172. .refcnt = m->tcf_refcnt - ref,
  173. .bindcnt = m->tcf_bindcnt - bind,
  174. .eaction = m->tcfm_eaction,
  175. .ifindex = m->tcfm_ifindex,
  176. };
  177. struct tcf_t t;
  178. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  179. goto nla_put_failure;
  180. t.install = jiffies_to_clock_t(jiffies - m->tcf_tm.install);
  181. t.lastuse = jiffies_to_clock_t(jiffies - m->tcf_tm.lastuse);
  182. t.expires = jiffies_to_clock_t(m->tcf_tm.expires);
  183. if (nla_put(skb, TCA_MIRRED_TM, sizeof(t), &t))
  184. goto nla_put_failure;
  185. return skb->len;
  186. nla_put_failure:
  187. nlmsg_trim(skb, b);
  188. return -1;
  189. }
  190. static int mirred_device_event(struct notifier_block *unused,
  191. unsigned long event, void *ptr)
  192. {
  193. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  194. struct tcf_mirred *m;
  195. ASSERT_RTNL();
  196. if (event == NETDEV_UNREGISTER)
  197. list_for_each_entry(m, &mirred_list, tcfm_list) {
  198. if (rcu_access_pointer(m->tcfm_dev) == dev) {
  199. dev_put(dev);
  200. /* Note : no rcu grace period necessary, as
  201. * net_device are already rcu protected.
  202. */
  203. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  204. }
  205. }
  206. return NOTIFY_DONE;
  207. }
  208. static struct notifier_block mirred_device_notifier = {
  209. .notifier_call = mirred_device_event,
  210. };
  211. static struct tc_action_ops act_mirred_ops = {
  212. .kind = "mirred",
  213. .type = TCA_ACT_MIRRED,
  214. .owner = THIS_MODULE,
  215. .act = tcf_mirred,
  216. .dump = tcf_mirred_dump,
  217. .cleanup = tcf_mirred_release,
  218. .init = tcf_mirred_init,
  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. int err = register_netdevice_notifier(&mirred_device_notifier);
  226. if (err)
  227. return err;
  228. pr_info("Mirror/redirect action on\n");
  229. return tcf_register_action(&act_mirred_ops, MIRRED_TAB_MASK);
  230. }
  231. static void __exit mirred_cleanup_module(void)
  232. {
  233. tcf_unregister_action(&act_mirred_ops);
  234. unregister_netdevice_notifier(&mirred_device_notifier);
  235. }
  236. module_init(mirred_init_module);
  237. module_exit(mirred_cleanup_module);