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