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