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