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