• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_api.c	Packet action API.
4  *
5  * Author:	Jamal Hadi Salim
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/slab.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/kmod.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <net/net_namespace.h>
19 #include <net/sock.h>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22 #include <net/act_api.h>
23 #include <net/netlink.h>
24 
tcf_action_goto_chain_exec(const struct tc_action * a,struct tcf_result * res)25 static void tcf_action_goto_chain_exec(const struct tc_action *a,
26 				       struct tcf_result *res)
27 {
28 	const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
29 
30 	res->goto_tp = rcu_dereference_bh(chain->filter_chain);
31 }
32 
tcf_free_cookie_rcu(struct rcu_head * p)33 static void tcf_free_cookie_rcu(struct rcu_head *p)
34 {
35 	struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
36 
37 	kfree(cookie->data);
38 	kfree(cookie);
39 }
40 
tcf_set_action_cookie(struct tc_cookie __rcu ** old_cookie,struct tc_cookie * new_cookie)41 static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
42 				  struct tc_cookie *new_cookie)
43 {
44 	struct tc_cookie *old;
45 
46 	old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
47 	if (old)
48 		call_rcu(&old->rcu, tcf_free_cookie_rcu);
49 }
50 
tcf_action_check_ctrlact(int action,struct tcf_proto * tp,struct tcf_chain ** newchain,struct netlink_ext_ack * extack)51 int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
52 			     struct tcf_chain **newchain,
53 			     struct netlink_ext_ack *extack)
54 {
55 	int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
56 	u32 chain_index;
57 
58 	if (!opcode)
59 		ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
60 	else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
61 		ret = 0;
62 	if (ret) {
63 		NL_SET_ERR_MSG(extack, "invalid control action");
64 		goto end;
65 	}
66 
67 	if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
68 		chain_index = action & TC_ACT_EXT_VAL_MASK;
69 		if (!tp || !newchain) {
70 			ret = -EINVAL;
71 			NL_SET_ERR_MSG(extack,
72 				       "can't goto NULL proto/chain");
73 			goto end;
74 		}
75 		*newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
76 		if (!*newchain) {
77 			ret = -ENOMEM;
78 			NL_SET_ERR_MSG(extack,
79 				       "can't allocate goto_chain");
80 		}
81 	}
82 end:
83 	return ret;
84 }
85 EXPORT_SYMBOL(tcf_action_check_ctrlact);
86 
tcf_action_set_ctrlact(struct tc_action * a,int action,struct tcf_chain * goto_chain)87 struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
88 					 struct tcf_chain *goto_chain)
89 {
90 	a->tcfa_action = action;
91 	goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
92 	return goto_chain;
93 }
94 EXPORT_SYMBOL(tcf_action_set_ctrlact);
95 
96 /* XXX: For standalone actions, we don't need a RCU grace period either, because
97  * actions are always connected to filters and filters are already destroyed in
98  * RCU callbacks, so after a RCU grace period actions are already disconnected
99  * from filters. Readers later can not find us.
100  */
free_tcf(struct tc_action * p)101 static void free_tcf(struct tc_action *p)
102 {
103 	struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
104 
105 	free_percpu(p->cpu_bstats);
106 	free_percpu(p->cpu_bstats_hw);
107 	free_percpu(p->cpu_qstats);
108 
109 	tcf_set_action_cookie(&p->act_cookie, NULL);
110 	if (chain)
111 		tcf_chain_put_by_act(chain);
112 
113 	kfree(p);
114 }
115 
tcf_action_cleanup(struct tc_action * p)116 static void tcf_action_cleanup(struct tc_action *p)
117 {
118 	if (p->ops->cleanup)
119 		p->ops->cleanup(p);
120 
121 	gen_kill_estimator(&p->tcfa_rate_est);
122 	free_tcf(p);
123 }
124 
__tcf_action_put(struct tc_action * p,bool bind)125 static int __tcf_action_put(struct tc_action *p, bool bind)
126 {
127 	struct tcf_idrinfo *idrinfo = p->idrinfo;
128 
129 	if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
130 		if (bind)
131 			atomic_dec(&p->tcfa_bindcnt);
132 		idr_remove(&idrinfo->action_idr, p->tcfa_index);
133 		mutex_unlock(&idrinfo->lock);
134 
135 		tcf_action_cleanup(p);
136 		return 1;
137 	}
138 
139 	if (bind)
140 		atomic_dec(&p->tcfa_bindcnt);
141 
142 	return 0;
143 }
144 
__tcf_idr_release(struct tc_action * p,bool bind,bool strict)145 static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
146 {
147 	int ret = 0;
148 
149 	/* Release with strict==1 and bind==0 is only called through act API
150 	 * interface (classifiers always bind). Only case when action with
151 	 * positive reference count and zero bind count can exist is when it was
152 	 * also created with act API (unbinding last classifier will destroy the
153 	 * action if it was created by classifier). So only case when bind count
154 	 * can be changed after initial check is when unbound action is
155 	 * destroyed by act API while classifier binds to action with same id
156 	 * concurrently. This result either creation of new action(same behavior
157 	 * as before), or reusing existing action if concurrent process
158 	 * increments reference count before action is deleted. Both scenarios
159 	 * are acceptable.
160 	 */
161 	if (p) {
162 		if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
163 			return -EPERM;
164 
165 		if (__tcf_action_put(p, bind))
166 			ret = ACT_P_DELETED;
167 	}
168 
169 	return ret;
170 }
171 
tcf_idr_release(struct tc_action * a,bool bind)172 int tcf_idr_release(struct tc_action *a, bool bind)
173 {
174 	const struct tc_action_ops *ops = a->ops;
175 	int ret;
176 
177 	ret = __tcf_idr_release(a, bind, false);
178 	if (ret == ACT_P_DELETED)
179 		module_put(ops->owner);
180 	return ret;
181 }
182 EXPORT_SYMBOL(tcf_idr_release);
183 
tcf_action_shared_attrs_size(const struct tc_action * act)184 static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
185 {
186 	struct tc_cookie *act_cookie;
187 	u32 cookie_len = 0;
188 
189 	rcu_read_lock();
190 	act_cookie = rcu_dereference(act->act_cookie);
191 
192 	if (act_cookie)
193 		cookie_len = nla_total_size(act_cookie->len);
194 	rcu_read_unlock();
195 
196 	return  nla_total_size(0) /* action number nested */
197 		+ nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
198 		+ cookie_len /* TCA_ACT_COOKIE */
199 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
200 		+ nla_total_size(0) /* TCA_ACT_STATS nested */
201 		+ nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
202 		/* TCA_STATS_BASIC */
203 		+ nla_total_size_64bit(sizeof(struct gnet_stats_basic))
204 		/* TCA_STATS_PKT64 */
205 		+ nla_total_size_64bit(sizeof(u64))
206 		/* TCA_STATS_QUEUE */
207 		+ nla_total_size_64bit(sizeof(struct gnet_stats_queue))
208 		+ nla_total_size(0) /* TCA_OPTIONS nested */
209 		+ nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
210 }
211 
tcf_action_full_attrs_size(size_t sz)212 static size_t tcf_action_full_attrs_size(size_t sz)
213 {
214 	return NLMSG_HDRLEN                     /* struct nlmsghdr */
215 		+ sizeof(struct tcamsg)
216 		+ nla_total_size(0)             /* TCA_ACT_TAB nested */
217 		+ sz;
218 }
219 
tcf_action_fill_size(const struct tc_action * act)220 static size_t tcf_action_fill_size(const struct tc_action *act)
221 {
222 	size_t sz = tcf_action_shared_attrs_size(act);
223 
224 	if (act->ops->get_fill_size)
225 		return act->ops->get_fill_size(act) + sz;
226 	return sz;
227 }
228 
tcf_dump_walker(struct tcf_idrinfo * idrinfo,struct sk_buff * skb,struct netlink_callback * cb)229 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
230 			   struct netlink_callback *cb)
231 {
232 	int err = 0, index = -1, s_i = 0, n_i = 0;
233 	u32 act_flags = cb->args[2];
234 	unsigned long jiffy_since = cb->args[3];
235 	struct nlattr *nest;
236 	struct idr *idr = &idrinfo->action_idr;
237 	struct tc_action *p;
238 	unsigned long id = 1;
239 	unsigned long tmp;
240 
241 	mutex_lock(&idrinfo->lock);
242 
243 	s_i = cb->args[0];
244 
245 	idr_for_each_entry_ul(idr, p, tmp, id) {
246 		index++;
247 		if (index < s_i)
248 			continue;
249 		if (IS_ERR(p))
250 			continue;
251 
252 		if (jiffy_since &&
253 		    time_after(jiffy_since,
254 			       (unsigned long)p->tcfa_tm.lastuse))
255 			continue;
256 
257 		nest = nla_nest_start_noflag(skb, n_i);
258 		if (!nest) {
259 			index--;
260 			goto nla_put_failure;
261 		}
262 		err = tcf_action_dump_1(skb, p, 0, 0);
263 		if (err < 0) {
264 			index--;
265 			nlmsg_trim(skb, nest);
266 			goto done;
267 		}
268 		nla_nest_end(skb, nest);
269 		n_i++;
270 		if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
271 		    n_i >= TCA_ACT_MAX_PRIO)
272 			goto done;
273 	}
274 done:
275 	if (index >= 0)
276 		cb->args[0] = index + 1;
277 
278 	mutex_unlock(&idrinfo->lock);
279 	if (n_i) {
280 		if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
281 			cb->args[1] = n_i;
282 	}
283 	return n_i;
284 
285 nla_put_failure:
286 	nla_nest_cancel(skb, nest);
287 	goto done;
288 }
289 
tcf_idr_release_unsafe(struct tc_action * p)290 static int tcf_idr_release_unsafe(struct tc_action *p)
291 {
292 	if (atomic_read(&p->tcfa_bindcnt) > 0)
293 		return -EPERM;
294 
295 	if (refcount_dec_and_test(&p->tcfa_refcnt)) {
296 		idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
297 		tcf_action_cleanup(p);
298 		return ACT_P_DELETED;
299 	}
300 
301 	return 0;
302 }
303 
tcf_del_walker(struct tcf_idrinfo * idrinfo,struct sk_buff * skb,const struct tc_action_ops * ops)304 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
305 			  const struct tc_action_ops *ops)
306 {
307 	struct nlattr *nest;
308 	int n_i = 0;
309 	int ret = -EINVAL;
310 	struct idr *idr = &idrinfo->action_idr;
311 	struct tc_action *p;
312 	unsigned long id = 1;
313 	unsigned long tmp;
314 
315 	nest = nla_nest_start_noflag(skb, 0);
316 	if (nest == NULL)
317 		goto nla_put_failure;
318 	if (nla_put_string(skb, TCA_KIND, ops->kind))
319 		goto nla_put_failure;
320 
321 	mutex_lock(&idrinfo->lock);
322 	idr_for_each_entry_ul(idr, p, tmp, id) {
323 		if (IS_ERR(p))
324 			continue;
325 		ret = tcf_idr_release_unsafe(p);
326 		if (ret == ACT_P_DELETED) {
327 			module_put(ops->owner);
328 			n_i++;
329 		} else if (ret < 0) {
330 			mutex_unlock(&idrinfo->lock);
331 			goto nla_put_failure;
332 		}
333 	}
334 	mutex_unlock(&idrinfo->lock);
335 
336 	ret = nla_put_u32(skb, TCA_FCNT, n_i);
337 	if (ret)
338 		goto nla_put_failure;
339 	nla_nest_end(skb, nest);
340 
341 	return n_i;
342 nla_put_failure:
343 	nla_nest_cancel(skb, nest);
344 	return ret;
345 }
346 
tcf_generic_walker(struct tc_action_net * tn,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)347 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
348 		       struct netlink_callback *cb, int type,
349 		       const struct tc_action_ops *ops,
350 		       struct netlink_ext_ack *extack)
351 {
352 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
353 
354 	if (type == RTM_DELACTION) {
355 		return tcf_del_walker(idrinfo, skb, ops);
356 	} else if (type == RTM_GETACTION) {
357 		return tcf_dump_walker(idrinfo, skb, cb);
358 	} else {
359 		WARN(1, "tcf_generic_walker: unknown command %d\n", type);
360 		NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
361 		return -EINVAL;
362 	}
363 }
364 EXPORT_SYMBOL(tcf_generic_walker);
365 
tcf_idr_search(struct tc_action_net * tn,struct tc_action ** a,u32 index)366 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
367 {
368 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
369 	struct tc_action *p;
370 
371 	mutex_lock(&idrinfo->lock);
372 	p = idr_find(&idrinfo->action_idr, index);
373 	if (IS_ERR(p))
374 		p = NULL;
375 	else if (p)
376 		refcount_inc(&p->tcfa_refcnt);
377 	mutex_unlock(&idrinfo->lock);
378 
379 	if (p) {
380 		*a = p;
381 		return true;
382 	}
383 	return false;
384 }
385 EXPORT_SYMBOL(tcf_idr_search);
386 
tcf_idr_delete_index(struct tcf_idrinfo * idrinfo,u32 index)387 static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
388 {
389 	struct tc_action *p;
390 	int ret = 0;
391 
392 	mutex_lock(&idrinfo->lock);
393 	p = idr_find(&idrinfo->action_idr, index);
394 	if (!p) {
395 		mutex_unlock(&idrinfo->lock);
396 		return -ENOENT;
397 	}
398 
399 	if (!atomic_read(&p->tcfa_bindcnt)) {
400 		if (refcount_dec_and_test(&p->tcfa_refcnt)) {
401 			struct module *owner = p->ops->owner;
402 
403 			WARN_ON(p != idr_remove(&idrinfo->action_idr,
404 						p->tcfa_index));
405 			mutex_unlock(&idrinfo->lock);
406 
407 			tcf_action_cleanup(p);
408 			module_put(owner);
409 			return 0;
410 		}
411 		ret = 0;
412 	} else {
413 		ret = -EPERM;
414 	}
415 
416 	mutex_unlock(&idrinfo->lock);
417 	return ret;
418 }
419 
tcf_idr_create(struct tc_action_net * tn,u32 index,struct nlattr * est,struct tc_action ** a,const struct tc_action_ops * ops,int bind,bool cpustats,u32 flags)420 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
421 		   struct tc_action **a, const struct tc_action_ops *ops,
422 		   int bind, bool cpustats, u32 flags)
423 {
424 	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
425 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
426 	int err = -ENOMEM;
427 
428 	if (unlikely(!p))
429 		return -ENOMEM;
430 	refcount_set(&p->tcfa_refcnt, 1);
431 	if (bind)
432 		atomic_set(&p->tcfa_bindcnt, 1);
433 
434 	if (cpustats) {
435 		p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
436 		if (!p->cpu_bstats)
437 			goto err1;
438 		p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
439 		if (!p->cpu_bstats_hw)
440 			goto err2;
441 		p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
442 		if (!p->cpu_qstats)
443 			goto err3;
444 	}
445 	spin_lock_init(&p->tcfa_lock);
446 	p->tcfa_index = index;
447 	p->tcfa_tm.install = jiffies;
448 	p->tcfa_tm.lastuse = jiffies;
449 	p->tcfa_tm.firstuse = 0;
450 	p->tcfa_flags = flags;
451 	if (est) {
452 		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
453 					&p->tcfa_rate_est,
454 					&p->tcfa_lock, NULL, est);
455 		if (err)
456 			goto err4;
457 	}
458 
459 	p->idrinfo = idrinfo;
460 	__module_get(ops->owner);
461 	p->ops = ops;
462 	*a = p;
463 	return 0;
464 err4:
465 	free_percpu(p->cpu_qstats);
466 err3:
467 	free_percpu(p->cpu_bstats_hw);
468 err2:
469 	free_percpu(p->cpu_bstats);
470 err1:
471 	kfree(p);
472 	return err;
473 }
474 EXPORT_SYMBOL(tcf_idr_create);
475 
tcf_idr_create_from_flags(struct tc_action_net * tn,u32 index,struct nlattr * est,struct tc_action ** a,const struct tc_action_ops * ops,int bind,u32 flags)476 int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
477 			      struct nlattr *est, struct tc_action **a,
478 			      const struct tc_action_ops *ops, int bind,
479 			      u32 flags)
480 {
481 	/* Set cpustats according to actions flags. */
482 	return tcf_idr_create(tn, index, est, a, ops, bind,
483 			      !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
484 }
485 EXPORT_SYMBOL(tcf_idr_create_from_flags);
486 
487 /* Cleanup idr index that was allocated but not initialized. */
488 
tcf_idr_cleanup(struct tc_action_net * tn,u32 index)489 void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
490 {
491 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
492 
493 	mutex_lock(&idrinfo->lock);
494 	/* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
495 	WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
496 	mutex_unlock(&idrinfo->lock);
497 }
498 EXPORT_SYMBOL(tcf_idr_cleanup);
499 
500 /* Check if action with specified index exists. If actions is found, increments
501  * its reference and bind counters, and return 1. Otherwise insert temporary
502  * error pointer (to prevent concurrent users from inserting actions with same
503  * index) and return 0.
504  */
505 
tcf_idr_check_alloc(struct tc_action_net * tn,u32 * index,struct tc_action ** a,int bind)506 int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
507 			struct tc_action **a, int bind)
508 {
509 	struct tcf_idrinfo *idrinfo = tn->idrinfo;
510 	struct tc_action *p;
511 	int ret;
512 
513 again:
514 	mutex_lock(&idrinfo->lock);
515 	if (*index) {
516 		p = idr_find(&idrinfo->action_idr, *index);
517 		if (IS_ERR(p)) {
518 			/* This means that another process allocated
519 			 * index but did not assign the pointer yet.
520 			 */
521 			mutex_unlock(&idrinfo->lock);
522 			goto again;
523 		}
524 
525 		if (p) {
526 			refcount_inc(&p->tcfa_refcnt);
527 			if (bind)
528 				atomic_inc(&p->tcfa_bindcnt);
529 			*a = p;
530 			ret = 1;
531 		} else {
532 			*a = NULL;
533 			ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
534 					    *index, GFP_KERNEL);
535 			if (!ret)
536 				idr_replace(&idrinfo->action_idr,
537 					    ERR_PTR(-EBUSY), *index);
538 		}
539 	} else {
540 		*index = 1;
541 		*a = NULL;
542 		ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
543 				    UINT_MAX, GFP_KERNEL);
544 		if (!ret)
545 			idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
546 				    *index);
547 	}
548 	mutex_unlock(&idrinfo->lock);
549 	return ret;
550 }
551 EXPORT_SYMBOL(tcf_idr_check_alloc);
552 
tcf_idrinfo_destroy(const struct tc_action_ops * ops,struct tcf_idrinfo * idrinfo)553 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
554 			 struct tcf_idrinfo *idrinfo)
555 {
556 	struct idr *idr = &idrinfo->action_idr;
557 	struct tc_action *p;
558 	int ret;
559 	unsigned long id = 1;
560 	unsigned long tmp;
561 
562 	idr_for_each_entry_ul(idr, p, tmp, id) {
563 		ret = __tcf_idr_release(p, false, true);
564 		if (ret == ACT_P_DELETED)
565 			module_put(ops->owner);
566 		else if (ret < 0)
567 			return;
568 	}
569 	idr_destroy(&idrinfo->action_idr);
570 }
571 EXPORT_SYMBOL(tcf_idrinfo_destroy);
572 
573 static LIST_HEAD(act_base);
574 static DEFINE_RWLOCK(act_mod_lock);
575 
tcf_register_action(struct tc_action_ops * act,struct pernet_operations * ops)576 int tcf_register_action(struct tc_action_ops *act,
577 			struct pernet_operations *ops)
578 {
579 	struct tc_action_ops *a;
580 	int ret;
581 
582 	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
583 		return -EINVAL;
584 
585 	/* We have to register pernet ops before making the action ops visible,
586 	 * otherwise tcf_action_init_1() could get a partially initialized
587 	 * netns.
588 	 */
589 	ret = register_pernet_subsys(ops);
590 	if (ret)
591 		return ret;
592 
593 	write_lock(&act_mod_lock);
594 	list_for_each_entry(a, &act_base, head) {
595 		if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
596 			write_unlock(&act_mod_lock);
597 			unregister_pernet_subsys(ops);
598 			return -EEXIST;
599 		}
600 	}
601 	list_add_tail(&act->head, &act_base);
602 	write_unlock(&act_mod_lock);
603 
604 	return 0;
605 }
606 EXPORT_SYMBOL(tcf_register_action);
607 
tcf_unregister_action(struct tc_action_ops * act,struct pernet_operations * ops)608 int tcf_unregister_action(struct tc_action_ops *act,
609 			  struct pernet_operations *ops)
610 {
611 	struct tc_action_ops *a;
612 	int err = -ENOENT;
613 
614 	write_lock(&act_mod_lock);
615 	list_for_each_entry(a, &act_base, head) {
616 		if (a == act) {
617 			list_del(&act->head);
618 			err = 0;
619 			break;
620 		}
621 	}
622 	write_unlock(&act_mod_lock);
623 	if (!err)
624 		unregister_pernet_subsys(ops);
625 	return err;
626 }
627 EXPORT_SYMBOL(tcf_unregister_action);
628 
629 /* lookup by name */
tc_lookup_action_n(char * kind)630 static struct tc_action_ops *tc_lookup_action_n(char *kind)
631 {
632 	struct tc_action_ops *a, *res = NULL;
633 
634 	if (kind) {
635 		read_lock(&act_mod_lock);
636 		list_for_each_entry(a, &act_base, head) {
637 			if (strcmp(kind, a->kind) == 0) {
638 				if (try_module_get(a->owner))
639 					res = a;
640 				break;
641 			}
642 		}
643 		read_unlock(&act_mod_lock);
644 	}
645 	return res;
646 }
647 
648 /* lookup by nlattr */
tc_lookup_action(struct nlattr * kind)649 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
650 {
651 	struct tc_action_ops *a, *res = NULL;
652 
653 	if (kind) {
654 		read_lock(&act_mod_lock);
655 		list_for_each_entry(a, &act_base, head) {
656 			if (nla_strcmp(kind, a->kind) == 0) {
657 				if (try_module_get(a->owner))
658 					res = a;
659 				break;
660 			}
661 		}
662 		read_unlock(&act_mod_lock);
663 	}
664 	return res;
665 }
666 
667 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
668 #define TCA_ACT_MAX_PRIO_MASK 0x1FF
tcf_action_exec(struct sk_buff * skb,struct tc_action ** actions,int nr_actions,struct tcf_result * res)669 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
670 		    int nr_actions, struct tcf_result *res)
671 {
672 	u32 jmp_prgcnt = 0;
673 	u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
674 	int i;
675 	int ret = TC_ACT_OK;
676 
677 	if (skb_skip_tc_classify(skb))
678 		return TC_ACT_OK;
679 
680 restart_act_graph:
681 	for (i = 0; i < nr_actions; i++) {
682 		const struct tc_action *a = actions[i];
683 
684 		if (jmp_prgcnt > 0) {
685 			jmp_prgcnt -= 1;
686 			continue;
687 		}
688 repeat:
689 		ret = a->ops->act(skb, a, res);
690 		if (ret == TC_ACT_REPEAT)
691 			goto repeat;	/* we need a ttl - JHS */
692 
693 		if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
694 			jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
695 			if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
696 				/* faulty opcode, stop pipeline */
697 				return TC_ACT_OK;
698 			} else {
699 				jmp_ttl -= 1;
700 				if (jmp_ttl > 0)
701 					goto restart_act_graph;
702 				else /* faulty graph, stop pipeline */
703 					return TC_ACT_OK;
704 			}
705 		} else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
706 			if (unlikely(!rcu_access_pointer(a->goto_chain))) {
707 				net_warn_ratelimited("can't go to NULL chain!\n");
708 				return TC_ACT_SHOT;
709 			}
710 			tcf_action_goto_chain_exec(a, res);
711 		}
712 
713 		if (ret != TC_ACT_PIPE)
714 			break;
715 	}
716 
717 	return ret;
718 }
719 EXPORT_SYMBOL(tcf_action_exec);
720 
tcf_action_destroy(struct tc_action * actions[],int bind)721 int tcf_action_destroy(struct tc_action *actions[], int bind)
722 {
723 	const struct tc_action_ops *ops;
724 	struct tc_action *a;
725 	int ret = 0, i;
726 
727 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
728 		a = actions[i];
729 		actions[i] = NULL;
730 		ops = a->ops;
731 		ret = __tcf_idr_release(a, bind, true);
732 		if (ret == ACT_P_DELETED)
733 			module_put(ops->owner);
734 		else if (ret < 0)
735 			return ret;
736 	}
737 	return ret;
738 }
739 
tcf_action_put(struct tc_action * p)740 static int tcf_action_put(struct tc_action *p)
741 {
742 	return __tcf_action_put(p, false);
743 }
744 
745 /* Put all actions in this array, skip those NULL's. */
tcf_action_put_many(struct tc_action * actions[])746 static void tcf_action_put_many(struct tc_action *actions[])
747 {
748 	int i;
749 
750 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
751 		struct tc_action *a = actions[i];
752 		const struct tc_action_ops *ops;
753 
754 		if (!a)
755 			continue;
756 		ops = a->ops;
757 		if (tcf_action_put(a))
758 			module_put(ops->owner);
759 	}
760 }
761 
762 int
tcf_action_dump_old(struct sk_buff * skb,struct tc_action * a,int bind,int ref)763 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
764 {
765 	return a->ops->dump(skb, a, bind, ref);
766 }
767 
768 static int
tcf_action_dump_terse(struct sk_buff * skb,struct tc_action * a)769 tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a)
770 {
771 	unsigned char *b = skb_tail_pointer(skb);
772 	struct tc_cookie *cookie;
773 
774 	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
775 		goto nla_put_failure;
776 	if (tcf_action_copy_stats(skb, a, 0))
777 		goto nla_put_failure;
778 
779 	rcu_read_lock();
780 	cookie = rcu_dereference(a->act_cookie);
781 	if (cookie) {
782 		if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
783 			rcu_read_unlock();
784 			goto nla_put_failure;
785 		}
786 	}
787 	rcu_read_unlock();
788 
789 	return 0;
790 
791 nla_put_failure:
792 	nlmsg_trim(skb, b);
793 	return -1;
794 }
795 
796 int
tcf_action_dump_1(struct sk_buff * skb,struct tc_action * a,int bind,int ref)797 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
798 {
799 	int err = -EINVAL;
800 	unsigned char *b = skb_tail_pointer(skb);
801 	struct nlattr *nest;
802 
803 	if (tcf_action_dump_terse(skb, a))
804 		goto nla_put_failure;
805 
806 	if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
807 	    nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
808 			       a->hw_stats, TCA_ACT_HW_STATS_ANY))
809 		goto nla_put_failure;
810 
811 	if (a->used_hw_stats_valid &&
812 	    nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
813 			       a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
814 		goto nla_put_failure;
815 
816 	if (a->tcfa_flags &&
817 	    nla_put_bitfield32(skb, TCA_ACT_FLAGS,
818 			       a->tcfa_flags, a->tcfa_flags))
819 		goto nla_put_failure;
820 
821 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
822 	if (nest == NULL)
823 		goto nla_put_failure;
824 	err = tcf_action_dump_old(skb, a, bind, ref);
825 	if (err > 0) {
826 		nla_nest_end(skb, nest);
827 		return err;
828 	}
829 
830 nla_put_failure:
831 	nlmsg_trim(skb, b);
832 	return -1;
833 }
834 EXPORT_SYMBOL(tcf_action_dump_1);
835 
tcf_action_dump(struct sk_buff * skb,struct tc_action * actions[],int bind,int ref,bool terse)836 int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
837 		    int bind, int ref, bool terse)
838 {
839 	struct tc_action *a;
840 	int err = -EINVAL, i;
841 	struct nlattr *nest;
842 
843 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
844 		a = actions[i];
845 		nest = nla_nest_start_noflag(skb, i + 1);
846 		if (nest == NULL)
847 			goto nla_put_failure;
848 		err = terse ? tcf_action_dump_terse(skb, a) :
849 			tcf_action_dump_1(skb, a, bind, ref);
850 		if (err < 0)
851 			goto errout;
852 		nla_nest_end(skb, nest);
853 	}
854 
855 	return 0;
856 
857 nla_put_failure:
858 	err = -EINVAL;
859 errout:
860 	nla_nest_cancel(skb, nest);
861 	return err;
862 }
863 
nla_memdup_cookie(struct nlattr ** tb)864 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
865 {
866 	struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
867 	if (!c)
868 		return NULL;
869 
870 	c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
871 	if (!c->data) {
872 		kfree(c);
873 		return NULL;
874 	}
875 	c->len = nla_len(tb[TCA_ACT_COOKIE]);
876 
877 	return c;
878 }
879 
tcf_action_hw_stats_get(struct nlattr * hw_stats_attr)880 static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
881 {
882 	struct nla_bitfield32 hw_stats_bf;
883 
884 	/* If the user did not pass the attr, that means he does
885 	 * not care about the type. Return "any" in that case
886 	 * which is setting on all supported types.
887 	 */
888 	if (!hw_stats_attr)
889 		return TCA_ACT_HW_STATS_ANY;
890 	hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
891 	return hw_stats_bf.value;
892 }
893 
894 static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
895 	[TCA_ACT_KIND]		= { .type = NLA_STRING },
896 	[TCA_ACT_INDEX]		= { .type = NLA_U32 },
897 	[TCA_ACT_COOKIE]	= { .type = NLA_BINARY,
898 				    .len = TC_COOKIE_MAX_SIZE },
899 	[TCA_ACT_OPTIONS]	= { .type = NLA_NESTED },
900 	[TCA_ACT_FLAGS]		= NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS),
901 	[TCA_ACT_HW_STATS]	= NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
902 };
903 
tcf_idr_insert_many(struct tc_action * actions[])904 void tcf_idr_insert_many(struct tc_action *actions[])
905 {
906 	int i;
907 
908 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
909 		struct tc_action *a = actions[i];
910 		struct tcf_idrinfo *idrinfo;
911 
912 		if (!a)
913 			continue;
914 		idrinfo = a->idrinfo;
915 		mutex_lock(&idrinfo->lock);
916 		/* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc if
917 		 * it is just created, otherwise this is just a nop.
918 		 */
919 		idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
920 		mutex_unlock(&idrinfo->lock);
921 	}
922 }
923 
tc_action_load_ops(char * name,struct nlattr * nla,bool rtnl_held,struct netlink_ext_ack * extack)924 struct tc_action_ops *tc_action_load_ops(char *name, struct nlattr *nla,
925 					 bool rtnl_held,
926 					 struct netlink_ext_ack *extack)
927 {
928 	struct nlattr *tb[TCA_ACT_MAX + 1];
929 	struct tc_action_ops *a_o;
930 	char act_name[IFNAMSIZ];
931 	struct nlattr *kind;
932 	int err;
933 
934 	if (name == NULL) {
935 		err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
936 						  tcf_action_policy, extack);
937 		if (err < 0)
938 			return ERR_PTR(err);
939 		err = -EINVAL;
940 		kind = tb[TCA_ACT_KIND];
941 		if (!kind) {
942 			NL_SET_ERR_MSG(extack, "TC action kind must be specified");
943 			return ERR_PTR(err);
944 		}
945 		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
946 			NL_SET_ERR_MSG(extack, "TC action name too long");
947 			return ERR_PTR(err);
948 		}
949 	} else {
950 		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
951 			NL_SET_ERR_MSG(extack, "TC action name too long");
952 			return ERR_PTR(-EINVAL);
953 		}
954 	}
955 
956 	a_o = tc_lookup_action_n(act_name);
957 	if (a_o == NULL) {
958 #ifdef CONFIG_MODULES
959 		if (rtnl_held)
960 			rtnl_unlock();
961 		request_module("act_%s", act_name);
962 		if (rtnl_held)
963 			rtnl_lock();
964 
965 		a_o = tc_lookup_action_n(act_name);
966 
967 		/* We dropped the RTNL semaphore in order to
968 		 * perform the module load.  So, even if we
969 		 * succeeded in loading the module we have to
970 		 * tell the caller to replay the request.  We
971 		 * indicate this using -EAGAIN.
972 		 */
973 		if (a_o != NULL) {
974 			module_put(a_o->owner);
975 			return ERR_PTR(-EAGAIN);
976 		}
977 #endif
978 		NL_SET_ERR_MSG(extack, "Failed to load TC action module");
979 		return ERR_PTR(-ENOENT);
980 	}
981 
982 	return a_o;
983 }
984 
tcf_action_init_1(struct net * net,struct tcf_proto * tp,struct nlattr * nla,struct nlattr * est,char * name,int ovr,int bind,struct tc_action_ops * a_o,int * init_res,bool rtnl_held,struct netlink_ext_ack * extack)985 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
986 				    struct nlattr *nla, struct nlattr *est,
987 				    char *name, int ovr, int bind,
988 				    struct tc_action_ops *a_o, int *init_res,
989 				    bool rtnl_held,
990 				    struct netlink_ext_ack *extack)
991 {
992 	struct nla_bitfield32 flags = { 0, 0 };
993 	u8 hw_stats = TCA_ACT_HW_STATS_ANY;
994 	struct nlattr *tb[TCA_ACT_MAX + 1];
995 	struct tc_cookie *cookie = NULL;
996 	struct tc_action *a;
997 	int err;
998 
999 	/* backward compatibility for policer */
1000 	if (name == NULL) {
1001 		err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1002 						  tcf_action_policy, extack);
1003 		if (err < 0)
1004 			return ERR_PTR(err);
1005 		if (tb[TCA_ACT_COOKIE]) {
1006 			cookie = nla_memdup_cookie(tb);
1007 			if (!cookie) {
1008 				NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
1009 				err = -ENOMEM;
1010 				goto err_out;
1011 			}
1012 		}
1013 		hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
1014 		if (tb[TCA_ACT_FLAGS])
1015 			flags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
1016 
1017 		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
1018 				rtnl_held, tp, flags.value, extack);
1019 	} else {
1020 		err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
1021 				tp, flags.value, extack);
1022 	}
1023 	if (err < 0)
1024 		goto err_out;
1025 	*init_res = err;
1026 
1027 	if (!name && tb[TCA_ACT_COOKIE])
1028 		tcf_set_action_cookie(&a->act_cookie, cookie);
1029 
1030 	if (!name)
1031 		a->hw_stats = hw_stats;
1032 
1033 	return a;
1034 
1035 err_out:
1036 	if (cookie) {
1037 		kfree(cookie->data);
1038 		kfree(cookie);
1039 	}
1040 	return ERR_PTR(err);
1041 }
1042 
1043 /* Returns numbers of initialized actions or negative error. */
1044 
tcf_action_init(struct net * net,struct tcf_proto * tp,struct nlattr * nla,struct nlattr * est,char * name,int ovr,int bind,struct tc_action * actions[],int init_res[],size_t * attr_size,bool rtnl_held,struct netlink_ext_ack * extack)1045 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
1046 		    struct nlattr *est, char *name, int ovr, int bind,
1047 		    struct tc_action *actions[], int init_res[], size_t *attr_size,
1048 		    bool rtnl_held, struct netlink_ext_ack *extack)
1049 {
1050 	struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
1051 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1052 	struct tc_action *act;
1053 	size_t sz = 0;
1054 	int err;
1055 	int i;
1056 
1057 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1058 					  extack);
1059 	if (err < 0)
1060 		return err;
1061 
1062 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1063 		struct tc_action_ops *a_o;
1064 
1065 		a_o = tc_action_load_ops(name, tb[i], rtnl_held, extack);
1066 		if (IS_ERR(a_o)) {
1067 			err = PTR_ERR(a_o);
1068 			goto err_mod;
1069 		}
1070 		ops[i - 1] = a_o;
1071 	}
1072 
1073 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1074 		act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
1075 					ops[i - 1], &init_res[i - 1], rtnl_held,
1076 					extack);
1077 		if (IS_ERR(act)) {
1078 			err = PTR_ERR(act);
1079 			goto err;
1080 		}
1081 		sz += tcf_action_fill_size(act);
1082 		/* Start from index 0 */
1083 		actions[i - 1] = act;
1084 	}
1085 
1086 	/* We have to commit them all together, because if any error happened in
1087 	 * between, we could not handle the failure gracefully.
1088 	 */
1089 	tcf_idr_insert_many(actions);
1090 
1091 	*attr_size = tcf_action_full_attrs_size(sz);
1092 	err = i - 1;
1093 	goto err_mod;
1094 
1095 err:
1096 	tcf_action_destroy(actions, bind);
1097 err_mod:
1098 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
1099 		if (ops[i])
1100 			module_put(ops[i]->owner);
1101 	}
1102 	return err;
1103 }
1104 
tcf_action_update_stats(struct tc_action * a,u64 bytes,u64 packets,u64 drops,bool hw)1105 void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
1106 			     u64 drops, bool hw)
1107 {
1108 	if (a->cpu_bstats) {
1109 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
1110 
1111 		this_cpu_ptr(a->cpu_qstats)->drops += drops;
1112 
1113 		if (hw)
1114 			_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
1115 					   bytes, packets);
1116 		return;
1117 	}
1118 
1119 	_bstats_update(&a->tcfa_bstats, bytes, packets);
1120 	a->tcfa_qstats.drops += drops;
1121 	if (hw)
1122 		_bstats_update(&a->tcfa_bstats_hw, bytes, packets);
1123 }
1124 EXPORT_SYMBOL(tcf_action_update_stats);
1125 
tcf_action_copy_stats(struct sk_buff * skb,struct tc_action * p,int compat_mode)1126 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
1127 			  int compat_mode)
1128 {
1129 	int err = 0;
1130 	struct gnet_dump d;
1131 
1132 	if (p == NULL)
1133 		goto errout;
1134 
1135 	/* compat_mode being true specifies a call that is supposed
1136 	 * to add additional backward compatibility statistic TLVs.
1137 	 */
1138 	if (compat_mode) {
1139 		if (p->type == TCA_OLD_COMPAT)
1140 			err = gnet_stats_start_copy_compat(skb, 0,
1141 							   TCA_STATS,
1142 							   TCA_XSTATS,
1143 							   &p->tcfa_lock, &d,
1144 							   TCA_PAD);
1145 		else
1146 			return 0;
1147 	} else
1148 		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
1149 					    &p->tcfa_lock, &d, TCA_ACT_PAD);
1150 
1151 	if (err < 0)
1152 		goto errout;
1153 
1154 	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
1155 	    gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw,
1156 				     &p->tcfa_bstats_hw) < 0 ||
1157 	    gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
1158 	    gnet_stats_copy_queue(&d, p->cpu_qstats,
1159 				  &p->tcfa_qstats,
1160 				  p->tcfa_qstats.qlen) < 0)
1161 		goto errout;
1162 
1163 	if (gnet_stats_finish_copy(&d) < 0)
1164 		goto errout;
1165 
1166 	return 0;
1167 
1168 errout:
1169 	return -1;
1170 }
1171 
tca_get_fill(struct sk_buff * skb,struct tc_action * actions[],u32 portid,u32 seq,u16 flags,int event,int bind,int ref)1172 static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
1173 			u32 portid, u32 seq, u16 flags, int event, int bind,
1174 			int ref)
1175 {
1176 	struct tcamsg *t;
1177 	struct nlmsghdr *nlh;
1178 	unsigned char *b = skb_tail_pointer(skb);
1179 	struct nlattr *nest;
1180 
1181 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
1182 	if (!nlh)
1183 		goto out_nlmsg_trim;
1184 	t = nlmsg_data(nlh);
1185 	t->tca_family = AF_UNSPEC;
1186 	t->tca__pad1 = 0;
1187 	t->tca__pad2 = 0;
1188 
1189 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1190 	if (!nest)
1191 		goto out_nlmsg_trim;
1192 
1193 	if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
1194 		goto out_nlmsg_trim;
1195 
1196 	nla_nest_end(skb, nest);
1197 
1198 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1199 	return skb->len;
1200 
1201 out_nlmsg_trim:
1202 	nlmsg_trim(skb, b);
1203 	return -1;
1204 }
1205 
1206 static int
tcf_get_notify(struct net * net,u32 portid,struct nlmsghdr * n,struct tc_action * actions[],int event,struct netlink_ext_ack * extack)1207 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
1208 	       struct tc_action *actions[], int event,
1209 	       struct netlink_ext_ack *extack)
1210 {
1211 	struct sk_buff *skb;
1212 
1213 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1214 	if (!skb)
1215 		return -ENOBUFS;
1216 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
1217 			 0, 1) <= 0) {
1218 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1219 		kfree_skb(skb);
1220 		return -EINVAL;
1221 	}
1222 
1223 	return rtnl_unicast(skb, net, portid);
1224 }
1225 
tcf_action_get_1(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,struct netlink_ext_ack * extack)1226 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
1227 					  struct nlmsghdr *n, u32 portid,
1228 					  struct netlink_ext_ack *extack)
1229 {
1230 	struct nlattr *tb[TCA_ACT_MAX + 1];
1231 	const struct tc_action_ops *ops;
1232 	struct tc_action *a;
1233 	int index;
1234 	int err;
1235 
1236 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1237 					  tcf_action_policy, extack);
1238 	if (err < 0)
1239 		goto err_out;
1240 
1241 	err = -EINVAL;
1242 	if (tb[TCA_ACT_INDEX] == NULL ||
1243 	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
1244 		NL_SET_ERR_MSG(extack, "Invalid TC action index value");
1245 		goto err_out;
1246 	}
1247 	index = nla_get_u32(tb[TCA_ACT_INDEX]);
1248 
1249 	err = -EINVAL;
1250 	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
1251 	if (!ops) { /* could happen in batch of actions */
1252 		NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
1253 		goto err_out;
1254 	}
1255 	err = -ENOENT;
1256 	if (ops->lookup(net, &a, index) == 0) {
1257 		NL_SET_ERR_MSG(extack, "TC action with specified index not found");
1258 		goto err_mod;
1259 	}
1260 
1261 	module_put(ops->owner);
1262 	return a;
1263 
1264 err_mod:
1265 	module_put(ops->owner);
1266 err_out:
1267 	return ERR_PTR(err);
1268 }
1269 
tca_action_flush(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,struct netlink_ext_ack * extack)1270 static int tca_action_flush(struct net *net, struct nlattr *nla,
1271 			    struct nlmsghdr *n, u32 portid,
1272 			    struct netlink_ext_ack *extack)
1273 {
1274 	struct sk_buff *skb;
1275 	unsigned char *b;
1276 	struct nlmsghdr *nlh;
1277 	struct tcamsg *t;
1278 	struct netlink_callback dcb;
1279 	struct nlattr *nest;
1280 	struct nlattr *tb[TCA_ACT_MAX + 1];
1281 	const struct tc_action_ops *ops;
1282 	struct nlattr *kind;
1283 	int err = -ENOMEM;
1284 
1285 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1286 	if (!skb)
1287 		return err;
1288 
1289 	b = skb_tail_pointer(skb);
1290 
1291 	err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
1292 					  tcf_action_policy, extack);
1293 	if (err < 0)
1294 		goto err_out;
1295 
1296 	err = -EINVAL;
1297 	kind = tb[TCA_ACT_KIND];
1298 	ops = tc_lookup_action(kind);
1299 	if (!ops) { /*some idjot trying to flush unknown action */
1300 		NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
1301 		goto err_out;
1302 	}
1303 
1304 	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
1305 			sizeof(*t), 0);
1306 	if (!nlh) {
1307 		NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
1308 		goto out_module_put;
1309 	}
1310 	t = nlmsg_data(nlh);
1311 	t->tca_family = AF_UNSPEC;
1312 	t->tca__pad1 = 0;
1313 	t->tca__pad2 = 0;
1314 
1315 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1316 	if (!nest) {
1317 		NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
1318 		goto out_module_put;
1319 	}
1320 
1321 	err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
1322 	if (err <= 0) {
1323 		nla_nest_cancel(skb, nest);
1324 		goto out_module_put;
1325 	}
1326 
1327 	nla_nest_end(skb, nest);
1328 
1329 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1330 	nlh->nlmsg_flags |= NLM_F_ROOT;
1331 	module_put(ops->owner);
1332 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1333 			     n->nlmsg_flags & NLM_F_ECHO);
1334 	if (err > 0)
1335 		return 0;
1336 	if (err < 0)
1337 		NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
1338 
1339 	return err;
1340 
1341 out_module_put:
1342 	module_put(ops->owner);
1343 err_out:
1344 	kfree_skb(skb);
1345 	return err;
1346 }
1347 
tcf_action_delete(struct net * net,struct tc_action * actions[])1348 static int tcf_action_delete(struct net *net, struct tc_action *actions[])
1349 {
1350 	int i;
1351 
1352 	for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
1353 		struct tc_action *a = actions[i];
1354 		const struct tc_action_ops *ops = a->ops;
1355 		/* Actions can be deleted concurrently so we must save their
1356 		 * type and id to search again after reference is released.
1357 		 */
1358 		struct tcf_idrinfo *idrinfo = a->idrinfo;
1359 		u32 act_index = a->tcfa_index;
1360 
1361 		actions[i] = NULL;
1362 		if (tcf_action_put(a)) {
1363 			/* last reference, action was deleted concurrently */
1364 			module_put(ops->owner);
1365 		} else  {
1366 			int ret;
1367 
1368 			/* now do the delete */
1369 			ret = tcf_idr_delete_index(idrinfo, act_index);
1370 			if (ret < 0)
1371 				return ret;
1372 		}
1373 	}
1374 	return 0;
1375 }
1376 
1377 static int
tcf_del_notify(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)1378 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1379 	       u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1380 {
1381 	int ret;
1382 	struct sk_buff *skb;
1383 
1384 	skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1385 			GFP_KERNEL);
1386 	if (!skb)
1387 		return -ENOBUFS;
1388 
1389 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
1390 			 0, 2) <= 0) {
1391 		NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
1392 		kfree_skb(skb);
1393 		return -EINVAL;
1394 	}
1395 
1396 	/* now do the delete */
1397 	ret = tcf_action_delete(net, actions);
1398 	if (ret < 0) {
1399 		NL_SET_ERR_MSG(extack, "Failed to delete TC action");
1400 		kfree_skb(skb);
1401 		return ret;
1402 	}
1403 
1404 	ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1405 			     n->nlmsg_flags & NLM_F_ECHO);
1406 	if (ret > 0)
1407 		return 0;
1408 	return ret;
1409 }
1410 
1411 static int
tca_action_gd(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,int event,struct netlink_ext_ack * extack)1412 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
1413 	      u32 portid, int event, struct netlink_ext_ack *extack)
1414 {
1415 	int i, ret;
1416 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1417 	struct tc_action *act;
1418 	size_t attr_size = 0;
1419 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1420 
1421 	ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
1422 					  extack);
1423 	if (ret < 0)
1424 		return ret;
1425 
1426 	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
1427 		if (tb[1])
1428 			return tca_action_flush(net, tb[1], n, portid, extack);
1429 
1430 		NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
1431 		return -EINVAL;
1432 	}
1433 
1434 	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
1435 		act = tcf_action_get_1(net, tb[i], n, portid, extack);
1436 		if (IS_ERR(act)) {
1437 			ret = PTR_ERR(act);
1438 			goto err;
1439 		}
1440 		attr_size += tcf_action_fill_size(act);
1441 		actions[i - 1] = act;
1442 	}
1443 
1444 	attr_size = tcf_action_full_attrs_size(attr_size);
1445 
1446 	if (event == RTM_GETACTION)
1447 		ret = tcf_get_notify(net, portid, n, actions, event, extack);
1448 	else { /* delete */
1449 		ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
1450 		if (ret)
1451 			goto err;
1452 		return 0;
1453 	}
1454 err:
1455 	tcf_action_put_many(actions);
1456 	return ret;
1457 }
1458 
1459 static int
tcf_add_notify(struct net * net,struct nlmsghdr * n,struct tc_action * actions[],u32 portid,size_t attr_size,struct netlink_ext_ack * extack)1460 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
1461 	       u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
1462 {
1463 	struct sk_buff *skb;
1464 	int err = 0;
1465 
1466 	skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
1467 			GFP_KERNEL);
1468 	if (!skb)
1469 		return -ENOBUFS;
1470 
1471 	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
1472 			 RTM_NEWACTION, 0, 0) <= 0) {
1473 		NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
1474 		kfree_skb(skb);
1475 		return -EINVAL;
1476 	}
1477 
1478 	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1479 			     n->nlmsg_flags & NLM_F_ECHO);
1480 	if (err > 0)
1481 		err = 0;
1482 	return err;
1483 }
1484 
tcf_action_add(struct net * net,struct nlattr * nla,struct nlmsghdr * n,u32 portid,int ovr,struct netlink_ext_ack * extack)1485 static int tcf_action_add(struct net *net, struct nlattr *nla,
1486 			  struct nlmsghdr *n, u32 portid, int ovr,
1487 			  struct netlink_ext_ack *extack)
1488 {
1489 	size_t attr_size = 0;
1490 	int loop, ret, i;
1491 	struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
1492 	int init_res[TCA_ACT_MAX_PRIO] = {};
1493 
1494 	for (loop = 0; loop < 10; loop++) {
1495 		ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0,
1496 				      actions, init_res, &attr_size, true, extack);
1497 		if (ret != -EAGAIN)
1498 			break;
1499 	}
1500 
1501 	if (ret < 0)
1502 		return ret;
1503 	ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
1504 
1505 	/* only put existing actions */
1506 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++)
1507 		if (init_res[i] == ACT_P_CREATED)
1508 			actions[i] = NULL;
1509 	tcf_action_put_many(actions);
1510 
1511 	return ret;
1512 }
1513 
1514 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
1515 	[TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_FLAG_LARGE_DUMP_ON),
1516 	[TCA_ROOT_TIME_DELTA]      = { .type = NLA_U32 },
1517 };
1518 
tc_ctl_action(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)1519 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
1520 			 struct netlink_ext_ack *extack)
1521 {
1522 	struct net *net = sock_net(skb->sk);
1523 	struct nlattr *tca[TCA_ROOT_MAX + 1];
1524 	u32 portid = NETLINK_CB(skb).portid;
1525 	int ret = 0, ovr = 0;
1526 
1527 	if ((n->nlmsg_type != RTM_GETACTION) &&
1528 	    !netlink_capable(skb, CAP_NET_ADMIN))
1529 		return -EPERM;
1530 
1531 	ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
1532 				     TCA_ROOT_MAX, NULL, extack);
1533 	if (ret < 0)
1534 		return ret;
1535 
1536 	if (tca[TCA_ACT_TAB] == NULL) {
1537 		NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
1538 		return -EINVAL;
1539 	}
1540 
1541 	/* n->nlmsg_flags & NLM_F_CREATE */
1542 	switch (n->nlmsg_type) {
1543 	case RTM_NEWACTION:
1544 		/* we are going to assume all other flags
1545 		 * imply create only if it doesn't exist
1546 		 * Note that CREATE | EXCL implies that
1547 		 * but since we want avoid ambiguity (eg when flags
1548 		 * is zero) then just set this
1549 		 */
1550 		if (n->nlmsg_flags & NLM_F_REPLACE)
1551 			ovr = 1;
1552 		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
1553 				     extack);
1554 		break;
1555 	case RTM_DELACTION:
1556 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1557 				    portid, RTM_DELACTION, extack);
1558 		break;
1559 	case RTM_GETACTION:
1560 		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1561 				    portid, RTM_GETACTION, extack);
1562 		break;
1563 	default:
1564 		BUG();
1565 	}
1566 
1567 	return ret;
1568 }
1569 
find_dump_kind(struct nlattr ** nla)1570 static struct nlattr *find_dump_kind(struct nlattr **nla)
1571 {
1572 	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1573 	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
1574 	struct nlattr *kind;
1575 
1576 	tb1 = nla[TCA_ACT_TAB];
1577 	if (tb1 == NULL)
1578 		return NULL;
1579 
1580 	if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
1581 		return NULL;
1582 
1583 	if (tb[1] == NULL)
1584 		return NULL;
1585 	if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
1586 		return NULL;
1587 	kind = tb2[TCA_ACT_KIND];
1588 
1589 	return kind;
1590 }
1591 
tc_dump_action(struct sk_buff * skb,struct netlink_callback * cb)1592 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1593 {
1594 	struct net *net = sock_net(skb->sk);
1595 	struct nlmsghdr *nlh;
1596 	unsigned char *b = skb_tail_pointer(skb);
1597 	struct nlattr *nest;
1598 	struct tc_action_ops *a_o;
1599 	int ret = 0;
1600 	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1601 	struct nlattr *tb[TCA_ROOT_MAX + 1];
1602 	struct nlattr *count_attr = NULL;
1603 	unsigned long jiffy_since = 0;
1604 	struct nlattr *kind = NULL;
1605 	struct nla_bitfield32 bf;
1606 	u32 msecs_since = 0;
1607 	u32 act_count = 0;
1608 
1609 	ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
1610 				     TCA_ROOT_MAX, tcaa_policy, cb->extack);
1611 	if (ret < 0)
1612 		return ret;
1613 
1614 	kind = find_dump_kind(tb);
1615 	if (kind == NULL) {
1616 		pr_info("tc_dump_action: action bad kind\n");
1617 		return 0;
1618 	}
1619 
1620 	a_o = tc_lookup_action(kind);
1621 	if (a_o == NULL)
1622 		return 0;
1623 
1624 	cb->args[2] = 0;
1625 	if (tb[TCA_ROOT_FLAGS]) {
1626 		bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
1627 		cb->args[2] = bf.value;
1628 	}
1629 
1630 	if (tb[TCA_ROOT_TIME_DELTA]) {
1631 		msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
1632 	}
1633 
1634 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1635 			cb->nlh->nlmsg_type, sizeof(*t), 0);
1636 	if (!nlh)
1637 		goto out_module_put;
1638 
1639 	if (msecs_since)
1640 		jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
1641 
1642 	t = nlmsg_data(nlh);
1643 	t->tca_family = AF_UNSPEC;
1644 	t->tca__pad1 = 0;
1645 	t->tca__pad2 = 0;
1646 	cb->args[3] = jiffy_since;
1647 	count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
1648 	if (!count_attr)
1649 		goto out_module_put;
1650 
1651 	nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
1652 	if (nest == NULL)
1653 		goto out_module_put;
1654 
1655 	ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
1656 	if (ret < 0)
1657 		goto out_module_put;
1658 
1659 	if (ret > 0) {
1660 		nla_nest_end(skb, nest);
1661 		ret = skb->len;
1662 		act_count = cb->args[1];
1663 		memcpy(nla_data(count_attr), &act_count, sizeof(u32));
1664 		cb->args[1] = 0;
1665 	} else
1666 		nlmsg_trim(skb, b);
1667 
1668 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1669 	if (NETLINK_CB(cb->skb).portid && ret)
1670 		nlh->nlmsg_flags |= NLM_F_MULTI;
1671 	module_put(a_o->owner);
1672 	return skb->len;
1673 
1674 out_module_put:
1675 	module_put(a_o->owner);
1676 	nlmsg_trim(skb, b);
1677 	return skb->len;
1678 }
1679 
tc_action_init(void)1680 static int __init tc_action_init(void)
1681 {
1682 	rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
1683 	rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
1684 	rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1685 		      0);
1686 
1687 	return 0;
1688 }
1689 
1690 subsys_initcall(tc_action_init);
1691