Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jiri Pirko | 0c6965d | 2014-11-05 20:51:51 +0100 | [diff] [blame] | 3 | * net/sched/act_mirred.c packet mirroring and redirect actions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Jamal Hadi Salim (2002-4) |
| 6 | * |
| 7 | * TODO: Add ingress support (and socket redirect support) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/rtnetlink.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
Daniel Borkmann | c491680 | 2016-11-26 01:28:06 +0100 | [diff] [blame] | 19 | #include <linux/if_arp.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 20 | #include <net/net_namespace.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 21 | #include <net/netlink.h> |
Xin Long | f799ada | 2021-11-12 11:33:11 -0500 | [diff] [blame] | 22 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/pkt_sched.h> |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 24 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/tc_act/tc_mirred.h> |
| 26 | #include <net/tc_act/tc_mirred.h> |
| 27 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 28 | static LIST_HEAD(mirred_list); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 29 | static DEFINE_SPINLOCK(mirred_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
John Hurley | e2ca070 | 2019-06-24 23:13:36 +0100 | [diff] [blame] | 31 | #define MIRRED_RECURSION_LIMIT 4 |
| 32 | static DEFINE_PER_CPU(unsigned int, mirred_rec_level); |
| 33 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 34 | static bool tcf_mirred_is_act_redirect(int action) |
| 35 | { |
| 36 | return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR; |
| 37 | } |
| 38 | |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 39 | static bool tcf_mirred_act_wants_ingress(int action) |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 40 | { |
| 41 | switch (action) { |
| 42 | case TCA_EGRESS_REDIR: |
| 43 | case TCA_EGRESS_MIRROR: |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 44 | return false; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 45 | case TCA_INGRESS_REDIR: |
| 46 | case TCA_INGRESS_MIRROR: |
Willem de Bruijn | 8dc07fd | 2017-01-07 17:06:37 -0500 | [diff] [blame] | 47 | return true; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 48 | default: |
| 49 | BUG(); |
| 50 | } |
| 51 | } |
| 52 | |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 53 | static bool tcf_mirred_can_reinsert(int action) |
| 54 | { |
| 55 | switch (action) { |
| 56 | case TC_ACT_SHOT: |
| 57 | case TC_ACT_STOLEN: |
| 58 | case TC_ACT_QUEUED: |
| 59 | case TC_ACT_TRAP: |
| 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 65 | static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m) |
| 66 | { |
| 67 | return rcu_dereference_protected(m->tcfm_dev, |
| 68 | lockdep_is_held(&m->tcf_lock)); |
| 69 | } |
| 70 | |
Cong Wang | 9a63b25 | 2017-12-05 12:53:07 -0800 | [diff] [blame] | 71 | static void tcf_mirred_release(struct tc_action *a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 73 | struct tcf_mirred *m = to_mirred(a); |
WANG Cong | dc327f8 | 2016-05-16 15:11:18 -0700 | [diff] [blame] | 74 | struct net_device *dev; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 75 | |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 76 | spin_lock(&mirred_list_lock); |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 77 | list_del(&m->tcfm_list); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 78 | spin_unlock(&mirred_list_lock); |
| 79 | |
| 80 | /* last reference to action, no need to lock */ |
| 81 | dev = rcu_dereference_protected(m->tcfm_dev, 1); |
Eric Dumazet | ada066b | 2021-12-06 17:30:39 -0800 | [diff] [blame] | 82 | dev_put_track(dev, &m->tcfm_dev_tracker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 85 | static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = { |
| 86 | [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) }, |
| 87 | }; |
| 88 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 89 | static unsigned int mirred_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 90 | static struct tc_action_ops act_mirred_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 91 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 92 | static int tcf_mirred_init(struct net *net, struct nlattr *nla, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 93 | struct nlattr *est, struct tc_action **a, |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 94 | struct tcf_proto *tp, |
Vlad Buslov | abbb0d3 | 2019-10-30 16:09:05 +0200 | [diff] [blame] | 95 | u32 flags, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 97 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 98 | bool bind = flags & TCA_ACT_FLAGS_BIND; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 99 | struct nlattr *tb[TCA_MIRRED_MAX + 1]; |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 100 | struct tcf_chain *goto_ch = NULL; |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 101 | bool mac_header_xmit = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct tc_mirred *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 103 | struct tcf_mirred *m; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 104 | bool exists = false; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 105 | int ret, err; |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 106 | u32 index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 108 | if (!nla) { |
| 109 | NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 111 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 112 | ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla, |
| 113 | mirred_policy, extack); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 114 | if (ret < 0) |
| 115 | return ret; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 116 | if (!tb[TCA_MIRRED_PARMS]) { |
| 117 | NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 119 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 120 | parm = nla_data(tb[TCA_MIRRED_PARMS]); |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 121 | index = parm->index; |
| 122 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 123 | if (err < 0) |
| 124 | return err; |
| 125 | exists = err; |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 126 | if (exists && bind) |
| 127 | return 0; |
| 128 | |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 129 | switch (parm->eaction) { |
| 130 | case TCA_EGRESS_MIRROR: |
| 131 | case TCA_EGRESS_REDIR: |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 132 | case TCA_INGRESS_REDIR: |
| 133 | case TCA_INGRESS_MIRROR: |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 134 | break; |
| 135 | default: |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 136 | if (exists) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 137 | tcf_idr_release(*a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 138 | else |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 139 | tcf_idr_cleanup(tn, index); |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 140 | NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option"); |
Changli Gao | b76965e | 2009-11-17 04:15:16 -0800 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Jamal Hadi Salim | 87dfbdc | 2016-05-10 16:49:28 -0400 | [diff] [blame] | 144 | if (!exists) { |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 145 | if (!parm->ifindex) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 146 | tcf_idr_cleanup(tn, index); |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 147 | NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return -EINVAL; |
Alexander Aring | 1d4760c | 2018-02-15 10:55:00 -0500 | [diff] [blame] | 149 | } |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 150 | ret = tcf_idr_create_from_flags(tn, index, est, a, |
| 151 | &act_mirred_ops, bind, flags); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 152 | if (ret) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 153 | tcf_idr_cleanup(tn, index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 154 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | ret = ACT_P_CREATED; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 157 | } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 158 | tcf_idr_release(*a, bind); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 159 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
John Hurley | 064c5d6 | 2019-03-22 12:37:35 +0000 | [diff] [blame] | 161 | |
| 162 | m = to_mirred(*a); |
| 163 | if (ret == ACT_P_CREATED) |
| 164 | INIT_LIST_HEAD(&m->tcfm_list); |
| 165 | |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 166 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 167 | if (err < 0) |
| 168 | goto release_idr; |
| 169 | |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 170 | spin_lock_bh(&m->tcf_lock); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 171 | |
| 172 | if (parm->ifindex) { |
Eric Dumazet | ada066b | 2021-12-06 17:30:39 -0800 | [diff] [blame] | 173 | struct net_device *odev, *ndev; |
| 174 | |
| 175 | ndev = dev_get_by_index(net, parm->ifindex); |
| 176 | if (!ndev) { |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 177 | spin_unlock_bh(&m->tcf_lock); |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 178 | err = -ENODEV; |
| 179 | goto put_chain; |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 180 | } |
Eric Dumazet | ada066b | 2021-12-06 17:30:39 -0800 | [diff] [blame] | 181 | mac_header_xmit = dev_is_mac_header_xmit(ndev); |
| 182 | odev = rcu_replace_pointer(m->tcfm_dev, ndev, |
Paul E. McKenney | 445d374 | 2019-09-23 16:09:18 -0700 | [diff] [blame] | 183 | lockdep_is_held(&m->tcf_lock)); |
Eric Dumazet | ada066b | 2021-12-06 17:30:39 -0800 | [diff] [blame] | 184 | dev_put_track(odev, &m->tcfm_dev_tracker); |
| 185 | netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC); |
Shmulik Ladkani | 1657792 | 2016-10-13 09:06:41 +0300 | [diff] [blame] | 186 | m->tcfm_mac_header_xmit = mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 188 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
| 189 | m->tcfm_eaction = parm->eaction; |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 190 | spin_unlock_bh(&m->tcf_lock); |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 191 | if (goto_ch) |
| 192 | tcf_chain_put_by_act(goto_ch); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 193 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 194 | if (ret == ACT_P_CREATED) { |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 195 | spin_lock(&mirred_list_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 196 | list_add(&m->tcfm_list, &mirred_list); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 197 | spin_unlock(&mirred_list_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return ret; |
Davide Caratti | ff9721d | 2019-03-20 15:00:04 +0100 | [diff] [blame] | 201 | put_chain: |
| 202 | if (goto_ch) |
| 203 | tcf_chain_put_by_act(goto_ch); |
| 204 | release_idr: |
| 205 | tcf_idr_release(*a, bind); |
| 206 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
wenxu | fa6d639 | 2020-11-25 12:01:22 +0800 | [diff] [blame] | 209 | static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb) |
| 210 | { |
| 211 | int err; |
| 212 | |
| 213 | if (!want_ingress) |
wenxu | c129412 | 2020-11-25 12:01:23 +0800 | [diff] [blame] | 214 | err = tcf_dev_queue_xmit(skb, dev_queue_xmit); |
wenxu | fa6d639 | 2020-11-25 12:01:22 +0800 | [diff] [blame] | 215 | else |
| 216 | err = netif_receive_skb(skb); |
| 217 | |
| 218 | return err; |
| 219 | } |
| 220 | |
Jamal Hadi Salim | 7c5790c | 2018-08-12 09:35:01 -0400 | [diff] [blame] | 221 | static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a, |
| 222 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 224 | struct tcf_mirred *m = to_mirred(a); |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 225 | struct sk_buff *skb2 = skb; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 226 | bool m_mac_header_xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct net_device *dev; |
John Hurley | e2ca070 | 2019-06-24 23:13:36 +0100 | [diff] [blame] | 228 | unsigned int rec_level; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 229 | int retval, err = 0; |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 230 | bool use_reinsert; |
| 231 | bool want_ingress; |
| 232 | bool is_redirect; |
Shmulik Ladkani | 70cf3dc | 2019-12-25 10:51:01 +0200 | [diff] [blame] | 233 | bool expects_nh; |
Xin Long | f799ada | 2021-11-12 11:33:11 -0500 | [diff] [blame] | 234 | bool at_ingress; |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 235 | int m_eaction; |
| 236 | int mac_len; |
Shmulik Ladkani | 70cf3dc | 2019-12-25 10:51:01 +0200 | [diff] [blame] | 237 | bool at_nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
John Hurley | e2ca070 | 2019-06-24 23:13:36 +0100 | [diff] [blame] | 239 | rec_level = __this_cpu_inc_return(mirred_rec_level); |
| 240 | if (unlikely(rec_level > MIRRED_RECURSION_LIMIT)) { |
| 241 | net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n", |
| 242 | netdev_name(skb->dev)); |
| 243 | __this_cpu_dec(mirred_rec_level); |
| 244 | return TC_ACT_SHOT; |
| 245 | } |
| 246 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 247 | tcf_lastuse_update(&m->tcf_tm); |
Vlad Buslov | 5e1ad95 | 2019-10-30 16:09:01 +0200 | [diff] [blame] | 248 | tcf_action_update_bstats(&m->common, skb); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 249 | |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 250 | m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit); |
| 251 | m_eaction = READ_ONCE(m->tcfm_eaction); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 252 | retval = READ_ONCE(m->tcf_action); |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 253 | dev = rcu_dereference_bh(m->tcfm_dev); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 254 | if (unlikely(!dev)) { |
| 255 | pr_notice_once("tc mirred: target device is gone\n"); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 256 | goto out; |
| 257 | } |
| 258 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 259 | if (unlikely(!(dev->flags & IFF_UP))) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 260 | net_notice_ratelimited("tc mirred to Houston: device %s is down\n", |
| 261 | dev->name); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 262 | goto out; |
| 263 | } |
| 264 | |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 265 | /* we could easily avoid the clone only if called by ingress and clsact; |
| 266 | * since we can't easily detect the clsact caller, skip clone only for |
| 267 | * ingress - that covers the TC S/W datapath. |
| 268 | */ |
| 269 | is_redirect = tcf_mirred_is_act_redirect(m_eaction); |
Xin Long | f799ada | 2021-11-12 11:33:11 -0500 | [diff] [blame] | 270 | at_ingress = skb_at_tc_ingress(skb); |
| 271 | use_reinsert = at_ingress && is_redirect && |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 272 | tcf_mirred_can_reinsert(retval); |
| 273 | if (!use_reinsert) { |
| 274 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 275 | if (!skb2) |
| 276 | goto out; |
| 277 | } |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 278 | |
Xin Long | f799ada | 2021-11-12 11:33:11 -0500 | [diff] [blame] | 279 | want_ingress = tcf_mirred_act_wants_ingress(m_eaction); |
| 280 | |
Hangbin Liu | d09c548 | 2021-08-09 15:04:55 +0800 | [diff] [blame] | 281 | /* All mirred/redirected skbs should clear previous ct info */ |
| 282 | nf_reset_ct(skb2); |
Xin Long | f799ada | 2021-11-12 11:33:11 -0500 | [diff] [blame] | 283 | if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */ |
| 284 | skb_dst_drop(skb2); |
Shmulik Ladkani | 70cf3dc | 2019-12-25 10:51:01 +0200 | [diff] [blame] | 285 | |
| 286 | expects_nh = want_ingress || !m_mac_header_xmit; |
| 287 | at_nh = skb->data == skb_network_header(skb); |
| 288 | if (at_nh != expects_nh) { |
| 289 | mac_len = skb_at_tc_ingress(skb) ? skb->mac_len : |
| 290 | skb_network_header(skb) - skb_mac_header(skb); |
| 291 | if (expects_nh) { |
| 292 | /* target device/action expect data at nh */ |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 293 | skb_pull_rcsum(skb2, mac_len); |
| 294 | } else { |
Shmulik Ladkani | 70cf3dc | 2019-12-25 10:51:01 +0200 | [diff] [blame] | 295 | /* target device/action expect data at mac */ |
| 296 | skb_push_rcsum(skb2, mac_len); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 297 | } |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 300 | skb2->skb_iif = skb->dev->ifindex; |
Changli Gao | 210d6de | 2010-06-24 16:25:12 +0000 | [diff] [blame] | 301 | skb2->dev = dev; |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 302 | |
| 303 | /* mirror is always swallowed */ |
| 304 | if (is_redirect) { |
Pablo Neira Ayuso | 2c64605 | 2020-03-25 13:47:18 +0100 | [diff] [blame] | 305 | skb_set_redirected(skb2, skb2->tc_at_ingress); |
| 306 | |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 307 | /* let's the caller reinsert the packet, if possible */ |
| 308 | if (use_reinsert) { |
| 309 | res->ingress = want_ingress; |
wenxu | fa6d639 | 2020-11-25 12:01:22 +0800 | [diff] [blame] | 310 | err = tcf_mirred_forward(res->ingress, skb); |
| 311 | if (err) |
Vlad Buslov | ef816f3 | 2019-10-30 16:09:03 +0200 | [diff] [blame] | 312 | tcf_action_inc_overlimit_qstats(&m->common); |
John Hurley | e2ca070 | 2019-06-24 23:13:36 +0100 | [diff] [blame] | 313 | __this_cpu_dec(mirred_rec_level); |
John Hurley | 720f22f | 2019-06-24 23:13:35 +0100 | [diff] [blame] | 314 | return TC_ACT_CONSUMED; |
Paolo Abeni | e5cf1ba | 2018-07-30 14:30:45 +0200 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
wenxu | fa6d639 | 2020-11-25 12:01:22 +0800 | [diff] [blame] | 318 | err = tcf_mirred_forward(want_ingress, skb2); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 319 | if (err) { |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 320 | out: |
Vlad Buslov | 26b537a | 2019-10-30 16:09:02 +0200 | [diff] [blame] | 321 | tcf_action_inc_overlimit_qstats(&m->common); |
Shmulik Ladkani | 53592b3 | 2016-10-13 09:06:44 +0300 | [diff] [blame] | 322 | if (tcf_mirred_is_act_redirect(m_eaction)) |
Jason Wang | 16c0b16 | 2012-08-15 20:44:27 +0000 | [diff] [blame] | 323 | retval = TC_ACT_SHOT; |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 324 | } |
John Hurley | e2ca070 | 2019-06-24 23:13:36 +0100 | [diff] [blame] | 325 | __this_cpu_dec(mirred_rec_level); |
Changli Gao | feed1f1 | 2009-11-17 04:14:00 -0800 | [diff] [blame] | 326 | |
| 327 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 330 | static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets, |
| 331 | u64 drops, u64 lastuse, bool hw) |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 332 | { |
Paul Blakey | 5712bf9 | 2016-10-19 17:42:39 +0300 | [diff] [blame] | 333 | struct tcf_mirred *m = to_mirred(a); |
| 334 | struct tcf_t *tm = &m->tcf_tm; |
| 335 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 336 | tcf_action_update_stats(a, bytes, packets, drops, hw); |
Roi Dayan | 3bb2342 | 2017-12-26 07:48:51 +0200 | [diff] [blame] | 337 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 340 | static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
| 341 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 343 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 344 | struct tcf_mirred *m = to_mirred(a); |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 345 | struct tc_mirred opt = { |
| 346 | .index = m->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 347 | .refcnt = refcount_read(&m->tcf_refcnt) - ref, |
| 348 | .bindcnt = atomic_read(&m->tcf_bindcnt) - bind, |
Eric Dumazet | 1c40be1 | 2010-08-16 20:04:22 +0000 | [diff] [blame] | 349 | }; |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 350 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct tcf_t t; |
| 352 | |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 353 | spin_lock_bh(&m->tcf_lock); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 354 | opt.action = m->tcf_action; |
| 355 | opt.eaction = m->tcfm_eaction; |
| 356 | dev = tcf_mirred_dev_dereference(m); |
| 357 | if (dev) |
| 358 | opt.ifindex = dev->ifindex; |
| 359 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 360 | if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt)) |
| 361 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 362 | |
| 363 | tcf_tm_dump(&t, &m->tcf_tm); |
Nicolas Dichtel | 9854518e | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 364 | if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 365 | goto nla_put_failure; |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 366 | spin_unlock_bh(&m->tcf_lock); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return skb->len; |
| 369 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 370 | nla_put_failure: |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 371 | spin_unlock_bh(&m->tcf_lock); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 372 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return -1; |
| 374 | } |
| 375 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 376 | static int tcf_mirred_walker(struct net *net, struct sk_buff *skb, |
| 377 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 378 | const struct tc_action_ops *ops, |
| 379 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 380 | { |
| 381 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 382 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 383 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 386 | static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 387 | { |
| 388 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 389 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 390 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 391 | } |
| 392 | |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 393 | static int mirred_device_event(struct notifier_block *unused, |
| 394 | unsigned long event, void *ptr) |
| 395 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 396 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 397 | struct tcf_mirred *m; |
| 398 | |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 399 | ASSERT_RTNL(); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 400 | if (event == NETDEV_UNREGISTER) { |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 401 | spin_lock(&mirred_list_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 402 | list_for_each_entry(m, &mirred_list, tcfm_list) { |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 403 | spin_lock_bh(&m->tcf_lock); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 404 | if (tcf_mirred_dev_dereference(m) == dev) { |
Eric Dumazet | ada066b | 2021-12-06 17:30:39 -0800 | [diff] [blame] | 405 | dev_put_track(dev, &m->tcfm_dev_tracker); |
Eric Dumazet | 2ee22a9 | 2015-07-06 05:18:09 -0700 | [diff] [blame] | 406 | /* Note : no rcu grace period necessary, as |
| 407 | * net_device are already rcu protected. |
| 408 | */ |
| 409 | RCU_INIT_POINTER(m->tcfm_dev, NULL); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 410 | } |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 411 | spin_unlock_bh(&m->tcf_lock); |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 412 | } |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 413 | spin_unlock(&mirred_list_lock); |
WANG Cong | 6bd00b8 | 2015-10-01 11:37:42 -0700 | [diff] [blame] | 414 | } |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 415 | |
| 416 | return NOTIFY_DONE; |
| 417 | } |
| 418 | |
| 419 | static struct notifier_block mirred_device_notifier = { |
| 420 | .notifier_call = mirred_device_event, |
| 421 | }; |
| 422 | |
Vlad Buslov | 470d506 | 2019-09-13 18:28:41 +0300 | [diff] [blame] | 423 | static void tcf_mirred_dev_put(void *priv) |
| 424 | { |
| 425 | struct net_device *dev = priv; |
| 426 | |
| 427 | dev_put(dev); |
| 428 | } |
| 429 | |
| 430 | static struct net_device * |
| 431 | tcf_mirred_get_dev(const struct tc_action *a, |
| 432 | tc_action_priv_destructor *destructor) |
Hadar Hen Zion | 255cb30 | 2016-12-01 14:06:36 +0200 | [diff] [blame] | 433 | { |
Jiri Pirko | 843e79d | 2017-10-11 09:41:07 +0200 | [diff] [blame] | 434 | struct tcf_mirred *m = to_mirred(a); |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 435 | struct net_device *dev; |
Hadar Hen Zion | 255cb30 | 2016-12-01 14:06:36 +0200 | [diff] [blame] | 436 | |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 437 | rcu_read_lock(); |
| 438 | dev = rcu_dereference(m->tcfm_dev); |
Vlad Buslov | 470d506 | 2019-09-13 18:28:41 +0300 | [diff] [blame] | 439 | if (dev) { |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame] | 440 | dev_hold(dev); |
Vlad Buslov | 470d506 | 2019-09-13 18:28:41 +0300 | [diff] [blame] | 441 | *destructor = tcf_mirred_dev_put; |
| 442 | } |
Vlad Buslov | 4e23281 | 2018-08-10 20:51:53 +0300 | [diff] [blame] | 443 | rcu_read_unlock(); |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame] | 444 | |
| 445 | return dev; |
| 446 | } |
| 447 | |
Roman Mashak | b84b2d4 | 2019-06-28 14:30:17 -0400 | [diff] [blame] | 448 | static size_t tcf_mirred_get_fill_size(const struct tc_action *act) |
| 449 | { |
| 450 | return nla_total_size(sizeof(struct tc_mirred)); |
| 451 | } |
| 452 | |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 453 | static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry, |
| 454 | const struct tc_action *act) |
| 455 | { |
| 456 | entry->dev = act->ops->get_dev(act, &entry->destructor); |
| 457 | if (!entry->dev) |
| 458 | return; |
| 459 | entry->destructor_priv = entry->dev; |
| 460 | } |
| 461 | |
| 462 | static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data, |
| 463 | u32 *index_inc, bool bind) |
| 464 | { |
| 465 | if (bind) { |
| 466 | struct flow_action_entry *entry = entry_data; |
| 467 | |
| 468 | if (is_tcf_mirred_egress_redirect(act)) { |
| 469 | entry->id = FLOW_ACTION_REDIRECT; |
| 470 | tcf_offload_mirred_get_dev(entry, act); |
| 471 | } else if (is_tcf_mirred_egress_mirror(act)) { |
| 472 | entry->id = FLOW_ACTION_MIRRED; |
| 473 | tcf_offload_mirred_get_dev(entry, act); |
| 474 | } else if (is_tcf_mirred_ingress_redirect(act)) { |
| 475 | entry->id = FLOW_ACTION_REDIRECT_INGRESS; |
| 476 | tcf_offload_mirred_get_dev(entry, act); |
| 477 | } else if (is_tcf_mirred_ingress_mirror(act)) { |
| 478 | entry->id = FLOW_ACTION_MIRRED_INGRESS; |
| 479 | tcf_offload_mirred_get_dev(entry, act); |
| 480 | } else { |
| 481 | return -EOPNOTSUPP; |
| 482 | } |
| 483 | *index_inc = 1; |
| 484 | } else { |
Baowen Zheng | 8cbfe93 | 2021-12-17 19:16:22 +0100 | [diff] [blame] | 485 | struct flow_offload_action *fl_action = entry_data; |
| 486 | |
| 487 | if (is_tcf_mirred_egress_redirect(act)) |
| 488 | fl_action->id = FLOW_ACTION_REDIRECT; |
| 489 | else if (is_tcf_mirred_egress_mirror(act)) |
| 490 | fl_action->id = FLOW_ACTION_MIRRED; |
| 491 | else if (is_tcf_mirred_ingress_redirect(act)) |
| 492 | fl_action->id = FLOW_ACTION_REDIRECT_INGRESS; |
| 493 | else if (is_tcf_mirred_ingress_mirror(act)) |
| 494 | fl_action->id = FLOW_ACTION_MIRRED_INGRESS; |
| 495 | else |
| 496 | return -EOPNOTSUPP; |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | static struct tc_action_ops act_mirred_ops = { |
| 503 | .kind = "mirred", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 504 | .id = TCA_ID_MIRRED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | .owner = THIS_MODULE, |
Jamal Hadi Salim | 7c5790c | 2018-08-12 09:35:01 -0400 | [diff] [blame] | 506 | .act = tcf_mirred_act, |
Jakub Kicinski | 9798e6f | 2016-09-21 11:44:05 +0100 | [diff] [blame] | 507 | .stats_update = tcf_stats_update, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | .dump = tcf_mirred_dump, |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 509 | .cleanup = tcf_mirred_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | .init = tcf_mirred_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 511 | .walk = tcf_mirred_walker, |
| 512 | .lookup = tcf_mirred_search, |
Roman Mashak | b84b2d4 | 2019-06-28 14:30:17 -0400 | [diff] [blame] | 513 | .get_fill_size = tcf_mirred_get_fill_size, |
Baowen Zheng | c54e1d9 | 2021-12-17 19:16:21 +0100 | [diff] [blame] | 514 | .offload_act_setup = tcf_mirred_offload_act_setup, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 515 | .size = sizeof(struct tcf_mirred), |
Jiri Pirko | 843e79d | 2017-10-11 09:41:07 +0200 | [diff] [blame] | 516 | .get_dev = tcf_mirred_get_dev, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | static __net_init int mirred_init_net(struct net *net) |
| 520 | { |
| 521 | struct tc_action_net *tn = net_generic(net, mirred_net_id); |
| 522 | |
Cong Wang | 981471b | 2019-08-25 10:01:32 -0700 | [diff] [blame] | 523 | return tc_action_net_init(net, tn, &act_mirred_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 526 | static void __net_exit mirred_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 527 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 528 | tc_action_net_exit(net_list, mirred_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static struct pernet_operations mirred_net_ops = { |
| 532 | .init = mirred_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 533 | .exit_batch = mirred_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 534 | .id = &mirred_net_id, |
| 535 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | MODULE_AUTHOR("Jamal Hadi Salim(2002)"); |
| 539 | MODULE_DESCRIPTION("Device Mirror/redirect actions"); |
| 540 | MODULE_LICENSE("GPL"); |
| 541 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 542 | static int __init mirred_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
stephen hemminger | 3b87956 | 2010-07-22 18:45:04 +0000 | [diff] [blame] | 544 | int err = register_netdevice_notifier(&mirred_device_notifier); |
| 545 | if (err) |
| 546 | return err; |
| 547 | |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 548 | pr_info("Mirror/redirect action on\n"); |
YueHaibing | 11c9a7d | 2019-10-09 11:10:52 +0800 | [diff] [blame] | 549 | err = tcf_register_action(&act_mirred_ops, &mirred_net_ops); |
| 550 | if (err) |
| 551 | unregister_netdevice_notifier(&mirred_device_notifier); |
| 552 | |
| 553 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 556 | static void __exit mirred_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 558 | tcf_unregister_action(&act_mirred_ops, &mirred_net_ops); |
WANG Cong | 568a153 | 2013-12-20 00:08:51 -0800 | [diff] [blame] | 559 | unregister_netdevice_notifier(&mirred_device_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | module_init(mirred_init_module); |
| 563 | module_exit(mirred_cleanup_module); |