1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/cls_api.c Packet classifier API.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Changes:
8 *
9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
10 */
11
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/jhash.h>
24 #include <linux/rculist.h>
25 #include <net/net_namespace.h>
26 #include <net/sock.h>
27 #include <net/netlink.h>
28 #include <net/pkt_sched.h>
29 #include <net/pkt_cls.h>
30 #include <net/tc_act/tc_pedit.h>
31 #include <net/tc_act/tc_mirred.h>
32 #include <net/tc_act/tc_vlan.h>
33 #include <net/tc_act/tc_tunnel_key.h>
34 #include <net/tc_act/tc_csum.h>
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_police.h>
37 #include <net/tc_act/tc_sample.h>
38 #include <net/tc_act/tc_skbedit.h>
39 #include <net/tc_act/tc_ct.h>
40 #include <net/tc_act/tc_mpls.h>
41 #include <net/tc_act/tc_gate.h>
42 #include <net/flow_offload.h>
43
44 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
45
46 /* The list of all installed classifier types */
47 static LIST_HEAD(tcf_proto_base);
48
49 /* Protects list of registered TC modules. It is pure SMP lock. */
50 static DEFINE_RWLOCK(cls_mod_lock);
51
destroy_obj_hashfn(const struct tcf_proto * tp)52 static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
53 {
54 return jhash_3words(tp->chain->index, tp->prio,
55 (__force __u32)tp->protocol, 0);
56 }
57
tcf_proto_signal_destroying(struct tcf_chain * chain,struct tcf_proto * tp)58 static void tcf_proto_signal_destroying(struct tcf_chain *chain,
59 struct tcf_proto *tp)
60 {
61 struct tcf_block *block = chain->block;
62
63 mutex_lock(&block->proto_destroy_lock);
64 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
65 destroy_obj_hashfn(tp));
66 mutex_unlock(&block->proto_destroy_lock);
67 }
68
tcf_proto_cmp(const struct tcf_proto * tp1,const struct tcf_proto * tp2)69 static bool tcf_proto_cmp(const struct tcf_proto *tp1,
70 const struct tcf_proto *tp2)
71 {
72 return tp1->chain->index == tp2->chain->index &&
73 tp1->prio == tp2->prio &&
74 tp1->protocol == tp2->protocol;
75 }
76
tcf_proto_exists_destroying(struct tcf_chain * chain,struct tcf_proto * tp)77 static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
78 struct tcf_proto *tp)
79 {
80 u32 hash = destroy_obj_hashfn(tp);
81 struct tcf_proto *iter;
82 bool found = false;
83
84 rcu_read_lock();
85 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
86 destroy_ht_node, hash) {
87 if (tcf_proto_cmp(tp, iter)) {
88 found = true;
89 break;
90 }
91 }
92 rcu_read_unlock();
93
94 return found;
95 }
96
97 static void
tcf_proto_signal_destroyed(struct tcf_chain * chain,struct tcf_proto * tp)98 tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
99 {
100 struct tcf_block *block = chain->block;
101
102 mutex_lock(&block->proto_destroy_lock);
103 if (hash_hashed(&tp->destroy_ht_node))
104 hash_del_rcu(&tp->destroy_ht_node);
105 mutex_unlock(&block->proto_destroy_lock);
106 }
107
108 /* Find classifier type by string name */
109
__tcf_proto_lookup_ops(const char * kind)110 static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
111 {
112 const struct tcf_proto_ops *t, *res = NULL;
113
114 if (kind) {
115 read_lock(&cls_mod_lock);
116 list_for_each_entry(t, &tcf_proto_base, head) {
117 if (strcmp(kind, t->kind) == 0) {
118 if (try_module_get(t->owner))
119 res = t;
120 break;
121 }
122 }
123 read_unlock(&cls_mod_lock);
124 }
125 return res;
126 }
127
128 static const struct tcf_proto_ops *
tcf_proto_lookup_ops(const char * kind,bool rtnl_held,struct netlink_ext_ack * extack)129 tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
130 struct netlink_ext_ack *extack)
131 {
132 const struct tcf_proto_ops *ops;
133
134 ops = __tcf_proto_lookup_ops(kind);
135 if (ops)
136 return ops;
137 #ifdef CONFIG_MODULES
138 if (rtnl_held)
139 rtnl_unlock();
140 request_module("cls_%s", kind);
141 if (rtnl_held)
142 rtnl_lock();
143 ops = __tcf_proto_lookup_ops(kind);
144 /* We dropped the RTNL semaphore in order to perform
145 * the module load. So, even if we succeeded in loading
146 * the module we have to replay the request. We indicate
147 * this using -EAGAIN.
148 */
149 if (ops) {
150 module_put(ops->owner);
151 return ERR_PTR(-EAGAIN);
152 }
153 #endif
154 NL_SET_ERR_MSG(extack, "TC classifier not found");
155 return ERR_PTR(-ENOENT);
156 }
157
158 /* Register(unregister) new classifier type */
159
register_tcf_proto_ops(struct tcf_proto_ops * ops)160 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
161 {
162 struct tcf_proto_ops *t;
163 int rc = -EEXIST;
164
165 write_lock(&cls_mod_lock);
166 list_for_each_entry(t, &tcf_proto_base, head)
167 if (!strcmp(ops->kind, t->kind))
168 goto out;
169
170 list_add_tail(&ops->head, &tcf_proto_base);
171 rc = 0;
172 out:
173 write_unlock(&cls_mod_lock);
174 return rc;
175 }
176 EXPORT_SYMBOL(register_tcf_proto_ops);
177
178 static struct workqueue_struct *tc_filter_wq;
179
unregister_tcf_proto_ops(struct tcf_proto_ops * ops)180 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
181 {
182 struct tcf_proto_ops *t;
183 int rc = -ENOENT;
184
185 /* Wait for outstanding call_rcu()s, if any, from a
186 * tcf_proto_ops's destroy() handler.
187 */
188 rcu_barrier();
189 flush_workqueue(tc_filter_wq);
190
191 write_lock(&cls_mod_lock);
192 list_for_each_entry(t, &tcf_proto_base, head) {
193 if (t == ops) {
194 list_del(&t->head);
195 rc = 0;
196 break;
197 }
198 }
199 write_unlock(&cls_mod_lock);
200 return rc;
201 }
202 EXPORT_SYMBOL(unregister_tcf_proto_ops);
203
tcf_queue_work(struct rcu_work * rwork,work_func_t func)204 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
205 {
206 INIT_RCU_WORK(rwork, func);
207 return queue_rcu_work(tc_filter_wq, rwork);
208 }
209 EXPORT_SYMBOL(tcf_queue_work);
210
211 /* Select new prio value from the range, managed by kernel. */
212
tcf_auto_prio(struct tcf_proto * tp)213 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
214 {
215 u32 first = TC_H_MAKE(0xC0000000U, 0U);
216
217 if (tp)
218 first = tp->prio - 1;
219
220 return TC_H_MAJ(first);
221 }
222
tcf_proto_check_kind(struct nlattr * kind,char * name)223 static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
224 {
225 if (kind)
226 return nla_strlcpy(name, kind, IFNAMSIZ) >= IFNAMSIZ;
227 memset(name, 0, IFNAMSIZ);
228 return false;
229 }
230
tcf_proto_is_unlocked(const char * kind)231 static bool tcf_proto_is_unlocked(const char *kind)
232 {
233 const struct tcf_proto_ops *ops;
234 bool ret;
235
236 if (strlen(kind) == 0)
237 return false;
238
239 ops = tcf_proto_lookup_ops(kind, false, NULL);
240 /* On error return false to take rtnl lock. Proto lookup/create
241 * functions will perform lookup again and properly handle errors.
242 */
243 if (IS_ERR(ops))
244 return false;
245
246 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
247 module_put(ops->owner);
248 return ret;
249 }
250
tcf_proto_create(const char * kind,u32 protocol,u32 prio,struct tcf_chain * chain,bool rtnl_held,struct netlink_ext_ack * extack)251 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
252 u32 prio, struct tcf_chain *chain,
253 bool rtnl_held,
254 struct netlink_ext_ack *extack)
255 {
256 struct tcf_proto *tp;
257 int err;
258
259 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
260 if (!tp)
261 return ERR_PTR(-ENOBUFS);
262
263 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
264 if (IS_ERR(tp->ops)) {
265 err = PTR_ERR(tp->ops);
266 goto errout;
267 }
268 tp->classify = tp->ops->classify;
269 tp->protocol = protocol;
270 tp->prio = prio;
271 tp->chain = chain;
272 spin_lock_init(&tp->lock);
273 refcount_set(&tp->refcnt, 1);
274
275 err = tp->ops->init(tp);
276 if (err) {
277 module_put(tp->ops->owner);
278 goto errout;
279 }
280 return tp;
281
282 errout:
283 kfree(tp);
284 return ERR_PTR(err);
285 }
286
tcf_proto_get(struct tcf_proto * tp)287 static void tcf_proto_get(struct tcf_proto *tp)
288 {
289 refcount_inc(&tp->refcnt);
290 }
291
292 static void tcf_chain_put(struct tcf_chain *chain);
293
tcf_proto_destroy(struct tcf_proto * tp,bool rtnl_held,bool sig_destroy,struct netlink_ext_ack * extack)294 static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
295 bool sig_destroy, struct netlink_ext_ack *extack)
296 {
297 tp->ops->destroy(tp, rtnl_held, extack);
298 if (sig_destroy)
299 tcf_proto_signal_destroyed(tp->chain, tp);
300 tcf_chain_put(tp->chain);
301 module_put(tp->ops->owner);
302 kfree_rcu(tp, rcu);
303 }
304
tcf_proto_put(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)305 static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
306 struct netlink_ext_ack *extack)
307 {
308 if (refcount_dec_and_test(&tp->refcnt))
309 tcf_proto_destroy(tp, rtnl_held, true, extack);
310 }
311
tcf_proto_check_delete(struct tcf_proto * tp)312 static bool tcf_proto_check_delete(struct tcf_proto *tp)
313 {
314 if (tp->ops->delete_empty)
315 return tp->ops->delete_empty(tp);
316
317 tp->deleting = true;
318 return tp->deleting;
319 }
320
tcf_proto_mark_delete(struct tcf_proto * tp)321 static void tcf_proto_mark_delete(struct tcf_proto *tp)
322 {
323 spin_lock(&tp->lock);
324 tp->deleting = true;
325 spin_unlock(&tp->lock);
326 }
327
tcf_proto_is_deleting(struct tcf_proto * tp)328 static bool tcf_proto_is_deleting(struct tcf_proto *tp)
329 {
330 bool deleting;
331
332 spin_lock(&tp->lock);
333 deleting = tp->deleting;
334 spin_unlock(&tp->lock);
335
336 return deleting;
337 }
338
339 #define ASSERT_BLOCK_LOCKED(block) \
340 lockdep_assert_held(&(block)->lock)
341
342 struct tcf_filter_chain_list_item {
343 struct list_head list;
344 tcf_chain_head_change_t *chain_head_change;
345 void *chain_head_change_priv;
346 };
347
tcf_chain_create(struct tcf_block * block,u32 chain_index)348 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
349 u32 chain_index)
350 {
351 struct tcf_chain *chain;
352
353 ASSERT_BLOCK_LOCKED(block);
354
355 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
356 if (!chain)
357 return NULL;
358 list_add_tail_rcu(&chain->list, &block->chain_list);
359 mutex_init(&chain->filter_chain_lock);
360 chain->block = block;
361 chain->index = chain_index;
362 chain->refcnt = 1;
363 if (!chain->index)
364 block->chain0.chain = chain;
365 return chain;
366 }
367
tcf_chain_head_change_item(struct tcf_filter_chain_list_item * item,struct tcf_proto * tp_head)368 static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
369 struct tcf_proto *tp_head)
370 {
371 if (item->chain_head_change)
372 item->chain_head_change(tp_head, item->chain_head_change_priv);
373 }
374
tcf_chain0_head_change(struct tcf_chain * chain,struct tcf_proto * tp_head)375 static void tcf_chain0_head_change(struct tcf_chain *chain,
376 struct tcf_proto *tp_head)
377 {
378 struct tcf_filter_chain_list_item *item;
379 struct tcf_block *block = chain->block;
380
381 if (chain->index)
382 return;
383
384 mutex_lock(&block->lock);
385 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
386 tcf_chain_head_change_item(item, tp_head);
387 mutex_unlock(&block->lock);
388 }
389
390 /* Returns true if block can be safely freed. */
391
tcf_chain_detach(struct tcf_chain * chain)392 static bool tcf_chain_detach(struct tcf_chain *chain)
393 {
394 struct tcf_block *block = chain->block;
395
396 ASSERT_BLOCK_LOCKED(block);
397
398 list_del_rcu(&chain->list);
399 if (!chain->index)
400 block->chain0.chain = NULL;
401
402 if (list_empty(&block->chain_list) &&
403 refcount_read(&block->refcnt) == 0)
404 return true;
405
406 return false;
407 }
408
tcf_block_destroy(struct tcf_block * block)409 static void tcf_block_destroy(struct tcf_block *block)
410 {
411 mutex_destroy(&block->lock);
412 mutex_destroy(&block->proto_destroy_lock);
413 kfree_rcu(block, rcu);
414 }
415
tcf_chain_destroy(struct tcf_chain * chain,bool free_block)416 static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
417 {
418 struct tcf_block *block = chain->block;
419
420 mutex_destroy(&chain->filter_chain_lock);
421 kfree_rcu(chain, rcu);
422 if (free_block)
423 tcf_block_destroy(block);
424 }
425
tcf_chain_hold(struct tcf_chain * chain)426 static void tcf_chain_hold(struct tcf_chain *chain)
427 {
428 ASSERT_BLOCK_LOCKED(chain->block);
429
430 ++chain->refcnt;
431 }
432
tcf_chain_held_by_acts_only(struct tcf_chain * chain)433 static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
434 {
435 ASSERT_BLOCK_LOCKED(chain->block);
436
437 /* In case all the references are action references, this
438 * chain should not be shown to the user.
439 */
440 return chain->refcnt == chain->action_refcnt;
441 }
442
tcf_chain_lookup(struct tcf_block * block,u32 chain_index)443 static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
444 u32 chain_index)
445 {
446 struct tcf_chain *chain;
447
448 ASSERT_BLOCK_LOCKED(block);
449
450 list_for_each_entry(chain, &block->chain_list, list) {
451 if (chain->index == chain_index)
452 return chain;
453 }
454 return NULL;
455 }
456
457 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
tcf_chain_lookup_rcu(const struct tcf_block * block,u32 chain_index)458 static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
459 u32 chain_index)
460 {
461 struct tcf_chain *chain;
462
463 list_for_each_entry_rcu(chain, &block->chain_list, list) {
464 if (chain->index == chain_index)
465 return chain;
466 }
467 return NULL;
468 }
469 #endif
470
471 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
472 u32 seq, u16 flags, int event, bool unicast);
473
__tcf_chain_get(struct tcf_block * block,u32 chain_index,bool create,bool by_act)474 static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
475 u32 chain_index, bool create,
476 bool by_act)
477 {
478 struct tcf_chain *chain = NULL;
479 bool is_first_reference;
480
481 mutex_lock(&block->lock);
482 chain = tcf_chain_lookup(block, chain_index);
483 if (chain) {
484 tcf_chain_hold(chain);
485 } else {
486 if (!create)
487 goto errout;
488 chain = tcf_chain_create(block, chain_index);
489 if (!chain)
490 goto errout;
491 }
492
493 if (by_act)
494 ++chain->action_refcnt;
495 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
496 mutex_unlock(&block->lock);
497
498 /* Send notification only in case we got the first
499 * non-action reference. Until then, the chain acts only as
500 * a placeholder for actions pointing to it and user ought
501 * not know about them.
502 */
503 if (is_first_reference && !by_act)
504 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
505 RTM_NEWCHAIN, false);
506
507 return chain;
508
509 errout:
510 mutex_unlock(&block->lock);
511 return chain;
512 }
513
tcf_chain_get(struct tcf_block * block,u32 chain_index,bool create)514 static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
515 bool create)
516 {
517 return __tcf_chain_get(block, chain_index, create, false);
518 }
519
tcf_chain_get_by_act(struct tcf_block * block,u32 chain_index)520 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
521 {
522 return __tcf_chain_get(block, chain_index, true, true);
523 }
524 EXPORT_SYMBOL(tcf_chain_get_by_act);
525
526 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
527 void *tmplt_priv);
528 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
529 void *tmplt_priv, u32 chain_index,
530 struct tcf_block *block, struct sk_buff *oskb,
531 u32 seq, u16 flags, bool unicast);
532
__tcf_chain_put(struct tcf_chain * chain,bool by_act,bool explicitly_created)533 static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
534 bool explicitly_created)
535 {
536 struct tcf_block *block = chain->block;
537 const struct tcf_proto_ops *tmplt_ops;
538 bool free_block = false;
539 unsigned int refcnt;
540 void *tmplt_priv;
541
542 mutex_lock(&block->lock);
543 if (explicitly_created) {
544 if (!chain->explicitly_created) {
545 mutex_unlock(&block->lock);
546 return;
547 }
548 chain->explicitly_created = false;
549 }
550
551 if (by_act)
552 chain->action_refcnt--;
553
554 /* tc_chain_notify_delete can't be called while holding block lock.
555 * However, when block is unlocked chain can be changed concurrently, so
556 * save these to temporary variables.
557 */
558 refcnt = --chain->refcnt;
559 tmplt_ops = chain->tmplt_ops;
560 tmplt_priv = chain->tmplt_priv;
561
562 /* The last dropped non-action reference will trigger notification. */
563 if (refcnt - chain->action_refcnt == 0 && !by_act) {
564 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
565 block, NULL, 0, 0, false);
566 /* Last reference to chain, no need to lock. */
567 chain->flushing = false;
568 }
569
570 if (refcnt == 0)
571 free_block = tcf_chain_detach(chain);
572 mutex_unlock(&block->lock);
573
574 if (refcnt == 0) {
575 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
576 tcf_chain_destroy(chain, free_block);
577 }
578 }
579
tcf_chain_put(struct tcf_chain * chain)580 static void tcf_chain_put(struct tcf_chain *chain)
581 {
582 __tcf_chain_put(chain, false, false);
583 }
584
tcf_chain_put_by_act(struct tcf_chain * chain)585 void tcf_chain_put_by_act(struct tcf_chain *chain)
586 {
587 __tcf_chain_put(chain, true, false);
588 }
589 EXPORT_SYMBOL(tcf_chain_put_by_act);
590
tcf_chain_put_explicitly_created(struct tcf_chain * chain)591 static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
592 {
593 __tcf_chain_put(chain, false, true);
594 }
595
tcf_chain_flush(struct tcf_chain * chain,bool rtnl_held)596 static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
597 {
598 struct tcf_proto *tp, *tp_next;
599
600 mutex_lock(&chain->filter_chain_lock);
601 tp = tcf_chain_dereference(chain->filter_chain, chain);
602 while (tp) {
603 tp_next = rcu_dereference_protected(tp->next, 1);
604 tcf_proto_signal_destroying(chain, tp);
605 tp = tp_next;
606 }
607 tp = tcf_chain_dereference(chain->filter_chain, chain);
608 RCU_INIT_POINTER(chain->filter_chain, NULL);
609 tcf_chain0_head_change(chain, NULL);
610 chain->flushing = true;
611 mutex_unlock(&chain->filter_chain_lock);
612
613 while (tp) {
614 tp_next = rcu_dereference_protected(tp->next, 1);
615 tcf_proto_put(tp, rtnl_held, NULL);
616 tp = tp_next;
617 }
618 }
619
620 static int tcf_block_setup(struct tcf_block *block,
621 struct flow_block_offload *bo);
622
tcf_block_offload_init(struct flow_block_offload * bo,struct net_device * dev,struct Qdisc * sch,enum flow_block_command command,enum flow_block_binder_type binder_type,struct flow_block * flow_block,bool shared,struct netlink_ext_ack * extack)623 static void tcf_block_offload_init(struct flow_block_offload *bo,
624 struct net_device *dev, struct Qdisc *sch,
625 enum flow_block_command command,
626 enum flow_block_binder_type binder_type,
627 struct flow_block *flow_block,
628 bool shared, struct netlink_ext_ack *extack)
629 {
630 bo->net = dev_net(dev);
631 bo->command = command;
632 bo->binder_type = binder_type;
633 bo->block = flow_block;
634 bo->block_shared = shared;
635 bo->extack = extack;
636 bo->sch = sch;
637 bo->cb_list_head = &flow_block->cb_list;
638 INIT_LIST_HEAD(&bo->cb_list);
639 }
640
641 static void tcf_block_unbind(struct tcf_block *block,
642 struct flow_block_offload *bo);
643
tc_block_indr_cleanup(struct flow_block_cb * block_cb)644 static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
645 {
646 struct tcf_block *block = block_cb->indr.data;
647 struct net_device *dev = block_cb->indr.dev;
648 struct Qdisc *sch = block_cb->indr.sch;
649 struct netlink_ext_ack extack = {};
650 struct flow_block_offload bo = {};
651
652 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
653 block_cb->indr.binder_type,
654 &block->flow_block, tcf_block_shared(block),
655 &extack);
656 rtnl_lock();
657 down_write(&block->cb_lock);
658 list_del(&block_cb->driver_list);
659 list_move(&block_cb->list, &bo.cb_list);
660 tcf_block_unbind(block, &bo);
661 up_write(&block->cb_lock);
662 rtnl_unlock();
663 }
664
tcf_block_offload_in_use(struct tcf_block * block)665 static bool tcf_block_offload_in_use(struct tcf_block *block)
666 {
667 return atomic_read(&block->offloadcnt);
668 }
669
tcf_block_offload_cmd(struct tcf_block * block,struct net_device * dev,struct Qdisc * sch,struct tcf_block_ext_info * ei,enum flow_block_command command,struct netlink_ext_ack * extack)670 static int tcf_block_offload_cmd(struct tcf_block *block,
671 struct net_device *dev, struct Qdisc *sch,
672 struct tcf_block_ext_info *ei,
673 enum flow_block_command command,
674 struct netlink_ext_ack *extack)
675 {
676 struct flow_block_offload bo = {};
677
678 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
679 &block->flow_block, tcf_block_shared(block),
680 extack);
681
682 if (dev->netdev_ops->ndo_setup_tc) {
683 int err;
684
685 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
686 if (err < 0) {
687 if (err != -EOPNOTSUPP)
688 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
689 return err;
690 }
691
692 return tcf_block_setup(block, &bo);
693 }
694
695 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
696 tc_block_indr_cleanup);
697 tcf_block_setup(block, &bo);
698
699 return -EOPNOTSUPP;
700 }
701
tcf_block_offload_bind(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)702 static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
703 struct tcf_block_ext_info *ei,
704 struct netlink_ext_ack *extack)
705 {
706 struct net_device *dev = q->dev_queue->dev;
707 int err;
708
709 down_write(&block->cb_lock);
710
711 /* If tc offload feature is disabled and the block we try to bind
712 * to already has some offloaded filters, forbid to bind.
713 */
714 if (dev->netdev_ops->ndo_setup_tc &&
715 !tc_can_offload(dev) &&
716 tcf_block_offload_in_use(block)) {
717 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
718 err = -EOPNOTSUPP;
719 goto err_unlock;
720 }
721
722 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
723 if (err == -EOPNOTSUPP)
724 goto no_offload_dev_inc;
725 if (err)
726 goto err_unlock;
727
728 up_write(&block->cb_lock);
729 return 0;
730
731 no_offload_dev_inc:
732 if (tcf_block_offload_in_use(block))
733 goto err_unlock;
734
735 err = 0;
736 block->nooffloaddevcnt++;
737 err_unlock:
738 up_write(&block->cb_lock);
739 return err;
740 }
741
tcf_block_offload_unbind(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei)742 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
743 struct tcf_block_ext_info *ei)
744 {
745 struct net_device *dev = q->dev_queue->dev;
746 int err;
747
748 down_write(&block->cb_lock);
749 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
750 if (err == -EOPNOTSUPP)
751 goto no_offload_dev_dec;
752 up_write(&block->cb_lock);
753 return;
754
755 no_offload_dev_dec:
756 WARN_ON(block->nooffloaddevcnt-- == 0);
757 up_write(&block->cb_lock);
758 }
759
760 static int
tcf_chain0_head_change_cb_add(struct tcf_block * block,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)761 tcf_chain0_head_change_cb_add(struct tcf_block *block,
762 struct tcf_block_ext_info *ei,
763 struct netlink_ext_ack *extack)
764 {
765 struct tcf_filter_chain_list_item *item;
766 struct tcf_chain *chain0;
767
768 item = kmalloc(sizeof(*item), GFP_KERNEL);
769 if (!item) {
770 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
771 return -ENOMEM;
772 }
773 item->chain_head_change = ei->chain_head_change;
774 item->chain_head_change_priv = ei->chain_head_change_priv;
775
776 mutex_lock(&block->lock);
777 chain0 = block->chain0.chain;
778 if (chain0)
779 tcf_chain_hold(chain0);
780 else
781 list_add(&item->list, &block->chain0.filter_chain_list);
782 mutex_unlock(&block->lock);
783
784 if (chain0) {
785 struct tcf_proto *tp_head;
786
787 mutex_lock(&chain0->filter_chain_lock);
788
789 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
790 if (tp_head)
791 tcf_chain_head_change_item(item, tp_head);
792
793 mutex_lock(&block->lock);
794 list_add(&item->list, &block->chain0.filter_chain_list);
795 mutex_unlock(&block->lock);
796
797 mutex_unlock(&chain0->filter_chain_lock);
798 tcf_chain_put(chain0);
799 }
800
801 return 0;
802 }
803
804 static void
tcf_chain0_head_change_cb_del(struct tcf_block * block,struct tcf_block_ext_info * ei)805 tcf_chain0_head_change_cb_del(struct tcf_block *block,
806 struct tcf_block_ext_info *ei)
807 {
808 struct tcf_filter_chain_list_item *item;
809
810 mutex_lock(&block->lock);
811 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
812 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
813 (item->chain_head_change == ei->chain_head_change &&
814 item->chain_head_change_priv == ei->chain_head_change_priv)) {
815 if (block->chain0.chain)
816 tcf_chain_head_change_item(item, NULL);
817 list_del(&item->list);
818 mutex_unlock(&block->lock);
819
820 kfree(item);
821 return;
822 }
823 }
824 mutex_unlock(&block->lock);
825 WARN_ON(1);
826 }
827
828 struct tcf_net {
829 spinlock_t idr_lock; /* Protects idr */
830 struct idr idr;
831 };
832
833 static unsigned int tcf_net_id;
834
tcf_block_insert(struct tcf_block * block,struct net * net,struct netlink_ext_ack * extack)835 static int tcf_block_insert(struct tcf_block *block, struct net *net,
836 struct netlink_ext_ack *extack)
837 {
838 struct tcf_net *tn = net_generic(net, tcf_net_id);
839 int err;
840
841 idr_preload(GFP_KERNEL);
842 spin_lock(&tn->idr_lock);
843 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
844 GFP_NOWAIT);
845 spin_unlock(&tn->idr_lock);
846 idr_preload_end();
847
848 return err;
849 }
850
tcf_block_remove(struct tcf_block * block,struct net * net)851 static void tcf_block_remove(struct tcf_block *block, struct net *net)
852 {
853 struct tcf_net *tn = net_generic(net, tcf_net_id);
854
855 spin_lock(&tn->idr_lock);
856 idr_remove(&tn->idr, block->index);
857 spin_unlock(&tn->idr_lock);
858 }
859
tcf_block_create(struct net * net,struct Qdisc * q,u32 block_index,struct netlink_ext_ack * extack)860 static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
861 u32 block_index,
862 struct netlink_ext_ack *extack)
863 {
864 struct tcf_block *block;
865
866 block = kzalloc(sizeof(*block), GFP_KERNEL);
867 if (!block) {
868 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
869 return ERR_PTR(-ENOMEM);
870 }
871 mutex_init(&block->lock);
872 mutex_init(&block->proto_destroy_lock);
873 init_rwsem(&block->cb_lock);
874 flow_block_init(&block->flow_block);
875 INIT_LIST_HEAD(&block->chain_list);
876 INIT_LIST_HEAD(&block->owner_list);
877 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
878
879 refcount_set(&block->refcnt, 1);
880 block->net = net;
881 block->index = block_index;
882
883 /* Don't store q pointer for blocks which are shared */
884 if (!tcf_block_shared(block))
885 block->q = q;
886 return block;
887 }
888
tcf_block_lookup(struct net * net,u32 block_index)889 static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
890 {
891 struct tcf_net *tn = net_generic(net, tcf_net_id);
892
893 return idr_find(&tn->idr, block_index);
894 }
895
tcf_block_refcnt_get(struct net * net,u32 block_index)896 static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
897 {
898 struct tcf_block *block;
899
900 rcu_read_lock();
901 block = tcf_block_lookup(net, block_index);
902 if (block && !refcount_inc_not_zero(&block->refcnt))
903 block = NULL;
904 rcu_read_unlock();
905
906 return block;
907 }
908
909 static struct tcf_chain *
__tcf_get_next_chain(struct tcf_block * block,struct tcf_chain * chain)910 __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
911 {
912 mutex_lock(&block->lock);
913 if (chain)
914 chain = list_is_last(&chain->list, &block->chain_list) ?
915 NULL : list_next_entry(chain, list);
916 else
917 chain = list_first_entry_or_null(&block->chain_list,
918 struct tcf_chain, list);
919
920 /* skip all action-only chains */
921 while (chain && tcf_chain_held_by_acts_only(chain))
922 chain = list_is_last(&chain->list, &block->chain_list) ?
923 NULL : list_next_entry(chain, list);
924
925 if (chain)
926 tcf_chain_hold(chain);
927 mutex_unlock(&block->lock);
928
929 return chain;
930 }
931
932 /* Function to be used by all clients that want to iterate over all chains on
933 * block. It properly obtains block->lock and takes reference to chain before
934 * returning it. Users of this function must be tolerant to concurrent chain
935 * insertion/deletion or ensure that no concurrent chain modification is
936 * possible. Note that all netlink dump callbacks cannot guarantee to provide
937 * consistent dump because rtnl lock is released each time skb is filled with
938 * data and sent to user-space.
939 */
940
941 struct tcf_chain *
tcf_get_next_chain(struct tcf_block * block,struct tcf_chain * chain)942 tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
943 {
944 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
945
946 if (chain)
947 tcf_chain_put(chain);
948
949 return chain_next;
950 }
951 EXPORT_SYMBOL(tcf_get_next_chain);
952
953 static struct tcf_proto *
__tcf_get_next_proto(struct tcf_chain * chain,struct tcf_proto * tp)954 __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
955 {
956 u32 prio = 0;
957
958 ASSERT_RTNL();
959 mutex_lock(&chain->filter_chain_lock);
960
961 if (!tp) {
962 tp = tcf_chain_dereference(chain->filter_chain, chain);
963 } else if (tcf_proto_is_deleting(tp)) {
964 /* 'deleting' flag is set and chain->filter_chain_lock was
965 * unlocked, which means next pointer could be invalid. Restart
966 * search.
967 */
968 prio = tp->prio + 1;
969 tp = tcf_chain_dereference(chain->filter_chain, chain);
970
971 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
972 if (!tp->deleting && tp->prio >= prio)
973 break;
974 } else {
975 tp = tcf_chain_dereference(tp->next, chain);
976 }
977
978 if (tp)
979 tcf_proto_get(tp);
980
981 mutex_unlock(&chain->filter_chain_lock);
982
983 return tp;
984 }
985
986 /* Function to be used by all clients that want to iterate over all tp's on
987 * chain. Users of this function must be tolerant to concurrent tp
988 * insertion/deletion or ensure that no concurrent chain modification is
989 * possible. Note that all netlink dump callbacks cannot guarantee to provide
990 * consistent dump because rtnl lock is released each time skb is filled with
991 * data and sent to user-space.
992 */
993
994 struct tcf_proto *
tcf_get_next_proto(struct tcf_chain * chain,struct tcf_proto * tp,bool rtnl_held)995 tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp,
996 bool rtnl_held)
997 {
998 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
999
1000 if (tp)
1001 tcf_proto_put(tp, rtnl_held, NULL);
1002
1003 return tp_next;
1004 }
1005 EXPORT_SYMBOL(tcf_get_next_proto);
1006
tcf_block_flush_all_chains(struct tcf_block * block,bool rtnl_held)1007 static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
1008 {
1009 struct tcf_chain *chain;
1010
1011 /* Last reference to block. At this point chains cannot be added or
1012 * removed concurrently.
1013 */
1014 for (chain = tcf_get_next_chain(block, NULL);
1015 chain;
1016 chain = tcf_get_next_chain(block, chain)) {
1017 tcf_chain_put_explicitly_created(chain);
1018 tcf_chain_flush(chain, rtnl_held);
1019 }
1020 }
1021
1022 /* Lookup Qdisc and increments its reference counter.
1023 * Set parent, if necessary.
1024 */
1025
__tcf_qdisc_find(struct net * net,struct Qdisc ** q,u32 * parent,int ifindex,bool rtnl_held,struct netlink_ext_ack * extack)1026 static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1027 u32 *parent, int ifindex, bool rtnl_held,
1028 struct netlink_ext_ack *extack)
1029 {
1030 const struct Qdisc_class_ops *cops;
1031 struct net_device *dev;
1032 int err = 0;
1033
1034 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1035 return 0;
1036
1037 rcu_read_lock();
1038
1039 /* Find link */
1040 dev = dev_get_by_index_rcu(net, ifindex);
1041 if (!dev) {
1042 rcu_read_unlock();
1043 return -ENODEV;
1044 }
1045
1046 /* Find qdisc */
1047 if (!*parent) {
1048 *q = dev->qdisc;
1049 *parent = (*q)->handle;
1050 } else {
1051 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1052 if (!*q) {
1053 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1054 err = -EINVAL;
1055 goto errout_rcu;
1056 }
1057 }
1058
1059 *q = qdisc_refcount_inc_nz(*q);
1060 if (!*q) {
1061 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1062 err = -EINVAL;
1063 goto errout_rcu;
1064 }
1065
1066 /* Is it classful? */
1067 cops = (*q)->ops->cl_ops;
1068 if (!cops) {
1069 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1070 err = -EINVAL;
1071 goto errout_qdisc;
1072 }
1073
1074 if (!cops->tcf_block) {
1075 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1076 err = -EOPNOTSUPP;
1077 goto errout_qdisc;
1078 }
1079
1080 errout_rcu:
1081 /* At this point we know that qdisc is not noop_qdisc,
1082 * which means that qdisc holds a reference to net_device
1083 * and we hold a reference to qdisc, so it is safe to release
1084 * rcu read lock.
1085 */
1086 rcu_read_unlock();
1087 return err;
1088
1089 errout_qdisc:
1090 rcu_read_unlock();
1091
1092 if (rtnl_held)
1093 qdisc_put(*q);
1094 else
1095 qdisc_put_unlocked(*q);
1096 *q = NULL;
1097
1098 return err;
1099 }
1100
__tcf_qdisc_cl_find(struct Qdisc * q,u32 parent,unsigned long * cl,int ifindex,struct netlink_ext_ack * extack)1101 static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1102 int ifindex, struct netlink_ext_ack *extack)
1103 {
1104 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1105 return 0;
1106
1107 /* Do we search for filter, attached to class? */
1108 if (TC_H_MIN(parent)) {
1109 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1110
1111 *cl = cops->find(q, parent);
1112 if (*cl == 0) {
1113 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1114 return -ENOENT;
1115 }
1116 }
1117
1118 return 0;
1119 }
1120
__tcf_block_find(struct net * net,struct Qdisc * q,unsigned long cl,int ifindex,u32 block_index,struct netlink_ext_ack * extack)1121 static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1122 unsigned long cl, int ifindex,
1123 u32 block_index,
1124 struct netlink_ext_ack *extack)
1125 {
1126 struct tcf_block *block;
1127
1128 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1129 block = tcf_block_refcnt_get(net, block_index);
1130 if (!block) {
1131 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1132 return ERR_PTR(-EINVAL);
1133 }
1134 } else {
1135 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1136
1137 block = cops->tcf_block(q, cl, extack);
1138 if (!block)
1139 return ERR_PTR(-EINVAL);
1140
1141 if (tcf_block_shared(block)) {
1142 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1143 return ERR_PTR(-EOPNOTSUPP);
1144 }
1145
1146 /* Always take reference to block in order to support execution
1147 * of rules update path of cls API without rtnl lock. Caller
1148 * must release block when it is finished using it. 'if' block
1149 * of this conditional obtain reference to block by calling
1150 * tcf_block_refcnt_get().
1151 */
1152 refcount_inc(&block->refcnt);
1153 }
1154
1155 return block;
1156 }
1157
__tcf_block_put(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei,bool rtnl_held)1158 static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
1159 struct tcf_block_ext_info *ei, bool rtnl_held)
1160 {
1161 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
1162 /* Flushing/putting all chains will cause the block to be
1163 * deallocated when last chain is freed. However, if chain_list
1164 * is empty, block has to be manually deallocated. After block
1165 * reference counter reached 0, it is no longer possible to
1166 * increment it or add new chains to block.
1167 */
1168 bool free_block = list_empty(&block->chain_list);
1169
1170 mutex_unlock(&block->lock);
1171 if (tcf_block_shared(block))
1172 tcf_block_remove(block, block->net);
1173
1174 if (q)
1175 tcf_block_offload_unbind(block, q, ei);
1176
1177 if (free_block)
1178 tcf_block_destroy(block);
1179 else
1180 tcf_block_flush_all_chains(block, rtnl_held);
1181 } else if (q) {
1182 tcf_block_offload_unbind(block, q, ei);
1183 }
1184 }
1185
tcf_block_refcnt_put(struct tcf_block * block,bool rtnl_held)1186 static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
1187 {
1188 __tcf_block_put(block, NULL, NULL, rtnl_held);
1189 }
1190
1191 /* Find tcf block.
1192 * Set q, parent, cl when appropriate.
1193 */
1194
tcf_block_find(struct net * net,struct Qdisc ** q,u32 * parent,unsigned long * cl,int ifindex,u32 block_index,struct netlink_ext_ack * extack)1195 static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1196 u32 *parent, unsigned long *cl,
1197 int ifindex, u32 block_index,
1198 struct netlink_ext_ack *extack)
1199 {
1200 struct tcf_block *block;
1201 int err = 0;
1202
1203 ASSERT_RTNL();
1204
1205 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1206 if (err)
1207 goto errout;
1208
1209 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1210 if (err)
1211 goto errout_qdisc;
1212
1213 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
1214 if (IS_ERR(block)) {
1215 err = PTR_ERR(block);
1216 goto errout_qdisc;
1217 }
1218
1219 return block;
1220
1221 errout_qdisc:
1222 if (*q)
1223 qdisc_put(*q);
1224 errout:
1225 *q = NULL;
1226 return ERR_PTR(err);
1227 }
1228
tcf_block_release(struct Qdisc * q,struct tcf_block * block,bool rtnl_held)1229 static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1230 bool rtnl_held)
1231 {
1232 if (!IS_ERR_OR_NULL(block))
1233 tcf_block_refcnt_put(block, rtnl_held);
1234
1235 if (q) {
1236 if (rtnl_held)
1237 qdisc_put(q);
1238 else
1239 qdisc_put_unlocked(q);
1240 }
1241 }
1242
1243 struct tcf_block_owner_item {
1244 struct list_head list;
1245 struct Qdisc *q;
1246 enum flow_block_binder_type binder_type;
1247 };
1248
1249 static void
tcf_block_owner_netif_keep_dst(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1250 tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1251 struct Qdisc *q,
1252 enum flow_block_binder_type binder_type)
1253 {
1254 if (block->keep_dst &&
1255 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1256 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1257 netif_keep_dst(qdisc_dev(q));
1258 }
1259
tcf_block_netif_keep_dst(struct tcf_block * block)1260 void tcf_block_netif_keep_dst(struct tcf_block *block)
1261 {
1262 struct tcf_block_owner_item *item;
1263
1264 block->keep_dst = true;
1265 list_for_each_entry(item, &block->owner_list, list)
1266 tcf_block_owner_netif_keep_dst(block, item->q,
1267 item->binder_type);
1268 }
1269 EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1270
tcf_block_owner_add(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1271 static int tcf_block_owner_add(struct tcf_block *block,
1272 struct Qdisc *q,
1273 enum flow_block_binder_type binder_type)
1274 {
1275 struct tcf_block_owner_item *item;
1276
1277 item = kmalloc(sizeof(*item), GFP_KERNEL);
1278 if (!item)
1279 return -ENOMEM;
1280 item->q = q;
1281 item->binder_type = binder_type;
1282 list_add(&item->list, &block->owner_list);
1283 return 0;
1284 }
1285
tcf_block_owner_del(struct tcf_block * block,struct Qdisc * q,enum flow_block_binder_type binder_type)1286 static void tcf_block_owner_del(struct tcf_block *block,
1287 struct Qdisc *q,
1288 enum flow_block_binder_type binder_type)
1289 {
1290 struct tcf_block_owner_item *item;
1291
1292 list_for_each_entry(item, &block->owner_list, list) {
1293 if (item->q == q && item->binder_type == binder_type) {
1294 list_del(&item->list);
1295 kfree(item);
1296 return;
1297 }
1298 }
1299 WARN_ON(1);
1300 }
1301
tcf_block_get_ext(struct tcf_block ** p_block,struct Qdisc * q,struct tcf_block_ext_info * ei,struct netlink_ext_ack * extack)1302 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1303 struct tcf_block_ext_info *ei,
1304 struct netlink_ext_ack *extack)
1305 {
1306 struct net *net = qdisc_net(q);
1307 struct tcf_block *block = NULL;
1308 int err;
1309
1310 if (ei->block_index)
1311 /* block_index not 0 means the shared block is requested */
1312 block = tcf_block_refcnt_get(net, ei->block_index);
1313
1314 if (!block) {
1315 block = tcf_block_create(net, q, ei->block_index, extack);
1316 if (IS_ERR(block))
1317 return PTR_ERR(block);
1318 if (tcf_block_shared(block)) {
1319 err = tcf_block_insert(block, net, extack);
1320 if (err)
1321 goto err_block_insert;
1322 }
1323 }
1324
1325 err = tcf_block_owner_add(block, q, ei->binder_type);
1326 if (err)
1327 goto err_block_owner_add;
1328
1329 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1330
1331 err = tcf_chain0_head_change_cb_add(block, ei, extack);
1332 if (err)
1333 goto err_chain0_head_change_cb_add;
1334
1335 err = tcf_block_offload_bind(block, q, ei, extack);
1336 if (err)
1337 goto err_block_offload_bind;
1338
1339 *p_block = block;
1340 return 0;
1341
1342 err_block_offload_bind:
1343 tcf_chain0_head_change_cb_del(block, ei);
1344 err_chain0_head_change_cb_add:
1345 tcf_block_owner_del(block, q, ei->binder_type);
1346 err_block_owner_add:
1347 err_block_insert:
1348 tcf_block_refcnt_put(block, true);
1349 return err;
1350 }
1351 EXPORT_SYMBOL(tcf_block_get_ext);
1352
tcf_chain_head_change_dflt(struct tcf_proto * tp_head,void * priv)1353 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1354 {
1355 struct tcf_proto __rcu **p_filter_chain = priv;
1356
1357 rcu_assign_pointer(*p_filter_chain, tp_head);
1358 }
1359
tcf_block_get(struct tcf_block ** p_block,struct tcf_proto __rcu ** p_filter_chain,struct Qdisc * q,struct netlink_ext_ack * extack)1360 int tcf_block_get(struct tcf_block **p_block,
1361 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1362 struct netlink_ext_ack *extack)
1363 {
1364 struct tcf_block_ext_info ei = {
1365 .chain_head_change = tcf_chain_head_change_dflt,
1366 .chain_head_change_priv = p_filter_chain,
1367 };
1368
1369 WARN_ON(!p_filter_chain);
1370 return tcf_block_get_ext(p_block, q, &ei, extack);
1371 }
1372 EXPORT_SYMBOL(tcf_block_get);
1373
1374 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
1375 * actions should be all removed after flushing.
1376 */
tcf_block_put_ext(struct tcf_block * block,struct Qdisc * q,struct tcf_block_ext_info * ei)1377 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
1378 struct tcf_block_ext_info *ei)
1379 {
1380 if (!block)
1381 return;
1382 tcf_chain0_head_change_cb_del(block, ei);
1383 tcf_block_owner_del(block, q, ei->binder_type);
1384
1385 __tcf_block_put(block, q, ei, true);
1386 }
1387 EXPORT_SYMBOL(tcf_block_put_ext);
1388
tcf_block_put(struct tcf_block * block)1389 void tcf_block_put(struct tcf_block *block)
1390 {
1391 struct tcf_block_ext_info ei = {0, };
1392
1393 if (!block)
1394 return;
1395 tcf_block_put_ext(block, block->q, &ei);
1396 }
1397
1398 EXPORT_SYMBOL(tcf_block_put);
1399
1400 static int
tcf_block_playback_offloads(struct tcf_block * block,flow_setup_cb_t * cb,void * cb_priv,bool add,bool offload_in_use,struct netlink_ext_ack * extack)1401 tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
1402 void *cb_priv, bool add, bool offload_in_use,
1403 struct netlink_ext_ack *extack)
1404 {
1405 struct tcf_chain *chain, *chain_prev;
1406 struct tcf_proto *tp, *tp_prev;
1407 int err;
1408
1409 lockdep_assert_held(&block->cb_lock);
1410
1411 for (chain = __tcf_get_next_chain(block, NULL);
1412 chain;
1413 chain_prev = chain,
1414 chain = __tcf_get_next_chain(block, chain),
1415 tcf_chain_put(chain_prev)) {
1416 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1417 tp_prev = tp,
1418 tp = __tcf_get_next_proto(chain, tp),
1419 tcf_proto_put(tp_prev, true, NULL)) {
1420 if (tp->ops->reoffload) {
1421 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1422 extack);
1423 if (err && add)
1424 goto err_playback_remove;
1425 } else if (add && offload_in_use) {
1426 err = -EOPNOTSUPP;
1427 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1428 goto err_playback_remove;
1429 }
1430 }
1431 }
1432
1433 return 0;
1434
1435 err_playback_remove:
1436 tcf_proto_put(tp, true, NULL);
1437 tcf_chain_put(chain);
1438 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1439 extack);
1440 return err;
1441 }
1442
tcf_block_bind(struct tcf_block * block,struct flow_block_offload * bo)1443 static int tcf_block_bind(struct tcf_block *block,
1444 struct flow_block_offload *bo)
1445 {
1446 struct flow_block_cb *block_cb, *next;
1447 int err, i = 0;
1448
1449 lockdep_assert_held(&block->cb_lock);
1450
1451 list_for_each_entry(block_cb, &bo->cb_list, list) {
1452 err = tcf_block_playback_offloads(block, block_cb->cb,
1453 block_cb->cb_priv, true,
1454 tcf_block_offload_in_use(block),
1455 bo->extack);
1456 if (err)
1457 goto err_unroll;
1458 if (!bo->unlocked_driver_cb)
1459 block->lockeddevcnt++;
1460
1461 i++;
1462 }
1463 list_splice(&bo->cb_list, &block->flow_block.cb_list);
1464
1465 return 0;
1466
1467 err_unroll:
1468 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1469 if (i-- > 0) {
1470 list_del(&block_cb->list);
1471 tcf_block_playback_offloads(block, block_cb->cb,
1472 block_cb->cb_priv, false,
1473 tcf_block_offload_in_use(block),
1474 NULL);
1475 if (!bo->unlocked_driver_cb)
1476 block->lockeddevcnt--;
1477 }
1478 flow_block_cb_free(block_cb);
1479 }
1480
1481 return err;
1482 }
1483
tcf_block_unbind(struct tcf_block * block,struct flow_block_offload * bo)1484 static void tcf_block_unbind(struct tcf_block *block,
1485 struct flow_block_offload *bo)
1486 {
1487 struct flow_block_cb *block_cb, *next;
1488
1489 lockdep_assert_held(&block->cb_lock);
1490
1491 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1492 tcf_block_playback_offloads(block, block_cb->cb,
1493 block_cb->cb_priv, false,
1494 tcf_block_offload_in_use(block),
1495 NULL);
1496 list_del(&block_cb->list);
1497 flow_block_cb_free(block_cb);
1498 if (!bo->unlocked_driver_cb)
1499 block->lockeddevcnt--;
1500 }
1501 }
1502
tcf_block_setup(struct tcf_block * block,struct flow_block_offload * bo)1503 static int tcf_block_setup(struct tcf_block *block,
1504 struct flow_block_offload *bo)
1505 {
1506 int err;
1507
1508 switch (bo->command) {
1509 case FLOW_BLOCK_BIND:
1510 err = tcf_block_bind(block, bo);
1511 break;
1512 case FLOW_BLOCK_UNBIND:
1513 err = 0;
1514 tcf_block_unbind(block, bo);
1515 break;
1516 default:
1517 WARN_ON_ONCE(1);
1518 err = -EOPNOTSUPP;
1519 }
1520
1521 return err;
1522 }
1523
1524 /* Main classifier routine: scans classifier chain attached
1525 * to this qdisc, (optionally) tests for protocol and asks
1526 * specific classifiers.
1527 */
__tcf_classify(struct sk_buff * skb,const struct tcf_proto * tp,const struct tcf_proto * orig_tp,struct tcf_result * res,bool compat_mode,u32 * last_executed_chain)1528 static inline int __tcf_classify(struct sk_buff *skb,
1529 const struct tcf_proto *tp,
1530 const struct tcf_proto *orig_tp,
1531 struct tcf_result *res,
1532 bool compat_mode,
1533 u32 *last_executed_chain)
1534 {
1535 #ifdef CONFIG_NET_CLS_ACT
1536 const int max_reclassify_loop = 16;
1537 const struct tcf_proto *first_tp;
1538 int limit = 0;
1539
1540 reclassify:
1541 #endif
1542 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1543 __be16 protocol = skb_protocol(skb, false);
1544 int err;
1545
1546 if (tp->protocol != protocol &&
1547 tp->protocol != htons(ETH_P_ALL))
1548 continue;
1549
1550 err = tp->classify(skb, tp, res);
1551 #ifdef CONFIG_NET_CLS_ACT
1552 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
1553 first_tp = orig_tp;
1554 *last_executed_chain = first_tp->chain->index;
1555 goto reset;
1556 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
1557 first_tp = res->goto_tp;
1558 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
1559 goto reset;
1560 }
1561 #endif
1562 if (err >= 0)
1563 return err;
1564 }
1565
1566 return TC_ACT_UNSPEC; /* signal: continue lookup */
1567 #ifdef CONFIG_NET_CLS_ACT
1568 reset:
1569 if (unlikely(limit++ >= max_reclassify_loop)) {
1570 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1571 tp->chain->block->index,
1572 tp->prio & 0xffff,
1573 ntohs(tp->protocol));
1574 return TC_ACT_SHOT;
1575 }
1576
1577 tp = first_tp;
1578 goto reclassify;
1579 #endif
1580 }
1581
tcf_classify(struct sk_buff * skb,const struct tcf_proto * tp,struct tcf_result * res,bool compat_mode)1582 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1583 struct tcf_result *res, bool compat_mode)
1584 {
1585 u32 last_executed_chain = 0;
1586
1587 return __tcf_classify(skb, tp, tp, res, compat_mode,
1588 &last_executed_chain);
1589 }
1590 EXPORT_SYMBOL(tcf_classify);
1591
tcf_classify_ingress(struct sk_buff * skb,const struct tcf_block * ingress_block,const struct tcf_proto * tp,struct tcf_result * res,bool compat_mode)1592 int tcf_classify_ingress(struct sk_buff *skb,
1593 const struct tcf_block *ingress_block,
1594 const struct tcf_proto *tp,
1595 struct tcf_result *res, bool compat_mode)
1596 {
1597 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1598 u32 last_executed_chain = 0;
1599
1600 return __tcf_classify(skb, tp, tp, res, compat_mode,
1601 &last_executed_chain);
1602 #else
1603 u32 last_executed_chain = tp ? tp->chain->index : 0;
1604 const struct tcf_proto *orig_tp = tp;
1605 struct tc_skb_ext *ext;
1606 int ret;
1607
1608 ext = skb_ext_find(skb, TC_SKB_EXT);
1609
1610 if (ext && ext->chain) {
1611 struct tcf_chain *fchain;
1612
1613 fchain = tcf_chain_lookup_rcu(ingress_block, ext->chain);
1614 if (!fchain)
1615 return TC_ACT_SHOT;
1616
1617 /* Consume, so cloned/redirect skbs won't inherit ext */
1618 skb_ext_del(skb, TC_SKB_EXT);
1619
1620 tp = rcu_dereference_bh(fchain->filter_chain);
1621 last_executed_chain = fchain->index;
1622 }
1623
1624 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1625 &last_executed_chain);
1626
1627 /* If we missed on some chain */
1628 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1629 ext = tc_skb_ext_alloc(skb);
1630 if (WARN_ON_ONCE(!ext))
1631 return TC_ACT_SHOT;
1632 ext->chain = last_executed_chain;
1633 ext->mru = qdisc_skb_cb(skb)->mru;
1634 }
1635
1636 return ret;
1637 #endif
1638 }
1639 EXPORT_SYMBOL(tcf_classify_ingress);
1640
1641 struct tcf_chain_info {
1642 struct tcf_proto __rcu **pprev;
1643 struct tcf_proto __rcu *next;
1644 };
1645
tcf_chain_tp_prev(struct tcf_chain * chain,struct tcf_chain_info * chain_info)1646 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1647 struct tcf_chain_info *chain_info)
1648 {
1649 return tcf_chain_dereference(*chain_info->pprev, chain);
1650 }
1651
tcf_chain_tp_insert(struct tcf_chain * chain,struct tcf_chain_info * chain_info,struct tcf_proto * tp)1652 static int tcf_chain_tp_insert(struct tcf_chain *chain,
1653 struct tcf_chain_info *chain_info,
1654 struct tcf_proto *tp)
1655 {
1656 if (chain->flushing)
1657 return -EAGAIN;
1658
1659 if (*chain_info->pprev == chain->filter_chain)
1660 tcf_chain0_head_change(chain, tp);
1661 tcf_proto_get(tp);
1662 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
1663 rcu_assign_pointer(*chain_info->pprev, tp);
1664
1665 return 0;
1666 }
1667
tcf_chain_tp_remove(struct tcf_chain * chain,struct tcf_chain_info * chain_info,struct tcf_proto * tp)1668 static void tcf_chain_tp_remove(struct tcf_chain *chain,
1669 struct tcf_chain_info *chain_info,
1670 struct tcf_proto *tp)
1671 {
1672 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
1673
1674 tcf_proto_mark_delete(tp);
1675 if (tp == chain->filter_chain)
1676 tcf_chain0_head_change(chain, next);
1677 RCU_INIT_POINTER(*chain_info->pprev, next);
1678 }
1679
1680 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1681 struct tcf_chain_info *chain_info,
1682 u32 protocol, u32 prio,
1683 bool prio_allocate);
1684
1685 /* Try to insert new proto.
1686 * If proto with specified priority already exists, free new proto
1687 * and return existing one.
1688 */
1689
tcf_chain_tp_insert_unique(struct tcf_chain * chain,struct tcf_proto * tp_new,u32 protocol,u32 prio,bool rtnl_held)1690 static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1691 struct tcf_proto *tp_new,
1692 u32 protocol, u32 prio,
1693 bool rtnl_held)
1694 {
1695 struct tcf_chain_info chain_info;
1696 struct tcf_proto *tp;
1697 int err = 0;
1698
1699 mutex_lock(&chain->filter_chain_lock);
1700
1701 if (tcf_proto_exists_destroying(chain, tp_new)) {
1702 mutex_unlock(&chain->filter_chain_lock);
1703 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1704 return ERR_PTR(-EAGAIN);
1705 }
1706
1707 tp = tcf_chain_tp_find(chain, &chain_info,
1708 protocol, prio, false);
1709 if (!tp)
1710 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
1711 mutex_unlock(&chain->filter_chain_lock);
1712
1713 if (tp) {
1714 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1715 tp_new = tp;
1716 } else if (err) {
1717 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1718 tp_new = ERR_PTR(err);
1719 }
1720
1721 return tp_new;
1722 }
1723
tcf_chain_tp_delete_empty(struct tcf_chain * chain,struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)1724 static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
1725 struct tcf_proto *tp, bool rtnl_held,
1726 struct netlink_ext_ack *extack)
1727 {
1728 struct tcf_chain_info chain_info;
1729 struct tcf_proto *tp_iter;
1730 struct tcf_proto **pprev;
1731 struct tcf_proto *next;
1732
1733 mutex_lock(&chain->filter_chain_lock);
1734
1735 /* Atomically find and remove tp from chain. */
1736 for (pprev = &chain->filter_chain;
1737 (tp_iter = tcf_chain_dereference(*pprev, chain));
1738 pprev = &tp_iter->next) {
1739 if (tp_iter == tp) {
1740 chain_info.pprev = pprev;
1741 chain_info.next = tp_iter->next;
1742 WARN_ON(tp_iter->deleting);
1743 break;
1744 }
1745 }
1746 /* Verify that tp still exists and no new filters were inserted
1747 * concurrently.
1748 * Mark tp for deletion if it is empty.
1749 */
1750 if (!tp_iter || !tcf_proto_check_delete(tp)) {
1751 mutex_unlock(&chain->filter_chain_lock);
1752 return;
1753 }
1754
1755 tcf_proto_signal_destroying(chain, tp);
1756 next = tcf_chain_dereference(chain_info.next, chain);
1757 if (tp == chain->filter_chain)
1758 tcf_chain0_head_change(chain, next);
1759 RCU_INIT_POINTER(*chain_info.pprev, next);
1760 mutex_unlock(&chain->filter_chain_lock);
1761
1762 tcf_proto_put(tp, rtnl_held, extack);
1763 }
1764
tcf_chain_tp_find(struct tcf_chain * chain,struct tcf_chain_info * chain_info,u32 protocol,u32 prio,bool prio_allocate)1765 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1766 struct tcf_chain_info *chain_info,
1767 u32 protocol, u32 prio,
1768 bool prio_allocate)
1769 {
1770 struct tcf_proto **pprev;
1771 struct tcf_proto *tp;
1772
1773 /* Check the chain for existence of proto-tcf with this priority */
1774 for (pprev = &chain->filter_chain;
1775 (tp = tcf_chain_dereference(*pprev, chain));
1776 pprev = &tp->next) {
1777 if (tp->prio >= prio) {
1778 if (tp->prio == prio) {
1779 if (prio_allocate ||
1780 (tp->protocol != protocol && protocol))
1781 return ERR_PTR(-EINVAL);
1782 } else {
1783 tp = NULL;
1784 }
1785 break;
1786 }
1787 }
1788 chain_info->pprev = pprev;
1789 if (tp) {
1790 chain_info->next = tp->next;
1791 tcf_proto_get(tp);
1792 } else {
1793 chain_info->next = NULL;
1794 }
1795 return tp;
1796 }
1797
tcf_fill_node(struct net * net,struct sk_buff * skb,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,u32 portid,u32 seq,u16 flags,int event,bool terse_dump,bool rtnl_held)1798 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
1799 struct tcf_proto *tp, struct tcf_block *block,
1800 struct Qdisc *q, u32 parent, void *fh,
1801 u32 portid, u32 seq, u16 flags, int event,
1802 bool terse_dump, bool rtnl_held)
1803 {
1804 struct tcmsg *tcm;
1805 struct nlmsghdr *nlh;
1806 unsigned char *b = skb_tail_pointer(skb);
1807
1808 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1809 if (!nlh)
1810 goto out_nlmsg_trim;
1811 tcm = nlmsg_data(nlh);
1812 tcm->tcm_family = AF_UNSPEC;
1813 tcm->tcm__pad1 = 0;
1814 tcm->tcm__pad2 = 0;
1815 if (q) {
1816 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1817 tcm->tcm_parent = parent;
1818 } else {
1819 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1820 tcm->tcm_block_index = block->index;
1821 }
1822 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1823 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1824 goto nla_put_failure;
1825 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1826 goto nla_put_failure;
1827 if (!fh) {
1828 tcm->tcm_handle = 0;
1829 } else if (terse_dump) {
1830 if (tp->ops->terse_dump) {
1831 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1832 rtnl_held) < 0)
1833 goto nla_put_failure;
1834 } else {
1835 goto cls_op_not_supp;
1836 }
1837 } else {
1838 if (tp->ops->dump &&
1839 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
1840 goto nla_put_failure;
1841 }
1842 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1843 return skb->len;
1844
1845 out_nlmsg_trim:
1846 nla_put_failure:
1847 cls_op_not_supp:
1848 nlmsg_trim(skb, b);
1849 return -1;
1850 }
1851
tfilter_notify(struct net * net,struct sk_buff * oskb,struct nlmsghdr * n,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,int event,bool unicast,bool rtnl_held)1852 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1853 struct nlmsghdr *n, struct tcf_proto *tp,
1854 struct tcf_block *block, struct Qdisc *q,
1855 u32 parent, void *fh, int event, bool unicast,
1856 bool rtnl_held)
1857 {
1858 struct sk_buff *skb;
1859 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1860 int err = 0;
1861
1862 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1863 if (!skb)
1864 return -ENOBUFS;
1865
1866 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1867 n->nlmsg_seq, n->nlmsg_flags, event,
1868 false, rtnl_held) <= 0) {
1869 kfree_skb(skb);
1870 return -EINVAL;
1871 }
1872
1873 if (unicast)
1874 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1875 else
1876 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1877 n->nlmsg_flags & NLM_F_ECHO);
1878
1879 if (err > 0)
1880 err = 0;
1881 return err;
1882 }
1883
tfilter_del_notify(struct net * net,struct sk_buff * oskb,struct nlmsghdr * n,struct tcf_proto * tp,struct tcf_block * block,struct Qdisc * q,u32 parent,void * fh,bool unicast,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)1884 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1885 struct nlmsghdr *n, struct tcf_proto *tp,
1886 struct tcf_block *block, struct Qdisc *q,
1887 u32 parent, void *fh, bool unicast, bool *last,
1888 bool rtnl_held, struct netlink_ext_ack *extack)
1889 {
1890 struct sk_buff *skb;
1891 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1892 int err;
1893
1894 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1895 if (!skb)
1896 return -ENOBUFS;
1897
1898 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1899 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1900 false, rtnl_held) <= 0) {
1901 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
1902 kfree_skb(skb);
1903 return -EINVAL;
1904 }
1905
1906 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
1907 if (err) {
1908 kfree_skb(skb);
1909 return err;
1910 }
1911
1912 if (unicast)
1913 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1914 else
1915 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1916 n->nlmsg_flags & NLM_F_ECHO);
1917 if (err < 0)
1918 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1919
1920 if (err > 0)
1921 err = 0;
1922 return err;
1923 }
1924
tfilter_notify_chain(struct net * net,struct sk_buff * oskb,struct tcf_block * block,struct Qdisc * q,u32 parent,struct nlmsghdr * n,struct tcf_chain * chain,int event,bool rtnl_held)1925 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
1926 struct tcf_block *block, struct Qdisc *q,
1927 u32 parent, struct nlmsghdr *n,
1928 struct tcf_chain *chain, int event,
1929 bool rtnl_held)
1930 {
1931 struct tcf_proto *tp;
1932
1933 for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1934 tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
1935 tfilter_notify(net, oskb, n, tp, block,
1936 q, parent, NULL, event, false, rtnl_held);
1937 }
1938
tfilter_put(struct tcf_proto * tp,void * fh)1939 static void tfilter_put(struct tcf_proto *tp, void *fh)
1940 {
1941 if (tp->ops->put && fh)
1942 tp->ops->put(tp, fh);
1943 }
1944
tc_new_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)1945 static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1946 struct netlink_ext_ack *extack)
1947 {
1948 struct net *net = sock_net(skb->sk);
1949 struct nlattr *tca[TCA_MAX + 1];
1950 char name[IFNAMSIZ];
1951 struct tcmsg *t;
1952 u32 protocol;
1953 u32 prio;
1954 bool prio_allocate;
1955 u32 parent;
1956 u32 chain_index;
1957 struct Qdisc *q;
1958 struct tcf_chain_info chain_info;
1959 struct tcf_chain *chain;
1960 struct tcf_block *block;
1961 struct tcf_proto *tp;
1962 unsigned long cl;
1963 void *fh;
1964 int err;
1965 int tp_created;
1966 bool rtnl_held = false;
1967
1968 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1969 return -EPERM;
1970
1971 replay:
1972 tp_created = 0;
1973
1974 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1975 rtm_tca_policy, extack);
1976 if (err < 0)
1977 return err;
1978
1979 t = nlmsg_data(n);
1980 protocol = TC_H_MIN(t->tcm_info);
1981 prio = TC_H_MAJ(t->tcm_info);
1982 prio_allocate = false;
1983 parent = t->tcm_parent;
1984 tp = NULL;
1985 cl = 0;
1986 block = NULL;
1987 q = NULL;
1988 chain = NULL;
1989
1990 if (prio == 0) {
1991 /* If no priority is provided by the user,
1992 * we allocate one.
1993 */
1994 if (n->nlmsg_flags & NLM_F_CREATE) {
1995 prio = TC_H_MAKE(0x80000000U, 0U);
1996 prio_allocate = true;
1997 } else {
1998 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1999 return -ENOENT;
2000 }
2001 }
2002
2003 /* Find head of filter chain. */
2004
2005 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2006 if (err)
2007 return err;
2008
2009 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2010 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2011 err = -EINVAL;
2012 goto errout;
2013 }
2014
2015 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2016 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2017 * type is not specified, classifier is not unlocked.
2018 */
2019 if (rtnl_held ||
2020 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2021 !tcf_proto_is_unlocked(name)) {
2022 rtnl_held = true;
2023 rtnl_lock();
2024 }
2025
2026 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2027 if (err)
2028 goto errout;
2029
2030 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2031 extack);
2032 if (IS_ERR(block)) {
2033 err = PTR_ERR(block);
2034 goto errout;
2035 }
2036 block->classid = parent;
2037
2038 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2039 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2040 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2041 err = -EINVAL;
2042 goto errout;
2043 }
2044 chain = tcf_chain_get(block, chain_index, true);
2045 if (!chain) {
2046 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
2047 err = -ENOMEM;
2048 goto errout;
2049 }
2050
2051 mutex_lock(&chain->filter_chain_lock);
2052 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2053 prio, prio_allocate);
2054 if (IS_ERR(tp)) {
2055 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2056 err = PTR_ERR(tp);
2057 goto errout_locked;
2058 }
2059
2060 if (tp == NULL) {
2061 struct tcf_proto *tp_new = NULL;
2062
2063 if (chain->flushing) {
2064 err = -EAGAIN;
2065 goto errout_locked;
2066 }
2067
2068 /* Proto-tcf does not exist, create new one */
2069
2070 if (tca[TCA_KIND] == NULL || !protocol) {
2071 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
2072 err = -EINVAL;
2073 goto errout_locked;
2074 }
2075
2076 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2077 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2078 err = -ENOENT;
2079 goto errout_locked;
2080 }
2081
2082 if (prio_allocate)
2083 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2084 &chain_info));
2085
2086 mutex_unlock(&chain->filter_chain_lock);
2087 tp_new = tcf_proto_create(name, protocol, prio, chain,
2088 rtnl_held, extack);
2089 if (IS_ERR(tp_new)) {
2090 err = PTR_ERR(tp_new);
2091 goto errout_tp;
2092 }
2093
2094 tp_created = 1;
2095 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2096 rtnl_held);
2097 if (IS_ERR(tp)) {
2098 err = PTR_ERR(tp);
2099 goto errout_tp;
2100 }
2101 } else {
2102 mutex_unlock(&chain->filter_chain_lock);
2103 }
2104
2105 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2106 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2107 err = -EINVAL;
2108 goto errout;
2109 }
2110
2111 fh = tp->ops->get(tp, t->tcm_handle);
2112
2113 if (!fh) {
2114 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2115 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
2116 err = -ENOENT;
2117 goto errout;
2118 }
2119 } else if (n->nlmsg_flags & NLM_F_EXCL) {
2120 tfilter_put(tp, fh);
2121 NL_SET_ERR_MSG(extack, "Filter already exists");
2122 err = -EEXIST;
2123 goto errout;
2124 }
2125
2126 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2127 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2128 err = -EINVAL;
2129 goto errout;
2130 }
2131
2132 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
2133 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
2134 rtnl_held, extack);
2135 if (err == 0) {
2136 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2137 RTM_NEWTFILTER, false, rtnl_held);
2138 tfilter_put(tp, fh);
2139 /* q pointer is NULL for shared blocks */
2140 if (q)
2141 q->flags &= ~TCQ_F_CAN_BYPASS;
2142 }
2143
2144 errout:
2145 if (err && tp_created)
2146 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
2147 errout_tp:
2148 if (chain) {
2149 if (tp && !IS_ERR(tp))
2150 tcf_proto_put(tp, rtnl_held, NULL);
2151 if (!tp_created)
2152 tcf_chain_put(chain);
2153 }
2154 tcf_block_release(q, block, rtnl_held);
2155
2156 if (rtnl_held)
2157 rtnl_unlock();
2158
2159 if (err == -EAGAIN) {
2160 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2161 * of target chain.
2162 */
2163 rtnl_held = true;
2164 /* Replay the request. */
2165 goto replay;
2166 }
2167 return err;
2168
2169 errout_locked:
2170 mutex_unlock(&chain->filter_chain_lock);
2171 goto errout;
2172 }
2173
tc_del_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2174 static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2175 struct netlink_ext_ack *extack)
2176 {
2177 struct net *net = sock_net(skb->sk);
2178 struct nlattr *tca[TCA_MAX + 1];
2179 char name[IFNAMSIZ];
2180 struct tcmsg *t;
2181 u32 protocol;
2182 u32 prio;
2183 u32 parent;
2184 u32 chain_index;
2185 struct Qdisc *q = NULL;
2186 struct tcf_chain_info chain_info;
2187 struct tcf_chain *chain = NULL;
2188 struct tcf_block *block = NULL;
2189 struct tcf_proto *tp = NULL;
2190 unsigned long cl = 0;
2191 void *fh = NULL;
2192 int err;
2193 bool rtnl_held = false;
2194
2195 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2196 return -EPERM;
2197
2198 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2199 rtm_tca_policy, extack);
2200 if (err < 0)
2201 return err;
2202
2203 t = nlmsg_data(n);
2204 protocol = TC_H_MIN(t->tcm_info);
2205 prio = TC_H_MAJ(t->tcm_info);
2206 parent = t->tcm_parent;
2207
2208 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2209 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2210 return -ENOENT;
2211 }
2212
2213 /* Find head of filter chain. */
2214
2215 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2216 if (err)
2217 return err;
2218
2219 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2220 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2221 err = -EINVAL;
2222 goto errout;
2223 }
2224 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2225 * found), qdisc is not unlocked, classifier type is not specified,
2226 * classifier is not unlocked.
2227 */
2228 if (!prio ||
2229 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2230 !tcf_proto_is_unlocked(name)) {
2231 rtnl_held = true;
2232 rtnl_lock();
2233 }
2234
2235 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2236 if (err)
2237 goto errout;
2238
2239 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2240 extack);
2241 if (IS_ERR(block)) {
2242 err = PTR_ERR(block);
2243 goto errout;
2244 }
2245
2246 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2247 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2248 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2249 err = -EINVAL;
2250 goto errout;
2251 }
2252 chain = tcf_chain_get(block, chain_index, false);
2253 if (!chain) {
2254 /* User requested flush on non-existent chain. Nothing to do,
2255 * so just return success.
2256 */
2257 if (prio == 0) {
2258 err = 0;
2259 goto errout;
2260 }
2261 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2262 err = -ENOENT;
2263 goto errout;
2264 }
2265
2266 if (prio == 0) {
2267 tfilter_notify_chain(net, skb, block, q, parent, n,
2268 chain, RTM_DELTFILTER, rtnl_held);
2269 tcf_chain_flush(chain, rtnl_held);
2270 err = 0;
2271 goto errout;
2272 }
2273
2274 mutex_lock(&chain->filter_chain_lock);
2275 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2276 prio, false);
2277 if (!tp || IS_ERR(tp)) {
2278 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2279 err = tp ? PTR_ERR(tp) : -ENOENT;
2280 goto errout_locked;
2281 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2282 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2283 err = -EINVAL;
2284 goto errout_locked;
2285 } else if (t->tcm_handle == 0) {
2286 tcf_proto_signal_destroying(chain, tp);
2287 tcf_chain_tp_remove(chain, &chain_info, tp);
2288 mutex_unlock(&chain->filter_chain_lock);
2289
2290 tcf_proto_put(tp, rtnl_held, NULL);
2291 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
2292 RTM_DELTFILTER, false, rtnl_held);
2293 err = 0;
2294 goto errout;
2295 }
2296 mutex_unlock(&chain->filter_chain_lock);
2297
2298 fh = tp->ops->get(tp, t->tcm_handle);
2299
2300 if (!fh) {
2301 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2302 err = -ENOENT;
2303 } else {
2304 bool last;
2305
2306 err = tfilter_del_notify(net, skb, n, tp, block,
2307 q, parent, fh, false, &last,
2308 rtnl_held, extack);
2309
2310 if (err)
2311 goto errout;
2312 if (last)
2313 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
2314 }
2315
2316 errout:
2317 if (chain) {
2318 if (tp && !IS_ERR(tp))
2319 tcf_proto_put(tp, rtnl_held, NULL);
2320 tcf_chain_put(chain);
2321 }
2322 tcf_block_release(q, block, rtnl_held);
2323
2324 if (rtnl_held)
2325 rtnl_unlock();
2326
2327 return err;
2328
2329 errout_locked:
2330 mutex_unlock(&chain->filter_chain_lock);
2331 goto errout;
2332 }
2333
tc_get_tfilter(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2334 static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2335 struct netlink_ext_ack *extack)
2336 {
2337 struct net *net = sock_net(skb->sk);
2338 struct nlattr *tca[TCA_MAX + 1];
2339 char name[IFNAMSIZ];
2340 struct tcmsg *t;
2341 u32 protocol;
2342 u32 prio;
2343 u32 parent;
2344 u32 chain_index;
2345 struct Qdisc *q = NULL;
2346 struct tcf_chain_info chain_info;
2347 struct tcf_chain *chain = NULL;
2348 struct tcf_block *block = NULL;
2349 struct tcf_proto *tp = NULL;
2350 unsigned long cl = 0;
2351 void *fh = NULL;
2352 int err;
2353 bool rtnl_held = false;
2354
2355 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2356 rtm_tca_policy, extack);
2357 if (err < 0)
2358 return err;
2359
2360 t = nlmsg_data(n);
2361 protocol = TC_H_MIN(t->tcm_info);
2362 prio = TC_H_MAJ(t->tcm_info);
2363 parent = t->tcm_parent;
2364
2365 if (prio == 0) {
2366 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2367 return -ENOENT;
2368 }
2369
2370 /* Find head of filter chain. */
2371
2372 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2373 if (err)
2374 return err;
2375
2376 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2377 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2378 err = -EINVAL;
2379 goto errout;
2380 }
2381 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2382 * unlocked, classifier type is not specified, classifier is not
2383 * unlocked.
2384 */
2385 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2386 !tcf_proto_is_unlocked(name)) {
2387 rtnl_held = true;
2388 rtnl_lock();
2389 }
2390
2391 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2392 if (err)
2393 goto errout;
2394
2395 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2396 extack);
2397 if (IS_ERR(block)) {
2398 err = PTR_ERR(block);
2399 goto errout;
2400 }
2401
2402 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2403 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2404 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2405 err = -EINVAL;
2406 goto errout;
2407 }
2408 chain = tcf_chain_get(block, chain_index, false);
2409 if (!chain) {
2410 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2411 err = -EINVAL;
2412 goto errout;
2413 }
2414
2415 mutex_lock(&chain->filter_chain_lock);
2416 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2417 prio, false);
2418 mutex_unlock(&chain->filter_chain_lock);
2419 if (!tp || IS_ERR(tp)) {
2420 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2421 err = tp ? PTR_ERR(tp) : -ENOENT;
2422 goto errout;
2423 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2424 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2425 err = -EINVAL;
2426 goto errout;
2427 }
2428
2429 fh = tp->ops->get(tp, t->tcm_handle);
2430
2431 if (!fh) {
2432 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2433 err = -ENOENT;
2434 } else {
2435 err = tfilter_notify(net, skb, n, tp, block, q, parent,
2436 fh, RTM_NEWTFILTER, true, rtnl_held);
2437 if (err < 0)
2438 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2439 }
2440
2441 tfilter_put(tp, fh);
2442 errout:
2443 if (chain) {
2444 if (tp && !IS_ERR(tp))
2445 tcf_proto_put(tp, rtnl_held, NULL);
2446 tcf_chain_put(chain);
2447 }
2448 tcf_block_release(q, block, rtnl_held);
2449
2450 if (rtnl_held)
2451 rtnl_unlock();
2452
2453 return err;
2454 }
2455
2456 struct tcf_dump_args {
2457 struct tcf_walker w;
2458 struct sk_buff *skb;
2459 struct netlink_callback *cb;
2460 struct tcf_block *block;
2461 struct Qdisc *q;
2462 u32 parent;
2463 bool terse_dump;
2464 };
2465
tcf_node_dump(struct tcf_proto * tp,void * n,struct tcf_walker * arg)2466 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2467 {
2468 struct tcf_dump_args *a = (void *)arg;
2469 struct net *net = sock_net(a->skb->sk);
2470
2471 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
2472 n, NETLINK_CB(a->cb->skb).portid,
2473 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2474 RTM_NEWTFILTER, a->terse_dump, true);
2475 }
2476
tcf_chain_dump(struct tcf_chain * chain,struct Qdisc * q,u32 parent,struct sk_buff * skb,struct netlink_callback * cb,long index_start,long * p_index,bool terse)2477 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2478 struct sk_buff *skb, struct netlink_callback *cb,
2479 long index_start, long *p_index, bool terse)
2480 {
2481 struct net *net = sock_net(skb->sk);
2482 struct tcf_block *block = chain->block;
2483 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2484 struct tcf_proto *tp, *tp_prev;
2485 struct tcf_dump_args arg;
2486
2487 for (tp = __tcf_get_next_proto(chain, NULL);
2488 tp;
2489 tp_prev = tp,
2490 tp = __tcf_get_next_proto(chain, tp),
2491 tcf_proto_put(tp_prev, true, NULL),
2492 (*p_index)++) {
2493 if (*p_index < index_start)
2494 continue;
2495 if (TC_H_MAJ(tcm->tcm_info) &&
2496 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2497 continue;
2498 if (TC_H_MIN(tcm->tcm_info) &&
2499 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2500 continue;
2501 if (*p_index > index_start)
2502 memset(&cb->args[1], 0,
2503 sizeof(cb->args) - sizeof(cb->args[0]));
2504 if (cb->args[1] == 0) {
2505 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
2506 NETLINK_CB(cb->skb).portid,
2507 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2508 RTM_NEWTFILTER, false, true) <= 0)
2509 goto errout;
2510 cb->args[1] = 1;
2511 }
2512 if (!tp->ops->walk)
2513 continue;
2514 arg.w.fn = tcf_node_dump;
2515 arg.skb = skb;
2516 arg.cb = cb;
2517 arg.block = block;
2518 arg.q = q;
2519 arg.parent = parent;
2520 arg.w.stop = 0;
2521 arg.w.skip = cb->args[1] - 1;
2522 arg.w.count = 0;
2523 arg.w.cookie = cb->args[2];
2524 arg.terse_dump = terse;
2525 tp->ops->walk(tp, &arg.w, true);
2526 cb->args[2] = arg.w.cookie;
2527 cb->args[1] = arg.w.count + 1;
2528 if (arg.w.stop)
2529 goto errout;
2530 }
2531 return true;
2532
2533 errout:
2534 tcf_proto_put(tp, true, NULL);
2535 return false;
2536 }
2537
2538 static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2539 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2540 };
2541
2542 /* called with RTNL */
tc_dump_tfilter(struct sk_buff * skb,struct netlink_callback * cb)2543 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2544 {
2545 struct tcf_chain *chain, *chain_prev;
2546 struct net *net = sock_net(skb->sk);
2547 struct nlattr *tca[TCA_MAX + 1];
2548 struct Qdisc *q = NULL;
2549 struct tcf_block *block;
2550 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2551 bool terse_dump = false;
2552 long index_start;
2553 long index;
2554 u32 parent;
2555 int err;
2556
2557 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2558 return skb->len;
2559
2560 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2561 tcf_tfilter_dump_policy, cb->extack);
2562 if (err)
2563 return err;
2564
2565 if (tca[TCA_DUMP_FLAGS]) {
2566 struct nla_bitfield32 flags =
2567 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2568
2569 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2570 }
2571
2572 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2573 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2574 if (!block)
2575 goto out;
2576 /* If we work with block index, q is NULL and parent value
2577 * will never be used in the following code. The check
2578 * in tcf_fill_node prevents it. However, compiler does not
2579 * see that far, so set parent to zero to silence the warning
2580 * about parent being uninitialized.
2581 */
2582 parent = 0;
2583 } else {
2584 const struct Qdisc_class_ops *cops;
2585 struct net_device *dev;
2586 unsigned long cl = 0;
2587
2588 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2589 if (!dev)
2590 return skb->len;
2591
2592 parent = tcm->tcm_parent;
2593 if (!parent)
2594 q = dev->qdisc;
2595 else
2596 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2597 if (!q)
2598 goto out;
2599 cops = q->ops->cl_ops;
2600 if (!cops)
2601 goto out;
2602 if (!cops->tcf_block)
2603 goto out;
2604 if (TC_H_MIN(tcm->tcm_parent)) {
2605 cl = cops->find(q, tcm->tcm_parent);
2606 if (cl == 0)
2607 goto out;
2608 }
2609 block = cops->tcf_block(q, cl, NULL);
2610 if (!block)
2611 goto out;
2612 parent = block->classid;
2613 if (tcf_block_shared(block))
2614 q = NULL;
2615 }
2616
2617 index_start = cb->args[0];
2618 index = 0;
2619
2620 for (chain = __tcf_get_next_chain(block, NULL);
2621 chain;
2622 chain_prev = chain,
2623 chain = __tcf_get_next_chain(block, chain),
2624 tcf_chain_put(chain_prev)) {
2625 if (tca[TCA_CHAIN] &&
2626 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2627 continue;
2628 if (!tcf_chain_dump(chain, q, parent, skb, cb,
2629 index_start, &index, terse_dump)) {
2630 tcf_chain_put(chain);
2631 err = -EMSGSIZE;
2632 break;
2633 }
2634 }
2635
2636 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2637 tcf_block_refcnt_put(block, true);
2638 cb->args[0] = index;
2639
2640 out:
2641 /* If we did no progress, the error (EMSGSIZE) is real */
2642 if (skb->len == 0 && err)
2643 return err;
2644 return skb->len;
2645 }
2646
tc_chain_fill_node(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv,u32 chain_index,struct net * net,struct sk_buff * skb,struct tcf_block * block,u32 portid,u32 seq,u16 flags,int event)2647 static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2648 void *tmplt_priv, u32 chain_index,
2649 struct net *net, struct sk_buff *skb,
2650 struct tcf_block *block,
2651 u32 portid, u32 seq, u16 flags, int event)
2652 {
2653 unsigned char *b = skb_tail_pointer(skb);
2654 const struct tcf_proto_ops *ops;
2655 struct nlmsghdr *nlh;
2656 struct tcmsg *tcm;
2657 void *priv;
2658
2659 ops = tmplt_ops;
2660 priv = tmplt_priv;
2661
2662 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2663 if (!nlh)
2664 goto out_nlmsg_trim;
2665 tcm = nlmsg_data(nlh);
2666 tcm->tcm_family = AF_UNSPEC;
2667 tcm->tcm__pad1 = 0;
2668 tcm->tcm__pad2 = 0;
2669 tcm->tcm_handle = 0;
2670 if (block->q) {
2671 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2672 tcm->tcm_parent = block->q->handle;
2673 } else {
2674 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2675 tcm->tcm_block_index = block->index;
2676 }
2677
2678 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
2679 goto nla_put_failure;
2680
2681 if (ops) {
2682 if (nla_put_string(skb, TCA_KIND, ops->kind))
2683 goto nla_put_failure;
2684 if (ops->tmplt_dump(skb, net, priv) < 0)
2685 goto nla_put_failure;
2686 }
2687
2688 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2689 return skb->len;
2690
2691 out_nlmsg_trim:
2692 nla_put_failure:
2693 nlmsg_trim(skb, b);
2694 return -EMSGSIZE;
2695 }
2696
tc_chain_notify(struct tcf_chain * chain,struct sk_buff * oskb,u32 seq,u16 flags,int event,bool unicast)2697 static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2698 u32 seq, u16 flags, int event, bool unicast)
2699 {
2700 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2701 struct tcf_block *block = chain->block;
2702 struct net *net = block->net;
2703 struct sk_buff *skb;
2704 int err = 0;
2705
2706 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2707 if (!skb)
2708 return -ENOBUFS;
2709
2710 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2711 chain->index, net, skb, block, portid,
2712 seq, flags, event) <= 0) {
2713 kfree_skb(skb);
2714 return -EINVAL;
2715 }
2716
2717 if (unicast)
2718 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2719 else
2720 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2721 flags & NLM_F_ECHO);
2722
2723 if (err > 0)
2724 err = 0;
2725 return err;
2726 }
2727
tc_chain_notify_delete(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv,u32 chain_index,struct tcf_block * block,struct sk_buff * oskb,u32 seq,u16 flags,bool unicast)2728 static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2729 void *tmplt_priv, u32 chain_index,
2730 struct tcf_block *block, struct sk_buff *oskb,
2731 u32 seq, u16 flags, bool unicast)
2732 {
2733 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2734 struct net *net = block->net;
2735 struct sk_buff *skb;
2736
2737 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2738 if (!skb)
2739 return -ENOBUFS;
2740
2741 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2742 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2743 kfree_skb(skb);
2744 return -EINVAL;
2745 }
2746
2747 if (unicast)
2748 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2749
2750 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2751 }
2752
tc_chain_tmplt_add(struct tcf_chain * chain,struct net * net,struct nlattr ** tca,struct netlink_ext_ack * extack)2753 static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2754 struct nlattr **tca,
2755 struct netlink_ext_ack *extack)
2756 {
2757 const struct tcf_proto_ops *ops;
2758 char name[IFNAMSIZ];
2759 void *tmplt_priv;
2760
2761 /* If kind is not set, user did not specify template. */
2762 if (!tca[TCA_KIND])
2763 return 0;
2764
2765 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2766 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2767 return -EINVAL;
2768 }
2769
2770 ops = tcf_proto_lookup_ops(name, true, extack);
2771 if (IS_ERR(ops))
2772 return PTR_ERR(ops);
2773 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2774 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2775 return -EOPNOTSUPP;
2776 }
2777
2778 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2779 if (IS_ERR(tmplt_priv)) {
2780 module_put(ops->owner);
2781 return PTR_ERR(tmplt_priv);
2782 }
2783 chain->tmplt_ops = ops;
2784 chain->tmplt_priv = tmplt_priv;
2785 return 0;
2786 }
2787
tc_chain_tmplt_del(const struct tcf_proto_ops * tmplt_ops,void * tmplt_priv)2788 static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2789 void *tmplt_priv)
2790 {
2791 /* If template ops are set, no work to do for us. */
2792 if (!tmplt_ops)
2793 return;
2794
2795 tmplt_ops->tmplt_destroy(tmplt_priv);
2796 module_put(tmplt_ops->owner);
2797 }
2798
2799 /* Add/delete/get a chain */
2800
tc_ctl_chain(struct sk_buff * skb,struct nlmsghdr * n,struct netlink_ext_ack * extack)2801 static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2802 struct netlink_ext_ack *extack)
2803 {
2804 struct net *net = sock_net(skb->sk);
2805 struct nlattr *tca[TCA_MAX + 1];
2806 struct tcmsg *t;
2807 u32 parent;
2808 u32 chain_index;
2809 struct Qdisc *q;
2810 struct tcf_chain *chain;
2811 struct tcf_block *block;
2812 unsigned long cl;
2813 int err;
2814
2815 if (n->nlmsg_type != RTM_GETCHAIN &&
2816 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2817 return -EPERM;
2818
2819 replay:
2820 q = NULL;
2821 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2822 rtm_tca_policy, extack);
2823 if (err < 0)
2824 return err;
2825
2826 t = nlmsg_data(n);
2827 parent = t->tcm_parent;
2828 cl = 0;
2829
2830 block = tcf_block_find(net, &q, &parent, &cl,
2831 t->tcm_ifindex, t->tcm_block_index, extack);
2832 if (IS_ERR(block))
2833 return PTR_ERR(block);
2834
2835 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2836 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2837 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2838 err = -EINVAL;
2839 goto errout_block;
2840 }
2841
2842 mutex_lock(&block->lock);
2843 chain = tcf_chain_lookup(block, chain_index);
2844 if (n->nlmsg_type == RTM_NEWCHAIN) {
2845 if (chain) {
2846 if (tcf_chain_held_by_acts_only(chain)) {
2847 /* The chain exists only because there is
2848 * some action referencing it.
2849 */
2850 tcf_chain_hold(chain);
2851 } else {
2852 NL_SET_ERR_MSG(extack, "Filter chain already exists");
2853 err = -EEXIST;
2854 goto errout_block_locked;
2855 }
2856 } else {
2857 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2858 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
2859 err = -ENOENT;
2860 goto errout_block_locked;
2861 }
2862 chain = tcf_chain_create(block, chain_index);
2863 if (!chain) {
2864 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
2865 err = -ENOMEM;
2866 goto errout_block_locked;
2867 }
2868 }
2869 } else {
2870 if (!chain || tcf_chain_held_by_acts_only(chain)) {
2871 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2872 err = -EINVAL;
2873 goto errout_block_locked;
2874 }
2875 tcf_chain_hold(chain);
2876 }
2877
2878 if (n->nlmsg_type == RTM_NEWCHAIN) {
2879 /* Modifying chain requires holding parent block lock. In case
2880 * the chain was successfully added, take a reference to the
2881 * chain. This ensures that an empty chain does not disappear at
2882 * the end of this function.
2883 */
2884 tcf_chain_hold(chain);
2885 chain->explicitly_created = true;
2886 }
2887 mutex_unlock(&block->lock);
2888
2889 switch (n->nlmsg_type) {
2890 case RTM_NEWCHAIN:
2891 err = tc_chain_tmplt_add(chain, net, tca, extack);
2892 if (err) {
2893 tcf_chain_put_explicitly_created(chain);
2894 goto errout;
2895 }
2896
2897 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2898 RTM_NEWCHAIN, false);
2899 break;
2900 case RTM_DELCHAIN:
2901 tfilter_notify_chain(net, skb, block, q, parent, n,
2902 chain, RTM_DELTFILTER, true);
2903 /* Flush the chain first as the user requested chain removal. */
2904 tcf_chain_flush(chain, true);
2905 /* In case the chain was successfully deleted, put a reference
2906 * to the chain previously taken during addition.
2907 */
2908 tcf_chain_put_explicitly_created(chain);
2909 break;
2910 case RTM_GETCHAIN:
2911 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2912 n->nlmsg_flags, n->nlmsg_type, true);
2913 if (err < 0)
2914 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2915 break;
2916 default:
2917 err = -EOPNOTSUPP;
2918 NL_SET_ERR_MSG(extack, "Unsupported message type");
2919 goto errout;
2920 }
2921
2922 errout:
2923 tcf_chain_put(chain);
2924 errout_block:
2925 tcf_block_release(q, block, true);
2926 if (err == -EAGAIN)
2927 /* Replay the request. */
2928 goto replay;
2929 return err;
2930
2931 errout_block_locked:
2932 mutex_unlock(&block->lock);
2933 goto errout_block;
2934 }
2935
2936 /* called with RTNL */
tc_dump_chain(struct sk_buff * skb,struct netlink_callback * cb)2937 static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2938 {
2939 struct net *net = sock_net(skb->sk);
2940 struct nlattr *tca[TCA_MAX + 1];
2941 struct Qdisc *q = NULL;
2942 struct tcf_block *block;
2943 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2944 struct tcf_chain *chain;
2945 long index_start;
2946 long index;
2947 u32 parent;
2948 int err;
2949
2950 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2951 return skb->len;
2952
2953 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2954 rtm_tca_policy, cb->extack);
2955 if (err)
2956 return err;
2957
2958 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2959 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
2960 if (!block)
2961 goto out;
2962 /* If we work with block index, q is NULL and parent value
2963 * will never be used in the following code. The check
2964 * in tcf_fill_node prevents it. However, compiler does not
2965 * see that far, so set parent to zero to silence the warning
2966 * about parent being uninitialized.
2967 */
2968 parent = 0;
2969 } else {
2970 const struct Qdisc_class_ops *cops;
2971 struct net_device *dev;
2972 unsigned long cl = 0;
2973
2974 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2975 if (!dev)
2976 return skb->len;
2977
2978 parent = tcm->tcm_parent;
2979 if (!parent) {
2980 q = dev->qdisc;
2981 parent = q->handle;
2982 } else {
2983 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2984 }
2985 if (!q)
2986 goto out;
2987 cops = q->ops->cl_ops;
2988 if (!cops)
2989 goto out;
2990 if (!cops->tcf_block)
2991 goto out;
2992 if (TC_H_MIN(tcm->tcm_parent)) {
2993 cl = cops->find(q, tcm->tcm_parent);
2994 if (cl == 0)
2995 goto out;
2996 }
2997 block = cops->tcf_block(q, cl, NULL);
2998 if (!block)
2999 goto out;
3000 if (tcf_block_shared(block))
3001 q = NULL;
3002 }
3003
3004 index_start = cb->args[0];
3005 index = 0;
3006
3007 mutex_lock(&block->lock);
3008 list_for_each_entry(chain, &block->chain_list, list) {
3009 if ((tca[TCA_CHAIN] &&
3010 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3011 continue;
3012 if (index < index_start) {
3013 index++;
3014 continue;
3015 }
3016 if (tcf_chain_held_by_acts_only(chain))
3017 continue;
3018 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3019 chain->index, net, skb, block,
3020 NETLINK_CB(cb->skb).portid,
3021 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3022 RTM_NEWCHAIN);
3023 if (err <= 0)
3024 break;
3025 index++;
3026 }
3027 mutex_unlock(&block->lock);
3028
3029 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
3030 tcf_block_refcnt_put(block, true);
3031 cb->args[0] = index;
3032
3033 out:
3034 /* If we did no progress, the error (EMSGSIZE) is real */
3035 if (skb->len == 0 && err)
3036 return err;
3037 return skb->len;
3038 }
3039
tcf_exts_destroy(struct tcf_exts * exts)3040 void tcf_exts_destroy(struct tcf_exts *exts)
3041 {
3042 #ifdef CONFIG_NET_CLS_ACT
3043 if (exts->actions) {
3044 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3045 kfree(exts->actions);
3046 }
3047 exts->nr_actions = 0;
3048 #endif
3049 }
3050 EXPORT_SYMBOL(tcf_exts_destroy);
3051
tcf_exts_validate(struct net * net,struct tcf_proto * tp,struct nlattr ** tb,struct nlattr * rate_tlv,struct tcf_exts * exts,bool ovr,bool rtnl_held,struct netlink_ext_ack * extack)3052 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
3053 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
3054 bool rtnl_held, struct netlink_ext_ack *extack)
3055 {
3056 #ifdef CONFIG_NET_CLS_ACT
3057 {
3058 int init_res[TCA_ACT_MAX_PRIO] = {};
3059 struct tc_action *act;
3060 size_t attr_size = 0;
3061
3062 if (exts->police && tb[exts->police]) {
3063 struct tc_action_ops *a_o;
3064
3065 a_o = tc_action_load_ops("police", tb[exts->police], rtnl_held, extack);
3066 if (IS_ERR(a_o))
3067 return PTR_ERR(a_o);
3068 act = tcf_action_init_1(net, tp, tb[exts->police],
3069 rate_tlv, "police", ovr,
3070 TCA_ACT_BIND, a_o, init_res,
3071 rtnl_held, extack);
3072 module_put(a_o->owner);
3073 if (IS_ERR(act))
3074 return PTR_ERR(act);
3075
3076 act->type = exts->type = TCA_OLD_COMPAT;
3077 exts->actions[0] = act;
3078 exts->nr_actions = 1;
3079 tcf_idr_insert_many(exts->actions);
3080 } else if (exts->action && tb[exts->action]) {
3081 int err;
3082
3083 err = tcf_action_init(net, tp, tb[exts->action],
3084 rate_tlv, NULL, ovr, TCA_ACT_BIND,
3085 exts->actions, init_res,
3086 &attr_size, rtnl_held, extack);
3087 if (err < 0)
3088 return err;
3089 exts->nr_actions = err;
3090 }
3091 }
3092 #else
3093 if ((exts->action && tb[exts->action]) ||
3094 (exts->police && tb[exts->police])) {
3095 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
3096 return -EOPNOTSUPP;
3097 }
3098 #endif
3099
3100 return 0;
3101 }
3102 EXPORT_SYMBOL(tcf_exts_validate);
3103
tcf_exts_change(struct tcf_exts * dst,struct tcf_exts * src)3104 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
3105 {
3106 #ifdef CONFIG_NET_CLS_ACT
3107 struct tcf_exts old = *dst;
3108
3109 *dst = *src;
3110 tcf_exts_destroy(&old);
3111 #endif
3112 }
3113 EXPORT_SYMBOL(tcf_exts_change);
3114
3115 #ifdef CONFIG_NET_CLS_ACT
tcf_exts_first_act(struct tcf_exts * exts)3116 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3117 {
3118 if (exts->nr_actions == 0)
3119 return NULL;
3120 else
3121 return exts->actions[0];
3122 }
3123 #endif
3124
tcf_exts_dump(struct sk_buff * skb,struct tcf_exts * exts)3125 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
3126 {
3127 #ifdef CONFIG_NET_CLS_ACT
3128 struct nlattr *nest;
3129
3130 if (exts->action && tcf_exts_has_actions(exts)) {
3131 /*
3132 * again for backward compatible mode - we want
3133 * to work with both old and new modes of entering
3134 * tc data even if iproute2 was newer - jhs
3135 */
3136 if (exts->type != TCA_OLD_COMPAT) {
3137 nest = nla_nest_start_noflag(skb, exts->action);
3138 if (nest == NULL)
3139 goto nla_put_failure;
3140
3141 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3142 < 0)
3143 goto nla_put_failure;
3144 nla_nest_end(skb, nest);
3145 } else if (exts->police) {
3146 struct tc_action *act = tcf_exts_first_act(exts);
3147 nest = nla_nest_start_noflag(skb, exts->police);
3148 if (nest == NULL || !act)
3149 goto nla_put_failure;
3150 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
3151 goto nla_put_failure;
3152 nla_nest_end(skb, nest);
3153 }
3154 }
3155 return 0;
3156
3157 nla_put_failure:
3158 nla_nest_cancel(skb, nest);
3159 return -1;
3160 #else
3161 return 0;
3162 #endif
3163 }
3164 EXPORT_SYMBOL(tcf_exts_dump);
3165
tcf_exts_terse_dump(struct sk_buff * skb,struct tcf_exts * exts)3166 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3167 {
3168 #ifdef CONFIG_NET_CLS_ACT
3169 struct nlattr *nest;
3170
3171 if (!exts->action || !tcf_exts_has_actions(exts))
3172 return 0;
3173
3174 nest = nla_nest_start_noflag(skb, exts->action);
3175 if (!nest)
3176 goto nla_put_failure;
3177
3178 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3179 goto nla_put_failure;
3180 nla_nest_end(skb, nest);
3181 return 0;
3182
3183 nla_put_failure:
3184 nla_nest_cancel(skb, nest);
3185 return -1;
3186 #else
3187 return 0;
3188 #endif
3189 }
3190 EXPORT_SYMBOL(tcf_exts_terse_dump);
3191
tcf_exts_dump_stats(struct sk_buff * skb,struct tcf_exts * exts)3192 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
3193 {
3194 #ifdef CONFIG_NET_CLS_ACT
3195 struct tc_action *a = tcf_exts_first_act(exts);
3196 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
3197 return -1;
3198 #endif
3199 return 0;
3200 }
3201 EXPORT_SYMBOL(tcf_exts_dump_stats);
3202
tcf_block_offload_inc(struct tcf_block * block,u32 * flags)3203 static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3204 {
3205 if (*flags & TCA_CLS_FLAGS_IN_HW)
3206 return;
3207 *flags |= TCA_CLS_FLAGS_IN_HW;
3208 atomic_inc(&block->offloadcnt);
3209 }
3210
tcf_block_offload_dec(struct tcf_block * block,u32 * flags)3211 static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3212 {
3213 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3214 return;
3215 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3216 atomic_dec(&block->offloadcnt);
3217 }
3218
tc_cls_offload_cnt_update(struct tcf_block * block,struct tcf_proto * tp,u32 * cnt,u32 * flags,u32 diff,bool add)3219 static void tc_cls_offload_cnt_update(struct tcf_block *block,
3220 struct tcf_proto *tp, u32 *cnt,
3221 u32 *flags, u32 diff, bool add)
3222 {
3223 lockdep_assert_held(&block->cb_lock);
3224
3225 spin_lock(&tp->lock);
3226 if (add) {
3227 if (!*cnt)
3228 tcf_block_offload_inc(block, flags);
3229 *cnt += diff;
3230 } else {
3231 *cnt -= diff;
3232 if (!*cnt)
3233 tcf_block_offload_dec(block, flags);
3234 }
3235 spin_unlock(&tp->lock);
3236 }
3237
3238 static void
tc_cls_offload_cnt_reset(struct tcf_block * block,struct tcf_proto * tp,u32 * cnt,u32 * flags)3239 tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3240 u32 *cnt, u32 *flags)
3241 {
3242 lockdep_assert_held(&block->cb_lock);
3243
3244 spin_lock(&tp->lock);
3245 tcf_block_offload_dec(block, flags);
3246 *cnt = 0;
3247 spin_unlock(&tp->lock);
3248 }
3249
3250 static int
__tc_setup_cb_call(struct tcf_block * block,enum tc_setup_type type,void * type_data,bool err_stop)3251 __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3252 void *type_data, bool err_stop)
3253 {
3254 struct flow_block_cb *block_cb;
3255 int ok_count = 0;
3256 int err;
3257
3258 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3259 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3260 if (err) {
3261 if (err_stop)
3262 return err;
3263 } else {
3264 ok_count++;
3265 }
3266 }
3267 return ok_count;
3268 }
3269
tc_setup_cb_call(struct tcf_block * block,enum tc_setup_type type,void * type_data,bool err_stop,bool rtnl_held)3270 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3271 void *type_data, bool err_stop, bool rtnl_held)
3272 {
3273 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3274 int ok_count;
3275
3276 retry:
3277 if (take_rtnl)
3278 rtnl_lock();
3279 down_read(&block->cb_lock);
3280 /* Need to obtain rtnl lock if block is bound to devs that require it.
3281 * In block bind code cb_lock is obtained while holding rtnl, so we must
3282 * obtain the locks in same order here.
3283 */
3284 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3285 up_read(&block->cb_lock);
3286 take_rtnl = true;
3287 goto retry;
3288 }
3289
3290 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3291
3292 up_read(&block->cb_lock);
3293 if (take_rtnl)
3294 rtnl_unlock();
3295 return ok_count;
3296 }
3297 EXPORT_SYMBOL(tc_setup_cb_call);
3298
3299 /* Non-destructive filter add. If filter that wasn't already in hardware is
3300 * successfully offloaded, increment block offloads counter. On failure,
3301 * previously offloaded filter is considered to be intact and offloads counter
3302 * is not decremented.
3303 */
3304
tc_setup_cb_add(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * flags,unsigned int * in_hw_count,bool rtnl_held)3305 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3306 enum tc_setup_type type, void *type_data, bool err_stop,
3307 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3308 {
3309 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3310 int ok_count;
3311
3312 retry:
3313 if (take_rtnl)
3314 rtnl_lock();
3315 down_read(&block->cb_lock);
3316 /* Need to obtain rtnl lock if block is bound to devs that require it.
3317 * In block bind code cb_lock is obtained while holding rtnl, so we must
3318 * obtain the locks in same order here.
3319 */
3320 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3321 up_read(&block->cb_lock);
3322 take_rtnl = true;
3323 goto retry;
3324 }
3325
3326 /* Make sure all netdevs sharing this block are offload-capable. */
3327 if (block->nooffloaddevcnt && err_stop) {
3328 ok_count = -EOPNOTSUPP;
3329 goto err_unlock;
3330 }
3331
3332 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3333 if (ok_count < 0)
3334 goto err_unlock;
3335
3336 if (tp->ops->hw_add)
3337 tp->ops->hw_add(tp, type_data);
3338 if (ok_count > 0)
3339 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3340 ok_count, true);
3341 err_unlock:
3342 up_read(&block->cb_lock);
3343 if (take_rtnl)
3344 rtnl_unlock();
3345 return ok_count < 0 ? ok_count : 0;
3346 }
3347 EXPORT_SYMBOL(tc_setup_cb_add);
3348
3349 /* Destructive filter replace. If filter that wasn't already in hardware is
3350 * successfully offloaded, increment block offload counter. On failure,
3351 * previously offloaded filter is considered to be destroyed and offload counter
3352 * is decremented.
3353 */
3354
tc_setup_cb_replace(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * old_flags,unsigned int * old_in_hw_count,u32 * new_flags,unsigned int * new_in_hw_count,bool rtnl_held)3355 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3356 enum tc_setup_type type, void *type_data, bool err_stop,
3357 u32 *old_flags, unsigned int *old_in_hw_count,
3358 u32 *new_flags, unsigned int *new_in_hw_count,
3359 bool rtnl_held)
3360 {
3361 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3362 int ok_count;
3363
3364 retry:
3365 if (take_rtnl)
3366 rtnl_lock();
3367 down_read(&block->cb_lock);
3368 /* Need to obtain rtnl lock if block is bound to devs that require it.
3369 * In block bind code cb_lock is obtained while holding rtnl, so we must
3370 * obtain the locks in same order here.
3371 */
3372 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3373 up_read(&block->cb_lock);
3374 take_rtnl = true;
3375 goto retry;
3376 }
3377
3378 /* Make sure all netdevs sharing this block are offload-capable. */
3379 if (block->nooffloaddevcnt && err_stop) {
3380 ok_count = -EOPNOTSUPP;
3381 goto err_unlock;
3382 }
3383
3384 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
3385 if (tp->ops->hw_del)
3386 tp->ops->hw_del(tp, type_data);
3387
3388 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3389 if (ok_count < 0)
3390 goto err_unlock;
3391
3392 if (tp->ops->hw_add)
3393 tp->ops->hw_add(tp, type_data);
3394 if (ok_count > 0)
3395 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3396 new_flags, ok_count, true);
3397 err_unlock:
3398 up_read(&block->cb_lock);
3399 if (take_rtnl)
3400 rtnl_unlock();
3401 return ok_count < 0 ? ok_count : 0;
3402 }
3403 EXPORT_SYMBOL(tc_setup_cb_replace);
3404
3405 /* Destroy filter and decrement block offload counter, if filter was previously
3406 * offloaded.
3407 */
3408
tc_setup_cb_destroy(struct tcf_block * block,struct tcf_proto * tp,enum tc_setup_type type,void * type_data,bool err_stop,u32 * flags,unsigned int * in_hw_count,bool rtnl_held)3409 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3410 enum tc_setup_type type, void *type_data, bool err_stop,
3411 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3412 {
3413 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
3414 int ok_count;
3415
3416 retry:
3417 if (take_rtnl)
3418 rtnl_lock();
3419 down_read(&block->cb_lock);
3420 /* Need to obtain rtnl lock if block is bound to devs that require it.
3421 * In block bind code cb_lock is obtained while holding rtnl, so we must
3422 * obtain the locks in same order here.
3423 */
3424 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3425 up_read(&block->cb_lock);
3426 take_rtnl = true;
3427 goto retry;
3428 }
3429
3430 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3431
3432 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
3433 if (tp->ops->hw_del)
3434 tp->ops->hw_del(tp, type_data);
3435
3436 up_read(&block->cb_lock);
3437 if (take_rtnl)
3438 rtnl_unlock();
3439 return ok_count < 0 ? ok_count : 0;
3440 }
3441 EXPORT_SYMBOL(tc_setup_cb_destroy);
3442
tc_setup_cb_reoffload(struct tcf_block * block,struct tcf_proto * tp,bool add,flow_setup_cb_t * cb,enum tc_setup_type type,void * type_data,void * cb_priv,u32 * flags,unsigned int * in_hw_count)3443 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3444 bool add, flow_setup_cb_t *cb,
3445 enum tc_setup_type type, void *type_data,
3446 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3447 {
3448 int err = cb(type, type_data, cb_priv);
3449
3450 if (err) {
3451 if (add && tc_skip_sw(*flags))
3452 return err;
3453 } else {
3454 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3455 add);
3456 }
3457
3458 return 0;
3459 }
3460 EXPORT_SYMBOL(tc_setup_cb_reoffload);
3461
tcf_act_get_cookie(struct flow_action_entry * entry,const struct tc_action * act)3462 static int tcf_act_get_cookie(struct flow_action_entry *entry,
3463 const struct tc_action *act)
3464 {
3465 struct tc_cookie *cookie;
3466 int err = 0;
3467
3468 rcu_read_lock();
3469 cookie = rcu_dereference(act->act_cookie);
3470 if (cookie) {
3471 entry->cookie = flow_action_cookie_create(cookie->data,
3472 cookie->len,
3473 GFP_ATOMIC);
3474 if (!entry->cookie)
3475 err = -ENOMEM;
3476 }
3477 rcu_read_unlock();
3478 return err;
3479 }
3480
tcf_act_put_cookie(struct flow_action_entry * entry)3481 static void tcf_act_put_cookie(struct flow_action_entry *entry)
3482 {
3483 flow_action_cookie_destroy(entry->cookie);
3484 }
3485
tc_cleanup_flow_action(struct flow_action * flow_action)3486 void tc_cleanup_flow_action(struct flow_action *flow_action)
3487 {
3488 struct flow_action_entry *entry;
3489 int i;
3490
3491 flow_action_for_each(i, entry, flow_action) {
3492 tcf_act_put_cookie(entry);
3493 if (entry->destructor)
3494 entry->destructor(entry->destructor_priv);
3495 }
3496 }
3497 EXPORT_SYMBOL(tc_cleanup_flow_action);
3498
tcf_mirred_get_dev(struct flow_action_entry * entry,const struct tc_action * act)3499 static void tcf_mirred_get_dev(struct flow_action_entry *entry,
3500 const struct tc_action *act)
3501 {
3502 #ifdef CONFIG_NET_CLS_ACT
3503 entry->dev = act->ops->get_dev(act, &entry->destructor);
3504 if (!entry->dev)
3505 return;
3506 entry->destructor_priv = entry->dev;
3507 #endif
3508 }
3509
tcf_tunnel_encap_put_tunnel(void * priv)3510 static void tcf_tunnel_encap_put_tunnel(void *priv)
3511 {
3512 struct ip_tunnel_info *tunnel = priv;
3513
3514 kfree(tunnel);
3515 }
3516
tcf_tunnel_encap_get_tunnel(struct flow_action_entry * entry,const struct tc_action * act)3517 static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
3518 const struct tc_action *act)
3519 {
3520 entry->tunnel = tcf_tunnel_info_copy(act);
3521 if (!entry->tunnel)
3522 return -ENOMEM;
3523 entry->destructor = tcf_tunnel_encap_put_tunnel;
3524 entry->destructor_priv = entry->tunnel;
3525 return 0;
3526 }
3527
tcf_sample_get_group(struct flow_action_entry * entry,const struct tc_action * act)3528 static void tcf_sample_get_group(struct flow_action_entry *entry,
3529 const struct tc_action *act)
3530 {
3531 #ifdef CONFIG_NET_CLS_ACT
3532 entry->sample.psample_group =
3533 act->ops->get_psample_group(act, &entry->destructor);
3534 entry->destructor_priv = entry->sample.psample_group;
3535 #endif
3536 }
3537
tcf_gate_entry_destructor(void * priv)3538 static void tcf_gate_entry_destructor(void *priv)
3539 {
3540 struct action_gate_entry *oe = priv;
3541
3542 kfree(oe);
3543 }
3544
tcf_gate_get_entries(struct flow_action_entry * entry,const struct tc_action * act)3545 static int tcf_gate_get_entries(struct flow_action_entry *entry,
3546 const struct tc_action *act)
3547 {
3548 entry->gate.entries = tcf_gate_get_list(act);
3549
3550 if (!entry->gate.entries)
3551 return -EINVAL;
3552
3553 entry->destructor = tcf_gate_entry_destructor;
3554 entry->destructor_priv = entry->gate.entries;
3555
3556 return 0;
3557 }
3558
tc_act_hw_stats(u8 hw_stats)3559 static enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
3560 {
3561 if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
3562 return FLOW_ACTION_HW_STATS_DONT_CARE;
3563 else if (!hw_stats)
3564 return FLOW_ACTION_HW_STATS_DISABLED;
3565
3566 return hw_stats;
3567 }
3568
tc_setup_flow_action(struct flow_action * flow_action,const struct tcf_exts * exts)3569 int tc_setup_flow_action(struct flow_action *flow_action,
3570 const struct tcf_exts *exts)
3571 {
3572 struct tc_action *act;
3573 int i, j, k, err = 0;
3574
3575 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3576 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3577 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
3578
3579 if (!exts)
3580 return 0;
3581
3582 j = 0;
3583 tcf_exts_for_each_action(i, act, exts) {
3584 struct flow_action_entry *entry;
3585
3586 entry = &flow_action->entries[j];
3587 spin_lock_bh(&act->tcfa_lock);
3588 err = tcf_act_get_cookie(entry, act);
3589 if (err)
3590 goto err_out_locked;
3591
3592 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
3593
3594 if (is_tcf_gact_ok(act)) {
3595 entry->id = FLOW_ACTION_ACCEPT;
3596 } else if (is_tcf_gact_shot(act)) {
3597 entry->id = FLOW_ACTION_DROP;
3598 } else if (is_tcf_gact_trap(act)) {
3599 entry->id = FLOW_ACTION_TRAP;
3600 } else if (is_tcf_gact_goto_chain(act)) {
3601 entry->id = FLOW_ACTION_GOTO;
3602 entry->chain_index = tcf_gact_goto_chain_index(act);
3603 } else if (is_tcf_mirred_egress_redirect(act)) {
3604 entry->id = FLOW_ACTION_REDIRECT;
3605 tcf_mirred_get_dev(entry, act);
3606 } else if (is_tcf_mirred_egress_mirror(act)) {
3607 entry->id = FLOW_ACTION_MIRRED;
3608 tcf_mirred_get_dev(entry, act);
3609 } else if (is_tcf_mirred_ingress_redirect(act)) {
3610 entry->id = FLOW_ACTION_REDIRECT_INGRESS;
3611 tcf_mirred_get_dev(entry, act);
3612 } else if (is_tcf_mirred_ingress_mirror(act)) {
3613 entry->id = FLOW_ACTION_MIRRED_INGRESS;
3614 tcf_mirred_get_dev(entry, act);
3615 } else if (is_tcf_vlan(act)) {
3616 switch (tcf_vlan_action(act)) {
3617 case TCA_VLAN_ACT_PUSH:
3618 entry->id = FLOW_ACTION_VLAN_PUSH;
3619 entry->vlan.vid = tcf_vlan_push_vid(act);
3620 entry->vlan.proto = tcf_vlan_push_proto(act);
3621 entry->vlan.prio = tcf_vlan_push_prio(act);
3622 break;
3623 case TCA_VLAN_ACT_POP:
3624 entry->id = FLOW_ACTION_VLAN_POP;
3625 break;
3626 case TCA_VLAN_ACT_MODIFY:
3627 entry->id = FLOW_ACTION_VLAN_MANGLE;
3628 entry->vlan.vid = tcf_vlan_push_vid(act);
3629 entry->vlan.proto = tcf_vlan_push_proto(act);
3630 entry->vlan.prio = tcf_vlan_push_prio(act);
3631 break;
3632 default:
3633 err = -EOPNOTSUPP;
3634 goto err_out_locked;
3635 }
3636 } else if (is_tcf_tunnel_set(act)) {
3637 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
3638 err = tcf_tunnel_encap_get_tunnel(entry, act);
3639 if (err)
3640 goto err_out_locked;
3641 } else if (is_tcf_tunnel_release(act)) {
3642 entry->id = FLOW_ACTION_TUNNEL_DECAP;
3643 } else if (is_tcf_pedit(act)) {
3644 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3645 switch (tcf_pedit_cmd(act, k)) {
3646 case TCA_PEDIT_KEY_EX_CMD_SET:
3647 entry->id = FLOW_ACTION_MANGLE;
3648 break;
3649 case TCA_PEDIT_KEY_EX_CMD_ADD:
3650 entry->id = FLOW_ACTION_ADD;
3651 break;
3652 default:
3653 err = -EOPNOTSUPP;
3654 goto err_out_locked;
3655 }
3656 entry->mangle.htype = tcf_pedit_htype(act, k);
3657 entry->mangle.mask = tcf_pedit_mask(act, k);
3658 entry->mangle.val = tcf_pedit_val(act, k);
3659 entry->mangle.offset = tcf_pedit_offset(act, k);
3660 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
3661 entry = &flow_action->entries[++j];
3662 }
3663 } else if (is_tcf_csum(act)) {
3664 entry->id = FLOW_ACTION_CSUM;
3665 entry->csum_flags = tcf_csum_update_flags(act);
3666 } else if (is_tcf_skbedit_mark(act)) {
3667 entry->id = FLOW_ACTION_MARK;
3668 entry->mark = tcf_skbedit_mark(act);
3669 } else if (is_tcf_sample(act)) {
3670 entry->id = FLOW_ACTION_SAMPLE;
3671 entry->sample.trunc_size = tcf_sample_trunc_size(act);
3672 entry->sample.truncate = tcf_sample_truncate(act);
3673 entry->sample.rate = tcf_sample_rate(act);
3674 tcf_sample_get_group(entry, act);
3675 } else if (is_tcf_police(act)) {
3676 entry->id = FLOW_ACTION_POLICE;
3677 entry->police.burst = tcf_police_burst(act);
3678 entry->police.rate_bytes_ps =
3679 tcf_police_rate_bytes_ps(act);
3680 entry->police.mtu = tcf_police_tcfp_mtu(act);
3681 entry->police.index = act->tcfa_index;
3682 } else if (is_tcf_ct(act)) {
3683 entry->id = FLOW_ACTION_CT;
3684 entry->ct.action = tcf_ct_action(act);
3685 entry->ct.zone = tcf_ct_zone(act);
3686 entry->ct.flow_table = tcf_ct_ft(act);
3687 } else if (is_tcf_mpls(act)) {
3688 switch (tcf_mpls_action(act)) {
3689 case TCA_MPLS_ACT_PUSH:
3690 entry->id = FLOW_ACTION_MPLS_PUSH;
3691 entry->mpls_push.proto = tcf_mpls_proto(act);
3692 entry->mpls_push.label = tcf_mpls_label(act);
3693 entry->mpls_push.tc = tcf_mpls_tc(act);
3694 entry->mpls_push.bos = tcf_mpls_bos(act);
3695 entry->mpls_push.ttl = tcf_mpls_ttl(act);
3696 break;
3697 case TCA_MPLS_ACT_POP:
3698 entry->id = FLOW_ACTION_MPLS_POP;
3699 entry->mpls_pop.proto = tcf_mpls_proto(act);
3700 break;
3701 case TCA_MPLS_ACT_MODIFY:
3702 entry->id = FLOW_ACTION_MPLS_MANGLE;
3703 entry->mpls_mangle.label = tcf_mpls_label(act);
3704 entry->mpls_mangle.tc = tcf_mpls_tc(act);
3705 entry->mpls_mangle.bos = tcf_mpls_bos(act);
3706 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
3707 break;
3708 default:
3709 err = -EOPNOTSUPP;
3710 goto err_out_locked;
3711 }
3712 } else if (is_tcf_skbedit_ptype(act)) {
3713 entry->id = FLOW_ACTION_PTYPE;
3714 entry->ptype = tcf_skbedit_ptype(act);
3715 } else if (is_tcf_skbedit_priority(act)) {
3716 entry->id = FLOW_ACTION_PRIORITY;
3717 entry->priority = tcf_skbedit_priority(act);
3718 } else if (is_tcf_gate(act)) {
3719 entry->id = FLOW_ACTION_GATE;
3720 entry->gate.index = tcf_gate_index(act);
3721 entry->gate.prio = tcf_gate_prio(act);
3722 entry->gate.basetime = tcf_gate_basetime(act);
3723 entry->gate.cycletime = tcf_gate_cycletime(act);
3724 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
3725 entry->gate.num_entries = tcf_gate_num_entries(act);
3726 err = tcf_gate_get_entries(entry, act);
3727 if (err)
3728 goto err_out_locked;
3729 } else {
3730 err = -EOPNOTSUPP;
3731 goto err_out_locked;
3732 }
3733 spin_unlock_bh(&act->tcfa_lock);
3734
3735 if (!is_tcf_pedit(act))
3736 j++;
3737 }
3738
3739 err_out:
3740 if (err)
3741 tc_cleanup_flow_action(flow_action);
3742
3743 return err;
3744 err_out_locked:
3745 spin_unlock_bh(&act->tcfa_lock);
3746 goto err_out;
3747 }
3748 EXPORT_SYMBOL(tc_setup_flow_action);
3749
tcf_exts_num_actions(struct tcf_exts * exts)3750 unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3751 {
3752 unsigned int num_acts = 0;
3753 struct tc_action *act;
3754 int i;
3755
3756 tcf_exts_for_each_action(i, act, exts) {
3757 if (is_tcf_pedit(act))
3758 num_acts += tcf_pedit_nkeys(act);
3759 else
3760 num_acts++;
3761 }
3762 return num_acts;
3763 }
3764 EXPORT_SYMBOL(tcf_exts_num_actions);
3765
3766 #ifdef CONFIG_NET_CLS_ACT
tcf_qevent_parse_block_index(struct nlattr * block_index_attr,u32 * p_block_index,struct netlink_ext_ack * extack)3767 static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3768 u32 *p_block_index,
3769 struct netlink_ext_ack *extack)
3770 {
3771 *p_block_index = nla_get_u32(block_index_attr);
3772 if (!*p_block_index) {
3773 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3774 return -EINVAL;
3775 }
3776
3777 return 0;
3778 }
3779
tcf_qevent_init(struct tcf_qevent * qe,struct Qdisc * sch,enum flow_block_binder_type binder_type,struct nlattr * block_index_attr,struct netlink_ext_ack * extack)3780 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3781 enum flow_block_binder_type binder_type,
3782 struct nlattr *block_index_attr,
3783 struct netlink_ext_ack *extack)
3784 {
3785 u32 block_index;
3786 int err;
3787
3788 if (!block_index_attr)
3789 return 0;
3790
3791 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3792 if (err)
3793 return err;
3794
3795 if (!block_index)
3796 return 0;
3797
3798 qe->info.binder_type = binder_type;
3799 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3800 qe->info.chain_head_change_priv = &qe->filter_chain;
3801 qe->info.block_index = block_index;
3802
3803 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3804 }
3805 EXPORT_SYMBOL(tcf_qevent_init);
3806
tcf_qevent_destroy(struct tcf_qevent * qe,struct Qdisc * sch)3807 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3808 {
3809 if (qe->info.block_index)
3810 tcf_block_put_ext(qe->block, sch, &qe->info);
3811 }
3812 EXPORT_SYMBOL(tcf_qevent_destroy);
3813
tcf_qevent_validate_change(struct tcf_qevent * qe,struct nlattr * block_index_attr,struct netlink_ext_ack * extack)3814 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3815 struct netlink_ext_ack *extack)
3816 {
3817 u32 block_index;
3818 int err;
3819
3820 if (!block_index_attr)
3821 return 0;
3822
3823 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3824 if (err)
3825 return err;
3826
3827 /* Bounce newly-configured block or change in block. */
3828 if (block_index != qe->info.block_index) {
3829 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3830 return -EINVAL;
3831 }
3832
3833 return 0;
3834 }
3835 EXPORT_SYMBOL(tcf_qevent_validate_change);
3836
tcf_qevent_handle(struct tcf_qevent * qe,struct Qdisc * sch,struct sk_buff * skb,struct sk_buff ** to_free,int * ret)3837 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
3838 struct sk_buff **to_free, int *ret)
3839 {
3840 struct tcf_result cl_res;
3841 struct tcf_proto *fl;
3842
3843 if (!qe->info.block_index)
3844 return skb;
3845
3846 fl = rcu_dereference_bh(qe->filter_chain);
3847
3848 switch (tcf_classify(skb, fl, &cl_res, false)) {
3849 case TC_ACT_SHOT:
3850 qdisc_qstats_drop(sch);
3851 __qdisc_drop(skb, to_free);
3852 *ret = __NET_XMIT_BYPASS;
3853 return NULL;
3854 case TC_ACT_STOLEN:
3855 case TC_ACT_QUEUED:
3856 case TC_ACT_TRAP:
3857 __qdisc_drop(skb, to_free);
3858 *ret = __NET_XMIT_STOLEN;
3859 return NULL;
3860 case TC_ACT_REDIRECT:
3861 skb_do_redirect(skb);
3862 *ret = __NET_XMIT_STOLEN;
3863 return NULL;
3864 }
3865
3866 return skb;
3867 }
3868 EXPORT_SYMBOL(tcf_qevent_handle);
3869
tcf_qevent_dump(struct sk_buff * skb,int attr_name,struct tcf_qevent * qe)3870 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3871 {
3872 if (!qe->info.block_index)
3873 return 0;
3874 return nla_put_u32(skb, attr_name, qe->info.block_index);
3875 }
3876 EXPORT_SYMBOL(tcf_qevent_dump);
3877 #endif
3878
tcf_net_init(struct net * net)3879 static __net_init int tcf_net_init(struct net *net)
3880 {
3881 struct tcf_net *tn = net_generic(net, tcf_net_id);
3882
3883 spin_lock_init(&tn->idr_lock);
3884 idr_init(&tn->idr);
3885 return 0;
3886 }
3887
tcf_net_exit(struct net * net)3888 static void __net_exit tcf_net_exit(struct net *net)
3889 {
3890 struct tcf_net *tn = net_generic(net, tcf_net_id);
3891
3892 idr_destroy(&tn->idr);
3893 }
3894
3895 static struct pernet_operations tcf_net_ops = {
3896 .init = tcf_net_init,
3897 .exit = tcf_net_exit,
3898 .id = &tcf_net_id,
3899 .size = sizeof(struct tcf_net),
3900 };
3901
tc_filter_init(void)3902 static int __init tc_filter_init(void)
3903 {
3904 int err;
3905
3906 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3907 if (!tc_filter_wq)
3908 return -ENOMEM;
3909
3910 err = register_pernet_subsys(&tcf_net_ops);
3911 if (err)
3912 goto err_register_pernet_subsys;
3913
3914 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3915 RTNL_FLAG_DOIT_UNLOCKED);
3916 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3917 RTNL_FLAG_DOIT_UNLOCKED);
3918 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
3919 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
3920 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3921 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3922 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3923 tc_dump_chain, 0);
3924
3925 return 0;
3926
3927 err_register_pernet_subsys:
3928 destroy_workqueue(tc_filter_wq);
3929 return err;
3930 }
3931
3932 subsys_initcall(tc_filter_init);
3933