blob: 17cc6bd4c57c3a6f12786c3d1109c6e48af185dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01002 * net/sched/act_mirred.c packet mirroring and redirect actions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jamal Hadi Salim (2002-4)
10 *
11 * TODO: Add ingress support (and socket redirect support)
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
20#include <linux/rtnetlink.h>
21#include <linux/module.h>
22#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Daniel Borkmannc4916802016-11-26 01:28:06 +010024#include <linux/if_arp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070025#include <net/net_namespace.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070026#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/pkt_sched.h>
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020028#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/tc_act/tc_mirred.h>
30#include <net/tc_act/tc_mirred.h>
31
stephen hemminger3b879562010-07-22 18:45:04 +000032static LIST_HEAD(mirred_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030033static DEFINE_SPINLOCK(mirred_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Shmulik Ladkani53592b32016-10-13 09:06:44 +030035static bool tcf_mirred_is_act_redirect(int action)
36{
37 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
38}
39
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050040static bool tcf_mirred_act_wants_ingress(int action)
Shmulik Ladkani53592b32016-10-13 09:06:44 +030041{
42 switch (action) {
43 case TCA_EGRESS_REDIR:
44 case TCA_EGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050045 return false;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030046 case TCA_INGRESS_REDIR:
47 case TCA_INGRESS_MIRROR:
Willem de Bruijn8dc07fd2017-01-07 17:06:37 -050048 return true;
Shmulik Ladkani53592b32016-10-13 09:06:44 +030049 default:
50 BUG();
51 }
52}
53
Paolo Abenie5cf1ba2018-07-30 14:30:45 +020054static bool tcf_mirred_can_reinsert(int action)
55{
56 switch (action) {
57 case TC_ACT_SHOT:
58 case TC_ACT_STOLEN:
59 case TC_ACT_QUEUED:
60 case TC_ACT_TRAP:
61 return true;
62 }
63 return false;
64}
65
Vlad Buslov4e232812018-08-10 20:51:53 +030066static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
67{
68 return rcu_dereference_protected(m->tcfm_dev,
69 lockdep_is_held(&m->tcf_lock));
70}
71
Cong Wang9a63b252017-12-05 12:53:07 -080072static void tcf_mirred_release(struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
WANG Cong86062032014-02-11 17:07:31 -080074 struct tcf_mirred *m = to_mirred(a);
WANG Congdc327f82016-05-16 15:11:18 -070075 struct net_device *dev;
Eric Dumazet2ee22a92015-07-06 05:18:09 -070076
Vlad Buslov4e232812018-08-10 20:51:53 +030077 spin_lock(&mirred_list_lock);
WANG Conga5b5c952014-02-11 17:07:32 -080078 list_del(&m->tcfm_list);
Vlad Buslov4e232812018-08-10 20:51:53 +030079 spin_unlock(&mirred_list_lock);
80
81 /* last reference to action, no need to lock */
82 dev = rcu_dereference_protected(m->tcfm_dev, 1);
Eric Dumazet2ee22a92015-07-06 05:18:09 -070083 if (dev)
84 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Patrick McHardy53b2bf32008-01-23 20:36:30 -080087static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
88 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
89};
90
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030091static unsigned int mirred_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070092static struct tc_action_ops act_mirred_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080093
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000094static int tcf_mirred_init(struct net *net, struct nlattr *nla,
Vlad Buslov789871b2018-07-05 17:24:25 +030095 struct nlattr *est, struct tc_action **a,
96 int ovr, int bind, bool rtnl_held,
Davide Caratti85d09662019-03-20 14:59:59 +010097 struct tcf_proto *tp,
Vlad Buslov789871b2018-07-05 17:24:25 +030098 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
WANG Congddf97cc2016-02-22 15:57:53 -0800100 struct tc_action_net *tn = net_generic(net, mirred_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800101 struct nlattr *tb[TCA_MIRRED_MAX + 1];
Davide Carattiff9721d2019-03-20 15:00:04 +0100102 struct tcf_chain *goto_ch = NULL;
Shmulik Ladkani16577922016-10-13 09:06:41 +0300103 bool mac_header_xmit = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct tc_mirred *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 struct tcf_mirred *m;
Changli Gaob76965e2009-11-17 04:15:16 -0800106 struct net_device *dev;
WANG Congb2313072016-06-13 13:46:28 -0700107 bool exists = false;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300108 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alexander Aring1d4760c2018-02-15 10:55:00 -0500110 if (!nla) {
111 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -EINVAL;
Alexander Aring1d4760c2018-02-15 10:55:00 -0500113 }
114 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, 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]);
Jamal Hadi Salim87dfbdc2016-05-10 16:49:28 -0400122
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300123 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
124 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
140 tcf_idr_cleanup(tn, parm->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) {
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300147 tcf_idr_cleanup(tn, parm->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 }
Chris Mi65a206c2017-08-30 02:31:59 -0400151 ret = tcf_idr_create(tn, parm->index, est, a,
152 &act_mirred_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300153 if (ret) {
154 tcf_idr_cleanup(tn, parm->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);
181 rcu_swap_protected(m->tcfm_dev, dev,
182 lockdep_is_held(&m->tcf_lock));
183 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;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300217 int retval, err = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200218 bool use_reinsert;
219 bool want_ingress;
220 bool is_redirect;
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300221 int m_eaction;
222 int mac_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700224 tcf_lastuse_update(&m->tcf_tm);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700225 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
226
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300227 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
228 m_eaction = READ_ONCE(m->tcfm_eaction);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700229 retval = READ_ONCE(m->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200230 dev = rcu_dereference_bh(m->tcfm_dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700231 if (unlikely(!dev)) {
232 pr_notice_once("tc mirred: target device is gone\n");
stephen hemminger3b879562010-07-22 18:45:04 +0000233 goto out;
234 }
235
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700236 if (unlikely(!(dev->flags & IFF_UP))) {
Joe Perchese87cc472012-05-13 21:56:26 +0000237 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
238 dev->name);
Changli Gaofeed1f12009-11-17 04:14:00 -0800239 goto out;
240 }
241
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200242 /* we could easily avoid the clone only if called by ingress and clsact;
243 * since we can't easily detect the clsact caller, skip clone only for
244 * ingress - that covers the TC S/W datapath.
245 */
246 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
247 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
248 tcf_mirred_can_reinsert(retval);
249 if (!use_reinsert) {
250 skb2 = skb_clone(skb, GFP_ATOMIC);
251 if (!skb2)
252 goto out;
253 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800254
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300255 /* If action's target direction differs than filter's direction,
256 * and devices expect a mac header on xmit, then mac push/pull is
257 * needed.
258 */
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200259 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
260 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500261 if (!skb_at_tc_ingress(skb)) {
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300262 /* caught at egress, act ingress: pull mac */
263 mac_len = skb_network_header(skb) - skb_mac_header(skb);
264 skb_pull_rcsum(skb2, mac_len);
265 } else {
266 /* caught at ingress, act egress: push mac */
WANG Cong82a31b92016-06-30 10:15:22 -0700267 skb_push_rcsum(skb2, skb->mac_len);
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300268 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Eric Dumazet8964be42009-11-20 15:35:04 -0800271 skb2->skb_iif = skb->dev->ifindex;
Changli Gao210d6de2010-06-24 16:25:12 +0000272 skb2->dev = dev;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200273
274 /* mirror is always swallowed */
275 if (is_redirect) {
276 skb2->tc_redirected = 1;
277 skb2->tc_from_ingress = skb2->tc_at_ingress;
Eric Dumazet7236ead2018-11-10 16:22:29 -0800278 if (skb2->tc_from_ingress)
279 skb2->tstamp = 0;
Paolo Abenie5cf1ba2018-07-30 14:30:45 +0200280 /* let's the caller reinsert the packet, if possible */
281 if (use_reinsert) {
282 res->ingress = want_ingress;
283 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
284 return TC_ACT_REINSERT;
285 }
286 }
287
288 if (!want_ingress)
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300289 err = dev_queue_xmit(skb2);
290 else
291 err = netif_receive_skb(skb2);
Changli Gaofeed1f12009-11-17 04:14:00 -0800292
Changli Gaofeed1f12009-11-17 04:14:00 -0800293 if (err) {
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700294out:
295 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
Shmulik Ladkani53592b32016-10-13 09:06:44 +0300296 if (tcf_mirred_is_act_redirect(m_eaction))
Jason Wang16c0b162012-08-15 20:44:27 +0000297 retval = TC_ACT_SHOT;
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700298 }
Changli Gaofeed1f12009-11-17 04:14:00 -0800299
300 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100303static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400304 u64 lastuse, bool hw)
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100305{
Paul Blakey5712bf92016-10-19 17:42:39 +0300306 struct tcf_mirred *m = to_mirred(a);
307 struct tcf_t *tm = &m->tcf_tm;
308
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100309 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400310 if (hw)
311 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
312 bytes, packets);
Roi Dayan3bb23422017-12-26 07:48:51 +0200313 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100314}
315
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400316static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
317 int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700319 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700320 struct tcf_mirred *m = to_mirred(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000321 struct tc_mirred opt = {
322 .index = m->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300323 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
324 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000325 };
Vlad Buslov4e232812018-08-10 20:51:53 +0300326 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct tcf_t t;
328
Vlad Buslov653cd282018-08-14 21:46:16 +0300329 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300330 opt.action = m->tcf_action;
331 opt.eaction = m->tcfm_eaction;
332 dev = tcf_mirred_dev_dereference(m);
333 if (dev)
334 opt.ifindex = dev->ifindex;
335
David S. Miller1b34ec42012-03-29 05:11:39 -0400336 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
337 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400338
339 tcf_tm_dump(&t, &m->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200340 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400341 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300342 spin_unlock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return skb->len;
345
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800346nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300347 spin_unlock_bh(&m->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700348 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -1;
350}
351
WANG Congddf97cc2016-02-22 15:57:53 -0800352static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
353 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500354 const struct tc_action_ops *ops,
355 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800356{
357 struct tc_action_net *tn = net_generic(net, mirred_net_id);
358
Alexander Aringb3620142018-02-15 10:54:59 -0500359 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800360}
361
Cong Wangf061b482018-08-29 10:15:35 -0700362static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800363{
364 struct tc_action_net *tn = net_generic(net, mirred_net_id);
365
Chris Mi65a206c2017-08-30 02:31:59 -0400366 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800367}
368
stephen hemminger3b879562010-07-22 18:45:04 +0000369static int mirred_device_event(struct notifier_block *unused,
370 unsigned long event, void *ptr)
371{
Jiri Pirko351638e2013-05-28 01:30:21 +0000372 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
stephen hemminger3b879562010-07-22 18:45:04 +0000373 struct tcf_mirred *m;
374
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700375 ASSERT_RTNL();
WANG Cong6bd00b82015-10-01 11:37:42 -0700376 if (event == NETDEV_UNREGISTER) {
Vlad Buslov4e232812018-08-10 20:51:53 +0300377 spin_lock(&mirred_list_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000378 list_for_each_entry(m, &mirred_list, tcfm_list) {
Vlad Buslov653cd282018-08-14 21:46:16 +0300379 spin_lock_bh(&m->tcf_lock);
Vlad Buslov4e232812018-08-10 20:51:53 +0300380 if (tcf_mirred_dev_dereference(m) == dev) {
stephen hemminger3b879562010-07-22 18:45:04 +0000381 dev_put(dev);
Eric Dumazet2ee22a92015-07-06 05:18:09 -0700382 /* Note : no rcu grace period necessary, as
383 * net_device are already rcu protected.
384 */
385 RCU_INIT_POINTER(m->tcfm_dev, NULL);
stephen hemminger3b879562010-07-22 18:45:04 +0000386 }
Vlad Buslov653cd282018-08-14 21:46:16 +0300387 spin_unlock_bh(&m->tcf_lock);
stephen hemminger3b879562010-07-22 18:45:04 +0000388 }
Vlad Buslov4e232812018-08-10 20:51:53 +0300389 spin_unlock(&mirred_list_lock);
WANG Cong6bd00b82015-10-01 11:37:42 -0700390 }
stephen hemminger3b879562010-07-22 18:45:04 +0000391
392 return NOTIFY_DONE;
393}
394
395static struct notifier_block mirred_device_notifier = {
396 .notifier_call = mirred_device_event,
397};
398
Jiri Pirko843e79d2017-10-11 09:41:07 +0200399static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200400{
Jiri Pirko843e79d2017-10-11 09:41:07 +0200401 struct tcf_mirred *m = to_mirred(a);
Vlad Buslov4e232812018-08-10 20:51:53 +0300402 struct net_device *dev;
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200403
Vlad Buslov4e232812018-08-10 20:51:53 +0300404 rcu_read_lock();
405 dev = rcu_dereference(m->tcfm_dev);
Vlad Buslov84a75b32018-08-10 20:51:52 +0300406 if (dev)
407 dev_hold(dev);
Vlad Buslov4e232812018-08-10 20:51:53 +0300408 rcu_read_unlock();
Vlad Buslov84a75b32018-08-10 20:51:52 +0300409
410 return dev;
411}
412
413static void tcf_mirred_put_dev(struct net_device *dev)
414{
415 dev_put(dev);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static struct tc_action_ops act_mirred_ops = {
419 .kind = "mirred",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200420 .id = TCA_ID_MIRRED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 .owner = THIS_MODULE,
Jamal Hadi Salim7c5790c2018-08-12 09:35:01 -0400422 .act = tcf_mirred_act,
Jakub Kicinski9798e6f2016-09-21 11:44:05 +0100423 .stats_update = tcf_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .dump = tcf_mirred_dump,
WANG Cong86062032014-02-11 17:07:31 -0800425 .cleanup = tcf_mirred_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .init = tcf_mirred_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800427 .walk = tcf_mirred_walker,
428 .lookup = tcf_mirred_search,
WANG Conga85a9702016-07-25 16:09:41 -0700429 .size = sizeof(struct tcf_mirred),
Jiri Pirko843e79d2017-10-11 09:41:07 +0200430 .get_dev = tcf_mirred_get_dev,
Vlad Buslov84a75b32018-08-10 20:51:52 +0300431 .put_dev = tcf_mirred_put_dev,
WANG Congddf97cc2016-02-22 15:57:53 -0800432};
433
434static __net_init int mirred_init_net(struct net *net)
435{
436 struct tc_action_net *tn = net_generic(net, mirred_net_id);
437
Cong Wangc7e460c2017-11-06 13:47:18 -0800438 return tc_action_net_init(tn, &act_mirred_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800439}
440
Cong Wang039af9c2017-12-11 15:35:03 -0800441static void __net_exit mirred_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800442{
Cong Wang039af9c2017-12-11 15:35:03 -0800443 tc_action_net_exit(net_list, mirred_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800444}
445
446static struct pernet_operations mirred_net_ops = {
447 .init = mirred_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800448 .exit_batch = mirred_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800449 .id = &mirred_net_id,
450 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
453MODULE_AUTHOR("Jamal Hadi Salim(2002)");
454MODULE_DESCRIPTION("Device Mirror/redirect actions");
455MODULE_LICENSE("GPL");
456
David S. Millere9ce1cd2006-08-21 23:54:55 -0700457static int __init mirred_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
stephen hemminger3b879562010-07-22 18:45:04 +0000459 int err = register_netdevice_notifier(&mirred_device_notifier);
460 if (err)
461 return err;
462
stephen hemminger6ff9c362010-05-12 06:37:05 +0000463 pr_info("Mirror/redirect action on\n");
WANG Congddf97cc2016-02-22 15:57:53 -0800464 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
David S. Millere9ce1cd2006-08-21 23:54:55 -0700467static void __exit mirred_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
WANG Congddf97cc2016-02-22 15:57:53 -0800469 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
WANG Cong568a1532013-12-20 00:08:51 -0800470 unregister_netdevice_notifier(&mirred_device_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473module_init(mirred_init_module);
474module_exit(mirred_cleanup_module);