act_mirred.c 11 KB

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