act_mirred.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_mirred.c packet mirroring and redirect actions
  4. *
  5. * Authors: Jamal Hadi Salim (2002-4)
  6. *
  7. * TODO: Add ingress support (and socket redirect support)
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/gfp.h>
  18. #include <linux/if_arp.h>
  19. #include <net/net_namespace.h>
  20. #include <net/netlink.h>
  21. #include <net/dst.h>
  22. #include <net/pkt_sched.h>
  23. #include <net/pkt_cls.h>
  24. #include <linux/tc_act/tc_mirred.h>
  25. #include <net/tc_act/tc_mirred.h>
  26. static LIST_HEAD(mirred_list);
  27. static DEFINE_SPINLOCK(mirred_list_lock);
  28. #define MIRRED_RECURSION_LIMIT 4
  29. static DEFINE_PER_CPU(unsigned int, mirred_rec_level);
  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 bool tcf_mirred_can_reinsert(int action)
  48. {
  49. switch (action) {
  50. case TC_ACT_SHOT:
  51. case TC_ACT_STOLEN:
  52. case TC_ACT_QUEUED:
  53. case TC_ACT_TRAP:
  54. return true;
  55. }
  56. return false;
  57. }
  58. static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
  59. {
  60. return rcu_dereference_protected(m->tcfm_dev,
  61. lockdep_is_held(&m->tcf_lock));
  62. }
  63. static void tcf_mirred_release(struct tc_action *a)
  64. {
  65. struct tcf_mirred *m = to_mirred(a);
  66. struct net_device *dev;
  67. spin_lock(&mirred_list_lock);
  68. list_del(&m->tcfm_list);
  69. spin_unlock(&mirred_list_lock);
  70. /* last reference to action, no need to lock */
  71. dev = rcu_dereference_protected(m->tcfm_dev, 1);
  72. if (dev)
  73. dev_put(dev);
  74. }
  75. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  76. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  77. };
  78. static unsigned int mirred_net_id;
  79. static struct tc_action_ops act_mirred_ops;
  80. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  81. struct nlattr *est, struct tc_action **a,
  82. int ovr, int bind, bool rtnl_held,
  83. struct tcf_proto *tp,
  84. u32 flags, struct netlink_ext_ack *extack)
  85. {
  86. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  87. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  88. struct tcf_chain *goto_ch = NULL;
  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_deprecated(tb, TCA_MIRRED_MAX, nla,
  101. mirred_policy, extack);
  102. if (ret < 0)
  103. return ret;
  104. if (!tb[TCA_MIRRED_PARMS]) {
  105. NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
  106. return -EINVAL;
  107. }
  108. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  109. index = parm->index;
  110. err = tcf_idr_check_alloc(tn, &index, a, bind);
  111. if (err < 0)
  112. return err;
  113. exists = err;
  114. if (exists && bind)
  115. return 0;
  116. switch (parm->eaction) {
  117. case TCA_EGRESS_MIRROR:
  118. case TCA_EGRESS_REDIR:
  119. case TCA_INGRESS_REDIR:
  120. case TCA_INGRESS_MIRROR:
  121. break;
  122. default:
  123. if (exists)
  124. tcf_idr_release(*a, bind);
  125. else
  126. tcf_idr_cleanup(tn, index);
  127. NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
  128. return -EINVAL;
  129. }
  130. if (!exists) {
  131. if (!parm->ifindex) {
  132. tcf_idr_cleanup(tn, index);
  133. NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
  134. return -EINVAL;
  135. }
  136. ret = tcf_idr_create_from_flags(tn, index, est, a,
  137. &act_mirred_ops, bind, flags);
  138. if (ret) {
  139. tcf_idr_cleanup(tn, index);
  140. return ret;
  141. }
  142. ret = ACT_P_CREATED;
  143. } else if (!ovr) {
  144. tcf_idr_release(*a, bind);
  145. return -EEXIST;
  146. }
  147. m = to_mirred(*a);
  148. if (ret == ACT_P_CREATED)
  149. INIT_LIST_HEAD(&m->tcfm_list);
  150. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  151. if (err < 0)
  152. goto release_idr;
  153. spin_lock_bh(&m->tcf_lock);
  154. if (parm->ifindex) {
  155. dev = dev_get_by_index(net, parm->ifindex);
  156. if (!dev) {
  157. spin_unlock_bh(&m->tcf_lock);
  158. err = -ENODEV;
  159. goto put_chain;
  160. }
  161. mac_header_xmit = dev_is_mac_header_xmit(dev);
  162. dev = rcu_replace_pointer(m->tcfm_dev, dev,
  163. lockdep_is_held(&m->tcf_lock));
  164. if (dev)
  165. dev_put(dev);
  166. m->tcfm_mac_header_xmit = mac_header_xmit;
  167. }
  168. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  169. m->tcfm_eaction = parm->eaction;
  170. spin_unlock_bh(&m->tcf_lock);
  171. if (goto_ch)
  172. tcf_chain_put_by_act(goto_ch);
  173. if (ret == ACT_P_CREATED) {
  174. spin_lock(&mirred_list_lock);
  175. list_add(&m->tcfm_list, &mirred_list);
  176. spin_unlock(&mirred_list_lock);
  177. }
  178. return ret;
  179. put_chain:
  180. if (goto_ch)
  181. tcf_chain_put_by_act(goto_ch);
  182. release_idr:
  183. tcf_idr_release(*a, bind);
  184. return err;
  185. }
  186. static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
  187. struct tcf_result *res)
  188. {
  189. struct tcf_mirred *m = to_mirred(a);
  190. struct sk_buff *skb2 = skb;
  191. bool m_mac_header_xmit;
  192. struct net_device *dev;
  193. unsigned int rec_level;
  194. int retval, err = 0;
  195. bool use_reinsert;
  196. bool want_ingress;
  197. bool is_redirect;
  198. bool expects_nh;
  199. bool at_ingress;
  200. int m_eaction;
  201. int mac_len;
  202. bool at_nh;
  203. rec_level = __this_cpu_inc_return(mirred_rec_level);
  204. if (unlikely(rec_level > MIRRED_RECURSION_LIMIT)) {
  205. net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
  206. netdev_name(skb->dev));
  207. __this_cpu_dec(mirred_rec_level);
  208. return TC_ACT_SHOT;
  209. }
  210. tcf_lastuse_update(&m->tcf_tm);
  211. tcf_action_update_bstats(&m->common, skb);
  212. m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  213. m_eaction = READ_ONCE(m->tcfm_eaction);
  214. retval = READ_ONCE(m->tcf_action);
  215. dev = rcu_dereference_bh(m->tcfm_dev);
  216. if (unlikely(!dev)) {
  217. pr_notice_once("tc mirred: target device is gone\n");
  218. goto out;
  219. }
  220. if (unlikely(!(dev->flags & IFF_UP))) {
  221. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  222. dev->name);
  223. goto out;
  224. }
  225. /* we could easily avoid the clone only if called by ingress and clsact;
  226. * since we can't easily detect the clsact caller, skip clone only for
  227. * ingress - that covers the TC S/W datapath.
  228. */
  229. is_redirect = tcf_mirred_is_act_redirect(m_eaction);
  230. at_ingress = skb_at_tc_ingress(skb);
  231. use_reinsert = at_ingress && is_redirect &&
  232. tcf_mirred_can_reinsert(retval);
  233. if (!use_reinsert) {
  234. skb2 = skb_clone(skb, GFP_ATOMIC);
  235. if (!skb2)
  236. goto out;
  237. }
  238. want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
  239. /* All mirred/redirected skbs should clear previous ct info */
  240. nf_reset_ct(skb2);
  241. if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
  242. skb_dst_drop(skb2);
  243. expects_nh = want_ingress || !m_mac_header_xmit;
  244. at_nh = skb->data == skb_network_header(skb);
  245. if (at_nh != expects_nh) {
  246. mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
  247. skb_network_header(skb) - skb_mac_header(skb);
  248. if (expects_nh) {
  249. /* target device/action expect data at nh */
  250. skb_pull_rcsum(skb2, mac_len);
  251. } else {
  252. /* target device/action expect data at mac */
  253. skb_push_rcsum(skb2, mac_len);
  254. }
  255. }
  256. skb2->skb_iif = skb->dev->ifindex;
  257. skb2->dev = dev;
  258. /* mirror is always swallowed */
  259. if (is_redirect) {
  260. skb_set_redirected(skb2, skb2->tc_at_ingress);
  261. /* let's the caller reinsert the packet, if possible */
  262. if (use_reinsert) {
  263. res->ingress = want_ingress;
  264. if (skb_tc_reinsert(skb, res))
  265. tcf_action_inc_overlimit_qstats(&m->common);
  266. __this_cpu_dec(mirred_rec_level);
  267. return TC_ACT_CONSUMED;
  268. }
  269. }
  270. if (!want_ingress)
  271. err = dev_queue_xmit(skb2);
  272. else
  273. err = netif_receive_skb(skb2);
  274. if (err) {
  275. out:
  276. tcf_action_inc_overlimit_qstats(&m->common);
  277. if (tcf_mirred_is_act_redirect(m_eaction))
  278. retval = TC_ACT_SHOT;
  279. }
  280. __this_cpu_dec(mirred_rec_level);
  281. return retval;
  282. }
  283. static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  284. u64 drops, u64 lastuse, bool hw)
  285. {
  286. struct tcf_mirred *m = to_mirred(a);
  287. struct tcf_t *tm = &m->tcf_tm;
  288. tcf_action_update_stats(a, bytes, packets, drops, hw);
  289. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  290. }
  291. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  292. int ref)
  293. {
  294. unsigned char *b = skb_tail_pointer(skb);
  295. struct tcf_mirred *m = to_mirred(a);
  296. struct tc_mirred opt = {
  297. .index = m->tcf_index,
  298. .refcnt = refcount_read(&m->tcf_refcnt) - ref,
  299. .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
  300. };
  301. struct net_device *dev;
  302. struct tcf_t t;
  303. spin_lock_bh(&m->tcf_lock);
  304. opt.action = m->tcf_action;
  305. opt.eaction = m->tcfm_eaction;
  306. dev = tcf_mirred_dev_dereference(m);
  307. if (dev)
  308. opt.ifindex = dev->ifindex;
  309. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  310. goto nla_put_failure;
  311. tcf_tm_dump(&t, &m->tcf_tm);
  312. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  313. goto nla_put_failure;
  314. spin_unlock_bh(&m->tcf_lock);
  315. return skb->len;
  316. nla_put_failure:
  317. spin_unlock_bh(&m->tcf_lock);
  318. nlmsg_trim(skb, b);
  319. return -1;
  320. }
  321. static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
  322. struct netlink_callback *cb, int type,
  323. const struct tc_action_ops *ops,
  324. struct netlink_ext_ack *extack)
  325. {
  326. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  327. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  328. }
  329. static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
  330. {
  331. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  332. return tcf_idr_search(tn, a, index);
  333. }
  334. static int mirred_device_event(struct notifier_block *unused,
  335. unsigned long event, void *ptr)
  336. {
  337. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  338. struct tcf_mirred *m;
  339. ASSERT_RTNL();
  340. if (event == NETDEV_UNREGISTER) {
  341. spin_lock(&mirred_list_lock);
  342. list_for_each_entry(m, &mirred_list, tcfm_list) {
  343. spin_lock_bh(&m->tcf_lock);
  344. if (tcf_mirred_dev_dereference(m) == dev) {
  345. dev_put(dev);
  346. /* Note : no rcu grace period necessary, as
  347. * net_device are already rcu protected.
  348. */
  349. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  350. }
  351. spin_unlock_bh(&m->tcf_lock);
  352. }
  353. spin_unlock(&mirred_list_lock);
  354. }
  355. return NOTIFY_DONE;
  356. }
  357. static struct notifier_block mirred_device_notifier = {
  358. .notifier_call = mirred_device_event,
  359. };
  360. static void tcf_mirred_dev_put(void *priv)
  361. {
  362. struct net_device *dev = priv;
  363. dev_put(dev);
  364. }
  365. static struct net_device *
  366. tcf_mirred_get_dev(const struct tc_action *a,
  367. tc_action_priv_destructor *destructor)
  368. {
  369. struct tcf_mirred *m = to_mirred(a);
  370. struct net_device *dev;
  371. rcu_read_lock();
  372. dev = rcu_dereference(m->tcfm_dev);
  373. if (dev) {
  374. dev_hold(dev);
  375. *destructor = tcf_mirred_dev_put;
  376. }
  377. rcu_read_unlock();
  378. return dev;
  379. }
  380. static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
  381. {
  382. return nla_total_size(sizeof(struct tc_mirred));
  383. }
  384. static struct tc_action_ops act_mirred_ops = {
  385. .kind = "mirred",
  386. .id = TCA_ID_MIRRED,
  387. .owner = THIS_MODULE,
  388. .act = tcf_mirred_act,
  389. .stats_update = tcf_stats_update,
  390. .dump = tcf_mirred_dump,
  391. .cleanup = tcf_mirred_release,
  392. .init = tcf_mirred_init,
  393. .walk = tcf_mirred_walker,
  394. .lookup = tcf_mirred_search,
  395. .get_fill_size = tcf_mirred_get_fill_size,
  396. .size = sizeof(struct tcf_mirred),
  397. .get_dev = tcf_mirred_get_dev,
  398. };
  399. static __net_init int mirred_init_net(struct net *net)
  400. {
  401. struct tc_action_net *tn = net_generic(net, mirred_net_id);
  402. return tc_action_net_init(net, tn, &act_mirred_ops);
  403. }
  404. static void __net_exit mirred_exit_net(struct list_head *net_list)
  405. {
  406. tc_action_net_exit(net_list, mirred_net_id);
  407. }
  408. static struct pernet_operations mirred_net_ops = {
  409. .init = mirred_init_net,
  410. .exit_batch = mirred_exit_net,
  411. .id = &mirred_net_id,
  412. .size = sizeof(struct tc_action_net),
  413. };
  414. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  415. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  416. MODULE_LICENSE("GPL");
  417. static int __init mirred_init_module(void)
  418. {
  419. int err = register_netdevice_notifier(&mirred_device_notifier);
  420. if (err)
  421. return err;
  422. pr_info("Mirror/redirect action on\n");
  423. err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  424. if (err)
  425. unregister_netdevice_notifier(&mirred_device_notifier);
  426. return err;
  427. }
  428. static void __exit mirred_cleanup_module(void)
  429. {
  430. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  431. unregister_netdevice_notifier(&mirred_device_notifier);
  432. }
  433. module_init(mirred_init_module);
  434. module_exit(mirred_cleanup_module);