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>
22 #include <net/pkt_sched.h>
23 #include <net/pkt_cls.h>
24 #include <linux/tc_act/tc_mirred.h>
25 #include <net/tc_act/tc_mirred.h>
27 static LIST_HEAD(mirred_list);
28 static DEFINE_SPINLOCK(mirred_list_lock);
30 static bool tcf_mirred_is_act_redirect(int action)
32 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
35 static bool tcf_mirred_act_wants_ingress(int action)
38 case TCA_EGRESS_REDIR:
39 case TCA_EGRESS_MIRROR:
41 case TCA_INGRESS_REDIR:
42 case TCA_INGRESS_MIRROR:
49 static bool tcf_mirred_can_reinsert(int action)
61 static struct net_device *tcf_mirred_dev_dereference(struct tcf_mirred *m)
63 return rcu_dereference_protected(m->tcfm_dev,
64 lockdep_is_held(&m->tcf_lock));
67 static void tcf_mirred_release(struct tc_action *a)
69 struct tcf_mirred *m = to_mirred(a);
70 struct net_device *dev;
72 spin_lock(&mirred_list_lock);
73 list_del(&m->tcfm_list);
74 spin_unlock(&mirred_list_lock);
76 /* last reference to action, no need to lock */
77 dev = rcu_dereference_protected(m->tcfm_dev, 1);
82 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
83 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
86 static unsigned int mirred_net_id;
87 static struct tc_action_ops act_mirred_ops;
89 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
90 struct nlattr *est, struct tc_action **a,
91 int ovr, int bind, bool rtnl_held,
93 struct netlink_ext_ack *extack)
95 struct tc_action_net *tn = net_generic(net, mirred_net_id);
96 struct nlattr *tb[TCA_MIRRED_MAX + 1];
97 struct tcf_chain *goto_ch = NULL;
98 bool mac_header_xmit = false;
99 struct tc_mirred *parm;
100 struct tcf_mirred *m;
101 struct net_device *dev;
106 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
109 ret = nla_parse_nested_deprecated(tb, TCA_MIRRED_MAX, nla,
110 mirred_policy, extack);
113 if (!tb[TCA_MIRRED_PARMS]) {
114 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
117 parm = nla_data(tb[TCA_MIRRED_PARMS]);
119 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
126 switch (parm->eaction) {
127 case TCA_EGRESS_MIRROR:
128 case TCA_EGRESS_REDIR:
129 case TCA_INGRESS_REDIR:
130 case TCA_INGRESS_MIRROR:
134 tcf_idr_release(*a, bind);
136 tcf_idr_cleanup(tn, parm->index);
137 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
142 if (!parm->ifindex) {
143 tcf_idr_cleanup(tn, parm->index);
144 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147 ret = tcf_idr_create(tn, parm->index, est, a,
148 &act_mirred_ops, bind, true);
150 tcf_idr_cleanup(tn, parm->index);
155 tcf_idr_release(*a, bind);
160 if (ret == ACT_P_CREATED)
161 INIT_LIST_HEAD(&m->tcfm_list);
163 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
167 spin_lock_bh(&m->tcf_lock);
170 dev = dev_get_by_index(net, parm->ifindex);
172 spin_unlock_bh(&m->tcf_lock);
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));
181 m->tcfm_mac_header_xmit = mac_header_xmit;
183 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
184 m->tcfm_eaction = parm->eaction;
185 spin_unlock_bh(&m->tcf_lock);
187 tcf_chain_put_by_act(goto_ch);
189 if (ret == ACT_P_CREATED) {
190 spin_lock(&mirred_list_lock);
191 list_add(&m->tcfm_list, &mirred_list);
192 spin_unlock(&mirred_list_lock);
194 tcf_idr_insert(tn, *a);
200 tcf_chain_put_by_act(goto_ch);
202 tcf_idr_release(*a, bind);
206 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
207 struct tcf_result *res)
209 struct tcf_mirred *m = to_mirred(a);
210 struct sk_buff *skb2 = skb;
211 bool m_mac_header_xmit;
212 struct net_device *dev;
220 tcf_lastuse_update(&m->tcf_tm);
221 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
223 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
224 m_eaction = READ_ONCE(m->tcfm_eaction);
225 retval = READ_ONCE(m->tcf_action);
226 dev = rcu_dereference_bh(m->tcfm_dev);
227 if (unlikely(!dev)) {
228 pr_notice_once("tc mirred: target device is gone\n");
232 if (unlikely(!(dev->flags & IFF_UP))) {
233 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
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.
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);
246 skb2 = skb_clone(skb, GFP_ATOMIC);
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
255 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
256 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
257 if (!skb_at_tc_ingress(skb)) {
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);
262 /* caught at ingress, act egress: push mac */
263 skb_push_rcsum(skb2, skb->mac_len);
267 skb2->skb_iif = skb->dev->ifindex;
270 /* mirror is always swallowed */
272 skb2->tc_redirected = 1;
273 skb2->tc_from_ingress = skb2->tc_at_ingress;
274 if (skb2->tc_from_ingress)
276 /* let's the caller reinsert the packet, if possible */
278 res->ingress = want_ingress;
279 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
280 skb_tc_reinsert(skb, res);
281 return TC_ACT_CONSUMED;
286 err = dev_queue_xmit(skb2);
288 err = netif_receive_skb(skb2);
292 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
293 if (tcf_mirred_is_act_redirect(m_eaction))
294 retval = TC_ACT_SHOT;
300 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
301 u64 lastuse, bool hw)
303 struct tcf_mirred *m = to_mirred(a);
304 struct tcf_t *tm = &m->tcf_tm;
306 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
308 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
310 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
313 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
316 unsigned char *b = skb_tail_pointer(skb);
317 struct tcf_mirred *m = to_mirred(a);
318 struct tc_mirred opt = {
319 .index = m->tcf_index,
320 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
321 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
323 struct net_device *dev;
326 spin_lock_bh(&m->tcf_lock);
327 opt.action = m->tcf_action;
328 opt.eaction = m->tcfm_eaction;
329 dev = tcf_mirred_dev_dereference(m);
331 opt.ifindex = dev->ifindex;
333 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
334 goto nla_put_failure;
336 tcf_tm_dump(&t, &m->tcf_tm);
337 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
338 goto nla_put_failure;
339 spin_unlock_bh(&m->tcf_lock);
344 spin_unlock_bh(&m->tcf_lock);
349 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
350 struct netlink_callback *cb, int type,
351 const struct tc_action_ops *ops,
352 struct netlink_ext_ack *extack)
354 struct tc_action_net *tn = net_generic(net, mirred_net_id);
356 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
359 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
361 struct tc_action_net *tn = net_generic(net, mirred_net_id);
363 return tcf_idr_search(tn, a, index);
366 static int mirred_device_event(struct notifier_block *unused,
367 unsigned long event, void *ptr)
369 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
370 struct tcf_mirred *m;
373 if (event == NETDEV_UNREGISTER) {
374 spin_lock(&mirred_list_lock);
375 list_for_each_entry(m, &mirred_list, tcfm_list) {
376 spin_lock_bh(&m->tcf_lock);
377 if (tcf_mirred_dev_dereference(m) == dev) {
379 /* Note : no rcu grace period necessary, as
380 * net_device are already rcu protected.
382 RCU_INIT_POINTER(m->tcfm_dev, NULL);
384 spin_unlock_bh(&m->tcf_lock);
386 spin_unlock(&mirred_list_lock);
392 static struct notifier_block mirred_device_notifier = {
393 .notifier_call = mirred_device_event,
396 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
398 struct tcf_mirred *m = to_mirred(a);
399 struct net_device *dev;
402 dev = rcu_dereference(m->tcfm_dev);
410 static void tcf_mirred_put_dev(struct net_device *dev)
415 static struct tc_action_ops act_mirred_ops = {
418 .owner = THIS_MODULE,
419 .act = tcf_mirred_act,
420 .stats_update = tcf_stats_update,
421 .dump = tcf_mirred_dump,
422 .cleanup = tcf_mirred_release,
423 .init = tcf_mirred_init,
424 .walk = tcf_mirred_walker,
425 .lookup = tcf_mirred_search,
426 .size = sizeof(struct tcf_mirred),
427 .get_dev = tcf_mirred_get_dev,
428 .put_dev = tcf_mirred_put_dev,
431 static __net_init int mirred_init_net(struct net *net)
433 struct tc_action_net *tn = net_generic(net, mirred_net_id);
435 return tc_action_net_init(tn, &act_mirred_ops);
438 static void __net_exit mirred_exit_net(struct list_head *net_list)
440 tc_action_net_exit(net_list, mirred_net_id);
443 static struct pernet_operations mirred_net_ops = {
444 .init = mirred_init_net,
445 .exit_batch = mirred_exit_net,
446 .id = &mirred_net_id,
447 .size = sizeof(struct tc_action_net),
450 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
451 MODULE_DESCRIPTION("Device Mirror/redirect actions");
452 MODULE_LICENSE("GPL");
454 static int __init mirred_init_module(void)
456 int err = register_netdevice_notifier(&mirred_device_notifier);
460 pr_info("Mirror/redirect action on\n");
461 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
464 static void __exit mirred_cleanup_module(void)
466 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
467 unregister_netdevice_notifier(&mirred_device_notifier);
470 module_init(mirred_init_module);
471 module_exit(mirred_cleanup_module);