xref: /openbmc/linux/net/sched/act_pedit.c (revision c745cfb2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_pedit.c	Generic packet editor
4  *
5  * Authors:	Jamal Hadi Salim (2002-4)
6  */
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <linux/tc_act/tc_pedit.h>
20 #include <net/tc_act/tc_pedit.h>
21 #include <uapi/linux/tc_act/tc_pedit.h>
22 #include <net/pkt_cls.h>
23 #include <net/tc_wrapper.h>
24 
25 static struct tc_action_ops act_pedit_ops;
26 
27 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
28 	[TCA_PEDIT_PARMS]	= { .len = sizeof(struct tc_pedit) },
29 	[TCA_PEDIT_KEYS_EX]   = { .type = NLA_NESTED },
30 };
31 
32 static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
33 	[TCA_PEDIT_KEY_EX_HTYPE]  = { .type = NLA_U16 },
34 	[TCA_PEDIT_KEY_EX_CMD]	  = { .type = NLA_U16 },
35 };
36 
37 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
38 							u8 n)
39 {
40 	struct tcf_pedit_key_ex *keys_ex;
41 	struct tcf_pedit_key_ex *k;
42 	const struct nlattr *ka;
43 	int err = -EINVAL;
44 	int rem;
45 
46 	if (!nla)
47 		return NULL;
48 
49 	keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
50 	if (!keys_ex)
51 		return ERR_PTR(-ENOMEM);
52 
53 	k = keys_ex;
54 
55 	nla_for_each_nested(ka, nla, rem) {
56 		struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
57 
58 		if (!n) {
59 			err = -EINVAL;
60 			goto err_out;
61 		}
62 		n--;
63 
64 		if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
65 			err = -EINVAL;
66 			goto err_out;
67 		}
68 
69 		err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
70 						  ka, pedit_key_ex_policy,
71 						  NULL);
72 		if (err)
73 			goto err_out;
74 
75 		if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
76 		    !tb[TCA_PEDIT_KEY_EX_CMD]) {
77 			err = -EINVAL;
78 			goto err_out;
79 		}
80 
81 		k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
82 		k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
83 
84 		if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
85 		    k->cmd > TCA_PEDIT_CMD_MAX) {
86 			err = -EINVAL;
87 			goto err_out;
88 		}
89 
90 		k++;
91 	}
92 
93 	if (n) {
94 		err = -EINVAL;
95 		goto err_out;
96 	}
97 
98 	return keys_ex;
99 
100 err_out:
101 	kfree(keys_ex);
102 	return ERR_PTR(err);
103 }
104 
105 static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
106 				 struct tcf_pedit_key_ex *keys_ex, int n)
107 {
108 	struct nlattr *keys_start = nla_nest_start_noflag(skb,
109 							  TCA_PEDIT_KEYS_EX);
110 
111 	if (!keys_start)
112 		goto nla_failure;
113 	for (; n > 0; n--) {
114 		struct nlattr *key_start;
115 
116 		key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX);
117 		if (!key_start)
118 			goto nla_failure;
119 
120 		if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
121 		    nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
122 			goto nla_failure;
123 
124 		nla_nest_end(skb, key_start);
125 
126 		keys_ex++;
127 	}
128 
129 	nla_nest_end(skb, keys_start);
130 
131 	return 0;
132 nla_failure:
133 	nla_nest_cancel(skb, keys_start);
134 	return -EINVAL;
135 }
136 
137 static void tcf_pedit_cleanup_rcu(struct rcu_head *head)
138 {
139 	struct tcf_pedit_parms *parms =
140 		container_of(head, struct tcf_pedit_parms, rcu);
141 
142 	kfree(parms->tcfp_keys_ex);
143 	kfree(parms->tcfp_keys);
144 
145 	kfree(parms);
146 }
147 
148 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
149 			  struct nlattr *est, struct tc_action **a,
150 			  struct tcf_proto *tp, u32 flags,
151 			  struct netlink_ext_ack *extack)
152 {
153 	struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
154 	bool bind = flags & TCA_ACT_FLAGS_BIND;
155 	struct tcf_chain *goto_ch = NULL;
156 	struct tcf_pedit_parms *oparms, *nparms;
157 	struct nlattr *tb[TCA_PEDIT_MAX + 1];
158 	struct tc_pedit *parm;
159 	struct nlattr *pattr;
160 	struct tcf_pedit *p;
161 	int ret = 0, err;
162 	int i, ksize;
163 	u32 index;
164 
165 	if (!nla) {
166 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
167 		return -EINVAL;
168 	}
169 
170 	err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
171 					  pedit_policy, NULL);
172 	if (err < 0)
173 		return err;
174 
175 	pattr = tb[TCA_PEDIT_PARMS];
176 	if (!pattr)
177 		pattr = tb[TCA_PEDIT_PARMS_EX];
178 	if (!pattr) {
179 		NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
180 		return -EINVAL;
181 	}
182 
183 	parm = nla_data(pattr);
184 	if (!parm->nkeys) {
185 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
186 		return -EINVAL;
187 	}
188 	ksize = parm->nkeys * sizeof(struct tc_pedit_key);
189 	if (nla_len(pattr) < sizeof(*parm) + ksize) {
190 		NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
191 		return -EINVAL;
192 	}
193 
194 	nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
195 	if (!nparms)
196 		return -ENOMEM;
197 
198 	nparms->tcfp_keys_ex =
199 		tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
200 	if (IS_ERR(nparms->tcfp_keys_ex)) {
201 		ret = PTR_ERR(nparms->tcfp_keys_ex);
202 		goto out_free;
203 	}
204 
205 	index = parm->index;
206 	err = tcf_idr_check_alloc(tn, &index, a, bind);
207 	if (!err) {
208 		ret = tcf_idr_create_from_flags(tn, index, est, a,
209 						&act_pedit_ops, bind, flags);
210 		if (ret) {
211 			tcf_idr_cleanup(tn, index);
212 			goto out_free_ex;
213 		}
214 		ret = ACT_P_CREATED;
215 	} else if (err > 0) {
216 		if (bind)
217 			goto out_free;
218 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
219 			ret = -EEXIST;
220 			goto out_release;
221 		}
222 	} else {
223 		ret = err;
224 		goto out_free_ex;
225 	}
226 
227 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
228 	if (err < 0) {
229 		ret = err;
230 		goto out_release;
231 	}
232 
233 	nparms->tcfp_off_max_hint = 0;
234 	nparms->tcfp_flags = parm->flags;
235 	nparms->tcfp_nkeys = parm->nkeys;
236 
237 	nparms->tcfp_keys = kmalloc(ksize, GFP_KERNEL);
238 	if (!nparms->tcfp_keys) {
239 		ret = -ENOMEM;
240 		goto put_chain;
241 	}
242 
243 	memcpy(nparms->tcfp_keys, parm->keys, ksize);
244 
245 	for (i = 0; i < nparms->tcfp_nkeys; ++i) {
246 		u32 cur = nparms->tcfp_keys[i].off;
247 
248 		/* sanitize the shift value for any later use */
249 		nparms->tcfp_keys[i].shift = min_t(size_t,
250 						   BITS_PER_TYPE(int) - 1,
251 						   nparms->tcfp_keys[i].shift);
252 
253 		/* The AT option can read a single byte, we can bound the actual
254 		 * value with uchar max.
255 		 */
256 		cur += (0xff & nparms->tcfp_keys[i].offmask) >> nparms->tcfp_keys[i].shift;
257 
258 		/* Each key touches 4 bytes starting from the computed offset */
259 		nparms->tcfp_off_max_hint =
260 			max(nparms->tcfp_off_max_hint, cur + 4);
261 	}
262 
263 	p = to_pedit(*a);
264 
265 	spin_lock_bh(&p->tcf_lock);
266 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
267 	oparms = rcu_replace_pointer(p->parms, nparms, 1);
268 	spin_unlock_bh(&p->tcf_lock);
269 
270 	if (oparms)
271 		call_rcu(&oparms->rcu, tcf_pedit_cleanup_rcu);
272 
273 	if (goto_ch)
274 		tcf_chain_put_by_act(goto_ch);
275 
276 	return ret;
277 
278 put_chain:
279 	if (goto_ch)
280 		tcf_chain_put_by_act(goto_ch);
281 out_release:
282 	tcf_idr_release(*a, bind);
283 out_free_ex:
284 	kfree(nparms->tcfp_keys_ex);
285 out_free:
286 	kfree(nparms);
287 	return ret;
288 }
289 
290 static void tcf_pedit_cleanup(struct tc_action *a)
291 {
292 	struct tcf_pedit *p = to_pedit(a);
293 	struct tcf_pedit_parms *parms;
294 
295 	parms = rcu_dereference_protected(p->parms, 1);
296 
297 	if (parms)
298 		call_rcu(&parms->rcu, tcf_pedit_cleanup_rcu);
299 }
300 
301 static bool offset_valid(struct sk_buff *skb, int offset)
302 {
303 	if (offset > 0 && offset > skb->len)
304 		return false;
305 
306 	if  (offset < 0 && -offset > skb_headroom(skb))
307 		return false;
308 
309 	return true;
310 }
311 
312 static int pedit_skb_hdr_offset(struct sk_buff *skb,
313 				enum pedit_header_type htype, int *hoffset)
314 {
315 	int ret = -EINVAL;
316 
317 	switch (htype) {
318 	case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
319 		if (skb_mac_header_was_set(skb)) {
320 			*hoffset = skb_mac_offset(skb);
321 			ret = 0;
322 		}
323 		break;
324 	case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
325 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
326 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
327 		*hoffset = skb_network_offset(skb);
328 		ret = 0;
329 		break;
330 	case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
331 	case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
332 		if (skb_transport_header_was_set(skb)) {
333 			*hoffset = skb_transport_offset(skb);
334 			ret = 0;
335 		}
336 		break;
337 	default:
338 		ret = -EINVAL;
339 		break;
340 	}
341 
342 	return ret;
343 }
344 
345 TC_INDIRECT_SCOPE int tcf_pedit_act(struct sk_buff *skb,
346 				    const struct tc_action *a,
347 				    struct tcf_result *res)
348 {
349 	enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
350 	enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
351 	struct tcf_pedit *p = to_pedit(a);
352 	struct tcf_pedit_key_ex *tkey_ex;
353 	struct tcf_pedit_parms *parms;
354 	struct tc_pedit_key *tkey;
355 	u32 max_offset;
356 	int i;
357 
358 	parms = rcu_dereference_bh(p->parms);
359 
360 	max_offset = (skb_transport_header_was_set(skb) ?
361 		      skb_transport_offset(skb) :
362 		      skb_network_offset(skb)) +
363 		     parms->tcfp_off_max_hint;
364 	if (skb_ensure_writable(skb, min(skb->len, max_offset)))
365 		goto done;
366 
367 	tcf_lastuse_update(&p->tcf_tm);
368 	tcf_action_update_bstats(&p->common, skb);
369 
370 	tkey = parms->tcfp_keys;
371 	tkey_ex = parms->tcfp_keys_ex;
372 
373 	for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
374 		int offset = tkey->off;
375 		u32 *ptr, hdata;
376 		int hoffset;
377 		u32 val;
378 		int rc;
379 
380 		if (tkey_ex) {
381 			htype = tkey_ex->htype;
382 			cmd = tkey_ex->cmd;
383 
384 			tkey_ex++;
385 		}
386 
387 		rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
388 		if (rc) {
389 			pr_info("tc action pedit bad header type specified (0x%x)\n",
390 				htype);
391 			goto bad;
392 		}
393 
394 		if (tkey->offmask) {
395 			u8 *d, _d;
396 
397 			if (!offset_valid(skb, hoffset + tkey->at)) {
398 				pr_info("tc action pedit 'at' offset %d out of bounds\n",
399 					hoffset + tkey->at);
400 				goto bad;
401 			}
402 			d = skb_header_pointer(skb, hoffset + tkey->at,
403 					       sizeof(_d), &_d);
404 			if (!d)
405 				goto bad;
406 			offset += (*d & tkey->offmask) >> tkey->shift;
407 		}
408 
409 		if (offset % 4) {
410 			pr_info("tc action pedit offset must be on 32 bit boundaries\n");
411 			goto bad;
412 		}
413 
414 		if (!offset_valid(skb, hoffset + offset)) {
415 			pr_info("tc action pedit offset %d out of bounds\n",
416 				hoffset + offset);
417 			goto bad;
418 		}
419 
420 		ptr = skb_header_pointer(skb, hoffset + offset,
421 					 sizeof(hdata), &hdata);
422 		if (!ptr)
423 			goto bad;
424 		/* just do it, baby */
425 		switch (cmd) {
426 		case TCA_PEDIT_KEY_EX_CMD_SET:
427 			val = tkey->val;
428 			break;
429 		case TCA_PEDIT_KEY_EX_CMD_ADD:
430 			val = (*ptr + tkey->val) & ~tkey->mask;
431 			break;
432 		default:
433 			pr_info("tc action pedit bad command (%d)\n",
434 				cmd);
435 			goto bad;
436 		}
437 
438 		*ptr = ((*ptr & tkey->mask) ^ val);
439 		if (ptr == &hdata)
440 			skb_store_bits(skb, hoffset + offset, ptr, 4);
441 	}
442 
443 	goto done;
444 
445 bad:
446 	spin_lock(&p->tcf_lock);
447 	p->tcf_qstats.overlimits++;
448 	spin_unlock(&p->tcf_lock);
449 done:
450 	return p->tcf_action;
451 }
452 
453 static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
454 				   u64 drops, u64 lastuse, bool hw)
455 {
456 	struct tcf_pedit *d = to_pedit(a);
457 	struct tcf_t *tm = &d->tcf_tm;
458 
459 	tcf_action_update_stats(a, bytes, packets, drops, hw);
460 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
461 }
462 
463 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
464 			  int bind, int ref)
465 {
466 	unsigned char *b = skb_tail_pointer(skb);
467 	struct tcf_pedit *p = to_pedit(a);
468 	struct tcf_pedit_parms *parms;
469 	struct tc_pedit *opt;
470 	struct tcf_t t;
471 	int s;
472 
473 	spin_lock_bh(&p->tcf_lock);
474 	parms = rcu_dereference_protected(p->parms, 1);
475 	s = struct_size(opt, keys, parms->tcfp_nkeys);
476 
477 	opt = kzalloc(s, GFP_ATOMIC);
478 	if (unlikely(!opt)) {
479 		spin_unlock_bh(&p->tcf_lock);
480 		return -ENOBUFS;
481 	}
482 
483 	memcpy(opt->keys, parms->tcfp_keys,
484 	       flex_array_size(opt, keys, parms->tcfp_nkeys));
485 	opt->index = p->tcf_index;
486 	opt->nkeys = parms->tcfp_nkeys;
487 	opt->flags = parms->tcfp_flags;
488 	opt->action = p->tcf_action;
489 	opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
490 	opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
491 
492 	if (parms->tcfp_keys_ex) {
493 		if (tcf_pedit_key_ex_dump(skb, parms->tcfp_keys_ex,
494 					  parms->tcfp_nkeys))
495 			goto nla_put_failure;
496 
497 		if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
498 			goto nla_put_failure;
499 	} else {
500 		if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
501 			goto nla_put_failure;
502 	}
503 
504 	tcf_tm_dump(&t, &p->tcf_tm);
505 	if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
506 		goto nla_put_failure;
507 	spin_unlock_bh(&p->tcf_lock);
508 
509 	kfree(opt);
510 	return skb->len;
511 
512 nla_put_failure:
513 	spin_unlock_bh(&p->tcf_lock);
514 	nlmsg_trim(skb, b);
515 	kfree(opt);
516 	return -1;
517 }
518 
519 static int tcf_pedit_offload_act_setup(struct tc_action *act, void *entry_data,
520 				       u32 *index_inc, bool bind,
521 				       struct netlink_ext_ack *extack)
522 {
523 	if (bind) {
524 		struct flow_action_entry *entry = entry_data;
525 		int k;
526 
527 		for (k = 0; k < tcf_pedit_nkeys(act); k++) {
528 			switch (tcf_pedit_cmd(act, k)) {
529 			case TCA_PEDIT_KEY_EX_CMD_SET:
530 				entry->id = FLOW_ACTION_MANGLE;
531 				break;
532 			case TCA_PEDIT_KEY_EX_CMD_ADD:
533 				entry->id = FLOW_ACTION_ADD;
534 				break;
535 			default:
536 				NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
537 				return -EOPNOTSUPP;
538 			}
539 			entry->mangle.htype = tcf_pedit_htype(act, k);
540 			entry->mangle.mask = tcf_pedit_mask(act, k);
541 			entry->mangle.val = tcf_pedit_val(act, k);
542 			entry->mangle.offset = tcf_pedit_offset(act, k);
543 			entry->hw_stats = tc_act_hw_stats(act->hw_stats);
544 			entry++;
545 		}
546 		*index_inc = k;
547 	} else {
548 		struct flow_offload_action *fl_action = entry_data;
549 		u32 cmd = tcf_pedit_cmd(act, 0);
550 		int k;
551 
552 		switch (cmd) {
553 		case TCA_PEDIT_KEY_EX_CMD_SET:
554 			fl_action->id = FLOW_ACTION_MANGLE;
555 			break;
556 		case TCA_PEDIT_KEY_EX_CMD_ADD:
557 			fl_action->id = FLOW_ACTION_ADD;
558 			break;
559 		default:
560 			NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
561 			return -EOPNOTSUPP;
562 		}
563 
564 		for (k = 1; k < tcf_pedit_nkeys(act); k++) {
565 			if (cmd != tcf_pedit_cmd(act, k)) {
566 				NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
567 				return -EOPNOTSUPP;
568 			}
569 		}
570 	}
571 
572 	return 0;
573 }
574 
575 static struct tc_action_ops act_pedit_ops = {
576 	.kind		=	"pedit",
577 	.id		=	TCA_ID_PEDIT,
578 	.owner		=	THIS_MODULE,
579 	.act		=	tcf_pedit_act,
580 	.stats_update	=	tcf_pedit_stats_update,
581 	.dump		=	tcf_pedit_dump,
582 	.cleanup	=	tcf_pedit_cleanup,
583 	.init		=	tcf_pedit_init,
584 	.offload_act_setup =	tcf_pedit_offload_act_setup,
585 	.size		=	sizeof(struct tcf_pedit),
586 };
587 
588 static __net_init int pedit_init_net(struct net *net)
589 {
590 	struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
591 
592 	return tc_action_net_init(net, tn, &act_pedit_ops);
593 }
594 
595 static void __net_exit pedit_exit_net(struct list_head *net_list)
596 {
597 	tc_action_net_exit(net_list, act_pedit_ops.net_id);
598 }
599 
600 static struct pernet_operations pedit_net_ops = {
601 	.init = pedit_init_net,
602 	.exit_batch = pedit_exit_net,
603 	.id   = &act_pedit_ops.net_id,
604 	.size = sizeof(struct tc_action_net),
605 };
606 
607 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
608 MODULE_DESCRIPTION("Generic Packet Editor actions");
609 MODULE_LICENSE("GPL");
610 
611 static int __init pedit_init_module(void)
612 {
613 	return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
614 }
615 
616 static void __exit pedit_cleanup_module(void)
617 {
618 	tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
619 }
620 
621 module_init(pedit_init_module);
622 module_exit(pedit_cleanup_module);
623