]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/act_pedit.c
net_sched: fix a NULL pointer deref in ipt action
[mirror_ubuntu-bionic-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
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
49720797
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);
49720797
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) ||
49720797
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;
49720797
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,
c1b52739 140 int ovr, int bind)
1da177e4 141{
ddf97ccd 142 struct tc_action_net *tn = net_generic(net, pedit_net_id);
7ba699c6 143 struct nlattr *tb[TCA_PEDIT_MAX + 1];
71d0ed70 144 struct nlattr *pattr;
1da177e4 145 struct tc_pedit *parm;
cee63723 146 int ret = 0, err;
1da177e4
LT
147 struct tcf_pedit *p;
148 struct tc_pedit_key *keys = NULL;
71d0ed70 149 struct tcf_pedit_key_ex *keys_ex;
1da177e4
LT
150 int ksize;
151
cee63723 152 if (nla == NULL)
1da177e4
LT
153 return -EINVAL;
154
fceb6435 155 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL);
cee63723
PM
156 if (err < 0)
157 return err;
158
71d0ed70
AV
159 pattr = tb[TCA_PEDIT_PARMS];
160 if (!pattr)
161 pattr = tb[TCA_PEDIT_PARMS_EX];
162 if (!pattr)
1da177e4 163 return -EINVAL;
71d0ed70
AV
164
165 parm = nla_data(pattr);
1da177e4 166 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
71d0ed70 167 if (nla_len(pattr) < sizeof(*parm) + ksize)
1da177e4
LT
168 return -EINVAL;
169
71d0ed70
AV
170 keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
171 if (IS_ERR(keys_ex))
172 return PTR_ERR(keys_ex);
173
65a206c0 174 if (!tcf_idr_check(tn, parm->index, a, bind)) {
1da177e4
LT
175 if (!parm->nkeys)
176 return -EINVAL;
65a206c0
CM
177 ret = tcf_idr_create(tn, parm->index, est, a,
178 &act_pedit_ops, bind, false);
86062033
WC
179 if (ret)
180 return ret;
a85a970a 181 p = to_pedit(*a);
1da177e4
LT
182 keys = kmalloc(ksize, GFP_KERNEL);
183 if (keys == NULL) {
f37f01ed 184 tcf_idr_release(*a, bind);
71d0ed70 185 kfree(keys_ex);
1da177e4
LT
186 return -ENOMEM;
187 }
188 ret = ACT_P_CREATED;
189 } else {
1a29321e
JHS
190 if (bind)
191 return 0;
65a206c0 192 tcf_idr_release(*a, bind);
1a29321e 193 if (!ovr)
1da177e4 194 return -EEXIST;
a85a970a 195 p = to_pedit(*a);
e9ce1cd3 196 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
1da177e4 197 keys = kmalloc(ksize, GFP_KERNEL);
71d0ed70
AV
198 if (!keys) {
199 kfree(keys_ex);
1da177e4 200 return -ENOMEM;
71d0ed70 201 }
1da177e4
LT
202 }
203 }
204
e9ce1cd3
DM
205 spin_lock_bh(&p->tcf_lock);
206 p->tcfp_flags = parm->flags;
207 p->tcf_action = parm->action;
1da177e4 208 if (keys) {
e9ce1cd3
DM
209 kfree(p->tcfp_keys);
210 p->tcfp_keys = keys;
211 p->tcfp_nkeys = parm->nkeys;
1da177e4 212 }
e9ce1cd3 213 memcpy(p->tcfp_keys, parm->keys, ksize);
71d0ed70
AV
214
215 kfree(p->tcfp_keys_ex);
216 p->tcfp_keys_ex = keys_ex;
217
e9ce1cd3 218 spin_unlock_bh(&p->tcf_lock);
1da177e4 219 if (ret == ACT_P_CREATED)
65a206c0 220 tcf_idr_insert(tn, *a);
1da177e4
LT
221 return ret;
222}
223
a5b5c958 224static void tcf_pedit_cleanup(struct tc_action *a, int bind)
1da177e4 225{
a85a970a 226 struct tcf_pedit *p = to_pedit(a);
a5b5c958
WC
227 struct tc_pedit_key *keys = p->tcfp_keys;
228 kfree(keys);
71d0ed70 229 kfree(p->tcfp_keys_ex);
1da177e4
LT
230}
231
95c2027b
AV
232static bool offset_valid(struct sk_buff *skb, int offset)
233{
234 if (offset > 0 && offset > skb->len)
235 return false;
236
237 if (offset < 0 && -offset > skb_headroom(skb))
238 return false;
239
240 return true;
241}
242
71d0ed70
AV
243static int pedit_skb_hdr_offset(struct sk_buff *skb,
244 enum pedit_header_type htype, int *hoffset)
245{
246 int ret = -EINVAL;
247
248 switch (htype) {
249 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
250 if (skb_mac_header_was_set(skb)) {
251 *hoffset = skb_mac_offset(skb);
252 ret = 0;
253 }
254 break;
255 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
256 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
257 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
258 *hoffset = skb_network_offset(skb);
259 ret = 0;
260 break;
261 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
262 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
263 if (skb_transport_header_was_set(skb)) {
264 *hoffset = skb_transport_offset(skb);
265 ret = 0;
266 }
267 break;
268 default:
269 ret = -EINVAL;
270 break;
271 };
272
273 return ret;
274}
275
dc7f9f6e 276static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
e9ce1cd3 277 struct tcf_result *res)
1da177e4 278{
a85a970a 279 struct tcf_pedit *p = to_pedit(a);
4749c3ef 280 int i;
1da177e4 281
14bbd6a5 282 if (skb_unclone(skb, GFP_ATOMIC))
cc7ec456 283 return p->tcf_action;
1da177e4 284
e9ce1cd3 285 spin_lock(&p->tcf_lock);
1da177e4 286
9c4a4e48 287 tcf_lastuse_update(&p->tcf_tm);
1da177e4 288
e9ce1cd3
DM
289 if (p->tcfp_nkeys > 0) {
290 struct tc_pedit_key *tkey = p->tcfp_keys;
71d0ed70
AV
291 struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
292 enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
853a14ba 293 enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
1da177e4 294
e9ce1cd3 295 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
db2c2417 296 u32 *ptr, _data;
1da177e4 297 int offset = tkey->off;
71d0ed70 298 int hoffset;
853a14ba 299 u32 val;
71d0ed70
AV
300 int rc;
301
302 if (tkey_ex) {
303 htype = tkey_ex->htype;
853a14ba
AV
304 cmd = tkey_ex->cmd;
305
71d0ed70
AV
306 tkey_ex++;
307 }
308
309 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
310 if (rc) {
311 pr_info("tc filter pedit bad header type specified (0x%x)\n",
312 htype);
313 goto bad;
314 }
1da177e4
LT
315
316 if (tkey->offmask) {
db2c2417
CG
317 char *d, _d;
318
71d0ed70 319 if (!offset_valid(skb, hoffset + tkey->at)) {
95c2027b 320 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
71d0ed70 321 hoffset + tkey->at);
95c2027b
AV
322 goto bad;
323 }
71d0ed70 324 d = skb_header_pointer(skb, hoffset + tkey->at, 1,
db2c2417
CG
325 &_d);
326 if (!d)
1da177e4 327 goto bad;
db2c2417 328 offset += (*d & tkey->offmask) >> tkey->shift;
1da177e4
LT
329 }
330
331 if (offset % 4) {
6ff9c364 332 pr_info("tc filter pedit"
333 " offset must be on 32 bit boundaries\n");
1da177e4
LT
334 goto bad;
335 }
95c2027b 336
71d0ed70 337 if (!offset_valid(skb, hoffset + offset)) {
95c2027b 338 pr_info("tc filter pedit offset %d out of bounds\n",
71d0ed70 339 hoffset + offset);
1da177e4
LT
340 goto bad;
341 }
342
71d0ed70 343 ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data);
db2c2417
CG
344 if (!ptr)
345 goto bad;
1da177e4 346 /* just do it, baby */
853a14ba
AV
347 switch (cmd) {
348 case TCA_PEDIT_KEY_EX_CMD_SET:
349 val = tkey->val;
350 break;
351 case TCA_PEDIT_KEY_EX_CMD_ADD:
352 val = (*ptr + tkey->val) & ~tkey->mask;
353 break;
354 default:
355 pr_info("tc filter pedit bad command (%d)\n",
356 cmd);
357 goto bad;
358 }
359
360 *ptr = ((*ptr & tkey->mask) ^ val);
db2c2417 361 if (ptr == &_data)
71d0ed70 362 skb_store_bits(skb, hoffset + offset, ptr, 4);
1da177e4 363 }
10297b99 364
1da177e4 365 goto done;
6ff9c364 366 } else
367 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
1da177e4
LT
368
369bad:
e9ce1cd3 370 p->tcf_qstats.overlimits++;
1da177e4 371done:
bfe0d029 372 bstats_update(&p->tcf_bstats, skb);
e9ce1cd3
DM
373 spin_unlock(&p->tcf_lock);
374 return p->tcf_action;
1da177e4
LT
375}
376
e9ce1cd3
DM
377static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
378 int bind, int ref)
1da177e4 379{
27a884dc 380 unsigned char *b = skb_tail_pointer(skb);
a85a970a 381 struct tcf_pedit *p = to_pedit(a);
1da177e4 382 struct tc_pedit *opt;
1da177e4 383 struct tcf_t t;
10297b99
YH
384 int s;
385
e9ce1cd3 386 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
1da177e4
LT
387
388 /* netlink spinlocks held above us - must use ATOMIC */
0da974f4 389 opt = kzalloc(s, GFP_ATOMIC);
e9ce1cd3 390 if (unlikely(!opt))
1da177e4 391 return -ENOBUFS;
1da177e4 392
e9ce1cd3
DM
393 memcpy(opt->keys, p->tcfp_keys,
394 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
395 opt->index = p->tcf_index;
396 opt->nkeys = p->tcfp_nkeys;
397 opt->flags = p->tcfp_flags;
398 opt->action = p->tcf_action;
399 opt->refcnt = p->tcf_refcnt - ref;
400 opt->bindcnt = p->tcf_bindcnt - bind;
1da177e4 401
71d0ed70 402 if (p->tcfp_keys_ex) {
49720797
DC
403 if (tcf_pedit_key_ex_dump(skb,
404 p->tcfp_keys_ex,
405 p->tcfp_nkeys))
406 goto nla_put_failure;
71d0ed70
AV
407
408 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
409 goto nla_put_failure;
410 } else {
411 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
412 goto nla_put_failure;
413 }
48d8ee16
JHS
414
415 tcf_tm_dump(&t, &p->tcf_tm);
9854518e 416 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
1b34ec43 417 goto nla_put_failure;
48d8ee16 418
541673c8 419 kfree(opt);
1da177e4
LT
420 return skb->len;
421
7ba699c6 422nla_put_failure:
dc5fc579 423 nlmsg_trim(skb, b);
541673c8 424 kfree(opt);
1da177e4
LT
425 return -1;
426}
427
ddf97ccd
WC
428static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
429 struct netlink_callback *cb, int type,
a85a970a 430 const struct tc_action_ops *ops)
ddf97ccd
WC
431{
432 struct tc_action_net *tn = net_generic(net, pedit_net_id);
433
a85a970a 434 return tcf_generic_walker(tn, skb, cb, type, ops);
ddf97ccd
WC
435}
436
a85a970a 437static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
438{
439 struct tc_action_net *tn = net_generic(net, pedit_net_id);
440
65a206c0 441 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
442}
443
e9ce1cd3 444static struct tc_action_ops act_pedit_ops = {
1da177e4
LT
445 .kind = "pedit",
446 .type = TCA_ACT_PEDIT,
1da177e4
LT
447 .owner = THIS_MODULE,
448 .act = tcf_pedit,
449 .dump = tcf_pedit_dump,
450 .cleanup = tcf_pedit_cleanup,
1da177e4 451 .init = tcf_pedit_init,
ddf97ccd
WC
452 .walk = tcf_pedit_walker,
453 .lookup = tcf_pedit_search,
a85a970a 454 .size = sizeof(struct tcf_pedit),
ddf97ccd
WC
455};
456
457static __net_init int pedit_init_net(struct net *net)
458{
459 struct tc_action_net *tn = net_generic(net, pedit_net_id);
460
8f4d0354 461 return tc_action_net_init(net, tn, &act_pedit_ops);
ddf97ccd
WC
462}
463
464static void __net_exit pedit_exit_net(struct net *net)
465{
466 struct tc_action_net *tn = net_generic(net, pedit_net_id);
467
468 tc_action_net_exit(tn);
469}
470
471static struct pernet_operations pedit_net_ops = {
472 .init = pedit_init_net,
473 .exit = pedit_exit_net,
474 .id = &pedit_net_id,
475 .size = sizeof(struct tc_action_net),
1da177e4
LT
476};
477
478MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
479MODULE_DESCRIPTION("Generic Packet Editor actions");
480MODULE_LICENSE("GPL");
481
e9ce1cd3 482static int __init pedit_init_module(void)
1da177e4 483{
ddf97ccd 484 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
1da177e4
LT
485}
486
e9ce1cd3 487static void __exit pedit_cleanup_module(void)
1da177e4 488{
ddf97ccd 489 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
1da177e4
LT
490}
491
492module_init(pedit_init_module);
493module_exit(pedit_cleanup_module);
494