xref: /linux/net/sched/act_pedit.c (revision 09e61efd884ca68a768717d60858f138685b161b)
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 int tcf_pedit_init(struct net *net, struct nlattr *nla,
138 			  struct nlattr *est, struct tc_action **a,
139 			  struct tcf_proto *tp, u32 flags,
140 			  struct netlink_ext_ack *extack)
141 {
142 	struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
143 	bool bind = flags & TCA_ACT_FLAGS_BIND;
144 	struct nlattr *tb[TCA_PEDIT_MAX + 1];
145 	struct tcf_chain *goto_ch = NULL;
146 	struct tc_pedit_key *keys = NULL;
147 	struct tcf_pedit_key_ex *keys_ex;
148 	struct tc_pedit *parm;
149 	struct nlattr *pattr;
150 	struct tcf_pedit *p;
151 	int ret = 0, err;
152 	int i, ksize;
153 	u32 index;
154 
155 	if (!nla) {
156 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
157 		return -EINVAL;
158 	}
159 
160 	err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
161 					  pedit_policy, NULL);
162 	if (err < 0)
163 		return err;
164 
165 	pattr = tb[TCA_PEDIT_PARMS];
166 	if (!pattr)
167 		pattr = tb[TCA_PEDIT_PARMS_EX];
168 	if (!pattr) {
169 		NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
170 		return -EINVAL;
171 	}
172 
173 	parm = nla_data(pattr);
174 	if (!parm->nkeys) {
175 		NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
176 		return -EINVAL;
177 	}
178 	ksize = parm->nkeys * sizeof(struct tc_pedit_key);
179 	if (nla_len(pattr) < sizeof(*parm) + ksize) {
180 		NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
181 		return -EINVAL;
182 	}
183 
184 	keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
185 	if (IS_ERR(keys_ex))
186 		return PTR_ERR(keys_ex);
187 
188 	index = parm->index;
189 	err = tcf_idr_check_alloc(tn, &index, a, bind);
190 	if (!err) {
191 		ret = tcf_idr_create(tn, index, est, a,
192 				     &act_pedit_ops, bind, false, flags);
193 		if (ret) {
194 			tcf_idr_cleanup(tn, index);
195 			goto out_free;
196 		}
197 		ret = ACT_P_CREATED;
198 	} else if (err > 0) {
199 		if (bind)
200 			goto out_free;
201 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
202 			ret = -EEXIST;
203 			goto out_release;
204 		}
205 	} else {
206 		ret = err;
207 		goto out_free;
208 	}
209 
210 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
211 	if (err < 0) {
212 		ret = err;
213 		goto out_release;
214 	}
215 	p = to_pedit(*a);
216 	spin_lock_bh(&p->tcf_lock);
217 
218 	if (ret == ACT_P_CREATED ||
219 	    (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys)) {
220 		keys = kmalloc(ksize, GFP_ATOMIC);
221 		if (!keys) {
222 			spin_unlock_bh(&p->tcf_lock);
223 			ret = -ENOMEM;
224 			goto put_chain;
225 		}
226 		kfree(p->tcfp_keys);
227 		p->tcfp_keys = keys;
228 		p->tcfp_nkeys = parm->nkeys;
229 	}
230 	memcpy(p->tcfp_keys, parm->keys, ksize);
231 	p->tcfp_off_max_hint = 0;
232 	for (i = 0; i < p->tcfp_nkeys; ++i) {
233 		u32 cur = p->tcfp_keys[i].off;
234 
235 		/* sanitize the shift value for any later use */
236 		p->tcfp_keys[i].shift = min_t(size_t, BITS_PER_TYPE(int) - 1,
237 					      p->tcfp_keys[i].shift);
238 
239 		/* The AT option can read a single byte, we can bound the actual
240 		 * value with uchar max.
241 		 */
242 		cur += (0xff & p->tcfp_keys[i].offmask) >> p->tcfp_keys[i].shift;
243 
244 		/* Each key touches 4 bytes starting from the computed offset */
245 		p->tcfp_off_max_hint = max(p->tcfp_off_max_hint, cur + 4);
246 	}
247 
248 	p->tcfp_flags = parm->flags;
249 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
250 
251 	kfree(p->tcfp_keys_ex);
252 	p->tcfp_keys_ex = keys_ex;
253 
254 	spin_unlock_bh(&p->tcf_lock);
255 	if (goto_ch)
256 		tcf_chain_put_by_act(goto_ch);
257 	return ret;
258 
259 put_chain:
260 	if (goto_ch)
261 		tcf_chain_put_by_act(goto_ch);
262 out_release:
263 	tcf_idr_release(*a, bind);
264 out_free:
265 	kfree(keys_ex);
266 	return ret;
267 
268 }
269 
270 static void tcf_pedit_cleanup(struct tc_action *a)
271 {
272 	struct tcf_pedit *p = to_pedit(a);
273 	struct tc_pedit_key *keys = p->tcfp_keys;
274 
275 	kfree(keys);
276 	kfree(p->tcfp_keys_ex);
277 }
278 
279 static bool offset_valid(struct sk_buff *skb, int offset)
280 {
281 	if (offset > 0 && offset > skb->len)
282 		return false;
283 
284 	if  (offset < 0 && -offset > skb_headroom(skb))
285 		return false;
286 
287 	return true;
288 }
289 
290 static int pedit_skb_hdr_offset(struct sk_buff *skb,
291 				enum pedit_header_type htype, int *hoffset)
292 {
293 	int ret = -EINVAL;
294 
295 	switch (htype) {
296 	case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
297 		if (skb_mac_header_was_set(skb)) {
298 			*hoffset = skb_mac_offset(skb);
299 			ret = 0;
300 		}
301 		break;
302 	case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
303 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
304 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
305 		*hoffset = skb_network_offset(skb);
306 		ret = 0;
307 		break;
308 	case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
309 	case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
310 		if (skb_transport_header_was_set(skb)) {
311 			*hoffset = skb_transport_offset(skb);
312 			ret = 0;
313 		}
314 		break;
315 	default:
316 		ret = -EINVAL;
317 		break;
318 	}
319 
320 	return ret;
321 }
322 
323 TC_INDIRECT_SCOPE int tcf_pedit_act(struct sk_buff *skb,
324 				    const struct tc_action *a,
325 				    struct tcf_result *res)
326 {
327 	struct tcf_pedit *p = to_pedit(a);
328 	u32 max_offset;
329 	int i;
330 
331 	spin_lock(&p->tcf_lock);
332 
333 	max_offset = (skb_transport_header_was_set(skb) ?
334 		      skb_transport_offset(skb) :
335 		      skb_network_offset(skb)) +
336 		     p->tcfp_off_max_hint;
337 	if (skb_ensure_writable(skb, min(skb->len, max_offset)))
338 		goto unlock;
339 
340 	tcf_lastuse_update(&p->tcf_tm);
341 
342 	if (p->tcfp_nkeys > 0) {
343 		struct tc_pedit_key *tkey = p->tcfp_keys;
344 		struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex;
345 		enum pedit_header_type htype =
346 			TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
347 		enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
348 
349 		for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
350 			u32 *ptr, hdata;
351 			int offset = tkey->off;
352 			int hoffset;
353 			u32 val;
354 			int rc;
355 
356 			if (tkey_ex) {
357 				htype = tkey_ex->htype;
358 				cmd = tkey_ex->cmd;
359 
360 				tkey_ex++;
361 			}
362 
363 			rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
364 			if (rc) {
365 				pr_info("tc action pedit bad header type specified (0x%x)\n",
366 					htype);
367 				goto bad;
368 			}
369 
370 			if (tkey->offmask) {
371 				u8 *d, _d;
372 
373 				if (!offset_valid(skb, hoffset + tkey->at)) {
374 					pr_info("tc action pedit 'at' offset %d out of bounds\n",
375 						hoffset + tkey->at);
376 					goto bad;
377 				}
378 				d = skb_header_pointer(skb, hoffset + tkey->at,
379 						       sizeof(_d), &_d);
380 				if (!d)
381 					goto bad;
382 				offset += (*d & tkey->offmask) >> tkey->shift;
383 			}
384 
385 			if (offset % 4) {
386 				pr_info("tc action pedit offset must be on 32 bit boundaries\n");
387 				goto bad;
388 			}
389 
390 			if (!offset_valid(skb, hoffset + offset)) {
391 				pr_info("tc action pedit offset %d out of bounds\n",
392 					hoffset + offset);
393 				goto bad;
394 			}
395 
396 			ptr = skb_header_pointer(skb, hoffset + offset,
397 						 sizeof(hdata), &hdata);
398 			if (!ptr)
399 				goto bad;
400 			/* just do it, baby */
401 			switch (cmd) {
402 			case TCA_PEDIT_KEY_EX_CMD_SET:
403 				val = tkey->val;
404 				break;
405 			case TCA_PEDIT_KEY_EX_CMD_ADD:
406 				val = (*ptr + tkey->val) & ~tkey->mask;
407 				break;
408 			default:
409 				pr_info("tc action pedit bad command (%d)\n",
410 					cmd);
411 				goto bad;
412 			}
413 
414 			*ptr = ((*ptr & tkey->mask) ^ val);
415 			if (ptr == &hdata)
416 				skb_store_bits(skb, hoffset + offset, ptr, 4);
417 		}
418 
419 		goto done;
420 	} else {
421 		WARN(1, "pedit BUG: index %d\n", p->tcf_index);
422 	}
423 
424 bad:
425 	p->tcf_qstats.overlimits++;
426 done:
427 	bstats_update(&p->tcf_bstats, skb);
428 unlock:
429 	spin_unlock(&p->tcf_lock);
430 	return p->tcf_action;
431 }
432 
433 static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
434 				   u64 drops, u64 lastuse, bool hw)
435 {
436 	struct tcf_pedit *d = to_pedit(a);
437 	struct tcf_t *tm = &d->tcf_tm;
438 
439 	tcf_action_update_stats(a, bytes, packets, drops, hw);
440 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
441 }
442 
443 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
444 			  int bind, int ref)
445 {
446 	unsigned char *b = skb_tail_pointer(skb);
447 	struct tcf_pedit *p = to_pedit(a);
448 	struct tc_pedit *opt;
449 	struct tcf_t t;
450 	int s;
451 
452 	s = struct_size(opt, keys, p->tcfp_nkeys);
453 
454 	/* netlink spinlocks held above us - must use ATOMIC */
455 	opt = kzalloc(s, GFP_ATOMIC);
456 	if (unlikely(!opt))
457 		return -ENOBUFS;
458 
459 	spin_lock_bh(&p->tcf_lock);
460 	memcpy(opt->keys, p->tcfp_keys, flex_array_size(opt, keys, p->tcfp_nkeys));
461 	opt->index = p->tcf_index;
462 	opt->nkeys = p->tcfp_nkeys;
463 	opt->flags = p->tcfp_flags;
464 	opt->action = p->tcf_action;
465 	opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
466 	opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
467 
468 	if (p->tcfp_keys_ex) {
469 		if (tcf_pedit_key_ex_dump(skb,
470 					  p->tcfp_keys_ex,
471 					  p->tcfp_nkeys))
472 			goto nla_put_failure;
473 
474 		if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
475 			goto nla_put_failure;
476 	} else {
477 		if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
478 			goto nla_put_failure;
479 	}
480 
481 	tcf_tm_dump(&t, &p->tcf_tm);
482 	if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
483 		goto nla_put_failure;
484 	spin_unlock_bh(&p->tcf_lock);
485 
486 	kfree(opt);
487 	return skb->len;
488 
489 nla_put_failure:
490 	spin_unlock_bh(&p->tcf_lock);
491 	nlmsg_trim(skb, b);
492 	kfree(opt);
493 	return -1;
494 }
495 
496 static int tcf_pedit_offload_act_setup(struct tc_action *act, void *entry_data,
497 				       u32 *index_inc, bool bind,
498 				       struct netlink_ext_ack *extack)
499 {
500 	if (bind) {
501 		struct flow_action_entry *entry = entry_data;
502 		int k;
503 
504 		for (k = 0; k < tcf_pedit_nkeys(act); k++) {
505 			switch (tcf_pedit_cmd(act, k)) {
506 			case TCA_PEDIT_KEY_EX_CMD_SET:
507 				entry->id = FLOW_ACTION_MANGLE;
508 				break;
509 			case TCA_PEDIT_KEY_EX_CMD_ADD:
510 				entry->id = FLOW_ACTION_ADD;
511 				break;
512 			default:
513 				NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
514 				return -EOPNOTSUPP;
515 			}
516 			entry->mangle.htype = tcf_pedit_htype(act, k);
517 			entry->mangle.mask = tcf_pedit_mask(act, k);
518 			entry->mangle.val = tcf_pedit_val(act, k);
519 			entry->mangle.offset = tcf_pedit_offset(act, k);
520 			entry->hw_stats = tc_act_hw_stats(act->hw_stats);
521 			entry++;
522 		}
523 		*index_inc = k;
524 	} else {
525 		return -EOPNOTSUPP;
526 	}
527 
528 	return 0;
529 }
530 
531 static struct tc_action_ops act_pedit_ops = {
532 	.kind		=	"pedit",
533 	.id		=	TCA_ID_PEDIT,
534 	.owner		=	THIS_MODULE,
535 	.act		=	tcf_pedit_act,
536 	.stats_update	=	tcf_pedit_stats_update,
537 	.dump		=	tcf_pedit_dump,
538 	.cleanup	=	tcf_pedit_cleanup,
539 	.init		=	tcf_pedit_init,
540 	.offload_act_setup =	tcf_pedit_offload_act_setup,
541 	.size		=	sizeof(struct tcf_pedit),
542 };
543 
544 static __net_init int pedit_init_net(struct net *net)
545 {
546 	struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
547 
548 	return tc_action_net_init(net, tn, &act_pedit_ops);
549 }
550 
551 static void __net_exit pedit_exit_net(struct list_head *net_list)
552 {
553 	tc_action_net_exit(net_list, act_pedit_ops.net_id);
554 }
555 
556 static struct pernet_operations pedit_net_ops = {
557 	.init = pedit_init_net,
558 	.exit_batch = pedit_exit_net,
559 	.id   = &act_pedit_ops.net_id,
560 	.size = sizeof(struct tc_action_net),
561 };
562 
563 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
564 MODULE_DESCRIPTION("Generic Packet Editor actions");
565 MODULE_LICENSE("GPL");
566 
567 static int __init pedit_init_module(void)
568 {
569 	return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
570 }
571 
572 static void __exit pedit_cleanup_module(void)
573 {
574 	tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
575 }
576 
577 module_init(pedit_init_module);
578 module_exit(pedit_cleanup_module);
579