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