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