root/include/linux/netfilter/ipset/ip_set.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. ip_set_ext_destroy
  2. ip_set_put_flags
  3. ip_set_get_hostipaddr4
  4. ip_set_eexist
  5. ip_set_enomatch
  6. ip_set_attr_netorder
  7. ip_set_optattr_netorder
  8. ip_set_get_h32
  9. ip_set_get_h16
  10. nla_put_ipaddr4
  11. nla_put_ipaddr6
  12. ip4addr
  13. ip4addrptr
  14. ip6addrptr
  15. ip_set_timeout_uget
  16. ip_set_timeout_expired
  17. ip_set_timeout_set
  18. ip_set_timeout_get
  19. ip_set_comment_uget
  20. ip_set_init_comment
  21. ip_set_put_comment
  22. ip_set_comment_free
  23. ip_set_add_bytes
  24. ip_set_add_packets
  25. ip_set_get_bytes
  26. ip_set_get_packets
  27. ip_set_match_counter
  28. ip_set_update_counter
  29. ip_set_put_counter
  30. ip_set_init_counter
  31. ip_set_get_skbinfo
  32. ip_set_put_skbinfo
  33. ip_set_init_skbinfo

   1 /* SPDX-License-Identifier: GPL-2.0-only */
   2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
   3  *                         Patrick Schaaf <bof@bof.de>
   4  *                         Martin Josefsson <gandalf@wlug.westbo.se>
   5  * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
   6  */
   7 #ifndef _IP_SET_H
   8 #define _IP_SET_H
   9 
  10 #include <linux/ip.h>
  11 #include <linux/ipv6.h>
  12 #include <linux/netlink.h>
  13 #include <linux/netfilter.h>
  14 #include <linux/netfilter/x_tables.h>
  15 #include <linux/stringify.h>
  16 #include <linux/vmalloc.h>
  17 #include <net/netlink.h>
  18 #include <uapi/linux/netfilter/ipset/ip_set.h>
  19 
  20 #define _IP_SET_MODULE_DESC(a, b, c)            \
  21         MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
  22 #define IP_SET_MODULE_DESC(a, b, c)             \
  23         _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
  24 
  25 /* Set features */
  26 enum ip_set_feature {
  27         IPSET_TYPE_IP_FLAG = 0,
  28         IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
  29         IPSET_TYPE_PORT_FLAG = 1,
  30         IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
  31         IPSET_TYPE_MAC_FLAG = 2,
  32         IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
  33         IPSET_TYPE_IP2_FLAG = 3,
  34         IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
  35         IPSET_TYPE_NAME_FLAG = 4,
  36         IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
  37         IPSET_TYPE_IFACE_FLAG = 5,
  38         IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
  39         IPSET_TYPE_MARK_FLAG = 6,
  40         IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
  41         IPSET_TYPE_NOMATCH_FLAG = 7,
  42         IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
  43         /* Strictly speaking not a feature, but a flag for dumping:
  44          * this settype must be dumped last */
  45         IPSET_DUMP_LAST_FLAG = 8,
  46         IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
  47 };
  48 
  49 /* Set extensions */
  50 enum ip_set_extension {
  51         IPSET_EXT_BIT_TIMEOUT = 0,
  52         IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
  53         IPSET_EXT_BIT_COUNTER = 1,
  54         IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
  55         IPSET_EXT_BIT_COMMENT = 2,
  56         IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
  57         IPSET_EXT_BIT_SKBINFO = 3,
  58         IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
  59         /* Mark set with an extension which needs to call destroy */
  60         IPSET_EXT_BIT_DESTROY = 7,
  61         IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
  62 };
  63 
  64 #define SET_WITH_TIMEOUT(s)     ((s)->extensions & IPSET_EXT_TIMEOUT)
  65 #define SET_WITH_COUNTER(s)     ((s)->extensions & IPSET_EXT_COUNTER)
  66 #define SET_WITH_COMMENT(s)     ((s)->extensions & IPSET_EXT_COMMENT)
  67 #define SET_WITH_SKBINFO(s)     ((s)->extensions & IPSET_EXT_SKBINFO)
  68 #define SET_WITH_FORCEADD(s)    ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
  69 
  70 /* Extension id, in size order */
  71 enum ip_set_ext_id {
  72         IPSET_EXT_ID_COUNTER = 0,
  73         IPSET_EXT_ID_TIMEOUT,
  74         IPSET_EXT_ID_SKBINFO,
  75         IPSET_EXT_ID_COMMENT,
  76         IPSET_EXT_ID_MAX,
  77 };
  78 
  79 struct ip_set;
  80 
  81 /* Extension type */
  82 struct ip_set_ext_type {
  83         /* Destroy extension private data (can be NULL) */
  84         void (*destroy)(struct ip_set *set, void *ext);
  85         enum ip_set_extension type;
  86         enum ipset_cadt_flags flag;
  87         /* Size and minimal alignment */
  88         u8 len;
  89         u8 align;
  90 };
  91 
  92 extern const struct ip_set_ext_type ip_set_extensions[];
  93 
  94 struct ip_set_counter {
  95         atomic64_t bytes;
  96         atomic64_t packets;
  97 };
  98 
  99 struct ip_set_comment_rcu {
 100         struct rcu_head rcu;
 101         char str[0];
 102 };
 103 
 104 struct ip_set_comment {
 105         struct ip_set_comment_rcu __rcu *c;
 106 };
 107 
 108 struct ip_set_skbinfo {
 109         u32 skbmark;
 110         u32 skbmarkmask;
 111         u32 skbprio;
 112         u16 skbqueue;
 113         u16 __pad;
 114 };
 115 
 116 struct ip_set_ext {
 117         struct ip_set_skbinfo skbinfo;
 118         u64 packets;
 119         u64 bytes;
 120         char *comment;
 121         u32 timeout;
 122         u8 packets_op;
 123         u8 bytes_op;
 124         bool target;
 125 };
 126 
 127 struct ip_set;
 128 
 129 #define ext_timeout(e, s)       \
 130 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
 131 #define ext_counter(e, s)       \
 132 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
 133 #define ext_comment(e, s)       \
 134 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
 135 #define ext_skbinfo(e, s)       \
 136 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
 137 
 138 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
 139                            const struct ip_set_ext *ext,
 140                            struct ip_set_ext *mext, u32 cmdflags);
 141 
 142 /* Kernel API function options */
 143 struct ip_set_adt_opt {
 144         u8 family;              /* Actual protocol family */
 145         u8 dim;                 /* Dimension of match/target */
 146         u8 flags;               /* Direction and negation flags */
 147         u32 cmdflags;           /* Command-like flags */
 148         struct ip_set_ext ext;  /* Extensions */
 149 };
 150 
 151 /* Set type, variant-specific part */
 152 struct ip_set_type_variant {
 153         /* Kernelspace: test/add/del entries
 154          *              returns negative error code,
 155          *                      zero for no match/success to add/delete
 156          *                      positive for matching element */
 157         int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
 158                     const struct xt_action_param *par,
 159                     enum ipset_adt adt, struct ip_set_adt_opt *opt);
 160 
 161         /* Userspace: test/add/del entries
 162          *              returns negative error code,
 163          *                      zero for no match/success to add/delete
 164          *                      positive for matching element */
 165         int (*uadt)(struct ip_set *set, struct nlattr *tb[],
 166                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
 167 
 168         /* Low level add/del/test functions */
 169         ipset_adtfn adt[IPSET_ADT_MAX];
 170 
 171         /* When adding entries and set is full, try to resize the set */
 172         int (*resize)(struct ip_set *set, bool retried);
 173         /* Destroy the set */
 174         void (*destroy)(struct ip_set *set);
 175         /* Flush the elements */
 176         void (*flush)(struct ip_set *set);
 177         /* Expire entries before listing */
 178         void (*expire)(struct ip_set *set);
 179         /* List set header data */
 180         int (*head)(struct ip_set *set, struct sk_buff *skb);
 181         /* List elements */
 182         int (*list)(const struct ip_set *set, struct sk_buff *skb,
 183                     struct netlink_callback *cb);
 184         /* Keep listing private when resizing runs parallel */
 185         void (*uref)(struct ip_set *set, struct netlink_callback *cb,
 186                      bool start);
 187 
 188         /* Return true if "b" set is the same as "a"
 189          * according to the create set parameters */
 190         bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
 191         /* Region-locking is used */
 192         bool region_lock;
 193 };
 194 
 195 struct ip_set_region {
 196         spinlock_t lock;        /* Region lock */
 197         size_t ext_size;        /* Size of the dynamic extensions */
 198         u32 elements;           /* Number of elements vs timeout */
 199 };
 200 
 201 /* The core set type structure */
 202 struct ip_set_type {
 203         struct list_head list;
 204 
 205         /* Typename */
 206         char name[IPSET_MAXNAMELEN];
 207         /* Protocol version */
 208         u8 protocol;
 209         /* Set type dimension */
 210         u8 dimension;
 211         /*
 212          * Supported family: may be NFPROTO_UNSPEC for both
 213          * NFPROTO_IPV4/NFPROTO_IPV6.
 214          */
 215         u8 family;
 216         /* Type revisions */
 217         u8 revision_min, revision_max;
 218         /* Set features to control swapping */
 219         u16 features;
 220 
 221         /* Create set */
 222         int (*create)(struct net *net, struct ip_set *set,
 223                       struct nlattr *tb[], u32 flags);
 224 
 225         /* Attribute policies */
 226         const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
 227         const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
 228 
 229         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
 230         struct module *me;
 231 };
 232 
 233 /* register and unregister set type */
 234 extern int ip_set_type_register(struct ip_set_type *set_type);
 235 extern void ip_set_type_unregister(struct ip_set_type *set_type);
 236 
 237 /* A generic IP set */
 238 struct ip_set {
 239         /* The name of the set */
 240         char name[IPSET_MAXNAMELEN];
 241         /* Lock protecting the set data */
 242         spinlock_t lock;
 243         /* References to the set */
 244         u32 ref;
 245         /* References to the set for netlink events like dump,
 246          * ref can be swapped out by ip_set_swap
 247          */
 248         u32 ref_netlink;
 249         /* The core set type */
 250         struct ip_set_type *type;
 251         /* The type variant doing the real job */
 252         const struct ip_set_type_variant *variant;
 253         /* The actual INET family of the set */
 254         u8 family;
 255         /* The type revision */
 256         u8 revision;
 257         /* Extensions */
 258         u8 extensions;
 259         /* Create flags */
 260         u8 flags;
 261         /* Default timeout value, if enabled */
 262         u32 timeout;
 263         /* Number of elements (vs timeout) */
 264         u32 elements;
 265         /* Size of the dynamic extensions (vs timeout) */
 266         size_t ext_size;
 267         /* Element data size */
 268         size_t dsize;
 269         /* Offsets to extensions in elements */
 270         size_t offset[IPSET_EXT_ID_MAX];
 271         /* The type specific data */
 272         void *data;
 273 };
 274 
 275 static inline void
 276 ip_set_ext_destroy(struct ip_set *set, void *data)
 277 {
 278         /* Check that the extension is enabled for the set and
 279          * call it's destroy function for its extension part in data.
 280          */
 281         if (SET_WITH_COMMENT(set))
 282                 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
 283                         set, ext_comment(data, set));
 284 }
 285 
 286 static inline int
 287 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
 288 {
 289         u32 cadt_flags = 0;
 290 
 291         if (SET_WITH_TIMEOUT(set))
 292                 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
 293                                            htonl(set->timeout))))
 294                         return -EMSGSIZE;
 295         if (SET_WITH_COUNTER(set))
 296                 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
 297         if (SET_WITH_COMMENT(set))
 298                 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
 299         if (SET_WITH_SKBINFO(set))
 300                 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
 301         if (SET_WITH_FORCEADD(set))
 302                 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
 303 
 304         if (!cadt_flags)
 305                 return 0;
 306         return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
 307 }
 308 
 309 /* Netlink CB args */
 310 enum {
 311         IPSET_CB_NET = 0,       /* net namespace */
 312         IPSET_CB_PROTO,         /* ipset protocol */
 313         IPSET_CB_DUMP,          /* dump single set/all sets */
 314         IPSET_CB_INDEX,         /* set index */
 315         IPSET_CB_PRIVATE,       /* set private data */
 316         IPSET_CB_ARG0,          /* type specific */
 317 };
 318 
 319 /* register and unregister set references */
 320 extern ip_set_id_t ip_set_get_byname(struct net *net,
 321                                      const char *name, struct ip_set **set);
 322 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
 323 extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
 324 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
 325 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
 326 
 327 /* API for iptables set match, and SET target */
 328 
 329 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
 330                       const struct xt_action_param *par,
 331                       struct ip_set_adt_opt *opt);
 332 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
 333                       const struct xt_action_param *par,
 334                       struct ip_set_adt_opt *opt);
 335 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
 336                        const struct xt_action_param *par,
 337                        struct ip_set_adt_opt *opt);
 338 
 339 /* Utility functions */
 340 extern void *ip_set_alloc(size_t size);
 341 extern void ip_set_free(void *members);
 342 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
 343 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
 344 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
 345                               size_t len, size_t align);
 346 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
 347                                  struct ip_set_ext *ext);
 348 extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
 349                                  const void *e, bool active);
 350 extern bool ip_set_match_extensions(struct ip_set *set,
 351                                     const struct ip_set_ext *ext,
 352                                     struct ip_set_ext *mext,
 353                                     u32 flags, void *data);
 354 
 355 static inline int
 356 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
 357 {
 358         __be32 ip;
 359         int ret = ip_set_get_ipaddr4(nla, &ip);
 360 
 361         if (ret)
 362                 return ret;
 363         *ipaddr = ntohl(ip);
 364         return 0;
 365 }
 366 
 367 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
 368 static inline bool
 369 ip_set_eexist(int ret, u32 flags)
 370 {
 371         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
 372 }
 373 
 374 /* Match elements marked with nomatch */
 375 static inline bool
 376 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
 377 {
 378         return adt == IPSET_TEST &&
 379                (set->type->features & IPSET_TYPE_NOMATCH) &&
 380                ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
 381                (ret > 0 || ret == -ENOTEMPTY);
 382 }
 383 
 384 /* Check the NLA_F_NET_BYTEORDER flag */
 385 static inline bool
 386 ip_set_attr_netorder(struct nlattr *tb[], int type)
 387 {
 388         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
 389 }
 390 
 391 static inline bool
 392 ip_set_optattr_netorder(struct nlattr *tb[], int type)
 393 {
 394         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
 395 }
 396 
 397 /* Useful converters */
 398 static inline u32
 399 ip_set_get_h32(const struct nlattr *attr)
 400 {
 401         return ntohl(nla_get_be32(attr));
 402 }
 403 
 404 static inline u16
 405 ip_set_get_h16(const struct nlattr *attr)
 406 {
 407         return ntohs(nla_get_be16(attr));
 408 }
 409 
 410 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
 411 {
 412         struct nlattr *__nested = nla_nest_start(skb, type);
 413         int ret;
 414 
 415         if (!__nested)
 416                 return -EMSGSIZE;
 417         ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
 418         if (!ret)
 419                 nla_nest_end(skb, __nested);
 420         return ret;
 421 }
 422 
 423 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
 424                                   const struct in6_addr *ipaddrptr)
 425 {
 426         struct nlattr *__nested = nla_nest_start(skb, type);
 427         int ret;
 428 
 429         if (!__nested)
 430                 return -EMSGSIZE;
 431         ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
 432         if (!ret)
 433                 nla_nest_end(skb, __nested);
 434         return ret;
 435 }
 436 
 437 /* Get address from skbuff */
 438 static inline __be32
 439 ip4addr(const struct sk_buff *skb, bool src)
 440 {
 441         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
 442 }
 443 
 444 static inline void
 445 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
 446 {
 447         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
 448 }
 449 
 450 static inline void
 451 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
 452 {
 453         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
 454                sizeof(*addr));
 455 }
 456 
 457 /* How often should the gc be run by default */
 458 #define IPSET_GC_TIME                   (3 * 60)
 459 
 460 /* Timeout period depending on the timeout value of the given set */
 461 #define IPSET_GC_PERIOD(timeout) \
 462         ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
 463 
 464 /* Entry is set with no timeout value */
 465 #define IPSET_ELEM_PERMANENT    0
 466 
 467 /* Set is defined with timeout support: timeout value may be 0 */
 468 #define IPSET_NO_TIMEOUT        UINT_MAX
 469 
 470 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
 471 #define IPSET_MAX_TIMEOUT       (UINT_MAX >> 1)/MSEC_PER_SEC
 472 
 473 #define ip_set_adt_opt_timeout(opt, set)        \
 474 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
 475 
 476 static inline unsigned int
 477 ip_set_timeout_uget(struct nlattr *tb)
 478 {
 479         unsigned int timeout = ip_set_get_h32(tb);
 480 
 481         /* Normalize to fit into jiffies */
 482         if (timeout > IPSET_MAX_TIMEOUT)
 483                 timeout = IPSET_MAX_TIMEOUT;
 484 
 485         return timeout;
 486 }
 487 
 488 static inline bool
 489 ip_set_timeout_expired(const unsigned long *t)
 490 {
 491         return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
 492 }
 493 
 494 static inline void
 495 ip_set_timeout_set(unsigned long *timeout, u32 value)
 496 {
 497         unsigned long t;
 498 
 499         if (!value) {
 500                 *timeout = IPSET_ELEM_PERMANENT;
 501                 return;
 502         }
 503 
 504         t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
 505         if (t == IPSET_ELEM_PERMANENT)
 506                 /* Bingo! :-) */
 507                 t--;
 508         *timeout = t;
 509 }
 510 
 511 static inline u32
 512 ip_set_timeout_get(const unsigned long *timeout)
 513 {
 514         u32 t;
 515 
 516         if (*timeout == IPSET_ELEM_PERMANENT)
 517                 return 0;
 518 
 519         t = jiffies_to_msecs(*timeout - jiffies)/MSEC_PER_SEC;
 520         /* Zero value in userspace means no timeout */
 521         return t == 0 ? 1 : t;
 522 }
 523 
 524 static inline char*
 525 ip_set_comment_uget(struct nlattr *tb)
 526 {
 527         return nla_data(tb);
 528 }
 529 
 530 /* Called from uadd only, protected by the set spinlock.
 531  * The kadt functions don't use the comment extensions in any way.
 532  */
 533 static inline void
 534 ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
 535                     const struct ip_set_ext *ext)
 536 {
 537         struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
 538         size_t len = ext->comment ? strlen(ext->comment) : 0;
 539 
 540         if (unlikely(c)) {
 541                 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
 542                 kfree_rcu(c, rcu);
 543                 rcu_assign_pointer(comment->c, NULL);
 544         }
 545         if (!len)
 546                 return;
 547         if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
 548                 len = IPSET_MAX_COMMENT_SIZE;
 549         c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
 550         if (unlikely(!c))
 551                 return;
 552         strlcpy(c->str, ext->comment, len + 1);
 553         set->ext_size += sizeof(*c) + strlen(c->str) + 1;
 554         rcu_assign_pointer(comment->c, c);
 555 }
 556 
 557 /* Used only when dumping a set, protected by rcu_read_lock() */
 558 static inline int
 559 ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
 560 {
 561         struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
 562 
 563         if (!c)
 564                 return 0;
 565         return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
 566 }
 567 
 568 /* Called from uadd/udel, flush or the garbage collectors protected
 569  * by the set spinlock.
 570  * Called when the set is destroyed and when there can't be any user
 571  * of the set data anymore.
 572  */
 573 static inline void
 574 ip_set_comment_free(struct ip_set *set, struct ip_set_comment *comment)
 575 {
 576         struct ip_set_comment_rcu *c;
 577 
 578         c = rcu_dereference_protected(comment->c, 1);
 579         if (unlikely(!c))
 580                 return;
 581         set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
 582         kfree_rcu(c, rcu);
 583         rcu_assign_pointer(comment->c, NULL);
 584 }
 585 
 586 static inline void
 587 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
 588 {
 589         atomic64_add((long long)bytes, &(counter)->bytes);
 590 }
 591 
 592 static inline void
 593 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
 594 {
 595         atomic64_add((long long)packets, &(counter)->packets);
 596 }
 597 
 598 static inline u64
 599 ip_set_get_bytes(const struct ip_set_counter *counter)
 600 {
 601         return (u64)atomic64_read(&(counter)->bytes);
 602 }
 603 
 604 static inline u64
 605 ip_set_get_packets(const struct ip_set_counter *counter)
 606 {
 607         return (u64)atomic64_read(&(counter)->packets);
 608 }
 609 
 610 static inline bool
 611 ip_set_match_counter(u64 counter, u64 match, u8 op)
 612 {
 613         switch (op) {
 614         case IPSET_COUNTER_NONE:
 615                 return true;
 616         case IPSET_COUNTER_EQ:
 617                 return counter == match;
 618         case IPSET_COUNTER_NE:
 619                 return counter != match;
 620         case IPSET_COUNTER_LT:
 621                 return counter < match;
 622         case IPSET_COUNTER_GT:
 623                 return counter > match;
 624         }
 625         return false;
 626 }
 627 
 628 static inline void
 629 ip_set_update_counter(struct ip_set_counter *counter,
 630                       const struct ip_set_ext *ext, u32 flags)
 631 {
 632         if (ext->packets != ULLONG_MAX &&
 633             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
 634                 ip_set_add_bytes(ext->bytes, counter);
 635                 ip_set_add_packets(ext->packets, counter);
 636         }
 637 }
 638 
 639 static inline bool
 640 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
 641 {
 642         return nla_put_net64(skb, IPSET_ATTR_BYTES,
 643                              cpu_to_be64(ip_set_get_bytes(counter)),
 644                              IPSET_ATTR_PAD) ||
 645                nla_put_net64(skb, IPSET_ATTR_PACKETS,
 646                              cpu_to_be64(ip_set_get_packets(counter)),
 647                              IPSET_ATTR_PAD);
 648 }
 649 
 650 static inline void
 651 ip_set_init_counter(struct ip_set_counter *counter,
 652                     const struct ip_set_ext *ext)
 653 {
 654         if (ext->bytes != ULLONG_MAX)
 655                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
 656         if (ext->packets != ULLONG_MAX)
 657                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
 658 }
 659 
 660 static inline void
 661 ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
 662                    const struct ip_set_ext *ext,
 663                    struct ip_set_ext *mext, u32 flags)
 664 {
 665         mext->skbinfo = *skbinfo;
 666 }
 667 
 668 static inline bool
 669 ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
 670 {
 671         /* Send nonzero parameters only */
 672         return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
 673                 nla_put_net64(skb, IPSET_ATTR_SKBMARK,
 674                               cpu_to_be64((u64)skbinfo->skbmark << 32 |
 675                                           skbinfo->skbmarkmask),
 676                               IPSET_ATTR_PAD)) ||
 677                (skbinfo->skbprio &&
 678                 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
 679                               cpu_to_be32(skbinfo->skbprio))) ||
 680                (skbinfo->skbqueue &&
 681                 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
 682                               cpu_to_be16(skbinfo->skbqueue)));
 683 }
 684 
 685 static inline void
 686 ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
 687                     const struct ip_set_ext *ext)
 688 {
 689         *skbinfo = ext->skbinfo;
 690 }
 691 
 692 #define IP_SET_INIT_KEXT(skb, opt, set)                 \
 693         { .bytes = (skb)->len, .packets = 1, .target = true,\
 694           .timeout = ip_set_adt_opt_timeout(opt, set) }
 695 
 696 #define IP_SET_INIT_UEXT(set)                           \
 697         { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
 698           .timeout = (set)->timeout }
 699 
 700 #define IPSET_CONCAT(a, b)              a##b
 701 #define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
 702 
 703 #endif /*_IP_SET_H */

/* [<][>][^][v][top][bottom][index][help] */