blob: 1ad300e6dbc0a0bfad2c278101e6f3446752ec2e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/pkt_sched.h>
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020023#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/tc_act/tc_mirred.h>
25#include <net/tc_act/tc_mirred.h>
26
stephen hemminger3b879562010-07-22 18:45:04 +000027static LIST_HEAD(mirred_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030028static DEFINE_SPINLOCK(mirred_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
John Hurleye2ca0702019-06-24 23:13:36 +010030#define MIRRED_RECURSION_LIMIT 4
31static DEFINE_PER_CPU(unsigned int, mirred_rec_level);
32
Shmulik Ladkani53592b32016-10-13 09:06:44 +030033static bool tcf_mirred_is_act_redirect(int action)
34{
35 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
36}
37
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050038static bool tcf_mirred_act_wants_ingress(int action)
Shmulik Ladkani53592b32016-10-13 09:06:44 +030039{
40 switch (action) {
41 case TCA_EGRESS_REDIR:
42 case TCA_EGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050043 return false;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030044 case TCA_INGRESS_REDIR:
45 case TCA_INGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050046 return true;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030047 default:
48 BUG();
49 }
50}
51
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020052static bool tcf_mirred_can_reinsert(int action)
53{
54 switch (action) {
55 case TC_ACT_SHOT:
56 case TC_ACT_STOLEN:
57 case TC_ACT_QUEUED:
58 case TC_ACT_TRAP:
59 return true;
60 }
61 return false;
62}
63
Vlad Buslov4e232812018-08-10 20:51:53 +030064static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
65{
66 return rcu_dereference_protected(m->tcfm_dev,
67 lockdep_is_held(&m->tcf_lock));
68}
69
Cong Wang9a63b252017-12-05 12:53:07 -080070static void tcf_mirred_release(struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
WANG Cong86062032014-02-11 17:07:31 -080072 struct tcf_mirred *m = to_mirred(a);
WANG Congdc327f82016-05-16 15:11:18 -070073 struct net_device *dev;
Eric Dumazet2ee22a92015-07-06 05:18:09 -070074
Vlad Buslov4e232812018-08-10 20:51:53 +030075 spin_lock(&mirred_list_lock);
WANG Conga5b5c952014-02-11 17:07:32 -080076 list_del(&m->tcfm_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030077 spin_unlock(&mirred_list_lock);
78
79 /* last reference to action, no need to lock */
80 dev = rcu_dereference_protected(m->tcfm_dev, 1);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070081 if (dev)
82 dev_put(dev);
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,
94 int ovr, int bind, bool rtnl_held,
Davide Caratti85d09662019-03-20 14:59:59 +010095 struct tcf_proto *tp,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020096 u32 flags, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
WANG Congddf97cc2016-02-22 15:57:53 -080098 struct tc_action_net *tn = net_generic(net, mirred_net_id);
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;
Changli Gaob76965e2009-11-17 04:15:16 -0800104 struct net_device *dev;
WANG Congb2313072016-06-13 13:46:28 -0700105 bool exists = false;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300106 int ret, err;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000107 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alexander Aring1d4760c2018-02-15 10:55:00 -0500109 if (!nla) {
110 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500112 }
Johannes Berg8cb08172019-04-26 14:07:28 +0200113 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
114 mirred_policy, extack);
Changli Gaob76965e2009-11-17 04:15:16 -0800115 if (ret < 0)
116 return ret;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500117 if (!tb[TCA_MIRRED_PARMS]) {
118 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500120 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800121 parm = nla_data(tb[TCA_MIRRED_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000122 index = parm->index;
123 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300124 if (err < 0)
125 return err;
126 exists = err;
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400127 if (exists && bind)
128 return 0;
129
Changli Gaob76965e2009-11-17 04:15:16 -0800130 switch (parm->eaction) {
131 case TCA_EGRESS_MIRROR:
132 case TCA_EGRESS_REDIR:
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300133 case TCA_INGRESS_REDIR:
134 case TCA_INGRESS_MIRROR:
Changli Gaob76965e2009-11-17 04:15:16 -0800135 break;
136 default:
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400137 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400138 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300139 else
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000140 tcf_idr_cleanup(tn, index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500141 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
Changli Gaob76965e2009-11-17 04:15:16 -0800142 return -EINVAL;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400145 if (!exists) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300146 if (!parm->ifindex) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000147 tcf_idr_cleanup(tn, index);
Alexander Aring1d4760c2018-02-15 10:55:00 -0500148 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500150 }
Vlad Buslove3822672019-10-30 16:09:06 +0200151 ret = tcf_idr_create_from_flags(tn, index, est, a,
152 &act_mirred_ops, bind, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300153 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000154 tcf_idr_cleanup(tn, index);
WANG Cong86062032014-02-11 17:07:31 -0800155 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300158 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400159 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300160 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
John Hurley064c5d62019-03-22 12:37:35 +0000162
163 m = to_mirred(*a);
164 if (ret == ACT_P_CREATED)
165 INIT_LIST_HEAD(&m->tcfm_list);
166
Davide Carattiff9721d2019-03-20 15:00:04 +0100167 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
168 if (err < 0)
169 goto release_idr;
170
Vlad Buslov653cd282018-08-14 21:46:16 +0300171 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300172
173 if (parm->ifindex) {
174 dev = dev_get_by_index(net, parm->ifindex);
175 if (!dev) {
Vlad Buslov653cd282018-08-14 21:46:16 +0300176 spin_unlock_bh(&m->tcf_lock);
Davide Carattiff9721d2019-03-20 15:00:04 +0100177 err = -ENODEV;
178 goto put_chain;
Vlad Buslov4e232812018-08-10 20:51:53 +0300179 }
180 mac_header_xmit = dev_is_mac_header_xmit(dev);
Paul E. McKenney445d3742019-09-23 16:09:18 -0700181 dev = rcu_replace_pointer(m->tcfm_dev, dev,
182 lockdep_is_held(&m->tcf_lock));
Vlad Buslov4e232812018-08-10 20:51:53 +0300183 if (dev)
184 dev_put(dev);
Shmulik Ladkani16577922016-10-13 09:06:41 +0300185 m->tcfm_mac_header_xmit = mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Davide Carattiff9721d2019-03-20 15:00:04 +0100187 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
188 m->tcfm_eaction = parm->eaction;
Vlad Buslov653cd282018-08-14 21:46:16 +0300189 spin_unlock_bh(&m->tcf_lock);
Davide Carattiff9721d2019-03-20 15:00:04 +0100190 if (goto_ch)
191 tcf_chain_put_by_act(goto_ch);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700192
stephen hemminger3b879562010-07-22 18:45:04 +0000193 if (ret == ACT_P_CREATED) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300194 spin_lock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000195 list_add(&m->tcfm_list, &mirred_list);
Vlad Buslov4e232812018-08-10 20:51:53 +0300196 spin_unlock(&mirred_list_lock);
197
Chris Mi65a206c2017-08-30 02:31:59 -0400198 tcf_idr_insert(tn, *a);
stephen hemminger3b879562010-07-22 18:45:04 +0000199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return ret;
Davide Carattiff9721d2019-03-20 15:00:04 +0100202put_chain:
203 if (goto_ch)
204 tcf_chain_put_by_act(goto_ch);
205release_idr:
206 tcf_idr_release(*a, bind);
207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Jamal Hadi Salim7c5790c2018-08-12 09:35:01 -0400210static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
211 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
WANG Conga85a9702016-07-25 16:09:41 -0700213 struct tcf_mirred *m = to_mirred(a);
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200214 struct sk_buff *skb2 = skb;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300215 bool m_mac_header_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct net_device *dev;
John Hurleye2ca0702019-06-24 23:13:36 +0100217 unsigned int rec_level;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300218 int retval, err = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200219 bool use_reinsert;
220 bool want_ingress;
221 bool is_redirect;
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200222 bool expects_nh;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300223 int m_eaction;
224 int mac_len;
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200225 bool at_nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
John Hurleye2ca0702019-06-24 23:13:36 +0100227 rec_level = __this_cpu_inc_return(mirred_rec_level);
228 if (unlikely(rec_level > MIRRED_RECURSION_LIMIT)) {
229 net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
230 netdev_name(skb->dev));
231 __this_cpu_dec(mirred_rec_level);
232 return TC_ACT_SHOT;
233 }
234
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700235 tcf_lastuse_update(&m->tcf_tm);
Vlad Buslov5e1ad952019-10-30 16:09:01 +0200236 tcf_action_update_bstats(&m->common, skb);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700237
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300238 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
239 m_eaction = READ_ONCE(m->tcfm_eaction);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700240 retval = READ_ONCE(m->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200241 dev = rcu_dereference_bh(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700242 if (unlikely(!dev)) {
243 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000244 goto out;
245 }
246
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700247 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000248 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
249 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800250 goto out;
251 }
252
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200253 /* we could easily avoid the clone only if called by ingress and clsact;
254 * since we can't easily detect the clsact caller, skip clone only for
255 * ingress - that covers the TC S/W datapath.
256 */
257 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
258 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
259 tcf_mirred_can_reinsert(retval);
260 if (!use_reinsert) {
261 skb2 = skb_clone(skb, GFP_ATOMIC);
262 if (!skb2)
263 goto out;
264 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800265
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200266 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200267
268 expects_nh = want_ingress || !m_mac_header_xmit;
269 at_nh = skb->data == skb_network_header(skb);
270 if (at_nh != expects_nh) {
271 mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
272 skb_network_header(skb) - skb_mac_header(skb);
273 if (expects_nh) {
274 /* target device/action expect data at nh */
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300275 skb_pull_rcsum(skb2, mac_len);
276 } else {
Shmulik Ladkani70cf3dc2019-12-25 10:51:01 +0200277 /* target device/action expect data at mac */
278 skb_push_rcsum(skb2, mac_len);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300279 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Eric Dumazet8964be42009-11-20 15:35:04 -0800282 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000283 skb2->dev = dev;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200284
285 /* mirror is always swallowed */
286 if (is_redirect) {
287 skb2->tc_redirected = 1;
288 skb2->tc_from_ingress = skb2->tc_at_ingress;
Eric Dumazet7236ead2018-11-10 16:22:29 -0800289 if (skb2->tc_from_ingress)
290 skb2->tstamp = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200291 /* let's the caller reinsert the packet, if possible */
292 if (use_reinsert) {
293 res->ingress = want_ingress;
Vlad Buslovef816f32019-10-30 16:09:03 +0200294 if (skb_tc_reinsert(skb, res))
295 tcf_action_inc_overlimit_qstats(&m->common);
John Hurleye2ca0702019-06-24 23:13:36 +0100296 __this_cpu_dec(mirred_rec_level);
John Hurley720f22f2019-06-24 23:13:35 +0100297 return TC_ACT_CONSUMED;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200298 }
299 }
300
301 if (!want_ingress)
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300302 err = dev_queue_xmit(skb2);
303 else
304 err = netif_receive_skb(skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800305
Changli Gaofeed1f12009-11-17 04:14:00 -0800306 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700307out:
Vlad Buslov26b537a2019-10-30 16:09:02 +0200308 tcf_action_inc_overlimit_qstats(&m->common);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300309 if (tcf_mirred_is_act_redirect(m_eaction))
Jason Wang16c0b162012-08-15 20:44:27 +0000310 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700311 }
John Hurleye2ca0702019-06-24 23:13:36 +0100312 __this_cpu_dec(mirred_rec_level);
Changli Gaofeed1f12009-11-17 04:14:00 -0800313
314 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100317static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400318 u64 lastuse, bool hw)
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100319{
Paul Blakey5712bf92016-10-19 17:42:39 +0300320 struct tcf_mirred *m = to_mirred(a);
321 struct tcf_t *tm = &m->tcf_tm;
322
Vlad Buslovc8ecebd2019-10-30 16:09:00 +0200323 tcf_action_update_stats(a, bytes, packets, false, hw);
Roi Dayan3bb23422017-12-26 07:48:51 +0200324 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100325}
326
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400327static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
328 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700330 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700331 struct tcf_mirred *m = to_mirred(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000332 struct tc_mirred opt = {
333 .index = m->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300334 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
335 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000336 };
Vlad Buslov4e232812018-08-10 20:51:53 +0300337 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct tcf_t t;
339
Vlad Buslov653cd282018-08-14 21:46:16 +0300340 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300341 opt.action = m->tcf_action;
342 opt.eaction = m->tcfm_eaction;
343 dev = tcf_mirred_dev_dereference(m);
344 if (dev)
345 opt.ifindex = dev->ifindex;
346
David S. Miller1b34ec42012-03-29 05:11:39 -0400347 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
348 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400349
350 tcf_tm_dump(&t, &m->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200351 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400352 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300353 spin_unlock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return skb->len;
356
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800357nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300358 spin_unlock_bh(&m->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700359 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -1;
361}
362
WANG Congddf97cc2016-02-22 15:57:53 -0800363static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
364 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500365 const struct tc_action_ops *ops,
366 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800367{
368 struct tc_action_net *tn = net_generic(net, mirred_net_id);
369
Alexander Aringb3620142018-02-15 10:54:59 -0500370 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800371}
372
Cong Wangf061b482018-08-29 10:15:35 -0700373static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800374{
375 struct tc_action_net *tn = net_generic(net, mirred_net_id);
376
Chris Mi65a206c2017-08-30 02:31:59 -0400377 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800378}
379
stephen hemminger3b879562010-07-22 18:45:04 +0000380static int mirred_device_event(struct notifier_block *unused,
381 unsigned long event, void *ptr)
382{
Jiri Pirko351638e2013-05-28 01:30:21 +0000383 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000384 struct tcf_mirred *m;
385
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700386 ASSERT_RTNL();
WANG Cong6bd00b82015-10-01 11:37:42 -0700387 if (event == NETDEV_UNREGISTER) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300388 spin_lock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000389 list_for_each_entry(m, &mirred_list, tcfm_list) {
Vlad Buslov653cd282018-08-14 21:46:16 +0300390 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300391 if (tcf_mirred_dev_dereference(m) == dev) {
stephen hemminger3b879562010-07-22 18:45:04 +0000392 dev_put(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700393 /* Note : no rcu grace period necessary, as
394 * net_device are already rcu protected.
395 */
396 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000397 }
Vlad Buslov653cd282018-08-14 21:46:16 +0300398 spin_unlock_bh(&m->tcf_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000399 }
Vlad Buslov4e232812018-08-10 20:51:53 +0300400 spin_unlock(&mirred_list_lock);
WANG Cong6bd00b82015-10-01 11:37:42 -0700401 }
stephen hemminger3b879562010-07-22 18:45:04 +0000402
403 return NOTIFY_DONE;
404}
405
406static struct notifier_block mirred_device_notifier = {
407 .notifier_call = mirred_device_event,
408};
409
Vlad Buslov470d5062019-09-13 18:28:41 +0300410static void tcf_mirred_dev_put(void *priv)
411{
412 struct net_device *dev = priv;
413
414 dev_put(dev);
415}
416
417static struct net_device *
418tcf_mirred_get_dev(const struct tc_action *a,
419 tc_action_priv_destructor *destructor)
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200420{
Jiri Pirko843e79d2017-10-11 09:41:07 +0200421 struct tcf_mirred *m = to_mirred(a);
Vlad Buslov4e232812018-08-10 20:51:53 +0300422 struct net_device *dev;
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200423
Vlad Buslov4e232812018-08-10 20:51:53 +0300424 rcu_read_lock();
425 dev = rcu_dereference(m->tcfm_dev);
Vlad Buslov470d5062019-09-13 18:28:41 +0300426 if (dev) {
Vlad Buslov84a75b32018-08-10 20:51:52 +0300427 dev_hold(dev);
Vlad Buslov470d5062019-09-13 18:28:41 +0300428 *destructor = tcf_mirred_dev_put;
429 }
Vlad Buslov4e232812018-08-10 20:51:53 +0300430 rcu_read_unlock();
Vlad Buslov84a75b32018-08-10 20:51:52 +0300431
432 return dev;
433}
434
Roman Mashakb84b2d42019-06-28 14:30:17 -0400435static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
436{
437 return nla_total_size(sizeof(struct tc_mirred));
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static struct tc_action_ops act_mirred_ops = {
441 .kind = "mirred",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200442 .id = TCA_ID_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .owner = THIS_MODULE,
Jamal Hadi Salim7c5790c2018-08-12 09:35:01 -0400444 .act = tcf_mirred_act,
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100445 .stats_update = tcf_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800447 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .init = tcf_mirred_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800449 .walk = tcf_mirred_walker,
450 .lookup = tcf_mirred_search,
Roman Mashakb84b2d42019-06-28 14:30:17 -0400451 .get_fill_size = tcf_mirred_get_fill_size,
WANG Conga85a9702016-07-25 16:09:41 -0700452 .size = sizeof(struct tcf_mirred),
Jiri Pirko843e79d2017-10-11 09:41:07 +0200453 .get_dev = tcf_mirred_get_dev,
WANG Congddf97cc2016-02-22 15:57:53 -0800454};
455
456static __net_init int mirred_init_net(struct net *net)
457{
458 struct tc_action_net *tn = net_generic(net, mirred_net_id);
459
Cong Wang981471bd2019-08-25 10:01:32 -0700460 return tc_action_net_init(net, tn, &act_mirred_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800461}
462
Cong Wang039af9c2017-12-11 15:35:03 -0800463static void __net_exit mirred_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800464{
Cong Wang039af9c2017-12-11 15:35:03 -0800465 tc_action_net_exit(net_list, mirred_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800466}
467
468static struct pernet_operations mirred_net_ops = {
469 .init = mirred_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800470 .exit_batch = mirred_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800471 .id = &mirred_net_id,
472 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
475MODULE_AUTHOR("Jamal Hadi Salim(2002)");
476MODULE_DESCRIPTION("Device Mirror/redirect actions");
477MODULE_LICENSE("GPL");
478
David S. Millere9ce1cd2006-08-21 23:54:55 -0700479static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
stephen hemminger3b879562010-07-22 18:45:04 +0000481 int err = register_netdevice_notifier(&mirred_device_notifier);
482 if (err)
483 return err;
484
stephen hemminger6ff9c362010-05-12 06:37:05 +0000485 pr_info("Mirror/redirect action on\n");
YueHaibing11c9a7d2019-10-09 11:10:52 +0800486 err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
487 if (err)
488 unregister_netdevice_notifier(&mirred_device_notifier);
489
490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
David S. Millere9ce1cd2006-08-21 23:54:55 -0700493static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
WANG Congddf97cc2016-02-22 15:57:53 -0800495 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800496 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499module_init(mirred_init_module);
500module_exit(mirred_cleanup_module);