act_mirred.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <linux/if_arp.h>
  24. #include <net/net_namespace.h>
  25. #include <net/netlink.h>
  26. #include <net/pkt_sched.h>
  27. #include <linux/tc_act/tc_mirred.h>
  28. #include <net/tc_act/tc_mirred.h>
  29. static LIST_HEAD(mirred_list);
  30. static DEFINE_SPINLOCK(mirred_list_lock);
  31. static bool tcf_mirred_is_act_redirect(int action)
  32. {
  33. return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
  34. }
  35. static bool tcf_mirred_act_wants_ingress(int action)
  36. {
  37. switch (action) {
  38. case TCA_EGRESS_REDIR:
  39. case TCA_EGRESS_MIRROR:
  40. return false;
  41. case TCA_INGRESS_REDIR:
  42. case TCA_INGRESS_MIRROR:
  43. return true;
  44. default:
  45. BUG();
  46. }
  47. }
  48. static void tcf_mirred_release(struct tc_action *a, int bind)
  49. {
  50. struct tcf_mirred *m = to_mirred(a);
  51. struct net_device *dev;
  52. /* We could be called either in a RCU callback or with RTNL lock held. */
  53. spin_lock_bh(&mirred_list_lock);
  54. list_del(&m->tcfm_list);
  55. dev = rcu_dereference_protected(m->tcfm_dev, 1);
  56. if (dev)
  57. dev_put(dev);
  58. spin_unlock_bh(&mirred_list_lock);
  59. }
  60. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  61. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  62. };
  63. static unsigned int mirred_net_id;
  64. static struct tc_action_ops act_mirred_ops;
  65. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  66. struct nlattr *est, struct tc_action **a, int ovr,
  67. int bind)
  68. {
  69. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  70. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  71. bool mac_header_xmit = false;
  72. struct tc_mirred *parm;
  73. struct tcf_mirred *m;
  74. struct net_device *dev;
  75. bool exists = false;
  76. int ret;
  77. if (nla == NULL)
  78. return -EINVAL;
  79. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, NULL);
  80. if (ret < 0)
  81. return ret;
  82. if (tb[TCA_MIRRED_PARMS] == NULL)
  83. return -EINVAL;
  84. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  85. exists = tcf_idr_check(tn, parm->index, a, bind);
  86. if (exists && bind)
  87. return 0;
  88. switch (parm->eaction) {
  89. case TCA_EGRESS_MIRROR:
  90. case TCA_EGRESS_REDIR:
  91. case TCA_INGRESS_REDIR:
  92. case TCA_INGRESS_MIRROR:
  93. break;
  94. default:
  95. if (exists)
  96. tcf_idr_release(*a, bind);
  97. return -EINVAL;
  98. }
  99. if (parm->ifindex) {
  100. dev = __dev_get_by_index(net, parm->ifindex);
  101. if (dev == NULL) {
  102. if (exists)
  103. tcf_idr_release(*a, bind);
  104. return -ENODEV;
  105. }
  106. mac_header_xmit = dev_is_mac_header_xmit(dev);
  107. } else {
  108. dev = NULL;
  109. }
  110. if (!exists) {
  111. if (dev == NULL)
  112. return -EINVAL;
  113. ret = tcf_idr_create(tn, parm->index, est, a,
  114. &act_mirred_ops, bind, true);
  115. if (ret)
  116. return ret;
  117. ret = ACT_P_CREATED;
  118. } else {
  119. tcf_idr_release(*a, bind);
  120. if (!ovr)
  121. return -EEXIST;
  122. }
  123. m = to_mirred(*a);
  124. ASSERT_RTNL();
  125. m->tcf_action = parm->action;
  126. m->tcfm_eaction = parm->eaction;
  127. if (dev != NULL) {
  128. m->tcfm_ifindex = parm->ifindex;
  129. if (ret != ACT_P_CREATED)
  130. dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
  131. dev_hold(dev);
  132. rcu_assign_pointer(m->tcfm_dev, dev);
  133. m->tcfm_mac_header_xmit = mac_header_xmit;
  134. }
  135. if (ret == ACT_P_CREATED) {
  136. spin_lock_bh(&mirred_list_lock);
  137. list_add(&m->tcfm_list, &mirred_list);
  138. spin_unlock_bh(&mirred_list_lock);
  139. tcf_idr_insert(tn, *a);
  140. }
  141. return ret;
  142. }
  143. static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
  144. struct tcf_result *res)
  145. {
  146. struct tcf_mirred *m = to_mirred(a);
  147. bool m_mac_header_xmit;
  148. struct net_device *dev;
  149. struct sk_buff *skb2;
  150. int retval, err = 0;
  151. int m_eaction;
  152. int mac_len;
  153. tcf_lastuse_update(&m->tcf_tm);
  154. bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
  155. rcu_read_lock();
  156. m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  157. m_eaction = READ_ONCE(m->tcfm_eaction);
  158. retval = READ_ONCE(m->tcf_action);
  159. dev = rcu_dereference(m->tcfm_dev);
  160. if (unlikely(!dev)) {
  161. pr_notice_once("tc mirred: target device is gone\n");
  162. goto out;
  163. }
  164. if (unlikely(!(dev->flags & IFF_UP))) {
  165. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  166. dev->name);
  167. goto out;
  168. }
  169. skb2 = skb_clone(skb, GFP_ATOMIC);
  170. if (!skb2)
  171. goto out;
  172. /* If action's target direction differs than filter's direction,
  173. * and devices expect a mac header on xmit, then mac push/pull is
  174. * needed.
  175. */
  176. if (skb_at_tc_ingress(skb) != tcf_mirred_act_wants_ingress(m_eaction) &&
  177. m_mac_header_xmit) {
  178. if (!skb_at_tc_ingress(skb)) {
  179. /* caught at egress, act ingress: pull mac */
  180. mac_len = skb_network_header(skb) - skb_mac_header(skb);
  181. skb_pull_rcsum(skb2, mac_len);
  182. } else {
  183. /* caught at ingress, act egress: push mac */
  184. skb_push_rcsum(skb2, skb->mac_len);
  185. }
  186. }
  187. /* mirror is always swallowed */
  188. if (tcf_mirred_is_act_redirect(m_eaction)) {
  189. skb2->tc_redirected = 1;
  190. skb2->tc_from_ingress = skb2->tc_at_ingress;
  191. }
  192. skb2->skb_iif = skb->dev->ifindex;
  193. skb2->dev = dev;
  194. if (!tcf_mirred_act_wants_ingress(m_eaction))
  195. err = dev_queue_xmit(skb2);
  196. else
  197. err = netif_receive_skb(skb2);
  198. if (err) {
  199. out:
  200. qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
  201. if (tcf_mirred_is_act_redirect(m_eaction))
  202. retval = TC_ACT_SHOT;
  203. }
  204. rcu_read_unlock();
  205. return retval;
  206. }
  207. static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
  208. u64 lastuse)
  209. {
  210. struct tcf_mirred *m = to_mirred(a);
  211. struct tcf_t *tm = &m->tcf_tm;
  212. _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
  213. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  214. }
  215. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  216. int ref)
  217. {
  218. unsigned char *b = skb_tail_pointer(skb);
  219. struct tcf_mirred *m = to_mirred(a);
  220. struct tc_mirred opt = {
  221. .index = m->tcf_index,
  222. .action = m->tcf_action,
  223. .refcnt = m->tcf_refcnt - ref,
  224. .bindcnt = m->tcf_bindcnt - bind,
  225. .eaction = m->tcfm_eaction,
  226. .ifindex = m->tcfm_ifindex,
  227. };
  228. struct tcf_t t;
  229. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  230. goto nla_put_failure;
  231. tcf_tm_dump(&t, &m->tcf_tm);
  232. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  233. goto nla_put_failure;
  234. return skb->len;
  235. nla_put_failure:
  236. nlmsg_trim(skb, b);
  237. return -1;
  238. }
  239. static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
  240. struct netlink_callback *cb, int type,
  241. const struct tc_action_ops *ops)
  242. {
  243. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  244. return tcf_generic_walker(tn, skb, cb, type, ops);
  245. }
  246. static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
  247. {
  248. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  249. return tcf_idr_search(tn, a, index);
  250. }
  251. static int mirred_device_event(struct notifier_block *unused,
  252. unsigned long event, void *ptr)
  253. {
  254. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  255. struct tcf_mirred *m;
  256. ASSERT_RTNL();
  257. if (event == NETDEV_UNREGISTER) {
  258. spin_lock_bh(&mirred_list_lock);
  259. list_for_each_entry(m, &mirred_list, tcfm_list) {
  260. if (rcu_access_pointer(m->tcfm_dev) == dev) {
  261. dev_put(dev);
  262. /* Note : no rcu grace period necessary, as
  263. * net_device are already rcu protected.
  264. */
  265. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  266. }
  267. }
  268. spin_unlock_bh(&mirred_list_lock);
  269. }
  270. return NOTIFY_DONE;
  271. }
  272. static struct notifier_block mirred_device_notifier = {
  273. .notifier_call = mirred_device_event,
  274. };
  275. static int tcf_mirred_device(const struct tc_action *a, struct net *net,
  276. struct net_device **mirred_dev)
  277. {
  278. int ifindex = tcf_mirred_ifindex(a);
  279. *mirred_dev = __dev_get_by_index(net, ifindex);
  280. if (!*mirred_dev)
  281. return -EINVAL;
  282. return 0;
  283. }
  284. static struct tc_action_ops act_mirred_ops = {
  285. .kind = "mirred",
  286. .type = TCA_ACT_MIRRED,
  287. .owner = THIS_MODULE,
  288. .act = tcf_mirred,
  289. .stats_update = tcf_stats_update,
  290. .dump = tcf_mirred_dump,
  291. .cleanup = tcf_mirred_release,
  292. .init = tcf_mirred_init,
  293. .walk = tcf_mirred_walker,
  294. .lookup = tcf_mirred_search,
  295. .size = sizeof(struct tcf_mirred),
  296. .get_dev = tcf_mirred_device,
  297. };
  298. static __net_init int mirred_init_net(struct net *net)
  299. {
  300. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  301. return tc_action_net_init(net, tn, &act_mirred_ops);
  302. }
  303. static void __net_exit mirred_exit_net(struct net *net)
  304. {
  305. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  306. tc_action_net_exit(tn);
  307. }
  308. static struct pernet_operations mirred_net_ops = {
  309. .init = mirred_init_net,
  310. .exit = mirred_exit_net,
  311. .id = &mirred_net_id,
  312. .size = sizeof(struct tc_action_net),
  313. };
  314. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  315. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  316. MODULE_LICENSE("GPL");
  317. static int __init mirred_init_module(void)
  318. {
  319. int err = register_netdevice_notifier(&mirred_device_notifier);
  320. if (err)
  321. return err;
  322. pr_info("Mirror/redirect action on\n");
  323. err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  324. if (err)
  325. unregister_netdevice_notifier(&mirred_device_notifier);
  326. return err;
  327. }
  328. static void __exit mirred_cleanup_module(void)
  329. {
  330. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  331. unregister_netdevice_notifier(&mirred_device_notifier);
  332. }
  333. module_init(mirred_init_module);
  334. module_exit(mirred_cleanup_module);