Merge tag 'apparmor-pr-2018-09-06' 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
VB
203 } else {
204 return err;
1da177e4
LT
205 }
206
67b0c1a3 207 p = to_pedit(*a);
e9ce1cd3 208 spin_lock_bh(&p->tcf_lock);
67b0c1a3
VB
209
210 if (ret == ACT_P_CREATED ||
211 (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys)) {
212 keys = kmalloc(ksize, GFP_ATOMIC);
213 if (!keys) {
214 spin_unlock_bh(&p->tcf_lock);
215 ret = -ENOMEM;
216 goto out_release;
217 }
e9ce1cd3
DM
218 kfree(p->tcfp_keys);
219 p->tcfp_keys = keys;
220 p->tcfp_nkeys = parm->nkeys;
1da177e4 221 }
e9ce1cd3 222 memcpy(p->tcfp_keys, parm->keys, ksize);
71d0ed70 223
67b0c1a3
VB
224 p->tcfp_flags = parm->flags;
225 p->tcf_action = parm->action;
226
71d0ed70
AV
227 kfree(p->tcfp_keys_ex);
228 p->tcfp_keys_ex = keys_ex;
229
e9ce1cd3 230 spin_unlock_bh(&p->tcf_lock);
1da177e4 231 if (ret == ACT_P_CREATED)
65a206c0 232 tcf_idr_insert(tn, *a);
1da177e4 233 return ret;
67b0c1a3
VB
234
235out_release:
236 tcf_idr_release(*a, bind);
30e99ed6
WY
237out_free:
238 kfree(keys_ex);
239 return ret;
240
1da177e4
LT
241}
242
9a63b255 243static void tcf_pedit_cleanup(struct tc_action *a)
1da177e4 244{
a85a970a 245 struct tcf_pedit *p = to_pedit(a);
a5b5c958 246 struct tc_pedit_key *keys = p->tcfp_keys;
80f0f574 247
a5b5c958 248 kfree(keys);
71d0ed70 249 kfree(p->tcfp_keys_ex);
1da177e4
LT
250}
251
95c2027b
AV
252static bool offset_valid(struct sk_buff *skb, int offset)
253{
254 if (offset > 0 && offset > skb->len)
255 return false;
256
257 if (offset < 0 && -offset > skb_headroom(skb))
258 return false;
259
260 return true;
261}
262
71d0ed70
AV
263static int pedit_skb_hdr_offset(struct sk_buff *skb,
264 enum pedit_header_type htype, int *hoffset)
265{
266 int ret = -EINVAL;
267
268 switch (htype) {
269 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
270 if (skb_mac_header_was_set(skb)) {
271 *hoffset = skb_mac_offset(skb);
272 ret = 0;
273 }
274 break;
275 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
276 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
277 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
278 *hoffset = skb_network_offset(skb);
279 ret = 0;
280 break;
281 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
282 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
283 if (skb_transport_header_was_set(skb)) {
284 *hoffset = skb_transport_offset(skb);
285 ret = 0;
286 }
287 break;
288 default:
289 ret = -EINVAL;
290 break;
0a80848e 291 }
71d0ed70
AV
292
293 return ret;
294}
295
6a2b401c
JHS
296static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
297 struct tcf_result *res)
1da177e4 298{
a85a970a 299 struct tcf_pedit *p = to_pedit(a);
4749c3ef 300 int i;
1da177e4 301
14bbd6a5 302 if (skb_unclone(skb, GFP_ATOMIC))
cc7ec456 303 return p->tcf_action;
1da177e4 304
e9ce1cd3 305 spin_lock(&p->tcf_lock);
1da177e4 306
9c4a4e48 307 tcf_lastuse_update(&p->tcf_tm);
1da177e4 308
e9ce1cd3
DM
309 if (p->tcfp_nkeys > 0) {
310 struct tc_pedit_key *tkey = p->tcfp_keys;
71d0ed70 311 struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
80f0f574
RM
312 enum pedit_header_type htype =
313 TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
853a14ba 314 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
1da177e4 315
e9ce1cd3 316 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
544377cd 317 u32 *ptr, hdata;
1da177e4 318 int offset = tkey->off;
71d0ed70 319 int hoffset;
853a14ba 320 u32 val;
71d0ed70
AV
321 int rc;
322
323 if (tkey_ex) {
324 htype = tkey_ex->htype;
853a14ba
AV
325 cmd = tkey_ex->cmd;
326
71d0ed70
AV
327 tkey_ex++;
328 }
329
330 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
331 if (rc) {
95b0d2dc 332 pr_info("tc action pedit bad header type specified (0x%x)\n",
71d0ed70
AV
333 htype);
334 goto bad;
335 }
1da177e4
LT
336
337 if (tkey->offmask) {
43052741 338 u8 *d, _d;
db2c2417 339
71d0ed70 340 if (!offset_valid(skb, hoffset + tkey->at)) {
95b0d2dc 341 pr_info("tc action pedit 'at' offset %d out of bounds\n",
71d0ed70 342 hoffset + tkey->at);
95c2027b
AV
343 goto bad;
344 }
80f0f574 345 d = skb_header_pointer(skb, hoffset + tkey->at,
6ff7586e 346 sizeof(_d), &_d);
db2c2417 347 if (!d)
1da177e4 348 goto bad;
db2c2417 349 offset += (*d & tkey->offmask) >> tkey->shift;
1da177e4
LT
350 }
351
352 if (offset % 4) {
95b0d2dc 353 pr_info("tc action pedit offset must be on 32 bit boundaries\n");
1da177e4
LT
354 goto bad;
355 }
95c2027b 356
71d0ed70 357 if (!offset_valid(skb, hoffset + offset)) {
95b0d2dc 358 pr_info("tc action pedit offset %d out of bounds\n",
71d0ed70 359 hoffset + offset);
1da177e4
LT
360 goto bad;
361 }
362
80f0f574 363 ptr = skb_header_pointer(skb, hoffset + offset,
6ff7586e 364 sizeof(hdata), &hdata);
db2c2417
CG
365 if (!ptr)
366 goto bad;
1da177e4 367 /* just do it, baby */
853a14ba
AV
368 switch (cmd) {
369 case TCA_PEDIT_KEY_EX_CMD_SET:
370 val = tkey->val;
371 break;
372 case TCA_PEDIT_KEY_EX_CMD_ADD:
373 val = (*ptr + tkey->val) & ~tkey->mask;
374 break;
375 default:
95b0d2dc 376 pr_info("tc action pedit bad command (%d)\n",
853a14ba
AV
377 cmd);
378 goto bad;
379 }
380
381 *ptr = ((*ptr & tkey->mask) ^ val);
544377cd 382 if (ptr == &hdata)
71d0ed70 383 skb_store_bits(skb, hoffset + offset, ptr, 4);
1da177e4 384 }
10297b99 385
1da177e4 386 goto done;
80f0f574 387 } else {
6ff9c364 388 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
80f0f574 389 }
1da177e4
LT
390
391bad:
e9ce1cd3 392 p->tcf_qstats.overlimits++;
1da177e4 393done:
bfe0d029 394 bstats_update(&p->tcf_bstats, skb);
e9ce1cd3
DM
395 spin_unlock(&p->tcf_lock);
396 return p->tcf_action;
1da177e4
LT
397}
398
e9ce1cd3
DM
399static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
400 int bind, int ref)
1da177e4 401{
27a884dc 402 unsigned char *b = skb_tail_pointer(skb);
a85a970a 403 struct tcf_pedit *p = to_pedit(a);
1da177e4 404 struct tc_pedit *opt;
1da177e4 405 struct tcf_t t;
10297b99
YH
406 int s;
407
e9ce1cd3 408 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
1da177e4
LT
409
410 /* netlink spinlocks held above us - must use ATOMIC */
0da974f4 411 opt = kzalloc(s, GFP_ATOMIC);
e9ce1cd3 412 if (unlikely(!opt))
1da177e4 413 return -ENOBUFS;
1da177e4 414
67b0c1a3 415 spin_lock_bh(&p->tcf_lock);
e9ce1cd3
DM
416 memcpy(opt->keys, p->tcfp_keys,
417 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
418 opt->index = p->tcf_index;
419 opt->nkeys = p->tcfp_nkeys;
420 opt->flags = p->tcfp_flags;
421 opt->action = p->tcf_action;
036bb443
VB
422 opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
423 opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
1da177e4 424
71d0ed70 425 if (p->tcfp_keys_ex) {
85eb9af1
DC
426 if (tcf_pedit_key_ex_dump(skb,
427 p->tcfp_keys_ex,
428 p->tcfp_nkeys))
429 goto nla_put_failure;
71d0ed70
AV
430
431 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
432 goto nla_put_failure;
433 } else {
434 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
435 goto nla_put_failure;
436 }
48d8ee16
JHS
437
438 tcf_tm_dump(&t, &p->tcf_tm);
9854518e 439 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
1b34ec43 440 goto nla_put_failure;
67b0c1a3 441 spin_unlock_bh(&p->tcf_lock);
48d8ee16 442
541673c8 443 kfree(opt);
1da177e4
LT
444 return skb->len;
445
7ba699c6 446nla_put_failure:
67b0c1a3 447 spin_unlock_bh(&p->tcf_lock);
dc5fc579 448 nlmsg_trim(skb, b);
541673c8 449 kfree(opt);
1da177e4
LT
450 return -1;
451}
452
ddf97ccd
WC
453static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
454 struct netlink_callback *cb, int type,
41780105
AA
455 const struct tc_action_ops *ops,
456 struct netlink_ext_ack *extack)
ddf97ccd
WC
457{
458 struct tc_action_net *tn = net_generic(net, pedit_net_id);
459
b3620145 460 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
461}
462
331a9295
AA
463static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index,
464 struct netlink_ext_ack *extack)
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);