Mention branches and keyring.
[releases.git] / sched / act_mirred.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_mirred.c       packet mirroring and redirect actions
4  *
5  * Authors:     Jamal Hadi Salim (2002-4)
6  *
7  * TODO: Add ingress support (and socket redirect support)
8  */
9
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/skbuff.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/gfp.h>
19 #include <linux/if_arp.h>
20 #include <net/net_namespace.h>
21 #include <net/netlink.h>
22 #include <net/dst.h>
23 #include <net/pkt_sched.h>
24 #include <net/pkt_cls.h>
25 #include <linux/tc_act/tc_mirred.h>
26 #include <net/tc_act/tc_mirred.h>
27
28 static LIST_HEAD(mirred_list);
29 static DEFINE_SPINLOCK(mirred_list_lock);
30
31 #define MIRRED_NEST_LIMIT    4
32 static DEFINE_PER_CPU(unsigned int, mirred_nest_level);
33
34 static bool tcf_mirred_is_act_redirect(int action)
35 {
36         return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
37 }
38
39 static bool tcf_mirred_act_wants_ingress(int action)
40 {
41         switch (action) {
42         case TCA_EGRESS_REDIR:
43         case TCA_EGRESS_MIRROR:
44                 return false;
45         case TCA_INGRESS_REDIR:
46         case TCA_INGRESS_MIRROR:
47                 return true;
48         default:
49                 BUG();
50         }
51 }
52
53 static bool tcf_mirred_can_reinsert(int action)
54 {
55         switch (action) {
56         case TC_ACT_SHOT:
57         case TC_ACT_STOLEN:
58         case TC_ACT_QUEUED:
59         case TC_ACT_TRAP:
60                 return true;
61         }
62         return false;
63 }
64
65 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
66 {
67         return rcu_dereference_protected(m->tcfm_dev,
68                                          lockdep_is_held(&m->tcf_lock));
69 }
70
71 static void tcf_mirred_release(struct tc_action *a)
72 {
73         struct tcf_mirred *m = to_mirred(a);
74         struct net_device *dev;
75
76         spin_lock(&mirred_list_lock);
77         list_del(&m->tcfm_list);
78         spin_unlock(&mirred_list_lock);
79
80         /* last reference to action, no need to lock */
81         dev = rcu_dereference_protected(m->tcfm_dev, 1);
82         netdev_put(dev, &m->tcfm_dev_tracker);
83 }
84
85 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
86         [TCA_MIRRED_PARMS]      = { .len = sizeof(struct tc_mirred) },
87 };
88
89 static struct tc_action_ops act_mirred_ops;
90
91 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
92                            struct nlattr *est, struct tc_action **a,
93                            struct tcf_proto *tp,
94                            u32 flags, struct netlink_ext_ack *extack)
95 {
96         struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
97         bool bind = flags & TCA_ACT_FLAGS_BIND;
98         struct nlattr *tb[TCA_MIRRED_MAX + 1];
99         struct tcf_chain *goto_ch = NULL;
100         bool mac_header_xmit = false;
101         struct tc_mirred *parm;
102         struct tcf_mirred *m;
103         bool exists = false;
104         int ret, err;
105         u32 index;
106
107         if (!nla) {
108                 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
109                 return -EINVAL;
110         }
111         ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
112                                           mirred_policy, extack);
113         if (ret < 0)
114                 return ret;
115         if (!tb[TCA_MIRRED_PARMS]) {
116                 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
117                 return -EINVAL;
118         }
119         parm = nla_data(tb[TCA_MIRRED_PARMS]);
120         index = parm->index;
121         err = tcf_idr_check_alloc(tn, &index, a, bind);
122         if (err < 0)
123                 return err;
124         exists = err;
125         if (exists && bind)
126                 return 0;
127
128         switch (parm->eaction) {
129         case TCA_EGRESS_MIRROR:
130         case TCA_EGRESS_REDIR:
131         case TCA_INGRESS_REDIR:
132         case TCA_INGRESS_MIRROR:
133                 break;
134         default:
135                 if (exists)
136                         tcf_idr_release(*a, bind);
137                 else
138                         tcf_idr_cleanup(tn, index);
139                 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
140                 return -EINVAL;
141         }
142
143         if (!exists) {
144                 if (!parm->ifindex) {
145                         tcf_idr_cleanup(tn, index);
146                         NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147                         return -EINVAL;
148                 }
149                 ret = tcf_idr_create_from_flags(tn, index, est, a,
150                                                 &act_mirred_ops, bind, flags);
151                 if (ret) {
152                         tcf_idr_cleanup(tn, index);
153                         return ret;
154                 }
155                 ret = ACT_P_CREATED;
156         } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
157                 tcf_idr_release(*a, bind);
158                 return -EEXIST;
159         }
160
161         m = to_mirred(*a);
162         if (ret == ACT_P_CREATED)
163                 INIT_LIST_HEAD(&m->tcfm_list);
164
165         err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
166         if (err < 0)
167                 goto release_idr;
168
169         spin_lock_bh(&m->tcf_lock);
170
171         if (parm->ifindex) {
172                 struct net_device *odev, *ndev;
173
174                 ndev = dev_get_by_index(net, parm->ifindex);
175                 if (!ndev) {
176                         spin_unlock_bh(&m->tcf_lock);
177                         err = -ENODEV;
178                         goto put_chain;
179                 }
180                 mac_header_xmit = dev_is_mac_header_xmit(ndev);
181                 odev = rcu_replace_pointer(m->tcfm_dev, ndev,
182                                           lockdep_is_held(&m->tcf_lock));
183                 netdev_put(odev, &m->tcfm_dev_tracker);
184                 netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
185                 m->tcfm_mac_header_xmit = mac_header_xmit;
186         }
187         goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
188         m->tcfm_eaction = parm->eaction;
189         spin_unlock_bh(&m->tcf_lock);
190         if (goto_ch)
191                 tcf_chain_put_by_act(goto_ch);
192
193         if (ret == ACT_P_CREATED) {
194                 spin_lock(&mirred_list_lock);
195                 list_add(&m->tcfm_list, &mirred_list);
196                 spin_unlock(&mirred_list_lock);
197         }
198
199         return ret;
200 put_chain:
201         if (goto_ch)
202                 tcf_chain_put_by_act(goto_ch);
203 release_idr:
204         tcf_idr_release(*a, bind);
205         return err;
206 }
207
208 static bool is_mirred_nested(void)
209 {
210         return unlikely(__this_cpu_read(mirred_nest_level) > 1);
211 }
212
213 static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
214 {
215         int err;
216
217         if (!want_ingress)
218                 err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
219         else if (is_mirred_nested())
220                 err = netif_rx(skb);
221         else
222                 err = netif_receive_skb(skb);
223
224         return err;
225 }
226
227 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
228                           struct tcf_result *res)
229 {
230         struct tcf_mirred *m = to_mirred(a);
231         struct sk_buff *skb2 = skb;
232         bool m_mac_header_xmit;
233         struct net_device *dev;
234         unsigned int nest_level;
235         int retval, err = 0;
236         bool use_reinsert;
237         bool want_ingress;
238         bool is_redirect;
239         bool expects_nh;
240         bool at_ingress;
241         int m_eaction;
242         int mac_len;
243         bool at_nh;
244
245         nest_level = __this_cpu_inc_return(mirred_nest_level);
246         if (unlikely(nest_level > MIRRED_NEST_LIMIT)) {
247                 net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
248                                      netdev_name(skb->dev));
249                 __this_cpu_dec(mirred_nest_level);
250                 return TC_ACT_SHOT;
251         }
252
253         tcf_lastuse_update(&m->tcf_tm);
254         tcf_action_update_bstats(&m->common, skb);
255
256         m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
257         m_eaction = READ_ONCE(m->tcfm_eaction);
258         retval = READ_ONCE(m->tcf_action);
259         dev = rcu_dereference_bh(m->tcfm_dev);
260         if (unlikely(!dev)) {
261                 pr_notice_once("tc mirred: target device is gone\n");
262                 goto out;
263         }
264
265         if (unlikely(!(dev->flags & IFF_UP)) || !netif_carrier_ok(dev)) {
266                 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
267                                        dev->name);
268                 goto out;
269         }
270
271         /* we could easily avoid the clone only if called by ingress and clsact;
272          * since we can't easily detect the clsact caller, skip clone only for
273          * ingress - that covers the TC S/W datapath.
274          */
275         is_redirect = tcf_mirred_is_act_redirect(m_eaction);
276         at_ingress = skb_at_tc_ingress(skb);
277         use_reinsert = at_ingress && is_redirect &&
278                        tcf_mirred_can_reinsert(retval);
279         if (!use_reinsert) {
280                 skb2 = skb_clone(skb, GFP_ATOMIC);
281                 if (!skb2)
282                         goto out;
283         }
284
285         want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
286
287         /* All mirred/redirected skbs should clear previous ct info */
288         nf_reset_ct(skb2);
289         if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
290                 skb_dst_drop(skb2);
291
292         expects_nh = want_ingress || !m_mac_header_xmit;
293         at_nh = skb->data == skb_network_header(skb);
294         if (at_nh != expects_nh) {
295                 mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
296                           skb_network_header(skb) - skb_mac_header(skb);
297                 if (expects_nh) {
298                         /* target device/action expect data at nh */
299                         skb_pull_rcsum(skb2, mac_len);
300                 } else {
301                         /* target device/action expect data at mac */
302                         skb_push_rcsum(skb2, mac_len);
303                 }
304         }
305
306         skb2->skb_iif = skb->dev->ifindex;
307         skb2->dev = dev;
308
309         /* mirror is always swallowed */
310         if (is_redirect) {
311                 skb_set_redirected(skb2, skb2->tc_at_ingress);
312
313                 /* let's the caller reinsert the packet, if possible */
314                 if (use_reinsert) {
315                         err = tcf_mirred_forward(want_ingress, skb);
316                         if (err)
317                                 tcf_action_inc_overlimit_qstats(&m->common);
318                         __this_cpu_dec(mirred_nest_level);
319                         return TC_ACT_CONSUMED;
320                 }
321         }
322
323         err = tcf_mirred_forward(want_ingress, skb2);
324         if (err) {
325 out:
326                 tcf_action_inc_overlimit_qstats(&m->common);
327                 if (tcf_mirred_is_act_redirect(m_eaction))
328                         retval = TC_ACT_SHOT;
329         }
330         __this_cpu_dec(mirred_nest_level);
331
332         return retval;
333 }
334
335 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
336                              u64 drops, u64 lastuse, bool hw)
337 {
338         struct tcf_mirred *m = to_mirred(a);
339         struct tcf_t *tm = &m->tcf_tm;
340
341         tcf_action_update_stats(a, bytes, packets, drops, hw);
342         tm->lastuse = max_t(u64, tm->lastuse, lastuse);
343 }
344
345 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
346                            int ref)
347 {
348         unsigned char *b = skb_tail_pointer(skb);
349         struct tcf_mirred *m = to_mirred(a);
350         struct tc_mirred opt = {
351                 .index   = m->tcf_index,
352                 .refcnt  = refcount_read(&m->tcf_refcnt) - ref,
353                 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
354         };
355         struct net_device *dev;
356         struct tcf_t t;
357
358         spin_lock_bh(&m->tcf_lock);
359         opt.action = m->tcf_action;
360         opt.eaction = m->tcfm_eaction;
361         dev = tcf_mirred_dev_dereference(m);
362         if (dev)
363                 opt.ifindex = dev->ifindex;
364
365         if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
366                 goto nla_put_failure;
367
368         tcf_tm_dump(&t, &m->tcf_tm);
369         if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
370                 goto nla_put_failure;
371         spin_unlock_bh(&m->tcf_lock);
372
373         return skb->len;
374
375 nla_put_failure:
376         spin_unlock_bh(&m->tcf_lock);
377         nlmsg_trim(skb, b);
378         return -1;
379 }
380
381 static int mirred_device_event(struct notifier_block *unused,
382                                unsigned long event, void *ptr)
383 {
384         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
385         struct tcf_mirred *m;
386
387         ASSERT_RTNL();
388         if (event == NETDEV_UNREGISTER) {
389                 spin_lock(&mirred_list_lock);
390                 list_for_each_entry(m, &mirred_list, tcfm_list) {
391                         spin_lock_bh(&m->tcf_lock);
392                         if (tcf_mirred_dev_dereference(m) == dev) {
393                                 netdev_put(dev, &m->tcfm_dev_tracker);
394                                 /* Note : no rcu grace period necessary, as
395                                  * net_device are already rcu protected.
396                                  */
397                                 RCU_INIT_POINTER(m->tcfm_dev, NULL);
398                         }
399                         spin_unlock_bh(&m->tcf_lock);
400                 }
401                 spin_unlock(&mirred_list_lock);
402         }
403
404         return NOTIFY_DONE;
405 }
406
407 static struct notifier_block mirred_device_notifier = {
408         .notifier_call = mirred_device_event,
409 };
410
411 static void tcf_mirred_dev_put(void *priv)
412 {
413         struct net_device *dev = priv;
414
415         dev_put(dev);
416 }
417
418 static struct net_device *
419 tcf_mirred_get_dev(const struct tc_action *a,
420                    tc_action_priv_destructor *destructor)
421 {
422         struct tcf_mirred *m = to_mirred(a);
423         struct net_device *dev;
424
425         rcu_read_lock();
426         dev = rcu_dereference(m->tcfm_dev);
427         if (dev) {
428                 dev_hold(dev);
429                 *destructor = tcf_mirred_dev_put;
430         }
431         rcu_read_unlock();
432
433         return dev;
434 }
435
436 static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
437 {
438         return nla_total_size(sizeof(struct tc_mirred));
439 }
440
441 static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
442                                        const struct tc_action *act)
443 {
444         entry->dev = act->ops->get_dev(act, &entry->destructor);
445         if (!entry->dev)
446                 return;
447         entry->destructor_priv = entry->dev;
448 }
449
450 static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data,
451                                         u32 *index_inc, bool bind,
452                                         struct netlink_ext_ack *extack)
453 {
454         if (bind) {
455                 struct flow_action_entry *entry = entry_data;
456
457                 if (is_tcf_mirred_egress_redirect(act)) {
458                         entry->id = FLOW_ACTION_REDIRECT;
459                         tcf_offload_mirred_get_dev(entry, act);
460                 } else if (is_tcf_mirred_egress_mirror(act)) {
461                         entry->id = FLOW_ACTION_MIRRED;
462                         tcf_offload_mirred_get_dev(entry, act);
463                 } else if (is_tcf_mirred_ingress_redirect(act)) {
464                         entry->id = FLOW_ACTION_REDIRECT_INGRESS;
465                         tcf_offload_mirred_get_dev(entry, act);
466                 } else if (is_tcf_mirred_ingress_mirror(act)) {
467                         entry->id = FLOW_ACTION_MIRRED_INGRESS;
468                         tcf_offload_mirred_get_dev(entry, act);
469                 } else {
470                         NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
471                         return -EOPNOTSUPP;
472                 }
473                 *index_inc = 1;
474         } else {
475                 struct flow_offload_action *fl_action = entry_data;
476
477                 if (is_tcf_mirred_egress_redirect(act))
478                         fl_action->id = FLOW_ACTION_REDIRECT;
479                 else if (is_tcf_mirred_egress_mirror(act))
480                         fl_action->id = FLOW_ACTION_MIRRED;
481                 else if (is_tcf_mirred_ingress_redirect(act))
482                         fl_action->id = FLOW_ACTION_REDIRECT_INGRESS;
483                 else if (is_tcf_mirred_ingress_mirror(act))
484                         fl_action->id = FLOW_ACTION_MIRRED_INGRESS;
485                 else
486                         return -EOPNOTSUPP;
487         }
488
489         return 0;
490 }
491
492 static struct tc_action_ops act_mirred_ops = {
493         .kind           =       "mirred",
494         .id             =       TCA_ID_MIRRED,
495         .owner          =       THIS_MODULE,
496         .act            =       tcf_mirred_act,
497         .stats_update   =       tcf_stats_update,
498         .dump           =       tcf_mirred_dump,
499         .cleanup        =       tcf_mirred_release,
500         .init           =       tcf_mirred_init,
501         .get_fill_size  =       tcf_mirred_get_fill_size,
502         .offload_act_setup =    tcf_mirred_offload_act_setup,
503         .size           =       sizeof(struct tcf_mirred),
504         .get_dev        =       tcf_mirred_get_dev,
505 };
506
507 static __net_init int mirred_init_net(struct net *net)
508 {
509         struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
510
511         return tc_action_net_init(net, tn, &act_mirred_ops);
512 }
513
514 static void __net_exit mirred_exit_net(struct list_head *net_list)
515 {
516         tc_action_net_exit(net_list, act_mirred_ops.net_id);
517 }
518
519 static struct pernet_operations mirred_net_ops = {
520         .init = mirred_init_net,
521         .exit_batch = mirred_exit_net,
522         .id   = &act_mirred_ops.net_id,
523         .size = sizeof(struct tc_action_net),
524 };
525
526 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
527 MODULE_DESCRIPTION("Device Mirror/redirect actions");
528 MODULE_LICENSE("GPL");
529
530 static int __init mirred_init_module(void)
531 {
532         int err = register_netdevice_notifier(&mirred_device_notifier);
533         if (err)
534                 return err;
535
536         pr_info("Mirror/redirect action on\n");
537         err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
538         if (err)
539                 unregister_netdevice_notifier(&mirred_device_notifier);
540
541         return err;
542 }
543
544 static void __exit mirred_cleanup_module(void)
545 {
546         tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
547         unregister_netdevice_notifier(&mirred_device_notifier);
548 }
549
550 module_init(mirred_init_module);
551 module_exit(mirred_cleanup_module);