1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_mirred.c packet mirroring and redirect actions
5 * Authors: Jamal Hadi Salim (2002-4)
7 * TODO: Add ingress support (and socket redirect support)
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>
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>
28 static LIST_HEAD(mirred_list);
29 static DEFINE_SPINLOCK(mirred_list_lock);
31 #define MIRRED_NEST_LIMIT 4
32 static DEFINE_PER_CPU(unsigned int, mirred_nest_level);
34 static bool tcf_mirred_is_act_redirect(int action)
36 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
39 static bool tcf_mirred_act_wants_ingress(int action)
42 case TCA_EGRESS_REDIR:
43 case TCA_EGRESS_MIRROR:
45 case TCA_INGRESS_REDIR:
46 case TCA_INGRESS_MIRROR:
53 static bool tcf_mirred_can_reinsert(int action)
65 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
67 return rcu_dereference_protected(m->tcfm_dev,
68 lockdep_is_held(&m->tcf_lock));
71 static void tcf_mirred_release(struct tc_action *a)
73 struct tcf_mirred *m = to_mirred(a);
74 struct net_device *dev;
76 spin_lock(&mirred_list_lock);
77 list_del(&m->tcfm_list);
78 spin_unlock(&mirred_list_lock);
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);
85 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
86 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
89 static struct tc_action_ops act_mirred_ops;
91 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
92 struct nlattr *est, struct tc_action **a,
94 u32 flags, struct netlink_ext_ack *extack)
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;
108 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
111 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
112 mirred_policy, extack);
115 if (!tb[TCA_MIRRED_PARMS]) {
116 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
119 parm = nla_data(tb[TCA_MIRRED_PARMS]);
121 err = tcf_idr_check_alloc(tn, &index, a, bind);
128 switch (parm->eaction) {
129 case TCA_EGRESS_MIRROR:
130 case TCA_EGRESS_REDIR:
131 case TCA_INGRESS_REDIR:
132 case TCA_INGRESS_MIRROR:
136 tcf_idr_release(*a, bind);
138 tcf_idr_cleanup(tn, index);
139 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
144 if (!parm->ifindex) {
145 tcf_idr_cleanup(tn, index);
146 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
149 ret = tcf_idr_create_from_flags(tn, index, est, a,
150 &act_mirred_ops, bind, flags);
152 tcf_idr_cleanup(tn, index);
156 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
157 tcf_idr_release(*a, bind);
162 if (ret == ACT_P_CREATED)
163 INIT_LIST_HEAD(&m->tcfm_list);
165 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
169 spin_lock_bh(&m->tcf_lock);
172 struct net_device *odev, *ndev;
174 ndev = dev_get_by_index(net, parm->ifindex);
176 spin_unlock_bh(&m->tcf_lock);
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;
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);
191 tcf_chain_put_by_act(goto_ch);
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);
202 tcf_chain_put_by_act(goto_ch);
204 tcf_idr_release(*a, bind);
208 static bool is_mirred_nested(void)
210 return unlikely(__this_cpu_read(mirred_nest_level) > 1);
213 static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
218 err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
219 else if (is_mirred_nested())
222 err = netif_receive_skb(skb);
227 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
228 struct tcf_result *res)
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;
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);
253 tcf_lastuse_update(&m->tcf_tm);
254 tcf_action_update_bstats(&m->common, skb);
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");
265 if (unlikely(!(dev->flags & IFF_UP))) {
266 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
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.
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);
280 skb2 = skb_clone(skb, GFP_ATOMIC);
285 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
287 /* All mirred/redirected skbs should clear previous ct info */
289 if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
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);
298 /* target device/action expect data at nh */
299 skb_pull_rcsum(skb2, mac_len);
301 /* target device/action expect data at mac */
302 skb_push_rcsum(skb2, mac_len);
306 skb2->skb_iif = skb->dev->ifindex;
309 /* mirror is always swallowed */
311 skb_set_redirected(skb2, skb2->tc_at_ingress);
313 /* let's the caller reinsert the packet, if possible */
315 err = tcf_mirred_forward(want_ingress, skb);
317 tcf_action_inc_overlimit_qstats(&m->common);
318 __this_cpu_dec(mirred_nest_level);
319 return TC_ACT_CONSUMED;
323 err = tcf_mirred_forward(want_ingress, skb2);
326 tcf_action_inc_overlimit_qstats(&m->common);
327 if (tcf_mirred_is_act_redirect(m_eaction))
328 retval = TC_ACT_SHOT;
330 __this_cpu_dec(mirred_nest_level);
335 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
336 u64 drops, u64 lastuse, bool hw)
338 struct tcf_mirred *m = to_mirred(a);
339 struct tcf_t *tm = &m->tcf_tm;
341 tcf_action_update_stats(a, bytes, packets, drops, hw);
342 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
345 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
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,
355 struct net_device *dev;
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);
363 opt.ifindex = dev->ifindex;
365 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
366 goto nla_put_failure;
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);
376 spin_unlock_bh(&m->tcf_lock);
381 static int mirred_device_event(struct notifier_block *unused,
382 unsigned long event, void *ptr)
384 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
385 struct tcf_mirred *m;
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.
397 RCU_INIT_POINTER(m->tcfm_dev, NULL);
399 spin_unlock_bh(&m->tcf_lock);
401 spin_unlock(&mirred_list_lock);
407 static struct notifier_block mirred_device_notifier = {
408 .notifier_call = mirred_device_event,
411 static void tcf_mirred_dev_put(void *priv)
413 struct net_device *dev = priv;
418 static struct net_device *
419 tcf_mirred_get_dev(const struct tc_action *a,
420 tc_action_priv_destructor *destructor)
422 struct tcf_mirred *m = to_mirred(a);
423 struct net_device *dev;
426 dev = rcu_dereference(m->tcfm_dev);
429 *destructor = tcf_mirred_dev_put;
436 static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
438 return nla_total_size(sizeof(struct tc_mirred));
441 static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
442 const struct tc_action *act)
444 entry->dev = act->ops->get_dev(act, &entry->destructor);
447 entry->destructor_priv = entry->dev;
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)
455 struct flow_action_entry *entry = entry_data;
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);
470 NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
475 struct flow_offload_action *fl_action = entry_data;
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;
492 static struct tc_action_ops act_mirred_ops = {
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,
507 static __net_init int mirred_init_net(struct net *net)
509 struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
511 return tc_action_net_init(net, tn, &act_mirred_ops);
514 static void __net_exit mirred_exit_net(struct list_head *net_list)
516 tc_action_net_exit(net_list, act_mirred_ops.net_id);
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),
526 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
527 MODULE_DESCRIPTION("Device Mirror/redirect actions");
528 MODULE_LICENSE("GPL");
530 static int __init mirred_init_module(void)
532 int err = register_netdevice_notifier(&mirred_device_notifier);
536 pr_info("Mirror/redirect action on\n");
537 err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
539 unregister_netdevice_notifier(&mirred_device_notifier);
544 static void __exit mirred_cleanup_module(void)
546 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
547 unregister_netdevice_notifier(&mirred_device_notifier);
550 module_init(mirred_init_module);
551 module_exit(mirred_cleanup_module);