act_mirred.c 8.2 KB

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