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