act_mirred.c 9.4 KB

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