act_mirred.c 6.7 KB

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