]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sched/act_pedit.c
UBUNTU: [Config] CONFIG_BCM2835_THERMAL=y
[mirror_ubuntu-artful-kernel.git] / net / sched / act_pedit.c
CommitLineData
1da177e4 1/*
0c6965dd 2 * net/sched/act_pedit.c Generic packet editor
1da177e4
LT
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
1da177e4
LT
12#include <linux/types.h>
13#include <linux/kernel.h>
1da177e4 14#include <linux/string.h>
1da177e4 15#include <linux/errno.h>
1da177e4
LT
16#include <linux/skbuff.h>
17#include <linux/rtnetlink.h>
18#include <linux/module.h>
19#include <linux/init.h>
5a0e3ad6 20#include <linux/slab.h>
dc5fc579 21#include <net/netlink.h>
1da177e4
LT
22#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_pedit.h>
24#include <net/tc_act/tc_pedit.h>
71d0ed70 25#include <uapi/linux/tc_act/tc_pedit.h>
1da177e4 26
e9ce1cd3 27#define PEDIT_TAB_MASK 15
1da177e4 28
c7d03a00 29static unsigned int pedit_net_id;
a85a970a 30static struct tc_action_ops act_pedit_ops;
ddf97ccd 31
53b2bf3f 32static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
53f7e35f 33 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
71d0ed70 34 [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED },
53b2bf3f
PM
35};
36
71d0ed70
AV
37static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
38 [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 },
853a14ba 39 [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 },
71d0ed70
AV
40};
41
42static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
43 u8 n)
44{
45 struct tcf_pedit_key_ex *keys_ex;
46 struct tcf_pedit_key_ex *k;
47 const struct nlattr *ka;
48 int err = -EINVAL;
49 int rem;
50
51 if (!nla || !n)
52 return NULL;
53
54 keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
55 if (!keys_ex)
56 return ERR_PTR(-ENOMEM);
57
58 k = keys_ex;
59
60 nla_for_each_nested(ka, nla, rem) {
61 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
62
63 if (!n) {
64 err = -EINVAL;
65 goto err_out;
66 }
67 n--;
68
69 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
70 err = -EINVAL;
71 goto err_out;
72 }
73
74 err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka,
fceb6435 75 pedit_key_ex_policy, NULL);
71d0ed70
AV
76 if (err)
77 goto err_out;
78
853a14ba
AV
79 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
80 !tb[TCA_PEDIT_KEY_EX_CMD]) {
71d0ed70
AV
81 err = -EINVAL;
82 goto err_out;
83 }
84
85 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
853a14ba 86 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
71d0ed70 87
853a14ba
AV
88 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
89 k->cmd > TCA_PEDIT_CMD_MAX) {
71d0ed70
AV
90 err = -EINVAL;
91 goto err_out;
92 }
93
94 k++;
95 }
96
c4f65b09
DC
97 if (n) {
98 err = -EINVAL;
71d0ed70 99 goto err_out;
c4f65b09 100 }
71d0ed70
AV
101
102 return keys_ex;
103
104err_out:
105 kfree(keys_ex);
106 return ERR_PTR(err);
107}
108
109static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
110 struct tcf_pedit_key_ex *keys_ex, int n)
111{
112 struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX);
113
114 for (; n > 0; n--) {
115 struct nlattr *key_start;
116
117 key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX);
118
853a14ba
AV
119 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
120 nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd)) {
71d0ed70
AV
121 nlmsg_trim(skb, keys_start);
122 return -EINVAL;
123 }
124
125 nla_nest_end(skb, key_start);
126
127 keys_ex++;
128 }
129
130 nla_nest_end(skb, keys_start);
131
132 return 0;
133}
134
c1b52739 135static int tcf_pedit_init(struct net *net, struct nlattr *nla,
a85a970a 136 struct nlattr *est, struct tc_action **a,
c1b52739 137 int ovr, int bind)
1da177e4 138{
ddf97ccd 139 struct tc_action_net *tn = net_generic(net, pedit_net_id);
7ba699c6 140 struct nlattr *tb[TCA_PEDIT_MAX + 1];
71d0ed70 141 struct nlattr *pattr;
1da177e4 142 struct tc_pedit *parm;
cee63723 143 int ret = 0, err;
1da177e4
LT
144 struct tcf_pedit *p;
145 struct tc_pedit_key *keys = NULL;
71d0ed70 146 struct tcf_pedit_key_ex *keys_ex;
1da177e4
LT
147 int ksize;
148
cee63723 149 if (nla == NULL)
1da177e4
LT
150 return -EINVAL;
151
fceb6435 152 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL);
cee63723
PM
153 if (err < 0)
154 return err;
155
71d0ed70
AV
156 pattr = tb[TCA_PEDIT_PARMS];
157 if (!pattr)
158 pattr = tb[TCA_PEDIT_PARMS_EX];
159 if (!pattr)
1da177e4 160 return -EINVAL;
71d0ed70
AV
161
162 parm = nla_data(pattr);
1da177e4 163 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
71d0ed70 164 if (nla_len(pattr) < sizeof(*parm) + ksize)
1da177e4
LT
165 return -EINVAL;
166
71d0ed70
AV
167 keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
168 if (IS_ERR(keys_ex))
169 return PTR_ERR(keys_ex);
170
ddf97ccd 171 if (!tcf_hash_check(tn, parm->index, a, bind)) {
1da177e4
LT
172 if (!parm->nkeys)
173 return -EINVAL;
ddf97ccd 174 ret = tcf_hash_create(tn, parm->index, est, a,
a85a970a 175 &act_pedit_ops, bind, false);
86062033
WC
176 if (ret)
177 return ret;
a85a970a 178 p = to_pedit(*a);
1da177e4
LT
179 keys = kmalloc(ksize, GFP_KERNEL);
180 if (keys == NULL) {
a85a970a 181 tcf_hash_cleanup(*a, est);
71d0ed70 182 kfree(keys_ex);
1da177e4
LT
183 return -ENOMEM;
184 }
185 ret = ACT_P_CREATED;
186 } else {
1a29321e
JHS
187 if (bind)
188 return 0;
a85a970a 189 tcf_hash_release(*a, bind);
1a29321e 190 if (!ovr)
1da177e4 191 return -EEXIST;
a85a970a 192 p = to_pedit(*a);
e9ce1cd3 193 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
1da177e4 194 keys = kmalloc(ksize, GFP_KERNEL);
71d0ed70
AV
195 if (!keys) {
196 kfree(keys_ex);
1da177e4 197 return -ENOMEM;
71d0ed70 198 }
1da177e4
LT
199 }
200 }
201
e9ce1cd3
DM
202 spin_lock_bh(&p->tcf_lock);
203 p->tcfp_flags = parm->flags;
204 p->tcf_action = parm->action;
1da177e4 205 if (keys) {
e9ce1cd3
DM
206 kfree(p->tcfp_keys);
207 p->tcfp_keys = keys;
208 p->tcfp_nkeys = parm->nkeys;
1da177e4 209 }
e9ce1cd3 210 memcpy(p->tcfp_keys, parm->keys, ksize);
71d0ed70
AV
211
212 kfree(p->tcfp_keys_ex);
213 p->tcfp_keys_ex = keys_ex;
214
e9ce1cd3 215 spin_unlock_bh(&p->tcf_lock);
1da177e4 216 if (ret == ACT_P_CREATED)
a85a970a 217 tcf_hash_insert(tn, *a);
1da177e4
LT
218 return ret;
219}
220
a5b5c958 221static void tcf_pedit_cleanup(struct tc_action *a, int bind)
1da177e4 222{
a85a970a 223 struct tcf_pedit *p = to_pedit(a);
a5b5c958
WC
224 struct tc_pedit_key *keys = p->tcfp_keys;
225 kfree(keys);
71d0ed70 226 kfree(p->tcfp_keys_ex);
1da177e4
LT
227}
228
95c2027b
AV
229static bool offset_valid(struct sk_buff *skb, int offset)
230{
231 if (offset > 0 && offset > skb->len)
232 return false;
233
234 if (offset < 0 && -offset > skb_headroom(skb))
235 return false;
236
237 return true;
238}
239
71d0ed70
AV
240static int pedit_skb_hdr_offset(struct sk_buff *skb,
241 enum pedit_header_type htype, int *hoffset)
242{
243 int ret = -EINVAL;
244
245 switch (htype) {
246 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
247 if (skb_mac_header_was_set(skb)) {
248 *hoffset = skb_mac_offset(skb);
249 ret = 0;
250 }
251 break;
252 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
253 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
254 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
255 *hoffset = skb_network_offset(skb);
256 ret = 0;
257 break;
258 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
259 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
260 if (skb_transport_header_was_set(skb)) {
261 *hoffset = skb_transport_offset(skb);
262 ret = 0;
263 }
264 break;
265 default:
266 ret = -EINVAL;
267 break;
268 };
269
270 return ret;
271}
272
dc7f9f6e 273static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
e9ce1cd3 274 struct tcf_result *res)
1da177e4 275{
a85a970a 276 struct tcf_pedit *p = to_pedit(a);
4749c3ef 277 int i;
1da177e4 278
14bbd6a5 279 if (skb_unclone(skb, GFP_ATOMIC))
cc7ec456 280 return p->tcf_action;
1da177e4 281
e9ce1cd3 282 spin_lock(&p->tcf_lock);
1da177e4 283
9c4a4e48 284 tcf_lastuse_update(&p->tcf_tm);
1da177e4 285
e9ce1cd3
DM
286 if (p->tcfp_nkeys > 0) {
287 struct tc_pedit_key *tkey = p->tcfp_keys;
71d0ed70
AV
288 struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
289 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
853a14ba 290 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
1da177e4 291
e9ce1cd3 292 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
db2c2417 293 u32 *ptr, _data;
1da177e4 294 int offset = tkey->off;
71d0ed70 295 int hoffset;
853a14ba 296 u32 val;
71d0ed70
AV
297 int rc;
298
299 if (tkey_ex) {
300 htype = tkey_ex->htype;
853a14ba
AV
301 cmd = tkey_ex->cmd;
302
71d0ed70
AV
303 tkey_ex++;
304 }
305
306 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
307 if (rc) {
308 pr_info("tc filter pedit bad header type specified (0x%x)\n",
309 htype);
310 goto bad;
311 }
1da177e4
LT
312
313 if (tkey->offmask) {
db2c2417
CG
314 char *d, _d;
315
71d0ed70 316 if (!offset_valid(skb, hoffset + tkey->at)) {
95c2027b 317 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
71d0ed70 318 hoffset + tkey->at);
95c2027b
AV
319 goto bad;
320 }
71d0ed70 321 d = skb_header_pointer(skb, hoffset + tkey->at, 1,
db2c2417
CG
322 &_d);
323 if (!d)
1da177e4 324 goto bad;
db2c2417 325 offset += (*d & tkey->offmask) >> tkey->shift;
1da177e4
LT
326 }
327
328 if (offset % 4) {
6ff9c364 329 pr_info("tc filter pedit"
330 " offset must be on 32 bit boundaries\n");
1da177e4
LT
331 goto bad;
332 }
95c2027b 333
71d0ed70 334 if (!offset_valid(skb, hoffset + offset)) {
95c2027b 335 pr_info("tc filter pedit offset %d out of bounds\n",
71d0ed70 336 hoffset + offset);
1da177e4
LT
337 goto bad;
338 }
339
71d0ed70 340 ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data);
db2c2417
CG
341 if (!ptr)
342 goto bad;
1da177e4 343 /* just do it, baby */
853a14ba
AV
344 switch (cmd) {
345 case TCA_PEDIT_KEY_EX_CMD_SET:
346 val = tkey->val;
347 break;
348 case TCA_PEDIT_KEY_EX_CMD_ADD:
349 val = (*ptr + tkey->val) & ~tkey->mask;
350 break;
351 default:
352 pr_info("tc filter pedit bad command (%d)\n",
353 cmd);
354 goto bad;
355 }
356
357 *ptr = ((*ptr & tkey->mask) ^ val);
db2c2417 358 if (ptr == &_data)
71d0ed70 359 skb_store_bits(skb, hoffset + offset, ptr, 4);
1da177e4 360 }
10297b99 361
1da177e4 362 goto done;
6ff9c364 363 } else
364 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
1da177e4
LT
365
366bad:
e9ce1cd3 367 p->tcf_qstats.overlimits++;
1da177e4 368done:
bfe0d029 369 bstats_update(&p->tcf_bstats, skb);
e9ce1cd3
DM
370 spin_unlock(&p->tcf_lock);
371 return p->tcf_action;
1da177e4
LT
372}
373
e9ce1cd3
DM
374static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
375 int bind, int ref)
1da177e4 376{
27a884dc 377 unsigned char *b = skb_tail_pointer(skb);
a85a970a 378 struct tcf_pedit *p = to_pedit(a);
1da177e4 379 struct tc_pedit *opt;
1da177e4 380 struct tcf_t t;
10297b99
YH
381 int s;
382
e9ce1cd3 383 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
1da177e4
LT
384
385 /* netlink spinlocks held above us - must use ATOMIC */
0da974f4 386 opt = kzalloc(s, GFP_ATOMIC);
e9ce1cd3 387 if (unlikely(!opt))
1da177e4 388 return -ENOBUFS;
1da177e4 389
e9ce1cd3
DM
390 memcpy(opt->keys, p->tcfp_keys,
391 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
392 opt->index = p->tcf_index;
393 opt->nkeys = p->tcfp_nkeys;
394 opt->flags = p->tcfp_flags;
395 opt->action = p->tcf_action;
396 opt->refcnt = p->tcf_refcnt - ref;
397 opt->bindcnt = p->tcf_bindcnt - bind;
1da177e4 398
71d0ed70
AV
399 if (p->tcfp_keys_ex) {
400 tcf_pedit_key_ex_dump(skb, p->tcfp_keys_ex, p->tcfp_nkeys);
401
402 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
403 goto nla_put_failure;
404 } else {
405 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
406 goto nla_put_failure;
407 }
48d8ee16
JHS
408
409 tcf_tm_dump(&t, &p->tcf_tm);
9854518e 410 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
1b34ec43 411 goto nla_put_failure;
48d8ee16 412
541673c8 413 kfree(opt);
1da177e4
LT
414 return skb->len;
415
7ba699c6 416nla_put_failure:
dc5fc579 417 nlmsg_trim(skb, b);
541673c8 418 kfree(opt);
1da177e4
LT
419 return -1;
420}
421
ddf97ccd
WC
422static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
423 struct netlink_callback *cb, int type,
a85a970a 424 const struct tc_action_ops *ops)
ddf97ccd
WC
425{
426 struct tc_action_net *tn = net_generic(net, pedit_net_id);
427
a85a970a 428 return tcf_generic_walker(tn, skb, cb, type, ops);
ddf97ccd
WC
429}
430
a85a970a 431static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
432{
433 struct tc_action_net *tn = net_generic(net, pedit_net_id);
434
435 return tcf_hash_search(tn, a, index);
436}
437
e9ce1cd3 438static struct tc_action_ops act_pedit_ops = {
1da177e4
LT
439 .kind = "pedit",
440 .type = TCA_ACT_PEDIT,
1da177e4
LT
441 .owner = THIS_MODULE,
442 .act = tcf_pedit,
443 .dump = tcf_pedit_dump,
444 .cleanup = tcf_pedit_cleanup,
1da177e4 445 .init = tcf_pedit_init,
ddf97ccd
WC
446 .walk = tcf_pedit_walker,
447 .lookup = tcf_pedit_search,
a85a970a 448 .size = sizeof(struct tcf_pedit),
ddf97ccd
WC
449};
450
451static __net_init int pedit_init_net(struct net *net)
452{
453 struct tc_action_net *tn = net_generic(net, pedit_net_id);
454
455 return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
456}
457
458static void __net_exit pedit_exit_net(struct net *net)
459{
460 struct tc_action_net *tn = net_generic(net, pedit_net_id);
461
462 tc_action_net_exit(tn);
463}
464
465static struct pernet_operations pedit_net_ops = {
466 .init = pedit_init_net,
467 .exit = pedit_exit_net,
468 .id = &pedit_net_id,
469 .size = sizeof(struct tc_action_net),
1da177e4
LT
470};
471
472MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
473MODULE_DESCRIPTION("Generic Packet Editor actions");
474MODULE_LICENSE("GPL");
475
e9ce1cd3 476static int __init pedit_init_module(void)
1da177e4 477{
ddf97ccd 478 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
1da177e4
LT
479}
480
e9ce1cd3 481static void __exit pedit_cleanup_module(void)
1da177e4 482{
ddf97ccd 483 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
1da177e4
LT
484}
485
486module_init(pedit_init_module);
487module_exit(pedit_cleanup_module);
488