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_RECURSION_LIMIT 4
32 static DEFINE_PER_CPU(unsigned int, mirred_rec_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);
86 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
87 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
90 static unsigned int mirred_net_id;
91 static struct tc_action_ops act_mirred_ops;
93 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
94 struct nlattr *est, struct tc_action **a,
95 int ovr, int bind, bool rtnl_held,
97 struct netlink_ext_ack *extack)
99 struct tc_action_net *tn = net_generic(net, mirred_net_id);
100 struct nlattr *tb[TCA_MIRRED_MAX + 1];
101 struct tcf_chain *goto_ch = NULL;
102 bool mac_header_xmit = false;
103 struct tc_mirred *parm;
104 struct tcf_mirred *m;
105 struct net_device *dev;
111 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
114 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
115 mirred_policy, extack);
118 if (!tb[TCA_MIRRED_PARMS]) {
119 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
122 parm = nla_data(tb[TCA_MIRRED_PARMS]);
124 err = tcf_idr_check_alloc(tn, &index, a, bind);
131 switch (parm->eaction) {
132 case TCA_EGRESS_MIRROR:
133 case TCA_EGRESS_REDIR:
134 case TCA_INGRESS_REDIR:
135 case TCA_INGRESS_MIRROR:
139 tcf_idr_release(*a, bind);
141 tcf_idr_cleanup(tn, index);
142 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
147 if (!parm->ifindex) {
148 tcf_idr_cleanup(tn, index);
149 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
152 ret = tcf_idr_create(tn, index, est, a,
153 &act_mirred_ops, bind, true);
155 tcf_idr_cleanup(tn, index);
160 tcf_idr_release(*a, bind);
165 if (ret == ACT_P_CREATED)
166 INIT_LIST_HEAD(&m->tcfm_list);
168 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
172 spin_lock_bh(&m->tcf_lock);
175 dev = dev_get_by_index(net, parm->ifindex);
177 spin_unlock_bh(&m->tcf_lock);
181 mac_header_xmit = dev_is_mac_header_xmit(dev);
182 rcu_swap_protected(m->tcfm_dev, dev,
183 lockdep_is_held(&m->tcf_lock));
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 int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
210 struct tcf_result *res)
212 struct tcf_mirred *m = to_mirred(a);
213 struct sk_buff *skb2 = skb;
214 bool m_mac_header_xmit;
215 struct net_device *dev;
216 unsigned int rec_level;
227 rec_level = __this_cpu_inc_return(mirred_rec_level);
228 if (unlikely(rec_level > MIRRED_RECURSION_LIMIT)) {
229 net_warn_ratelimited("Packet exceeded mirred recursion limit on dev %s\n",
230 netdev_name(skb->dev));
231 __this_cpu_dec(mirred_rec_level);
235 tcf_lastuse_update(&m->tcf_tm);
236 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
238 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
239 m_eaction = READ_ONCE(m->tcfm_eaction);
240 retval = READ_ONCE(m->tcf_action);
241 dev = rcu_dereference_bh(m->tcfm_dev);
242 if (unlikely(!dev)) {
243 pr_notice_once("tc mirred: target device is gone\n");
247 if (unlikely(!(dev->flags & IFF_UP)) || !netif_carrier_ok(dev)) {
248 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
253 /* we could easily avoid the clone only if called by ingress and clsact;
254 * since we can't easily detect the clsact caller, skip clone only for
255 * ingress - that covers the TC S/W datapath.
257 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
258 at_ingress = skb_at_tc_ingress(skb);
259 use_reinsert = at_ingress && is_redirect &&
260 tcf_mirred_can_reinsert(retval);
262 skb2 = skb_clone(skb, GFP_ATOMIC);
267 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
269 /* All mirred/redirected skbs should clear previous ct info */
271 if (want_ingress && !at_ingress) /* drop dst for egress -> ingress */
274 expects_nh = want_ingress || !m_mac_header_xmit;
275 at_nh = skb->data == skb_network_header(skb);
276 if (at_nh != expects_nh) {
277 mac_len = skb_at_tc_ingress(skb) ? skb->mac_len :
278 skb_network_header(skb) - skb_mac_header(skb);
280 /* target device/action expect data at nh */
281 skb_pull_rcsum(skb2, mac_len);
283 /* target device/action expect data at mac */
284 skb_push_rcsum(skb2, mac_len);
288 skb2->skb_iif = skb->dev->ifindex;
291 /* mirror is always swallowed */
293 skb_set_redirected(skb2, skb2->tc_at_ingress);
295 /* let's the caller reinsert the packet, if possible */
297 res->ingress = want_ingress;
298 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
299 skb_tc_reinsert(skb, res);
300 __this_cpu_dec(mirred_rec_level);
301 return TC_ACT_CONSUMED;
306 err = dev_queue_xmit(skb2);
308 err = netif_receive_skb(skb2);
312 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
313 if (tcf_mirred_is_act_redirect(m_eaction))
314 retval = TC_ACT_SHOT;
316 __this_cpu_dec(mirred_rec_level);
321 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
322 u64 lastuse, bool hw)
324 struct tcf_mirred *m = to_mirred(a);
325 struct tcf_t *tm = &m->tcf_tm;
327 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
329 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
331 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
334 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
337 unsigned char *b = skb_tail_pointer(skb);
338 struct tcf_mirred *m = to_mirred(a);
339 struct tc_mirred opt = {
340 .index = m->tcf_index,
341 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
342 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
344 struct net_device *dev;
347 spin_lock_bh(&m->tcf_lock);
348 opt.action = m->tcf_action;
349 opt.eaction = m->tcfm_eaction;
350 dev = tcf_mirred_dev_dereference(m);
352 opt.ifindex = dev->ifindex;
354 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
355 goto nla_put_failure;
357 tcf_tm_dump(&t, &m->tcf_tm);
358 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
359 goto nla_put_failure;
360 spin_unlock_bh(&m->tcf_lock);
365 spin_unlock_bh(&m->tcf_lock);
370 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
371 struct netlink_callback *cb, int type,
372 const struct tc_action_ops *ops,
373 struct netlink_ext_ack *extack)
375 struct tc_action_net *tn = net_generic(net, mirred_net_id);
377 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
380 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
382 struct tc_action_net *tn = net_generic(net, mirred_net_id);
384 return tcf_idr_search(tn, a, index);
387 static int mirred_device_event(struct notifier_block *unused,
388 unsigned long event, void *ptr)
390 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
391 struct tcf_mirred *m;
394 if (event == NETDEV_UNREGISTER) {
395 spin_lock(&mirred_list_lock);
396 list_for_each_entry(m, &mirred_list, tcfm_list) {
397 spin_lock_bh(&m->tcf_lock);
398 if (tcf_mirred_dev_dereference(m) == dev) {
400 /* Note : no rcu grace period necessary, as
401 * net_device are already rcu protected.
403 RCU_INIT_POINTER(m->tcfm_dev, NULL);
405 spin_unlock_bh(&m->tcf_lock);
407 spin_unlock(&mirred_list_lock);
413 static struct notifier_block mirred_device_notifier = {
414 .notifier_call = mirred_device_event,
417 static void tcf_mirred_dev_put(void *priv)
419 struct net_device *dev = priv;
424 static struct net_device *
425 tcf_mirred_get_dev(const struct tc_action *a,
426 tc_action_priv_destructor *destructor)
428 struct tcf_mirred *m = to_mirred(a);
429 struct net_device *dev;
432 dev = rcu_dereference(m->tcfm_dev);
435 *destructor = tcf_mirred_dev_put;
442 static size_t tcf_mirred_get_fill_size(const struct tc_action *act)
444 return nla_total_size(sizeof(struct tc_mirred));
447 static struct tc_action_ops act_mirred_ops = {
450 .owner = THIS_MODULE,
451 .act = tcf_mirred_act,
452 .stats_update = tcf_stats_update,
453 .dump = tcf_mirred_dump,
454 .cleanup = tcf_mirred_release,
455 .init = tcf_mirred_init,
456 .walk = tcf_mirred_walker,
457 .lookup = tcf_mirred_search,
458 .get_fill_size = tcf_mirred_get_fill_size,
459 .size = sizeof(struct tcf_mirred),
460 .get_dev = tcf_mirred_get_dev,
463 static __net_init int mirred_init_net(struct net *net)
465 struct tc_action_net *tn = net_generic(net, mirred_net_id);
467 return tc_action_net_init(net, tn, &act_mirred_ops);
470 static void __net_exit mirred_exit_net(struct list_head *net_list)
472 tc_action_net_exit(net_list, mirred_net_id);
475 static struct pernet_operations mirred_net_ops = {
476 .init = mirred_init_net,
477 .exit_batch = mirred_exit_net,
478 .id = &mirred_net_id,
479 .size = sizeof(struct tc_action_net),
482 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
483 MODULE_DESCRIPTION("Device Mirror/redirect actions");
484 MODULE_LICENSE("GPL");
486 static int __init mirred_init_module(void)
488 int err = register_netdevice_notifier(&mirred_device_notifier);
492 pr_info("Mirror/redirect action on\n");
493 err = tcf_register_action(&act_mirred_ops, &mirred_net_ops);
495 unregister_netdevice_notifier(&mirred_device_notifier);
500 static void __exit mirred_cleanup_module(void)
502 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
503 unregister_netdevice_notifier(&mirred_device_notifier);
506 module_init(mirred_init_module);
507 module_exit(mirred_cleanup_module);