act_mirred.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. #include <net/tc_wrapper.h>
  27. static LIST_HEAD(mirred_list);
  28. static DEFINE_SPINLOCK(mirred_list_lock);
  29. static bool tcf_mirred_is_act_redirect(int action)
  30. {
  31. return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
  32. }
  33. static bool tcf_mirred_act_wants_ingress(int action)
  34. {
  35. switch (action) {
  36. case TCA_EGRESS_REDIR:
  37. case TCA_EGRESS_MIRROR:
  38. return false;
  39. case TCA_INGRESS_REDIR:
  40. case TCA_INGRESS_MIRROR:
  41. return true;
  42. default:
  43. BUG();
  44. }
  45. }
  46. static bool tcf_mirred_can_reinsert(int action)
  47. {
  48. switch (action) {
  49. case TC_ACT_SHOT:
  50. case TC_ACT_STOLEN:
  51. case TC_ACT_QUEUED:
  52. case TC_ACT_TRAP:
  53. return true;
  54. }
  55. return false;
  56. }
  57. static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
  58. {
  59. return rcu_dereference_protected(m->tcfm_dev,
  60. lockdep_is_held(&m->tcf_lock));
  61. }
  62. static void tcf_mirred_release(struct tc_action *a)
  63. {
  64. struct tcf_mirred *m = to_mirred(a);
  65. struct net_device *dev;
  66. spin_lock(&mirred_list_lock);
  67. list_del(&m->tcfm_list);
  68. spin_unlock(&mirred_list_lock);
  69. /* last reference to action, no need to lock */
  70. dev = rcu_dereference_protected(m->tcfm_dev, 1);
  71. netdev_put(dev, &m->tcfm_dev_tracker);
  72. }
  73. static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
  74. [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
  75. [TCA_MIRRED_BLOCKID] = NLA_POLICY_MIN(NLA_U32, 1),
  76. };
  77. static struct tc_action_ops act_mirred_ops;
  78. static void tcf_mirred_replace_dev(struct tcf_mirred *m,
  79. struct net_device *ndev)
  80. {
  81. struct net_device *odev;
  82. odev = rcu_replace_pointer(m->tcfm_dev, ndev,
  83. lockdep_is_held(&m->tcf_lock));
  84. netdev_put(odev, &m->tcfm_dev_tracker);
  85. }
  86. static int tcf_mirred_init(struct net *net, struct nlattr *nla,
  87. struct nlattr *est, struct tc_action **a,
  88. struct tcf_proto *tp,
  89. u32 flags, struct netlink_ext_ack *extack)
  90. {
  91. struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
  92. bool bind = flags & TCA_ACT_FLAGS_BIND;
  93. struct nlattr *tb[TCA_MIRRED_MAX + 1];
  94. struct tcf_chain *goto_ch = NULL;
  95. bool mac_header_xmit = false;
  96. struct tc_mirred *parm;
  97. struct tcf_mirred *m;
  98. bool exists = false;
  99. int ret, err;
  100. u32 index;
  101. if (!nla) {
  102. NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
  103. return -EINVAL;
  104. }
  105. ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
  106. mirred_policy, extack);
  107. if (ret < 0)
  108. return ret;
  109. if (!tb[TCA_MIRRED_PARMS]) {
  110. NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
  111. return -EINVAL;
  112. }
  113. parm = nla_data(tb[TCA_MIRRED_PARMS]);
  114. index = parm->index;
  115. err = tcf_idr_check_alloc(tn, &index, a, bind);
  116. if (err < 0)
  117. return err;
  118. exists = err;
  119. if (exists && bind)
  120. return ACT_P_BOUND;
  121. if (tb[TCA_MIRRED_BLOCKID] && parm->ifindex) {
  122. NL_SET_ERR_MSG_MOD(extack,
  123. "Cannot specify Block ID and dev simultaneously");
  124. if (exists)
  125. tcf_idr_release(*a, bind);
  126. else
  127. tcf_idr_cleanup(tn, index);
  128. return -EINVAL;
  129. }
  130. switch (parm->eaction) {
  131. case TCA_EGRESS_MIRROR:
  132. case TCA_EGRESS_REDIR:
  133. case TCA_INGRESS_REDIR:
  134. case TCA_INGRESS_MIRROR:
  135. break;
  136. default:
  137. if (exists)
  138. tcf_idr_release(*a, bind);
  139. else
  140. tcf_idr_cleanup(tn, index);
  141. NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
  142. return -EINVAL;
  143. }
  144. if (!exists) {
  145. if (!parm->ifindex && !tb[TCA_MIRRED_BLOCKID]) {
  146. tcf_idr_cleanup(tn, index);
  147. NL_SET_ERR_MSG_MOD(extack,
  148. "Must specify device or block");
  149. return -EINVAL;
  150. }
  151. ret = tcf_idr_create_from_flags(tn, index, est, a,
  152. &act_mirred_ops, bind, flags);
  153. if (ret) {
  154. tcf_idr_cleanup(tn, index);
  155. return ret;
  156. }
  157. ret = ACT_P_CREATED;
  158. } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  159. tcf_idr_release(*a, bind);
  160. return -EEXIST;
  161. }
  162. m = to_mirred(*a);
  163. if (ret == ACT_P_CREATED)
  164. INIT_LIST_HEAD(&m->tcfm_list);
  165. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  166. if (err < 0)
  167. goto release_idr;
  168. spin_lock_bh(&m->tcf_lock);
  169. if (parm->ifindex) {
  170. struct net_device *ndev;
  171. ndev = dev_get_by_index(net, parm->ifindex);
  172. if (!ndev) {
  173. spin_unlock_bh(&m->tcf_lock);
  174. err = -ENODEV;
  175. goto put_chain;
  176. }
  177. mac_header_xmit = dev_is_mac_header_xmit(ndev);
  178. tcf_mirred_replace_dev(m, ndev);
  179. netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
  180. m->tcfm_mac_header_xmit = mac_header_xmit;
  181. m->tcfm_blockid = 0;
  182. } else if (tb[TCA_MIRRED_BLOCKID]) {
  183. tcf_mirred_replace_dev(m, NULL);
  184. m->tcfm_mac_header_xmit = false;
  185. m->tcfm_blockid = nla_get_u32(tb[TCA_MIRRED_BLOCKID]);
  186. }
  187. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  188. m->tcfm_eaction = parm->eaction;
  189. spin_unlock_bh(&m->tcf_lock);
  190. if (goto_ch)
  191. tcf_chain_put_by_act(goto_ch);
  192. if (ret == ACT_P_CREATED) {
  193. spin_lock(&mirred_list_lock);
  194. list_add(&m->tcfm_list, &mirred_list);
  195. spin_unlock(&mirred_list_lock);
  196. }
  197. return ret;
  198. put_chain:
  199. if (goto_ch)
  200. tcf_chain_put_by_act(goto_ch);
  201. release_idr:
  202. tcf_idr_release(*a, bind);
  203. return err;
  204. }
  205. static int
  206. tcf_mirred_forward(bool at_ingress, bool want_ingress, struct sk_buff *skb)
  207. {
  208. int err;
  209. if (!want_ingress)
  210. err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
  211. else if (!at_ingress)
  212. err = netif_rx(skb);
  213. else
  214. err = netif_receive_skb(skb);
  215. return err;
  216. }
  217. static int tcf_mirred_to_dev(struct sk_buff *skb, struct tcf_mirred *m,
  218. struct net_device *dev,
  219. const bool m_mac_header_xmit, int m_eaction,
  220. int retval)
  221. {
  222. struct sk_buff *skb_to_send = skb;
  223. bool want_ingress;
  224. bool is_redirect;
  225. bool expects_nh;
  226. bool at_ingress;
  227. bool dont_clone;
  228. int mac_len;
  229. bool at_nh;
  230. int err;
  231. is_redirect = tcf_mirred_is_act_redirect(m_eaction);
  232. if (unlikely(!(dev->flags & IFF_UP)) || !netif_carrier_ok(dev)) {
  233. net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
  234. dev->name);
  235. goto err_cant_do;
  236. }
  237. want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
  238. at_ingress = skb_at_tc_ingress(skb);
  239. if (dev == skb->dev && want_ingress == at_ingress) {
  240. pr_notice_once("tc mirred: Loop (%s:%s --> %s:%s)\n",
  241. netdev_name(skb->dev),
  242. at_ingress ? "ingress" : "egress",
  243. netdev_name(dev),
  244. want_ingress ? "ingress" : "egress");
  245. goto err_cant_do;
  246. }
  247. /* we could easily avoid the clone only if called by ingress and clsact;
  248. * since we can't easily detect the clsact caller, skip clone only for
  249. * ingress - that covers the TC S/W datapath.
  250. */
  251. dont_clone = skb_at_tc_ingress(skb) && is_redirect &&
  252. tcf_mirred_can_reinsert(retval);
  253. if (!dont_clone) {
  254. skb_to_send = skb_clone(skb, GFP_ATOMIC);
  255. if (!skb_to_send)
  256. goto err_cant_do;
  257. }
  258. /* All mirred/redirected skbs should clear previous ct info */
  259. nf_reset_ct(skb_to_send);
  260. if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
  261. skb_dst_drop(skb_to_send);
  262. expects_nh = want_ingress || !m_mac_header_xmit;
  263. at_nh = skb->data == skb_network_header(skb);
  264. if (at_nh != expects_nh) {
  265. mac_len = at_ingress ? skb->mac_len :
  266. skb_network_offset(skb);
  267. if (expects_nh) {
  268. /* target device/action expect data at nh */
  269. skb_pull_rcsum(skb_to_send, mac_len);
  270. } else {
  271. /* target device/action expect data at mac */
  272. skb_push_rcsum(skb_to_send, mac_len);
  273. }
  274. }
  275. skb_to_send->skb_iif = skb->dev->ifindex;
  276. skb_to_send->dev = dev;
  277. if (is_redirect) {
  278. if (skb == skb_to_send)
  279. retval = TC_ACT_CONSUMED;
  280. skb_set_redirected(skb_to_send, skb_to_send->tc_at_ingress);
  281. err = tcf_mirred_forward(at_ingress, want_ingress, skb_to_send);
  282. } else {
  283. err = tcf_mirred_forward(at_ingress, want_ingress, skb_to_send);
  284. }
  285. if (err)
  286. tcf_action_inc_overlimit_qstats(&m->common);
  287. return retval;
  288. err_cant_do:
  289. if (is_redirect)
  290. retval = TC_ACT_SHOT;
  291. tcf_action_inc_overlimit_qstats(&m->common);
  292. return retval;
  293. }
  294. static int tcf_blockcast_redir(struct sk_buff *skb, struct tcf_mirred *m,
  295. struct tcf_block *block, int m_eaction,
  296. const u32 exception_ifindex, int retval)
  297. {
  298. struct net_device *dev_prev = NULL;
  299. struct net_device *dev = NULL;
  300. unsigned long index;
  301. int mirred_eaction;
  302. mirred_eaction = tcf_mirred_act_wants_ingress(m_eaction) ?
  303. TCA_INGRESS_MIRROR : TCA_EGRESS_MIRROR;
  304. xa_for_each(&block->ports, index, dev) {
  305. if (index == exception_ifindex)
  306. continue;
  307. if (!dev_prev)
  308. goto assign_prev;
  309. tcf_mirred_to_dev(skb, m, dev_prev,
  310. dev_is_mac_header_xmit(dev),
  311. mirred_eaction, retval);
  312. assign_prev:
  313. dev_prev = dev;
  314. }
  315. if (dev_prev)
  316. return tcf_mirred_to_dev(skb, m, dev_prev,
  317. dev_is_mac_header_xmit(dev_prev),
  318. m_eaction, retval);
  319. return retval;
  320. }
  321. static int tcf_blockcast_mirror(struct sk_buff *skb, struct tcf_mirred *m,
  322. struct tcf_block *block, int m_eaction,
  323. const u32 exception_ifindex, int retval)
  324. {
  325. struct net_device *dev = NULL;
  326. unsigned long index;
  327. xa_for_each(&block->ports, index, dev) {
  328. if (index == exception_ifindex)
  329. continue;
  330. tcf_mirred_to_dev(skb, m, dev,
  331. dev_is_mac_header_xmit(dev),
  332. m_eaction, retval);
  333. }
  334. return retval;
  335. }
  336. static int tcf_blockcast(struct sk_buff *skb, struct tcf_mirred *m,
  337. const u32 blockid, struct tcf_result *res,
  338. int retval)
  339. {
  340. const u32 exception_ifindex = skb->dev->ifindex;
  341. struct tcf_block *block;
  342. bool is_redirect;
  343. int m_eaction;
  344. m_eaction = READ_ONCE(m->tcfm_eaction);
  345. is_redirect = tcf_mirred_is_act_redirect(m_eaction);
  346. /* we are already under rcu protection, so can call block lookup
  347. * directly.
  348. */
  349. block = tcf_block_lookup(dev_net(skb->dev), blockid);
  350. if (!block || xa_empty(&block->ports)) {
  351. tcf_action_inc_overlimit_qstats(&m->common);
  352. return retval;
  353. }
  354. if (is_redirect)
  355. return tcf_blockcast_redir(skb, m, block, m_eaction,
  356. exception_ifindex, retval);
  357. /* If it's not redirect, it is mirror */
  358. return tcf_blockcast_mirror(skb, m, block, m_eaction, exception_ifindex,
  359. retval);
  360. }
  361. TC_INDIRECT_SCOPE int tcf_mirred_act(struct sk_buff *skb,
  362. const struct tc_action *a,
  363. struct tcf_result *res)
  364. {
  365. struct tcf_mirred *m = to_mirred(a);
  366. int retval = READ_ONCE(m->tcf_action);
  367. struct netdev_xmit *xmit;
  368. bool m_mac_header_xmit;
  369. struct net_device *dev;
  370. int i, m_eaction;
  371. u32 blockid;
  372. #ifdef CONFIG_PREEMPT_RT
  373. xmit = &current->net_xmit;
  374. #else
  375. xmit = this_cpu_ptr(&softnet_data.xmit);
  376. #endif
  377. if (unlikely(xmit->sched_mirred_nest >= MIRRED_NEST_LIMIT)) {
  378. net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
  379. netdev_name(skb->dev));
  380. return TC_ACT_SHOT;
  381. }
  382. tcf_lastuse_update(&m->tcf_tm);
  383. tcf_action_update_bstats(&m->common, skb);
  384. blockid = READ_ONCE(m->tcfm_blockid);
  385. if (blockid)
  386. return tcf_blockcast(skb, m, blockid, res, retval);
  387. dev = rcu_dereference_bh(m->tcfm_dev);
  388. if (unlikely(!dev)) {
  389. pr_notice_once("tc mirred: target device is gone\n");
  390. tcf_action_inc_overlimit_qstats(&m->common);
  391. return retval;
  392. }
  393. for (i = 0; i < xmit->sched_mirred_nest; i++) {
  394. if (xmit->sched_mirred_dev[i] != dev)
  395. continue;
  396. pr_notice_once("tc mirred: loop on device %s\n",
  397. netdev_name(dev));
  398. tcf_action_inc_overlimit_qstats(&m->common);
  399. return retval;
  400. }
  401. xmit->sched_mirred_dev[xmit->sched_mirred_nest++] = dev;
  402. m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
  403. m_eaction = READ_ONCE(m->tcfm_eaction);
  404. retval = tcf_mirred_to_dev(skb, m, dev, m_mac_header_xmit, m_eaction,
  405. retval);
  406. xmit->sched_mirred_nest--;
  407. return retval;
  408. }
  409. static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  410. u64 drops, u64 lastuse, bool hw)
  411. {
  412. struct tcf_mirred *m = to_mirred(a);
  413. struct tcf_t *tm = &m->tcf_tm;
  414. tcf_action_update_stats(a, bytes, packets, drops, hw);
  415. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  416. }
  417. static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  418. int ref)
  419. {
  420. unsigned char *b = skb_tail_pointer(skb);
  421. struct tcf_mirred *m = to_mirred(a);
  422. struct tc_mirred opt = {
  423. .index = m->tcf_index,
  424. .refcnt = refcount_read(&m->tcf_refcnt) - ref,
  425. .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
  426. };
  427. struct net_device *dev;
  428. struct tcf_t t;
  429. u32 blockid;
  430. spin_lock_bh(&m->tcf_lock);
  431. opt.action = m->tcf_action;
  432. opt.eaction = m->tcfm_eaction;
  433. dev = tcf_mirred_dev_dereference(m);
  434. if (dev)
  435. opt.ifindex = dev->ifindex;
  436. if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
  437. goto nla_put_failure;
  438. blockid = m->tcfm_blockid;
  439. if (blockid && nla_put_u32(skb, TCA_MIRRED_BLOCKID, blockid))
  440. goto nla_put_failure;
  441. tcf_tm_dump(&t, &m->tcf_tm);
  442. if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
  443. goto nla_put_failure;
  444. spin_unlock_bh(&m->tcf_lock);
  445. return skb->len;
  446. nla_put_failure:
  447. spin_unlock_bh(&m->tcf_lock);
  448. nlmsg_trim(skb, b);
  449. return -1;
  450. }
  451. static int mirred_device_event(struct notifier_block *unused,
  452. unsigned long event, void *ptr)
  453. {
  454. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  455. struct tcf_mirred *m;
  456. ASSERT_RTNL();
  457. if (event == NETDEV_UNREGISTER) {
  458. spin_lock(&mirred_list_lock);
  459. list_for_each_entry(m, &mirred_list, tcfm_list) {
  460. spin_lock_bh(&m->tcf_lock);
  461. if (tcf_mirred_dev_dereference(m) == dev) {
  462. netdev_put(dev, &m->tcfm_dev_tracker);
  463. /* Note : no rcu grace period necessary, as
  464. * net_device are already rcu protected.
  465. */
  466. RCU_INIT_POINTER(m->tcfm_dev, NULL);
  467. }
  468. spin_unlock_bh(&m->tcf_lock);
  469. }
  470. spin_unlock(&mirred_list_lock);
  471. }
  472. return NOTIFY_DONE;
  473. }
  474. static struct notifier_block mirred_device_notifier = {
  475. .notifier_call = mirred_device_event,
  476. };
  477. static void tcf_mirred_dev_put(void *priv)
  478. {
  479. struct net_device *dev = priv;
  480. dev_put(dev);
  481. }
  482. static struct net_device *
  483. tcf_mirred_get_dev(const struct tc_action *a,
  484. tc_action_priv_destructor *destructor)
  485. {
  486. struct tcf_mirred *m = to_mirred(a);
  487. struct net_device *dev;
  488. rcu_read_lock();
  489. dev = rcu_dereference(m->tcfm_dev);
  490. if (dev) {
  491. dev_hold(dev);
  492. *destructor = tcf_mirred_dev_put;
  493. }
  494. rcu_read_unlock();
  495. return dev;
  496. }
  497. static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
  498. {
  499. return nla_total_size(sizeof(struct tc_mirred));
  500. }
  501. static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
  502. const struct tc_action *act)
  503. {
  504. entry->dev = act->ops->get_dev(act, &entry->destructor);
  505. if (!entry->dev)
  506. return;
  507. entry->destructor_priv = entry->dev;
  508. }
  509. static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data,
  510. u32 *index_inc, bool bind,
  511. struct netlink_ext_ack *extack)
  512. {
  513. if (bind) {
  514. struct flow_action_entry *entry = entry_data;
  515. if (is_tcf_mirred_egress_redirect(act)) {
  516. entry->id = FLOW_ACTION_REDIRECT;
  517. tcf_offload_mirred_get_dev(entry, act);
  518. } else if (is_tcf_mirred_egress_mirror(act)) {
  519. entry->id = FLOW_ACTION_MIRRED;
  520. tcf_offload_mirred_get_dev(entry, act);
  521. } else if (is_tcf_mirred_ingress_redirect(act)) {
  522. entry->id = FLOW_ACTION_REDIRECT_INGRESS;
  523. tcf_offload_mirred_get_dev(entry, act);
  524. } else if (is_tcf_mirred_ingress_mirror(act)) {
  525. entry->id = FLOW_ACTION_MIRRED_INGRESS;
  526. tcf_offload_mirred_get_dev(entry, act);
  527. } else {
  528. NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
  529. return -EOPNOTSUPP;
  530. }
  531. *index_inc = 1;
  532. } else {
  533. struct flow_offload_action *fl_action = entry_data;
  534. if (is_tcf_mirred_egress_redirect(act))
  535. fl_action->id = FLOW_ACTION_REDIRECT;
  536. else if (is_tcf_mirred_egress_mirror(act))
  537. fl_action->id = FLOW_ACTION_MIRRED;
  538. else if (is_tcf_mirred_ingress_redirect(act))
  539. fl_action->id = FLOW_ACTION_REDIRECT_INGRESS;
  540. else if (is_tcf_mirred_ingress_mirror(act))
  541. fl_action->id = FLOW_ACTION_MIRRED_INGRESS;
  542. else
  543. return -EOPNOTSUPP;
  544. }
  545. return 0;
  546. }
  547. static struct tc_action_ops act_mirred_ops = {
  548. .kind = "mirred",
  549. .id = TCA_ID_MIRRED,
  550. .owner = THIS_MODULE,
  551. .act = tcf_mirred_act,
  552. .stats_update = tcf_stats_update,
  553. .dump = tcf_mirred_dump,
  554. .cleanup = tcf_mirred_release,
  555. .init = tcf_mirred_init,
  556. .get_fill_size = tcf_mirred_get_fill_size,
  557. .offload_act_setup = tcf_mirred_offload_act_setup,
  558. .size = sizeof(struct tcf_mirred),
  559. .get_dev = tcf_mirred_get_dev,
  560. };
  561. MODULE_ALIAS_NET_ACT("mirred");
  562. static __net_init int mirred_init_net(struct net *net)
  563. {
  564. struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
  565. return tc_action_net_init(net, tn, &act_mirred_ops);
  566. }
  567. static void __net_exit mirred_exit_net(struct list_head *net_list)
  568. {
  569. tc_action_net_exit(net_list, act_mirred_ops.net_id);
  570. }
  571. static struct pernet_operations mirred_net_ops = {
  572. .init = mirred_init_net,
  573. .exit_batch = mirred_exit_net,
  574. .id = &act_mirred_ops.net_id,
  575. .size = sizeof(struct tc_action_net),
  576. };
  577. MODULE_AUTHOR("Jamal Hadi Salim(2002)");
  578. MODULE_DESCRIPTION("Device Mirror/redirect actions");
  579. MODULE_LICENSE("GPL");
  580. static int __init mirred_init_module(void)
  581. {
  582. int err = register_netdevice_notifier(&mirred_device_notifier);
  583. if (err)
  584. return err;
  585. pr_info("Mirror/redirect action on\n");
  586. err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
  587. if (err)
  588. unregister_netdevice_notifier(&mirred_device_notifier);
  589. return err;
  590. }
  591. static void __exit mirred_cleanup_module(void)
  592. {
  593. tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
  594. unregister_netdevice_notifier(&mirred_device_notifier);
  595. }
  596. module_init(mirred_init_module);
  597. module_exit(mirred_cleanup_module);