act_mirred.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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_NEST_LIMIT 4
  29. static DEFINE_PER_CPU(unsigned int, mirred_nest_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. netdev_put(dev, &m->tcfm_dev_tracker);
  73. }
  74. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  75. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  76. };
  77. static struct tc_action_ops act_mirred_ops;
  78. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  79. struct nlattr *est, struct tc_action **a,
  80. struct tcf_proto *tp,
  81. u32 flags, struct netlink_ext_ack *extack)
  82. {
  83. struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
  84. bool bind = flags & TCA_ACT_FLAGS_BIND;
  85. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  86. struct tcf_chain *goto_ch = NULL;
  87. bool mac_header_xmit = false;
  88. struct tc_mirred *parm;
  89. struct tcf_mirred *m;
  90. bool exists = false;
  91. int ret, err;
  92. u32 index;
  93. if (!nla) {
  94. NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
  95. return -EINVAL;
  96. }
  97. ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
  98. mirred_policy, extack);
  99. if (ret < 0)
  100. return ret;
  101. if (!tb[TCA_MIRRED_PARMS]) {
  102. NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
  103. return -EINVAL;
  104. }
  105. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  106. index = parm->index;
  107. err = tcf_idr_check_alloc(tn, &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, 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, index);
  130. NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
  131. return -EINVAL;
  132. }
  133. ret = tcf_idr_create_from_flags(tn, index, est, a,
  134. &act_mirred_ops, bind, flags);
  135. if (ret) {
  136. tcf_idr_cleanup(tn, index);
  137. return ret;
  138. }
  139. ret = ACT_P_CREATED;
  140. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  141. tcf_idr_release(*a, bind);
  142. return -EEXIST;
  143. }
  144. m = to_mirred(*a);
  145. if (ret == ACT_P_CREATED)
  146. INIT_LIST_HEAD(&m->tcfm_list);
  147. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  148. if (err < 0)
  149. goto release_idr;
  150. spin_lock_bh(&m->tcf_lock);
  151. if (parm->ifindex) {
  152. struct net_device *odev, *ndev;
  153. ndev = dev_get_by_index(net, parm->ifindex);
  154. if (!ndev) {
  155. spin_unlock_bh(&m->tcf_lock);
  156. err = -ENODEV;
  157. goto put_chain;
  158. }
  159. mac_header_xmit = dev_is_mac_header_xmit(ndev);
  160. odev = rcu_replace_pointer(m->tcfm_dev, ndev,
  161. lockdep_is_held(&m->tcf_lock));
  162. netdev_put(odev, &m->tcfm_dev_tracker);
  163. netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
  164. m->tcfm_mac_header_xmit = mac_header_xmit;
  165. }
  166. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  167. m->tcfm_eaction = parm->eaction;
  168. spin_unlock_bh(&m->tcf_lock);
  169. if (goto_ch)
  170. tcf_chain_put_by_act(goto_ch);
  171. if (ret == ACT_P_CREATED) {
  172. spin_lock(&mirred_list_lock);
  173. list_add(&m->tcfm_list, &mirred_list);
  174. spin_unlock(&mirred_list_lock);
  175. }
  176. return ret;
  177. put_chain:
  178. if (goto_ch)
  179. tcf_chain_put_by_act(goto_ch);
  180. release_idr:
  181. tcf_idr_release(*a, bind);
  182. return err;
  183. }
  184. static bool is_mirred_nested(void)
  185. {
  186. return unlikely(__this_cpu_read(mirred_nest_level) > 1);
  187. }
  188. static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
  189. {
  190. int err;
  191. if (!want_ingress)
  192. err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
  193. else if (is_mirred_nested())
  194. err = netif_rx(skb);
  195. else
  196. err = netif_receive_skb(skb);
  197. return err;
  198. }
  199. static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
  200. struct tcf_result *res)
  201. {
  202. struct tcf_mirred *m = to_mirred(a);
  203. struct sk_buff *skb2 = skb;
  204. bool m_mac_header_xmit;
  205. struct net_device *dev;
  206. unsigned int nest_level;
  207. int retval, err = 0;
  208. bool use_reinsert;
  209. bool want_ingress;
  210. bool is_redirect;
  211. bool expects_nh;
  212. bool at_ingress;
  213. int m_eaction;
  214. int mac_len;
  215. bool at_nh;
  216. nest_level = __this_cpu_inc_return(mirred_nest_level);
  217. if (unlikely(nest_level > MIRRED_NEST_LIMIT)) {
  218. net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
  219. netdev_name(skb->dev));
  220. __this_cpu_dec(mirred_nest_level);
  221. return TC_ACT_SHOT;
  222. }
  223. tcf_lastuse_update(&m->tcf_tm);
  224. tcf_action_update_bstats(&m->common, skb);
  225. m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  226. m_eaction = READ_ONCE(m->tcfm_eaction);
  227. retval = READ_ONCE(m->tcf_action);
  228. dev = rcu_dereference_bh(m->tcfm_dev);
  229. if (unlikely(!dev)) {
  230. pr_notice_once("tc mirred: target device is gone\n");
  231. goto out;
  232. }
  233. if (unlikely(!(dev->flags & IFF_UP)) || !netif_carrier_ok(dev)) {
  234. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  235. dev->name);
  236. goto out;
  237. }
  238. /* we could easily avoid the clone only if called by ingress and clsact;
  239. * since we can't easily detect the clsact caller, skip clone only for
  240. * ingress - that covers the TC S/W datapath.
  241. */
  242. is_redirect = tcf_mirred_is_act_redirect(m_eaction);
  243. at_ingress = skb_at_tc_ingress(skb);
  244. use_reinsert = at_ingress && is_redirect &&
  245. tcf_mirred_can_reinsert(retval);
  246. if (!use_reinsert) {
  247. skb2 = skb_clone(skb, GFP_ATOMIC);
  248. if (!skb2)
  249. goto out;
  250. }
  251. want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
  252. /* All mirred/redirected skbs should clear previous ct info */
  253. nf_reset_ct(skb2);
  254. if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
  255. skb_dst_drop(skb2);
  256. expects_nh = want_ingress || !m_mac_header_xmit;
  257. at_nh = skb->data == skb_network_header(skb);
  258. if (at_nh != expects_nh) {
  259. mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
  260. skb_network_header(skb) - skb_mac_header(skb);
  261. if (expects_nh) {
  262. /* target device/action expect data at nh */
  263. skb_pull_rcsum(skb2, mac_len);
  264. } else {
  265. /* target device/action expect data at mac */
  266. skb_push_rcsum(skb2, mac_len);
  267. }
  268. }
  269. skb2->skb_iif = skb->dev->ifindex;
  270. skb2->dev = dev;
  271. /* mirror is always swallowed */
  272. if (is_redirect) {
  273. skb_set_redirected(skb2, skb2->tc_at_ingress);
  274. /* let's the caller reinsert the packet, if possible */
  275. if (use_reinsert) {
  276. err = tcf_mirred_forward(want_ingress, skb);
  277. if (err)
  278. tcf_action_inc_overlimit_qstats(&m->common);
  279. __this_cpu_dec(mirred_nest_level);
  280. return TC_ACT_CONSUMED;
  281. }
  282. }
  283. err = tcf_mirred_forward(want_ingress, skb2);
  284. if (err) {
  285. out:
  286. tcf_action_inc_overlimit_qstats(&m->common);
  287. if (tcf_mirred_is_act_redirect(m_eaction))
  288. retval = TC_ACT_SHOT;
  289. }
  290. __this_cpu_dec(mirred_nest_level);
  291. return retval;
  292. }
  293. static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  294. u64 drops, u64 lastuse, bool hw)
  295. {
  296. struct tcf_mirred *m = to_mirred(a);
  297. struct tcf_t *tm = &m->tcf_tm;
  298. tcf_action_update_stats(a, bytes, packets, drops, hw);
  299. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  300. }
  301. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  302. int ref)
  303. {
  304. unsigned char *b = skb_tail_pointer(skb);
  305. struct tcf_mirred *m = to_mirred(a);
  306. struct tc_mirred opt = {
  307. .index = m->tcf_index,
  308. .refcnt = refcount_read(&m->tcf_refcnt) - ref,
  309. .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
  310. };
  311. struct net_device *dev;
  312. struct tcf_t t;
  313. spin_lock_bh(&m->tcf_lock);
  314. opt.action = m->tcf_action;
  315. opt.eaction = m->tcfm_eaction;
  316. dev = tcf_mirred_dev_dereference(m);
  317. if (dev)
  318. opt.ifindex = dev->ifindex;
  319. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  320. goto nla_put_failure;
  321. tcf_tm_dump(&t, &m->tcf_tm);
  322. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  323. goto nla_put_failure;
  324. spin_unlock_bh(&m->tcf_lock);
  325. return skb->len;
  326. nla_put_failure:
  327. spin_unlock_bh(&m->tcf_lock);
  328. nlmsg_trim(skb, b);
  329. return -1;
  330. }
  331. static int mirred_device_event(struct notifier_block *unused,
  332. unsigned long event, void *ptr)
  333. {
  334. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  335. struct tcf_mirred *m;
  336. ASSERT_RTNL();
  337. if (event == NETDEV_UNREGISTER) {
  338. spin_lock(&mirred_list_lock);
  339. list_for_each_entry(m, &mirred_list, tcfm_list) {
  340. spin_lock_bh(&m->tcf_lock);
  341. if (tcf_mirred_dev_dereference(m) == dev) {
  342. netdev_put(dev, &m->tcfm_dev_tracker);
  343. /* Note : no rcu grace period necessary, as
  344. * net_device are already rcu protected.
  345. */
  346. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  347. }
  348. spin_unlock_bh(&m->tcf_lock);
  349. }
  350. spin_unlock(&mirred_list_lock);
  351. }
  352. return NOTIFY_DONE;
  353. }
  354. static struct notifier_block mirred_device_notifier = {
  355. .notifier_call = mirred_device_event,
  356. };
  357. static void tcf_mirred_dev_put(void *priv)
  358. {
  359. struct net_device *dev = priv;
  360. dev_put(dev);
  361. }
  362. static struct net_device *
  363. tcf_mirred_get_dev(const struct tc_action *a,
  364. tc_action_priv_destructor *destructor)
  365. {
  366. struct tcf_mirred *m = to_mirred(a);
  367. struct net_device *dev;
  368. rcu_read_lock();
  369. dev = rcu_dereference(m->tcfm_dev);
  370. if (dev) {
  371. dev_hold(dev);
  372. *destructor = tcf_mirred_dev_put;
  373. }
  374. rcu_read_unlock();
  375. return dev;
  376. }
  377. static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
  378. {
  379. return nla_total_size(sizeof(struct tc_mirred));
  380. }
  381. static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
  382. const struct tc_action *act)
  383. {
  384. entry->dev = act->ops->get_dev(act, &entry->destructor);
  385. if (!entry->dev)
  386. return;
  387. entry->destructor_priv = entry->dev;
  388. }
  389. static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data,
  390. u32 *index_inc, bool bind,
  391. struct netlink_ext_ack *extack)
  392. {
  393. if (bind) {
  394. struct flow_action_entry *entry = entry_data;
  395. if (is_tcf_mirred_egress_redirect(act)) {
  396. entry->id = FLOW_ACTION_REDIRECT;
  397. tcf_offload_mirred_get_dev(entry, act);
  398. } else if (is_tcf_mirred_egress_mirror(act)) {
  399. entry->id = FLOW_ACTION_MIRRED;
  400. tcf_offload_mirred_get_dev(entry, act);
  401. } else if (is_tcf_mirred_ingress_redirect(act)) {
  402. entry->id = FLOW_ACTION_REDIRECT_INGRESS;
  403. tcf_offload_mirred_get_dev(entry, act);
  404. } else if (is_tcf_mirred_ingress_mirror(act)) {
  405. entry->id = FLOW_ACTION_MIRRED_INGRESS;
  406. tcf_offload_mirred_get_dev(entry, act);
  407. } else {
  408. NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
  409. return -EOPNOTSUPP;
  410. }
  411. *index_inc = 1;
  412. } else {
  413. struct flow_offload_action *fl_action = entry_data;
  414. if (is_tcf_mirred_egress_redirect(act))
  415. fl_action->id = FLOW_ACTION_REDIRECT;
  416. else if (is_tcf_mirred_egress_mirror(act))
  417. fl_action->id = FLOW_ACTION_MIRRED;
  418. else if (is_tcf_mirred_ingress_redirect(act))
  419. fl_action->id = FLOW_ACTION_REDIRECT_INGRESS;
  420. else if (is_tcf_mirred_ingress_mirror(act))
  421. fl_action->id = FLOW_ACTION_MIRRED_INGRESS;
  422. else
  423. return -EOPNOTSUPP;
  424. }
  425. return 0;
  426. }
  427. static struct tc_action_ops act_mirred_ops = {
  428. .kind = "mirred",
  429. .id = TCA_ID_MIRRED,
  430. .owner = THIS_MODULE,
  431. .act = tcf_mirred_act,
  432. .stats_update = tcf_stats_update,
  433. .dump = tcf_mirred_dump,
  434. .cleanup = tcf_mirred_release,
  435. .init = tcf_mirred_init,
  436. .get_fill_size = tcf_mirred_get_fill_size,
  437. .offload_act_setup = tcf_mirred_offload_act_setup,
  438. .size = sizeof(struct tcf_mirred),
  439. .get_dev = tcf_mirred_get_dev,
  440. };
  441. static __net_init int mirred_init_net(struct net *net)
  442. {
  443. struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
  444. return tc_action_net_init(net, tn, &act_mirred_ops);
  445. }
  446. static void __net_exit mirred_exit_net(struct list_head *net_list)
  447. {
  448. tc_action_net_exit(net_list, act_mirred_ops.net_id);
  449. }
  450. static struct pernet_operations mirred_net_ops = {
  451. .init = mirred_init_net,
  452. .exit_batch = mirred_exit_net,
  453. .id = &act_mirred_ops.net_id,
  454. .size = sizeof(struct tc_action_net),
  455. };
  456. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  457. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  458. MODULE_LICENSE("GPL");
  459. static int __init mirred_init_module(void)
  460. {
  461. int err = register_netdevice_notifier(&mirred_device_notifier);
  462. if (err)
  463. return err;
  464. pr_info("Mirror/redirect action on\n");
  465. err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  466. if (err)
  467. unregister_netdevice_notifier(&mirred_device_notifier);
  468. return err;
  469. }
  470. static void __exit mirred_cleanup_module(void)
  471. {
  472. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  473. unregister_netdevice_notifier(&mirred_device_notifier);
  474. }
  475. module_init(mirred_init_module);
  476. module_exit(mirred_cleanup_module);