misc: rtsx_usb: Use USB remote wakeup signaling for card insertion detection
[linux/fpc-iii.git] / net / sched / act_mirred.c
blobc8cf4d10c4355f934c02d407ec725670e36433b8
1 /*
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)
42 switch (action) {
43 case TCA_EGRESS_REDIR:
44 case TCA_EGRESS_MIRROR:
45 return false;
46 case TCA_INGRESS_REDIR:
47 case TCA_INGRESS_MIRROR:
48 return true;
49 default:
50 BUG();
54 static bool tcf_mirred_can_reinsert(int action)
56 switch (action) {
57 case TC_ACT_SHOT:
58 case TC_ACT_STOLEN:
59 case TC_ACT_QUEUED:
60 case TC_ACT_TRAP:
61 return true;
63 return false;
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 if (dev)
84 dev_put(dev);
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,
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 bool mac_header_xmit = false;
102 struct tc_mirred *parm;
103 struct tcf_mirred *m;
104 struct net_device *dev;
105 bool exists = false;
106 int ret, err;
108 if (!nla) {
109 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
110 return -EINVAL;
112 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy, extack);
113 if (ret < 0)
114 return ret;
115 if (!tb[TCA_MIRRED_PARMS]) {
116 NL_SET_ERR_MSG_MOD(extack, "Missing required mirred parameters");
117 return -EINVAL;
119 parm = nla_data(tb[TCA_MIRRED_PARMS]);
121 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
122 if (err < 0)
123 return err;
124 exists = err;
125 if (exists && bind)
126 return 0;
128 switch (parm->eaction) {
129 case TCA_EGRESS_MIRROR:
130 case TCA_EGRESS_REDIR:
131 case TCA_INGRESS_REDIR:
132 case TCA_INGRESS_MIRROR:
133 break;
134 default:
135 if (exists)
136 tcf_idr_release(*a, bind);
137 else
138 tcf_idr_cleanup(tn, parm->index);
139 NL_SET_ERR_MSG_MOD(extack, "Unknown mirred option");
140 return -EINVAL;
143 if (!exists) {
144 if (!parm->ifindex) {
145 tcf_idr_cleanup(tn, parm->index);
146 NL_SET_ERR_MSG_MOD(extack, "Specified device does not exist");
147 return -EINVAL;
149 ret = tcf_idr_create(tn, parm->index, est, a,
150 &act_mirred_ops, bind, true);
151 if (ret) {
152 tcf_idr_cleanup(tn, parm->index);
153 return ret;
155 ret = ACT_P_CREATED;
156 } else if (!ovr) {
157 tcf_idr_release(*a, bind);
158 return -EEXIST;
160 m = to_mirred(*a);
162 spin_lock_bh(&m->tcf_lock);
163 m->tcf_action = parm->action;
164 m->tcfm_eaction = parm->eaction;
166 if (parm->ifindex) {
167 dev = dev_get_by_index(net, parm->ifindex);
168 if (!dev) {
169 spin_unlock_bh(&m->tcf_lock);
170 tcf_idr_release(*a, bind);
171 return -ENODEV;
173 mac_header_xmit = dev_is_mac_header_xmit(dev);
174 rcu_swap_protected(m->tcfm_dev, dev,
175 lockdep_is_held(&m->tcf_lock));
176 if (dev)
177 dev_put(dev);
178 m->tcfm_mac_header_xmit = mac_header_xmit;
180 spin_unlock_bh(&m->tcf_lock);
182 if (ret == ACT_P_CREATED) {
183 spin_lock(&mirred_list_lock);
184 list_add(&m->tcfm_list, &mirred_list);
185 spin_unlock(&mirred_list_lock);
187 tcf_idr_insert(tn, *a);
190 return ret;
193 static int tcf_mirred_act(struct sk_buff *skb, const struct tc_action *a,
194 struct tcf_result *res)
196 struct tcf_mirred *m = to_mirred(a);
197 struct sk_buff *skb2 = skb;
198 bool m_mac_header_xmit;
199 struct net_device *dev;
200 int retval, err = 0;
201 bool use_reinsert;
202 bool want_ingress;
203 bool is_redirect;
204 int m_eaction;
205 int mac_len;
207 tcf_lastuse_update(&m->tcf_tm);
208 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
210 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
211 m_eaction = READ_ONCE(m->tcfm_eaction);
212 retval = READ_ONCE(m->tcf_action);
213 dev = rcu_dereference_bh(m->tcfm_dev);
214 if (unlikely(!dev)) {
215 pr_notice_once("tc mirred: target device is gone\n");
216 goto out;
219 if (unlikely(!(dev->flags & IFF_UP))) {
220 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
221 dev->name);
222 goto out;
225 /* we could easily avoid the clone only if called by ingress and clsact;
226 * since we can't easily detect the clsact caller, skip clone only for
227 * ingress - that covers the TC S/W datapath.
229 is_redirect = tcf_mirred_is_act_redirect(m_eaction);
230 use_reinsert = skb_at_tc_ingress(skb) && is_redirect &&
231 tcf_mirred_can_reinsert(retval);
232 if (!use_reinsert) {
233 skb2 = skb_clone(skb, GFP_ATOMIC);
234 if (!skb2)
235 goto out;
238 /* If action's target direction differs than filter's direction,
239 * and devices expect a mac header on xmit, then mac push/pull is
240 * needed.
242 want_ingress = tcf_mirred_act_wants_ingress(m_eaction);
243 if (skb_at_tc_ingress(skb) != want_ingress && m_mac_header_xmit) {
244 if (!skb_at_tc_ingress(skb)) {
245 /* caught at egress, act ingress: pull mac */
246 mac_len = skb_network_header(skb) - skb_mac_header(skb);
247 skb_pull_rcsum(skb2, mac_len);
248 } else {
249 /* caught at ingress, act egress: push mac */
250 skb_push_rcsum(skb2, skb->mac_len);
254 skb2->skb_iif = skb->dev->ifindex;
255 skb2->dev = dev;
257 /* mirror is always swallowed */
258 if (is_redirect) {
259 skb2->tc_redirected = 1;
260 skb2->tc_from_ingress = skb2->tc_at_ingress;
261 if (skb2->tc_from_ingress)
262 skb2->tstamp = 0;
263 /* let's the caller reinsert the packet, if possible */
264 if (use_reinsert) {
265 res->ingress = want_ingress;
266 res->qstats = this_cpu_ptr(m->common.cpu_qstats);
267 return TC_ACT_REINSERT;
271 if (!want_ingress)
272 err = dev_queue_xmit(skb2);
273 else
274 err = netif_receive_skb(skb2);
276 if (err) {
277 out:
278 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
279 if (tcf_mirred_is_act_redirect(m_eaction))
280 retval = TC_ACT_SHOT;
283 return retval;
286 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
287 u64 lastuse, bool hw)
289 struct tcf_mirred *m = to_mirred(a);
290 struct tcf_t *tm = &m->tcf_tm;
292 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
293 if (hw)
294 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
295 bytes, packets);
296 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
299 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
300 int ref)
302 unsigned char *b = skb_tail_pointer(skb);
303 struct tcf_mirred *m = to_mirred(a);
304 struct tc_mirred opt = {
305 .index = m->tcf_index,
306 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
307 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
309 struct net_device *dev;
310 struct tcf_t t;
312 spin_lock_bh(&m->tcf_lock);
313 opt.action = m->tcf_action;
314 opt.eaction = m->tcfm_eaction;
315 dev = tcf_mirred_dev_dereference(m);
316 if (dev)
317 opt.ifindex = dev->ifindex;
319 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
320 goto nla_put_failure;
322 tcf_tm_dump(&t, &m->tcf_tm);
323 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
324 goto nla_put_failure;
325 spin_unlock_bh(&m->tcf_lock);
327 return skb->len;
329 nla_put_failure:
330 spin_unlock_bh(&m->tcf_lock);
331 nlmsg_trim(skb, b);
332 return -1;
335 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
336 struct netlink_callback *cb, int type,
337 const struct tc_action_ops *ops,
338 struct netlink_ext_ack *extack)
340 struct tc_action_net *tn = net_generic(net, mirred_net_id);
342 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
345 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
347 struct tc_action_net *tn = net_generic(net, mirred_net_id);
349 return tcf_idr_search(tn, a, index);
352 static int mirred_device_event(struct notifier_block *unused,
353 unsigned long event, void *ptr)
355 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
356 struct tcf_mirred *m;
358 ASSERT_RTNL();
359 if (event == NETDEV_UNREGISTER) {
360 spin_lock(&mirred_list_lock);
361 list_for_each_entry(m, &mirred_list, tcfm_list) {
362 spin_lock_bh(&m->tcf_lock);
363 if (tcf_mirred_dev_dereference(m) == dev) {
364 dev_put(dev);
365 /* Note : no rcu grace period necessary, as
366 * net_device are already rcu protected.
368 RCU_INIT_POINTER(m->tcfm_dev, NULL);
370 spin_unlock_bh(&m->tcf_lock);
372 spin_unlock(&mirred_list_lock);
375 return NOTIFY_DONE;
378 static struct notifier_block mirred_device_notifier = {
379 .notifier_call = mirred_device_event,
382 static struct net_device *tcf_mirred_get_dev(const struct tc_action *a)
384 struct tcf_mirred *m = to_mirred(a);
385 struct net_device *dev;
387 rcu_read_lock();
388 dev = rcu_dereference(m->tcfm_dev);
389 if (dev)
390 dev_hold(dev);
391 rcu_read_unlock();
393 return dev;
396 static void tcf_mirred_put_dev(struct net_device *dev)
398 dev_put(dev);
401 static struct tc_action_ops act_mirred_ops = {
402 .kind = "mirred",
403 .type = TCA_ACT_MIRRED,
404 .owner = THIS_MODULE,
405 .act = tcf_mirred_act,
406 .stats_update = tcf_stats_update,
407 .dump = tcf_mirred_dump,
408 .cleanup = tcf_mirred_release,
409 .init = tcf_mirred_init,
410 .walk = tcf_mirred_walker,
411 .lookup = tcf_mirred_search,
412 .size = sizeof(struct tcf_mirred),
413 .get_dev = tcf_mirred_get_dev,
414 .put_dev = tcf_mirred_put_dev,
417 static __net_init int mirred_init_net(struct net *net)
419 struct tc_action_net *tn = net_generic(net, mirred_net_id);
421 return tc_action_net_init(tn, &act_mirred_ops);
424 static void __net_exit mirred_exit_net(struct list_head *net_list)
426 tc_action_net_exit(net_list, mirred_net_id);
429 static struct pernet_operations mirred_net_ops = {
430 .init = mirred_init_net,
431 .exit_batch = mirred_exit_net,
432 .id = &mirred_net_id,
433 .size = sizeof(struct tc_action_net),
436 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
437 MODULE_DESCRIPTION("Device Mirror/redirect actions");
438 MODULE_LICENSE("GPL");
440 static int __init mirred_init_module(void)
442 int err = register_netdevice_notifier(&mirred_device_notifier);
443 if (err)
444 return err;
446 pr_info("Mirror/redirect action on\n");
447 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
450 static void __exit mirred_cleanup_module(void)
452 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
453 unregister_netdevice_notifier(&mirred_device_notifier);
456 module_init(mirred_init_module);
457 module_exit(mirred_cleanup_module);