act_mirred.c 17 KB

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