act_mirred.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <net/pkt_cls.h>
  28. #include <linux/tc_act/tc_mirred.h>
  29. #include <net/tc_act/tc_mirred.h>
  30. static LIST_HEAD(mirred_list);
  31. static DEFINE_SPINLOCK(mirred_list_lock);
  32. static bool tcf_mirred_is_act_redirect(int action)
  33. {
  34. return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
  35. }
  36. static bool tcf_mirred_act_wants_ingress(int action)
  37. {
  38. switch (action) {
  39. case TCA_EGRESS_REDIR:
  40. case TCA_EGRESS_MIRROR:
  41. return false;
  42. case TCA_INGRESS_REDIR:
  43. case TCA_INGRESS_MIRROR:
  44. return true;
  45. default:
  46. BUG();
  47. }
  48. }
  49. static bool tcf_mirred_can_reinsert(int action)
  50. {
  51. switch (action) {
  52. case TC_ACT_SHOT:
  53. case TC_ACT_STOLEN:
  54. case TC_ACT_QUEUED:
  55. case TC_ACT_TRAP:
  56. return true;
  57. }
  58. return false;
  59. }
  60. static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
  61. {
  62. return rcu_dereference_protected(m->tcfm_dev,
  63. lockdep_is_held(&m->tcf_lock));
  64. }
  65. static void tcf_mirred_release(struct tc_action *a)
  66. {
  67. struct tcf_mirred *m = to_mirred(a);
  68. struct net_device *dev;
  69. spin_lock(&mirred_list_lock);
  70. list_del(&m->tcfm_list);
  71. spin_unlock(&mirred_list_lock);
  72. /* last reference to action, no need to lock */
  73. dev = rcu_dereference_protected(m->tcfm_dev, 1);
  74. if (dev)
  75. dev_put(dev);
  76. }
  77. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  78. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  79. };
  80. static unsigned int mirred_net_id;
  81. static struct tc_action_ops act_mirred_ops;
  82. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  83. struct nlattr *est, struct tc_action **a,
  84. int ovr, int bind, bool rtnl_held,
  85. struct netlink_ext_ack *extack)
  86. {
  87. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  88. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  89. bool mac_header_xmit = false;
  90. struct tc_mirred *parm;
  91. struct tcf_mirred *m;
  92. struct net_device *dev;
  93. bool exists = false;
  94. int ret, err;
  95. u32 index;
  96. if (!nla) {
  97. NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
  98. return -EINVAL;
  99. }
  100. ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
  101. if (ret < 0)
  102. return ret;
  103. if (!tb[TCA_MIRRED_PARMS]) {
  104. NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
  105. return -EINVAL;
  106. }
  107. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  108. index = parm->index;
  109. err = tcf_idr_check_alloc(tn, &index, a, bind);
  110. if (err < 0)
  111. return err;
  112. exists = err;
  113. if (exists && bind)
  114. return 0;
  115. switch (parm->eaction) {
  116. case TCA_EGRESS_MIRROR:
  117. case TCA_EGRESS_REDIR:
  118. case TCA_INGRESS_REDIR:
  119. case TCA_INGRESS_MIRROR:
  120. break;
  121. default:
  122. if (exists)
  123. tcf_idr_release(*a, bind);
  124. else
  125. tcf_idr_cleanup(tn, index);
  126. NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
  127. return -EINVAL;
  128. }
  129. if (!exists) {
  130. if (!parm->ifindex) {
  131. tcf_idr_cleanup(tn, index);
  132. NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
  133. return -EINVAL;
  134. }
  135. ret = tcf_idr_create(tn, index, est, a,
  136. &act_mirred_ops, bind, true);
  137. if (ret) {
  138. tcf_idr_cleanup(tn, index);
  139. return ret;
  140. }
  141. ret = ACT_P_CREATED;
  142. } else if (!ovr) {
  143. tcf_idr_release(*a, bind);
  144. return -EEXIST;
  145. }
  146. m = to_mirred(*a);
  147. if (ret == ACT_P_CREATED)
  148. INIT_LIST_HEAD(&m->tcfm_list);
  149. spin_lock_bh(&m->tcf_lock);
  150. m->tcf_action = parm->action;
  151. m->tcfm_eaction = parm->eaction;
  152. if (parm->ifindex) {
  153. dev = dev_get_by_index(net, parm->ifindex);
  154. if (!dev) {
  155. spin_unlock_bh(&m->tcf_lock);
  156. tcf_idr_release(*a, bind);
  157. return -ENODEV;
  158. }
  159. mac_header_xmit = dev_is_mac_header_xmit(dev);
  160. rcu_swap_protected(m->tcfm_dev, dev,
  161. lockdep_is_held(&m->tcf_lock));
  162. if (dev)
  163. dev_put(dev);
  164. m->tcfm_mac_header_xmit = mac_header_xmit;
  165. }
  166. spin_unlock_bh(&m->tcf_lock);
  167. if (ret == ACT_P_CREATED) {
  168. spin_lock(&mirred_list_lock);
  169. list_add(&m->tcfm_list, &mirred_list);
  170. spin_unlock(&mirred_list_lock);
  171. tcf_idr_insert(tn, *a);
  172. }
  173. return ret;
  174. }
  175. static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
  176. struct tcf_result *res)
  177. {
  178. struct tcf_mirred *m = to_mirred(a);
  179. struct sk_buff *skb2 = skb;
  180. bool m_mac_header_xmit;
  181. struct net_device *dev;
  182. int retval, err = 0;
  183. bool use_reinsert;
  184. bool want_ingress;
  185. bool is_redirect;
  186. int m_eaction;
  187. int mac_len;
  188. tcf_lastuse_update(&m->tcf_tm);
  189. bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
  190. m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  191. m_eaction = READ_ONCE(m->tcfm_eaction);
  192. retval = READ_ONCE(m->tcf_action);
  193. dev = rcu_dereference_bh(m->tcfm_dev);
  194. if (unlikely(!dev)) {
  195. pr_notice_once("tc mirred: target device is gone\n");
  196. goto out;
  197. }
  198. if (unlikely(!(dev->flags & IFF_UP))) {
  199. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  200. dev->name);
  201. goto out;
  202. }
  203. /* we could easily avoid the clone only if called by ingress and clsact;
  204. * since we can't easily detect the clsact caller, skip clone only for
  205. * ingress - that covers the TC S/W datapath.
  206. */
  207. is_redirect = tcf_mirred_is_act_redirect(m_eaction);
  208. use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
  209. tcf_mirred_can_reinsert(retval);
  210. if (!use_reinsert) {
  211. skb2 = skb_clone(skb, GFP_ATOMIC);
  212. if (!skb2)
  213. goto out;
  214. }
  215. /* If action's target direction differs than filter's direction,
  216. * and devices expect a mac header on xmit, then mac push/pull is
  217. * needed.
  218. */
  219. want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
  220. if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
  221. if (!skb_at_tc_ingress(skb)) {
  222. /* caught at egress, act ingress: pull mac */
  223. mac_len = skb_network_header(skb) - skb_mac_header(skb);
  224. skb_pull_rcsum(skb2, mac_len);
  225. } else {
  226. /* caught at ingress, act egress: push mac */
  227. skb_push_rcsum(skb2, skb->mac_len);
  228. }
  229. }
  230. skb2->skb_iif = skb->dev->ifindex;
  231. skb2->dev = dev;
  232. /* mirror is always swallowed */
  233. if (is_redirect) {
  234. skb2->tc_redirected = 1;
  235. skb2->tc_from_ingress = skb2->tc_at_ingress;
  236. /* let's the caller reinsert the packet, if possible */
  237. if (use_reinsert) {
  238. res->ingress = want_ingress;
  239. res->qstats = this_cpu_ptr(m->common.cpu_qstats);
  240. return TC_ACT_REINSERT;
  241. }
  242. }
  243. if (!want_ingress)
  244. err = dev_queue_xmit(skb2);
  245. else
  246. err = netif_receive_skb(skb2);
  247. if (err) {
  248. out:
  249. qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
  250. if (tcf_mirred_is_act_redirect(m_eaction))
  251. retval = TC_ACT_SHOT;
  252. }
  253. return retval;
  254. }
  255. static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
  256. u64 lastuse)
  257. {
  258. struct tcf_mirred *m = to_mirred(a);
  259. struct tcf_t *tm = &m->tcf_tm;
  260. _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
  261. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  262. }
  263. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  264. int ref)
  265. {
  266. unsigned char *b = skb_tail_pointer(skb);
  267. struct tcf_mirred *m = to_mirred(a);
  268. struct tc_mirred opt = {
  269. .index = m->tcf_index,
  270. .refcnt = refcount_read(&m->tcf_refcnt) - ref,
  271. .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
  272. };
  273. struct net_device *dev;
  274. struct tcf_t t;
  275. spin_lock_bh(&m->tcf_lock);
  276. opt.action = m->tcf_action;
  277. opt.eaction = m->tcfm_eaction;
  278. dev = tcf_mirred_dev_dereference(m);
  279. if (dev)
  280. opt.ifindex = dev->ifindex;
  281. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  282. goto nla_put_failure;
  283. tcf_tm_dump(&t, &m->tcf_tm);
  284. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  285. goto nla_put_failure;
  286. spin_unlock_bh(&m->tcf_lock);
  287. return skb->len;
  288. nla_put_failure:
  289. spin_unlock_bh(&m->tcf_lock);
  290. nlmsg_trim(skb, b);
  291. return -1;
  292. }
  293. static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
  294. struct netlink_callback *cb, int type,
  295. const struct tc_action_ops *ops,
  296. struct netlink_ext_ack *extack)
  297. {
  298. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  299. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  300. }
  301. static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index,
  302. struct netlink_ext_ack *extack)
  303. {
  304. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  305. return tcf_idr_search(tn, a, index);
  306. }
  307. static int mirred_device_event(struct notifier_block *unused,
  308. unsigned long event, void *ptr)
  309. {
  310. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  311. struct tcf_mirred *m;
  312. ASSERT_RTNL();
  313. if (event == NETDEV_UNREGISTER) {
  314. spin_lock(&mirred_list_lock);
  315. list_for_each_entry(m, &mirred_list, tcfm_list) {
  316. spin_lock_bh(&m->tcf_lock);
  317. if (tcf_mirred_dev_dereference(m) == dev) {
  318. dev_put(dev);
  319. /* Note : no rcu grace period necessary, as
  320. * net_device are already rcu protected.
  321. */
  322. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  323. }
  324. spin_unlock_bh(&m->tcf_lock);
  325. }
  326. spin_unlock(&mirred_list_lock);
  327. }
  328. return NOTIFY_DONE;
  329. }
  330. static struct notifier_block mirred_device_notifier = {
  331. .notifier_call = mirred_device_event,
  332. };
  333. static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
  334. {
  335. struct tcf_mirred *m = to_mirred(a);
  336. struct net_device *dev;
  337. rcu_read_lock();
  338. dev = rcu_dereference(m->tcfm_dev);
  339. if (dev)
  340. dev_hold(dev);
  341. rcu_read_unlock();
  342. return dev;
  343. }
  344. static void tcf_mirred_put_dev(struct net_device *dev)
  345. {
  346. dev_put(dev);
  347. }
  348. static struct tc_action_ops act_mirred_ops = {
  349. .kind = "mirred",
  350. .type = TCA_ACT_MIRRED,
  351. .owner = THIS_MODULE,
  352. .act = tcf_mirred_act,
  353. .stats_update = tcf_stats_update,
  354. .dump = tcf_mirred_dump,
  355. .cleanup = tcf_mirred_release,
  356. .init = tcf_mirred_init,
  357. .walk = tcf_mirred_walker,
  358. .lookup = tcf_mirred_search,
  359. .size = sizeof(struct tcf_mirred),
  360. .get_dev = tcf_mirred_get_dev,
  361. .put_dev = tcf_mirred_put_dev,
  362. };
  363. static __net_init int mirred_init_net(struct net *net)
  364. {
  365. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  366. return tc_action_net_init(net, tn, &act_mirred_ops);
  367. }
  368. static void __net_exit mirred_exit_net(struct list_head *net_list)
  369. {
  370. tc_action_net_exit(net_list, mirred_net_id);
  371. }
  372. static struct pernet_operations mirred_net_ops = {
  373. .init = mirred_init_net,
  374. .exit_batch = mirred_exit_net,
  375. .id = &mirred_net_id,
  376. .size = sizeof(struct tc_action_net),
  377. };
  378. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  379. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  380. MODULE_LICENSE("GPL");
  381. static int __init mirred_init_module(void)
  382. {
  383. int err = register_netdevice_notifier(&mirred_device_notifier);
  384. if (err)
  385. return err;
  386. pr_info("Mirror/redirect action on\n");
  387. err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  388. if (err)
  389. unregister_netdevice_notifier(&mirred_device_notifier);
  390. return err;
  391. }
  392. static void __exit mirred_cleanup_module(void)
  393. {
  394. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  395. unregister_netdevice_notifier(&mirred_device_notifier);
  396. }
  397. module_init(mirred_init_module);
  398. module_exit(mirred_cleanup_module);