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