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>
27 #include <net/tc_wrapper.h>
29 static LIST_HEAD(mirred_list);
30 static DEFINE_SPINLOCK(mirred_list_lock);
32 #define MIRRED_NEST_LIMIT 4
33 static DEFINE_PER_CPU(unsigned int, mirred_nest_level);
35 static bool tcf_mirred_is_act_redirect(int action)
37 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
40 static bool tcf_mirred_act_wants_ingress(int action)
43 case TCA_EGRESS_REDIR:
44 case TCA_EGRESS_MIRROR:
46 case TCA_INGRESS_REDIR:
47 case TCA_INGRESS_MIRROR:
54 static bool tcf_mirred_can_reinsert(int action)
66 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
68 return rcu_dereference_protected(m->tcfm_dev,
69 lockdep_is_held(&m->tcf_lock));
72 static void tcf_mirred_release(struct tc_action *a)
74 struct tcf_mirred *m = to_mirred(a);
75 struct net_device *dev;
77 spin_lock(&mirred_list_lock);
78 list_del(&m->tcfm_list);
79 spin_unlock(&mirred_list_lock);
81 /* last reference to action, no need to lock */
82 dev = rcu_dereference_protected(m->tcfm_dev, 1);
83 netdev_put(dev, &m->tcfm_dev_tracker);
86 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
87 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
90 static struct tc_action_ops act_mirred_ops;
92 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
93 struct nlattr *est, struct tc_action **a,
95 u32 flags, struct netlink_ext_ack *extack)
97 struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
98 bool bind = flags & TCA_ACT_FLAGS_BIND;
99 struct nlattr *tb[TCA_MIRRED_MAX + 1];
100 struct tcf_chain *goto_ch = NULL;
101 bool mac_header_xmit = false;
102 struct tc_mirred *parm;
103 struct tcf_mirred *m;
109 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
112 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
113 mirred_policy, extack);
116 if (!tb[TCA_MIRRED_PARMS]) {
117 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
120 parm = nla_data(tb[TCA_MIRRED_PARMS]);
122 err = tcf_idr_check_alloc(tn, &index, a, bind);
129 switch (parm->eaction) {
130 case TCA_EGRESS_MIRROR:
131 case TCA_EGRESS_REDIR:
132 case TCA_INGRESS_REDIR:
133 case TCA_INGRESS_MIRROR:
137 tcf_idr_release(*a, bind);
139 tcf_idr_cleanup(tn, index);
140 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
145 if (!parm->ifindex) {
146 tcf_idr_cleanup(tn, index);
147 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
150 ret = tcf_idr_create_from_flags(tn, index, est, a,
151 &act_mirred_ops, bind, flags);
153 tcf_idr_cleanup(tn, index);
157 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
158 tcf_idr_release(*a, bind);
163 if (ret == ACT_P_CREATED)
164 INIT_LIST_HEAD(&m->tcfm_list);
166 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
170 spin_lock_bh(&m->tcf_lock);
173 struct net_device *odev, *ndev;
175 ndev = dev_get_by_index(net, parm->ifindex);
177 spin_unlock_bh(&m->tcf_lock);
181 mac_header_xmit = dev_is_mac_header_xmit(ndev);
182 odev = rcu_replace_pointer(m->tcfm_dev, ndev,
183 lockdep_is_held(&m->tcf_lock));
184 netdev_put(odev, &m->tcfm_dev_tracker);
185 netdev_tracker_alloc(ndev, &m->tcfm_dev_tracker, GFP_ATOMIC);
186 m->tcfm_mac_header_xmit = mac_header_xmit;
188 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
189 m->tcfm_eaction = parm->eaction;
190 spin_unlock_bh(&m->tcf_lock);
192 tcf_chain_put_by_act(goto_ch);
194 if (ret == ACT_P_CREATED) {
195 spin_lock(&mirred_list_lock);
196 list_add(&m->tcfm_list, &mirred_list);
197 spin_unlock(&mirred_list_lock);
203 tcf_chain_put_by_act(goto_ch);
205 tcf_idr_release(*a, bind);
209 static bool is_mirred_nested(void)
211 return unlikely(__this_cpu_read(mirred_nest_level) > 1);
214 static int tcf_mirred_forward(bool want_ingress, struct sk_buff *skb)
219 err = tcf_dev_queue_xmit(skb, dev_queue_xmit);
220 else if (is_mirred_nested())
223 err = netif_receive_skb(skb);
228 TC_INDIRECT_SCOPE int tcf_mirred_act(struct sk_buff *skb,
229 const struct tc_action *a,
230 struct tcf_result *res)
232 struct tcf_mirred *m = to_mirred(a);
233 struct sk_buff *skb2 = skb;
234 bool m_mac_header_xmit;
235 struct net_device *dev;
236 unsigned int nest_level;
247 nest_level = __this_cpu_inc_return(mirred_nest_level);
248 if (unlikely(nest_level > MIRRED_NEST_LIMIT)) {
249 net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
250 netdev_name(skb->dev));
251 __this_cpu_dec(mirred_nest_level);
255 tcf_lastuse_update(&m->tcf_tm);
256 tcf_action_update_bstats(&m->common, skb);
258 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
259 m_eaction = READ_ONCE(m->tcfm_eaction);
260 retval = READ_ONCE(m->tcf_action);
261 dev = rcu_dereference_bh(m->tcfm_dev);
262 if (unlikely(!dev)) {
263 pr_notice_once("tc mirred: target device is gone\n");
267 if (unlikely(!(dev->flags & IFF_UP)) || !netif_carrier_ok(dev)) {
268 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
273 /* we could easily avoid the clone only if called by ingress and clsact;
274 * since we can't easily detect the clsact caller, skip clone only for
275 * ingress - that covers the TC S/W datapath.
277 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
278 at_ingress = skb_at_tc_ingress(skb);
279 use_reinsert = at_ingress && is_redirect &&
280 tcf_mirred_can_reinsert(retval);
282 skb2 = skb_clone(skb, GFP_ATOMIC);
287 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
289 /* All mirred/redirected skbs should clear previous ct info */
291 if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
294 expects_nh = want_ingress || !m_mac_header_xmit;
295 at_nh = skb->data == skb_network_header(skb);
296 if (at_nh != expects_nh) {
297 mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
298 skb_network_offset(skb);
300 /* target device/action expect data at nh */
301 skb_pull_rcsum(skb2, mac_len);
303 /* target device/action expect data at mac */
304 skb_push_rcsum(skb2, mac_len);
308 skb2->skb_iif = skb->dev->ifindex;
311 /* mirror is always swallowed */
313 skb_set_redirected(skb2, skb2->tc_at_ingress);
315 /* let's the caller reinsert the packet, if possible */
317 err = tcf_mirred_forward(want_ingress, skb);
319 tcf_action_inc_overlimit_qstats(&m->common);
320 __this_cpu_dec(mirred_nest_level);
321 return TC_ACT_CONSUMED;
325 err = tcf_mirred_forward(want_ingress, skb2);
328 tcf_action_inc_overlimit_qstats(&m->common);
329 if (tcf_mirred_is_act_redirect(m_eaction))
330 retval = TC_ACT_SHOT;
332 __this_cpu_dec(mirred_nest_level);
337 static void tcf_stats_update(struct tc_action *a, u64 bytes, u64 packets,
338 u64 drops, u64 lastuse, bool hw)
340 struct tcf_mirred *m = to_mirred(a);
341 struct tcf_t *tm = &m->tcf_tm;
343 tcf_action_update_stats(a, bytes, packets, drops, hw);
344 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
347 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
350 unsigned char *b = skb_tail_pointer(skb);
351 struct tcf_mirred *m = to_mirred(a);
352 struct tc_mirred opt = {
353 .index = m->tcf_index,
354 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
355 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
357 struct net_device *dev;
360 spin_lock_bh(&m->tcf_lock);
361 opt.action = m->tcf_action;
362 opt.eaction = m->tcfm_eaction;
363 dev = tcf_mirred_dev_dereference(m);
365 opt.ifindex = dev->ifindex;
367 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
368 goto nla_put_failure;
370 tcf_tm_dump(&t, &m->tcf_tm);
371 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
372 goto nla_put_failure;
373 spin_unlock_bh(&m->tcf_lock);
378 spin_unlock_bh(&m->tcf_lock);
383 static int mirred_device_event(struct notifier_block *unused,
384 unsigned long event, void *ptr)
386 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
387 struct tcf_mirred *m;
390 if (event == NETDEV_UNREGISTER) {
391 spin_lock(&mirred_list_lock);
392 list_for_each_entry(m, &mirred_list, tcfm_list) {
393 spin_lock_bh(&m->tcf_lock);
394 if (tcf_mirred_dev_dereference(m) == dev) {
395 netdev_put(dev, &m->tcfm_dev_tracker);
396 /* Note : no rcu grace period necessary, as
397 * net_device are already rcu protected.
399 RCU_INIT_POINTER(m->tcfm_dev, NULL);
401 spin_unlock_bh(&m->tcf_lock);
403 spin_unlock(&mirred_list_lock);
409 static struct notifier_block mirred_device_notifier = {
410 .notifier_call = mirred_device_event,
413 static void tcf_mirred_dev_put(void *priv)
415 struct net_device *dev = priv;
420 static struct net_device *
421 tcf_mirred_get_dev(const struct tc_action *a,
422 tc_action_priv_destructor *destructor)
424 struct tcf_mirred *m = to_mirred(a);
425 struct net_device *dev;
428 dev = rcu_dereference(m->tcfm_dev);
431 *destructor = tcf_mirred_dev_put;
438 static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
440 return nla_total_size(sizeof(struct tc_mirred));
443 static void tcf_offload_mirred_get_dev(struct flow_action_entry *entry,
444 const struct tc_action *act)
446 entry->dev = act->ops->get_dev(act, &entry->destructor);
449 entry->destructor_priv = entry->dev;
452 static int tcf_mirred_offload_act_setup(struct tc_action *act, void *entry_data,
453 u32 *index_inc, bool bind,
454 struct netlink_ext_ack *extack)
457 struct flow_action_entry *entry = entry_data;
459 if (is_tcf_mirred_egress_redirect(act)) {
460 entry->id = FLOW_ACTION_REDIRECT;
461 tcf_offload_mirred_get_dev(entry, act);
462 } else if (is_tcf_mirred_egress_mirror(act)) {
463 entry->id = FLOW_ACTION_MIRRED;
464 tcf_offload_mirred_get_dev(entry, act);
465 } else if (is_tcf_mirred_ingress_redirect(act)) {
466 entry->id = FLOW_ACTION_REDIRECT_INGRESS;
467 tcf_offload_mirred_get_dev(entry, act);
468 } else if (is_tcf_mirred_ingress_mirror(act)) {
469 entry->id = FLOW_ACTION_MIRRED_INGRESS;
470 tcf_offload_mirred_get_dev(entry, act);
472 NL_SET_ERR_MSG_MOD(extack, "Unsupported mirred offload");
477 struct flow_offload_action *fl_action = entry_data;
479 if (is_tcf_mirred_egress_redirect(act))
480 fl_action->id = FLOW_ACTION_REDIRECT;
481 else if (is_tcf_mirred_egress_mirror(act))
482 fl_action->id = FLOW_ACTION_MIRRED;
483 else if (is_tcf_mirred_ingress_redirect(act))
484 fl_action->id = FLOW_ACTION_REDIRECT_INGRESS;
485 else if (is_tcf_mirred_ingress_mirror(act))
486 fl_action->id = FLOW_ACTION_MIRRED_INGRESS;
494 static struct tc_action_ops act_mirred_ops = {
497 .owner = THIS_MODULE,
498 .act = tcf_mirred_act,
499 .stats_update = tcf_stats_update,
500 .dump = tcf_mirred_dump,
501 .cleanup = tcf_mirred_release,
502 .init = tcf_mirred_init,
503 .get_fill_size = tcf_mirred_get_fill_size,
504 .offload_act_setup = tcf_mirred_offload_act_setup,
505 .size = sizeof(struct tcf_mirred),
506 .get_dev = tcf_mirred_get_dev,
509 static __net_init int mirred_init_net(struct net *net)
511 struct tc_action_net *tn = net_generic(net, act_mirred_ops.net_id);
513 return tc_action_net_init(net, tn, &act_mirred_ops);
516 static void __net_exit mirred_exit_net(struct list_head *net_list)
518 tc_action_net_exit(net_list, act_mirred_ops.net_id);
521 static struct pernet_operations mirred_net_ops = {
522 .init = mirred_init_net,
523 .exit_batch = mirred_exit_net,
524 .id = &act_mirred_ops.net_id,
525 .size = sizeof(struct tc_action_net),
528 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
529 MODULE_DESCRIPTION("Device Mirror/redirect actions");
530 MODULE_LICENSE("GPL");
532 static int __init mirred_init_module(void)
534 int err = register_netdevice_notifier(&mirred_device_notifier);
538 pr_info("Mirror/redirect action on\n");
539 err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
541 unregister_netdevice_notifier(&mirred_device_notifier);
546 static void __exit mirred_cleanup_module(void)
548 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
549 unregister_netdevice_notifier(&mirred_device_notifier);
552 module_init(mirred_init_module);
553 module_exit(mirred_cleanup_module);