linux/include/net/netfilter/nf_flow_table.h
<<
>>
Prefs
   1#ifndef _NF_FLOW_TABLE_H
   2#define _NF_FLOW_TABLE_H
   3
   4#include <linux/in.h>
   5#include <linux/in6.h>
   6#include <linux/netdevice.h>
   7#include <linux/rhashtable-types.h>
   8#include <linux/rcupdate.h>
   9#include <linux/netfilter/nf_conntrack_tuple_common.h>
  10#include <net/flow_offload.h>
  11#include <net/dst.h>
  12
  13struct nf_flowtable;
  14struct nf_flow_rule;
  15struct flow_offload;
  16enum flow_offload_tuple_dir;
  17
  18struct nf_flow_key {
  19        struct flow_dissector_key_meta                  meta;
  20        struct flow_dissector_key_control               control;
  21        struct flow_dissector_key_control               enc_control;
  22        struct flow_dissector_key_basic                 basic;
  23        union {
  24                struct flow_dissector_key_ipv4_addrs    ipv4;
  25                struct flow_dissector_key_ipv6_addrs    ipv6;
  26        };
  27        struct flow_dissector_key_keyid                 enc_key_id;
  28        union {
  29                struct flow_dissector_key_ipv4_addrs    enc_ipv4;
  30                struct flow_dissector_key_ipv6_addrs    enc_ipv6;
  31        };
  32        struct flow_dissector_key_tcp                   tcp;
  33        struct flow_dissector_key_ports                 tp;
  34} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  35
  36struct nf_flow_match {
  37        struct flow_dissector   dissector;
  38        struct nf_flow_key      key;
  39        struct nf_flow_key      mask;
  40};
  41
  42struct nf_flow_rule {
  43        struct nf_flow_match    match;
  44        struct flow_rule        *rule;
  45};
  46
  47struct nf_flowtable_type {
  48        struct list_head                list;
  49        int                             family;
  50        int                             (*init)(struct nf_flowtable *ft);
  51        int                             (*setup)(struct nf_flowtable *ft,
  52                                                 struct net_device *dev,
  53                                                 enum flow_block_command cmd);
  54        int                             (*action)(struct net *net,
  55                                                  const struct flow_offload *flow,
  56                                                  enum flow_offload_tuple_dir dir,
  57                                                  struct nf_flow_rule *flow_rule);
  58        void                            (*free)(struct nf_flowtable *ft);
  59        nf_hookfn                       *hook;
  60        struct module                   *owner;
  61};
  62
  63enum nf_flowtable_flags {
  64        NF_FLOWTABLE_HW_OFFLOAD         = 0x1,  /* NFT_FLOWTABLE_HW_OFFLOAD */
  65        NF_FLOWTABLE_COUNTER            = 0x2,  /* NFT_FLOWTABLE_COUNTER */
  66};
  67
  68struct nf_flowtable {
  69        struct list_head                list;
  70        struct rhashtable               rhashtable;
  71        int                             priority;
  72        const struct nf_flowtable_type  *type;
  73        struct delayed_work             gc_work;
  74        unsigned int                    flags;
  75        struct flow_block               flow_block;
  76        struct rw_semaphore             flow_block_lock; /* Guards flow_block */
  77        possible_net_t                  net;
  78};
  79
  80static inline bool nf_flowtable_hw_offload(struct nf_flowtable *flowtable)
  81{
  82        return flowtable->flags & NF_FLOWTABLE_HW_OFFLOAD;
  83}
  84
  85enum flow_offload_tuple_dir {
  86        FLOW_OFFLOAD_DIR_ORIGINAL = IP_CT_DIR_ORIGINAL,
  87        FLOW_OFFLOAD_DIR_REPLY = IP_CT_DIR_REPLY,
  88        FLOW_OFFLOAD_DIR_MAX = IP_CT_DIR_MAX
  89};
  90
  91struct flow_offload_tuple {
  92        union {
  93                struct in_addr          src_v4;
  94                struct in6_addr         src_v6;
  95        };
  96        union {
  97                struct in_addr          dst_v4;
  98                struct in6_addr         dst_v6;
  99        };
 100        struct {
 101                __be16                  src_port;
 102                __be16                  dst_port;
 103        };
 104
 105        int                             iifidx;
 106
 107        u8                              l3proto;
 108        u8                              l4proto;
 109        u8                              dir;
 110
 111        u16                             mtu;
 112
 113        struct dst_entry                *dst_cache;
 114};
 115
 116struct flow_offload_tuple_rhash {
 117        struct rhash_head               node;
 118        struct flow_offload_tuple       tuple;
 119};
 120
 121enum nf_flow_flags {
 122        NF_FLOW_SNAT,
 123        NF_FLOW_DNAT,
 124        NF_FLOW_TEARDOWN,
 125        NF_FLOW_HW,
 126        NF_FLOW_HW_DYING,
 127        NF_FLOW_HW_DEAD,
 128        NF_FLOW_HW_PENDING,
 129};
 130
 131enum flow_offload_type {
 132        NF_FLOW_OFFLOAD_UNSPEC  = 0,
 133        NF_FLOW_OFFLOAD_ROUTE,
 134};
 135
 136struct flow_offload {
 137        struct flow_offload_tuple_rhash         tuplehash[FLOW_OFFLOAD_DIR_MAX];
 138        struct nf_conn                          *ct;
 139        unsigned long                           flags;
 140        u16                                     type;
 141        u32                                     timeout;
 142        struct rcu_head                         rcu_head;
 143};
 144
 145#define NF_FLOW_TIMEOUT (30 * HZ)
 146#define nf_flowtable_time_stamp (u32)jiffies
 147
 148unsigned long flow_offload_get_timeout(struct flow_offload *flow);
 149
 150static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
 151{
 152        return (__s32)(timeout - nf_flowtable_time_stamp);
 153}
 154
 155struct nf_flow_route {
 156        struct {
 157                struct dst_entry        *dst;
 158        } tuple[FLOW_OFFLOAD_DIR_MAX];
 159};
 160
 161struct flow_offload *flow_offload_alloc(struct nf_conn *ct);
 162void flow_offload_free(struct flow_offload *flow);
 163
 164static inline int
 165nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
 166                             flow_setup_cb_t *cb, void *cb_priv)
 167{
 168        struct flow_block *block = &flow_table->flow_block;
 169        struct flow_block_cb *block_cb;
 170        int err = 0;
 171
 172        down_write(&flow_table->flow_block_lock);
 173        block_cb = flow_block_cb_lookup(block, cb, cb_priv);
 174        if (block_cb) {
 175                err = -EEXIST;
 176                goto unlock;
 177        }
 178
 179        block_cb = flow_block_cb_alloc(cb, cb_priv, cb_priv, NULL);
 180        if (IS_ERR(block_cb)) {
 181                err = PTR_ERR(block_cb);
 182                goto unlock;
 183        }
 184
 185        list_add_tail(&block_cb->list, &block->cb_list);
 186
 187unlock:
 188        up_write(&flow_table->flow_block_lock);
 189        return err;
 190}
 191
 192static inline void
 193nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
 194                             flow_setup_cb_t *cb, void *cb_priv)
 195{
 196        struct flow_block *block = &flow_table->flow_block;
 197        struct flow_block_cb *block_cb;
 198
 199        down_write(&flow_table->flow_block_lock);
 200        block_cb = flow_block_cb_lookup(block, cb, cb_priv);
 201        if (block_cb) {
 202                list_del(&block_cb->list);
 203                flow_block_cb_free(block_cb);
 204        } else {
 205                WARN_ON(true);
 206        }
 207        up_write(&flow_table->flow_block_lock);
 208}
 209
 210int flow_offload_route_init(struct flow_offload *flow,
 211                            const struct nf_flow_route *route);
 212
 213int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
 214void flow_offload_refresh(struct nf_flowtable *flow_table,
 215                          struct flow_offload *flow);
 216
 217struct flow_offload_tuple_rhash *flow_offload_lookup(struct nf_flowtable *flow_table,
 218                                                     struct flow_offload_tuple *tuple);
 219void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
 220                              struct net_device *dev);
 221void nf_flow_table_cleanup(struct net_device *dev);
 222
 223int nf_flow_table_init(struct nf_flowtable *flow_table);
 224void nf_flow_table_free(struct nf_flowtable *flow_table);
 225
 226void flow_offload_teardown(struct flow_offload *flow);
 227
 228int nf_flow_snat_port(const struct flow_offload *flow,
 229                      struct sk_buff *skb, unsigned int thoff,
 230                      u8 protocol, enum flow_offload_tuple_dir dir);
 231int nf_flow_dnat_port(const struct flow_offload *flow,
 232                      struct sk_buff *skb, unsigned int thoff,
 233                      u8 protocol, enum flow_offload_tuple_dir dir);
 234
 235struct flow_ports {
 236        __be16 source, dest;
 237};
 238
 239unsigned int nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
 240                                     const struct nf_hook_state *state);
 241unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
 242                                       const struct nf_hook_state *state);
 243
 244#define MODULE_ALIAS_NF_FLOWTABLE(family)       \
 245        MODULE_ALIAS("nf-flowtable-" __stringify(family))
 246
 247void nf_flow_offload_add(struct nf_flowtable *flowtable,
 248                         struct flow_offload *flow);
 249void nf_flow_offload_del(struct nf_flowtable *flowtable,
 250                         struct flow_offload *flow);
 251void nf_flow_offload_stats(struct nf_flowtable *flowtable,
 252                           struct flow_offload *flow);
 253
 254void nf_flow_table_offload_flush(struct nf_flowtable *flowtable);
 255int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
 256                                struct net_device *dev,
 257                                enum flow_block_command cmd);
 258int nf_flow_rule_route_ipv4(struct net *net, const struct flow_offload *flow,
 259                            enum flow_offload_tuple_dir dir,
 260                            struct nf_flow_rule *flow_rule);
 261int nf_flow_rule_route_ipv6(struct net *net, const struct flow_offload *flow,
 262                            enum flow_offload_tuple_dir dir,
 263                            struct nf_flow_rule *flow_rule);
 264
 265int nf_flow_table_offload_init(void);
 266void nf_flow_table_offload_exit(void);
 267
 268#endif /* _NF_FLOW_TABLE_H */
 269