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/err.h> 27 #include <linux/slab.h> 28 #include <net/net_namespace.h> 29 #include <net/sock.h> 30 #include <net/netlink.h> 31 #include <net/pkt_sched.h> 32 #include <net/pkt_cls.h> 33 34 /* The list of all installed classifier types */ 35 static LIST_HEAD(tcf_proto_base); 36 37 /* Protects list of registered TC modules. It is pure SMP lock. */ 38 static DEFINE_RWLOCK(cls_mod_lock); 39 40 /* Find classifier type by string name */ 41 42 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind) 43 { 44 const struct tcf_proto_ops *t, *res = NULL; 45 46 if (kind) { 47 read_lock(&cls_mod_lock); 48 list_for_each_entry(t, &tcf_proto_base, head) { 49 if (strcmp(kind, t->kind) == 0) { 50 if (try_module_get(t->owner)) 51 res = t; 52 break; 53 } 54 } 55 read_unlock(&cls_mod_lock); 56 } 57 return res; 58 } 59 60 /* Register(unregister) new classifier type */ 61 62 int register_tcf_proto_ops(struct tcf_proto_ops *ops) 63 { 64 struct tcf_proto_ops *t; 65 int rc = -EEXIST; 66 67 write_lock(&cls_mod_lock); 68 list_for_each_entry(t, &tcf_proto_base, head) 69 if (!strcmp(ops->kind, t->kind)) 70 goto out; 71 72 list_add_tail(&ops->head, &tcf_proto_base); 73 rc = 0; 74 out: 75 write_unlock(&cls_mod_lock); 76 return rc; 77 } 78 EXPORT_SYMBOL(register_tcf_proto_ops); 79 80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) 81 { 82 struct tcf_proto_ops *t; 83 int rc = -ENOENT; 84 85 /* Wait for outstanding call_rcu()s, if any, from a 86 * tcf_proto_ops's destroy() handler. 87 */ 88 rcu_barrier(); 89 90 write_lock(&cls_mod_lock); 91 list_for_each_entry(t, &tcf_proto_base, head) { 92 if (t == ops) { 93 list_del(&t->head); 94 rc = 0; 95 break; 96 } 97 } 98 write_unlock(&cls_mod_lock); 99 return rc; 100 } 101 EXPORT_SYMBOL(unregister_tcf_proto_ops); 102 103 /* Select new prio value from the range, managed by kernel. */ 104 105 static inline u32 tcf_auto_prio(struct tcf_proto *tp) 106 { 107 u32 first = TC_H_MAKE(0xC0000000U, 0U); 108 109 if (tp) 110 first = tp->prio - 1; 111 112 return TC_H_MAJ(first); 113 } 114 115 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, 116 u32 prio, u32 parent, struct Qdisc *q, 117 struct tcf_chain *chain) 118 { 119 struct tcf_proto *tp; 120 int err; 121 122 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 123 if (!tp) 124 return ERR_PTR(-ENOBUFS); 125 126 err = -ENOENT; 127 tp->ops = tcf_proto_lookup_ops(kind); 128 if (!tp->ops) { 129 #ifdef CONFIG_MODULES 130 rtnl_unlock(); 131 request_module("cls_%s", kind); 132 rtnl_lock(); 133 tp->ops = tcf_proto_lookup_ops(kind); 134 /* We dropped the RTNL semaphore in order to perform 135 * the module load. So, even if we succeeded in loading 136 * the module we have to replay the request. We indicate 137 * this using -EAGAIN. 138 */ 139 if (tp->ops) { 140 module_put(tp->ops->owner); 141 err = -EAGAIN; 142 } else { 143 err = -ENOENT; 144 } 145 goto errout; 146 #endif 147 } 148 tp->classify = tp->ops->classify; 149 tp->protocol = protocol; 150 tp->prio = prio; 151 tp->classid = parent; 152 tp->q = q; 153 tp->chain = chain; 154 155 err = tp->ops->init(tp); 156 if (err) { 157 module_put(tp->ops->owner); 158 goto errout; 159 } 160 return tp; 161 162 errout: 163 kfree(tp); 164 return ERR_PTR(err); 165 } 166 167 static void tcf_proto_destroy(struct tcf_proto *tp) 168 { 169 tp->ops->destroy(tp); 170 module_put(tp->ops->owner); 171 kfree_rcu(tp, rcu); 172 } 173 174 static struct tcf_chain *tcf_chain_create(struct tcf_block *block, 175 u32 chain_index) 176 { 177 struct tcf_chain *chain; 178 179 chain = kzalloc(sizeof(*chain), GFP_KERNEL); 180 if (!chain) 181 return NULL; 182 list_add_tail(&chain->list, &block->chain_list); 183 chain->block = block; 184 chain->index = chain_index; 185 chain->refcnt = 1; 186 return chain; 187 } 188 189 static void tcf_chain_flush(struct tcf_chain *chain) 190 { 191 struct tcf_proto *tp; 192 193 if (chain->p_filter_chain) 194 RCU_INIT_POINTER(*chain->p_filter_chain, NULL); 195 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) { 196 RCU_INIT_POINTER(chain->filter_chain, tp->next); 197 tcf_proto_destroy(tp); 198 } 199 } 200 201 static void tcf_chain_destroy(struct tcf_chain *chain) 202 { 203 list_del(&chain->list); 204 tcf_chain_flush(chain); 205 kfree(chain); 206 } 207 208 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, 209 bool create) 210 { 211 struct tcf_chain *chain; 212 213 list_for_each_entry(chain, &block->chain_list, list) { 214 if (chain->index == chain_index) { 215 chain->refcnt++; 216 return chain; 217 } 218 } 219 if (create) 220 return tcf_chain_create(block, chain_index); 221 else 222 return NULL; 223 } 224 EXPORT_SYMBOL(tcf_chain_get); 225 226 void tcf_chain_put(struct tcf_chain *chain) 227 { 228 /* Destroy unused chain, with exception of chain 0, which is the 229 * default one and has to be always present. 230 */ 231 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0) 232 tcf_chain_destroy(chain); 233 } 234 EXPORT_SYMBOL(tcf_chain_put); 235 236 static void 237 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain, 238 struct tcf_proto __rcu **p_filter_chain) 239 { 240 chain->p_filter_chain = p_filter_chain; 241 } 242 243 int tcf_block_get(struct tcf_block **p_block, 244 struct tcf_proto __rcu **p_filter_chain) 245 { 246 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL); 247 struct tcf_chain *chain; 248 int err; 249 250 if (!block) 251 return -ENOMEM; 252 INIT_LIST_HEAD(&block->chain_list); 253 /* Create chain 0 by default, it has to be always present. */ 254 chain = tcf_chain_create(block, 0); 255 if (!chain) { 256 err = -ENOMEM; 257 goto err_chain_create; 258 } 259 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain); 260 *p_block = block; 261 return 0; 262 263 err_chain_create: 264 kfree(block); 265 return err; 266 } 267 EXPORT_SYMBOL(tcf_block_get); 268 269 void tcf_block_put(struct tcf_block *block) 270 { 271 struct tcf_chain *chain, *tmp; 272 273 if (!block) 274 return; 275 276 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) 277 tcf_chain_destroy(chain); 278 kfree(block); 279 } 280 EXPORT_SYMBOL(tcf_block_put); 281 282 /* Main classifier routine: scans classifier chain attached 283 * to this qdisc, (optionally) tests for protocol and asks 284 * specific classifiers. 285 */ 286 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 287 struct tcf_result *res, bool compat_mode) 288 { 289 __be16 protocol = tc_skb_protocol(skb); 290 #ifdef CONFIG_NET_CLS_ACT 291 const int max_reclassify_loop = 4; 292 const struct tcf_proto *orig_tp = tp; 293 const struct tcf_proto *first_tp; 294 int limit = 0; 295 296 reclassify: 297 #endif 298 for (; tp; tp = rcu_dereference_bh(tp->next)) { 299 int err; 300 301 if (tp->protocol != protocol && 302 tp->protocol != htons(ETH_P_ALL)) 303 continue; 304 305 err = tp->classify(skb, tp, res); 306 #ifdef CONFIG_NET_CLS_ACT 307 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { 308 first_tp = orig_tp; 309 goto reset; 310 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { 311 first_tp = res->goto_tp; 312 goto reset; 313 } 314 #endif 315 if (err >= 0) 316 return err; 317 } 318 319 return TC_ACT_UNSPEC; /* signal: continue lookup */ 320 #ifdef CONFIG_NET_CLS_ACT 321 reset: 322 if (unlikely(limit++ >= max_reclassify_loop)) { 323 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n", 324 tp->q->ops->id, tp->prio & 0xffff, 325 ntohs(tp->protocol)); 326 return TC_ACT_SHOT; 327 } 328 329 tp = first_tp; 330 protocol = tc_skb_protocol(skb); 331 goto reclassify; 332 #endif 333 } 334 EXPORT_SYMBOL(tcf_classify); 335 336 struct tcf_chain_info { 337 struct tcf_proto __rcu **pprev; 338 struct tcf_proto __rcu *next; 339 }; 340 341 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) 342 { 343 return rtnl_dereference(*chain_info->pprev); 344 } 345 346 static void tcf_chain_tp_insert(struct tcf_chain *chain, 347 struct tcf_chain_info *chain_info, 348 struct tcf_proto *tp) 349 { 350 if (chain->p_filter_chain && 351 *chain_info->pprev == chain->filter_chain) 352 rcu_assign_pointer(*chain->p_filter_chain, tp); 353 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); 354 rcu_assign_pointer(*chain_info->pprev, tp); 355 } 356 357 static void tcf_chain_tp_remove(struct tcf_chain *chain, 358 struct tcf_chain_info *chain_info, 359 struct tcf_proto *tp) 360 { 361 struct tcf_proto *next = rtnl_dereference(chain_info->next); 362 363 if (chain->p_filter_chain && tp == chain->filter_chain) 364 RCU_INIT_POINTER(*chain->p_filter_chain, next); 365 RCU_INIT_POINTER(*chain_info->pprev, next); 366 } 367 368 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, 369 struct tcf_chain_info *chain_info, 370 u32 protocol, u32 prio, 371 bool prio_allocate) 372 { 373 struct tcf_proto **pprev; 374 struct tcf_proto *tp; 375 376 /* Check the chain for existence of proto-tcf with this priority */ 377 for (pprev = &chain->filter_chain; 378 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { 379 if (tp->prio >= prio) { 380 if (tp->prio == prio) { 381 if (prio_allocate || 382 (tp->protocol != protocol && protocol)) 383 return ERR_PTR(-EINVAL); 384 } else { 385 tp = NULL; 386 } 387 break; 388 } 389 } 390 chain_info->pprev = pprev; 391 chain_info->next = tp ? tp->next : NULL; 392 return tp; 393 } 394 395 static int tcf_fill_node(struct net *net, struct sk_buff *skb, 396 struct tcf_proto *tp, void *fh, u32 portid, 397 u32 seq, u16 flags, int event) 398 { 399 struct tcmsg *tcm; 400 struct nlmsghdr *nlh; 401 unsigned char *b = skb_tail_pointer(skb); 402 403 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 404 if (!nlh) 405 goto out_nlmsg_trim; 406 tcm = nlmsg_data(nlh); 407 tcm->tcm_family = AF_UNSPEC; 408 tcm->tcm__pad1 = 0; 409 tcm->tcm__pad2 = 0; 410 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex; 411 tcm->tcm_parent = tp->classid; 412 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); 413 if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) 414 goto nla_put_failure; 415 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) 416 goto nla_put_failure; 417 if (!fh) { 418 tcm->tcm_handle = 0; 419 } else { 420 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) 421 goto nla_put_failure; 422 } 423 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 424 return skb->len; 425 426 out_nlmsg_trim: 427 nla_put_failure: 428 nlmsg_trim(skb, b); 429 return -1; 430 } 431 432 static int tfilter_notify(struct net *net, struct sk_buff *oskb, 433 struct nlmsghdr *n, struct tcf_proto *tp, 434 void *fh, int event, bool unicast) 435 { 436 struct sk_buff *skb; 437 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 438 439 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 440 if (!skb) 441 return -ENOBUFS; 442 443 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, 444 n->nlmsg_flags, event) <= 0) { 445 kfree_skb(skb); 446 return -EINVAL; 447 } 448 449 if (unicast) 450 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 451 452 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 453 n->nlmsg_flags & NLM_F_ECHO); 454 } 455 456 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, 457 struct nlmsghdr *n, struct tcf_proto *tp, 458 void *fh, bool unicast, bool *last) 459 { 460 struct sk_buff *skb; 461 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 462 int err; 463 464 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 465 if (!skb) 466 return -ENOBUFS; 467 468 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, 469 n->nlmsg_flags, RTM_DELTFILTER) <= 0) { 470 kfree_skb(skb); 471 return -EINVAL; 472 } 473 474 err = tp->ops->delete(tp, fh, last); 475 if (err) { 476 kfree_skb(skb); 477 return err; 478 } 479 480 if (unicast) 481 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); 482 483 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 484 n->nlmsg_flags & NLM_F_ECHO); 485 } 486 487 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, 488 struct nlmsghdr *n, 489 struct tcf_chain *chain, int event) 490 { 491 struct tcf_proto *tp; 492 493 for (tp = rtnl_dereference(chain->filter_chain); 494 tp; tp = rtnl_dereference(tp->next)) 495 tfilter_notify(net, oskb, n, tp, 0, event, false); 496 } 497 498 /* Add/change/delete/get a filter node */ 499 500 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, 501 struct netlink_ext_ack *extack) 502 { 503 struct net *net = sock_net(skb->sk); 504 struct nlattr *tca[TCA_MAX + 1]; 505 struct tcmsg *t; 506 u32 protocol; 507 u32 prio; 508 bool prio_allocate; 509 u32 parent; 510 u32 chain_index; 511 struct net_device *dev; 512 struct Qdisc *q; 513 struct tcf_chain_info chain_info; 514 struct tcf_chain *chain = NULL; 515 struct tcf_block *block; 516 struct tcf_proto *tp; 517 const struct Qdisc_class_ops *cops; 518 unsigned long cl; 519 void *fh; 520 int err; 521 int tp_created; 522 523 if ((n->nlmsg_type != RTM_GETTFILTER) && 524 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) 525 return -EPERM; 526 527 replay: 528 tp_created = 0; 529 530 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); 531 if (err < 0) 532 return err; 533 534 t = nlmsg_data(n); 535 protocol = TC_H_MIN(t->tcm_info); 536 prio = TC_H_MAJ(t->tcm_info); 537 prio_allocate = false; 538 parent = t->tcm_parent; 539 cl = 0; 540 541 if (prio == 0) { 542 switch (n->nlmsg_type) { 543 case RTM_DELTFILTER: 544 if (protocol || t->tcm_handle || tca[TCA_KIND]) 545 return -ENOENT; 546 break; 547 case RTM_NEWTFILTER: 548 /* If no priority is provided by the user, 549 * we allocate one. 550 */ 551 if (n->nlmsg_flags & NLM_F_CREATE) { 552 prio = TC_H_MAKE(0x80000000U, 0U); 553 prio_allocate = true; 554 break; 555 } 556 /* fall-through */ 557 default: 558 return -ENOENT; 559 } 560 } 561 562 /* Find head of filter chain. */ 563 564 /* Find link */ 565 dev = __dev_get_by_index(net, t->tcm_ifindex); 566 if (dev == NULL) 567 return -ENODEV; 568 569 /* Find qdisc */ 570 if (!parent) { 571 q = dev->qdisc; 572 parent = q->handle; 573 } else { 574 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); 575 if (q == NULL) 576 return -EINVAL; 577 } 578 579 /* Is it classful? */ 580 cops = q->ops->cl_ops; 581 if (!cops) 582 return -EINVAL; 583 584 if (!cops->tcf_block) 585 return -EOPNOTSUPP; 586 587 /* Do we search for filter, attached to class? */ 588 if (TC_H_MIN(parent)) { 589 cl = cops->find(q, parent); 590 if (cl == 0) 591 return -ENOENT; 592 } 593 594 /* And the last stroke */ 595 block = cops->tcf_block(q, cl); 596 if (!block) { 597 err = -EINVAL; 598 goto errout; 599 } 600 601 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; 602 if (chain_index > TC_ACT_EXT_VAL_MASK) { 603 err = -EINVAL; 604 goto errout; 605 } 606 chain = tcf_chain_get(block, chain_index, 607 n->nlmsg_type == RTM_NEWTFILTER); 608 if (!chain) { 609 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL; 610 goto errout; 611 } 612 613 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { 614 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER); 615 tcf_chain_flush(chain); 616 err = 0; 617 goto errout; 618 } 619 620 tp = tcf_chain_tp_find(chain, &chain_info, protocol, 621 prio, prio_allocate); 622 if (IS_ERR(tp)) { 623 err = PTR_ERR(tp); 624 goto errout; 625 } 626 627 if (tp == NULL) { 628 /* Proto-tcf does not exist, create new one */ 629 630 if (tca[TCA_KIND] == NULL || !protocol) { 631 err = -EINVAL; 632 goto errout; 633 } 634 635 if (n->nlmsg_type != RTM_NEWTFILTER || 636 !(n->nlmsg_flags & NLM_F_CREATE)) { 637 err = -ENOENT; 638 goto errout; 639 } 640 641 if (prio_allocate) 642 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); 643 644 tp = tcf_proto_create(nla_data(tca[TCA_KIND]), 645 protocol, prio, parent, q, chain); 646 if (IS_ERR(tp)) { 647 err = PTR_ERR(tp); 648 goto errout; 649 } 650 tp_created = 1; 651 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { 652 err = -EINVAL; 653 goto errout; 654 } 655 656 fh = tp->ops->get(tp, t->tcm_handle); 657 658 if (!fh) { 659 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { 660 tcf_chain_tp_remove(chain, &chain_info, tp); 661 tfilter_notify(net, skb, n, tp, fh, 662 RTM_DELTFILTER, false); 663 tcf_proto_destroy(tp); 664 err = 0; 665 goto errout; 666 } 667 668 if (n->nlmsg_type != RTM_NEWTFILTER || 669 !(n->nlmsg_flags & NLM_F_CREATE)) { 670 err = -ENOENT; 671 goto errout; 672 } 673 } else { 674 bool last; 675 676 switch (n->nlmsg_type) { 677 case RTM_NEWTFILTER: 678 if (n->nlmsg_flags & NLM_F_EXCL) { 679 if (tp_created) 680 tcf_proto_destroy(tp); 681 err = -EEXIST; 682 goto errout; 683 } 684 break; 685 case RTM_DELTFILTER: 686 err = tfilter_del_notify(net, skb, n, tp, fh, false, 687 &last); 688 if (err) 689 goto errout; 690 if (last) { 691 tcf_chain_tp_remove(chain, &chain_info, tp); 692 tcf_proto_destroy(tp); 693 } 694 goto errout; 695 case RTM_GETTFILTER: 696 err = tfilter_notify(net, skb, n, tp, fh, 697 RTM_NEWTFILTER, true); 698 goto errout; 699 default: 700 err = -EINVAL; 701 goto errout; 702 } 703 } 704 705 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, 706 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); 707 if (err == 0) { 708 if (tp_created) 709 tcf_chain_tp_insert(chain, &chain_info, tp); 710 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false); 711 } else { 712 if (tp_created) 713 tcf_proto_destroy(tp); 714 } 715 716 errout: 717 if (chain) 718 tcf_chain_put(chain); 719 if (err == -EAGAIN) 720 /* Replay the request. */ 721 goto replay; 722 return err; 723 } 724 725 struct tcf_dump_args { 726 struct tcf_walker w; 727 struct sk_buff *skb; 728 struct netlink_callback *cb; 729 }; 730 731 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) 732 { 733 struct tcf_dump_args *a = (void *)arg; 734 struct net *net = sock_net(a->skb->sk); 735 736 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid, 737 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, 738 RTM_NEWTFILTER); 739 } 740 741 static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb, 742 struct netlink_callback *cb, 743 long index_start, long *p_index) 744 { 745 struct net *net = sock_net(skb->sk); 746 struct tcmsg *tcm = nlmsg_data(cb->nlh); 747 struct tcf_dump_args arg; 748 struct tcf_proto *tp; 749 750 for (tp = rtnl_dereference(chain->filter_chain); 751 tp; tp = rtnl_dereference(tp->next), (*p_index)++) { 752 if (*p_index < index_start) 753 continue; 754 if (TC_H_MAJ(tcm->tcm_info) && 755 TC_H_MAJ(tcm->tcm_info) != tp->prio) 756 continue; 757 if (TC_H_MIN(tcm->tcm_info) && 758 TC_H_MIN(tcm->tcm_info) != tp->protocol) 759 continue; 760 if (*p_index > index_start) 761 memset(&cb->args[1], 0, 762 sizeof(cb->args) - sizeof(cb->args[0])); 763 if (cb->args[1] == 0) { 764 if (tcf_fill_node(net, skb, tp, 0, 765 NETLINK_CB(cb->skb).portid, 766 cb->nlh->nlmsg_seq, NLM_F_MULTI, 767 RTM_NEWTFILTER) <= 0) 768 return false; 769 770 cb->args[1] = 1; 771 } 772 if (!tp->ops->walk) 773 continue; 774 arg.w.fn = tcf_node_dump; 775 arg.skb = skb; 776 arg.cb = cb; 777 arg.w.stop = 0; 778 arg.w.skip = cb->args[1] - 1; 779 arg.w.count = 0; 780 tp->ops->walk(tp, &arg.w); 781 cb->args[1] = arg.w.count + 1; 782 if (arg.w.stop) 783 return false; 784 } 785 return true; 786 } 787 788 /* called with RTNL */ 789 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) 790 { 791 struct net *net = sock_net(skb->sk); 792 struct nlattr *tca[TCA_MAX + 1]; 793 struct net_device *dev; 794 struct Qdisc *q; 795 struct tcf_block *block; 796 struct tcf_chain *chain; 797 struct tcmsg *tcm = nlmsg_data(cb->nlh); 798 unsigned long cl = 0; 799 const struct Qdisc_class_ops *cops; 800 long index_start; 801 long index; 802 int err; 803 804 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 805 return skb->len; 806 807 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); 808 if (err) 809 return err; 810 811 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 812 if (!dev) 813 return skb->len; 814 815 if (!tcm->tcm_parent) 816 q = dev->qdisc; 817 else 818 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); 819 if (!q) 820 goto out; 821 cops = q->ops->cl_ops; 822 if (!cops) 823 goto out; 824 if (!cops->tcf_block) 825 goto out; 826 if (TC_H_MIN(tcm->tcm_parent)) { 827 cl = cops->find(q, tcm->tcm_parent); 828 if (cl == 0) 829 goto out; 830 } 831 block = cops->tcf_block(q, cl); 832 if (!block) 833 goto out; 834 835 index_start = cb->args[0]; 836 index = 0; 837 838 list_for_each_entry(chain, &block->chain_list, list) { 839 if (tca[TCA_CHAIN] && 840 nla_get_u32(tca[TCA_CHAIN]) != chain->index) 841 continue; 842 if (!tcf_chain_dump(chain, skb, cb, index_start, &index)) 843 break; 844 } 845 846 cb->args[0] = index; 847 848 out: 849 return skb->len; 850 } 851 852 void tcf_exts_destroy(struct tcf_exts *exts) 853 { 854 #ifdef CONFIG_NET_CLS_ACT 855 LIST_HEAD(actions); 856 857 tcf_exts_to_list(exts, &actions); 858 tcf_action_destroy(&actions, TCA_ACT_UNBIND); 859 kfree(exts->actions); 860 exts->nr_actions = 0; 861 #endif 862 } 863 EXPORT_SYMBOL(tcf_exts_destroy); 864 865 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, 866 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) 867 { 868 #ifdef CONFIG_NET_CLS_ACT 869 { 870 struct tc_action *act; 871 872 if (exts->police && tb[exts->police]) { 873 act = tcf_action_init_1(net, tp, tb[exts->police], 874 rate_tlv, "police", ovr, 875 TCA_ACT_BIND); 876 if (IS_ERR(act)) 877 return PTR_ERR(act); 878 879 act->type = exts->type = TCA_OLD_COMPAT; 880 exts->actions[0] = act; 881 exts->nr_actions = 1; 882 } else if (exts->action && tb[exts->action]) { 883 LIST_HEAD(actions); 884 int err, i = 0; 885 886 err = tcf_action_init(net, tp, tb[exts->action], 887 rate_tlv, NULL, ovr, TCA_ACT_BIND, 888 &actions); 889 if (err) 890 return err; 891 list_for_each_entry(act, &actions, list) 892 exts->actions[i++] = act; 893 exts->nr_actions = i; 894 } 895 } 896 #else 897 if ((exts->action && tb[exts->action]) || 898 (exts->police && tb[exts->police])) 899 return -EOPNOTSUPP; 900 #endif 901 902 return 0; 903 } 904 EXPORT_SYMBOL(tcf_exts_validate); 905 906 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) 907 { 908 #ifdef CONFIG_NET_CLS_ACT 909 struct tcf_exts old = *dst; 910 911 *dst = *src; 912 tcf_exts_destroy(&old); 913 #endif 914 } 915 EXPORT_SYMBOL(tcf_exts_change); 916 917 #ifdef CONFIG_NET_CLS_ACT 918 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) 919 { 920 if (exts->nr_actions == 0) 921 return NULL; 922 else 923 return exts->actions[0]; 924 } 925 #endif 926 927 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) 928 { 929 #ifdef CONFIG_NET_CLS_ACT 930 struct nlattr *nest; 931 932 if (exts->action && tcf_exts_has_actions(exts)) { 933 /* 934 * again for backward compatible mode - we want 935 * to work with both old and new modes of entering 936 * tc data even if iproute2 was newer - jhs 937 */ 938 if (exts->type != TCA_OLD_COMPAT) { 939 LIST_HEAD(actions); 940 941 nest = nla_nest_start(skb, exts->action); 942 if (nest == NULL) 943 goto nla_put_failure; 944 945 tcf_exts_to_list(exts, &actions); 946 if (tcf_action_dump(skb, &actions, 0, 0) < 0) 947 goto nla_put_failure; 948 nla_nest_end(skb, nest); 949 } else if (exts->police) { 950 struct tc_action *act = tcf_exts_first_act(exts); 951 nest = nla_nest_start(skb, exts->police); 952 if (nest == NULL || !act) 953 goto nla_put_failure; 954 if (tcf_action_dump_old(skb, act, 0, 0) < 0) 955 goto nla_put_failure; 956 nla_nest_end(skb, nest); 957 } 958 } 959 return 0; 960 961 nla_put_failure: 962 nla_nest_cancel(skb, nest); 963 return -1; 964 #else 965 return 0; 966 #endif 967 } 968 EXPORT_SYMBOL(tcf_exts_dump); 969 970 971 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) 972 { 973 #ifdef CONFIG_NET_CLS_ACT 974 struct tc_action *a = tcf_exts_first_act(exts); 975 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) 976 return -1; 977 #endif 978 return 0; 979 } 980 EXPORT_SYMBOL(tcf_exts_dump_stats); 981 982 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, 983 struct net_device **hw_dev) 984 { 985 #ifdef CONFIG_NET_CLS_ACT 986 const struct tc_action *a; 987 LIST_HEAD(actions); 988 989 if (!tcf_exts_has_actions(exts)) 990 return -EINVAL; 991 992 tcf_exts_to_list(exts, &actions); 993 list_for_each_entry(a, &actions, list) { 994 if (a->ops->get_dev) { 995 a->ops->get_dev(a, dev_net(dev), hw_dev); 996 break; 997 } 998 } 999 if (*hw_dev) 1000 return 0; 1001 #endif 1002 return -EOPNOTSUPP; 1003 } 1004 EXPORT_SYMBOL(tcf_exts_get_dev); 1005 1006 static int __init tc_filter_init(void) 1007 { 1008 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0); 1009 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0); 1010 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, 1011 tc_dump_tfilter, 0); 1012 1013 return 0; 1014 } 1015 1016 subsys_initcall(tc_filter_init); 1017