act_mirred.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 bool tcf_mirred_is_act_redirect(int action)
  31. {
  32. return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
  33. }
  34. static bool tcf_mirred_act_wants_ingress(int action)
  35. {
  36. switch (action) {
  37. case TCA_EGRESS_REDIR:
  38. case TCA_EGRESS_MIRROR:
  39. return false;
  40. case TCA_INGRESS_REDIR:
  41. case TCA_INGRESS_MIRROR:
  42. return true;
  43. default:
  44. BUG();
  45. }
  46. }
  47. static void tcf_mirred_release(struct tc_action *a)
  48. {
  49. struct tcf_mirred *m = to_mirred(a);
  50. struct net_device *dev;
  51. list_del(&m->tcfm_list);
  52. dev = rtnl_dereference(m->tcfm_dev);
  53. if (dev)
  54. dev_put(dev);
  55. }
  56. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  57. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  58. };
  59. static unsigned int mirred_net_id;
  60. static struct tc_action_ops act_mirred_ops;
  61. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  62. struct nlattr *est, struct tc_action **a, int ovr,
  63. int bind, struct netlink_ext_ack *extack)
  64. {
  65. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  66. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  67. bool mac_header_xmit = false;
  68. struct tc_mirred *parm;
  69. struct tcf_mirred *m;
  70. struct net_device *dev;
  71. bool exists = false;
  72. int ret;
  73. if (!nla) {
  74. NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
  75. return -EINVAL;
  76. }
  77. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
  78. if (ret < 0)
  79. return ret;
  80. if (!tb[TCA_MIRRED_PARMS]) {
  81. NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
  82. return -EINVAL;
  83. }
  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. NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
  98. return -EINVAL;
  99. }
  100. if (parm->ifindex) {
  101. dev = __dev_get_by_index(net, parm->ifindex);
  102. if (dev == NULL) {
  103. if (exists)
  104. tcf_idr_release(*a, bind);
  105. return -ENODEV;
  106. }
  107. mac_header_xmit = dev_is_mac_header_xmit(dev);
  108. } else {
  109. dev = NULL;
  110. }
  111. if (!exists) {
  112. if (!dev) {
  113. NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
  114. return -EINVAL;
  115. }
  116. ret = tcf_idr_create(tn, parm->index, est, a,
  117. &act_mirred_ops, bind, true);
  118. if (ret)
  119. return ret;
  120. ret = ACT_P_CREATED;
  121. } else {
  122. tcf_idr_release(*a, bind);
  123. if (!ovr)
  124. return -EEXIST;
  125. }
  126. m = to_mirred(*a);
  127. ASSERT_RTNL();
  128. m->tcf_action = parm->action;
  129. m->tcfm_eaction = parm->eaction;
  130. if (dev != NULL) {
  131. if (ret != ACT_P_CREATED)
  132. dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
  133. dev_hold(dev);
  134. rcu_assign_pointer(m->tcfm_dev, dev);
  135. m->tcfm_mac_header_xmit = mac_header_xmit;
  136. }
  137. if (ret == ACT_P_CREATED) {
  138. list_add(&m->tcfm_list, &mirred_list);
  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 net_device *dev = rtnl_dereference(m->tcfm_dev);
  221. struct tc_mirred opt = {
  222. .index = m->tcf_index,
  223. .action = m->tcf_action,
  224. .refcnt = m->tcf_refcnt - ref,
  225. .bindcnt = m->tcf_bindcnt - bind,
  226. .eaction = m->tcfm_eaction,
  227. .ifindex = dev ? dev->ifindex : 0,
  228. };
  229. struct tcf_t t;
  230. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  231. goto nla_put_failure;
  232. tcf_tm_dump(&t, &m->tcf_tm);
  233. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  234. goto nla_put_failure;
  235. return skb->len;
  236. nla_put_failure:
  237. nlmsg_trim(skb, b);
  238. return -1;
  239. }
  240. static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
  241. struct netlink_callback *cb, int type,
  242. const struct tc_action_ops *ops,
  243. struct netlink_ext_ack *extack)
  244. {
  245. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  246. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  247. }
  248. static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index,
  249. struct netlink_ext_ack *extack)
  250. {
  251. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  252. return tcf_idr_search(tn, a, index);
  253. }
  254. static int mirred_device_event(struct notifier_block *unused,
  255. unsigned long event, void *ptr)
  256. {
  257. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  258. struct tcf_mirred *m;
  259. ASSERT_RTNL();
  260. if (event == NETDEV_UNREGISTER) {
  261. list_for_each_entry(m, &mirred_list, tcfm_list) {
  262. if (rcu_access_pointer(m->tcfm_dev) == dev) {
  263. dev_put(dev);
  264. /* Note : no rcu grace period necessary, as
  265. * net_device are already rcu protected.
  266. */
  267. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  268. }
  269. }
  270. }
  271. return NOTIFY_DONE;
  272. }
  273. static struct notifier_block mirred_device_notifier = {
  274. .notifier_call = mirred_device_event,
  275. };
  276. static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
  277. {
  278. struct tcf_mirred *m = to_mirred(a);
  279. return rtnl_dereference(m->tcfm_dev);
  280. }
  281. static struct tc_action_ops act_mirred_ops = {
  282. .kind = "mirred",
  283. .type = TCA_ACT_MIRRED,
  284. .owner = THIS_MODULE,
  285. .act = tcf_mirred,
  286. .stats_update = tcf_stats_update,
  287. .dump = tcf_mirred_dump,
  288. .cleanup = tcf_mirred_release,
  289. .init = tcf_mirred_init,
  290. .walk = tcf_mirred_walker,
  291. .lookup = tcf_mirred_search,
  292. .size = sizeof(struct tcf_mirred),
  293. .get_dev = tcf_mirred_get_dev,
  294. };
  295. static __net_init int mirred_init_net(struct net *net)
  296. {
  297. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  298. return tc_action_net_init(tn, &act_mirred_ops);
  299. }
  300. static void __net_exit mirred_exit_net(struct list_head *net_list)
  301. {
  302. tc_action_net_exit(net_list, mirred_net_id);
  303. }
  304. static struct pernet_operations mirred_net_ops = {
  305. .init = mirred_init_net,
  306. .exit_batch = mirred_exit_net,
  307. .id = &mirred_net_id,
  308. .size = sizeof(struct tc_action_net),
  309. };
  310. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  311. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  312. MODULE_LICENSE("GPL");
  313. static int __init mirred_init_module(void)
  314. {
  315. int err = register_netdevice_notifier(&mirred_device_notifier);
  316. if (err)
  317. return err;
  318. pr_info("Mirror/redirect action on\n");
  319. return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  320. }
  321. static void __exit mirred_cleanup_module(void)
  322. {
  323. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  324. unregister_netdevice_notifier(&mirred_device_notifier);
  325. }
  326. module_init(mirred_init_module);
  327. module_exit(mirred_cleanup_module);