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 return TC_ACT_REINSERT;
285 err = dev_queue_xmit(skb2);
287 err = netif_receive_skb(skb2);
291 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
292 if (tcf_mirred_is_act_redirect(m_eaction))
293 retval = TC_ACT_SHOT;
299 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
300 u64 lastuse, bool hw)
302 struct tcf_mirred *m = to_mirred(a);
303 struct tcf_t *tm = &m->tcf_tm;
305 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
307 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
309 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
312 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
315 unsigned char *b = skb_tail_pointer(skb);
316 struct tcf_mirred *m = to_mirred(a);
317 struct tc_mirred opt = {
318 .index = m->tcf_index,
319 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
320 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
322 struct net_device *dev;
325 spin_lock_bh(&m->tcf_lock);
326 opt.action = m->tcf_action;
327 opt.eaction = m->tcfm_eaction;
328 dev = tcf_mirred_dev_dereference(m);
330 opt.ifindex = dev->ifindex;
332 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
333 goto nla_put_failure;
335 tcf_tm_dump(&t, &m->tcf_tm);
336 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
337 goto nla_put_failure;
338 spin_unlock_bh(&m->tcf_lock);
343 spin_unlock_bh(&m->tcf_lock);
348 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
349 struct netlink_callback *cb, int type,
350 const struct tc_action_ops *ops,
351 struct netlink_ext_ack *extack)
353 struct tc_action_net *tn = net_generic(net, mirred_net_id);
355 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
358 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
360 struct tc_action_net *tn = net_generic(net, mirred_net_id);
362 return tcf_idr_search(tn, a, index);
365 static int mirred_device_event(struct notifier_block *unused,
366 unsigned long event, void *ptr)
368 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
369 struct tcf_mirred *m;
372 if (event == NETDEV_UNREGISTER) {
373 spin_lock(&mirred_list_lock);
374 list_for_each_entry(m, &mirred_list, tcfm_list) {
375 spin_lock_bh(&m->tcf_lock);
376 if (tcf_mirred_dev_dereference(m) == dev) {
378 /* Note : no rcu grace period necessary, as
379 * net_device are already rcu protected.
381 RCU_INIT_POINTER(m->tcfm_dev, NULL);
383 spin_unlock_bh(&m->tcf_lock);
385 spin_unlock(&mirred_list_lock);
391 static struct notifier_block mirred_device_notifier = {
392 .notifier_call = mirred_device_event,
395 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
397 struct tcf_mirred *m = to_mirred(a);
398 struct net_device *dev;
401 dev = rcu_dereference(m->tcfm_dev);
409 static void tcf_mirred_put_dev(struct net_device *dev)
414 static struct tc_action_ops act_mirred_ops = {
417 .owner = THIS_MODULE,
418 .act = tcf_mirred_act,
419 .stats_update = tcf_stats_update,
420 .dump = tcf_mirred_dump,
421 .cleanup = tcf_mirred_release,
422 .init = tcf_mirred_init,
423 .walk = tcf_mirred_walker,
424 .lookup = tcf_mirred_search,
425 .size = sizeof(struct tcf_mirred),
426 .get_dev = tcf_mirred_get_dev,
427 .put_dev = tcf_mirred_put_dev,
430 static __net_init int mirred_init_net(struct net *net)
432 struct tc_action_net *tn = net_generic(net, mirred_net_id);
434 return tc_action_net_init(tn, &act_mirred_ops);
437 static void __net_exit mirred_exit_net(struct list_head *net_list)
439 tc_action_net_exit(net_list, mirred_net_id);
442 static struct pernet_operations mirred_net_ops = {
443 .init = mirred_init_net,
444 .exit_batch = mirred_exit_net,
445 .id = &mirred_net_id,
446 .size = sizeof(struct tc_action_net),
449 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
450 MODULE_DESCRIPTION("Device Mirror/redirect actions");
451 MODULE_LICENSE("GPL");
453 static int __init mirred_init_module(void)
455 int err = register_netdevice_notifier(&mirred_device_notifier);
459 pr_info("Mirror/redirect action on\n");
460 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
463 static void __exit mirred_cleanup_module(void)
465 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
466 unregister_netdevice_notifier(&mirred_device_notifier);
469 module_init(mirred_init_module);
470 module_exit(mirred_cleanup_module);