Merge tag 'drm-misc-fixes-2019-06-13' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-block.git] / net / sched / act_mirred.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
0c6965dd 3 * net/sched/act_mirred.c packet mirroring and redirect actions
1da177e4 4 *
1da177e4
LT
5 * Authors: Jamal Hadi Salim (2002-4)
6 *
7 * TODO: Add ingress support (and socket redirect support)
1da177e4
LT
8 */
9
1da177e4
LT
10#include <linux/types.h>
11#include <linux/kernel.h>
1da177e4 12#include <linux/string.h>
1da177e4 13#include <linux/errno.h>
1da177e4
LT
14#include <linux/skbuff.h>
15#include <linux/rtnetlink.h>
16#include <linux/module.h>
17#include <linux/init.h>
5a0e3ad6 18#include <linux/gfp.h>
c491680f 19#include <linux/if_arp.h>
881d966b 20#include <net/net_namespace.h>
dc5fc579 21#include <net/netlink.h>
1da177e4 22#include <net/pkt_sched.h>
e5cf1baf 23#include <net/pkt_cls.h>
1da177e4
LT
24#include <linux/tc_act/tc_mirred.h>
25#include <net/tc_act/tc_mirred.h>
26
3b87956e 27static LIST_HEAD(mirred_list);
4e232818 28static DEFINE_SPINLOCK(mirred_list_lock);
1da177e4 29
53592b36
SL
30static bool tcf_mirred_is_act_redirect(int action)
31{
32 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
33}
34
8dc07fdb 35static bool tcf_mirred_act_wants_ingress(int action)
53592b36
SL
36{
37 switch (action) {
38 case TCA_EGRESS_REDIR:
39 case TCA_EGRESS_MIRROR:
8dc07fdb 40 return false;
53592b36
SL
41 case TCA_INGRESS_REDIR:
42 case TCA_INGRESS_MIRROR:
8dc07fdb 43 return true;
53592b36
SL
44 default:
45 BUG();
46 }
47}
48
e5cf1baf
PA
49static bool tcf_mirred_can_reinsert(int action)
50{
51 switch (action) {
52 case TC_ACT_SHOT:
53 case TC_ACT_STOLEN:
54 case TC_ACT_QUEUED:
55 case TC_ACT_TRAP:
56 return true;
57 }
58 return false;
59}
60
4e232818
VB
61static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
62{
63 return rcu_dereference_protected(m->tcfm_dev,
64 lockdep_is_held(&m->tcf_lock));
65}
66
9a63b255 67static void tcf_mirred_release(struct tc_action *a)
1da177e4 68{
86062033 69 struct tcf_mirred *m = to_mirred(a);
dc327f89 70 struct net_device *dev;
2ee22a90 71
4e232818 72 spin_lock(&mirred_list_lock);
a5b5c958 73 list_del(&m->tcfm_list);
4e232818
VB
74 spin_unlock(&mirred_list_lock);
75
76 /* last reference to action, no need to lock */
77 dev = rcu_dereference_protected(m->tcfm_dev, 1);
2ee22a90
ED
78 if (dev)
79 dev_put(dev);
1da177e4
LT
80}
81
53b2bf3f
PM
82static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
83 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
84};
85
c7d03a00 86static unsigned int mirred_net_id;
a85a970a 87static struct tc_action_ops act_mirred_ops;
ddf97ccd 88
c1b52739 89static int tcf_mirred_init(struct net *net, struct nlattr *nla,
789871bb
VB
90 struct nlattr *est, struct tc_action **a,
91 int ovr, int bind, bool rtnl_held,
85d0966f 92 struct tcf_proto *tp,
789871bb 93 struct netlink_ext_ack *extack)
1da177e4 94{
ddf97ccd 95 struct tc_action_net *tn = net_generic(net, mirred_net_id);
7ba699c6 96 struct nlattr *tb[TCA_MIRRED_MAX + 1];
ff9721d3 97 struct tcf_chain *goto_ch = NULL;
16577923 98 bool mac_header_xmit = false;
1da177e4 99 struct tc_mirred *parm;
e9ce1cd3 100 struct tcf_mirred *m;
b76965e0 101 struct net_device *dev;
b2313077 102 bool exists = false;
0190c1d4 103 int ret, err;
1da177e4 104
1d4760c7
AA
105 if (!nla) {
106 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
1da177e4 107 return -EINVAL;
1d4760c7 108 }
8cb08174
JB
109 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
110 mirred_policy, extack);
b76965e0
CG
111 if (ret < 0)
112 return ret;
1d4760c7
AA
113 if (!tb[TCA_MIRRED_PARMS]) {
114 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
1da177e4 115 return -EINVAL;
1d4760c7 116 }
7ba699c6 117 parm = nla_data(tb[TCA_MIRRED_PARMS]);
87dfbdc6 118
0190c1d4
VB
119 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
120 if (err < 0)
121 return err;
122 exists = err;
87dfbdc6
JHS
123 if (exists && bind)
124 return 0;
125
b76965e0
CG
126 switch (parm->eaction) {
127 case TCA_EGRESS_MIRROR:
128 case TCA_EGRESS_REDIR:
53592b36
SL
129 case TCA_INGRESS_REDIR:
130 case TCA_INGRESS_MIRROR:
b76965e0
CG
131 break;
132 default:
87dfbdc6 133 if (exists)
65a206c0 134 tcf_idr_release(*a, bind);
0190c1d4
VB
135 else
136 tcf_idr_cleanup(tn, parm->index);
1d4760c7 137 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
b76965e0
CG
138 return -EINVAL;
139 }
1da177e4 140
87dfbdc6 141 if (!exists) {
4e232818 142 if (!parm->ifindex) {
0190c1d4 143 tcf_idr_cleanup(tn, parm->index);
1d4760c7 144 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
1da177e4 145 return -EINVAL;
1d4760c7 146 }
65a206c0
CM
147 ret = tcf_idr_create(tn, parm->index, est, a,
148 &act_mirred_ops, bind, true);
0190c1d4
VB
149 if (ret) {
150 tcf_idr_cleanup(tn, parm->index);
86062033 151 return ret;
0190c1d4 152 }
1da177e4 153 ret = ACT_P_CREATED;
4e8ddd7f 154 } else if (!ovr) {
65a206c0 155 tcf_idr_release(*a, bind);
4e8ddd7f 156 return -EEXIST;
1da177e4 157 }
064c5d68
JH
158
159 m = to_mirred(*a);
160 if (ret == ACT_P_CREATED)
161 INIT_LIST_HEAD(&m->tcfm_list);
162
ff9721d3
DC
163 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
164 if (err < 0)
165 goto release_idr;
166
653cd284 167 spin_lock_bh(&m->tcf_lock);
4e232818
VB
168
169 if (parm->ifindex) {
170 dev = dev_get_by_index(net, parm->ifindex);
171 if (!dev) {
653cd284 172 spin_unlock_bh(&m->tcf_lock);
ff9721d3
DC
173 err = -ENODEV;
174 goto put_chain;
4e232818
VB
175 }
176 mac_header_xmit = dev_is_mac_header_xmit(dev);
177 rcu_swap_protected(m->tcfm_dev, dev,
178 lockdep_is_held(&m->tcf_lock));
179 if (dev)
180 dev_put(dev);
16577923 181 m->tcfm_mac_header_xmit = mac_header_xmit;
1da177e4 182 }
ff9721d3
DC
183 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
184 m->tcfm_eaction = parm->eaction;
653cd284 185 spin_unlock_bh(&m->tcf_lock);
ff9721d3
DC
186 if (goto_ch)
187 tcf_chain_put_by_act(goto_ch);
2ee22a90 188
3b87956e 189 if (ret == ACT_P_CREATED) {
4e232818 190 spin_lock(&mirred_list_lock);
3b87956e 191 list_add(&m->tcfm_list, &mirred_list);
4e232818
VB
192 spin_unlock(&mirred_list_lock);
193
65a206c0 194 tcf_idr_insert(tn, *a);
3b87956e 195 }
1da177e4 196
1da177e4 197 return ret;
ff9721d3
DC
198put_chain:
199 if (goto_ch)
200 tcf_chain_put_by_act(goto_ch);
201release_idr:
202 tcf_idr_release(*a, bind);
203 return err;
1da177e4
LT
204}
205
7c5790c4
JHS
206static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
207 struct tcf_result *res)
1da177e4 208{
a85a970a 209 struct tcf_mirred *m = to_mirred(a);
e5cf1baf 210 struct sk_buff *skb2 = skb;
53592b36 211 bool m_mac_header_xmit;
1da177e4 212 struct net_device *dev;
53592b36 213 int retval, err = 0;
e5cf1baf
PA
214 bool use_reinsert;
215 bool want_ingress;
216 bool is_redirect;
53592b36
SL
217 int m_eaction;
218 int mac_len;
1da177e4 219
2ee22a90 220 tcf_lastuse_update(&m->tcf_tm);
2ee22a90 221 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
1da177e4 222
53592b36
SL
223 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
224 m_eaction = READ_ONCE(m->tcfm_eaction);
2ee22a90 225 retval = READ_ONCE(m->tcf_action);
7fd4b288 226 dev = rcu_dereference_bh(m->tcfm_dev);
2ee22a90
ED
227 if (unlikely(!dev)) {
228 pr_notice_once("tc mirred: target device is gone\n");
3b87956e 229 goto out;
230 }
231
2ee22a90 232 if (unlikely(!(dev->flags & IFF_UP))) {
e87cc472
JP
233 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
234 dev->name);
feed1f17 235 goto out;
1da177e4
LT
236 }
237
e5cf1baf
PA
238 /* we could easily avoid the clone only if called by ingress and clsact;
239 * since we can't easily detect the clsact caller, skip clone only for
240 * ingress - that covers the TC S/W datapath.
241 */
242 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
243 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
244 tcf_mirred_can_reinsert(retval);
245 if (!use_reinsert) {
246 skb2 = skb_clone(skb, GFP_ATOMIC);
247 if (!skb2)
248 goto out;
249 }
1da177e4 250
53592b36
SL
251 /* If action's target direction differs than filter's direction,
252 * and devices expect a mac header on xmit, then mac push/pull is
253 * needed.
254 */
e5cf1baf
PA
255 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
256 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
a5135bcf 257 if (!skb_at_tc_ingress(skb)) {
53592b36
SL
258 /* caught at egress, act ingress: pull mac */
259 mac_len = skb_network_header(skb) - skb_mac_header(skb);
260 skb_pull_rcsum(skb2, mac_len);
261 } else {
262 /* caught at ingress, act egress: push mac */
82a31b92 263 skb_push_rcsum(skb2, skb->mac_len);
53592b36 264 }
feed1f17 265 }
1da177e4 266
e5cf1baf
PA
267 skb2->skb_iif = skb->dev->ifindex;
268 skb2->dev = dev;
269
1da177e4 270 /* mirror is always swallowed */
e5cf1baf 271 if (is_redirect) {
bc31c905
WB
272 skb2->tc_redirected = 1;
273 skb2->tc_from_ingress = skb2->tc_at_ingress;
7236ead1
ED
274 if (skb2->tc_from_ingress)
275 skb2->tstamp = 0;
e5cf1baf
PA
276 /* let's the caller reinsert the packet, if possible */
277 if (use_reinsert) {
278 res->ingress = want_ingress;
279 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
280 return TC_ACT_REINSERT;
281 }
bc31c905 282 }
1da177e4 283
e5cf1baf 284 if (!want_ingress)
53592b36
SL
285 err = dev_queue_xmit(skb2);
286 else
287 err = netif_receive_skb(skb2);
feed1f17 288
feed1f17 289 if (err) {
2ee22a90
ED
290out:
291 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
53592b36 292 if (tcf_mirred_is_act_redirect(m_eaction))
16c0b164 293 retval = TC_ACT_SHOT;
2ee22a90 294 }
feed1f17
CG
295
296 return retval;
1da177e4
LT
297}
298
9798e6fe 299static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
28169aba 300 u64 lastuse, bool hw)
9798e6fe 301{
5712bf9c
PB
302 struct tcf_mirred *m = to_mirred(a);
303 struct tcf_t *tm = &m->tcf_tm;
304
9798e6fe 305 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
28169aba
EC
306 if (hw)
307 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
308 bytes, packets);
3bb23421 309 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
9798e6fe
JK
310}
311
5a7a5555
JHS
312static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
313 int ref)
1da177e4 314{
27a884dc 315 unsigned char *b = skb_tail_pointer(skb);
a85a970a 316 struct tcf_mirred *m = to_mirred(a);
1c40be12
ED
317 struct tc_mirred opt = {
318 .index = m->tcf_index,
036bb443
VB
319 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
320 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
1c40be12 321 };
4e232818 322 struct net_device *dev;
1da177e4
LT
323 struct tcf_t t;
324
653cd284 325 spin_lock_bh(&m->tcf_lock);
4e232818
VB
326 opt.action = m->tcf_action;
327 opt.eaction = m->tcfm_eaction;
328 dev = tcf_mirred_dev_dereference(m);
329 if (dev)
330 opt.ifindex = dev->ifindex;
331
1b34ec43
DM
332 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
333 goto nla_put_failure;
48d8ee16
JHS
334
335 tcf_tm_dump(&t, &m->tcf_tm);
9854518e 336 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
1b34ec43 337 goto nla_put_failure;
653cd284 338 spin_unlock_bh(&m->tcf_lock);
4e232818 339
1da177e4
LT
340 return skb->len;
341
7ba699c6 342nla_put_failure:
653cd284 343 spin_unlock_bh(&m->tcf_lock);
dc5fc579 344 nlmsg_trim(skb, b);
1da177e4
LT
345 return -1;
346}
347
ddf97ccd
WC
348static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
349 struct netlink_callback *cb, int type,
41780105
AA
350 const struct tc_action_ops *ops,
351 struct netlink_ext_ack *extack)
ddf97ccd
WC
352{
353 struct tc_action_net *tn = net_generic(net, mirred_net_id);
354
b3620145 355 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
356}
357
f061b48c 358static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
359{
360 struct tc_action_net *tn = net_generic(net, mirred_net_id);
361
65a206c0 362 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
363}
364
3b87956e 365static int mirred_device_event(struct notifier_block *unused,
366 unsigned long event, void *ptr)
367{
351638e7 368 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3b87956e 369 struct tcf_mirred *m;
370
2ee22a90 371 ASSERT_RTNL();
6bd00b85 372 if (event == NETDEV_UNREGISTER) {
4e232818 373 spin_lock(&mirred_list_lock);
3b87956e 374 list_for_each_entry(m, &mirred_list, tcfm_list) {
653cd284 375 spin_lock_bh(&m->tcf_lock);
4e232818 376 if (tcf_mirred_dev_dereference(m) == dev) {
3b87956e 377 dev_put(dev);
2ee22a90
ED
378 /* Note : no rcu grace period necessary, as
379 * net_device are already rcu protected.
380 */
381 RCU_INIT_POINTER(m->tcfm_dev, NULL);
3b87956e 382 }
653cd284 383 spin_unlock_bh(&m->tcf_lock);
3b87956e 384 }
4e232818 385 spin_unlock(&mirred_list_lock);
6bd00b85 386 }
3b87956e 387
388 return NOTIFY_DONE;
389}
390
391static struct notifier_block mirred_device_notifier = {
392 .notifier_call = mirred_device_event,
393};
394
843e79d0 395static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
255cb304 396{
843e79d0 397 struct tcf_mirred *m = to_mirred(a);
4e232818 398 struct net_device *dev;
84a75b32 399
4e232818
VB
400 rcu_read_lock();
401 dev = rcu_dereference(m->tcfm_dev);
84a75b32
VB
402 if (dev)
403 dev_hold(dev);
4e232818 404 rcu_read_unlock();
255cb304 405
84a75b32
VB
406 return dev;
407}
408
409static void tcf_mirred_put_dev(struct net_device *dev)
410{
411 dev_put(dev);
255cb304
HHZ
412}
413
1da177e4
LT
414static struct tc_action_ops act_mirred_ops = {
415 .kind = "mirred",
eddd2cf1 416 .id = TCA_ID_MIRRED,
1da177e4 417 .owner = THIS_MODULE,
7c5790c4 418 .act = tcf_mirred_act,
9798e6fe 419 .stats_update = tcf_stats_update,
1da177e4 420 .dump = tcf_mirred_dump,
86062033 421 .cleanup = tcf_mirred_release,
1da177e4 422 .init = tcf_mirred_init,
ddf97ccd
WC
423 .walk = tcf_mirred_walker,
424 .lookup = tcf_mirred_search,
a85a970a 425 .size = sizeof(struct tcf_mirred),
843e79d0 426 .get_dev = tcf_mirred_get_dev,
84a75b32 427 .put_dev = tcf_mirred_put_dev,
ddf97ccd
WC
428};
429
430static __net_init int mirred_init_net(struct net *net)
431{
432 struct tc_action_net *tn = net_generic(net, mirred_net_id);
433
c7e460ce 434 return tc_action_net_init(tn, &act_mirred_ops);
ddf97ccd
WC
435}
436
039af9c6 437static void __net_exit mirred_exit_net(struct list_head *net_list)
ddf97ccd 438{
039af9c6 439 tc_action_net_exit(net_list, mirred_net_id);
ddf97ccd
WC
440}
441
442static struct pernet_operations mirred_net_ops = {
443 .init = mirred_init_net,
039af9c6 444 .exit_batch = mirred_exit_net,
ddf97ccd
WC
445 .id = &mirred_net_id,
446 .size = sizeof(struct tc_action_net),
1da177e4
LT
447};
448
449MODULE_AUTHOR("Jamal Hadi Salim(2002)");
450MODULE_DESCRIPTION("Device Mirror/redirect actions");
451MODULE_LICENSE("GPL");
452
e9ce1cd3 453static int __init mirred_init_module(void)
1da177e4 454{
3b87956e 455 int err = register_netdevice_notifier(&mirred_device_notifier);
456 if (err)
457 return err;
458
6ff9c364 459 pr_info("Mirror/redirect action on\n");
ddf97ccd 460 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
1da177e4
LT
461}
462
e9ce1cd3 463static void __exit mirred_cleanup_module(void)
1da177e4 464{
ddf97ccd 465 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
568a153a 466 unregister_netdevice_notifier(&mirred_device_notifier);
1da177e4
LT
467}
468
469module_init(mirred_init_module);
470module_exit(mirred_cleanup_module);