]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/sched/act_mirred.c
net_sched: make tcf_hash_check() boolean
[karo-tx-linux.git] / net / sched / act_mirred.c
1 /*
2  * net/sched/act_mirred.c       packet mirroring and redirect actions
3  *
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.
8  *
9  * Authors:     Jamal Hadi Salim (2002-4)
10  *
11  * TODO: Add ingress support (and socket redirect support)
12  *
13  */
14
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 <net/net_namespace.h>
25 #include <net/netlink.h>
26 #include <net/pkt_sched.h>
27 #include <linux/tc_act/tc_mirred.h>
28 #include <net/tc_act/tc_mirred.h>
29
30 #include <linux/if_arp.h>
31
32 #define MIRRED_TAB_MASK     7
33 static LIST_HEAD(mirred_list);
34 static DEFINE_SPINLOCK(mirred_list_lock);
35
36 static void tcf_mirred_release(struct tc_action *a, int bind)
37 {
38         struct tcf_mirred *m = to_mirred(a);
39         struct net_device *dev;
40
41         /* We could be called either in a RCU callback or with RTNL lock held. */
42         spin_lock_bh(&mirred_list_lock);
43         list_del(&m->tcfm_list);
44         dev = rcu_dereference_protected(m->tcfm_dev, 1);
45         if (dev)
46                 dev_put(dev);
47         spin_unlock_bh(&mirred_list_lock);
48 }
49
50 static const struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
51         [TCA_MIRRED_PARMS]      = { .len = sizeof(struct tc_mirred) },
52 };
53
54 static int mirred_net_id;
55
56 static int tcf_mirred_init(struct net *net, struct nlattr *nla,
57                            struct nlattr *est, struct tc_action *a, int ovr,
58                            int bind)
59 {
60         struct tc_action_net *tn = net_generic(net, mirred_net_id);
61         struct nlattr *tb[TCA_MIRRED_MAX + 1];
62         struct tc_mirred *parm;
63         struct tcf_mirred *m;
64         struct net_device *dev;
65         int ret, ok_push = 0;
66         bool exists = false;
67
68         if (nla == NULL)
69                 return -EINVAL;
70         ret = nla_parse_nested(tb, TCA_MIRRED_MAX, nla, mirred_policy);
71         if (ret < 0)
72                 return ret;
73         if (tb[TCA_MIRRED_PARMS] == NULL)
74                 return -EINVAL;
75         parm = nla_data(tb[TCA_MIRRED_PARMS]);
76
77         exists = tcf_hash_check(tn, parm->index, a, bind);
78         if (exists && bind)
79                 return 0;
80
81         switch (parm->eaction) {
82         case TCA_EGRESS_MIRROR:
83         case TCA_EGRESS_REDIR:
84                 break;
85         default:
86                 if (exists)
87                         tcf_hash_release(a, bind);
88                 return -EINVAL;
89         }
90         if (parm->ifindex) {
91                 dev = __dev_get_by_index(net, parm->ifindex);
92                 if (dev == NULL) {
93                         if (exists)
94                                 tcf_hash_release(a, bind);
95                         return -ENODEV;
96                 }
97                 switch (dev->type) {
98                 case ARPHRD_TUNNEL:
99                 case ARPHRD_TUNNEL6:
100                 case ARPHRD_SIT:
101                 case ARPHRD_IPGRE:
102                 case ARPHRD_VOID:
103                 case ARPHRD_NONE:
104                         ok_push = 0;
105                         break;
106                 default:
107                         ok_push = 1;
108                         break;
109                 }
110         } else {
111                 dev = NULL;
112         }
113
114         if (!exists) {
115                 if (dev == NULL)
116                         return -EINVAL;
117                 ret = tcf_hash_create(tn, parm->index, est, a,
118                                       sizeof(*m), bind, true);
119                 if (ret)
120                         return ret;
121                 ret = ACT_P_CREATED;
122         } else {
123                 tcf_hash_release(a, bind);
124                 if (!ovr)
125                         return -EEXIST;
126         }
127         m = to_mirred(a);
128
129         ASSERT_RTNL();
130         m->tcf_action = parm->action;
131         m->tcfm_eaction = parm->eaction;
132         if (dev != NULL) {
133                 m->tcfm_ifindex = parm->ifindex;
134                 if (ret != ACT_P_CREATED)
135                         dev_put(rcu_dereference_protected(m->tcfm_dev, 1));
136                 dev_hold(dev);
137                 rcu_assign_pointer(m->tcfm_dev, dev);
138                 m->tcfm_ok_push = ok_push;
139         }
140
141         if (ret == ACT_P_CREATED) {
142                 spin_lock_bh(&mirred_list_lock);
143                 list_add(&m->tcfm_list, &mirred_list);
144                 spin_unlock_bh(&mirred_list_lock);
145                 tcf_hash_insert(tn, a);
146         }
147
148         return ret;
149 }
150
151 static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
152                       struct tcf_result *res)
153 {
154         struct tcf_mirred *m = a->priv;
155         struct net_device *dev;
156         struct sk_buff *skb2;
157         int retval, err;
158         u32 at;
159
160         tcf_lastuse_update(&m->tcf_tm);
161         bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
162
163         rcu_read_lock();
164         retval = READ_ONCE(m->tcf_action);
165         dev = rcu_dereference(m->tcfm_dev);
166         if (unlikely(!dev)) {
167                 pr_notice_once("tc mirred: target device is gone\n");
168                 goto out;
169         }
170
171         if (unlikely(!(dev->flags & IFF_UP))) {
172                 net_notice_ratelimited("tc mirred to Houston: device %s is down\n",
173                                        dev->name);
174                 goto out;
175         }
176
177         at = G_TC_AT(skb->tc_verd);
178         skb2 = skb_clone(skb, GFP_ATOMIC);
179         if (!skb2)
180                 goto out;
181
182         if (!(at & AT_EGRESS)) {
183                 if (m->tcfm_ok_push)
184                         skb_push(skb2, skb->mac_len);
185         }
186
187         /* mirror is always swallowed */
188         if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
189                 skb2->tc_verd = SET_TC_FROM(skb2->tc_verd, at);
190
191         skb2->skb_iif = skb->dev->ifindex;
192         skb2->dev = dev;
193         err = dev_queue_xmit(skb2);
194
195         if (err) {
196 out:
197                 qstats_overlimit_inc(this_cpu_ptr(m->common.cpu_qstats));
198                 if (m->tcfm_eaction != TCA_EGRESS_MIRROR)
199                         retval = TC_ACT_SHOT;
200         }
201         rcu_read_unlock();
202
203         return retval;
204 }
205
206 static int tcf_mirred_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
207 {
208         unsigned char *b = skb_tail_pointer(skb);
209         struct tcf_mirred *m = a->priv;
210         struct tc_mirred opt = {
211                 .index   = m->tcf_index,
212                 .action  = m->tcf_action,
213                 .refcnt  = m->tcf_refcnt - ref,
214                 .bindcnt = m->tcf_bindcnt - bind,
215                 .eaction = m->tcfm_eaction,
216                 .ifindex = m->tcfm_ifindex,
217         };
218         struct tcf_t t;
219
220         if (nla_put(skb, TCA_MIRRED_PARMS, sizeof(opt), &opt))
221                 goto nla_put_failure;
222
223         tcf_tm_dump(&t, &m->tcf_tm);
224         if (nla_put_64bit(skb, TCA_MIRRED_TM, sizeof(t), &t, TCA_MIRRED_PAD))
225                 goto nla_put_failure;
226         return skb->len;
227
228 nla_put_failure:
229         nlmsg_trim(skb, b);
230         return -1;
231 }
232
233 static int tcf_mirred_walker(struct net *net, struct sk_buff *skb,
234                              struct netlink_callback *cb, int type,
235                              struct tc_action *a)
236 {
237         struct tc_action_net *tn = net_generic(net, mirred_net_id);
238
239         return tcf_generic_walker(tn, skb, cb, type, a);
240 }
241
242 static int tcf_mirred_search(struct net *net, struct tc_action *a, u32 index)
243 {
244         struct tc_action_net *tn = net_generic(net, mirred_net_id);
245
246         return tcf_hash_search(tn, a, index);
247 }
248
249 static int mirred_device_event(struct notifier_block *unused,
250                                unsigned long event, void *ptr)
251 {
252         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
253         struct tcf_mirred *m;
254
255         ASSERT_RTNL();
256         if (event == NETDEV_UNREGISTER) {
257                 spin_lock_bh(&mirred_list_lock);
258                 list_for_each_entry(m, &mirred_list, tcfm_list) {
259                         if (rcu_access_pointer(m->tcfm_dev) == dev) {
260                                 dev_put(dev);
261                                 /* Note : no rcu grace period necessary, as
262                                  * net_device are already rcu protected.
263                                  */
264                                 RCU_INIT_POINTER(m->tcfm_dev, NULL);
265                         }
266                 }
267                 spin_unlock_bh(&mirred_list_lock);
268         }
269
270         return NOTIFY_DONE;
271 }
272
273 static struct notifier_block mirred_device_notifier = {
274         .notifier_call = mirred_device_event,
275 };
276
277 static struct tc_action_ops act_mirred_ops = {
278         .kind           =       "mirred",
279         .type           =       TCA_ACT_MIRRED,
280         .owner          =       THIS_MODULE,
281         .act            =       tcf_mirred,
282         .dump           =       tcf_mirred_dump,
283         .cleanup        =       tcf_mirred_release,
284         .init           =       tcf_mirred_init,
285         .walk           =       tcf_mirred_walker,
286         .lookup         =       tcf_mirred_search,
287 };
288
289 static __net_init int mirred_init_net(struct net *net)
290 {
291         struct tc_action_net *tn = net_generic(net, mirred_net_id);
292
293         return tc_action_net_init(tn, &act_mirred_ops, MIRRED_TAB_MASK);
294 }
295
296 static void __net_exit mirred_exit_net(struct net *net)
297 {
298         struct tc_action_net *tn = net_generic(net, mirred_net_id);
299
300         tc_action_net_exit(tn);
301 }
302
303 static struct pernet_operations mirred_net_ops = {
304         .init = mirred_init_net,
305         .exit = mirred_exit_net,
306         .id   = &mirred_net_id,
307         .size = sizeof(struct tc_action_net),
308 };
309
310 MODULE_AUTHOR("Jamal Hadi Salim(2002)");
311 MODULE_DESCRIPTION("Device Mirror/redirect actions");
312 MODULE_LICENSE("GPL");
313
314 static int __init mirred_init_module(void)
315 {
316         int err = register_netdevice_notifier(&mirred_device_notifier);
317         if (err)
318                 return err;
319
320         pr_info("Mirror/redirect action on\n");
321         return tcf_register_action(&act_mirred_ops, &mirred_net_ops);
322 }
323
324 static void __exit mirred_cleanup_module(void)
325 {
326         tcf_unregister_action(&act_mirred_ops, &mirred_net_ops);
327         unregister_netdevice_notifier(&mirred_device_notifier);
328 }
329
330 module_init(mirred_init_module);
331 module_exit(mirred_cleanup_module);