act_mirred.c 6.7 KB

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