linux/net/sched/cls_flower.c
<<
>>
Prefs
   1/*
   2 * net/sched/cls_flower.c               Flower classifier
   3 *
   4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/init.h>
  14#include <linux/module.h>
  15#include <linux/rhashtable.h>
  16#include <linux/workqueue.h>
  17
  18#include <linux/if_ether.h>
  19#include <linux/in6.h>
  20#include <linux/ip.h>
  21#include <linux/mpls.h>
  22
  23#include <net/sch_generic.h>
  24#include <net/pkt_cls.h>
  25#include <net/ip.h>
  26#include <net/flow_dissector.h>
  27
  28#include <net/dst.h>
  29#include <net/dst_metadata.h>
  30
  31struct fl_flow_key {
  32        int     indev_ifindex;
  33        struct flow_dissector_key_control control;
  34        struct flow_dissector_key_control enc_control;
  35        struct flow_dissector_key_basic basic;
  36        struct flow_dissector_key_eth_addrs eth;
  37        struct flow_dissector_key_vlan vlan;
  38        union {
  39                struct flow_dissector_key_ipv4_addrs ipv4;
  40                struct flow_dissector_key_ipv6_addrs ipv6;
  41        };
  42        struct flow_dissector_key_ports tp;
  43        struct flow_dissector_key_icmp icmp;
  44        struct flow_dissector_key_arp arp;
  45        struct flow_dissector_key_keyid enc_key_id;
  46        union {
  47                struct flow_dissector_key_ipv4_addrs enc_ipv4;
  48                struct flow_dissector_key_ipv6_addrs enc_ipv6;
  49        };
  50        struct flow_dissector_key_ports enc_tp;
  51        struct flow_dissector_key_mpls mpls;
  52} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  53
  54struct fl_flow_mask_range {
  55        unsigned short int start;
  56        unsigned short int end;
  57};
  58
  59struct fl_flow_mask {
  60        struct fl_flow_key key;
  61        struct fl_flow_mask_range range;
  62        struct rcu_head rcu;
  63};
  64
  65struct cls_fl_head {
  66        struct rhashtable ht;
  67        struct fl_flow_mask mask;
  68        struct flow_dissector dissector;
  69        u32 hgen;
  70        bool mask_assigned;
  71        struct list_head filters;
  72        struct rhashtable_params ht_params;
  73        union {
  74                struct work_struct work;
  75                struct rcu_head rcu;
  76        };
  77};
  78
  79struct cls_fl_filter {
  80        struct rhash_head ht_node;
  81        struct fl_flow_key mkey;
  82        struct tcf_exts exts;
  83        struct tcf_result res;
  84        struct fl_flow_key key;
  85        struct list_head list;
  86        u32 handle;
  87        u32 flags;
  88        struct rcu_head rcu;
  89        struct tc_to_netdev tc;
  90        struct net_device *hw_dev;
  91};
  92
  93static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
  94{
  95        return mask->range.end - mask->range.start;
  96}
  97
  98static void fl_mask_update_range(struct fl_flow_mask *mask)
  99{
 100        const u8 *bytes = (const u8 *) &mask->key;
 101        size_t size = sizeof(mask->key);
 102        size_t i, first = 0, last = size - 1;
 103
 104        for (i = 0; i < sizeof(mask->key); i++) {
 105                if (bytes[i]) {
 106                        if (!first && i)
 107                                first = i;
 108                        last = i;
 109                }
 110        }
 111        mask->range.start = rounddown(first, sizeof(long));
 112        mask->range.end = roundup(last + 1, sizeof(long));
 113}
 114
 115static void *fl_key_get_start(struct fl_flow_key *key,
 116                              const struct fl_flow_mask *mask)
 117{
 118        return (u8 *) key + mask->range.start;
 119}
 120
 121static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
 122                              struct fl_flow_mask *mask)
 123{
 124        const long *lkey = fl_key_get_start(key, mask);
 125        const long *lmask = fl_key_get_start(&mask->key, mask);
 126        long *lmkey = fl_key_get_start(mkey, mask);
 127        int i;
 128
 129        for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
 130                *lmkey++ = *lkey++ & *lmask++;
 131}
 132
 133static void fl_clear_masked_range(struct fl_flow_key *key,
 134                                  struct fl_flow_mask *mask)
 135{
 136        memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
 137}
 138
 139static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
 140                                       struct fl_flow_key *mkey)
 141{
 142        return rhashtable_lookup_fast(&head->ht,
 143                                      fl_key_get_start(mkey, &head->mask),
 144                                      head->ht_params);
 145}
 146
 147static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 148                       struct tcf_result *res)
 149{
 150        struct cls_fl_head *head = rcu_dereference_bh(tp->root);
 151        struct cls_fl_filter *f;
 152        struct fl_flow_key skb_key;
 153        struct fl_flow_key skb_mkey;
 154        struct ip_tunnel_info *info;
 155
 156        if (!atomic_read(&head->ht.nelems))
 157                return -1;
 158
 159        fl_clear_masked_range(&skb_key, &head->mask);
 160
 161        info = skb_tunnel_info(skb);
 162        if (info) {
 163                struct ip_tunnel_key *key = &info->key;
 164
 165                switch (ip_tunnel_info_af(info)) {
 166                case AF_INET:
 167                        skb_key.enc_control.addr_type =
 168                                FLOW_DISSECTOR_KEY_IPV4_ADDRS;
 169                        skb_key.enc_ipv4.src = key->u.ipv4.src;
 170                        skb_key.enc_ipv4.dst = key->u.ipv4.dst;
 171                        break;
 172                case AF_INET6:
 173                        skb_key.enc_control.addr_type =
 174                                FLOW_DISSECTOR_KEY_IPV6_ADDRS;
 175                        skb_key.enc_ipv6.src = key->u.ipv6.src;
 176                        skb_key.enc_ipv6.dst = key->u.ipv6.dst;
 177                        break;
 178                }
 179
 180                skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
 181                skb_key.enc_tp.src = key->tp_src;
 182                skb_key.enc_tp.dst = key->tp_dst;
 183        }
 184
 185        skb_key.indev_ifindex = skb->skb_iif;
 186        /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
 187         * so do it rather here.
 188         */
 189        skb_key.basic.n_proto = skb->protocol;
 190        skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
 191
 192        fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
 193
 194        f = fl_lookup(head, &skb_mkey);
 195        if (f && !tc_skip_sw(f->flags)) {
 196                *res = f->res;
 197                return tcf_exts_exec(skb, &f->exts, res);
 198        }
 199        return -1;
 200}
 201
 202static int fl_init(struct tcf_proto *tp)
 203{
 204        struct cls_fl_head *head;
 205
 206        head = kzalloc(sizeof(*head), GFP_KERNEL);
 207        if (!head)
 208                return -ENOBUFS;
 209
 210        INIT_LIST_HEAD_RCU(&head->filters);
 211        rcu_assign_pointer(tp->root, head);
 212
 213        return 0;
 214}
 215
 216static void fl_destroy_filter(struct rcu_head *head)
 217{
 218        struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
 219
 220        tcf_exts_destroy(&f->exts);
 221        kfree(f);
 222}
 223
 224static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
 225{
 226        struct tc_cls_flower_offload offload = {0};
 227        struct net_device *dev = f->hw_dev;
 228        struct tc_to_netdev *tc = &f->tc;
 229
 230        if (!tc_can_offload(dev, tp))
 231                return;
 232
 233        offload.command = TC_CLSFLOWER_DESTROY;
 234        offload.prio = tp->prio;
 235        offload.cookie = (unsigned long)f;
 236
 237        tc->type = TC_SETUP_CLSFLOWER;
 238        tc->cls_flower = &offload;
 239
 240        dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
 241}
 242
 243static int fl_hw_replace_filter(struct tcf_proto *tp,
 244                                struct flow_dissector *dissector,
 245                                struct fl_flow_key *mask,
 246                                struct cls_fl_filter *f)
 247{
 248        struct net_device *dev = tp->q->dev_queue->dev;
 249        struct tc_cls_flower_offload offload = {0};
 250        struct tc_to_netdev *tc = &f->tc;
 251        int err;
 252
 253        if (!tc_can_offload(dev, tp)) {
 254                if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
 255                    (f->hw_dev && !tc_can_offload(f->hw_dev, tp))) {
 256                        f->hw_dev = dev;
 257                        return tc_skip_sw(f->flags) ? -EINVAL : 0;
 258                }
 259                dev = f->hw_dev;
 260                tc->egress_dev = true;
 261        } else {
 262                f->hw_dev = dev;
 263        }
 264
 265        offload.command = TC_CLSFLOWER_REPLACE;
 266        offload.prio = tp->prio;
 267        offload.cookie = (unsigned long)f;
 268        offload.dissector = dissector;
 269        offload.mask = mask;
 270        offload.key = &f->mkey;
 271        offload.exts = &f->exts;
 272
 273        tc->type = TC_SETUP_CLSFLOWER;
 274        tc->cls_flower = &offload;
 275
 276        err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
 277                                            tc);
 278        if (!err)
 279                f->flags |= TCA_CLS_FLAGS_IN_HW;
 280
 281        if (tc_skip_sw(f->flags))
 282                return err;
 283        return 0;
 284}
 285
 286static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
 287{
 288        struct tc_cls_flower_offload offload = {0};
 289        struct net_device *dev = f->hw_dev;
 290        struct tc_to_netdev *tc = &f->tc;
 291
 292        if (!tc_can_offload(dev, tp))
 293                return;
 294
 295        offload.command = TC_CLSFLOWER_STATS;
 296        offload.prio = tp->prio;
 297        offload.cookie = (unsigned long)f;
 298        offload.exts = &f->exts;
 299
 300        tc->type = TC_SETUP_CLSFLOWER;
 301        tc->cls_flower = &offload;
 302
 303        dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
 304}
 305
 306static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
 307{
 308        list_del_rcu(&f->list);
 309        if (!tc_skip_hw(f->flags))
 310                fl_hw_destroy_filter(tp, f);
 311        tcf_unbind_filter(tp, &f->res);
 312        call_rcu(&f->rcu, fl_destroy_filter);
 313}
 314
 315static void fl_destroy_sleepable(struct work_struct *work)
 316{
 317        struct cls_fl_head *head = container_of(work, struct cls_fl_head,
 318                                                work);
 319        if (head->mask_assigned)
 320                rhashtable_destroy(&head->ht);
 321        kfree(head);
 322        module_put(THIS_MODULE);
 323}
 324
 325static void fl_destroy_rcu(struct rcu_head *rcu)
 326{
 327        struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
 328
 329        INIT_WORK(&head->work, fl_destroy_sleepable);
 330        schedule_work(&head->work);
 331}
 332
 333static void fl_destroy(struct tcf_proto *tp)
 334{
 335        struct cls_fl_head *head = rtnl_dereference(tp->root);
 336        struct cls_fl_filter *f, *next;
 337
 338        list_for_each_entry_safe(f, next, &head->filters, list)
 339                __fl_delete(tp, f);
 340
 341        __module_get(THIS_MODULE);
 342        call_rcu(&head->rcu, fl_destroy_rcu);
 343}
 344
 345static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
 346{
 347        struct cls_fl_head *head = rtnl_dereference(tp->root);
 348        struct cls_fl_filter *f;
 349
 350        list_for_each_entry(f, &head->filters, list)
 351                if (f->handle == handle)
 352                        return (unsigned long) f;
 353        return 0;
 354}
 355
 356static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
 357        [TCA_FLOWER_UNSPEC]             = { .type = NLA_UNSPEC },
 358        [TCA_FLOWER_CLASSID]            = { .type = NLA_U32 },
 359        [TCA_FLOWER_INDEV]              = { .type = NLA_STRING,
 360                                            .len = IFNAMSIZ },
 361        [TCA_FLOWER_KEY_ETH_DST]        = { .len = ETH_ALEN },
 362        [TCA_FLOWER_KEY_ETH_DST_MASK]   = { .len = ETH_ALEN },
 363        [TCA_FLOWER_KEY_ETH_SRC]        = { .len = ETH_ALEN },
 364        [TCA_FLOWER_KEY_ETH_SRC_MASK]   = { .len = ETH_ALEN },
 365        [TCA_FLOWER_KEY_ETH_TYPE]       = { .type = NLA_U16 },
 366        [TCA_FLOWER_KEY_IP_PROTO]       = { .type = NLA_U8 },
 367        [TCA_FLOWER_KEY_IPV4_SRC]       = { .type = NLA_U32 },
 368        [TCA_FLOWER_KEY_IPV4_SRC_MASK]  = { .type = NLA_U32 },
 369        [TCA_FLOWER_KEY_IPV4_DST]       = { .type = NLA_U32 },
 370        [TCA_FLOWER_KEY_IPV4_DST_MASK]  = { .type = NLA_U32 },
 371        [TCA_FLOWER_KEY_IPV6_SRC]       = { .len = sizeof(struct in6_addr) },
 372        [TCA_FLOWER_KEY_IPV6_SRC_MASK]  = { .len = sizeof(struct in6_addr) },
 373        [TCA_FLOWER_KEY_IPV6_DST]       = { .len = sizeof(struct in6_addr) },
 374        [TCA_FLOWER_KEY_IPV6_DST_MASK]  = { .len = sizeof(struct in6_addr) },
 375        [TCA_FLOWER_KEY_TCP_SRC]        = { .type = NLA_U16 },
 376        [TCA_FLOWER_KEY_TCP_DST]        = { .type = NLA_U16 },
 377        [TCA_FLOWER_KEY_UDP_SRC]        = { .type = NLA_U16 },
 378        [TCA_FLOWER_KEY_UDP_DST]        = { .type = NLA_U16 },
 379        [TCA_FLOWER_KEY_VLAN_ID]        = { .type = NLA_U16 },
 380        [TCA_FLOWER_KEY_VLAN_PRIO]      = { .type = NLA_U8 },
 381        [TCA_FLOWER_KEY_VLAN_ETH_TYPE]  = { .type = NLA_U16 },
 382        [TCA_FLOWER_KEY_ENC_KEY_ID]     = { .type = NLA_U32 },
 383        [TCA_FLOWER_KEY_ENC_IPV4_SRC]   = { .type = NLA_U32 },
 384        [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
 385        [TCA_FLOWER_KEY_ENC_IPV4_DST]   = { .type = NLA_U32 },
 386        [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
 387        [TCA_FLOWER_KEY_ENC_IPV6_SRC]   = { .len = sizeof(struct in6_addr) },
 388        [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
 389        [TCA_FLOWER_KEY_ENC_IPV6_DST]   = { .len = sizeof(struct in6_addr) },
 390        [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
 391        [TCA_FLOWER_KEY_TCP_SRC_MASK]   = { .type = NLA_U16 },
 392        [TCA_FLOWER_KEY_TCP_DST_MASK]   = { .type = NLA_U16 },
 393        [TCA_FLOWER_KEY_UDP_SRC_MASK]   = { .type = NLA_U16 },
 394        [TCA_FLOWER_KEY_UDP_DST_MASK]   = { .type = NLA_U16 },
 395        [TCA_FLOWER_KEY_SCTP_SRC_MASK]  = { .type = NLA_U16 },
 396        [TCA_FLOWER_KEY_SCTP_DST_MASK]  = { .type = NLA_U16 },
 397        [TCA_FLOWER_KEY_SCTP_SRC]       = { .type = NLA_U16 },
 398        [TCA_FLOWER_KEY_SCTP_DST]       = { .type = NLA_U16 },
 399        [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT]       = { .type = NLA_U16 },
 400        [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK]  = { .type = NLA_U16 },
 401        [TCA_FLOWER_KEY_ENC_UDP_DST_PORT]       = { .type = NLA_U16 },
 402        [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]  = { .type = NLA_U16 },
 403        [TCA_FLOWER_KEY_FLAGS]          = { .type = NLA_U32 },
 404        [TCA_FLOWER_KEY_FLAGS_MASK]     = { .type = NLA_U32 },
 405        [TCA_FLOWER_KEY_ICMPV4_TYPE]    = { .type = NLA_U8 },
 406        [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
 407        [TCA_FLOWER_KEY_ICMPV4_CODE]    = { .type = NLA_U8 },
 408        [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
 409        [TCA_FLOWER_KEY_ICMPV6_TYPE]    = { .type = NLA_U8 },
 410        [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
 411        [TCA_FLOWER_KEY_ICMPV6_CODE]    = { .type = NLA_U8 },
 412        [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
 413        [TCA_FLOWER_KEY_ARP_SIP]        = { .type = NLA_U32 },
 414        [TCA_FLOWER_KEY_ARP_SIP_MASK]   = { .type = NLA_U32 },
 415        [TCA_FLOWER_KEY_ARP_TIP]        = { .type = NLA_U32 },
 416        [TCA_FLOWER_KEY_ARP_TIP_MASK]   = { .type = NLA_U32 },
 417        [TCA_FLOWER_KEY_ARP_OP]         = { .type = NLA_U8 },
 418        [TCA_FLOWER_KEY_ARP_OP_MASK]    = { .type = NLA_U8 },
 419        [TCA_FLOWER_KEY_ARP_SHA]        = { .len = ETH_ALEN },
 420        [TCA_FLOWER_KEY_ARP_SHA_MASK]   = { .len = ETH_ALEN },
 421        [TCA_FLOWER_KEY_ARP_THA]        = { .len = ETH_ALEN },
 422        [TCA_FLOWER_KEY_ARP_THA_MASK]   = { .len = ETH_ALEN },
 423        [TCA_FLOWER_KEY_MPLS_TTL]       = { .type = NLA_U8 },
 424        [TCA_FLOWER_KEY_MPLS_BOS]       = { .type = NLA_U8 },
 425        [TCA_FLOWER_KEY_MPLS_TC]        = { .type = NLA_U8 },
 426        [TCA_FLOWER_KEY_MPLS_LABEL]     = { .type = NLA_U32 },
 427};
 428
 429static void fl_set_key_val(struct nlattr **tb,
 430                           void *val, int val_type,
 431                           void *mask, int mask_type, int len)
 432{
 433        if (!tb[val_type])
 434                return;
 435        memcpy(val, nla_data(tb[val_type]), len);
 436        if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
 437                memset(mask, 0xff, len);
 438        else
 439                memcpy(mask, nla_data(tb[mask_type]), len);
 440}
 441
 442static int fl_set_key_mpls(struct nlattr **tb,
 443                           struct flow_dissector_key_mpls *key_val,
 444                           struct flow_dissector_key_mpls *key_mask)
 445{
 446        if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
 447                key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
 448                key_mask->mpls_ttl = MPLS_TTL_MASK;
 449        }
 450        if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
 451                u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
 452
 453                if (bos & ~MPLS_BOS_MASK)
 454                        return -EINVAL;
 455                key_val->mpls_bos = bos;
 456                key_mask->mpls_bos = MPLS_BOS_MASK;
 457        }
 458        if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
 459                u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
 460
 461                if (tc & ~MPLS_TC_MASK)
 462                        return -EINVAL;
 463                key_val->mpls_tc = tc;
 464                key_mask->mpls_tc = MPLS_TC_MASK;
 465        }
 466        if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
 467                u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
 468
 469                if (label & ~MPLS_LABEL_MASK)
 470                        return -EINVAL;
 471                key_val->mpls_label = label;
 472                key_mask->mpls_label = MPLS_LABEL_MASK;
 473        }
 474        return 0;
 475}
 476
 477static void fl_set_key_vlan(struct nlattr **tb,
 478                            struct flow_dissector_key_vlan *key_val,
 479                            struct flow_dissector_key_vlan *key_mask)
 480{
 481#define VLAN_PRIORITY_MASK      0x7
 482
 483        if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
 484                key_val->vlan_id =
 485                        nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
 486                key_mask->vlan_id = VLAN_VID_MASK;
 487        }
 488        if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
 489                key_val->vlan_priority =
 490                        nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
 491                        VLAN_PRIORITY_MASK;
 492                key_mask->vlan_priority = VLAN_PRIORITY_MASK;
 493        }
 494}
 495
 496static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
 497                            u32 *dissector_key, u32 *dissector_mask,
 498                            u32 flower_flag_bit, u32 dissector_flag_bit)
 499{
 500        if (flower_mask & flower_flag_bit) {
 501                *dissector_mask |= dissector_flag_bit;
 502                if (flower_key & flower_flag_bit)
 503                        *dissector_key |= dissector_flag_bit;
 504        }
 505}
 506
 507static int fl_set_key_flags(struct nlattr **tb,
 508                            u32 *flags_key, u32 *flags_mask)
 509{
 510        u32 key, mask;
 511
 512        /* mask is mandatory for flags */
 513        if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
 514                return -EINVAL;
 515
 516        key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
 517        mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
 518
 519        *flags_key  = 0;
 520        *flags_mask = 0;
 521
 522        fl_set_key_flag(key, mask, flags_key, flags_mask,
 523                        TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
 524
 525        return 0;
 526}
 527
 528static int fl_set_key(struct net *net, struct nlattr **tb,
 529                      struct fl_flow_key *key, struct fl_flow_key *mask)
 530{
 531        __be16 ethertype;
 532        int ret = 0;
 533#ifdef CONFIG_NET_CLS_IND
 534        if (tb[TCA_FLOWER_INDEV]) {
 535                int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
 536                if (err < 0)
 537                        return err;
 538                key->indev_ifindex = err;
 539                mask->indev_ifindex = 0xffffffff;
 540        }
 541#endif
 542
 543        fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
 544                       mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
 545                       sizeof(key->eth.dst));
 546        fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
 547                       mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
 548                       sizeof(key->eth.src));
 549
 550        if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
 551                ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
 552
 553                if (ethertype == htons(ETH_P_8021Q)) {
 554                        fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
 555                        fl_set_key_val(tb, &key->basic.n_proto,
 556                                       TCA_FLOWER_KEY_VLAN_ETH_TYPE,
 557                                       &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
 558                                       sizeof(key->basic.n_proto));
 559                } else {
 560                        key->basic.n_proto = ethertype;
 561                        mask->basic.n_proto = cpu_to_be16(~0);
 562                }
 563        }
 564
 565        if (key->basic.n_proto == htons(ETH_P_IP) ||
 566            key->basic.n_proto == htons(ETH_P_IPV6)) {
 567                fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
 568                               &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
 569                               sizeof(key->basic.ip_proto));
 570        }
 571
 572        if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
 573                key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
 574                mask->control.addr_type = ~0;
 575                fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
 576                               &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
 577                               sizeof(key->ipv4.src));
 578                fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
 579                               &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
 580                               sizeof(key->ipv4.dst));
 581        } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
 582                key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
 583                mask->control.addr_type = ~0;
 584                fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
 585                               &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
 586                               sizeof(key->ipv6.src));
 587                fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
 588                               &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
 589                               sizeof(key->ipv6.dst));
 590        }
 591
 592        if (key->basic.ip_proto == IPPROTO_TCP) {
 593                fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
 594                               &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
 595                               sizeof(key->tp.src));
 596                fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
 597                               &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
 598                               sizeof(key->tp.dst));
 599        } else if (key->basic.ip_proto == IPPROTO_UDP) {
 600                fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
 601                               &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
 602                               sizeof(key->tp.src));
 603                fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
 604                               &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
 605                               sizeof(key->tp.dst));
 606        } else if (key->basic.ip_proto == IPPROTO_SCTP) {
 607                fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
 608                               &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
 609                               sizeof(key->tp.src));
 610                fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
 611                               &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
 612                               sizeof(key->tp.dst));
 613        } else if (key->basic.n_proto == htons(ETH_P_IP) &&
 614                   key->basic.ip_proto == IPPROTO_ICMP) {
 615                fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
 616                               &mask->icmp.type,
 617                               TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
 618                               sizeof(key->icmp.type));
 619                fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
 620                               &mask->icmp.code,
 621                               TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
 622                               sizeof(key->icmp.code));
 623        } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
 624                   key->basic.ip_proto == IPPROTO_ICMPV6) {
 625                fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
 626                               &mask->icmp.type,
 627                               TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
 628                               sizeof(key->icmp.type));
 629                fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
 630                               &mask->icmp.code,
 631                               TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
 632                               sizeof(key->icmp.code));
 633        } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
 634                   key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
 635                ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
 636                if (ret)
 637                        return ret;
 638        } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
 639                   key->basic.n_proto == htons(ETH_P_RARP)) {
 640                fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
 641                               &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
 642                               sizeof(key->arp.sip));
 643                fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
 644                               &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
 645                               sizeof(key->arp.tip));
 646                fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
 647                               &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
 648                               sizeof(key->arp.op));
 649                fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
 650                               mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
 651                               sizeof(key->arp.sha));
 652                fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
 653                               mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
 654                               sizeof(key->arp.tha));
 655        }
 656
 657        if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
 658            tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
 659                key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
 660                mask->enc_control.addr_type = ~0;
 661                fl_set_key_val(tb, &key->enc_ipv4.src,
 662                               TCA_FLOWER_KEY_ENC_IPV4_SRC,
 663                               &mask->enc_ipv4.src,
 664                               TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
 665                               sizeof(key->enc_ipv4.src));
 666                fl_set_key_val(tb, &key->enc_ipv4.dst,
 667                               TCA_FLOWER_KEY_ENC_IPV4_DST,
 668                               &mask->enc_ipv4.dst,
 669                               TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
 670                               sizeof(key->enc_ipv4.dst));
 671        }
 672
 673        if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
 674            tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
 675                key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
 676                mask->enc_control.addr_type = ~0;
 677                fl_set_key_val(tb, &key->enc_ipv6.src,
 678                               TCA_FLOWER_KEY_ENC_IPV6_SRC,
 679                               &mask->enc_ipv6.src,
 680                               TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
 681                               sizeof(key->enc_ipv6.src));
 682                fl_set_key_val(tb, &key->enc_ipv6.dst,
 683                               TCA_FLOWER_KEY_ENC_IPV6_DST,
 684                               &mask->enc_ipv6.dst,
 685                               TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
 686                               sizeof(key->enc_ipv6.dst));
 687        }
 688
 689        fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
 690                       &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
 691                       sizeof(key->enc_key_id.keyid));
 692
 693        fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
 694                       &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
 695                       sizeof(key->enc_tp.src));
 696
 697        fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
 698                       &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
 699                       sizeof(key->enc_tp.dst));
 700
 701        if (tb[TCA_FLOWER_KEY_FLAGS])
 702                ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
 703
 704        return ret;
 705}
 706
 707static bool fl_mask_eq(struct fl_flow_mask *mask1,
 708                       struct fl_flow_mask *mask2)
 709{
 710        const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
 711        const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
 712
 713        return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
 714               !memcmp(lmask1, lmask2, fl_mask_range(mask1));
 715}
 716
 717static const struct rhashtable_params fl_ht_params = {
 718        .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
 719        .head_offset = offsetof(struct cls_fl_filter, ht_node),
 720        .automatic_shrinking = true,
 721};
 722
 723static int fl_init_hashtable(struct cls_fl_head *head,
 724                             struct fl_flow_mask *mask)
 725{
 726        head->ht_params = fl_ht_params;
 727        head->ht_params.key_len = fl_mask_range(mask);
 728        head->ht_params.key_offset += mask->range.start;
 729
 730        return rhashtable_init(&head->ht, &head->ht_params);
 731}
 732
 733#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
 734#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
 735
 736#define FL_KEY_IS_MASKED(mask, member)                                          \
 737        memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),               \
 738                   0, FL_KEY_MEMBER_SIZE(member))                               \
 739
 740#define FL_KEY_SET(keys, cnt, id, member)                                       \
 741        do {                                                                    \
 742                keys[cnt].key_id = id;                                          \
 743                keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);                \
 744                cnt++;                                                          \
 745        } while(0);
 746
 747#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)                       \
 748        do {                                                                    \
 749                if (FL_KEY_IS_MASKED(mask, member))                             \
 750                        FL_KEY_SET(keys, cnt, id, member);                      \
 751        } while(0);
 752
 753static void fl_init_dissector(struct cls_fl_head *head,
 754                              struct fl_flow_mask *mask)
 755{
 756        struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
 757        size_t cnt = 0;
 758
 759        FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
 760        FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
 761        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 762                             FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
 763        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 764                             FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
 765        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 766                             FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
 767        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 768                             FLOW_DISSECTOR_KEY_PORTS, tp);
 769        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 770                             FLOW_DISSECTOR_KEY_ICMP, icmp);
 771        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 772                             FLOW_DISSECTOR_KEY_ARP, arp);
 773        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 774                             FLOW_DISSECTOR_KEY_MPLS, mpls);
 775        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 776                             FLOW_DISSECTOR_KEY_VLAN, vlan);
 777        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 778                             FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
 779        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 780                             FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
 781        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 782                             FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
 783        if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
 784            FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
 785                FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
 786                           enc_control);
 787        FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
 788                             FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
 789
 790        skb_flow_dissector_init(&head->dissector, keys, cnt);
 791}
 792
 793static int fl_check_assign_mask(struct cls_fl_head *head,
 794                                struct fl_flow_mask *mask)
 795{
 796        int err;
 797
 798        if (head->mask_assigned) {
 799                if (!fl_mask_eq(&head->mask, mask))
 800                        return -EINVAL;
 801                else
 802                        return 0;
 803        }
 804
 805        /* Mask is not assigned yet. So assign it and init hashtable
 806         * according to that.
 807         */
 808        err = fl_init_hashtable(head, mask);
 809        if (err)
 810                return err;
 811        memcpy(&head->mask, mask, sizeof(head->mask));
 812        head->mask_assigned = true;
 813
 814        fl_init_dissector(head, mask);
 815
 816        return 0;
 817}
 818
 819static int fl_set_parms(struct net *net, struct tcf_proto *tp,
 820                        struct cls_fl_filter *f, struct fl_flow_mask *mask,
 821                        unsigned long base, struct nlattr **tb,
 822                        struct nlattr *est, bool ovr)
 823{
 824        struct tcf_exts e;
 825        int err;
 826
 827        err = tcf_exts_init(&e, TCA_FLOWER_ACT, 0);
 828        if (err < 0)
 829                return err;
 830        err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
 831        if (err < 0)
 832                goto errout;
 833
 834        if (tb[TCA_FLOWER_CLASSID]) {
 835                f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
 836                tcf_bind_filter(tp, &f->res, base);
 837        }
 838
 839        err = fl_set_key(net, tb, &f->key, &mask->key);
 840        if (err)
 841                goto errout;
 842
 843        fl_mask_update_range(mask);
 844        fl_set_masked_key(&f->mkey, &f->key, mask);
 845
 846        tcf_exts_change(tp, &f->exts, &e);
 847
 848        return 0;
 849errout:
 850        tcf_exts_destroy(&e);
 851        return err;
 852}
 853
 854static u32 fl_grab_new_handle(struct tcf_proto *tp,
 855                              struct cls_fl_head *head)
 856{
 857        unsigned int i = 0x80000000;
 858        u32 handle;
 859
 860        do {
 861                if (++head->hgen == 0x7FFFFFFF)
 862                        head->hgen = 1;
 863        } while (--i > 0 && fl_get(tp, head->hgen));
 864
 865        if (unlikely(i == 0)) {
 866                pr_err("Insufficient number of handles\n");
 867                handle = 0;
 868        } else {
 869                handle = head->hgen;
 870        }
 871
 872        return handle;
 873}
 874
 875static int fl_change(struct net *net, struct sk_buff *in_skb,
 876                     struct tcf_proto *tp, unsigned long base,
 877                     u32 handle, struct nlattr **tca,
 878                     unsigned long *arg, bool ovr)
 879{
 880        struct cls_fl_head *head = rtnl_dereference(tp->root);
 881        struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
 882        struct cls_fl_filter *fnew;
 883        struct nlattr **tb;
 884        struct fl_flow_mask mask = {};
 885        int err;
 886
 887        if (!tca[TCA_OPTIONS])
 888                return -EINVAL;
 889
 890        tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
 891        if (!tb)
 892                return -ENOBUFS;
 893
 894        err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
 895                               fl_policy, NULL);
 896        if (err < 0)
 897                goto errout_tb;
 898
 899        if (fold && handle && fold->handle != handle) {
 900                err = -EINVAL;
 901                goto errout_tb;
 902        }
 903
 904        fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
 905        if (!fnew) {
 906                err = -ENOBUFS;
 907                goto errout_tb;
 908        }
 909
 910        err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
 911        if (err < 0)
 912                goto errout;
 913
 914        if (!handle) {
 915                handle = fl_grab_new_handle(tp, head);
 916                if (!handle) {
 917                        err = -EINVAL;
 918                        goto errout;
 919                }
 920        }
 921        fnew->handle = handle;
 922
 923        if (tb[TCA_FLOWER_FLAGS]) {
 924                fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
 925
 926                if (!tc_flags_valid(fnew->flags)) {
 927                        err = -EINVAL;
 928                        goto errout;
 929                }
 930        }
 931
 932        err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
 933        if (err)
 934                goto errout;
 935
 936        err = fl_check_assign_mask(head, &mask);
 937        if (err)
 938                goto errout;
 939
 940        if (!tc_skip_sw(fnew->flags)) {
 941                if (!fold && fl_lookup(head, &fnew->mkey)) {
 942                        err = -EEXIST;
 943                        goto errout;
 944                }
 945
 946                err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
 947                                             head->ht_params);
 948                if (err)
 949                        goto errout;
 950        }
 951
 952        if (!tc_skip_hw(fnew->flags)) {
 953                err = fl_hw_replace_filter(tp,
 954                                           &head->dissector,
 955                                           &mask.key,
 956                                           fnew);
 957                if (err)
 958                        goto errout;
 959        }
 960
 961        if (!tc_in_hw(fnew->flags))
 962                fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
 963
 964        if (fold) {
 965                if (!tc_skip_sw(fold->flags))
 966                        rhashtable_remove_fast(&head->ht, &fold->ht_node,
 967                                               head->ht_params);
 968                if (!tc_skip_hw(fold->flags))
 969                        fl_hw_destroy_filter(tp, fold);
 970        }
 971
 972        *arg = (unsigned long) fnew;
 973
 974        if (fold) {
 975                list_replace_rcu(&fold->list, &fnew->list);
 976                tcf_unbind_filter(tp, &fold->res);
 977                call_rcu(&fold->rcu, fl_destroy_filter);
 978        } else {
 979                list_add_tail_rcu(&fnew->list, &head->filters);
 980        }
 981
 982        kfree(tb);
 983        return 0;
 984
 985errout:
 986        tcf_exts_destroy(&fnew->exts);
 987        kfree(fnew);
 988errout_tb:
 989        kfree(tb);
 990        return err;
 991}
 992
 993static int fl_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
 994{
 995        struct cls_fl_head *head = rtnl_dereference(tp->root);
 996        struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
 997
 998        if (!tc_skip_sw(f->flags))
 999                rhashtable_remove_fast(&head->ht, &f->ht_node,
1000                                       head->ht_params);
1001        __fl_delete(tp, f);
1002        *last = list_empty(&head->filters);
1003        return 0;
1004}
1005
1006static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1007{
1008        struct cls_fl_head *head = rtnl_dereference(tp->root);
1009        struct cls_fl_filter *f;
1010
1011        list_for_each_entry_rcu(f, &head->filters, list) {
1012                if (arg->count < arg->skip)
1013                        goto skip;
1014                if (arg->fn(tp, (unsigned long) f, arg) < 0) {
1015                        arg->stop = 1;
1016                        break;
1017                }
1018skip:
1019                arg->count++;
1020        }
1021}
1022
1023static int fl_dump_key_val(struct sk_buff *skb,
1024                           void *val, int val_type,
1025                           void *mask, int mask_type, int len)
1026{
1027        int err;
1028
1029        if (!memchr_inv(mask, 0, len))
1030                return 0;
1031        err = nla_put(skb, val_type, len, val);
1032        if (err)
1033                return err;
1034        if (mask_type != TCA_FLOWER_UNSPEC) {
1035                err = nla_put(skb, mask_type, len, mask);
1036                if (err)
1037                        return err;
1038        }
1039        return 0;
1040}
1041
1042static int fl_dump_key_mpls(struct sk_buff *skb,
1043                            struct flow_dissector_key_mpls *mpls_key,
1044                            struct flow_dissector_key_mpls *mpls_mask)
1045{
1046        int err;
1047
1048        if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1049                return 0;
1050        if (mpls_mask->mpls_ttl) {
1051                err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1052                                 mpls_key->mpls_ttl);
1053                if (err)
1054                        return err;
1055        }
1056        if (mpls_mask->mpls_tc) {
1057                err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1058                                 mpls_key->mpls_tc);
1059                if (err)
1060                        return err;
1061        }
1062        if (mpls_mask->mpls_label) {
1063                err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1064                                  mpls_key->mpls_label);
1065                if (err)
1066                        return err;
1067        }
1068        if (mpls_mask->mpls_bos) {
1069                err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1070                                 mpls_key->mpls_bos);
1071                if (err)
1072                        return err;
1073        }
1074        return 0;
1075}
1076
1077static int fl_dump_key_vlan(struct sk_buff *skb,
1078                            struct flow_dissector_key_vlan *vlan_key,
1079                            struct flow_dissector_key_vlan *vlan_mask)
1080{
1081        int err;
1082
1083        if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1084                return 0;
1085        if (vlan_mask->vlan_id) {
1086                err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1087                                  vlan_key->vlan_id);
1088                if (err)
1089                        return err;
1090        }
1091        if (vlan_mask->vlan_priority) {
1092                err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1093                                 vlan_key->vlan_priority);
1094                if (err)
1095                        return err;
1096        }
1097        return 0;
1098}
1099
1100static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1101                            u32 *flower_key, u32 *flower_mask,
1102                            u32 flower_flag_bit, u32 dissector_flag_bit)
1103{
1104        if (dissector_mask & dissector_flag_bit) {
1105                *flower_mask |= flower_flag_bit;
1106                if (dissector_key & dissector_flag_bit)
1107                        *flower_key |= flower_flag_bit;
1108        }
1109}
1110
1111static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1112{
1113        u32 key, mask;
1114        __be32 _key, _mask;
1115        int err;
1116
1117        if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1118                return 0;
1119
1120        key = 0;
1121        mask = 0;
1122
1123        fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1124                        TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1125
1126        _key = cpu_to_be32(key);
1127        _mask = cpu_to_be32(mask);
1128
1129        err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1130        if (err)
1131                return err;
1132
1133        return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1134}
1135
1136static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1137                   struct sk_buff *skb, struct tcmsg *t)
1138{
1139        struct cls_fl_head *head = rtnl_dereference(tp->root);
1140        struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
1141        struct nlattr *nest;
1142        struct fl_flow_key *key, *mask;
1143
1144        if (!f)
1145                return skb->len;
1146
1147        t->tcm_handle = f->handle;
1148
1149        nest = nla_nest_start(skb, TCA_OPTIONS);
1150        if (!nest)
1151                goto nla_put_failure;
1152
1153        if (f->res.classid &&
1154            nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1155                goto nla_put_failure;
1156
1157        key = &f->key;
1158        mask = &head->mask.key;
1159
1160        if (mask->indev_ifindex) {
1161                struct net_device *dev;
1162
1163                dev = __dev_get_by_index(net, key->indev_ifindex);
1164                if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1165                        goto nla_put_failure;
1166        }
1167
1168        if (!tc_skip_hw(f->flags))
1169                fl_hw_update_stats(tp, f);
1170
1171        if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1172                            mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1173                            sizeof(key->eth.dst)) ||
1174            fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1175                            mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1176                            sizeof(key->eth.src)) ||
1177            fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1178                            &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1179                            sizeof(key->basic.n_proto)))
1180                goto nla_put_failure;
1181
1182        if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1183                goto nla_put_failure;
1184
1185        if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1186                goto nla_put_failure;
1187
1188        if ((key->basic.n_proto == htons(ETH_P_IP) ||
1189             key->basic.n_proto == htons(ETH_P_IPV6)) &&
1190            fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1191                            &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1192                            sizeof(key->basic.ip_proto)))
1193                goto nla_put_failure;
1194
1195        if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1196            (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1197                             &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1198                             sizeof(key->ipv4.src)) ||
1199             fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1200                             &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1201                             sizeof(key->ipv4.dst))))
1202                goto nla_put_failure;
1203        else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1204                 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1205                                  &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1206                                  sizeof(key->ipv6.src)) ||
1207                  fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1208                                  &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1209                                  sizeof(key->ipv6.dst))))
1210                goto nla_put_failure;
1211
1212        if (key->basic.ip_proto == IPPROTO_TCP &&
1213            (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1214                             &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1215                             sizeof(key->tp.src)) ||
1216             fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1217                             &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1218                             sizeof(key->tp.dst))))
1219                goto nla_put_failure;
1220        else if (key->basic.ip_proto == IPPROTO_UDP &&
1221                 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1222                                  &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1223                                  sizeof(key->tp.src)) ||
1224                  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1225                                  &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1226                                  sizeof(key->tp.dst))))
1227                goto nla_put_failure;
1228        else if (key->basic.ip_proto == IPPROTO_SCTP &&
1229                 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1230                                  &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1231                                  sizeof(key->tp.src)) ||
1232                  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1233                                  &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1234                                  sizeof(key->tp.dst))))
1235                goto nla_put_failure;
1236        else if (key->basic.n_proto == htons(ETH_P_IP) &&
1237                 key->basic.ip_proto == IPPROTO_ICMP &&
1238                 (fl_dump_key_val(skb, &key->icmp.type,
1239                                  TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1240                                  TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1241                                  sizeof(key->icmp.type)) ||
1242                  fl_dump_key_val(skb, &key->icmp.code,
1243                                  TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1244                                  TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1245                                  sizeof(key->icmp.code))))
1246                goto nla_put_failure;
1247        else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1248                 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1249                 (fl_dump_key_val(skb, &key->icmp.type,
1250                                  TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1251                                  TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1252                                  sizeof(key->icmp.type)) ||
1253                  fl_dump_key_val(skb, &key->icmp.code,
1254                                  TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1255                                  TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1256                                  sizeof(key->icmp.code))))
1257                goto nla_put_failure;
1258        else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1259                  key->basic.n_proto == htons(ETH_P_RARP)) &&
1260                 (fl_dump_key_val(skb, &key->arp.sip,
1261                                  TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1262                                  TCA_FLOWER_KEY_ARP_SIP_MASK,
1263                                  sizeof(key->arp.sip)) ||
1264                  fl_dump_key_val(skb, &key->arp.tip,
1265                                  TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1266                                  TCA_FLOWER_KEY_ARP_TIP_MASK,
1267                                  sizeof(key->arp.tip)) ||
1268                  fl_dump_key_val(skb, &key->arp.op,
1269                                  TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1270                                  TCA_FLOWER_KEY_ARP_OP_MASK,
1271                                  sizeof(key->arp.op)) ||
1272                  fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1273                                  mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1274                                  sizeof(key->arp.sha)) ||
1275                  fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1276                                  mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1277                                  sizeof(key->arp.tha))))
1278                goto nla_put_failure;
1279
1280        if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1281            (fl_dump_key_val(skb, &key->enc_ipv4.src,
1282                            TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1283                            TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1284                            sizeof(key->enc_ipv4.src)) ||
1285             fl_dump_key_val(skb, &key->enc_ipv4.dst,
1286                             TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1287                             TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1288                             sizeof(key->enc_ipv4.dst))))
1289                goto nla_put_failure;
1290        else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1291                 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1292                            TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1293                            TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1294                            sizeof(key->enc_ipv6.src)) ||
1295                 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1296                                 TCA_FLOWER_KEY_ENC_IPV6_DST,
1297                                 &mask->enc_ipv6.dst,
1298                                 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1299                            sizeof(key->enc_ipv6.dst))))
1300                goto nla_put_failure;
1301
1302        if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1303                            &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1304                            sizeof(key->enc_key_id)) ||
1305            fl_dump_key_val(skb, &key->enc_tp.src,
1306                            TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1307                            &mask->enc_tp.src,
1308                            TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1309                            sizeof(key->enc_tp.src)) ||
1310            fl_dump_key_val(skb, &key->enc_tp.dst,
1311                            TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1312                            &mask->enc_tp.dst,
1313                            TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1314                            sizeof(key->enc_tp.dst)))
1315                goto nla_put_failure;
1316
1317        if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1318                goto nla_put_failure;
1319
1320        if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1321                goto nla_put_failure;
1322
1323        if (tcf_exts_dump(skb, &f->exts))
1324                goto nla_put_failure;
1325
1326        nla_nest_end(skb, nest);
1327
1328        if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1329                goto nla_put_failure;
1330
1331        return skb->len;
1332
1333nla_put_failure:
1334        nla_nest_cancel(skb, nest);
1335        return -1;
1336}
1337
1338static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1339        .kind           = "flower",
1340        .classify       = fl_classify,
1341        .init           = fl_init,
1342        .destroy        = fl_destroy,
1343        .get            = fl_get,
1344        .change         = fl_change,
1345        .delete         = fl_delete,
1346        .walk           = fl_walk,
1347        .dump           = fl_dump,
1348        .owner          = THIS_MODULE,
1349};
1350
1351static int __init cls_fl_init(void)
1352{
1353        return register_tcf_proto_ops(&cls_fl_ops);
1354}
1355
1356static void __exit cls_fl_exit(void)
1357{
1358        unregister_tcf_proto_ops(&cls_fl_ops);
1359}
1360
1361module_init(cls_fl_init);
1362module_exit(cls_fl_exit);
1363
1364MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1365MODULE_DESCRIPTION("Flower classifier");
1366MODULE_LICENSE("GPL v2");
1367