blob: 39acd1d186098bbb958822f3c087276ee0ea84e4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01003 * net/sched/act_mirred.c packet mirroring and redirect actions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Jamal Hadi Salim (2002-4)
6 *
7 * TODO: Add ingress support (and socket redirect support)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
15#include <linux/rtnetlink.h>
16#include <linux/module.h>
17#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Daniel Borkmannc4916802016-11-26 01:28:06 +010019#include <linux/if_arp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070020#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070021#include <net/netlink.h>
Xin Longf799ada2021-11-12 11:33:11 -050022#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/pkt_sched.h>
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020024#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/tc_act/tc_mirred.h>
26#include <net/tc_act/tc_mirred.h>
27
stephen hemminger3b879562010-07-22 18:45:04 +000028static LIST_HEAD(mirred_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030029static DEFINE_SPINLOCK(mirred_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
John Hurleye2ca0702019-06-24 23:13:36 +010031#define MIRRED_RECURSION_LIMIT 4
32static DEFINE_PER_CPU(unsigned int, mirred_rec_level);
33
Shmulik Ladkani53592b32016-10-13 09:06:44 +030034static bool tcf_mirred_is_act_redirect(int action)
35{
36 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
37}
38
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050039static bool tcf_mirred_act_wants_ingress(int action)
Shmulik Ladkani53592b32016-10-13 09:06:44 +030040{
41 switch (action) {
42 case TCA_EGRESS_REDIR:
43 case TCA_EGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050044 return false;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030045 case TCA_INGRESS_REDIR:
46 case TCA_INGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050047 return true;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030048 default:
49 BUG();
50 }
51}
52
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020053static 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 Buslov4e232812018-08-10 20:51:53 +030065static 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 Wang9a63b252017-12-05 12:53:07 -080071static void tcf_mirred_release(struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
WANG Cong86062032014-02-11 17:07:31 -080073 struct tcf_mirred *m = to_mirred(a);
WANG Congdc327f82016-05-16 15:11:18 -070074 struct net_device *dev;
Eric Dumazet2ee22a92015-07-06 05:18:09 -070075
Vlad Buslov4e232812018-08-10 20:51:53 +030076 spin_lock(&mirred_list_lock);
WANG Conga5b5c952014-02-11 17:07:32 -080077 list_del(&m->tcfm_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030078 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 Dumazetada066b2021-12-06 17:30:39 -080082 dev_put_track(dev, &m->tcfm_dev_tracker);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Patrick McHardy53b2bf32008-01-23 20:36:30 -080085static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
86 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
87};
88
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030089static unsigned int mirred_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070090static struct tc_action_ops act_mirred_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080091
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000092static int tcf_mirred_init(struct net *net, struct nlattr *nla,
Vlad Buslov789871b2018-07-05 17:24:25 +030093 struct nlattr *est, struct tc_action **a,
Davide Caratti85d09662019-03-20 14:59:59 +010094 struct tcf_proto *tp,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020095 u32 flags, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
WANG Congddf97cc2016-02-22 15:57:53 -080097 struct tc_action_net *tn = net_generic(net, mirred_net_id);
Cong Wang695176b2021-07-29 16:12:14 -070098 bool bind = flags & TCA_ACT_FLAGS_BIND;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080099 struct nlattr *tb[TCA_MIRRED_MAX + 1];
Davide Carattiff9721d2019-03-20 15:00:04 +0100100 struct tcf_chain *goto_ch = NULL;
Shmulik Ladkani16577922016-10-13 09:06:41 +0300101 bool mac_header_xmit = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct tc_mirred *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 struct tcf_mirred *m;
WANG Congb2313072016-06-13 13:46:28 -0700104 bool exists = false;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300105 int ret, err;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000106 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Alexander Aring1d4760c2018-02-15 10:55:00 -0500108 if (!nla) {
109 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500111 }
Johannes Berg8cb08172019-04-26 14:07:28 +0200112 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
113 mirred_policy, extack);
Changli Gaob76965e2009-11-17 04:15:16 -0800114 if (ret < 0)
115 return ret;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500116 if (!tb[TCA_MIRRED_PARMS]) {
117 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500119 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800120 parm = nla_data(tb[TCA_MIRRED_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000121 index = parm->index;
122 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300123 if (err < 0)
124 return err;
125 exists = err;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400126 if (exists && bind)
127 return 0;
128
Changli Gaob76965e2009-11-17 04:15:16 -0800129 switch (parm->eaction) {
130 case TCA_EGRESS_MIRROR:
131 case TCA_EGRESS_REDIR:
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300132 case TCA_INGRESS_REDIR:
133 case TCA_INGRESS_MIRROR:
Changli Gaob76965e2009-11-17 04:15:16 -0800134 break;
135 default:
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400136 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400137 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300138 else
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000139 tcf_idr_cleanup(tn, index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500140 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
Changli Gaob76965e2009-11-17 04:15:16 -0800141 return -EINVAL;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400144 if (!exists) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300145 if (!parm->ifindex) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000146 tcf_idr_cleanup(tn, index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500147 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500149 }
Vlad Buslove3822672019-10-30 16:09:06 +0200150 ret = tcf_idr_create_from_flags(tn, index, est, a,
151 &act_mirred_ops, bind, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300152 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000153 tcf_idr_cleanup(tn, index);
WANG Cong86062032014-02-11 17:07:31 -0800154 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ret = ACT_P_CREATED;
Cong Wang695176b2021-07-29 16:12:14 -0700157 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
Chris Mi65a206c2017-08-30 02:31:59 -0400158 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300159 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
John Hurley064c5d62019-03-22 12:37:35 +0000161
162 m = to_mirred(*a);
163 if (ret == ACT_P_CREATED)
164 INIT_LIST_HEAD(&m->tcfm_list);
165
Davide Carattiff9721d2019-03-20 15:00:04 +0100166 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
167 if (err < 0)
168 goto release_idr;
169
Vlad Buslov653cd282018-08-14 21:46:16 +0300170 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300171
172 if (parm->ifindex) {
Eric Dumazetada066b2021-12-06 17:30:39 -0800173 struct net_device *odev, *ndev;
174
175 ndev = dev_get_by_index(net, parm->ifindex);
176 if (!ndev) {
Vlad Buslov653cd282018-08-14 21:46:16 +0300177 spin_unlock_bh(&m->tcf_lock);
Davide Carattiff9721d2019-03-20 15:00:04 +0100178 err = -ENODEV;
179 goto put_chain;
Vlad Buslov4e232812018-08-10 20:51:53 +0300180 }
Eric Dumazetada066b2021-12-06 17:30:39 -0800181 mac_header_xmit = dev_is_mac_header_xmit(ndev);
182 odev = rcu_replace_pointer(m->tcfm_dev, ndev,
Paul E. McKenney445d3742019-09-23 16:09:18 -0700183 lockdep_is_held(&m->tcf_lock));
Eric Dumazetada066b2021-12-06 17:30:39 -0800184 dev_put_track(odev, &m->tcfm_dev_tracker);
185 netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
Shmulik Ladkani16577922016-10-13 09:06:41 +0300186 m->tcfm_mac_header_xmit = mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Davide Carattiff9721d2019-03-20 15:00:04 +0100188 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
189 m->tcfm_eaction = parm->eaction;
Vlad Buslov653cd282018-08-14 21:46:16 +0300190 spin_unlock_bh(&m->tcf_lock);
Davide Carattiff9721d2019-03-20 15:00:04 +0100191 if (goto_ch)
192 tcf_chain_put_by_act(goto_ch);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700193
stephen hemminger3b879562010-07-22 18:45:04 +0000194 if (ret == ACT_P_CREATED) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300195 spin_lock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000196 list_add(&m->tcfm_list, &mirred_list);
Vlad Buslov4e232812018-08-10 20:51:53 +0300197 spin_unlock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return ret;
Davide Carattiff9721d2019-03-20 15:00:04 +0100201put_chain:
202 if (goto_ch)
203 tcf_chain_put_by_act(goto_ch);
204release_idr:
205 tcf_idr_release(*a, bind);
206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
wenxufa6d6392020-11-25 12:01:22 +0800209static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
210{
211 int err;
212
213 if (!want_ingress)
wenxuc1294122020-11-25 12:01:23 +0800214 err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
wenxufa6d6392020-11-25 12:01:22 +0800215 else
216 err = netif_receive_skb(skb);
217
218 return err;
219}
220
Jamal Hadi Salim7c5790c2018-08-12 09:35:01 -0400221static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
222 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
WANG Conga85a9702016-07-25 16:09:41 -0700224 struct tcf_mirred *m = to_mirred(a);
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200225 struct sk_buff *skb2 = skb;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300226 bool m_mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct net_device *dev;
John Hurleye2ca0702019-06-24 23:13:36 +0100228 unsigned int rec_level;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300229 int retval, err = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200230 bool use_reinsert;
231 bool want_ingress;
232 bool is_redirect;
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200233 bool expects_nh;
Xin Longf799ada2021-11-12 11:33:11 -0500234 bool at_ingress;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300235 int m_eaction;
236 int mac_len;
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200237 bool at_nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
John Hurleye2ca0702019-06-24 23:13:36 +0100239 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 Dumazet2ee22a92015-07-06 05:18:09 -0700247 tcf_lastuse_update(&m->tcf_tm);
Vlad Buslov5e1ad952019-10-30 16:09:01 +0200248 tcf_action_update_bstats(&m->common, skb);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700249
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300250 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
251 m_eaction = READ_ONCE(m->tcfm_eaction);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700252 retval = READ_ONCE(m->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200253 dev = rcu_dereference_bh(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700254 if (unlikely(!dev)) {
255 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000256 goto out;
257 }
258
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700259 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000260 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
261 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800262 goto out;
263 }
264
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200265 /* 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 Longf799ada2021-11-12 11:33:11 -0500270 at_ingress = skb_at_tc_ingress(skb);
271 use_reinsert = at_ingress && is_redirect &&
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200272 tcf_mirred_can_reinsert(retval);
273 if (!use_reinsert) {
274 skb2 = skb_clone(skb, GFP_ATOMIC);
275 if (!skb2)
276 goto out;
277 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800278
Xin Longf799ada2021-11-12 11:33:11 -0500279 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
280
Hangbin Liud09c5482021-08-09 15:04:55 +0800281 /* All mirred/redirected skbs should clear previous ct info */
282 nf_reset_ct(skb2);
Xin Longf799ada2021-11-12 11:33:11 -0500283 if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
284 skb_dst_drop(skb2);
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200285
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 Ladkani53592b32016-10-13 09:06:44 +0300293 skb_pull_rcsum(skb2, mac_len);
294 } else {
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200295 /* target device/action expect data at mac */
296 skb_push_rcsum(skb2, mac_len);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300297 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Eric Dumazet8964be42009-11-20 15:35:04 -0800300 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000301 skb2->dev = dev;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200302
303 /* mirror is always swallowed */
304 if (is_redirect) {
Pablo Neira Ayuso2c646052020-03-25 13:47:18 +0100305 skb_set_redirected(skb2, skb2->tc_at_ingress);
306
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200307 /* let's the caller reinsert the packet, if possible */
308 if (use_reinsert) {
309 res->ingress = want_ingress;
wenxufa6d6392020-11-25 12:01:22 +0800310 err = tcf_mirred_forward(res->ingress, skb);
311 if (err)
Vlad Buslovef816f32019-10-30 16:09:03 +0200312 tcf_action_inc_overlimit_qstats(&m->common);
John Hurleye2ca0702019-06-24 23:13:36 +0100313 __this_cpu_dec(mirred_rec_level);
John Hurley720f22f2019-06-24 23:13:35 +0100314 return TC_ACT_CONSUMED;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200315 }
316 }
317
wenxufa6d6392020-11-25 12:01:22 +0800318 err = tcf_mirred_forward(want_ingress, skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800319 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700320out:
Vlad Buslov26b537a2019-10-30 16:09:02 +0200321 tcf_action_inc_overlimit_qstats(&m->common);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300322 if (tcf_mirred_is_act_redirect(m_eaction))
Jason Wang16c0b162012-08-15 20:44:27 +0000323 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700324 }
John Hurleye2ca0702019-06-24 23:13:36 +0100325 __this_cpu_dec(mirred_rec_level);
Changli Gaofeed1f12009-11-17 04:14:00 -0800326
327 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Po Liu4b61d3e2020-06-19 14:01:07 +0800330static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
331 u64 drops, u64 lastuse, bool hw)
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100332{
Paul Blakey5712bf92016-10-19 17:42:39 +0300333 struct tcf_mirred *m = to_mirred(a);
334 struct tcf_t *tm = &m->tcf_tm;
335
Po Liu4b61d3e2020-06-19 14:01:07 +0800336 tcf_action_update_stats(a, bytes, packets, drops, hw);
Roi Dayan3bb23422017-12-26 07:48:51 +0200337 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100338}
339
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400340static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
341 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700343 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700344 struct tcf_mirred *m = to_mirred(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000345 struct tc_mirred opt = {
346 .index = m->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300347 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
348 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000349 };
Vlad Buslov4e232812018-08-10 20:51:53 +0300350 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct tcf_t t;
352
Vlad Buslov653cd282018-08-14 21:46:16 +0300353 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300354 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. Miller1b34ec42012-03-29 05:11:39 -0400360 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
361 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400362
363 tcf_tm_dump(&t, &m->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200364 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400365 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300366 spin_unlock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return skb->len;
369
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800370nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300371 spin_unlock_bh(&m->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700372 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -1;
374}
375
WANG Congddf97cc2016-02-22 15:57:53 -0800376static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
377 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500378 const struct tc_action_ops *ops,
379 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800380{
381 struct tc_action_net *tn = net_generic(net, mirred_net_id);
382
Alexander Aringb3620142018-02-15 10:54:59 -0500383 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800384}
385
Cong Wangf061b482018-08-29 10:15:35 -0700386static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800387{
388 struct tc_action_net *tn = net_generic(net, mirred_net_id);
389
Chris Mi65a206c2017-08-30 02:31:59 -0400390 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800391}
392
stephen hemminger3b879562010-07-22 18:45:04 +0000393static int mirred_device_event(struct notifier_block *unused,
394 unsigned long event, void *ptr)
395{
Jiri Pirko351638e2013-05-28 01:30:21 +0000396 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000397 struct tcf_mirred *m;
398
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700399 ASSERT_RTNL();
WANG Cong6bd00b82015-10-01 11:37:42 -0700400 if (event == NETDEV_UNREGISTER) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300401 spin_lock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000402 list_for_each_entry(m, &mirred_list, tcfm_list) {
Vlad Buslov653cd282018-08-14 21:46:16 +0300403 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300404 if (tcf_mirred_dev_dereference(m) == dev) {
Eric Dumazetada066b2021-12-06 17:30:39 -0800405 dev_put_track(dev, &m->tcfm_dev_tracker);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700406 /* Note : no rcu grace period necessary, as
407 * net_device are already rcu protected.
408 */
409 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000410 }
Vlad Buslov653cd282018-08-14 21:46:16 +0300411 spin_unlock_bh(&m->tcf_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000412 }
Vlad Buslov4e232812018-08-10 20:51:53 +0300413 spin_unlock(&mirred_list_lock);
WANG Cong6bd00b82015-10-01 11:37:42 -0700414 }
stephen hemminger3b879562010-07-22 18:45:04 +0000415
416 return NOTIFY_DONE;
417}
418
419static struct notifier_block mirred_device_notifier = {
420 .notifier_call = mirred_device_event,
421};
422
Vlad Buslov470d5062019-09-13 18:28:41 +0300423static void tcf_mirred_dev_put(void *priv)
424{
425 struct net_device *dev = priv;
426
427 dev_put(dev);
428}
429
430static struct net_device *
431tcf_mirred_get_dev(const struct tc_action *a,
432 tc_action_priv_destructor *destructor)
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200433{
Jiri Pirko843e79d2017-10-11 09:41:07 +0200434 struct tcf_mirred *m = to_mirred(a);
Vlad Buslov4e232812018-08-10 20:51:53 +0300435 struct net_device *dev;
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200436
Vlad Buslov4e232812018-08-10 20:51:53 +0300437 rcu_read_lock();
438 dev = rcu_dereference(m->tcfm_dev);
Vlad Buslov470d5062019-09-13 18:28:41 +0300439 if (dev) {
Vlad Buslov84a75b32018-08-10 20:51:52 +0300440 dev_hold(dev);
Vlad Buslov470d5062019-09-13 18:28:41 +0300441 *destructor = tcf_mirred_dev_put;
442 }
Vlad Buslov4e232812018-08-10 20:51:53 +0300443 rcu_read_unlock();
Vlad Buslov84a75b32018-08-10 20:51:52 +0300444
445 return dev;
446}
447
Roman Mashakb84b2d42019-06-28 14:30:17 -0400448static 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 Zhengc54e1d92021-12-17 19:16:21 +0100453static 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
462static 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 Zheng8cbfe932021-12-17 19:16:22 +0100485 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 Zhengc54e1d92021-12-17 19:16:21 +0100497 }
498
499 return 0;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static struct tc_action_ops act_mirred_ops = {
503 .kind = "mirred",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200504 .id = TCA_ID_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .owner = THIS_MODULE,
Jamal Hadi Salim7c5790c2018-08-12 09:35:01 -0400506 .act = tcf_mirred_act,
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100507 .stats_update = tcf_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800509 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .init = tcf_mirred_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800511 .walk = tcf_mirred_walker,
512 .lookup = tcf_mirred_search,
Roman Mashakb84b2d42019-06-28 14:30:17 -0400513 .get_fill_size = tcf_mirred_get_fill_size,
Baowen Zhengc54e1d92021-12-17 19:16:21 +0100514 .offload_act_setup = tcf_mirred_offload_act_setup,
WANG Conga85a9702016-07-25 16:09:41 -0700515 .size = sizeof(struct tcf_mirred),
Jiri Pirko843e79d2017-10-11 09:41:07 +0200516 .get_dev = tcf_mirred_get_dev,
WANG Congddf97cc2016-02-22 15:57:53 -0800517};
518
519static __net_init int mirred_init_net(struct net *net)
520{
521 struct tc_action_net *tn = net_generic(net, mirred_net_id);
522
Cong Wang981471b2019-08-25 10:01:32 -0700523 return tc_action_net_init(net, tn, &act_mirred_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800524}
525
Cong Wang039af9c2017-12-11 15:35:03 -0800526static void __net_exit mirred_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800527{
Cong Wang039af9c2017-12-11 15:35:03 -0800528 tc_action_net_exit(net_list, mirred_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800529}
530
531static struct pernet_operations mirred_net_ops = {
532 .init = mirred_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800533 .exit_batch = mirred_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800534 .id = &mirred_net_id,
535 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536};
537
538MODULE_AUTHOR("Jamal Hadi Salim(2002)");
539MODULE_DESCRIPTION("Device Mirror/redirect actions");
540MODULE_LICENSE("GPL");
541
David S. Millere9ce1cd2006-08-21 23:54:55 -0700542static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
stephen hemminger3b879562010-07-22 18:45:04 +0000544 int err = register_netdevice_notifier(&mirred_device_notifier);
545 if (err)
546 return err;
547
stephen hemminger6ff9c362010-05-12 06:37:05 +0000548 pr_info("Mirror/redirect action on\n");
YueHaibing11c9a7d2019-10-09 11:10:52 +0800549 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 Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
David S. Millere9ce1cd2006-08-21 23:54:55 -0700556static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
WANG Congddf97cc2016-02-22 15:57:53 -0800558 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800559 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562module_init(mirred_init_module);
563module_exit(mirred_cleanup_module);