net: sched: don't release reference on action overwrite
[linux-2.6-microblaze.git] / net / sched / act_skbedit.c
1 /*
2  * Copyright (c) 2008, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Alexander Duyck <alexander.h.duyck@intel.com>
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/ip.h>
27 #include <net/ipv6.h>
28 #include <net/dsfield.h>
29
30 #include <linux/tc_act/tc_skbedit.h>
31 #include <net/tc_act/tc_skbedit.h>
32
33 static unsigned int skbedit_net_id;
34 static struct tc_action_ops act_skbedit_ops;
35
36 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
37                        struct tcf_result *res)
38 {
39         struct tcf_skbedit *d = to_skbedit(a);
40
41         spin_lock(&d->tcf_lock);
42         tcf_lastuse_update(&d->tcf_tm);
43         bstats_update(&d->tcf_bstats, skb);
44
45         if (d->flags & SKBEDIT_F_PRIORITY)
46                 skb->priority = d->priority;
47         if (d->flags & SKBEDIT_F_INHERITDSFIELD) {
48                 int wlen = skb_network_offset(skb);
49
50                 switch (tc_skb_protocol(skb)) {
51                 case htons(ETH_P_IP):
52                         wlen += sizeof(struct iphdr);
53                         if (!pskb_may_pull(skb, wlen))
54                                 goto err;
55                         skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
56                         break;
57
58                 case htons(ETH_P_IPV6):
59                         wlen += sizeof(struct ipv6hdr);
60                         if (!pskb_may_pull(skb, wlen))
61                                 goto err;
62                         skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
63                         break;
64                 }
65         }
66         if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
67             skb->dev->real_num_tx_queues > d->queue_mapping)
68                 skb_set_queue_mapping(skb, d->queue_mapping);
69         if (d->flags & SKBEDIT_F_MARK) {
70                 skb->mark &= ~d->mask;
71                 skb->mark |= d->mark & d->mask;
72         }
73         if (d->flags & SKBEDIT_F_PTYPE)
74                 skb->pkt_type = d->ptype;
75
76         spin_unlock(&d->tcf_lock);
77         return d->tcf_action;
78
79 err:
80         d->tcf_qstats.drops++;
81         spin_unlock(&d->tcf_lock);
82         return TC_ACT_SHOT;
83 }
84
85 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
86         [TCA_SKBEDIT_PARMS]             = { .len = sizeof(struct tc_skbedit) },
87         [TCA_SKBEDIT_PRIORITY]          = { .len = sizeof(u32) },
88         [TCA_SKBEDIT_QUEUE_MAPPING]     = { .len = sizeof(u16) },
89         [TCA_SKBEDIT_MARK]              = { .len = sizeof(u32) },
90         [TCA_SKBEDIT_PTYPE]             = { .len = sizeof(u16) },
91         [TCA_SKBEDIT_MASK]              = { .len = sizeof(u32) },
92         [TCA_SKBEDIT_FLAGS]             = { .len = sizeof(u64) },
93 };
94
95 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
96                             struct nlattr *est, struct tc_action **a,
97                             int ovr, int bind, bool rtnl_held,
98                             struct netlink_ext_ack *extack)
99 {
100         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
101         struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
102         struct tc_skbedit *parm;
103         struct tcf_skbedit *d;
104         u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
105         u16 *queue_mapping = NULL, *ptype = NULL;
106         bool exists = false;
107         int ret = 0, err;
108
109         if (nla == NULL)
110                 return -EINVAL;
111
112         err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
113         if (err < 0)
114                 return err;
115
116         if (tb[TCA_SKBEDIT_PARMS] == NULL)
117                 return -EINVAL;
118
119         if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
120                 flags |= SKBEDIT_F_PRIORITY;
121                 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
122         }
123
124         if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
125                 flags |= SKBEDIT_F_QUEUE_MAPPING;
126                 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
127         }
128
129         if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
130                 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
131                 if (!skb_pkt_type_ok(*ptype))
132                         return -EINVAL;
133                 flags |= SKBEDIT_F_PTYPE;
134         }
135
136         if (tb[TCA_SKBEDIT_MARK] != NULL) {
137                 flags |= SKBEDIT_F_MARK;
138                 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
139         }
140
141         if (tb[TCA_SKBEDIT_MASK] != NULL) {
142                 flags |= SKBEDIT_F_MASK;
143                 mask = nla_data(tb[TCA_SKBEDIT_MASK]);
144         }
145
146         if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
147                 u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
148
149                 if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
150                         flags |= SKBEDIT_F_INHERITDSFIELD;
151         }
152
153         parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
154
155         exists = tcf_idr_check(tn, parm->index, a, bind);
156         if (exists && bind)
157                 return 0;
158
159         if (!flags) {
160                 if (exists)
161                         tcf_idr_release(*a, bind);
162                 return -EINVAL;
163         }
164
165         if (!exists) {
166                 ret = tcf_idr_create(tn, parm->index, est, a,
167                                      &act_skbedit_ops, bind, false);
168                 if (ret)
169                         return ret;
170
171                 d = to_skbedit(*a);
172                 ret = ACT_P_CREATED;
173         } else {
174                 d = to_skbedit(*a);
175                 if (!ovr) {
176                         tcf_idr_release(*a, bind);
177                         return -EEXIST;
178                 }
179         }
180
181         spin_lock_bh(&d->tcf_lock);
182
183         d->flags = flags;
184         if (flags & SKBEDIT_F_PRIORITY)
185                 d->priority = *priority;
186         if (flags & SKBEDIT_F_QUEUE_MAPPING)
187                 d->queue_mapping = *queue_mapping;
188         if (flags & SKBEDIT_F_MARK)
189                 d->mark = *mark;
190         if (flags & SKBEDIT_F_PTYPE)
191                 d->ptype = *ptype;
192         /* default behaviour is to use all the bits */
193         d->mask = 0xffffffff;
194         if (flags & SKBEDIT_F_MASK)
195                 d->mask = *mask;
196
197         d->tcf_action = parm->action;
198
199         spin_unlock_bh(&d->tcf_lock);
200
201         if (ret == ACT_P_CREATED)
202                 tcf_idr_insert(tn, *a);
203         return ret;
204 }
205
206 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
207                             int bind, int ref)
208 {
209         unsigned char *b = skb_tail_pointer(skb);
210         struct tcf_skbedit *d = to_skbedit(a);
211         struct tc_skbedit opt = {
212                 .index   = d->tcf_index,
213                 .refcnt  = refcount_read(&d->tcf_refcnt) - ref,
214                 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
215                 .action  = d->tcf_action,
216         };
217         struct tcf_t t;
218         u64 pure_flags = 0;
219
220         if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
221                 goto nla_put_failure;
222         if ((d->flags & SKBEDIT_F_PRIORITY) &&
223             nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
224                 goto nla_put_failure;
225         if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
226             nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
227                 goto nla_put_failure;
228         if ((d->flags & SKBEDIT_F_MARK) &&
229             nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
230                 goto nla_put_failure;
231         if ((d->flags & SKBEDIT_F_PTYPE) &&
232             nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
233                 goto nla_put_failure;
234         if ((d->flags & SKBEDIT_F_MASK) &&
235             nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
236                 goto nla_put_failure;
237         if (d->flags & SKBEDIT_F_INHERITDSFIELD)
238                 pure_flags |= SKBEDIT_F_INHERITDSFIELD;
239         if (pure_flags != 0 &&
240             nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
241                 goto nla_put_failure;
242
243         tcf_tm_dump(&t, &d->tcf_tm);
244         if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
245                 goto nla_put_failure;
246         return skb->len;
247
248 nla_put_failure:
249         nlmsg_trim(skb, b);
250         return -1;
251 }
252
253 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
254                               struct netlink_callback *cb, int type,
255                               const struct tc_action_ops *ops,
256                               struct netlink_ext_ack *extack)
257 {
258         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
259
260         return tcf_generic_walker(tn, skb, cb, type, ops, extack);
261 }
262
263 static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index,
264                               struct netlink_ext_ack *extack)
265 {
266         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
267
268         return tcf_idr_search(tn, a, index);
269 }
270
271 static int tcf_skbedit_delete(struct net *net, u32 index)
272 {
273         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
274
275         return tcf_idr_delete_index(tn, index);
276 }
277
278 static struct tc_action_ops act_skbedit_ops = {
279         .kind           =       "skbedit",
280         .type           =       TCA_ACT_SKBEDIT,
281         .owner          =       THIS_MODULE,
282         .act            =       tcf_skbedit,
283         .dump           =       tcf_skbedit_dump,
284         .init           =       tcf_skbedit_init,
285         .walk           =       tcf_skbedit_walker,
286         .lookup         =       tcf_skbedit_search,
287         .delete         =       tcf_skbedit_delete,
288         .size           =       sizeof(struct tcf_skbedit),
289 };
290
291 static __net_init int skbedit_init_net(struct net *net)
292 {
293         struct tc_action_net *tn = net_generic(net, skbedit_net_id);
294
295         return tc_action_net_init(tn, &act_skbedit_ops);
296 }
297
298 static void __net_exit skbedit_exit_net(struct list_head *net_list)
299 {
300         tc_action_net_exit(net_list, skbedit_net_id);
301 }
302
303 static struct pernet_operations skbedit_net_ops = {
304         .init = skbedit_init_net,
305         .exit_batch = skbedit_exit_net,
306         .id   = &skbedit_net_id,
307         .size = sizeof(struct tc_action_net),
308 };
309
310 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
311 MODULE_DESCRIPTION("SKB Editing");
312 MODULE_LICENSE("GPL");
313
314 static int __init skbedit_init_module(void)
315 {
316         return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
317 }
318
319 static void __exit skbedit_cleanup_module(void)
320 {
321         tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
322 }
323
324 module_init(skbedit_init_module);
325 module_exit(skbedit_cleanup_module);