2 * net/sched/act_mirred.c packet mirroring and redirect actions
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jamal Hadi Salim (2002-4)
11 * TODO: Add ingress support (and socket redirect support)
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/gfp.h>
24 #include <linux/if_arp.h>
25 #include <net/net_namespace.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
28 #include <net/pkt_cls.h>
29 #include <linux/tc_act/tc_mirred.h>
30 #include <net/tc_act/tc_mirred.h>
32 static LIST_HEAD(mirred_list);
33 static DEFINE_SPINLOCK(mirred_list_lock);
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);
87 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
88 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
91 static unsigned int mirred_net_id;
92 static struct tc_action_ops act_mirred_ops;
94 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
95 struct nlattr *est, struct tc_action **a,
96 int ovr, int bind, bool rtnl_held,
98 struct netlink_ext_ack *extack)
100 struct tc_action_net *tn = net_generic(net, mirred_net_id);
101 struct nlattr *tb[TCA_MIRRED_MAX + 1];
102 struct tcf_chain *goto_ch = NULL;
103 bool mac_header_xmit = false;
104 struct tc_mirred *parm;
105 struct tcf_mirred *m;
106 struct net_device *dev;
111 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
114 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
117 if (!tb[TCA_MIRRED_PARMS]) {
118 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
121 parm = nla_data(tb[TCA_MIRRED_PARMS]);
123 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
130 switch (parm->eaction) {
131 case TCA_EGRESS_MIRROR:
132 case TCA_EGRESS_REDIR:
133 case TCA_INGRESS_REDIR:
134 case TCA_INGRESS_MIRROR:
138 tcf_idr_release(*a, bind);
140 tcf_idr_cleanup(tn, parm->index);
141 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
146 if (!parm->ifindex) {
147 tcf_idr_cleanup(tn, parm->index);
148 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
151 ret = tcf_idr_create(tn, parm->index, est, a,
152 &act_mirred_ops, bind, true);
154 tcf_idr_cleanup(tn, parm->index);
159 tcf_idr_release(*a, bind);
164 if (ret == ACT_P_CREATED)
165 INIT_LIST_HEAD(&m->tcfm_list);
167 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
171 spin_lock_bh(&m->tcf_lock);
174 dev = dev_get_by_index(net, parm->ifindex);
176 spin_unlock_bh(&m->tcf_lock);
180 mac_header_xmit = dev_is_mac_header_xmit(dev);
181 rcu_swap_protected(m->tcfm_dev, dev,
182 lockdep_is_held(&m->tcf_lock));
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);
198 tcf_idr_insert(tn, *a);
204 tcf_chain_put_by_act(goto_ch);
206 tcf_idr_release(*a, bind);
210 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
211 struct tcf_result *res)
213 struct tcf_mirred *m = to_mirred(a);
214 struct sk_buff *skb2 = skb;
215 bool m_mac_header_xmit;
216 struct net_device *dev;
224 tcf_lastuse_update(&m->tcf_tm);
225 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
227 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
228 m_eaction = READ_ONCE(m->tcfm_eaction);
229 retval = READ_ONCE(m->tcf_action);
230 dev = rcu_dereference_bh(m->tcfm_dev);
231 if (unlikely(!dev)) {
232 pr_notice_once("tc mirred: target device is gone\n");
236 if (unlikely(!(dev->flags & IFF_UP))) {
237 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
242 /* we could easily avoid the clone only if called by ingress and clsact;
243 * since we can't easily detect the clsact caller, skip clone only for
244 * ingress - that covers the TC S/W datapath.
246 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
247 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
248 tcf_mirred_can_reinsert(retval);
250 skb2 = skb_clone(skb, GFP_ATOMIC);
255 /* If action's target direction differs than filter's direction,
256 * and devices expect a mac header on xmit, then mac push/pull is
259 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
260 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
261 if (!skb_at_tc_ingress(skb)) {
262 /* caught at egress, act ingress: pull mac */
263 mac_len = skb_network_header(skb) - skb_mac_header(skb);
264 skb_pull_rcsum(skb2, mac_len);
266 /* caught at ingress, act egress: push mac */
267 skb_push_rcsum(skb2, skb->mac_len);
271 skb2->skb_iif = skb->dev->ifindex;
274 /* mirror is always swallowed */
276 skb2->tc_redirected = 1;
277 skb2->tc_from_ingress = skb2->tc_at_ingress;
278 if (skb2->tc_from_ingress)
280 /* let's the caller reinsert the packet, if possible */
282 res->ingress = want_ingress;
283 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
284 return TC_ACT_REINSERT;
289 err = dev_queue_xmit(skb2);
291 err = netif_receive_skb(skb2);
295 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
296 if (tcf_mirred_is_act_redirect(m_eaction))
297 retval = TC_ACT_SHOT;
303 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
304 u64 lastuse, bool hw)
306 struct tcf_mirred *m = to_mirred(a);
307 struct tcf_t *tm = &m->tcf_tm;
309 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
311 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
313 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
316 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
319 unsigned char *b = skb_tail_pointer(skb);
320 struct tcf_mirred *m = to_mirred(a);
321 struct tc_mirred opt = {
322 .index = m->tcf_index,
323 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
324 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
326 struct net_device *dev;
329 spin_lock_bh(&m->tcf_lock);
330 opt.action = m->tcf_action;
331 opt.eaction = m->tcfm_eaction;
332 dev = tcf_mirred_dev_dereference(m);
334 opt.ifindex = dev->ifindex;
336 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
337 goto nla_put_failure;
339 tcf_tm_dump(&t, &m->tcf_tm);
340 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
341 goto nla_put_failure;
342 spin_unlock_bh(&m->tcf_lock);
347 spin_unlock_bh(&m->tcf_lock);
352 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
353 struct netlink_callback *cb, int type,
354 const struct tc_action_ops *ops,
355 struct netlink_ext_ack *extack)
357 struct tc_action_net *tn = net_generic(net, mirred_net_id);
359 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
362 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
364 struct tc_action_net *tn = net_generic(net, mirred_net_id);
366 return tcf_idr_search(tn, a, index);
369 static int mirred_device_event(struct notifier_block *unused,
370 unsigned long event, void *ptr)
372 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
373 struct tcf_mirred *m;
376 if (event == NETDEV_UNREGISTER) {
377 spin_lock(&mirred_list_lock);
378 list_for_each_entry(m, &mirred_list, tcfm_list) {
379 spin_lock_bh(&m->tcf_lock);
380 if (tcf_mirred_dev_dereference(m) == dev) {
382 /* Note : no rcu grace period necessary, as
383 * net_device are already rcu protected.
385 RCU_INIT_POINTER(m->tcfm_dev, NULL);
387 spin_unlock_bh(&m->tcf_lock);
389 spin_unlock(&mirred_list_lock);
395 static struct notifier_block mirred_device_notifier = {
396 .notifier_call = mirred_device_event,
399 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
401 struct tcf_mirred *m = to_mirred(a);
402 struct net_device *dev;
405 dev = rcu_dereference(m->tcfm_dev);
413 static void tcf_mirred_put_dev(struct net_device *dev)
418 static struct tc_action_ops act_mirred_ops = {
421 .owner = THIS_MODULE,
422 .act = tcf_mirred_act,
423 .stats_update = tcf_stats_update,
424 .dump = tcf_mirred_dump,
425 .cleanup = tcf_mirred_release,
426 .init = tcf_mirred_init,
427 .walk = tcf_mirred_walker,
428 .lookup = tcf_mirred_search,
429 .size = sizeof(struct tcf_mirred),
430 .get_dev = tcf_mirred_get_dev,
431 .put_dev = tcf_mirred_put_dev,
434 static __net_init int mirred_init_net(struct net *net)
436 struct tc_action_net *tn = net_generic(net, mirred_net_id);
438 return tc_action_net_init(tn, &act_mirred_ops);
441 static void __net_exit mirred_exit_net(struct list_head *net_list)
443 tc_action_net_exit(net_list, mirred_net_id);
446 static struct pernet_operations mirred_net_ops = {
447 .init = mirred_init_net,
448 .exit_batch = mirred_exit_net,
449 .id = &mirred_net_id,
450 .size = sizeof(struct tc_action_net),
453 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
454 MODULE_DESCRIPTION("Device Mirror/redirect actions");
455 MODULE_LICENSE("GPL");
457 static int __init mirred_init_module(void)
459 int err = register_netdevice_notifier(&mirred_device_notifier);
463 pr_info("Mirror/redirect action on\n");
464 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
467 static void __exit mirred_cleanup_module(void)
469 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
470 unregister_netdevice_notifier(&mirred_device_notifier);
473 module_init(mirred_init_module);
474 module_exit(mirred_cleanup_module);