1 /* 2 * net/sched/act_api.c Packet action 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 * Author: Jamal Hadi Salim 10 * 11 * 12 */ 13 14 #include <linux/types.h> 15 #include <linux/kernel.h> 16 #include <linux/string.h> 17 #include <linux/errno.h> 18 #include <linux/slab.h> 19 #include <linux/skbuff.h> 20 #include <linux/init.h> 21 #include <linux/kmod.h> 22 #include <linux/err.h> 23 #include <linux/module.h> 24 #include <net/net_namespace.h> 25 #include <net/sock.h> 26 #include <net/sch_generic.h> 27 #include <net/pkt_cls.h> 28 #include <net/act_api.h> 29 #include <net/netlink.h> 30 31 static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp) 32 { 33 u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK; 34 35 if (!tp) 36 return -EINVAL; 37 a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true); 38 if (!a->goto_chain) 39 return -ENOMEM; 40 return 0; 41 } 42 43 static void tcf_action_goto_chain_fini(struct tc_action *a) 44 { 45 tcf_chain_put(a->goto_chain); 46 } 47 48 static void tcf_action_goto_chain_exec(const struct tc_action *a, 49 struct tcf_result *res) 50 { 51 const struct tcf_chain *chain = a->goto_chain; 52 53 res->goto_tp = rcu_dereference_bh(chain->filter_chain); 54 } 55 56 /* XXX: For standalone actions, we don't need a RCU grace period either, because 57 * actions are always connected to filters and filters are already destroyed in 58 * RCU callbacks, so after a RCU grace period actions are already disconnected 59 * from filters. Readers later can not find us. 60 */ 61 static void free_tcf(struct tc_action *p) 62 { 63 free_percpu(p->cpu_bstats); 64 free_percpu(p->cpu_qstats); 65 66 if (p->act_cookie) { 67 kfree(p->act_cookie->data); 68 kfree(p->act_cookie); 69 } 70 if (p->goto_chain) 71 tcf_action_goto_chain_fini(p); 72 73 kfree(p); 74 } 75 76 static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p) 77 { 78 spin_lock_bh(&idrinfo->lock); 79 idr_remove_ext(&idrinfo->action_idr, p->tcfa_index); 80 spin_unlock_bh(&idrinfo->lock); 81 gen_kill_estimator(&p->tcfa_rate_est); 82 free_tcf(p); 83 } 84 85 int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) 86 { 87 int ret = 0; 88 89 if (p) { 90 if (bind) 91 p->tcfa_bindcnt--; 92 else if (strict && p->tcfa_bindcnt > 0) 93 return -EPERM; 94 95 p->tcfa_refcnt--; 96 if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) { 97 if (p->ops->cleanup) 98 p->ops->cleanup(p, bind); 99 tcf_idr_remove(p->idrinfo, p); 100 ret = ACT_P_DELETED; 101 } 102 } 103 104 return ret; 105 } 106 EXPORT_SYMBOL(__tcf_idr_release); 107 108 static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 109 struct netlink_callback *cb) 110 { 111 int err = 0, index = -1, s_i = 0, n_i = 0; 112 u32 act_flags = cb->args[2]; 113 unsigned long jiffy_since = cb->args[3]; 114 struct nlattr *nest; 115 struct idr *idr = &idrinfo->action_idr; 116 struct tc_action *p; 117 unsigned long id = 1; 118 119 spin_lock_bh(&idrinfo->lock); 120 121 s_i = cb->args[0]; 122 123 idr_for_each_entry_ext(idr, p, id) { 124 index++; 125 if (index < s_i) 126 continue; 127 128 if (jiffy_since && 129 time_after(jiffy_since, 130 (unsigned long)p->tcfa_tm.lastuse)) 131 continue; 132 133 nest = nla_nest_start(skb, n_i); 134 if (!nest) 135 goto nla_put_failure; 136 err = tcf_action_dump_1(skb, p, 0, 0); 137 if (err < 0) { 138 index--; 139 nlmsg_trim(skb, nest); 140 goto done; 141 } 142 nla_nest_end(skb, nest); 143 n_i++; 144 if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && 145 n_i >= TCA_ACT_MAX_PRIO) 146 goto done; 147 } 148 done: 149 if (index >= 0) 150 cb->args[0] = index + 1; 151 152 spin_unlock_bh(&idrinfo->lock); 153 if (n_i) { 154 if (act_flags & TCA_FLAG_LARGE_DUMP_ON) 155 cb->args[1] = n_i; 156 } 157 return n_i; 158 159 nla_put_failure: 160 nla_nest_cancel(skb, nest); 161 goto done; 162 } 163 164 static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, 165 const struct tc_action_ops *ops) 166 { 167 struct nlattr *nest; 168 int n_i = 0; 169 int ret = -EINVAL; 170 struct idr *idr = &idrinfo->action_idr; 171 struct tc_action *p; 172 unsigned long id = 1; 173 174 nest = nla_nest_start(skb, 0); 175 if (nest == NULL) 176 goto nla_put_failure; 177 if (nla_put_string(skb, TCA_KIND, ops->kind)) 178 goto nla_put_failure; 179 180 idr_for_each_entry_ext(idr, p, id) { 181 ret = __tcf_idr_release(p, false, true); 182 if (ret == ACT_P_DELETED) { 183 module_put(ops->owner); 184 n_i++; 185 } else if (ret < 0) { 186 goto nla_put_failure; 187 } 188 } 189 if (nla_put_u32(skb, TCA_FCNT, n_i)) 190 goto nla_put_failure; 191 nla_nest_end(skb, nest); 192 193 return n_i; 194 nla_put_failure: 195 nla_nest_cancel(skb, nest); 196 return ret; 197 } 198 199 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, 200 struct netlink_callback *cb, int type, 201 const struct tc_action_ops *ops) 202 { 203 struct tcf_idrinfo *idrinfo = tn->idrinfo; 204 205 if (type == RTM_DELACTION) { 206 return tcf_del_walker(idrinfo, skb, ops); 207 } else if (type == RTM_GETACTION) { 208 return tcf_dump_walker(idrinfo, skb, cb); 209 } else { 210 WARN(1, "tcf_generic_walker: unknown action %d\n", type); 211 return -EINVAL; 212 } 213 } 214 EXPORT_SYMBOL(tcf_generic_walker); 215 216 static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo) 217 { 218 struct tc_action *p = NULL; 219 220 spin_lock_bh(&idrinfo->lock); 221 p = idr_find_ext(&idrinfo->action_idr, index); 222 spin_unlock_bh(&idrinfo->lock); 223 224 return p; 225 } 226 227 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) 228 { 229 struct tcf_idrinfo *idrinfo = tn->idrinfo; 230 struct tc_action *p = tcf_idr_lookup(index, idrinfo); 231 232 if (p) { 233 *a = p; 234 return 1; 235 } 236 return 0; 237 } 238 EXPORT_SYMBOL(tcf_idr_search); 239 240 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a, 241 int bind) 242 { 243 struct tcf_idrinfo *idrinfo = tn->idrinfo; 244 struct tc_action *p = tcf_idr_lookup(index, idrinfo); 245 246 if (index && p) { 247 if (bind) 248 p->tcfa_bindcnt++; 249 p->tcfa_refcnt++; 250 *a = p; 251 return true; 252 } 253 return false; 254 } 255 EXPORT_SYMBOL(tcf_idr_check); 256 257 void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est) 258 { 259 if (est) 260 gen_kill_estimator(&a->tcfa_rate_est); 261 free_tcf(a); 262 } 263 EXPORT_SYMBOL(tcf_idr_cleanup); 264 265 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, 266 struct tc_action **a, const struct tc_action_ops *ops, 267 int bind, bool cpustats) 268 { 269 struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); 270 struct tcf_idrinfo *idrinfo = tn->idrinfo; 271 struct idr *idr = &idrinfo->action_idr; 272 int err = -ENOMEM; 273 unsigned long idr_index; 274 275 if (unlikely(!p)) 276 return -ENOMEM; 277 p->tcfa_refcnt = 1; 278 if (bind) 279 p->tcfa_bindcnt = 1; 280 281 if (cpustats) { 282 p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); 283 if (!p->cpu_bstats) { 284 err1: 285 kfree(p); 286 return err; 287 } 288 p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); 289 if (!p->cpu_qstats) { 290 err2: 291 free_percpu(p->cpu_bstats); 292 goto err1; 293 } 294 } 295 spin_lock_init(&p->tcfa_lock); 296 /* user doesn't specify an index */ 297 if (!index) { 298 idr_preload(GFP_KERNEL); 299 spin_lock_bh(&idrinfo->lock); 300 err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0, 301 GFP_ATOMIC); 302 spin_unlock_bh(&idrinfo->lock); 303 idr_preload_end(); 304 if (err) { 305 err3: 306 free_percpu(p->cpu_qstats); 307 goto err2; 308 } 309 p->tcfa_index = idr_index; 310 } else { 311 idr_preload(GFP_KERNEL); 312 spin_lock_bh(&idrinfo->lock); 313 err = idr_alloc_ext(idr, NULL, NULL, index, index + 1, 314 GFP_ATOMIC); 315 spin_unlock_bh(&idrinfo->lock); 316 idr_preload_end(); 317 if (err) 318 goto err3; 319 p->tcfa_index = index; 320 } 321 322 p->tcfa_tm.install = jiffies; 323 p->tcfa_tm.lastuse = jiffies; 324 p->tcfa_tm.firstuse = 0; 325 if (est) { 326 err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, 327 &p->tcfa_rate_est, 328 &p->tcfa_lock, NULL, est); 329 if (err) { 330 goto err3; 331 } 332 } 333 334 p->idrinfo = idrinfo; 335 p->ops = ops; 336 INIT_LIST_HEAD(&p->list); 337 *a = p; 338 return 0; 339 } 340 EXPORT_SYMBOL(tcf_idr_create); 341 342 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) 343 { 344 struct tcf_idrinfo *idrinfo = tn->idrinfo; 345 346 spin_lock_bh(&idrinfo->lock); 347 idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index); 348 spin_unlock_bh(&idrinfo->lock); 349 } 350 EXPORT_SYMBOL(tcf_idr_insert); 351 352 void tcf_idrinfo_destroy(const struct tc_action_ops *ops, 353 struct tcf_idrinfo *idrinfo) 354 { 355 struct idr *idr = &idrinfo->action_idr; 356 struct tc_action *p; 357 int ret; 358 unsigned long id = 1; 359 360 idr_for_each_entry_ext(idr, p, id) { 361 ret = __tcf_idr_release(p, false, true); 362 if (ret == ACT_P_DELETED) 363 module_put(ops->owner); 364 else if (ret < 0) 365 return; 366 } 367 idr_destroy(&idrinfo->action_idr); 368 } 369 EXPORT_SYMBOL(tcf_idrinfo_destroy); 370 371 static LIST_HEAD(act_base); 372 static DEFINE_RWLOCK(act_mod_lock); 373 374 int tcf_register_action(struct tc_action_ops *act, 375 struct pernet_operations *ops) 376 { 377 struct tc_action_ops *a; 378 int ret; 379 380 if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) 381 return -EINVAL; 382 383 /* We have to register pernet ops before making the action ops visible, 384 * otherwise tcf_action_init_1() could get a partially initialized 385 * netns. 386 */ 387 ret = register_pernet_subsys(ops); 388 if (ret) 389 return ret; 390 391 write_lock(&act_mod_lock); 392 list_for_each_entry(a, &act_base, head) { 393 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { 394 write_unlock(&act_mod_lock); 395 unregister_pernet_subsys(ops); 396 return -EEXIST; 397 } 398 } 399 list_add_tail(&act->head, &act_base); 400 write_unlock(&act_mod_lock); 401 402 return 0; 403 } 404 EXPORT_SYMBOL(tcf_register_action); 405 406 int tcf_unregister_action(struct tc_action_ops *act, 407 struct pernet_operations *ops) 408 { 409 struct tc_action_ops *a; 410 int err = -ENOENT; 411 412 write_lock(&act_mod_lock); 413 list_for_each_entry(a, &act_base, head) { 414 if (a == act) { 415 list_del(&act->head); 416 err = 0; 417 break; 418 } 419 } 420 write_unlock(&act_mod_lock); 421 if (!err) 422 unregister_pernet_subsys(ops); 423 return err; 424 } 425 EXPORT_SYMBOL(tcf_unregister_action); 426 427 /* lookup by name */ 428 static struct tc_action_ops *tc_lookup_action_n(char *kind) 429 { 430 struct tc_action_ops *a, *res = NULL; 431 432 if (kind) { 433 read_lock(&act_mod_lock); 434 list_for_each_entry(a, &act_base, head) { 435 if (strcmp(kind, a->kind) == 0) { 436 if (try_module_get(a->owner)) 437 res = a; 438 break; 439 } 440 } 441 read_unlock(&act_mod_lock); 442 } 443 return res; 444 } 445 446 /* lookup by nlattr */ 447 static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) 448 { 449 struct tc_action_ops *a, *res = NULL; 450 451 if (kind) { 452 read_lock(&act_mod_lock); 453 list_for_each_entry(a, &act_base, head) { 454 if (nla_strcmp(kind, a->kind) == 0) { 455 if (try_module_get(a->owner)) 456 res = a; 457 break; 458 } 459 } 460 read_unlock(&act_mod_lock); 461 } 462 return res; 463 } 464 465 /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ 466 #define TCA_ACT_MAX_PRIO_MASK 0x1FF 467 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, 468 int nr_actions, struct tcf_result *res) 469 { 470 u32 jmp_prgcnt = 0; 471 u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ 472 int i; 473 int ret = TC_ACT_OK; 474 475 if (skb_skip_tc_classify(skb)) 476 return TC_ACT_OK; 477 478 restart_act_graph: 479 for (i = 0; i < nr_actions; i++) { 480 const struct tc_action *a = actions[i]; 481 482 if (jmp_prgcnt > 0) { 483 jmp_prgcnt -= 1; 484 continue; 485 } 486 repeat: 487 ret = a->ops->act(skb, a, res); 488 if (ret == TC_ACT_REPEAT) 489 goto repeat; /* we need a ttl - JHS */ 490 491 if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { 492 jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; 493 if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { 494 /* faulty opcode, stop pipeline */ 495 return TC_ACT_OK; 496 } else { 497 jmp_ttl -= 1; 498 if (jmp_ttl > 0) 499 goto restart_act_graph; 500 else /* faulty graph, stop pipeline */ 501 return TC_ACT_OK; 502 } 503 } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { 504 tcf_action_goto_chain_exec(a, res); 505 } 506 507 if (ret != TC_ACT_PIPE) 508 break; 509 } 510 511 return ret; 512 } 513 EXPORT_SYMBOL(tcf_action_exec); 514 515 int tcf_action_destroy(struct list_head *actions, int bind) 516 { 517 const struct tc_action_ops *ops; 518 struct tc_action *a, *tmp; 519 int ret = 0; 520 521 list_for_each_entry_safe(a, tmp, actions, list) { 522 ops = a->ops; 523 ret = __tcf_idr_release(a, bind, true); 524 if (ret == ACT_P_DELETED) 525 module_put(ops->owner); 526 else if (ret < 0) 527 return ret; 528 } 529 return ret; 530 } 531 532 int 533 tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 534 { 535 return a->ops->dump(skb, a, bind, ref); 536 } 537 538 int 539 tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) 540 { 541 int err = -EINVAL; 542 unsigned char *b = skb_tail_pointer(skb); 543 struct nlattr *nest; 544 545 if (nla_put_string(skb, TCA_KIND, a->ops->kind)) 546 goto nla_put_failure; 547 if (tcf_action_copy_stats(skb, a, 0)) 548 goto nla_put_failure; 549 if (a->act_cookie) { 550 if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len, 551 a->act_cookie->data)) 552 goto nla_put_failure; 553 } 554 555 nest = nla_nest_start(skb, TCA_OPTIONS); 556 if (nest == NULL) 557 goto nla_put_failure; 558 err = tcf_action_dump_old(skb, a, bind, ref); 559 if (err > 0) { 560 nla_nest_end(skb, nest); 561 return err; 562 } 563 564 nla_put_failure: 565 nlmsg_trim(skb, b); 566 return -1; 567 } 568 EXPORT_SYMBOL(tcf_action_dump_1); 569 570 int tcf_action_dump(struct sk_buff *skb, struct list_head *actions, 571 int bind, int ref) 572 { 573 struct tc_action *a; 574 int err = -EINVAL; 575 struct nlattr *nest; 576 577 list_for_each_entry(a, actions, list) { 578 nest = nla_nest_start(skb, a->order); 579 if (nest == NULL) 580 goto nla_put_failure; 581 err = tcf_action_dump_1(skb, a, bind, ref); 582 if (err < 0) 583 goto errout; 584 nla_nest_end(skb, nest); 585 } 586 587 return 0; 588 589 nla_put_failure: 590 err = -EINVAL; 591 errout: 592 nla_nest_cancel(skb, nest); 593 return err; 594 } 595 596 static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) 597 { 598 struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); 599 if (!c) 600 return NULL; 601 602 c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); 603 if (!c->data) { 604 kfree(c); 605 return NULL; 606 } 607 c->len = nla_len(tb[TCA_ACT_COOKIE]); 608 609 return c; 610 } 611 612 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, 613 struct nlattr *nla, struct nlattr *est, 614 char *name, int ovr, int bind) 615 { 616 struct tc_action *a; 617 struct tc_action_ops *a_o; 618 struct tc_cookie *cookie = NULL; 619 char act_name[IFNAMSIZ]; 620 struct nlattr *tb[TCA_ACT_MAX + 1]; 621 struct nlattr *kind; 622 int err; 623 624 if (name == NULL) { 625 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); 626 if (err < 0) 627 goto err_out; 628 err = -EINVAL; 629 kind = tb[TCA_ACT_KIND]; 630 if (kind == NULL) 631 goto err_out; 632 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) 633 goto err_out; 634 if (tb[TCA_ACT_COOKIE]) { 635 int cklen = nla_len(tb[TCA_ACT_COOKIE]); 636 637 if (cklen > TC_COOKIE_MAX_SIZE) 638 goto err_out; 639 640 cookie = nla_memdup_cookie(tb); 641 if (!cookie) { 642 err = -ENOMEM; 643 goto err_out; 644 } 645 } 646 } else { 647 err = -EINVAL; 648 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) 649 goto err_out; 650 } 651 652 a_o = tc_lookup_action_n(act_name); 653 if (a_o == NULL) { 654 #ifdef CONFIG_MODULES 655 rtnl_unlock(); 656 request_module("act_%s", act_name); 657 rtnl_lock(); 658 659 a_o = tc_lookup_action_n(act_name); 660 661 /* We dropped the RTNL semaphore in order to 662 * perform the module load. So, even if we 663 * succeeded in loading the module we have to 664 * tell the caller to replay the request. We 665 * indicate this using -EAGAIN. 666 */ 667 if (a_o != NULL) { 668 err = -EAGAIN; 669 goto err_mod; 670 } 671 #endif 672 err = -ENOENT; 673 goto err_out; 674 } 675 676 /* backward compatibility for policer */ 677 if (name == NULL) 678 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind); 679 else 680 err = a_o->init(net, nla, est, &a, ovr, bind); 681 if (err < 0) 682 goto err_mod; 683 684 if (name == NULL && tb[TCA_ACT_COOKIE]) { 685 if (a->act_cookie) { 686 kfree(a->act_cookie->data); 687 kfree(a->act_cookie); 688 } 689 a->act_cookie = cookie; 690 } 691 692 /* module count goes up only when brand new policy is created 693 * if it exists and is only bound to in a_o->init() then 694 * ACT_P_CREATED is not returned (a zero is). 695 */ 696 if (err != ACT_P_CREATED) 697 module_put(a_o->owner); 698 699 if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { 700 err = tcf_action_goto_chain_init(a, tp); 701 if (err) { 702 LIST_HEAD(actions); 703 704 list_add_tail(&a->list, &actions); 705 tcf_action_destroy(&actions, bind); 706 return ERR_PTR(err); 707 } 708 } 709 710 return a; 711 712 err_mod: 713 module_put(a_o->owner); 714 err_out: 715 if (cookie) { 716 kfree(cookie->data); 717 kfree(cookie); 718 } 719 return ERR_PTR(err); 720 } 721 722 static void cleanup_a(struct list_head *actions, int ovr) 723 { 724 struct tc_action *a; 725 726 if (!ovr) 727 return; 728 729 list_for_each_entry(a, actions, list) 730 a->tcfa_refcnt--; 731 } 732 733 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, 734 struct nlattr *est, char *name, int ovr, int bind, 735 struct list_head *actions) 736 { 737 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 738 struct tc_action *act; 739 int err; 740 int i; 741 742 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); 743 if (err < 0) 744 return err; 745 746 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 747 act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind); 748 if (IS_ERR(act)) { 749 err = PTR_ERR(act); 750 goto err; 751 } 752 act->order = i; 753 if (ovr) 754 act->tcfa_refcnt++; 755 list_add_tail(&act->list, actions); 756 } 757 758 /* Remove the temp refcnt which was necessary to protect against 759 * destroying an existing action which was being replaced 760 */ 761 cleanup_a(actions, ovr); 762 return 0; 763 764 err: 765 tcf_action_destroy(actions, bind); 766 return err; 767 } 768 769 int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, 770 int compat_mode) 771 { 772 int err = 0; 773 struct gnet_dump d; 774 775 if (p == NULL) 776 goto errout; 777 778 /* compat_mode being true specifies a call that is supposed 779 * to add additional backward compatibility statistic TLVs. 780 */ 781 if (compat_mode) { 782 if (p->type == TCA_OLD_COMPAT) 783 err = gnet_stats_start_copy_compat(skb, 0, 784 TCA_STATS, 785 TCA_XSTATS, 786 &p->tcfa_lock, &d, 787 TCA_PAD); 788 else 789 return 0; 790 } else 791 err = gnet_stats_start_copy(skb, TCA_ACT_STATS, 792 &p->tcfa_lock, &d, TCA_ACT_PAD); 793 794 if (err < 0) 795 goto errout; 796 797 if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || 798 gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || 799 gnet_stats_copy_queue(&d, p->cpu_qstats, 800 &p->tcfa_qstats, 801 p->tcfa_qstats.qlen) < 0) 802 goto errout; 803 804 if (gnet_stats_finish_copy(&d) < 0) 805 goto errout; 806 807 return 0; 808 809 errout: 810 return -1; 811 } 812 813 static int tca_get_fill(struct sk_buff *skb, struct list_head *actions, 814 u32 portid, u32 seq, u16 flags, int event, int bind, 815 int ref) 816 { 817 struct tcamsg *t; 818 struct nlmsghdr *nlh; 819 unsigned char *b = skb_tail_pointer(skb); 820 struct nlattr *nest; 821 822 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); 823 if (!nlh) 824 goto out_nlmsg_trim; 825 t = nlmsg_data(nlh); 826 t->tca_family = AF_UNSPEC; 827 t->tca__pad1 = 0; 828 t->tca__pad2 = 0; 829 830 nest = nla_nest_start(skb, TCA_ACT_TAB); 831 if (nest == NULL) 832 goto out_nlmsg_trim; 833 834 if (tcf_action_dump(skb, actions, bind, ref) < 0) 835 goto out_nlmsg_trim; 836 837 nla_nest_end(skb, nest); 838 839 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 840 return skb->len; 841 842 out_nlmsg_trim: 843 nlmsg_trim(skb, b); 844 return -1; 845 } 846 847 static int 848 tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, 849 struct list_head *actions, int event) 850 { 851 struct sk_buff *skb; 852 853 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 854 if (!skb) 855 return -ENOBUFS; 856 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 857 0, 0) <= 0) { 858 kfree_skb(skb); 859 return -EINVAL; 860 } 861 862 return rtnl_unicast(skb, net, portid); 863 } 864 865 static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, 866 struct nlmsghdr *n, u32 portid) 867 { 868 struct nlattr *tb[TCA_ACT_MAX + 1]; 869 const struct tc_action_ops *ops; 870 struct tc_action *a; 871 int index; 872 int err; 873 874 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); 875 if (err < 0) 876 goto err_out; 877 878 err = -EINVAL; 879 if (tb[TCA_ACT_INDEX] == NULL || 880 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) 881 goto err_out; 882 index = nla_get_u32(tb[TCA_ACT_INDEX]); 883 884 err = -EINVAL; 885 ops = tc_lookup_action(tb[TCA_ACT_KIND]); 886 if (!ops) /* could happen in batch of actions */ 887 goto err_out; 888 err = -ENOENT; 889 if (ops->lookup(net, &a, index) == 0) 890 goto err_mod; 891 892 module_put(ops->owner); 893 return a; 894 895 err_mod: 896 module_put(ops->owner); 897 err_out: 898 return ERR_PTR(err); 899 } 900 901 static int tca_action_flush(struct net *net, struct nlattr *nla, 902 struct nlmsghdr *n, u32 portid) 903 { 904 struct sk_buff *skb; 905 unsigned char *b; 906 struct nlmsghdr *nlh; 907 struct tcamsg *t; 908 struct netlink_callback dcb; 909 struct nlattr *nest; 910 struct nlattr *tb[TCA_ACT_MAX + 1]; 911 const struct tc_action_ops *ops; 912 struct nlattr *kind; 913 int err = -ENOMEM; 914 915 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 916 if (!skb) { 917 pr_debug("tca_action_flush: failed skb alloc\n"); 918 return err; 919 } 920 921 b = skb_tail_pointer(skb); 922 923 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); 924 if (err < 0) 925 goto err_out; 926 927 err = -EINVAL; 928 kind = tb[TCA_ACT_KIND]; 929 ops = tc_lookup_action(kind); 930 if (!ops) /*some idjot trying to flush unknown action */ 931 goto err_out; 932 933 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, 934 sizeof(*t), 0); 935 if (!nlh) 936 goto out_module_put; 937 t = nlmsg_data(nlh); 938 t->tca_family = AF_UNSPEC; 939 t->tca__pad1 = 0; 940 t->tca__pad2 = 0; 941 942 nest = nla_nest_start(skb, TCA_ACT_TAB); 943 if (nest == NULL) 944 goto out_module_put; 945 946 err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops); 947 if (err <= 0) 948 goto out_module_put; 949 950 nla_nest_end(skb, nest); 951 952 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 953 nlh->nlmsg_flags |= NLM_F_ROOT; 954 module_put(ops->owner); 955 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 956 n->nlmsg_flags & NLM_F_ECHO); 957 if (err > 0) 958 return 0; 959 960 return err; 961 962 out_module_put: 963 module_put(ops->owner); 964 err_out: 965 kfree_skb(skb); 966 return err; 967 } 968 969 static int 970 tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, 971 u32 portid) 972 { 973 int ret; 974 struct sk_buff *skb; 975 976 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 977 if (!skb) 978 return -ENOBUFS; 979 980 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, 981 0, 1) <= 0) { 982 kfree_skb(skb); 983 return -EINVAL; 984 } 985 986 /* now do the delete */ 987 ret = tcf_action_destroy(actions, 0); 988 if (ret < 0) { 989 kfree_skb(skb); 990 return ret; 991 } 992 993 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 994 n->nlmsg_flags & NLM_F_ECHO); 995 if (ret > 0) 996 return 0; 997 return ret; 998 } 999 1000 static int 1001 tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, 1002 u32 portid, int event) 1003 { 1004 int i, ret; 1005 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 1006 struct tc_action *act; 1007 LIST_HEAD(actions); 1008 1009 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); 1010 if (ret < 0) 1011 return ret; 1012 1013 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { 1014 if (tb[1] != NULL) 1015 return tca_action_flush(net, tb[1], n, portid); 1016 else 1017 return -EINVAL; 1018 } 1019 1020 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { 1021 act = tcf_action_get_1(net, tb[i], n, portid); 1022 if (IS_ERR(act)) { 1023 ret = PTR_ERR(act); 1024 goto err; 1025 } 1026 act->order = i; 1027 list_add_tail(&act->list, &actions); 1028 } 1029 1030 if (event == RTM_GETACTION) 1031 ret = tcf_get_notify(net, portid, n, &actions, event); 1032 else { /* delete */ 1033 ret = tcf_del_notify(net, n, &actions, portid); 1034 if (ret) 1035 goto err; 1036 return ret; 1037 } 1038 err: 1039 if (event != RTM_GETACTION) 1040 tcf_action_destroy(&actions, 0); 1041 return ret; 1042 } 1043 1044 static int 1045 tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, 1046 u32 portid) 1047 { 1048 struct sk_buff *skb; 1049 int err = 0; 1050 1051 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1052 if (!skb) 1053 return -ENOBUFS; 1054 1055 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, 1056 RTM_NEWACTION, 0, 0) <= 0) { 1057 kfree_skb(skb); 1058 return -EINVAL; 1059 } 1060 1061 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1062 n->nlmsg_flags & NLM_F_ECHO); 1063 if (err > 0) 1064 err = 0; 1065 return err; 1066 } 1067 1068 static int tcf_action_add(struct net *net, struct nlattr *nla, 1069 struct nlmsghdr *n, u32 portid, int ovr) 1070 { 1071 int ret = 0; 1072 LIST_HEAD(actions); 1073 1074 ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions); 1075 if (ret) 1076 return ret; 1077 1078 return tcf_add_notify(net, n, &actions, portid); 1079 } 1080 1081 static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; 1082 static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { 1083 [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, 1084 .validation_data = &tcaa_root_flags_allowed }, 1085 [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, 1086 }; 1087 1088 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, 1089 struct netlink_ext_ack *extack) 1090 { 1091 struct net *net = sock_net(skb->sk); 1092 struct nlattr *tca[TCA_ROOT_MAX + 1]; 1093 u32 portid = skb ? NETLINK_CB(skb).portid : 0; 1094 int ret = 0, ovr = 0; 1095 1096 if ((n->nlmsg_type != RTM_GETACTION) && 1097 !netlink_capable(skb, CAP_NET_ADMIN)) 1098 return -EPERM; 1099 1100 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, 1101 extack); 1102 if (ret < 0) 1103 return ret; 1104 1105 if (tca[TCA_ACT_TAB] == NULL) { 1106 pr_notice("tc_ctl_action: received NO action attribs\n"); 1107 return -EINVAL; 1108 } 1109 1110 /* n->nlmsg_flags & NLM_F_CREATE */ 1111 switch (n->nlmsg_type) { 1112 case RTM_NEWACTION: 1113 /* we are going to assume all other flags 1114 * imply create only if it doesn't exist 1115 * Note that CREATE | EXCL implies that 1116 * but since we want avoid ambiguity (eg when flags 1117 * is zero) then just set this 1118 */ 1119 if (n->nlmsg_flags & NLM_F_REPLACE) 1120 ovr = 1; 1121 replay: 1122 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); 1123 if (ret == -EAGAIN) 1124 goto replay; 1125 break; 1126 case RTM_DELACTION: 1127 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 1128 portid, RTM_DELACTION); 1129 break; 1130 case RTM_GETACTION: 1131 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, 1132 portid, RTM_GETACTION); 1133 break; 1134 default: 1135 BUG(); 1136 } 1137 1138 return ret; 1139 } 1140 1141 static struct nlattr *find_dump_kind(struct nlattr **nla) 1142 { 1143 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; 1144 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; 1145 struct nlattr *kind; 1146 1147 tb1 = nla[TCA_ACT_TAB]; 1148 if (tb1 == NULL) 1149 return NULL; 1150 1151 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), 1152 NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) 1153 return NULL; 1154 1155 if (tb[1] == NULL) 1156 return NULL; 1157 if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0) 1158 return NULL; 1159 kind = tb2[TCA_ACT_KIND]; 1160 1161 return kind; 1162 } 1163 1164 static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) 1165 { 1166 struct net *net = sock_net(skb->sk); 1167 struct nlmsghdr *nlh; 1168 unsigned char *b = skb_tail_pointer(skb); 1169 struct nlattr *nest; 1170 struct tc_action_ops *a_o; 1171 int ret = 0; 1172 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); 1173 struct nlattr *tb[TCA_ROOT_MAX + 1]; 1174 struct nlattr *count_attr = NULL; 1175 unsigned long jiffy_since = 0; 1176 struct nlattr *kind = NULL; 1177 struct nla_bitfield32 bf; 1178 u32 msecs_since = 0; 1179 u32 act_count = 0; 1180 1181 ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, 1182 tcaa_policy, NULL); 1183 if (ret < 0) 1184 return ret; 1185 1186 kind = find_dump_kind(tb); 1187 if (kind == NULL) { 1188 pr_info("tc_dump_action: action bad kind\n"); 1189 return 0; 1190 } 1191 1192 a_o = tc_lookup_action(kind); 1193 if (a_o == NULL) 1194 return 0; 1195 1196 cb->args[2] = 0; 1197 if (tb[TCA_ROOT_FLAGS]) { 1198 bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); 1199 cb->args[2] = bf.value; 1200 } 1201 1202 if (tb[TCA_ROOT_TIME_DELTA]) { 1203 msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); 1204 } 1205 1206 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1207 cb->nlh->nlmsg_type, sizeof(*t), 0); 1208 if (!nlh) 1209 goto out_module_put; 1210 1211 if (msecs_since) 1212 jiffy_since = jiffies - msecs_to_jiffies(msecs_since); 1213 1214 t = nlmsg_data(nlh); 1215 t->tca_family = AF_UNSPEC; 1216 t->tca__pad1 = 0; 1217 t->tca__pad2 = 0; 1218 cb->args[3] = jiffy_since; 1219 count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); 1220 if (!count_attr) 1221 goto out_module_put; 1222 1223 nest = nla_nest_start(skb, TCA_ACT_TAB); 1224 if (nest == NULL) 1225 goto out_module_put; 1226 1227 ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o); 1228 if (ret < 0) 1229 goto out_module_put; 1230 1231 if (ret > 0) { 1232 nla_nest_end(skb, nest); 1233 ret = skb->len; 1234 act_count = cb->args[1]; 1235 memcpy(nla_data(count_attr), &act_count, sizeof(u32)); 1236 cb->args[1] = 0; 1237 } else 1238 nlmsg_trim(skb, b); 1239 1240 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 1241 if (NETLINK_CB(cb->skb).portid && ret) 1242 nlh->nlmsg_flags |= NLM_F_MULTI; 1243 module_put(a_o->owner); 1244 return skb->len; 1245 1246 out_module_put: 1247 module_put(a_o->owner); 1248 nlmsg_trim(skb, b); 1249 return skb->len; 1250 } 1251 1252 static int __init tc_action_init(void) 1253 { 1254 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); 1255 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); 1256 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, 1257 0); 1258 1259 return 0; 1260 } 1261 1262 subsys_initcall(tc_action_init); 1263