linux/net/sched/cls_fw.c
<<
>>
Prefs
   1/*
   2 * net/sched/cls_fw.c   Classifier mapping ipchains' fwmark to traffic class.
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10 *
  11 * Changes:
  12 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
  13 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
  14 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
  15 *
  16 * JHS: We should remove the CONFIG_NET_CLS_IND from here
  17 * eventually when the meta match extension is made available
  18 *
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/slab.h>
  23#include <linux/types.h>
  24#include <linux/kernel.h>
  25#include <linux/string.h>
  26#include <linux/errno.h>
  27#include <linux/skbuff.h>
  28#include <net/netlink.h>
  29#include <net/act_api.h>
  30#include <net/pkt_cls.h>
  31
  32#define HTSIZE 256
  33
  34struct fw_head {
  35        u32                     mask;
  36        struct fw_filter __rcu  *ht[HTSIZE];
  37        struct rcu_head         rcu;
  38};
  39
  40struct fw_filter {
  41        struct fw_filter __rcu  *next;
  42        u32                     id;
  43        struct tcf_result       res;
  44#ifdef CONFIG_NET_CLS_IND
  45        int                     ifindex;
  46#endif /* CONFIG_NET_CLS_IND */
  47        struct tcf_exts         exts;
  48        struct tcf_proto        *tp;
  49        struct rcu_head         rcu;
  50};
  51
  52static u32 fw_hash(u32 handle)
  53{
  54        handle ^= (handle >> 16);
  55        handle ^= (handle >> 8);
  56        return handle % HTSIZE;
  57}
  58
  59static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  60                       struct tcf_result *res)
  61{
  62        struct fw_head *head = rcu_dereference_bh(tp->root);
  63        struct fw_filter *f;
  64        int r;
  65        u32 id = skb->mark;
  66
  67        if (head != NULL) {
  68                id &= head->mask;
  69
  70                for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
  71                     f = rcu_dereference_bh(f->next)) {
  72                        if (f->id == id) {
  73                                *res = f->res;
  74#ifdef CONFIG_NET_CLS_IND
  75                                if (!tcf_match_indev(skb, f->ifindex))
  76                                        continue;
  77#endif /* CONFIG_NET_CLS_IND */
  78                                r = tcf_exts_exec(skb, &f->exts, res);
  79                                if (r < 0)
  80                                        continue;
  81
  82                                return r;
  83                        }
  84                }
  85        } else {
  86                /* old method */
  87                if (id && (TC_H_MAJ(id) == 0 ||
  88                           !(TC_H_MAJ(id ^ tp->q->handle)))) {
  89                        res->classid = id;
  90                        res->class = 0;
  91                        return 0;
  92                }
  93        }
  94
  95        return -1;
  96}
  97
  98static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
  99{
 100        struct fw_head *head = rtnl_dereference(tp->root);
 101        struct fw_filter *f;
 102
 103        if (head == NULL)
 104                return 0;
 105
 106        f = rtnl_dereference(head->ht[fw_hash(handle)]);
 107        for (; f; f = rtnl_dereference(f->next)) {
 108                if (f->id == handle)
 109                        return (unsigned long)f;
 110        }
 111        return 0;
 112}
 113
 114static int fw_init(struct tcf_proto *tp)
 115{
 116        return 0;
 117}
 118
 119static void fw_delete_filter(struct rcu_head *head)
 120{
 121        struct fw_filter *f = container_of(head, struct fw_filter, rcu);
 122
 123        tcf_exts_destroy(&f->exts);
 124        kfree(f);
 125}
 126
 127static bool fw_destroy(struct tcf_proto *tp, bool force)
 128{
 129        struct fw_head *head = rtnl_dereference(tp->root);
 130        struct fw_filter *f;
 131        int h;
 132
 133        if (head == NULL)
 134                return true;
 135
 136        if (!force) {
 137                for (h = 0; h < HTSIZE; h++)
 138                        if (rcu_access_pointer(head->ht[h]))
 139                                return false;
 140        }
 141
 142        for (h = 0; h < HTSIZE; h++) {
 143                while ((f = rtnl_dereference(head->ht[h])) != NULL) {
 144                        RCU_INIT_POINTER(head->ht[h],
 145                                         rtnl_dereference(f->next));
 146                        tcf_unbind_filter(tp, &f->res);
 147                        call_rcu(&f->rcu, fw_delete_filter);
 148                }
 149        }
 150        RCU_INIT_POINTER(tp->root, NULL);
 151        kfree_rcu(head, rcu);
 152        return true;
 153}
 154
 155static int fw_delete(struct tcf_proto *tp, unsigned long arg)
 156{
 157        struct fw_head *head = rtnl_dereference(tp->root);
 158        struct fw_filter *f = (struct fw_filter *)arg;
 159        struct fw_filter __rcu **fp;
 160        struct fw_filter *pfp;
 161
 162        if (head == NULL || f == NULL)
 163                goto out;
 164
 165        fp = &head->ht[fw_hash(f->id)];
 166
 167        for (pfp = rtnl_dereference(*fp); pfp;
 168             fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
 169                if (pfp == f) {
 170                        RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
 171                        tcf_unbind_filter(tp, &f->res);
 172                        call_rcu(&f->rcu, fw_delete_filter);
 173                        return 0;
 174                }
 175        }
 176out:
 177        return -EINVAL;
 178}
 179
 180static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
 181        [TCA_FW_CLASSID]        = { .type = NLA_U32 },
 182        [TCA_FW_INDEV]          = { .type = NLA_STRING, .len = IFNAMSIZ },
 183        [TCA_FW_MASK]           = { .type = NLA_U32 },
 184};
 185
 186static int
 187fw_change_attrs(struct net *net, struct tcf_proto *tp, struct fw_filter *f,
 188                struct nlattr **tb, struct nlattr **tca, unsigned long base,
 189                bool ovr)
 190{
 191        struct fw_head *head = rtnl_dereference(tp->root);
 192        struct tcf_exts e;
 193        u32 mask;
 194        int err;
 195
 196        tcf_exts_init(&e, TCA_FW_ACT, TCA_FW_POLICE);
 197        err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
 198        if (err < 0)
 199                return err;
 200
 201        if (tb[TCA_FW_CLASSID]) {
 202                f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
 203                tcf_bind_filter(tp, &f->res, base);
 204        }
 205
 206#ifdef CONFIG_NET_CLS_IND
 207        if (tb[TCA_FW_INDEV]) {
 208                int ret;
 209                ret = tcf_change_indev(net, tb[TCA_FW_INDEV]);
 210                if (ret < 0) {
 211                        err = ret;
 212                        goto errout;
 213                }
 214                f->ifindex = ret;
 215        }
 216#endif /* CONFIG_NET_CLS_IND */
 217
 218        err = -EINVAL;
 219        if (tb[TCA_FW_MASK]) {
 220                mask = nla_get_u32(tb[TCA_FW_MASK]);
 221                if (mask != head->mask)
 222                        goto errout;
 223        } else if (head->mask != 0xFFFFFFFF)
 224                goto errout;
 225
 226        tcf_exts_change(tp, &f->exts, &e);
 227
 228        return 0;
 229errout:
 230        tcf_exts_destroy(&e);
 231        return err;
 232}
 233
 234static int fw_change(struct net *net, struct sk_buff *in_skb,
 235                     struct tcf_proto *tp, unsigned long base,
 236                     u32 handle, struct nlattr **tca, unsigned long *arg,
 237                     bool ovr)
 238{
 239        struct fw_head *head = rtnl_dereference(tp->root);
 240        struct fw_filter *f = (struct fw_filter *) *arg;
 241        struct nlattr *opt = tca[TCA_OPTIONS];
 242        struct nlattr *tb[TCA_FW_MAX + 1];
 243        int err;
 244
 245        if (!opt)
 246                return handle ? -EINVAL : 0;
 247
 248        err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy);
 249        if (err < 0)
 250                return err;
 251
 252        if (f) {
 253                struct fw_filter *pfp, *fnew;
 254                struct fw_filter __rcu **fp;
 255
 256                if (f->id != handle && handle)
 257                        return -EINVAL;
 258
 259                fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
 260                if (!fnew)
 261                        return -ENOBUFS;
 262
 263                fnew->id = f->id;
 264                fnew->res = f->res;
 265#ifdef CONFIG_NET_CLS_IND
 266                fnew->ifindex = f->ifindex;
 267#endif /* CONFIG_NET_CLS_IND */
 268                fnew->tp = f->tp;
 269
 270                tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE);
 271
 272                err = fw_change_attrs(net, tp, fnew, tb, tca, base, ovr);
 273                if (err < 0) {
 274                        kfree(fnew);
 275                        return err;
 276                }
 277
 278                fp = &head->ht[fw_hash(fnew->id)];
 279                for (pfp = rtnl_dereference(*fp); pfp;
 280                     fp = &pfp->next, pfp = rtnl_dereference(*fp))
 281                        if (pfp == f)
 282                                break;
 283
 284                RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
 285                rcu_assign_pointer(*fp, fnew);
 286                tcf_unbind_filter(tp, &f->res);
 287                call_rcu(&f->rcu, fw_delete_filter);
 288
 289                *arg = (unsigned long)fnew;
 290                return err;
 291        }
 292
 293        if (!handle)
 294                return -EINVAL;
 295
 296        if (head == NULL) {
 297                u32 mask = 0xFFFFFFFF;
 298                if (tb[TCA_FW_MASK])
 299                        mask = nla_get_u32(tb[TCA_FW_MASK]);
 300
 301                head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
 302                if (head == NULL)
 303                        return -ENOBUFS;
 304                head->mask = mask;
 305
 306                rcu_assign_pointer(tp->root, head);
 307        }
 308
 309        f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
 310        if (f == NULL)
 311                return -ENOBUFS;
 312
 313        tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE);
 314        f->id = handle;
 315        f->tp = tp;
 316
 317        err = fw_change_attrs(net, tp, f, tb, tca, base, ovr);
 318        if (err < 0)
 319                goto errout;
 320
 321        RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
 322        rcu_assign_pointer(head->ht[fw_hash(handle)], f);
 323
 324        *arg = (unsigned long)f;
 325        return 0;
 326
 327errout:
 328        kfree(f);
 329        return err;
 330}
 331
 332static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
 333{
 334        struct fw_head *head = rtnl_dereference(tp->root);
 335        int h;
 336
 337        if (head == NULL)
 338                arg->stop = 1;
 339
 340        if (arg->stop)
 341                return;
 342
 343        for (h = 0; h < HTSIZE; h++) {
 344                struct fw_filter *f;
 345
 346                for (f = rtnl_dereference(head->ht[h]); f;
 347                     f = rtnl_dereference(f->next)) {
 348                        if (arg->count < arg->skip) {
 349                                arg->count++;
 350                                continue;
 351                        }
 352                        if (arg->fn(tp, (unsigned long)f, arg) < 0) {
 353                                arg->stop = 1;
 354                                return;
 355                        }
 356                        arg->count++;
 357                }
 358        }
 359}
 360
 361static int fw_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
 362                   struct sk_buff *skb, struct tcmsg *t)
 363{
 364        struct fw_head *head = rtnl_dereference(tp->root);
 365        struct fw_filter *f = (struct fw_filter *)fh;
 366        struct nlattr *nest;
 367
 368        if (f == NULL)
 369                return skb->len;
 370
 371        t->tcm_handle = f->id;
 372
 373        if (!f->res.classid && !tcf_exts_is_available(&f->exts))
 374                return skb->len;
 375
 376        nest = nla_nest_start(skb, TCA_OPTIONS);
 377        if (nest == NULL)
 378                goto nla_put_failure;
 379
 380        if (f->res.classid &&
 381            nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
 382                goto nla_put_failure;
 383#ifdef CONFIG_NET_CLS_IND
 384        if (f->ifindex) {
 385                struct net_device *dev;
 386                dev = __dev_get_by_index(net, f->ifindex);
 387                if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
 388                        goto nla_put_failure;
 389        }
 390#endif /* CONFIG_NET_CLS_IND */
 391        if (head->mask != 0xFFFFFFFF &&
 392            nla_put_u32(skb, TCA_FW_MASK, head->mask))
 393                goto nla_put_failure;
 394
 395        if (tcf_exts_dump(skb, &f->exts) < 0)
 396                goto nla_put_failure;
 397
 398        nla_nest_end(skb, nest);
 399
 400        if (tcf_exts_dump_stats(skb, &f->exts) < 0)
 401                goto nla_put_failure;
 402
 403        return skb->len;
 404
 405nla_put_failure:
 406        nla_nest_cancel(skb, nest);
 407        return -1;
 408}
 409
 410static struct tcf_proto_ops cls_fw_ops __read_mostly = {
 411        .kind           =       "fw",
 412        .classify       =       fw_classify,
 413        .init           =       fw_init,
 414        .destroy        =       fw_destroy,
 415        .get            =       fw_get,
 416        .change         =       fw_change,
 417        .delete         =       fw_delete,
 418        .walk           =       fw_walk,
 419        .dump           =       fw_dump,
 420        .owner          =       THIS_MODULE,
 421};
 422
 423static int __init init_fw(void)
 424{
 425        return register_tcf_proto_ops(&cls_fw_ops);
 426}
 427
 428static void __exit exit_fw(void)
 429{
 430        unregister_tcf_proto_ops(&cls_fw_ops);
 431}
 432
 433module_init(init_fw)
 434module_exit(exit_fw)
 435MODULE_LICENSE("GPL");
 436