]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sched/cls_api.c
pkt_sched: fq: do not accept silly TCA_FQ_QUANTUM
[mirror_ubuntu-bionic-kernel.git] / net / sched / cls_api.c
1 /*
2 * net/sched/cls_api.c Packet classifier API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 *
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/slab.h>
27 #include <net/net_namespace.h>
28 #include <net/sock.h>
29 #include <net/netlink.h>
30 #include <net/pkt_sched.h>
31 #include <net/pkt_cls.h>
32
33 extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
34
35 /* The list of all installed classifier types */
36 static LIST_HEAD(tcf_proto_base);
37
38 /* Protects list of registered TC modules. It is pure SMP lock. */
39 static DEFINE_RWLOCK(cls_mod_lock);
40
41 /* Find classifier type by string name */
42
43 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
44 {
45 const struct tcf_proto_ops *t, *res = NULL;
46
47 if (kind) {
48 read_lock(&cls_mod_lock);
49 list_for_each_entry(t, &tcf_proto_base, head) {
50 if (strcmp(kind, t->kind) == 0) {
51 if (try_module_get(t->owner))
52 res = t;
53 break;
54 }
55 }
56 read_unlock(&cls_mod_lock);
57 }
58 return res;
59 }
60
61 /* Register(unregister) new classifier type */
62
63 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
64 {
65 struct tcf_proto_ops *t;
66 int rc = -EEXIST;
67
68 write_lock(&cls_mod_lock);
69 list_for_each_entry(t, &tcf_proto_base, head)
70 if (!strcmp(ops->kind, t->kind))
71 goto out;
72
73 list_add_tail(&ops->head, &tcf_proto_base);
74 rc = 0;
75 out:
76 write_unlock(&cls_mod_lock);
77 return rc;
78 }
79 EXPORT_SYMBOL(register_tcf_proto_ops);
80
81 static struct workqueue_struct *tc_filter_wq;
82
83 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
84 {
85 struct tcf_proto_ops *t;
86 int rc = -ENOENT;
87
88 /* Wait for outstanding call_rcu()s, if any, from a
89 * tcf_proto_ops's destroy() handler.
90 */
91 rcu_barrier();
92 flush_workqueue(tc_filter_wq);
93
94 write_lock(&cls_mod_lock);
95 list_for_each_entry(t, &tcf_proto_base, head) {
96 if (t == ops) {
97 list_del(&t->head);
98 rc = 0;
99 break;
100 }
101 }
102 write_unlock(&cls_mod_lock);
103 return rc;
104 }
105 EXPORT_SYMBOL(unregister_tcf_proto_ops);
106
107 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
108 {
109 INIT_RCU_WORK(rwork, func);
110 return queue_rcu_work(tc_filter_wq, rwork);
111 }
112 EXPORT_SYMBOL(tcf_queue_work);
113
114 /* Select new prio value from the range, managed by kernel. */
115
116 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
117 {
118 u32 first = TC_H_MAKE(0xC0000000U, 0U);
119
120 if (tp)
121 first = tp->prio - 1;
122
123 return TC_H_MAJ(first);
124 }
125
126 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
127 u32 prio, u32 parent, struct Qdisc *q,
128 struct tcf_chain *chain)
129 {
130 struct tcf_proto *tp;
131 int err;
132
133 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
134 if (!tp)
135 return ERR_PTR(-ENOBUFS);
136
137 err = -ENOENT;
138 tp->ops = tcf_proto_lookup_ops(kind);
139 if (!tp->ops) {
140 #ifdef CONFIG_MODULES
141 rtnl_unlock();
142 request_module("cls_%s", kind);
143 rtnl_lock();
144 tp->ops = tcf_proto_lookup_ops(kind);
145 /* We dropped the RTNL semaphore in order to perform
146 * the module load. So, even if we succeeded in loading
147 * the module we have to replay the request. We indicate
148 * this using -EAGAIN.
149 */
150 if (tp->ops) {
151 module_put(tp->ops->owner);
152 err = -EAGAIN;
153 } else {
154 err = -ENOENT;
155 }
156 #endif
157 goto errout;
158 }
159 tp->classify = tp->ops->classify;
160 tp->protocol = protocol;
161 tp->prio = prio;
162 tp->classid = parent;
163 tp->q = q;
164 tp->chain = chain;
165
166 err = tp->ops->init(tp);
167 if (err) {
168 module_put(tp->ops->owner);
169 goto errout;
170 }
171 return tp;
172
173 errout:
174 kfree(tp);
175 return ERR_PTR(err);
176 }
177
178 static void tcf_proto_destroy(struct tcf_proto *tp)
179 {
180 tp->ops->destroy(tp);
181 module_put(tp->ops->owner);
182 kfree_rcu(tp, rcu);
183 }
184
185 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
186 u32 chain_index)
187 {
188 struct tcf_chain *chain;
189
190 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
191 if (!chain)
192 return NULL;
193 list_add_tail(&chain->list, &block->chain_list);
194 chain->block = block;
195 chain->index = chain_index;
196 chain->refcnt = 1;
197 return chain;
198 }
199
200 static void tcf_chain_head_change(struct tcf_chain *chain,
201 struct tcf_proto *tp_head)
202 {
203 if (chain->chain_head_change)
204 chain->chain_head_change(tp_head,
205 chain->chain_head_change_priv);
206 }
207
208 static void tcf_chain_flush(struct tcf_chain *chain)
209 {
210 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
211
212 tcf_chain_head_change(chain, NULL);
213 while (tp) {
214 RCU_INIT_POINTER(chain->filter_chain, tp->next);
215 tcf_proto_destroy(tp);
216 tp = rtnl_dereference(chain->filter_chain);
217 tcf_chain_put(chain);
218 }
219 }
220
221 static void tcf_chain_destroy(struct tcf_chain *chain)
222 {
223 struct tcf_block *block = chain->block;
224
225 list_del(&chain->list);
226 kfree(chain);
227 if (list_empty(&block->chain_list))
228 kfree(block);
229 }
230
231 static void tcf_chain_hold(struct tcf_chain *chain)
232 {
233 ++chain->refcnt;
234 }
235
236 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
237 bool create)
238 {
239 struct tcf_chain *chain;
240
241 list_for_each_entry(chain, &block->chain_list, list) {
242 if (chain->index == chain_index) {
243 tcf_chain_hold(chain);
244 return chain;
245 }
246 }
247
248 return create ? tcf_chain_create(block, chain_index) : NULL;
249 }
250 EXPORT_SYMBOL(tcf_chain_get);
251
252 void tcf_chain_put(struct tcf_chain *chain)
253 {
254 if (--chain->refcnt == 0)
255 tcf_chain_destroy(chain);
256 }
257 EXPORT_SYMBOL(tcf_chain_put);
258
259 static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
260 struct tcf_block_ext_info *ei,
261 enum tc_block_command command)
262 {
263 struct net_device *dev = q->dev_queue->dev;
264 struct tc_block_offload bo = {};
265
266 if (!dev->netdev_ops->ndo_setup_tc)
267 return;
268 bo.command = command;
269 bo.binder_type = ei->binder_type;
270 bo.block = block;
271 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
272 }
273
274 static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
275 struct tcf_block_ext_info *ei)
276 {
277 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
278 }
279
280 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
281 struct tcf_block_ext_info *ei)
282 {
283 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
284 }
285
286 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
287 struct tcf_block_ext_info *ei)
288 {
289 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
290 struct tcf_chain *chain;
291 int err;
292
293 if (!block)
294 return -ENOMEM;
295 INIT_LIST_HEAD(&block->chain_list);
296 INIT_LIST_HEAD(&block->cb_list);
297
298 /* Create chain 0 by default, it has to be always present. */
299 chain = tcf_chain_create(block, 0);
300 if (!chain) {
301 err = -ENOMEM;
302 goto err_chain_create;
303 }
304 WARN_ON(!ei->chain_head_change);
305 chain->chain_head_change = ei->chain_head_change;
306 chain->chain_head_change_priv = ei->chain_head_change_priv;
307 block->net = qdisc_net(q);
308 block->q = q;
309 tcf_block_offload_bind(block, q, ei);
310 *p_block = block;
311 return 0;
312
313 err_chain_create:
314 kfree(block);
315 return err;
316 }
317 EXPORT_SYMBOL(tcf_block_get_ext);
318
319 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
320 {
321 struct tcf_proto __rcu **p_filter_chain = priv;
322
323 rcu_assign_pointer(*p_filter_chain, tp_head);
324 }
325
326 int tcf_block_get(struct tcf_block **p_block,
327 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
328 {
329 struct tcf_block_ext_info ei = {
330 .chain_head_change = tcf_chain_head_change_dflt,
331 .chain_head_change_priv = p_filter_chain,
332 };
333
334 WARN_ON(!p_filter_chain);
335 return tcf_block_get_ext(p_block, q, &ei);
336 }
337 EXPORT_SYMBOL(tcf_block_get);
338
339 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
340 * actions should be all removed after flushing.
341 */
342 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
343 struct tcf_block_ext_info *ei)
344 {
345 struct tcf_chain *chain, *tmp;
346
347 if (!block)
348 return;
349 /* Hold a refcnt for all chains, so that they don't disappear
350 * while we are iterating.
351 */
352 list_for_each_entry(chain, &block->chain_list, list)
353 tcf_chain_hold(chain);
354
355 list_for_each_entry(chain, &block->chain_list, list)
356 tcf_chain_flush(chain);
357
358 tcf_block_offload_unbind(block, q, ei);
359
360 /* At this point, all the chains should have refcnt >= 1. */
361 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
362 tcf_chain_put(chain);
363
364 /* Finally, put chain 0 and allow block to be freed. */
365 chain = list_first_entry(&block->chain_list, struct tcf_chain, list);
366 tcf_chain_put(chain);
367 }
368 EXPORT_SYMBOL(tcf_block_put_ext);
369
370 void tcf_block_put(struct tcf_block *block)
371 {
372 struct tcf_block_ext_info ei = {0, };
373
374 if (!block)
375 return;
376 tcf_block_put_ext(block, block->q, &ei);
377 }
378
379 EXPORT_SYMBOL(tcf_block_put);
380
381 struct tcf_block_cb {
382 struct list_head list;
383 tc_setup_cb_t *cb;
384 void *cb_ident;
385 void *cb_priv;
386 unsigned int refcnt;
387 };
388
389 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
390 {
391 return block_cb->cb_priv;
392 }
393 EXPORT_SYMBOL(tcf_block_cb_priv);
394
395 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
396 tc_setup_cb_t *cb, void *cb_ident)
397 { struct tcf_block_cb *block_cb;
398
399 list_for_each_entry(block_cb, &block->cb_list, list)
400 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
401 return block_cb;
402 return NULL;
403 }
404 EXPORT_SYMBOL(tcf_block_cb_lookup);
405
406 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
407 {
408 block_cb->refcnt++;
409 }
410 EXPORT_SYMBOL(tcf_block_cb_incref);
411
412 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
413 {
414 return --block_cb->refcnt;
415 }
416 EXPORT_SYMBOL(tcf_block_cb_decref);
417
418 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
419 tc_setup_cb_t *cb, void *cb_ident,
420 void *cb_priv)
421 {
422 struct tcf_block_cb *block_cb;
423
424 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
425 if (!block_cb)
426 return NULL;
427 block_cb->cb = cb;
428 block_cb->cb_ident = cb_ident;
429 block_cb->cb_priv = cb_priv;
430 list_add(&block_cb->list, &block->cb_list);
431 return block_cb;
432 }
433 EXPORT_SYMBOL(__tcf_block_cb_register);
434
435 int tcf_block_cb_register(struct tcf_block *block,
436 tc_setup_cb_t *cb, void *cb_ident,
437 void *cb_priv)
438 {
439 struct tcf_block_cb *block_cb;
440
441 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
442 return block_cb ? 0 : -ENOMEM;
443 }
444 EXPORT_SYMBOL(tcf_block_cb_register);
445
446 void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
447 {
448 list_del(&block_cb->list);
449 kfree(block_cb);
450 }
451 EXPORT_SYMBOL(__tcf_block_cb_unregister);
452
453 void tcf_block_cb_unregister(struct tcf_block *block,
454 tc_setup_cb_t *cb, void *cb_ident)
455 {
456 struct tcf_block_cb *block_cb;
457
458 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
459 if (!block_cb)
460 return;
461 __tcf_block_cb_unregister(block_cb);
462 }
463 EXPORT_SYMBOL(tcf_block_cb_unregister);
464
465 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
466 void *type_data, bool err_stop)
467 {
468 struct tcf_block_cb *block_cb;
469 int ok_count = 0;
470 int err;
471
472 list_for_each_entry(block_cb, &block->cb_list, list) {
473 err = block_cb->cb(type, type_data, block_cb->cb_priv);
474 if (err) {
475 if (err_stop)
476 return err;
477 } else {
478 ok_count++;
479 }
480 }
481 return ok_count;
482 }
483
484 /* Main classifier routine: scans classifier chain attached
485 * to this qdisc, (optionally) tests for protocol and asks
486 * specific classifiers.
487 */
488 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
489 struct tcf_result *res, bool compat_mode)
490 {
491 #ifdef CONFIG_NET_CLS_ACT
492 const int max_reclassify_loop = 4;
493 const struct tcf_proto *orig_tp = tp;
494 const struct tcf_proto *first_tp;
495 int limit = 0;
496
497 reclassify:
498 #endif
499 for (; tp; tp = rcu_dereference_bh(tp->next)) {
500 __be16 protocol = tc_skb_protocol(skb);
501 int err;
502
503 if (tp->protocol != protocol &&
504 tp->protocol != htons(ETH_P_ALL))
505 continue;
506
507 err = tp->classify(skb, tp, res);
508 #ifdef CONFIG_NET_CLS_ACT
509 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
510 first_tp = orig_tp;
511 goto reset;
512 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
513 first_tp = res->goto_tp;
514 goto reset;
515 }
516 #endif
517 if (err >= 0)
518 return err;
519 }
520
521 return TC_ACT_UNSPEC; /* signal: continue lookup */
522 #ifdef CONFIG_NET_CLS_ACT
523 reset:
524 if (unlikely(limit++ >= max_reclassify_loop)) {
525 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
526 tp->q->ops->id, tp->prio & 0xffff,
527 ntohs(tp->protocol));
528 return TC_ACT_SHOT;
529 }
530
531 tp = first_tp;
532 goto reclassify;
533 #endif
534 }
535 EXPORT_SYMBOL(tcf_classify);
536
537 struct tcf_chain_info {
538 struct tcf_proto __rcu **pprev;
539 struct tcf_proto __rcu *next;
540 };
541
542 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
543 {
544 return rtnl_dereference(*chain_info->pprev);
545 }
546
547 static void tcf_chain_tp_insert(struct tcf_chain *chain,
548 struct tcf_chain_info *chain_info,
549 struct tcf_proto *tp)
550 {
551 if (*chain_info->pprev == chain->filter_chain)
552 tcf_chain_head_change(chain, tp);
553 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
554 rcu_assign_pointer(*chain_info->pprev, tp);
555 tcf_chain_hold(chain);
556 }
557
558 static void tcf_chain_tp_remove(struct tcf_chain *chain,
559 struct tcf_chain_info *chain_info,
560 struct tcf_proto *tp)
561 {
562 struct tcf_proto *next = rtnl_dereference(chain_info->next);
563
564 if (tp == chain->filter_chain)
565 tcf_chain_head_change(chain, next);
566 RCU_INIT_POINTER(*chain_info->pprev, next);
567 tcf_chain_put(chain);
568 }
569
570 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
571 struct tcf_chain_info *chain_info,
572 u32 protocol, u32 prio,
573 bool prio_allocate)
574 {
575 struct tcf_proto **pprev;
576 struct tcf_proto *tp;
577
578 /* Check the chain for existence of proto-tcf with this priority */
579 for (pprev = &chain->filter_chain;
580 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
581 if (tp->prio >= prio) {
582 if (tp->prio == prio) {
583 if (prio_allocate ||
584 (tp->protocol != protocol && protocol))
585 return ERR_PTR(-EINVAL);
586 } else {
587 tp = NULL;
588 }
589 break;
590 }
591 }
592 chain_info->pprev = pprev;
593 chain_info->next = tp ? tp->next : NULL;
594 return tp;
595 }
596
597 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
598 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
599 void *fh, u32 portid, u32 seq, u16 flags, int event)
600 {
601 struct tcmsg *tcm;
602 struct nlmsghdr *nlh;
603 unsigned char *b = skb_tail_pointer(skb);
604
605 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
606 if (!nlh)
607 goto out_nlmsg_trim;
608 tcm = nlmsg_data(nlh);
609 tcm->tcm_family = AF_UNSPEC;
610 tcm->tcm__pad1 = 0;
611 tcm->tcm__pad2 = 0;
612 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
613 tcm->tcm_parent = parent;
614 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
615 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
616 goto nla_put_failure;
617 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
618 goto nla_put_failure;
619 if (!fh) {
620 tcm->tcm_handle = 0;
621 } else {
622 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
623 goto nla_put_failure;
624 }
625 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
626 return skb->len;
627
628 out_nlmsg_trim:
629 nla_put_failure:
630 nlmsg_trim(skb, b);
631 return -1;
632 }
633
634 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
635 struct nlmsghdr *n, struct tcf_proto *tp,
636 struct Qdisc *q, u32 parent,
637 void *fh, int event, bool unicast)
638 {
639 struct sk_buff *skb;
640 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
641
642 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
643 if (!skb)
644 return -ENOBUFS;
645
646 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
647 n->nlmsg_flags, event) <= 0) {
648 kfree_skb(skb);
649 return -EINVAL;
650 }
651
652 if (unicast)
653 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
654
655 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
656 n->nlmsg_flags & NLM_F_ECHO);
657 }
658
659 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
660 struct nlmsghdr *n, struct tcf_proto *tp,
661 struct Qdisc *q, u32 parent,
662 void *fh, bool unicast, bool *last)
663 {
664 struct sk_buff *skb;
665 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
666 int err;
667
668 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
669 if (!skb)
670 return -ENOBUFS;
671
672 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
673 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
674 kfree_skb(skb);
675 return -EINVAL;
676 }
677
678 err = tp->ops->delete(tp, fh, last);
679 if (err) {
680 kfree_skb(skb);
681 return err;
682 }
683
684 if (unicast)
685 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
686
687 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
688 n->nlmsg_flags & NLM_F_ECHO);
689 }
690
691 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
692 struct Qdisc *q, u32 parent,
693 struct nlmsghdr *n,
694 struct tcf_chain *chain, int event)
695 {
696 struct tcf_proto *tp;
697
698 for (tp = rtnl_dereference(chain->filter_chain);
699 tp; tp = rtnl_dereference(tp->next))
700 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
701 }
702
703 /* Add/change/delete/get a filter node */
704
705 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
706 struct netlink_ext_ack *extack)
707 {
708 struct net *net = sock_net(skb->sk);
709 struct nlattr *tca[TCA_MAX + 1];
710 struct tcmsg *t;
711 u32 protocol;
712 u32 prio;
713 bool prio_allocate;
714 u32 parent;
715 u32 chain_index;
716 struct net_device *dev;
717 struct Qdisc *q;
718 struct tcf_chain_info chain_info;
719 struct tcf_chain *chain = NULL;
720 struct tcf_block *block;
721 struct tcf_proto *tp;
722 const struct Qdisc_class_ops *cops;
723 unsigned long cl;
724 void *fh;
725 int err;
726 int tp_created;
727
728 if ((n->nlmsg_type != RTM_GETTFILTER) &&
729 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
730 return -EPERM;
731
732 replay:
733 tp_created = 0;
734
735 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
736 if (err < 0)
737 return err;
738
739 t = nlmsg_data(n);
740 protocol = TC_H_MIN(t->tcm_info);
741 prio = TC_H_MAJ(t->tcm_info);
742 prio_allocate = false;
743 parent = t->tcm_parent;
744 cl = 0;
745
746 if (prio == 0) {
747 switch (n->nlmsg_type) {
748 case RTM_DELTFILTER:
749 if (protocol || t->tcm_handle || tca[TCA_KIND])
750 return -ENOENT;
751 break;
752 case RTM_NEWTFILTER:
753 /* If no priority is provided by the user,
754 * we allocate one.
755 */
756 if (n->nlmsg_flags & NLM_F_CREATE) {
757 prio = TC_H_MAKE(0x80000000U, 0U);
758 prio_allocate = true;
759 break;
760 }
761 /* fall-through */
762 default:
763 return -ENOENT;
764 }
765 }
766
767 /* Find head of filter chain. */
768
769 /* Find link */
770 dev = __dev_get_by_index(net, t->tcm_ifindex);
771 if (dev == NULL)
772 return -ENODEV;
773
774 /* Find qdisc */
775 if (!parent) {
776 q = dev->qdisc;
777 parent = q->handle;
778 } else {
779 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
780 if (q == NULL)
781 return -EINVAL;
782 }
783
784 /* Is it classful? */
785 cops = q->ops->cl_ops;
786 if (!cops)
787 return -EINVAL;
788
789 if (!cops->tcf_block)
790 return -EOPNOTSUPP;
791
792 /* Do we search for filter, attached to class? */
793 if (TC_H_MIN(parent)) {
794 cl = cops->find(q, parent);
795 if (cl == 0)
796 return -ENOENT;
797 }
798
799 /* And the last stroke */
800 block = cops->tcf_block(q, cl);
801 if (!block) {
802 err = -EINVAL;
803 goto errout;
804 }
805
806 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
807 if (chain_index > TC_ACT_EXT_VAL_MASK) {
808 err = -EINVAL;
809 goto errout;
810 }
811 chain = tcf_chain_get(block, chain_index,
812 n->nlmsg_type == RTM_NEWTFILTER);
813 if (!chain) {
814 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
815 goto errout;
816 }
817
818 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
819 tfilter_notify_chain(net, skb, q, parent, n,
820 chain, RTM_DELTFILTER);
821 tcf_chain_flush(chain);
822 err = 0;
823 goto errout;
824 }
825
826 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
827 prio, prio_allocate);
828 if (IS_ERR(tp)) {
829 err = PTR_ERR(tp);
830 goto errout;
831 }
832
833 if (tp == NULL) {
834 /* Proto-tcf does not exist, create new one */
835
836 if (tca[TCA_KIND] == NULL || !protocol) {
837 err = -EINVAL;
838 goto errout;
839 }
840
841 if (n->nlmsg_type != RTM_NEWTFILTER ||
842 !(n->nlmsg_flags & NLM_F_CREATE)) {
843 err = -ENOENT;
844 goto errout;
845 }
846
847 if (prio_allocate)
848 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
849
850 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
851 protocol, prio, parent, q, chain);
852 if (IS_ERR(tp)) {
853 err = PTR_ERR(tp);
854 goto errout;
855 }
856 tp_created = 1;
857 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
858 err = -EINVAL;
859 goto errout;
860 }
861
862 fh = tp->ops->get(tp, t->tcm_handle);
863
864 if (!fh) {
865 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
866 tcf_chain_tp_remove(chain, &chain_info, tp);
867 tfilter_notify(net, skb, n, tp, q, parent, fh,
868 RTM_DELTFILTER, false);
869 tcf_proto_destroy(tp);
870 err = 0;
871 goto errout;
872 }
873
874 if (n->nlmsg_type != RTM_NEWTFILTER ||
875 !(n->nlmsg_flags & NLM_F_CREATE)) {
876 err = -ENOENT;
877 goto errout;
878 }
879 } else {
880 bool last;
881
882 switch (n->nlmsg_type) {
883 case RTM_NEWTFILTER:
884 if (n->nlmsg_flags & NLM_F_EXCL) {
885 if (tp_created)
886 tcf_proto_destroy(tp);
887 err = -EEXIST;
888 goto errout;
889 }
890 break;
891 case RTM_DELTFILTER:
892 err = tfilter_del_notify(net, skb, n, tp, q, parent,
893 fh, false, &last);
894 if (err)
895 goto errout;
896 if (last) {
897 tcf_chain_tp_remove(chain, &chain_info, tp);
898 tcf_proto_destroy(tp);
899 }
900 goto errout;
901 case RTM_GETTFILTER:
902 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
903 RTM_NEWTFILTER, true);
904 goto errout;
905 default:
906 err = -EINVAL;
907 goto errout;
908 }
909 }
910
911 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
912 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
913 if (err == 0) {
914 if (tp_created)
915 tcf_chain_tp_insert(chain, &chain_info, tp);
916 tfilter_notify(net, skb, n, tp, q, parent, fh,
917 RTM_NEWTFILTER, false);
918 /* q pointer is NULL for shared blocks */
919 if (q)
920 q->flags &= ~TCQ_F_CAN_BYPASS;
921 } else {
922 if (tp_created)
923 tcf_proto_destroy(tp);
924 }
925
926 errout:
927 if (chain)
928 tcf_chain_put(chain);
929 if (err == -EAGAIN)
930 /* Replay the request. */
931 goto replay;
932 return err;
933 }
934
935 struct tcf_dump_args {
936 struct tcf_walker w;
937 struct sk_buff *skb;
938 struct netlink_callback *cb;
939 struct Qdisc *q;
940 u32 parent;
941 };
942
943 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
944 {
945 struct tcf_dump_args *a = (void *)arg;
946 struct net *net = sock_net(a->skb->sk);
947
948 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
949 n, NETLINK_CB(a->cb->skb).portid,
950 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
951 RTM_NEWTFILTER);
952 }
953
954 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
955 struct sk_buff *skb, struct netlink_callback *cb,
956 long index_start, long *p_index)
957 {
958 struct net *net = sock_net(skb->sk);
959 struct tcmsg *tcm = nlmsg_data(cb->nlh);
960 struct tcf_dump_args arg;
961 struct tcf_proto *tp;
962
963 for (tp = rtnl_dereference(chain->filter_chain);
964 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
965 if (*p_index < index_start)
966 continue;
967 if (TC_H_MAJ(tcm->tcm_info) &&
968 TC_H_MAJ(tcm->tcm_info) != tp->prio)
969 continue;
970 if (TC_H_MIN(tcm->tcm_info) &&
971 TC_H_MIN(tcm->tcm_info) != tp->protocol)
972 continue;
973 if (*p_index > index_start)
974 memset(&cb->args[1], 0,
975 sizeof(cb->args) - sizeof(cb->args[0]));
976 if (cb->args[1] == 0) {
977 if (tcf_fill_node(net, skb, tp, q, parent, 0,
978 NETLINK_CB(cb->skb).portid,
979 cb->nlh->nlmsg_seq, NLM_F_MULTI,
980 RTM_NEWTFILTER) <= 0)
981 return false;
982
983 cb->args[1] = 1;
984 }
985 if (!tp->ops->walk)
986 continue;
987 arg.w.fn = tcf_node_dump;
988 arg.skb = skb;
989 arg.cb = cb;
990 arg.q = q;
991 arg.parent = parent;
992 arg.w.stop = 0;
993 arg.w.skip = cb->args[1] - 1;
994 arg.w.count = 0;
995 tp->ops->walk(tp, &arg.w);
996 cb->args[1] = arg.w.count + 1;
997 if (arg.w.stop)
998 return false;
999 }
1000 return true;
1001 }
1002
1003 /* called with RTNL */
1004 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1005 {
1006 struct net *net = sock_net(skb->sk);
1007 struct nlattr *tca[TCA_MAX + 1];
1008 struct net_device *dev;
1009 struct Qdisc *q;
1010 struct tcf_block *block;
1011 struct tcf_chain *chain;
1012 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1013 unsigned long cl = 0;
1014 const struct Qdisc_class_ops *cops;
1015 long index_start;
1016 long index;
1017 u32 parent;
1018 int err;
1019
1020 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1021 return skb->len;
1022
1023 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
1024 NULL);
1025 if (err)
1026 return err;
1027
1028 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1029 if (!dev)
1030 return skb->len;
1031
1032 parent = tcm->tcm_parent;
1033 if (!parent) {
1034 q = dev->qdisc;
1035 parent = q->handle;
1036 } else {
1037 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1038 }
1039 if (!q)
1040 goto out;
1041 cops = q->ops->cl_ops;
1042 if (!cops)
1043 goto out;
1044 if (!cops->tcf_block)
1045 goto out;
1046 if (TC_H_MIN(tcm->tcm_parent)) {
1047 cl = cops->find(q, tcm->tcm_parent);
1048 if (cl == 0)
1049 goto out;
1050 }
1051 block = cops->tcf_block(q, cl);
1052 if (!block)
1053 goto out;
1054
1055 index_start = cb->args[0];
1056 index = 0;
1057
1058 list_for_each_entry(chain, &block->chain_list, list) {
1059 if (tca[TCA_CHAIN] &&
1060 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1061 continue;
1062 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1063 index_start, &index)) {
1064 err = -EMSGSIZE;
1065 break;
1066 }
1067 }
1068
1069 cb->args[0] = index;
1070
1071 out:
1072 /* If we did no progress, the error (EMSGSIZE) is real */
1073 if (skb->len == 0 && err)
1074 return err;
1075 return skb->len;
1076 }
1077
1078 void tcf_exts_destroy(struct tcf_exts *exts)
1079 {
1080 #ifdef CONFIG_NET_CLS_ACT
1081 LIST_HEAD(actions);
1082
1083 ASSERT_RTNL();
1084 tcf_exts_to_list(exts, &actions);
1085 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1086 kfree(exts->actions);
1087 exts->nr_actions = 0;
1088 #endif
1089 }
1090 EXPORT_SYMBOL(tcf_exts_destroy);
1091
1092 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1093 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1094 {
1095 #ifdef CONFIG_NET_CLS_ACT
1096 {
1097 struct tc_action *act;
1098
1099 if (exts->police && tb[exts->police]) {
1100 act = tcf_action_init_1(net, tp, tb[exts->police],
1101 rate_tlv, "police", ovr,
1102 TCA_ACT_BIND);
1103 if (IS_ERR(act))
1104 return PTR_ERR(act);
1105
1106 act->type = exts->type = TCA_OLD_COMPAT;
1107 exts->actions[0] = act;
1108 exts->nr_actions = 1;
1109 } else if (exts->action && tb[exts->action]) {
1110 LIST_HEAD(actions);
1111 int err, i = 0;
1112
1113 err = tcf_action_init(net, tp, tb[exts->action],
1114 rate_tlv, NULL, ovr, TCA_ACT_BIND,
1115 &actions);
1116 if (err)
1117 return err;
1118 list_for_each_entry(act, &actions, list)
1119 exts->actions[i++] = act;
1120 exts->nr_actions = i;
1121 }
1122 }
1123 #else
1124 if ((exts->action && tb[exts->action]) ||
1125 (exts->police && tb[exts->police]))
1126 return -EOPNOTSUPP;
1127 #endif
1128
1129 return 0;
1130 }
1131 EXPORT_SYMBOL(tcf_exts_validate);
1132
1133 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1134 {
1135 #ifdef CONFIG_NET_CLS_ACT
1136 struct tcf_exts old = *dst;
1137
1138 *dst = *src;
1139 tcf_exts_destroy(&old);
1140 #endif
1141 }
1142 EXPORT_SYMBOL(tcf_exts_change);
1143
1144 #ifdef CONFIG_NET_CLS_ACT
1145 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1146 {
1147 if (exts->nr_actions == 0)
1148 return NULL;
1149 else
1150 return exts->actions[0];
1151 }
1152 #endif
1153
1154 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1155 {
1156 #ifdef CONFIG_NET_CLS_ACT
1157 struct nlattr *nest;
1158
1159 if (exts->action && tcf_exts_has_actions(exts)) {
1160 /*
1161 * again for backward compatible mode - we want
1162 * to work with both old and new modes of entering
1163 * tc data even if iproute2 was newer - jhs
1164 */
1165 if (exts->type != TCA_OLD_COMPAT) {
1166 LIST_HEAD(actions);
1167
1168 nest = nla_nest_start(skb, exts->action);
1169 if (nest == NULL)
1170 goto nla_put_failure;
1171
1172 tcf_exts_to_list(exts, &actions);
1173 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1174 goto nla_put_failure;
1175 nla_nest_end(skb, nest);
1176 } else if (exts->police) {
1177 struct tc_action *act = tcf_exts_first_act(exts);
1178 nest = nla_nest_start(skb, exts->police);
1179 if (nest == NULL || !act)
1180 goto nla_put_failure;
1181 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1182 goto nla_put_failure;
1183 nla_nest_end(skb, nest);
1184 }
1185 }
1186 return 0;
1187
1188 nla_put_failure:
1189 nla_nest_cancel(skb, nest);
1190 return -1;
1191 #else
1192 return 0;
1193 #endif
1194 }
1195 EXPORT_SYMBOL(tcf_exts_dump);
1196
1197
1198 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1199 {
1200 #ifdef CONFIG_NET_CLS_ACT
1201 struct tc_action *a = tcf_exts_first_act(exts);
1202 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1203 return -1;
1204 #endif
1205 return 0;
1206 }
1207 EXPORT_SYMBOL(tcf_exts_dump_stats);
1208
1209 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1210 enum tc_setup_type type,
1211 void *type_data, bool err_stop)
1212 {
1213 int ok_count = 0;
1214 #ifdef CONFIG_NET_CLS_ACT
1215 const struct tc_action *a;
1216 struct net_device *dev;
1217 int i, ret;
1218
1219 if (!tcf_exts_has_actions(exts))
1220 return 0;
1221
1222 for (i = 0; i < exts->nr_actions; i++) {
1223 a = exts->actions[i];
1224 if (!a->ops->get_dev)
1225 continue;
1226 dev = a->ops->get_dev(a);
1227 if (!dev)
1228 continue;
1229 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1230 if (ret < 0)
1231 return ret;
1232 ok_count += ret;
1233 }
1234 #endif
1235 return ok_count;
1236 }
1237
1238 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1239 enum tc_setup_type type, void *type_data, bool err_stop)
1240 {
1241 int ok_count;
1242 int ret;
1243
1244 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1245 if (ret < 0)
1246 return ret;
1247 ok_count = ret;
1248
1249 if (!exts || ok_count)
1250 return ok_count;
1251 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1252 if (ret < 0)
1253 return ret;
1254 ok_count += ret;
1255
1256 return ok_count;
1257 }
1258 EXPORT_SYMBOL(tc_setup_cb_call);
1259
1260 static int __init tc_filter_init(void)
1261 {
1262 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1263 if (!tc_filter_wq)
1264 return -ENOMEM;
1265
1266 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1267 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1268 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1269 tc_dump_tfilter, 0);
1270
1271 return 0;
1272 }
1273
1274 subsys_initcall(tc_filter_init);