linux/include/linux/netfilter/ipset/ip_set.h
<<
>>
Prefs
   1/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
   2 *                         Patrick Schaaf <bof@bof.de>
   3 *                         Martin Josefsson <gandalf@wlug.westbo.se>
   4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
   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 version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10#ifndef _IP_SET_H
  11#define _IP_SET_H
  12
  13#include <linux/ip.h>
  14#include <linux/ipv6.h>
  15#include <linux/netlink.h>
  16#include <linux/netfilter.h>
  17#include <linux/netfilter/x_tables.h>
  18#include <linux/stringify.h>
  19#include <linux/vmalloc.h>
  20#include <net/netlink.h>
  21#include <uapi/linux/netfilter/ipset/ip_set.h>
  22
  23#define _IP_SET_MODULE_DESC(a, b, c)            \
  24        MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
  25#define IP_SET_MODULE_DESC(a, b, c)             \
  26        _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
  27
  28/* Set features */
  29enum ip_set_feature {
  30        IPSET_TYPE_IP_FLAG = 0,
  31        IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
  32        IPSET_TYPE_PORT_FLAG = 1,
  33        IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
  34        IPSET_TYPE_MAC_FLAG = 2,
  35        IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
  36        IPSET_TYPE_IP2_FLAG = 3,
  37        IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
  38        IPSET_TYPE_NAME_FLAG = 4,
  39        IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
  40        IPSET_TYPE_IFACE_FLAG = 5,
  41        IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
  42        IPSET_TYPE_MARK_FLAG = 6,
  43        IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
  44        IPSET_TYPE_NOMATCH_FLAG = 7,
  45        IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
  46        /* Strictly speaking not a feature, but a flag for dumping:
  47         * this settype must be dumped last */
  48        IPSET_DUMP_LAST_FLAG = 8,
  49        IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
  50};
  51
  52/* Set extensions */
  53enum ip_set_extension {
  54        IPSET_EXT_BIT_TIMEOUT = 0,
  55        IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
  56        IPSET_EXT_BIT_COUNTER = 1,
  57        IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
  58        IPSET_EXT_BIT_COMMENT = 2,
  59        IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
  60        IPSET_EXT_BIT_SKBINFO = 3,
  61        IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
  62        /* Mark set with an extension which needs to call destroy */
  63        IPSET_EXT_BIT_DESTROY = 7,
  64        IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
  65};
  66
  67#define SET_WITH_TIMEOUT(s)     ((s)->extensions & IPSET_EXT_TIMEOUT)
  68#define SET_WITH_COUNTER(s)     ((s)->extensions & IPSET_EXT_COUNTER)
  69#define SET_WITH_COMMENT(s)     ((s)->extensions & IPSET_EXT_COMMENT)
  70#define SET_WITH_SKBINFO(s)     ((s)->extensions & IPSET_EXT_SKBINFO)
  71#define SET_WITH_FORCEADD(s)    ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
  72
  73/* Extension id, in size order */
  74enum ip_set_ext_id {
  75        IPSET_EXT_ID_COUNTER = 0,
  76        IPSET_EXT_ID_TIMEOUT,
  77        IPSET_EXT_ID_SKBINFO,
  78        IPSET_EXT_ID_COMMENT,
  79        IPSET_EXT_ID_MAX,
  80};
  81
  82/* Extension type */
  83struct ip_set_ext_type {
  84        /* Destroy extension private data (can be NULL) */
  85        void (*destroy)(void *ext);
  86        enum ip_set_extension type;
  87        enum ipset_cadt_flags flag;
  88        /* Size and minimal alignment */
  89        u8 len;
  90        u8 align;
  91};
  92
  93extern const struct ip_set_ext_type ip_set_extensions[];
  94
  95struct ip_set_ext {
  96        u64 packets;
  97        u64 bytes;
  98        u32 timeout;
  99        u32 skbmark;
 100        u32 skbmarkmask;
 101        u32 skbprio;
 102        u16 skbqueue;
 103        char *comment;
 104};
 105
 106struct ip_set_counter {
 107        atomic64_t bytes;
 108        atomic64_t packets;
 109};
 110
 111struct ip_set_comment {
 112        char *str;
 113};
 114
 115struct ip_set_skbinfo {
 116        u32 skbmark;
 117        u32 skbmarkmask;
 118        u32 skbprio;
 119        u16 skbqueue;
 120};
 121
 122struct ip_set;
 123
 124#define ext_timeout(e, s)       \
 125(unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
 126#define ext_counter(e, s)       \
 127(struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
 128#define ext_comment(e, s)       \
 129(struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT])
 130#define ext_skbinfo(e, s)       \
 131(struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO])
 132
 133typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
 134                           const struct ip_set_ext *ext,
 135                           struct ip_set_ext *mext, u32 cmdflags);
 136
 137/* Kernel API function options */
 138struct ip_set_adt_opt {
 139        u8 family;              /* Actual protocol family */
 140        u8 dim;                 /* Dimension of match/target */
 141        u8 flags;               /* Direction and negation flags */
 142        u32 cmdflags;           /* Command-like flags */
 143        struct ip_set_ext ext;  /* Extensions */
 144};
 145
 146/* Set type, variant-specific part */
 147struct ip_set_type_variant {
 148        /* Kernelspace: test/add/del entries
 149         *              returns negative error code,
 150         *                      zero for no match/success to add/delete
 151         *                      positive for matching element */
 152        int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
 153                    const struct xt_action_param *par,
 154                    enum ipset_adt adt, struct ip_set_adt_opt *opt);
 155
 156        /* Userspace: test/add/del entries
 157         *              returns negative error code,
 158         *                      zero for no match/success to add/delete
 159         *                      positive for matching element */
 160        int (*uadt)(struct ip_set *set, struct nlattr *tb[],
 161                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
 162
 163        /* Low level add/del/test functions */
 164        ipset_adtfn adt[IPSET_ADT_MAX];
 165
 166        /* When adding entries and set is full, try to resize the set */
 167        int (*resize)(struct ip_set *set, bool retried);
 168        /* Destroy the set */
 169        void (*destroy)(struct ip_set *set);
 170        /* Flush the elements */
 171        void (*flush)(struct ip_set *set);
 172        /* Expire entries before listing */
 173        void (*expire)(struct ip_set *set);
 174        /* List set header data */
 175        int (*head)(struct ip_set *set, struct sk_buff *skb);
 176        /* List elements */
 177        int (*list)(const struct ip_set *set, struct sk_buff *skb,
 178                    struct netlink_callback *cb);
 179
 180        /* Return true if "b" set is the same as "a"
 181         * according to the create set parameters */
 182        bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
 183};
 184
 185/* The core set type structure */
 186struct ip_set_type {
 187        struct list_head list;
 188
 189        /* Typename */
 190        char name[IPSET_MAXNAMELEN];
 191        /* Protocol version */
 192        u8 protocol;
 193        /* Set type dimension */
 194        u8 dimension;
 195        /*
 196         * Supported family: may be NFPROTO_UNSPEC for both
 197         * NFPROTO_IPV4/NFPROTO_IPV6.
 198         */
 199        u8 family;
 200        /* Type revisions */
 201        u8 revision_min, revision_max;
 202        /* Set features to control swapping */
 203        u16 features;
 204
 205        /* Create set */
 206        int (*create)(struct net *net, struct ip_set *set,
 207                      struct nlattr *tb[], u32 flags);
 208
 209        /* Attribute policies */
 210        const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
 211        const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
 212
 213        /* Set this to THIS_MODULE if you are a module, otherwise NULL */
 214        struct module *me;
 215};
 216
 217/* register and unregister set type */
 218extern int ip_set_type_register(struct ip_set_type *set_type);
 219extern void ip_set_type_unregister(struct ip_set_type *set_type);
 220
 221/* A generic IP set */
 222struct ip_set {
 223        /* The name of the set */
 224        char name[IPSET_MAXNAMELEN];
 225        /* Lock protecting the set data */
 226        rwlock_t lock;
 227        /* References to the set */
 228        u32 ref;
 229        /* The core set type */
 230        struct ip_set_type *type;
 231        /* The type variant doing the real job */
 232        const struct ip_set_type_variant *variant;
 233        /* The actual INET family of the set */
 234        u8 family;
 235        /* The type revision */
 236        u8 revision;
 237        /* Extensions */
 238        u8 extensions;
 239        /* Create flags */
 240        u8 flags;
 241        /* Default timeout value, if enabled */
 242        u32 timeout;
 243        /* Element data size */
 244        size_t dsize;
 245        /* Offsets to extensions in elements */
 246        size_t offset[IPSET_EXT_ID_MAX];
 247        /* The type specific data */
 248        void *data;
 249};
 250
 251static inline void
 252ip_set_ext_destroy(struct ip_set *set, void *data)
 253{
 254        /* Check that the extension is enabled for the set and
 255         * call it's destroy function for its extension part in data.
 256         */
 257        if (SET_WITH_COMMENT(set))
 258                ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
 259                        ext_comment(data, set));
 260}
 261
 262static inline int
 263ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
 264{
 265        u32 cadt_flags = 0;
 266
 267        if (SET_WITH_TIMEOUT(set))
 268                if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
 269                                           htonl(set->timeout))))
 270                        return -EMSGSIZE;
 271        if (SET_WITH_COUNTER(set))
 272                cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
 273        if (SET_WITH_COMMENT(set))
 274                cadt_flags |= IPSET_FLAG_WITH_COMMENT;
 275        if (SET_WITH_SKBINFO(set))
 276                cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
 277        if (SET_WITH_FORCEADD(set))
 278                cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
 279
 280        if (!cadt_flags)
 281                return 0;
 282        return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
 283}
 284
 285static inline void
 286ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
 287{
 288        atomic64_add((long long)bytes, &(counter)->bytes);
 289}
 290
 291static inline void
 292ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
 293{
 294        atomic64_add((long long)packets, &(counter)->packets);
 295}
 296
 297static inline u64
 298ip_set_get_bytes(const struct ip_set_counter *counter)
 299{
 300        return (u64)atomic64_read(&(counter)->bytes);
 301}
 302
 303static inline u64
 304ip_set_get_packets(const struct ip_set_counter *counter)
 305{
 306        return (u64)atomic64_read(&(counter)->packets);
 307}
 308
 309static inline void
 310ip_set_update_counter(struct ip_set_counter *counter,
 311                      const struct ip_set_ext *ext,
 312                      struct ip_set_ext *mext, u32 flags)
 313{
 314        if (ext->packets != ULLONG_MAX &&
 315            !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
 316                ip_set_add_bytes(ext->bytes, counter);
 317                ip_set_add_packets(ext->packets, counter);
 318        }
 319        if (flags & IPSET_FLAG_MATCH_COUNTERS) {
 320                mext->packets = ip_set_get_packets(counter);
 321                mext->bytes = ip_set_get_bytes(counter);
 322        }
 323}
 324
 325static inline void
 326ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
 327                      const struct ip_set_ext *ext,
 328                      struct ip_set_ext *mext, u32 flags)
 329{
 330                mext->skbmark = skbinfo->skbmark;
 331                mext->skbmarkmask = skbinfo->skbmarkmask;
 332                mext->skbprio = skbinfo->skbprio;
 333                mext->skbqueue = skbinfo->skbqueue;
 334}
 335static inline bool
 336ip_set_put_skbinfo(struct sk_buff *skb, struct ip_set_skbinfo *skbinfo)
 337{
 338        /* Send nonzero parameters only */
 339        return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
 340                nla_put_net64(skb, IPSET_ATTR_SKBMARK,
 341                              cpu_to_be64((u64)skbinfo->skbmark << 32 |
 342                                          skbinfo->skbmarkmask))) ||
 343               (skbinfo->skbprio &&
 344                nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
 345                              cpu_to_be32(skbinfo->skbprio))) ||
 346               (skbinfo->skbqueue &&
 347                nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
 348                             cpu_to_be16(skbinfo->skbqueue)));
 349
 350}
 351
 352static inline void
 353ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
 354                    const struct ip_set_ext *ext)
 355{
 356        skbinfo->skbmark = ext->skbmark;
 357        skbinfo->skbmarkmask = ext->skbmarkmask;
 358        skbinfo->skbprio = ext->skbprio;
 359        skbinfo->skbqueue = ext->skbqueue;
 360}
 361
 362static inline bool
 363ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
 364{
 365        return nla_put_net64(skb, IPSET_ATTR_BYTES,
 366                             cpu_to_be64(ip_set_get_bytes(counter))) ||
 367               nla_put_net64(skb, IPSET_ATTR_PACKETS,
 368                             cpu_to_be64(ip_set_get_packets(counter)));
 369}
 370
 371static inline void
 372ip_set_init_counter(struct ip_set_counter *counter,
 373                    const struct ip_set_ext *ext)
 374{
 375        if (ext->bytes != ULLONG_MAX)
 376                atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
 377        if (ext->packets != ULLONG_MAX)
 378                atomic64_set(&(counter)->packets, (long long)(ext->packets));
 379}
 380
 381/* Netlink CB args */
 382enum {
 383        IPSET_CB_NET = 0,
 384        IPSET_CB_DUMP,
 385        IPSET_CB_INDEX,
 386        IPSET_CB_ARG0,
 387        IPSET_CB_ARG1,
 388        IPSET_CB_ARG2,
 389};
 390
 391/* register and unregister set references */
 392extern ip_set_id_t ip_set_get_byname(struct net *net,
 393                                     const char *name, struct ip_set **set);
 394extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
 395extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
 396extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
 397extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
 398
 399/* API for iptables set match, and SET target */
 400
 401extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
 402                      const struct xt_action_param *par,
 403                      struct ip_set_adt_opt *opt);
 404extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
 405                      const struct xt_action_param *par,
 406                      struct ip_set_adt_opt *opt);
 407extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
 408                       const struct xt_action_param *par,
 409                       struct ip_set_adt_opt *opt);
 410
 411/* Utility functions */
 412extern void *ip_set_alloc(size_t size);
 413extern void ip_set_free(void *members);
 414extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
 415extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
 416extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
 417                              size_t len);
 418extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
 419                                 struct ip_set_ext *ext);
 420
 421static inline int
 422ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
 423{
 424        __be32 ip;
 425        int ret = ip_set_get_ipaddr4(nla, &ip);
 426
 427        if (ret)
 428                return ret;
 429        *ipaddr = ntohl(ip);
 430        return 0;
 431}
 432
 433/* Ignore IPSET_ERR_EXIST errors if asked to do so? */
 434static inline bool
 435ip_set_eexist(int ret, u32 flags)
 436{
 437        return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
 438}
 439
 440/* Match elements marked with nomatch */
 441static inline bool
 442ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
 443{
 444        return adt == IPSET_TEST &&
 445               (set->type->features & IPSET_TYPE_NOMATCH) &&
 446               ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
 447               (ret > 0 || ret == -ENOTEMPTY);
 448}
 449
 450/* Check the NLA_F_NET_BYTEORDER flag */
 451static inline bool
 452ip_set_attr_netorder(struct nlattr *tb[], int type)
 453{
 454        return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
 455}
 456
 457static inline bool
 458ip_set_optattr_netorder(struct nlattr *tb[], int type)
 459{
 460        return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
 461}
 462
 463/* Useful converters */
 464static inline u32
 465ip_set_get_h32(const struct nlattr *attr)
 466{
 467        return ntohl(nla_get_be32(attr));
 468}
 469
 470static inline u16
 471ip_set_get_h16(const struct nlattr *attr)
 472{
 473        return ntohs(nla_get_be16(attr));
 474}
 475
 476#define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
 477#define ipset_nest_end(skb, start)  nla_nest_end(skb, start)
 478
 479static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
 480{
 481        struct nlattr *__nested = ipset_nest_start(skb, type);
 482        int ret;
 483
 484        if (!__nested)
 485                return -EMSGSIZE;
 486        ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
 487        if (!ret)
 488                ipset_nest_end(skb, __nested);
 489        return ret;
 490}
 491
 492static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
 493                                  const struct in6_addr *ipaddrptr)
 494{
 495        struct nlattr *__nested = ipset_nest_start(skb, type);
 496        int ret;
 497
 498        if (!__nested)
 499                return -EMSGSIZE;
 500        ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
 501                      sizeof(struct in6_addr), ipaddrptr);
 502        if (!ret)
 503                ipset_nest_end(skb, __nested);
 504        return ret;
 505}
 506
 507/* Get address from skbuff */
 508static inline __be32
 509ip4addr(const struct sk_buff *skb, bool src)
 510{
 511        return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
 512}
 513
 514static inline void
 515ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
 516{
 517        *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
 518}
 519
 520static inline void
 521ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
 522{
 523        memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
 524               sizeof(*addr));
 525}
 526
 527/* Calculate the bytes required to store the inclusive range of a-b */
 528static inline int
 529bitmap_bytes(u32 a, u32 b)
 530{
 531        return 4 * ((((b - a + 8) / 8) + 3) / 4);
 532}
 533
 534#include <linux/netfilter/ipset/ip_set_timeout.h>
 535#include <linux/netfilter/ipset/ip_set_comment.h>
 536
 537static inline int
 538ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
 539                      const void *e, bool active)
 540{
 541        if (SET_WITH_TIMEOUT(set)) {
 542                unsigned long *timeout = ext_timeout(e, set);
 543
 544                if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
 545                        htonl(active ? ip_set_timeout_get(timeout)
 546                                : *timeout)))
 547                        return -EMSGSIZE;
 548        }
 549        if (SET_WITH_COUNTER(set) &&
 550            ip_set_put_counter(skb, ext_counter(e, set)))
 551                return -EMSGSIZE;
 552        if (SET_WITH_COMMENT(set) &&
 553            ip_set_put_comment(skb, ext_comment(e, set)))
 554                return -EMSGSIZE;
 555        if (SET_WITH_SKBINFO(set) &&
 556            ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
 557                return -EMSGSIZE;
 558        return 0;
 559}
 560
 561#define IP_SET_INIT_KEXT(skb, opt, set)                 \
 562        { .bytes = (skb)->len, .packets = 1,            \
 563          .timeout = ip_set_adt_opt_timeout(opt, set) }
 564
 565#define IP_SET_INIT_UEXT(set)                           \
 566        { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
 567          .timeout = (set)->timeout }
 568
 569#define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
 570
 571#define IPSET_CONCAT(a, b)              a##b
 572#define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
 573
 574#endif /*_IP_SET_H */
 575