staging:rtl8712 Aligned code with open parenthesis
[linux/fpc-iii.git] / net / sched / act_mirred.c
blob2d9fa6e0a1b4a7320a44799a525013273f5cf9eb
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 <linux/tc_act/tc_mirred.h>
29 #include <net/tc_act/tc_mirred.h>
31 #include <linux/if_arp.h>
33 #define MIRRED_TAB_MASK 7
34 static LIST_HEAD(mirred_list);
35 static DEFINE_SPINLOCK(mirred_list_lock);
37 static bool tcf_mirred_is_act_redirect(int action)
39 return action == TCA_EGRESS_REDIR || action == TCA_INGRESS_REDIR;
42 static u32 tcf_mirred_act_direction(int action)
44 switch (action) {
45 case TCA_EGRESS_REDIR:
46 case TCA_EGRESS_MIRROR:
47 return AT_EGRESS;
48 case TCA_INGRESS_REDIR:
49 case TCA_INGRESS_MIRROR:
50 return AT_INGRESS;
51 default:
52 BUG();
56 static void tcf_mirred_release(struct tc_action *a, int bind)
58 struct tcf_mirred *m = to_mirred(a);
59 struct net_device *dev;
61 /* We could be called either in a RCU callback or with RTNL lock held. */
62 spin_lock_bh(&mirred_list_lock);
63 list_del(&m->tcfm_list);
64 dev = rcu_dereference_protected(m->tcfm_dev, 1);
65 if (dev)
66 dev_put(dev);
67 spin_unlock_bh(&mirred_list_lock);
70 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
71 [TCA_MIRRED_PARMS] = { .len = sizeof(struct tc_mirred) },
74 static unsigned int mirred_net_id;
75 static struct tc_action_ops act_mirred_ops;
77 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
78 struct nlattr *est, struct tc_action **a, int ovr,
79 int bind)
81 struct tc_action_net *tn = net_generic(net, mirred_net_id);
82 struct nlattr *tb[TCA_MIRRED_MAX + 1];
83 bool mac_header_xmit = false;
84 struct tc_mirred *parm;
85 struct tcf_mirred *m;
86 struct net_device *dev;
87 bool exists = false;
88 int ret;
90 if (nla == NULL)
91 return -EINVAL;
92 ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
93 if (ret < 0)
94 return ret;
95 if (tb[TCA_MIRRED_PARMS] == NULL)
96 return -EINVAL;
97 parm = nla_data(tb[TCA_MIRRED_PARMS]);
99 exists = tcf_hash_check(tn, parm->index, a, bind);
100 if (exists && bind)
101 return 0;
103 switch (parm->eaction) {
104 case TCA_EGRESS_MIRROR:
105 case TCA_EGRESS_REDIR:
106 case TCA_INGRESS_REDIR:
107 case TCA_INGRESS_MIRROR:
108 break;
109 default:
110 if (exists)
111 tcf_hash_release(*a, bind);
112 return -EINVAL;
114 if (parm->ifindex) {
115 dev = __dev_get_by_index(net, parm->ifindex);
116 if (dev == NULL) {
117 if (exists)
118 tcf_hash_release(*a, bind);
119 return -ENODEV;
121 mac_header_xmit = dev_is_mac_header_xmit(dev);
122 } else {
123 dev = NULL;
126 if (!exists) {
127 if (dev == NULL)
128 return -EINVAL;
129 ret = tcf_hash_create(tn, parm->index, est, a,
130 &act_mirred_ops, bind, true);
131 if (ret)
132 return ret;
133 ret = ACT_P_CREATED;
134 } else {
135 tcf_hash_release(*a, bind);
136 if (!ovr)
137 return -EEXIST;
139 m = to_mirred(*a);
141 ASSERT_RTNL();
142 m->tcf_action = parm->action;
143 m->tcfm_eaction = parm->eaction;
144 if (dev != NULL) {
145 m->tcfm_ifindex = parm->ifindex;
146 if (ret != ACT_P_CREATED)
147 dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
148 dev_hold(dev);
149 rcu_assign_pointer(m->tcfm_dev, dev);
150 m->tcfm_mac_header_xmit = mac_header_xmit;
153 if (ret == ACT_P_CREATED) {
154 spin_lock_bh(&mirred_list_lock);
155 list_add(&m->tcfm_list, &mirred_list);
156 spin_unlock_bh(&mirred_list_lock);
157 tcf_hash_insert(tn, *a);
160 return ret;
163 static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
164 struct tcf_result *res)
166 struct tcf_mirred *m = to_mirred(a);
167 bool m_mac_header_xmit;
168 struct net_device *dev;
169 struct sk_buff *skb2;
170 int retval, err = 0;
171 int m_eaction;
172 int mac_len;
173 u32 at;
175 tcf_lastuse_update(&m->tcf_tm);
176 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
178 rcu_read_lock();
179 m_mac_header_xmit = READ_ONCE(m->tcfm_mac_header_xmit);
180 m_eaction = READ_ONCE(m->tcfm_eaction);
181 retval = READ_ONCE(m->tcf_action);
182 dev = rcu_dereference(m->tcfm_dev);
183 if (unlikely(!dev)) {
184 pr_notice_once("tc mirred: target device is gone\n");
185 goto out;
188 if (unlikely(!(dev->flags & IFF_UP))) {
189 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
190 dev->name);
191 goto out;
194 at = G_TC_AT(skb->tc_verd);
195 skb2 = skb_clone(skb, GFP_ATOMIC);
196 if (!skb2)
197 goto out;
199 /* If action's target direction differs than filter's direction,
200 * and devices expect a mac header on xmit, then mac push/pull is
201 * needed.
203 if (at != tcf_mirred_act_direction(m_eaction) && m_mac_header_xmit) {
204 if (at & AT_EGRESS) {
205 /* caught at egress, act ingress: pull mac */
206 mac_len = skb_network_header(skb) - skb_mac_header(skb);
207 skb_pull_rcsum(skb2, mac_len);
208 } else {
209 /* caught at ingress, act egress: push mac */
210 skb_push_rcsum(skb2, skb->mac_len);
214 /* mirror is always swallowed */
215 if (tcf_mirred_is_act_redirect(m_eaction))
216 skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
218 skb2->skb_iif = skb->dev->ifindex;
219 skb2->dev = dev;
220 if (tcf_mirred_act_direction(m_eaction) & AT_EGRESS)
221 err = dev_queue_xmit(skb2);
222 else
223 err = netif_receive_skb(skb2);
225 if (err) {
226 out:
227 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
228 if (tcf_mirred_is_act_redirect(m_eaction))
229 retval = TC_ACT_SHOT;
231 rcu_read_unlock();
233 return retval;
236 static void tcf_stats_update(struct tc_action *a, u64 bytes, u32 packets,
237 u64 lastuse)
239 struct tcf_mirred *m = to_mirred(a);
240 struct tcf_t *tm = &m->tcf_tm;
242 _bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
243 tm->lastuse = lastuse;
246 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind,
247 int ref)
249 unsigned char *b = skb_tail_pointer(skb);
250 struct tcf_mirred *m = to_mirred(a);
251 struct tc_mirred opt = {
252 .index = m->tcf_index,
253 .action = m->tcf_action,
254 .refcnt = m->tcf_refcnt - ref,
255 .bindcnt = m->tcf_bindcnt - bind,
256 .eaction = m->tcfm_eaction,
257 .ifindex = m->tcfm_ifindex,
259 struct tcf_t t;
261 if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
262 goto nla_put_failure;
264 tcf_tm_dump(&t, &m->tcf_tm);
265 if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
266 goto nla_put_failure;
267 return skb->len;
269 nla_put_failure:
270 nlmsg_trim(skb, b);
271 return -1;
274 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
275 struct netlink_callback *cb, int type,
276 const struct tc_action_ops *ops)
278 struct tc_action_net *tn = net_generic(net, mirred_net_id);
280 return tcf_generic_walker(tn, skb, cb, type, ops);
283 static int tcf_mirred_search(struct net *net, struct tc_action **a, u32 index)
285 struct tc_action_net *tn = net_generic(net, mirred_net_id);
287 return tcf_hash_search(tn, a, index);
290 static int mirred_device_event(struct notifier_block *unused,
291 unsigned long event, void *ptr)
293 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
294 struct tcf_mirred *m;
296 ASSERT_RTNL();
297 if (event == NETDEV_UNREGISTER) {
298 spin_lock_bh(&mirred_list_lock);
299 list_for_each_entry(m, &mirred_list, tcfm_list) {
300 if (rcu_access_pointer(m->tcfm_dev) == dev) {
301 dev_put(dev);
302 /* Note : no rcu grace period necessary, as
303 * net_device are already rcu protected.
305 RCU_INIT_POINTER(m->tcfm_dev, NULL);
308 spin_unlock_bh(&mirred_list_lock);
311 return NOTIFY_DONE;
314 static struct notifier_block mirred_device_notifier = {
315 .notifier_call = mirred_device_event,
318 static int tcf_mirred_device(const struct tc_action *a, struct net *net,
319 struct net_device **mirred_dev)
321 int ifindex = tcf_mirred_ifindex(a);
323 *mirred_dev = __dev_get_by_index(net, ifindex);
324 if (!*mirred_dev)
325 return -EINVAL;
326 return 0;
329 static struct tc_action_ops act_mirred_ops = {
330 .kind = "mirred",
331 .type = TCA_ACT_MIRRED,
332 .owner = THIS_MODULE,
333 .act = tcf_mirred,
334 .stats_update = tcf_stats_update,
335 .dump = tcf_mirred_dump,
336 .cleanup = tcf_mirred_release,
337 .init = tcf_mirred_init,
338 .walk = tcf_mirred_walker,
339 .lookup = tcf_mirred_search,
340 .size = sizeof(struct tcf_mirred),
341 .get_dev = tcf_mirred_device,
344 static __net_init int mirred_init_net(struct net *net)
346 struct tc_action_net *tn = net_generic(net, mirred_net_id);
348 return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK);
351 static void __net_exit mirred_exit_net(struct net *net)
353 struct tc_action_net *tn = net_generic(net, mirred_net_id);
355 tc_action_net_exit(tn);
358 static struct pernet_operations mirred_net_ops = {
359 .init = mirred_init_net,
360 .exit = mirred_exit_net,
361 .id = &mirred_net_id,
362 .size = sizeof(struct tc_action_net),
365 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
366 MODULE_DESCRIPTION("Device Mirror/redirect actions");
367 MODULE_LICENSE("GPL");
369 static int __init mirred_init_module(void)
371 int err = register_netdevice_notifier(&mirred_device_notifier);
372 if (err)
373 return err;
375 pr_info("Mirror/redirect action on\n");
376 return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
379 static void __exit mirred_cleanup_module(void)
381 tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
382 unregister_netdevice_notifier(&mirred_device_notifier);
385 module_init(mirred_init_module);
386 module_exit(mirred_cleanup_module);