linux/net/netfilter/nft_set_pipapo.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2
   3/* PIPAPO: PIle PAcket POlicies: set for arbitrary concatenations of ranges
   4 *
   5 * Copyright (c) 2019-2020 Red Hat GmbH
   6 *
   7 * Author: Stefano Brivio <sbrivio@redhat.com>
   8 */
   9
  10/**
  11 * DOC: Theory of Operation
  12 *
  13 *
  14 * Problem
  15 * -------
  16 *
  17 * Match packet bytes against entries composed of ranged or non-ranged packet
  18 * field specifiers, mapping them to arbitrary references. For example:
  19 *
  20 * ::
  21 *
  22 *               --- fields --->
  23 *      |    [net],[port],[net]... => [reference]
  24 *   entries [net],[port],[net]... => [reference]
  25 *      |    [net],[port],[net]... => [reference]
  26 *      V    ...
  27 *
  28 * where [net] fields can be IP ranges or netmasks, and [port] fields are port
  29 * ranges. Arbitrary packet fields can be matched.
  30 *
  31 *
  32 * Algorithm Overview
  33 * ------------------
  34 *
  35 * This algorithm is loosely inspired by [Ligatti 2010], and fundamentally
  36 * relies on the consideration that every contiguous range in a space of b bits
  37 * can be converted into b * 2 netmasks, from Theorem 3 in [Rottenstreich 2010],
  38 * as also illustrated in Section 9 of [Kogan 2014].
  39 *
  40 * Classification against a number of entries, that require matching given bits
  41 * of a packet field, is performed by grouping those bits in sets of arbitrary
  42 * size, and classifying packet bits one group at a time.
  43 *
  44 * Example:
  45 *   to match the source port (16 bits) of a packet, we can divide those 16 bits
  46 *   in 4 groups of 4 bits each. Given the entry:
  47 *      0000 0001 0101 1001
  48 *   and a packet with source port:
  49 *      0000 0001 1010 1001
  50 *   first and second groups match, but the third doesn't. We conclude that the
  51 *   packet doesn't match the given entry.
  52 *
  53 * Translate the set to a sequence of lookup tables, one per field. Each table
  54 * has two dimensions: bit groups to be matched for a single packet field, and
  55 * all the possible values of said groups (buckets). Input entries are
  56 * represented as one or more rules, depending on the number of composing
  57 * netmasks for the given field specifier, and a group match is indicated as a
  58 * set bit, with number corresponding to the rule index, in all the buckets
  59 * whose value matches the entry for a given group.
  60 *
  61 * Rules are mapped between fields through an array of x, n pairs, with each
  62 * item mapping a matched rule to one or more rules. The position of the pair in
  63 * the array indicates the matched rule to be mapped to the next field, x
  64 * indicates the first rule index in the next field, and n the amount of
  65 * next-field rules the current rule maps to.
  66 *
  67 * The mapping array for the last field maps to the desired references.
  68 *
  69 * To match, we perform table lookups using the values of grouped packet bits,
  70 * and use a sequence of bitwise operations to progressively evaluate rule
  71 * matching.
  72 *
  73 * A stand-alone, reference implementation, also including notes about possible
  74 * future optimisations, is available at:
  75 *    https://pipapo.lameexcu.se/
  76 *
  77 * Insertion
  78 * ---------
  79 *
  80 * - For each packet field:
  81 *
  82 *   - divide the b packet bits we want to classify into groups of size t,
  83 *     obtaining ceil(b / t) groups
  84 *
  85 *      Example: match on destination IP address, with t = 4: 32 bits, 8 groups
  86 *      of 4 bits each
  87 *
  88 *   - allocate a lookup table with one column ("bucket") for each possible
  89 *     value of a group, and with one row for each group
  90 *
  91 *      Example: 8 groups, 2^4 buckets:
  92 *
  93 * ::
  94 *
  95 *                     bucket
  96 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
  97 *        0
  98 *        1
  99 *        2
 100 *        3
 101 *        4
 102 *        5
 103 *        6
 104 *        7
 105 *
 106 *   - map the bits we want to classify for the current field, for a given
 107 *     entry, to a single rule for non-ranged and netmask set items, and to one
 108 *     or multiple rules for ranges. Ranges are expanded to composing netmasks
 109 *     by pipapo_expand().
 110 *
 111 *      Example: 2 entries, 10.0.0.5:1024 and 192.168.1.0-192.168.2.1:2048
 112 *      - rule #0: 10.0.0.5
 113 *      - rule #1: 192.168.1.0/24
 114 *      - rule #2: 192.168.2.0/31
 115 *
 116 *   - insert references to the rules in the lookup table, selecting buckets
 117 *     according to bit values of a rule in the given group. This is done by
 118 *     pipapo_insert().
 119 *
 120 *      Example: given:
 121 *      - rule #0: 10.0.0.5 mapping to buckets
 122 *        < 0 10  0 0   0 0  0 5 >
 123 *      - rule #1: 192.168.1.0/24 mapping to buckets
 124 *        < 12 0  10 8  0 1  < 0..15 > < 0..15 > >
 125 *      - rule #2: 192.168.2.0/31 mapping to buckets
 126 *        < 12 0  10 8  0 2  0 < 0..1 > >
 127 *
 128 *      these bits are set in the lookup table:
 129 *
 130 * ::
 131 *
 132 *                     bucket
 133 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
 134 *        0    0                                              1,2
 135 *        1   1,2                                      0
 136 *        2    0                                      1,2
 137 *        3    0                              1,2
 138 *        4  0,1,2
 139 *        5    0   1   2
 140 *        6  0,1,2 1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 141 *        7   1,2 1,2  1   1   1  0,1  1   1   1   1   1   1   1   1   1   1
 142 *
 143 *   - if this is not the last field in the set, fill a mapping array that maps
 144 *     rules from the lookup table to rules belonging to the same entry in
 145 *     the next lookup table, done by pipapo_map().
 146 *
 147 *     Note that as rules map to contiguous ranges of rules, given how netmask
 148 *     expansion and insertion is performed, &union nft_pipapo_map_bucket stores
 149 *     this information as pairs of first rule index, rule count.
 150 *
 151 *      Example: 2 entries, 10.0.0.5:1024 and 192.168.1.0-192.168.2.1:2048,
 152 *      given lookup table #0 for field 0 (see example above):
 153 *
 154 * ::
 155 *
 156 *                     bucket
 157 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
 158 *        0    0                                              1,2
 159 *        1   1,2                                      0
 160 *        2    0                                      1,2
 161 *        3    0                              1,2
 162 *        4  0,1,2
 163 *        5    0   1   2
 164 *        6  0,1,2 1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 165 *        7   1,2 1,2  1   1   1  0,1  1   1   1   1   1   1   1   1   1   1
 166 *
 167 *      and lookup table #1 for field 1 with:
 168 *      - rule #0: 1024 mapping to buckets
 169 *        < 0  0  4  0 >
 170 *      - rule #1: 2048 mapping to buckets
 171 *        < 0  0  5  0 >
 172 *
 173 * ::
 174 *
 175 *                     bucket
 176 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
 177 *        0   0,1
 178 *        1   0,1
 179 *        2                    0   1
 180 *        3   0,1
 181 *
 182 *      we need to map rules for 10.0.0.5 in lookup table #0 (rule #0) to 1024
 183 *      in lookup table #1 (rule #0) and rules for 192.168.1.0-192.168.2.1
 184 *      (rules #1, #2) to 2048 in lookup table #2 (rule #1):
 185 *
 186 * ::
 187 *
 188 *       rule indices in current field: 0    1    2
 189 *       map to rules in next field:    0    1    1
 190 *
 191 *   - if this is the last field in the set, fill a mapping array that maps
 192 *     rules from the last lookup table to element pointers, also done by
 193 *     pipapo_map().
 194 *
 195 *     Note that, in this implementation, we have two elements (start, end) for
 196 *     each entry. The pointer to the end element is stored in this array, and
 197 *     the pointer to the start element is linked from it.
 198 *
 199 *      Example: entry 10.0.0.5:1024 has a corresponding &struct nft_pipapo_elem
 200 *      pointer, 0x66, and element for 192.168.1.0-192.168.2.1:2048 is at 0x42.
 201 *      From the rules of lookup table #1 as mapped above:
 202 *
 203 * ::
 204 *
 205 *       rule indices in last field:    0    1
 206 *       map to elements:             0x42  0x66
 207 *
 208 *
 209 * Matching
 210 * --------
 211 *
 212 * We use a result bitmap, with the size of a single lookup table bucket, to
 213 * represent the matching state that applies at every algorithm step. This is
 214 * done by pipapo_lookup().
 215 *
 216 * - For each packet field:
 217 *
 218 *   - start with an all-ones result bitmap (res_map in pipapo_lookup())
 219 *
 220 *   - perform a lookup into the table corresponding to the current field,
 221 *     for each group, and at every group, AND the current result bitmap with
 222 *     the value from the lookup table bucket
 223 *
 224 * ::
 225 *
 226 *      Example: 192.168.1.5 < 12 0  10 8  0 1  0 5 >, with lookup table from
 227 *      insertion examples.
 228 *      Lookup table buckets are at least 3 bits wide, we'll assume 8 bits for
 229 *      convenience in this example. Initial result bitmap is 0xff, the steps
 230 *      below show the value of the result bitmap after each group is processed:
 231 *
 232 *                     bucket
 233 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
 234 *        0    0                                              1,2
 235 *        result bitmap is now: 0xff & 0x6 [bucket 12] = 0x6
 236 *
 237 *        1   1,2                                      0
 238 *        result bitmap is now: 0x6 & 0x6 [bucket 0] = 0x6
 239 *
 240 *        2    0                                      1,2
 241 *        result bitmap is now: 0x6 & 0x6 [bucket 10] = 0x6
 242 *
 243 *        3    0                              1,2
 244 *        result bitmap is now: 0x6 & 0x6 [bucket 8] = 0x6
 245 *
 246 *        4  0,1,2
 247 *        result bitmap is now: 0x6 & 0x7 [bucket 0] = 0x6
 248 *
 249 *        5    0   1   2
 250 *        result bitmap is now: 0x6 & 0x2 [bucket 1] = 0x2
 251 *
 252 *        6  0,1,2 1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 253 *        result bitmap is now: 0x2 & 0x7 [bucket 0] = 0x2
 254 *
 255 *        7   1,2 1,2  1   1   1  0,1  1   1   1   1   1   1   1   1   1   1
 256 *        final result bitmap for this field is: 0x2 & 0x3 [bucket 5] = 0x2
 257 *
 258 *   - at the next field, start with a new, all-zeroes result bitmap. For each
 259 *     bit set in the previous result bitmap, fill the new result bitmap
 260 *     (fill_map in pipapo_lookup()) with the rule indices from the
 261 *     corresponding buckets of the mapping field for this field, done by
 262 *     pipapo_refill()
 263 *
 264 *      Example: with mapping table from insertion examples, with the current
 265 *      result bitmap from the previous example, 0x02:
 266 *
 267 * ::
 268 *
 269 *       rule indices in current field: 0    1    2
 270 *       map to rules in next field:    0    1    1
 271 *
 272 *      the new result bitmap will be 0x02: rule 1 was set, and rule 1 will be
 273 *      set.
 274 *
 275 *      We can now extend this example to cover the second iteration of the step
 276 *      above (lookup and AND bitmap): assuming the port field is
 277 *      2048 < 0  0  5  0 >, with starting result bitmap 0x2, and lookup table
 278 *      for "port" field from pre-computation example:
 279 *
 280 * ::
 281 *
 282 *                     bucket
 283 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
 284 *        0   0,1
 285 *        1   0,1
 286 *        2                    0   1
 287 *        3   0,1
 288 *
 289 *       operations are: 0x2 & 0x3 [bucket 0] & 0x3 [bucket 0] & 0x2 [bucket 5]
 290 *       & 0x3 [bucket 0], resulting bitmap is 0x2.
 291 *
 292 *   - if this is the last field in the set, look up the value from the mapping
 293 *     array corresponding to the final result bitmap
 294 *
 295 *      Example: 0x2 resulting bitmap from 192.168.1.5:2048, mapping array for
 296 *      last field from insertion example:
 297 *
 298 * ::
 299 *
 300 *       rule indices in last field:    0    1
 301 *       map to elements:             0x42  0x66
 302 *
 303 *      the matching element is at 0x42.
 304 *
 305 *
 306 * References
 307 * ----------
 308 *
 309 * [Ligatti 2010]
 310 *      A Packet-classification Algorithm for Arbitrary Bitmask Rules, with
 311 *      Automatic Time-space Tradeoffs
 312 *      Jay Ligatti, Josh Kuhn, and Chris Gage.
 313 *      Proceedings of the IEEE International Conference on Computer
 314 *      Communication Networks (ICCCN), August 2010.
 315 *      http://www.cse.usf.edu/~ligatti/papers/grouper-conf.pdf
 316 *
 317 * [Rottenstreich 2010]
 318 *      Worst-Case TCAM Rule Expansion
 319 *      Ori Rottenstreich and Isaac Keslassy.
 320 *      2010 Proceedings IEEE INFOCOM, San Diego, CA, 2010.
 321 *      http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.212.4592&rep=rep1&type=pdf
 322 *
 323 * [Kogan 2014]
 324 *      SAX-PAC (Scalable And eXpressive PAcket Classification)
 325 *      Kirill Kogan, Sergey Nikolenko, Ori Rottenstreich, William Culhane,
 326 *      and Patrick Eugster.
 327 *      Proceedings of the 2014 ACM conference on SIGCOMM, August 2014.
 328 *      http://www.sigcomm.org/sites/default/files/ccr/papers/2014/August/2619239-2626294.pdf
 329 */
 330
 331#include <linux/kernel.h>
 332#include <linux/init.h>
 333#include <linux/log2.h>
 334#include <linux/module.h>
 335#include <linux/netlink.h>
 336#include <linux/netfilter.h>
 337#include <linux/netfilter/nf_tables.h>
 338#include <net/netfilter/nf_tables_core.h>
 339#include <uapi/linux/netfilter/nf_tables.h>
 340#include <net/ipv6.h>                   /* For the maximum length of a field */
 341#include <linux/bitmap.h>
 342#include <linux/bitops.h>
 343
 344/* Count of concatenated fields depends on count of 32-bit nftables registers */
 345#define NFT_PIPAPO_MAX_FIELDS           NFT_REG32_COUNT
 346
 347/* Largest supported field size */
 348#define NFT_PIPAPO_MAX_BYTES            (sizeof(struct in6_addr))
 349#define NFT_PIPAPO_MAX_BITS             (NFT_PIPAPO_MAX_BYTES * BITS_PER_BYTE)
 350
 351/* Number of bits to be grouped together in lookup table buckets, arbitrary */
 352#define NFT_PIPAPO_GROUP_BITS           4
 353#define NFT_PIPAPO_GROUPS_PER_BYTE      (BITS_PER_BYTE / NFT_PIPAPO_GROUP_BITS)
 354
 355/* Fields are padded to 32 bits in input registers */
 356#define NFT_PIPAPO_GROUPS_PADDED_SIZE(x)                                \
 357        (round_up((x) / NFT_PIPAPO_GROUPS_PER_BYTE, sizeof(u32)))
 358#define NFT_PIPAPO_GROUPS_PADDING(x)                                    \
 359        (NFT_PIPAPO_GROUPS_PADDED_SIZE((x)) - (x) / NFT_PIPAPO_GROUPS_PER_BYTE)
 360
 361/* Number of buckets, given by 2 ^ n, with n grouped bits */
 362#define NFT_PIPAPO_BUCKETS              (1 << NFT_PIPAPO_GROUP_BITS)
 363
 364/* Each n-bit range maps to up to n * 2 rules */
 365#define NFT_PIPAPO_MAP_NBITS            (const_ilog2(NFT_PIPAPO_MAX_BITS * 2))
 366
 367/* Use the rest of mapping table buckets for rule indices, but it makes no sense
 368 * to exceed 32 bits
 369 */
 370#if BITS_PER_LONG == 64
 371#define NFT_PIPAPO_MAP_TOBITS           32
 372#else
 373#define NFT_PIPAPO_MAP_TOBITS           (BITS_PER_LONG - NFT_PIPAPO_MAP_NBITS)
 374#endif
 375
 376/* ...which gives us the highest allowed index for a rule */
 377#define NFT_PIPAPO_RULE0_MAX            ((1UL << (NFT_PIPAPO_MAP_TOBITS - 1)) \
 378                                        - (1UL << NFT_PIPAPO_MAP_NBITS))
 379
 380#define nft_pipapo_for_each_field(field, index, match)          \
 381        for ((field) = (match)->f, (index) = 0;                 \
 382             (index) < (match)->field_count;                    \
 383             (index)++, (field)++)
 384
 385/**
 386 * union nft_pipapo_map_bucket - Bucket of mapping table
 387 * @to:         First rule number (in next field) this rule maps to
 388 * @n:          Number of rules (in next field) this rule maps to
 389 * @e:          If there's no next field, pointer to element this rule maps to
 390 */
 391union nft_pipapo_map_bucket {
 392        struct {
 393#if BITS_PER_LONG == 64
 394                static_assert(NFT_PIPAPO_MAP_TOBITS <= 32);
 395                u32 to;
 396
 397                static_assert(NFT_PIPAPO_MAP_NBITS <= 32);
 398                u32 n;
 399#else
 400                unsigned long to:NFT_PIPAPO_MAP_TOBITS;
 401                unsigned long  n:NFT_PIPAPO_MAP_NBITS;
 402#endif
 403        };
 404        struct nft_pipapo_elem *e;
 405};
 406
 407/**
 408 * struct nft_pipapo_field - Lookup, mapping tables and related data for a field
 409 * @groups:     Amount of 4-bit groups
 410 * @rules:      Number of inserted rules
 411 * @bsize:      Size of each bucket in lookup table, in longs
 412 * @lt:         Lookup table: 'groups' rows of NFT_PIPAPO_BUCKETS buckets
 413 * @mt:         Mapping table: one bucket per rule
 414 */
 415struct nft_pipapo_field {
 416        int groups;
 417        unsigned long rules;
 418        size_t bsize;
 419        unsigned long *lt;
 420        union nft_pipapo_map_bucket *mt;
 421};
 422
 423/**
 424 * struct nft_pipapo_match - Data used for lookup and matching
 425 * @field_count         Amount of fields in set
 426 * @scratch:            Preallocated per-CPU maps for partial matching results
 427 * @bsize_max:          Maximum lookup table bucket size of all fields, in longs
 428 * @rcu                 Matching data is swapped on commits
 429 * @f:                  Fields, with lookup and mapping tables
 430 */
 431struct nft_pipapo_match {
 432        int field_count;
 433        unsigned long * __percpu *scratch;
 434        size_t bsize_max;
 435        struct rcu_head rcu;
 436        struct nft_pipapo_field f[0];
 437};
 438
 439/* Current working bitmap index, toggled between field matches */
 440static DEFINE_PER_CPU(bool, nft_pipapo_scratch_index);
 441
 442/**
 443 * struct nft_pipapo - Representation of a set
 444 * @match:      Currently in-use matching data
 445 * @clone:      Copy where pending insertions and deletions are kept
 446 * @groups:     Total amount of 4-bit groups for fields in this set
 447 * @width:      Total bytes to be matched for one packet, including padding
 448 * @dirty:      Working copy has pending insertions or deletions
 449 * @last_gc:    Timestamp of last garbage collection run, jiffies
 450 */
 451struct nft_pipapo {
 452        struct nft_pipapo_match __rcu *match;
 453        struct nft_pipapo_match *clone;
 454        int groups;
 455        int width;
 456        bool dirty;
 457        unsigned long last_gc;
 458};
 459
 460struct nft_pipapo_elem;
 461
 462/**
 463 * struct nft_pipapo_elem - API-facing representation of single set element
 464 * @ext:        nftables API extensions
 465 */
 466struct nft_pipapo_elem {
 467        struct nft_set_ext ext;
 468};
 469
 470/**
 471 * pipapo_refill() - For each set bit, set bits from selected mapping table item
 472 * @map:        Bitmap to be scanned for set bits
 473 * @len:        Length of bitmap in longs
 474 * @rules:      Number of rules in field
 475 * @dst:        Destination bitmap
 476 * @mt:         Mapping table containing bit set specifiers
 477 * @match_only: Find a single bit and return, don't fill
 478 *
 479 * Iteration over set bits with __builtin_ctzl(): Daniel Lemire, public domain.
 480 *
 481 * For each bit set in map, select the bucket from mapping table with index
 482 * corresponding to the position of the bit set. Use start bit and amount of
 483 * bits specified in bucket to fill region in dst.
 484 *
 485 * Return: -1 on no match, bit position on 'match_only', 0 otherwise.
 486 */
 487static int pipapo_refill(unsigned long *map, int len, int rules,
 488                         unsigned long *dst, union nft_pipapo_map_bucket *mt,
 489                         bool match_only)
 490{
 491        unsigned long bitset;
 492        int k, ret = -1;
 493
 494        for (k = 0; k < len; k++) {
 495                bitset = map[k];
 496                while (bitset) {
 497                        unsigned long t = bitset & -bitset;
 498                        int r = __builtin_ctzl(bitset);
 499                        int i = k * BITS_PER_LONG + r;
 500
 501                        if (unlikely(i >= rules)) {
 502                                map[k] = 0;
 503                                return -1;
 504                        }
 505
 506                        if (unlikely(match_only)) {
 507                                bitmap_clear(map, i, 1);
 508                                return i;
 509                        }
 510
 511                        ret = 0;
 512
 513                        bitmap_set(dst, mt[i].to, mt[i].n);
 514
 515                        bitset ^= t;
 516                }
 517                map[k] = 0;
 518        }
 519
 520        return ret;
 521}
 522
 523/**
 524 * nft_pipapo_lookup() - Lookup function
 525 * @net:        Network namespace
 526 * @set:        nftables API set representation
 527 * @elem:       nftables API element representation containing key data
 528 * @ext:        nftables API extension pointer, filled with matching reference
 529 *
 530 * For more details, see DOC: Theory of Operation.
 531 *
 532 * Return: true on match, false otherwise.
 533 */
 534static bool nft_pipapo_lookup(const struct net *net, const struct nft_set *set,
 535                              const u32 *key, const struct nft_set_ext **ext)
 536{
 537        struct nft_pipapo *priv = nft_set_priv(set);
 538        unsigned long *res_map, *fill_map;
 539        u8 genmask = nft_genmask_cur(net);
 540        const u8 *rp = (const u8 *)key;
 541        struct nft_pipapo_match *m;
 542        struct nft_pipapo_field *f;
 543        bool map_index;
 544        int i;
 545
 546        local_bh_disable();
 547
 548        map_index = raw_cpu_read(nft_pipapo_scratch_index);
 549
 550        m = rcu_dereference(priv->match);
 551
 552        if (unlikely(!m || !*raw_cpu_ptr(m->scratch)))
 553                goto out;
 554
 555        res_map  = *raw_cpu_ptr(m->scratch) + (map_index ? m->bsize_max : 0);
 556        fill_map = *raw_cpu_ptr(m->scratch) + (map_index ? 0 : m->bsize_max);
 557
 558        memset(res_map, 0xff, m->bsize_max * sizeof(*res_map));
 559
 560        nft_pipapo_for_each_field(f, i, m) {
 561                bool last = i == m->field_count - 1;
 562                unsigned long *lt = f->lt;
 563                int b, group;
 564
 565                /* For each 4-bit group: select lookup table bucket depending on
 566                 * packet bytes value, then AND bucket value
 567                 */
 568                for (group = 0; group < f->groups; group += 2) {
 569                        u8 v;
 570
 571                        v = *rp >> 4;
 572                        __bitmap_and(res_map, res_map, lt + v * f->bsize,
 573                                     f->bsize * BITS_PER_LONG);
 574                        lt += f->bsize * NFT_PIPAPO_BUCKETS;
 575
 576                        v = *rp & 0x0f;
 577                        rp++;
 578                        __bitmap_and(res_map, res_map, lt + v * f->bsize,
 579                                     f->bsize * BITS_PER_LONG);
 580                        lt += f->bsize * NFT_PIPAPO_BUCKETS;
 581                }
 582
 583                /* Now populate the bitmap for the next field, unless this is
 584                 * the last field, in which case return the matched 'ext'
 585                 * pointer if any.
 586                 *
 587                 * Now res_map contains the matching bitmap, and fill_map is the
 588                 * bitmap for the next field.
 589                 */
 590next_match:
 591                b = pipapo_refill(res_map, f->bsize, f->rules, fill_map, f->mt,
 592                                  last);
 593                if (b < 0) {
 594                        raw_cpu_write(nft_pipapo_scratch_index, map_index);
 595                        local_bh_enable();
 596
 597                        return false;
 598                }
 599
 600                if (last) {
 601                        *ext = &f->mt[b].e->ext;
 602                        if (unlikely(nft_set_elem_expired(*ext) ||
 603                                     !nft_set_elem_active(*ext, genmask)))
 604                                goto next_match;
 605
 606                        /* Last field: we're just returning the key without
 607                         * filling the initial bitmap for the next field, so the
 608                         * current inactive bitmap is clean and can be reused as
 609                         * *next* bitmap (not initial) for the next packet.
 610                         */
 611                        raw_cpu_write(nft_pipapo_scratch_index, map_index);
 612                        local_bh_enable();
 613
 614                        return true;
 615                }
 616
 617                /* Swap bitmap indices: res_map is the initial bitmap for the
 618                 * next field, and fill_map is guaranteed to be all-zeroes at
 619                 * this point.
 620                 */
 621                map_index = !map_index;
 622                swap(res_map, fill_map);
 623
 624                rp += NFT_PIPAPO_GROUPS_PADDING(f->groups);
 625        }
 626
 627out:
 628        local_bh_enable();
 629        return false;
 630}
 631
 632/**
 633 * pipapo_get() - Get matching element reference given key data
 634 * @net:        Network namespace
 635 * @set:        nftables API set representation
 636 * @data:       Key data to be matched against existing elements
 637 * @genmask:    If set, check that element is active in given genmask
 638 *
 639 * This is essentially the same as the lookup function, except that it matches
 640 * key data against the uncommitted copy and doesn't use preallocated maps for
 641 * bitmap results.
 642 *
 643 * Return: pointer to &struct nft_pipapo_elem on match, error pointer otherwise.
 644 */
 645static struct nft_pipapo_elem *pipapo_get(const struct net *net,
 646                                          const struct nft_set *set,
 647                                          const u8 *data, u8 genmask)
 648{
 649        struct nft_pipapo_elem *ret = ERR_PTR(-ENOENT);
 650        struct nft_pipapo *priv = nft_set_priv(set);
 651        struct nft_pipapo_match *m = priv->clone;
 652        unsigned long *res_map, *fill_map = NULL;
 653        struct nft_pipapo_field *f;
 654        int i;
 655
 656        res_map = kmalloc_array(m->bsize_max, sizeof(*res_map), GFP_ATOMIC);
 657        if (!res_map) {
 658                ret = ERR_PTR(-ENOMEM);
 659                goto out;
 660        }
 661
 662        fill_map = kcalloc(m->bsize_max, sizeof(*res_map), GFP_ATOMIC);
 663        if (!fill_map) {
 664                ret = ERR_PTR(-ENOMEM);
 665                goto out;
 666        }
 667
 668        memset(res_map, 0xff, m->bsize_max * sizeof(*res_map));
 669
 670        nft_pipapo_for_each_field(f, i, m) {
 671                bool last = i == m->field_count - 1;
 672                unsigned long *lt = f->lt;
 673                int b, group;
 674
 675                /* For each 4-bit group: select lookup table bucket depending on
 676                 * packet bytes value, then AND bucket value
 677                 */
 678                for (group = 0; group < f->groups; group++) {
 679                        u8 v;
 680
 681                        if (group % 2) {
 682                                v = *data & 0x0f;
 683                                data++;
 684                        } else {
 685                                v = *data >> 4;
 686                        }
 687                        __bitmap_and(res_map, res_map, lt + v * f->bsize,
 688                                     f->bsize * BITS_PER_LONG);
 689
 690                        lt += f->bsize * NFT_PIPAPO_BUCKETS;
 691                }
 692
 693                /* Now populate the bitmap for the next field, unless this is
 694                 * the last field, in which case return the matched 'ext'
 695                 * pointer if any.
 696                 *
 697                 * Now res_map contains the matching bitmap, and fill_map is the
 698                 * bitmap for the next field.
 699                 */
 700next_match:
 701                b = pipapo_refill(res_map, f->bsize, f->rules, fill_map, f->mt,
 702                                  last);
 703                if (b < 0)
 704                        goto out;
 705
 706                if (last) {
 707                        if (nft_set_elem_expired(&f->mt[b].e->ext) ||
 708                            (genmask &&
 709                             !nft_set_elem_active(&f->mt[b].e->ext, genmask)))
 710                                goto next_match;
 711
 712                        ret = f->mt[b].e;
 713                        goto out;
 714                }
 715
 716                data += NFT_PIPAPO_GROUPS_PADDING(f->groups);
 717
 718                /* Swap bitmap indices: fill_map will be the initial bitmap for
 719                 * the next field (i.e. the new res_map), and res_map is
 720                 * guaranteed to be all-zeroes at this point, ready to be filled
 721                 * according to the next mapping table.
 722                 */
 723                swap(res_map, fill_map);
 724        }
 725
 726out:
 727        kfree(fill_map);
 728        kfree(res_map);
 729        return ret;
 730}
 731
 732/**
 733 * nft_pipapo_get() - Get matching element reference given key data
 734 * @net:        Network namespace
 735 * @set:        nftables API set representation
 736 * @elem:       nftables API element representation containing key data
 737 * @flags:      Unused
 738 */
 739void *nft_pipapo_get(const struct net *net, const struct nft_set *set,
 740                     const struct nft_set_elem *elem, unsigned int flags)
 741{
 742        return pipapo_get(net, set, (const u8 *)elem->key.val.data,
 743                          nft_genmask_cur(net));
 744}
 745
 746/**
 747 * pipapo_resize() - Resize lookup or mapping table, or both
 748 * @f:          Field containing lookup and mapping tables
 749 * @old_rules:  Previous amount of rules in field
 750 * @rules:      New amount of rules
 751 *
 752 * Increase, decrease or maintain tables size depending on new amount of rules,
 753 * and copy data over. In case the new size is smaller, throw away data for
 754 * highest-numbered rules.
 755 *
 756 * Return: 0 on success, -ENOMEM on allocation failure.
 757 */
 758static int pipapo_resize(struct nft_pipapo_field *f, int old_rules, int rules)
 759{
 760        long *new_lt = NULL, *new_p, *old_lt = f->lt, *old_p;
 761        union nft_pipapo_map_bucket *new_mt, *old_mt = f->mt;
 762        size_t new_bucket_size, copy;
 763        int group, bucket;
 764
 765        new_bucket_size = DIV_ROUND_UP(rules, BITS_PER_LONG);
 766
 767        if (new_bucket_size == f->bsize)
 768                goto mt;
 769
 770        if (new_bucket_size > f->bsize)
 771                copy = f->bsize;
 772        else
 773                copy = new_bucket_size;
 774
 775        new_lt = kvzalloc(f->groups * NFT_PIPAPO_BUCKETS * new_bucket_size *
 776                          sizeof(*new_lt), GFP_KERNEL);
 777        if (!new_lt)
 778                return -ENOMEM;
 779
 780        new_p = new_lt;
 781        old_p = old_lt;
 782        for (group = 0; group < f->groups; group++) {
 783                for (bucket = 0; bucket < NFT_PIPAPO_BUCKETS; bucket++) {
 784                        memcpy(new_p, old_p, copy * sizeof(*new_p));
 785                        new_p += copy;
 786                        old_p += copy;
 787
 788                        if (new_bucket_size > f->bsize)
 789                                new_p += new_bucket_size - f->bsize;
 790                        else
 791                                old_p += f->bsize - new_bucket_size;
 792                }
 793        }
 794
 795mt:
 796        new_mt = kvmalloc(rules * sizeof(*new_mt), GFP_KERNEL);
 797        if (!new_mt) {
 798                kvfree(new_lt);
 799                return -ENOMEM;
 800        }
 801
 802        memcpy(new_mt, f->mt, min(old_rules, rules) * sizeof(*new_mt));
 803        if (rules > old_rules) {
 804                memset(new_mt + old_rules, 0,
 805                       (rules - old_rules) * sizeof(*new_mt));
 806        }
 807
 808        if (new_lt) {
 809                f->bsize = new_bucket_size;
 810                f->lt = new_lt;
 811                kvfree(old_lt);
 812        }
 813
 814        f->mt = new_mt;
 815        kvfree(old_mt);
 816
 817        return 0;
 818}
 819
 820/**
 821 * pipapo_bucket_set() - Set rule bit in bucket given group and group value
 822 * @f:          Field containing lookup table
 823 * @rule:       Rule index
 824 * @group:      Group index
 825 * @v:          Value of bit group
 826 */
 827static void pipapo_bucket_set(struct nft_pipapo_field *f, int rule, int group,
 828                              int v)
 829{
 830        unsigned long *pos;
 831
 832        pos = f->lt + f->bsize * NFT_PIPAPO_BUCKETS * group;
 833        pos += f->bsize * v;
 834
 835        __set_bit(rule, pos);
 836}
 837
 838/**
 839 * pipapo_insert() - Insert new rule in field given input key and mask length
 840 * @f:          Field containing lookup table
 841 * @k:          Input key for classification, without nftables padding
 842 * @mask_bits:  Length of mask; matches field length for non-ranged entry
 843 *
 844 * Insert a new rule reference in lookup buckets corresponding to k and
 845 * mask_bits.
 846 *
 847 * Return: 1 on success (one rule inserted), negative error code on failure.
 848 */
 849static int pipapo_insert(struct nft_pipapo_field *f, const uint8_t *k,
 850                         int mask_bits)
 851{
 852        int rule = f->rules++, group, ret;
 853
 854        ret = pipapo_resize(f, f->rules - 1, f->rules);
 855        if (ret)
 856                return ret;
 857
 858        for (group = 0; group < f->groups; group++) {
 859                int i, v;
 860                u8 mask;
 861
 862                if (group % 2)
 863                        v = k[group / 2] & 0x0f;
 864                else
 865                        v = k[group / 2] >> 4;
 866
 867                if (mask_bits >= (group + 1) * 4) {
 868                        /* Not masked */
 869                        pipapo_bucket_set(f, rule, group, v);
 870                } else if (mask_bits <= group * 4) {
 871                        /* Completely masked */
 872                        for (i = 0; i < NFT_PIPAPO_BUCKETS; i++)
 873                                pipapo_bucket_set(f, rule, group, i);
 874                } else {
 875                        /* The mask limit falls on this group */
 876                        mask = 0x0f >> (mask_bits - group * 4);
 877                        for (i = 0; i < NFT_PIPAPO_BUCKETS; i++) {
 878                                if ((i & ~mask) == (v & ~mask))
 879                                        pipapo_bucket_set(f, rule, group, i);
 880                        }
 881                }
 882        }
 883
 884        return 1;
 885}
 886
 887/**
 888 * pipapo_step_diff() - Check if setting @step bit in netmask would change it
 889 * @base:       Mask we are expanding
 890 * @step:       Step bit for given expansion step
 891 * @len:        Total length of mask space (set and unset bits), bytes
 892 *
 893 * Convenience function for mask expansion.
 894 *
 895 * Return: true if step bit changes mask (i.e. isn't set), false otherwise.
 896 */
 897static bool pipapo_step_diff(u8 *base, int step, int len)
 898{
 899        /* Network order, byte-addressed */
 900#ifdef __BIG_ENDIAN__
 901        return !(BIT(step % BITS_PER_BYTE) & base[step / BITS_PER_BYTE]);
 902#else
 903        return !(BIT(step % BITS_PER_BYTE) &
 904                 base[len - 1 - step / BITS_PER_BYTE]);
 905#endif
 906}
 907
 908/**
 909 * pipapo_step_after_end() - Check if mask exceeds range end with given step
 910 * @base:       Mask we are expanding
 911 * @end:        End of range
 912 * @step:       Step bit for given expansion step, highest bit to be set
 913 * @len:        Total length of mask space (set and unset bits), bytes
 914 *
 915 * Convenience function for mask expansion.
 916 *
 917 * Return: true if mask exceeds range setting step bits, false otherwise.
 918 */
 919static bool pipapo_step_after_end(const u8 *base, const u8 *end, int step,
 920                                  int len)
 921{
 922        u8 tmp[NFT_PIPAPO_MAX_BYTES];
 923        int i;
 924
 925        memcpy(tmp, base, len);
 926
 927        /* Network order, byte-addressed */
 928        for (i = 0; i <= step; i++)
 929#ifdef __BIG_ENDIAN__
 930                tmp[i / BITS_PER_BYTE] |= BIT(i % BITS_PER_BYTE);
 931#else
 932                tmp[len - 1 - i / BITS_PER_BYTE] |= BIT(i % BITS_PER_BYTE);
 933#endif
 934
 935        return memcmp(tmp, end, len) > 0;
 936}
 937
 938/**
 939 * pipapo_base_sum() - Sum step bit to given len-sized netmask base with carry
 940 * @base:       Netmask base
 941 * @step:       Step bit to sum
 942 * @len:        Netmask length, bytes
 943 */
 944static void pipapo_base_sum(u8 *base, int step, int len)
 945{
 946        bool carry = false;
 947        int i;
 948
 949        /* Network order, byte-addressed */
 950#ifdef __BIG_ENDIAN__
 951        for (i = step / BITS_PER_BYTE; i < len; i++) {
 952#else
 953        for (i = len - 1 - step / BITS_PER_BYTE; i >= 0; i--) {
 954#endif
 955                if (carry)
 956                        base[i]++;
 957                else
 958                        base[i] += 1 << (step % BITS_PER_BYTE);
 959
 960                if (base[i])
 961                        break;
 962
 963                carry = true;
 964        }
 965}
 966
 967/**
 968 * pipapo_expand() - Expand to composing netmasks, insert into lookup table
 969 * @f:          Field containing lookup table
 970 * @start:      Start of range
 971 * @end:        End of range
 972 * @len:        Length of value in bits
 973 *
 974 * Expand range to composing netmasks and insert corresponding rule references
 975 * in lookup buckets.
 976 *
 977 * Return: number of inserted rules on success, negative error code on failure.
 978 */
 979static int pipapo_expand(struct nft_pipapo_field *f,
 980                         const u8 *start, const u8 *end, int len)
 981{
 982        int step, masks = 0, bytes = DIV_ROUND_UP(len, BITS_PER_BYTE);
 983        u8 base[NFT_PIPAPO_MAX_BYTES];
 984
 985        memcpy(base, start, bytes);
 986        while (memcmp(base, end, bytes) <= 0) {
 987                int err;
 988
 989                step = 0;
 990                while (pipapo_step_diff(base, step, bytes)) {
 991                        if (pipapo_step_after_end(base, end, step, bytes))
 992                                break;
 993
 994                        step++;
 995                        if (step >= len) {
 996                                if (!masks) {
 997                                        pipapo_insert(f, base, 0);
 998                                        masks = 1;
 999                                }
1000                                goto out;
1001                        }
1002                }
1003
1004                err = pipapo_insert(f, base, len - step);
1005
1006                if (err < 0)
1007                        return err;
1008
1009                masks++;
1010                pipapo_base_sum(base, step, bytes);
1011        }
1012out:
1013        return masks;
1014}
1015
1016/**
1017 * pipapo_map() - Insert rules in mapping tables, mapping them between fields
1018 * @m:          Matching data, including mapping table
1019 * @map:        Table of rule maps: array of first rule and amount of rules
1020 *              in next field a given rule maps to, for each field
1021 * @ext:        For last field, nft_set_ext pointer matching rules map to
1022 */
1023static void pipapo_map(struct nft_pipapo_match *m,
1024                       union nft_pipapo_map_bucket map[NFT_PIPAPO_MAX_FIELDS],
1025                       struct nft_pipapo_elem *e)
1026{
1027        struct nft_pipapo_field *f;
1028        int i, j;
1029
1030        for (i = 0, f = m->f; i < m->field_count - 1; i++, f++) {
1031                for (j = 0; j < map[i].n; j++) {
1032                        f->mt[map[i].to + j].to = map[i + 1].to;
1033                        f->mt[map[i].to + j].n = map[i + 1].n;
1034                }
1035        }
1036
1037        /* Last field: map to ext instead of mapping to next field */
1038        for (j = 0; j < map[i].n; j++)
1039                f->mt[map[i].to + j].e = e;
1040}
1041
1042/**
1043 * pipapo_realloc_scratch() - Reallocate scratch maps for partial match results
1044 * @clone:      Copy of matching data with pending insertions and deletions
1045 * @bsize_max   Maximum bucket size, scratch maps cover two buckets
1046 *
1047 * Return: 0 on success, -ENOMEM on failure.
1048 */
1049static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,
1050                                  unsigned long bsize_max)
1051{
1052        int i;
1053
1054        for_each_possible_cpu(i) {
1055                unsigned long *scratch;
1056
1057                scratch = kzalloc_node(bsize_max * sizeof(*scratch) * 2,
1058                                       GFP_KERNEL, cpu_to_node(i));
1059                if (!scratch) {
1060                        /* On failure, there's no need to undo previous
1061                         * allocations: this means that some scratch maps have
1062                         * a bigger allocated size now (this is only called on
1063                         * insertion), but the extra space won't be used by any
1064                         * CPU as new elements are not inserted and m->bsize_max
1065                         * is not updated.
1066                         */
1067                        return -ENOMEM;
1068                }
1069
1070                kfree(*per_cpu_ptr(clone->scratch, i));
1071
1072                *per_cpu_ptr(clone->scratch, i) = scratch;
1073        }
1074
1075        return 0;
1076}
1077
1078/**
1079 * nft_pipapo_insert() - Validate and insert ranged elements
1080 * @net:        Network namespace
1081 * @set:        nftables API set representation
1082 * @elem:       nftables API element representation containing key data
1083 * @ext2:       Filled with pointer to &struct nft_set_ext in inserted element
1084 *
1085 * Return: 0 on success, error pointer on failure.
1086 */
1087static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,
1088                             const struct nft_set_elem *elem,
1089                             struct nft_set_ext **ext2)
1090{
1091        const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
1092        union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS];
1093        const u8 *start = (const u8 *)elem->key.val.data, *end;
1094        struct nft_pipapo_elem *e = elem->priv, *dup;
1095        struct nft_pipapo *priv = nft_set_priv(set);
1096        struct nft_pipapo_match *m = priv->clone;
1097        u8 genmask = nft_genmask_next(net);
1098        struct nft_pipapo_field *f;
1099        int i, bsize_max, err = 0;
1100
1101        dup = pipapo_get(net, set, start, genmask);
1102        if (PTR_ERR(dup) == -ENOENT) {
1103                if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END)) {
1104                        end = (const u8 *)nft_set_ext_key_end(ext)->data;
1105                        dup = pipapo_get(net, set, end, nft_genmask_next(net));
1106                } else {
1107                        end = start;
1108                }
1109        }
1110
1111        if (PTR_ERR(dup) != -ENOENT) {
1112                if (IS_ERR(dup))
1113                        return PTR_ERR(dup);
1114                *ext2 = &dup->ext;
1115                return -EEXIST;
1116        }
1117
1118        /* Validate */
1119        nft_pipapo_for_each_field(f, i, m) {
1120                const u8 *start_p = start, *end_p = end;
1121
1122                if (f->rules >= (unsigned long)NFT_PIPAPO_RULE0_MAX)
1123                        return -ENOSPC;
1124
1125                if (memcmp(start_p, end_p,
1126                           f->groups / NFT_PIPAPO_GROUPS_PER_BYTE) > 0)
1127                        return -EINVAL;
1128
1129                start_p += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1130                end_p += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1131        }
1132
1133        /* Insert */
1134        priv->dirty = true;
1135
1136        bsize_max = m->bsize_max;
1137
1138        nft_pipapo_for_each_field(f, i, m) {
1139                int ret;
1140
1141                rulemap[i].to = f->rules;
1142
1143                ret = memcmp(start, end,
1144                             f->groups / NFT_PIPAPO_GROUPS_PER_BYTE);
1145                if (!ret) {
1146                        ret = pipapo_insert(f, start,
1147                                            f->groups * NFT_PIPAPO_GROUP_BITS);
1148                } else {
1149                        ret = pipapo_expand(f, start, end,
1150                                            f->groups * NFT_PIPAPO_GROUP_BITS);
1151                }
1152
1153                if (f->bsize > bsize_max)
1154                        bsize_max = f->bsize;
1155
1156                rulemap[i].n = ret;
1157
1158                start += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1159                end += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1160        }
1161
1162        if (!*get_cpu_ptr(m->scratch) || bsize_max > m->bsize_max) {
1163                put_cpu_ptr(m->scratch);
1164
1165                err = pipapo_realloc_scratch(m, bsize_max);
1166                if (err)
1167                        return err;
1168
1169                m->bsize_max = bsize_max;
1170        } else {
1171                put_cpu_ptr(m->scratch);
1172        }
1173
1174        *ext2 = &e->ext;
1175
1176        pipapo_map(m, rulemap, e);
1177
1178        return 0;
1179}
1180
1181/**
1182 * pipapo_clone() - Clone matching data to create new working copy
1183 * @old:        Existing matching data
1184 *
1185 * Return: copy of matching data passed as 'old', error pointer on failure
1186 */
1187static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)
1188{
1189        struct nft_pipapo_field *dst, *src;
1190        struct nft_pipapo_match *new;
1191        int i;
1192
1193        new = kmalloc(sizeof(*new) + sizeof(*dst) * old->field_count,
1194                      GFP_KERNEL);
1195        if (!new)
1196                return ERR_PTR(-ENOMEM);
1197
1198        new->field_count = old->field_count;
1199        new->bsize_max = old->bsize_max;
1200
1201        new->scratch = alloc_percpu(*new->scratch);
1202        if (!new->scratch)
1203                goto out_scratch;
1204
1205        for_each_possible_cpu(i)
1206                *per_cpu_ptr(new->scratch, i) = NULL;
1207
1208        if (pipapo_realloc_scratch(new, old->bsize_max))
1209                goto out_scratch_realloc;
1210
1211        rcu_head_init(&new->rcu);
1212
1213        src = old->f;
1214        dst = new->f;
1215
1216        for (i = 0; i < old->field_count; i++) {
1217                memcpy(dst, src, offsetof(struct nft_pipapo_field, lt));
1218
1219                dst->lt = kvzalloc(src->groups * NFT_PIPAPO_BUCKETS *
1220                                   src->bsize * sizeof(*dst->lt),
1221                                   GFP_KERNEL);
1222                if (!dst->lt)
1223                        goto out_lt;
1224
1225                memcpy(dst->lt, src->lt,
1226                       src->bsize * sizeof(*dst->lt) *
1227                       src->groups * NFT_PIPAPO_BUCKETS);
1228
1229                dst->mt = kvmalloc(src->rules * sizeof(*src->mt), GFP_KERNEL);
1230                if (!dst->mt)
1231                        goto out_mt;
1232
1233                memcpy(dst->mt, src->mt, src->rules * sizeof(*src->mt));
1234                src++;
1235                dst++;
1236        }
1237
1238        return new;
1239
1240out_mt:
1241        kvfree(dst->lt);
1242out_lt:
1243        for (dst--; i > 0; i--) {
1244                kvfree(dst->mt);
1245                kvfree(dst->lt);
1246                dst--;
1247        }
1248out_scratch_realloc:
1249        for_each_possible_cpu(i)
1250                kfree(*per_cpu_ptr(new->scratch, i));
1251out_scratch:
1252        free_percpu(new->scratch);
1253        kfree(new);
1254
1255        return ERR_PTR(-ENOMEM);
1256}
1257
1258/**
1259 * pipapo_rules_same_key() - Get number of rules originated from the same entry
1260 * @f:          Field containing mapping table
1261 * @first:      Index of first rule in set of rules mapping to same entry
1262 *
1263 * Using the fact that all rules in a field that originated from the same entry
1264 * will map to the same set of rules in the next field, or to the same element
1265 * reference, return the cardinality of the set of rules that originated from
1266 * the same entry as the rule with index @first, @first rule included.
1267 *
1268 * In pictures:
1269 *                              rules
1270 *      field #0                0    1    2    3    4
1271 *              map to:         0    1   2-4  2-4  5-9
1272 *                              .    .    .......   . ...
1273 *                              |    |    |    | \   \
1274 *                              |    |    |    |  \   \
1275 *                              |    |    |    |   \   \
1276 *                              '    '    '    '    '   \
1277 *      in field #1             0    1    2    3    4    5 ...
1278 *
1279 * if this is called for rule 2 on field #0, it will return 3, as also rules 2
1280 * and 3 in field 0 map to the same set of rules (2, 3, 4) in the next field.
1281 *
1282 * For the last field in a set, we can rely on associated entries to map to the
1283 * same element references.
1284 *
1285 * Return: Number of rules that originated from the same entry as @first.
1286 */
1287static int pipapo_rules_same_key(struct nft_pipapo_field *f, int first)
1288{
1289        struct nft_pipapo_elem *e = NULL; /* Keep gcc happy */
1290        int r;
1291
1292        for (r = first; r < f->rules; r++) {
1293                if (r != first && e != f->mt[r].e)
1294                        return r - first;
1295
1296                e = f->mt[r].e;
1297        }
1298
1299        if (r != first)
1300                return r - first;
1301
1302        return 0;
1303}
1304
1305/**
1306 * pipapo_unmap() - Remove rules from mapping tables, renumber remaining ones
1307 * @mt:         Mapping array
1308 * @rules:      Original amount of rules in mapping table
1309 * @start:      First rule index to be removed
1310 * @n:          Amount of rules to be removed
1311 * @to_offset:  First rule index, in next field, this group of rules maps to
1312 * @is_last:    If this is the last field, delete reference from mapping array
1313 *
1314 * This is used to unmap rules from the mapping table for a single field,
1315 * maintaining consistency and compactness for the existing ones.
1316 *
1317 * In pictures: let's assume that we want to delete rules 2 and 3 from the
1318 * following mapping array:
1319 *
1320 *                 rules
1321 *               0      1      2      3      4
1322 *      map to:  4-10   4-10   11-15  11-15  16-18
1323 *
1324 * the result will be:
1325 *
1326 *                 rules
1327 *               0      1      2
1328 *      map to:  4-10   4-10   11-13
1329 *
1330 * for fields before the last one. In case this is the mapping table for the
1331 * last field in a set, and rules map to pointers to &struct nft_pipapo_elem:
1332 *
1333 *                      rules
1334 *                        0      1      2      3      4
1335 *  element pointers:  0x42   0x42   0x33   0x33   0x44
1336 *
1337 * the result will be:
1338 *
1339 *                      rules
1340 *                        0      1      2
1341 *  element pointers:  0x42   0x42   0x44
1342 */
1343static void pipapo_unmap(union nft_pipapo_map_bucket *mt, int rules,
1344                         int start, int n, int to_offset, bool is_last)
1345{
1346        int i;
1347
1348        memmove(mt + start, mt + start + n, (rules - start - n) * sizeof(*mt));
1349        memset(mt + rules - n, 0, n * sizeof(*mt));
1350
1351        if (is_last)
1352                return;
1353
1354        for (i = start; i < rules - n; i++)
1355                mt[i].to -= to_offset;
1356}
1357
1358/**
1359 * pipapo_drop() - Delete entry from lookup and mapping tables, given rule map
1360 * @m:          Matching data
1361 * @rulemap     Table of rule maps, arrays of first rule and amount of rules
1362 *              in next field a given entry maps to, for each field
1363 *
1364 * For each rule in lookup table buckets mapping to this set of rules, drop
1365 * all bits set in lookup table mapping. In pictures, assuming we want to drop
1366 * rules 0 and 1 from this lookup table:
1367 *
1368 *                     bucket
1369 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1370 *        0    0                                              1,2
1371 *        1   1,2                                      0
1372 *        2    0                                      1,2
1373 *        3    0                              1,2
1374 *        4  0,1,2
1375 *        5    0   1   2
1376 *        6  0,1,2 1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
1377 *        7   1,2 1,2  1   1   1  0,1  1   1   1   1   1   1   1   1   1   1
1378 *
1379 * rule 2 becomes rule 0, and the result will be:
1380 *
1381 *                     bucket
1382 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1383 *        0                                                    0
1384 *        1    0
1385 *        2                                            0
1386 *        3                                    0
1387 *        4    0
1388 *        5            0
1389 *        6    0
1390 *        7    0   0
1391 *
1392 * once this is done, call unmap() to drop all the corresponding rule references
1393 * from mapping tables.
1394 */
1395static void pipapo_drop(struct nft_pipapo_match *m,
1396                        union nft_pipapo_map_bucket rulemap[])
1397{
1398        struct nft_pipapo_field *f;
1399        int i;
1400
1401        nft_pipapo_for_each_field(f, i, m) {
1402                int g;
1403
1404                for (g = 0; g < f->groups; g++) {
1405                        unsigned long *pos;
1406                        int b;
1407
1408                        pos = f->lt + g * NFT_PIPAPO_BUCKETS * f->bsize;
1409
1410                        for (b = 0; b < NFT_PIPAPO_BUCKETS; b++) {
1411                                bitmap_cut(pos, pos, rulemap[i].to,
1412                                           rulemap[i].n,
1413                                           f->bsize * BITS_PER_LONG);
1414
1415                                pos += f->bsize;
1416                        }
1417                }
1418
1419                pipapo_unmap(f->mt, f->rules, rulemap[i].to, rulemap[i].n,
1420                             rulemap[i + 1].n, i == m->field_count - 1);
1421                if (pipapo_resize(f, f->rules, f->rules - rulemap[i].n)) {
1422                        /* We can ignore this, a failure to shrink tables down
1423                         * doesn't make tables invalid.
1424                         */
1425                        ;
1426                }
1427                f->rules -= rulemap[i].n;
1428        }
1429}
1430
1431/**
1432 * pipapo_gc() - Drop expired entries from set, destroy start and end elements
1433 * @set:        nftables API set representation
1434 * @m:          Matching data
1435 */
1436static void pipapo_gc(const struct nft_set *set, struct nft_pipapo_match *m)
1437{
1438        struct nft_pipapo *priv = nft_set_priv(set);
1439        int rules_f0, first_rule = 0;
1440
1441        while ((rules_f0 = pipapo_rules_same_key(m->f, first_rule))) {
1442                union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS];
1443                struct nft_pipapo_field *f;
1444                struct nft_pipapo_elem *e;
1445                int i, start, rules_fx;
1446
1447                start = first_rule;
1448                rules_fx = rules_f0;
1449
1450                nft_pipapo_for_each_field(f, i, m) {
1451                        rulemap[i].to = start;
1452                        rulemap[i].n = rules_fx;
1453
1454                        if (i < m->field_count - 1) {
1455                                rules_fx = f->mt[start].n;
1456                                start = f->mt[start].to;
1457                        }
1458                }
1459
1460                /* Pick the last field, and its last index */
1461                f--;
1462                i--;
1463                e = f->mt[rulemap[i].to].e;
1464                if (nft_set_elem_expired(&e->ext) &&
1465                    !nft_set_elem_mark_busy(&e->ext)) {
1466                        priv->dirty = true;
1467                        pipapo_drop(m, rulemap);
1468
1469                        rcu_barrier();
1470                        nft_set_elem_destroy(set, e, true);
1471
1472                        /* And check again current first rule, which is now the
1473                         * first we haven't checked.
1474                         */
1475                } else {
1476                        first_rule += rules_f0;
1477                }
1478        }
1479
1480        priv->last_gc = jiffies;
1481}
1482
1483/**
1484 * pipapo_free_fields() - Free per-field tables contained in matching data
1485 * @m:          Matching data
1486 */
1487static void pipapo_free_fields(struct nft_pipapo_match *m)
1488{
1489        struct nft_pipapo_field *f;
1490        int i;
1491
1492        nft_pipapo_for_each_field(f, i, m) {
1493                kvfree(f->lt);
1494                kvfree(f->mt);
1495        }
1496}
1497
1498/**
1499 * pipapo_reclaim_match - RCU callback to free fields from old matching data
1500 * @rcu:        RCU head
1501 */
1502static void pipapo_reclaim_match(struct rcu_head *rcu)
1503{
1504        struct nft_pipapo_match *m;
1505        int i;
1506
1507        m = container_of(rcu, struct nft_pipapo_match, rcu);
1508
1509        for_each_possible_cpu(i)
1510                kfree(*per_cpu_ptr(m->scratch, i));
1511
1512        free_percpu(m->scratch);
1513
1514        pipapo_free_fields(m);
1515
1516        kfree(m);
1517}
1518
1519/**
1520 * pipapo_commit() - Replace lookup data with current working copy
1521 * @set:        nftables API set representation
1522 *
1523 * While at it, check if we should perform garbage collection on the working
1524 * copy before committing it for lookup, and don't replace the table if the
1525 * working copy doesn't have pending changes.
1526 *
1527 * We also need to create a new working copy for subsequent insertions and
1528 * deletions.
1529 */
1530static void pipapo_commit(const struct nft_set *set)
1531{
1532        struct nft_pipapo *priv = nft_set_priv(set);
1533        struct nft_pipapo_match *new_clone, *old;
1534
1535        if (time_after_eq(jiffies, priv->last_gc + nft_set_gc_interval(set)))
1536                pipapo_gc(set, priv->clone);
1537
1538        if (!priv->dirty)
1539                return;
1540
1541        new_clone = pipapo_clone(priv->clone);
1542        if (IS_ERR(new_clone))
1543                return;
1544
1545        priv->dirty = false;
1546
1547        old = rcu_access_pointer(priv->match);
1548        rcu_assign_pointer(priv->match, priv->clone);
1549        if (old)
1550                call_rcu(&old->rcu, pipapo_reclaim_match);
1551
1552        priv->clone = new_clone;
1553}
1554
1555/**
1556 * nft_pipapo_activate() - Mark element reference as active given key, commit
1557 * @net:        Network namespace
1558 * @set:        nftables API set representation
1559 * @elem:       nftables API element representation containing key data
1560 *
1561 * On insertion, elements are added to a copy of the matching data currently
1562 * in use for lookups, and not directly inserted into current lookup data, so
1563 * we'll take care of that by calling pipapo_commit() here. Both
1564 * nft_pipapo_insert() and nft_pipapo_activate() are called once for each
1565 * element, hence we can't purpose either one as a real commit operation.
1566 */
1567static void nft_pipapo_activate(const struct net *net,
1568                                const struct nft_set *set,
1569                                const struct nft_set_elem *elem)
1570{
1571        struct nft_pipapo_elem *e;
1572
1573        e = pipapo_get(net, set, (const u8 *)elem->key.val.data, 0);
1574        if (IS_ERR(e))
1575                return;
1576
1577        nft_set_elem_change_active(net, set, &e->ext);
1578        nft_set_elem_clear_busy(&e->ext);
1579
1580        pipapo_commit(set);
1581}
1582
1583/**
1584 * pipapo_deactivate() - Check that element is in set, mark as inactive
1585 * @net:        Network namespace
1586 * @set:        nftables API set representation
1587 * @data:       Input key data
1588 * @ext:        nftables API extension pointer, used to check for end element
1589 *
1590 * This is a convenience function that can be called from both
1591 * nft_pipapo_deactivate() and nft_pipapo_flush(), as they are in fact the same
1592 * operation.
1593 *
1594 * Return: deactivated element if found, NULL otherwise.
1595 */
1596static void *pipapo_deactivate(const struct net *net, const struct nft_set *set,
1597                               const u8 *data, const struct nft_set_ext *ext)
1598{
1599        struct nft_pipapo_elem *e;
1600
1601        e = pipapo_get(net, set, data, nft_genmask_next(net));
1602        if (IS_ERR(e))
1603                return NULL;
1604
1605        nft_set_elem_change_active(net, set, &e->ext);
1606
1607        return e;
1608}
1609
1610/**
1611 * nft_pipapo_deactivate() - Call pipapo_deactivate() to make element inactive
1612 * @net:        Network namespace
1613 * @set:        nftables API set representation
1614 * @elem:       nftables API element representation containing key data
1615 *
1616 * Return: deactivated element if found, NULL otherwise.
1617 */
1618static void *nft_pipapo_deactivate(const struct net *net,
1619                                   const struct nft_set *set,
1620                                   const struct nft_set_elem *elem)
1621{
1622        const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
1623
1624        return pipapo_deactivate(net, set, (const u8 *)elem->key.val.data, ext);
1625}
1626
1627/**
1628 * nft_pipapo_flush() - Call pipapo_deactivate() to make element inactive
1629 * @net:        Network namespace
1630 * @set:        nftables API set representation
1631 * @elem:       nftables API element representation containing key data
1632 *
1633 * This is functionally the same as nft_pipapo_deactivate(), with a slightly
1634 * different interface, and it's also called once for each element in a set
1635 * being flushed, so we can't implement, strictly speaking, a flush operation,
1636 * which would otherwise be as simple as allocating an empty copy of the
1637 * matching data.
1638 *
1639 * Note that we could in theory do that, mark the set as flushed, and ignore
1640 * subsequent calls, but we would leak all the elements after the first one,
1641 * because they wouldn't then be freed as result of API calls.
1642 *
1643 * Return: true if element was found and deactivated.
1644 */
1645static bool nft_pipapo_flush(const struct net *net, const struct nft_set *set,
1646                             void *elem)
1647{
1648        struct nft_pipapo_elem *e = elem;
1649
1650        return pipapo_deactivate(net, set, (const u8 *)nft_set_ext_key(&e->ext),
1651                                 &e->ext);
1652}
1653
1654/**
1655 * pipapo_get_boundaries() - Get byte interval for associated rules
1656 * @f:          Field including lookup table
1657 * @first_rule: First rule (lowest index)
1658 * @rule_count: Number of associated rules
1659 * @left:       Byte expression for left boundary (start of range)
1660 * @right:      Byte expression for right boundary (end of range)
1661 *
1662 * Given the first rule and amount of rules that originated from the same entry,
1663 * build the original range associated with the entry, and calculate the length
1664 * of the originating netmask.
1665 *
1666 * In pictures:
1667 *
1668 *                     bucket
1669 *      group  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1670 *        0                                                   1,2
1671 *        1   1,2
1672 *        2                                           1,2
1673 *        3                                   1,2
1674 *        4   1,2
1675 *        5        1   2
1676 *        6   1,2  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
1677 *        7   1,2 1,2  1   1   1   1   1   1   1   1   1   1   1   1   1   1
1678 *
1679 * this is the lookup table corresponding to the IPv4 range
1680 * 192.168.1.0-192.168.2.1, which was expanded to the two composing netmasks,
1681 * rule #1: 192.168.1.0/24, and rule #2: 192.168.2.0/31.
1682 *
1683 * This function fills @left and @right with the byte values of the leftmost
1684 * and rightmost bucket indices for the lowest and highest rule indices,
1685 * respectively. If @first_rule is 1 and @rule_count is 2, we obtain, in
1686 * nibbles:
1687 *   left:  < 12, 0, 10, 8, 0, 1, 0, 0 >
1688 *   right: < 12, 0, 10, 8, 0, 2, 2, 1 >
1689 * corresponding to bytes:
1690 *   left:  < 192, 168, 1, 0 >
1691 *   right: < 192, 168, 2, 1 >
1692 * with mask length irrelevant here, unused on return, as the range is already
1693 * defined by its start and end points. The mask length is relevant for a single
1694 * ranged entry instead: if @first_rule is 1 and @rule_count is 1, we ignore
1695 * rule 2 above: @left becomes < 192, 168, 1, 0 >, @right becomes
1696 * < 192, 168, 1, 255 >, and the mask length, calculated from the distances
1697 * between leftmost and rightmost bucket indices for each group, would be 24.
1698 *
1699 * Return: mask length, in bits.
1700 */
1701static int pipapo_get_boundaries(struct nft_pipapo_field *f, int first_rule,
1702                                 int rule_count, u8 *left, u8 *right)
1703{
1704        u8 *l = left, *r = right;
1705        int g, mask_len = 0;
1706
1707        for (g = 0; g < f->groups; g++) {
1708                int b, x0, x1;
1709
1710                x0 = -1;
1711                x1 = -1;
1712                for (b = 0; b < NFT_PIPAPO_BUCKETS; b++) {
1713                        unsigned long *pos;
1714
1715                        pos = f->lt + (g * NFT_PIPAPO_BUCKETS + b) * f->bsize;
1716                        if (test_bit(first_rule, pos) && x0 == -1)
1717                                x0 = b;
1718                        if (test_bit(first_rule + rule_count - 1, pos))
1719                                x1 = b;
1720                }
1721
1722                if (g % 2) {
1723                        *(l++) |= x0 & 0x0f;
1724                        *(r++) |= x1 & 0x0f;
1725                } else {
1726                        *l |= x0 << 4;
1727                        *r |= x1 << 4;
1728                }
1729
1730                if (x1 - x0 == 0)
1731                        mask_len += 4;
1732                else if (x1 - x0 == 1)
1733                        mask_len += 3;
1734                else if (x1 - x0 == 3)
1735                        mask_len += 2;
1736                else if (x1 - x0 == 7)
1737                        mask_len += 1;
1738        }
1739
1740        return mask_len;
1741}
1742
1743/**
1744 * pipapo_match_field() - Match rules against byte ranges
1745 * @f:          Field including the lookup table
1746 * @first_rule: First of associated rules originating from same entry
1747 * @rule_count: Amount of associated rules
1748 * @start:      Start of range to be matched
1749 * @end:        End of range to be matched
1750 *
1751 * Return: true on match, false otherwise.
1752 */
1753static bool pipapo_match_field(struct nft_pipapo_field *f,
1754                               int first_rule, int rule_count,
1755                               const u8 *start, const u8 *end)
1756{
1757        u8 right[NFT_PIPAPO_MAX_BYTES] = { 0 };
1758        u8 left[NFT_PIPAPO_MAX_BYTES] = { 0 };
1759
1760        pipapo_get_boundaries(f, first_rule, rule_count, left, right);
1761
1762        return !memcmp(start, left, f->groups / NFT_PIPAPO_GROUPS_PER_BYTE) &&
1763               !memcmp(end, right, f->groups / NFT_PIPAPO_GROUPS_PER_BYTE);
1764}
1765
1766/**
1767 * nft_pipapo_remove() - Remove element given key, commit
1768 * @net:        Network namespace
1769 * @set:        nftables API set representation
1770 * @elem:       nftables API element representation containing key data
1771 *
1772 * Similarly to nft_pipapo_activate(), this is used as commit operation by the
1773 * API, but it's called once per element in the pending transaction, so we can't
1774 * implement this as a single commit operation. Closest we can get is to remove
1775 * the matched element here, if any, and commit the updated matching data.
1776 */
1777static void nft_pipapo_remove(const struct net *net, const struct nft_set *set,
1778                              const struct nft_set_elem *elem)
1779{
1780        struct nft_pipapo *priv = nft_set_priv(set);
1781        struct nft_pipapo_match *m = priv->clone;
1782        struct nft_pipapo_elem *e = elem->priv;
1783        int rules_f0, first_rule = 0;
1784        const u8 *data;
1785
1786        data = (const u8 *)nft_set_ext_key(&e->ext);
1787
1788        e = pipapo_get(net, set, data, 0);
1789        if (IS_ERR(e))
1790                return;
1791
1792        while ((rules_f0 = pipapo_rules_same_key(m->f, first_rule))) {
1793                union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS];
1794                const u8 *match_start, *match_end;
1795                struct nft_pipapo_field *f;
1796                int i, start, rules_fx;
1797
1798                match_start = data;
1799                match_end = (const u8 *)nft_set_ext_key_end(&e->ext)->data;
1800
1801                start = first_rule;
1802                rules_fx = rules_f0;
1803
1804                nft_pipapo_for_each_field(f, i, m) {
1805                        if (!pipapo_match_field(f, start, rules_fx,
1806                                                match_start, match_end))
1807                                break;
1808
1809                        rulemap[i].to = start;
1810                        rulemap[i].n = rules_fx;
1811
1812                        rules_fx = f->mt[start].n;
1813                        start = f->mt[start].to;
1814
1815                        match_start += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1816                        match_end += NFT_PIPAPO_GROUPS_PADDED_SIZE(f->groups);
1817                }
1818
1819                if (i == m->field_count) {
1820                        priv->dirty = true;
1821                        pipapo_drop(m, rulemap);
1822                        pipapo_commit(set);
1823                        return;
1824                }
1825
1826                first_rule += rules_f0;
1827        }
1828}
1829
1830/**
1831 * nft_pipapo_walk() - Walk over elements
1832 * @ctx:        nftables API context
1833 * @set:        nftables API set representation
1834 * @iter:       Iterator
1835 *
1836 * As elements are referenced in the mapping array for the last field, directly
1837 * scan that array: there's no need to follow rule mappings from the first
1838 * field.
1839 */
1840static void nft_pipapo_walk(const struct nft_ctx *ctx, struct nft_set *set,
1841                            struct nft_set_iter *iter)
1842{
1843        struct nft_pipapo *priv = nft_set_priv(set);
1844        struct nft_pipapo_match *m;
1845        struct nft_pipapo_field *f;
1846        int i, r;
1847
1848        rcu_read_lock();
1849        m = rcu_dereference(priv->match);
1850
1851        if (unlikely(!m))
1852                goto out;
1853
1854        for (i = 0, f = m->f; i < m->field_count - 1; i++, f++)
1855                ;
1856
1857        for (r = 0; r < f->rules; r++) {
1858                struct nft_pipapo_elem *e;
1859                struct nft_set_elem elem;
1860
1861                if (r < f->rules - 1 && f->mt[r + 1].e == f->mt[r].e)
1862                        continue;
1863
1864                if (iter->count < iter->skip)
1865                        goto cont;
1866
1867                e = f->mt[r].e;
1868                if (nft_set_elem_expired(&e->ext))
1869                        goto cont;
1870
1871                elem.priv = e;
1872
1873                iter->err = iter->fn(ctx, set, iter, &elem);
1874                if (iter->err < 0)
1875                        goto out;
1876
1877cont:
1878                iter->count++;
1879        }
1880
1881out:
1882        rcu_read_unlock();
1883}
1884
1885/**
1886 * nft_pipapo_privsize() - Return the size of private data for the set
1887 * @nla:        netlink attributes, ignored as size doesn't depend on them
1888 * @desc:       Set description, ignored as size doesn't depend on it
1889 *
1890 * Return: size of private data for this set implementation, in bytes
1891 */
1892static u64 nft_pipapo_privsize(const struct nlattr * const nla[],
1893                               const struct nft_set_desc *desc)
1894{
1895        return sizeof(struct nft_pipapo);
1896}
1897
1898/**
1899 * nft_pipapo_estimate() - Estimate set size, space and lookup complexity
1900 * @desc:       Set description, element count and field description used here
1901 * @features:   Flags: NFT_SET_INTERVAL needs to be there
1902 * @est:        Storage for estimation data
1903 *
1904 * The size for this set type can vary dramatically, as it depends on the number
1905 * of rules (composing netmasks) the entries expand to. We compute the worst
1906 * case here.
1907 *
1908 * In general, for a non-ranged entry or a single composing netmask, we need
1909 * one bit in each of the sixteen NFT_PIPAPO_BUCKETS, for each 4-bit group (that
1910 * is, each input bit needs four bits of matching data), plus a bucket in the
1911 * mapping table for each field.
1912 *
1913 * Return: true only for compatible range concatenations
1914 */
1915static bool nft_pipapo_estimate(const struct nft_set_desc *desc, u32 features,
1916                                struct nft_set_estimate *est)
1917{
1918        unsigned long entry_size;
1919        int i;
1920
1921        if (!(features & NFT_SET_INTERVAL) || desc->field_count <= 1)
1922                return false;
1923
1924        for (i = 0, entry_size = 0; i < desc->field_count; i++) {
1925                unsigned long rules;
1926
1927                if (desc->field_len[i] > NFT_PIPAPO_MAX_BYTES)
1928                        return false;
1929
1930                /* Worst-case ranges for each concatenated field: each n-bit
1931                 * field can expand to up to n * 2 rules in each bucket, and
1932                 * each rule also needs a mapping bucket.
1933                 */
1934                rules = ilog2(desc->field_len[i] * BITS_PER_BYTE) * 2;
1935                entry_size += rules * NFT_PIPAPO_BUCKETS / BITS_PER_BYTE;
1936                entry_size += rules * sizeof(union nft_pipapo_map_bucket);
1937        }
1938
1939        /* Rules in lookup and mapping tables are needed for each entry */
1940        est->size = desc->size * entry_size;
1941        if (est->size && div_u64(est->size, desc->size) != entry_size)
1942                return false;
1943
1944        est->size += sizeof(struct nft_pipapo) +
1945                     sizeof(struct nft_pipapo_match) * 2;
1946
1947        est->size += sizeof(struct nft_pipapo_field) * desc->field_count;
1948
1949        est->lookup = NFT_SET_CLASS_O_LOG_N;
1950
1951        est->space = NFT_SET_CLASS_O_N;
1952
1953        return true;
1954}
1955
1956/**
1957 * nft_pipapo_init() - Initialise data for a set instance
1958 * @set:        nftables API set representation
1959 * @desc:       Set description
1960 * @nla:        netlink attributes
1961 *
1962 * Validate number and size of fields passed as NFTA_SET_DESC_CONCAT netlink
1963 * attributes, initialise internal set parameters, current instance of matching
1964 * data and a copy for subsequent insertions.
1965 *
1966 * Return: 0 on success, negative error code on failure.
1967 */
1968static int nft_pipapo_init(const struct nft_set *set,
1969                           const struct nft_set_desc *desc,
1970                           const struct nlattr * const nla[])
1971{
1972        struct nft_pipapo *priv = nft_set_priv(set);
1973        struct nft_pipapo_match *m;
1974        struct nft_pipapo_field *f;
1975        int err, i;
1976
1977        if (desc->field_count > NFT_PIPAPO_MAX_FIELDS)
1978                return -EINVAL;
1979
1980        m = kmalloc(sizeof(*priv->match) + sizeof(*f) * desc->field_count,
1981                    GFP_KERNEL);
1982        if (!m)
1983                return -ENOMEM;
1984
1985        m->field_count = desc->field_count;
1986        m->bsize_max = 0;
1987
1988        m->scratch = alloc_percpu(unsigned long *);
1989        if (!m->scratch) {
1990                err = -ENOMEM;
1991                goto out_free;
1992        }
1993        for_each_possible_cpu(i)
1994                *per_cpu_ptr(m->scratch, i) = NULL;
1995
1996        rcu_head_init(&m->rcu);
1997
1998        nft_pipapo_for_each_field(f, i, m) {
1999                f->groups = desc->field_len[i] * NFT_PIPAPO_GROUPS_PER_BYTE;
2000                priv->groups += f->groups;
2001
2002                priv->width += round_up(desc->field_len[i], sizeof(u32));
2003
2004                f->bsize = 0;
2005                f->rules = 0;
2006                f->lt = NULL;
2007                f->mt = NULL;
2008        }
2009
2010        /* Create an initial clone of matching data for next insertion */
2011        priv->clone = pipapo_clone(m);
2012        if (IS_ERR(priv->clone)) {
2013                err = PTR_ERR(priv->clone);
2014                goto out_free;
2015        }
2016
2017        priv->dirty = false;
2018
2019        rcu_assign_pointer(priv->match, m);
2020
2021        return 0;
2022
2023out_free:
2024        free_percpu(m->scratch);
2025        kfree(m);
2026
2027        return err;
2028}
2029
2030/**
2031 * nft_pipapo_destroy() - Free private data for set and all committed elements
2032 * @set:        nftables API set representation
2033 */
2034static void nft_pipapo_destroy(const struct nft_set *set)
2035{
2036        struct nft_pipapo *priv = nft_set_priv(set);
2037        struct nft_pipapo_match *m;
2038        struct nft_pipapo_field *f;
2039        int i, r, cpu;
2040
2041        m = rcu_dereference_protected(priv->match, true);
2042        if (m) {
2043                rcu_barrier();
2044
2045                for (i = 0, f = m->f; i < m->field_count - 1; i++, f++)
2046                        ;
2047
2048                for (r = 0; r < f->rules; r++) {
2049                        struct nft_pipapo_elem *e;
2050
2051                        if (r < f->rules - 1 && f->mt[r + 1].e == f->mt[r].e)
2052                                continue;
2053
2054                        e = f->mt[r].e;
2055
2056                        nft_set_elem_destroy(set, e, true);
2057                }
2058
2059                for_each_possible_cpu(cpu)
2060                        kfree(*per_cpu_ptr(m->scratch, cpu));
2061                free_percpu(m->scratch);
2062
2063                pipapo_free_fields(m);
2064                kfree(m);
2065                priv->match = NULL;
2066        }
2067
2068        if (priv->clone) {
2069                for_each_possible_cpu(cpu)
2070                        kfree(*per_cpu_ptr(priv->clone->scratch, cpu));
2071                free_percpu(priv->clone->scratch);
2072
2073                pipapo_free_fields(priv->clone);
2074                kfree(priv->clone);
2075                priv->clone = NULL;
2076        }
2077}
2078
2079/**
2080 * nft_pipapo_gc_init() - Initialise garbage collection
2081 * @set:        nftables API set representation
2082 *
2083 * Instead of actually setting up a periodic work for garbage collection, as
2084 * this operation requires a swap of matching data with the working copy, we'll
2085 * do that opportunistically with other commit operations if the interval is
2086 * elapsed, so we just need to set the current jiffies timestamp here.
2087 */
2088static void nft_pipapo_gc_init(const struct nft_set *set)
2089{
2090        struct nft_pipapo *priv = nft_set_priv(set);
2091
2092        priv->last_gc = jiffies;
2093}
2094
2095struct nft_set_type nft_set_pipapo_type __read_mostly = {
2096        .owner          = THIS_MODULE,
2097        .features       = NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_OBJECT |
2098                          NFT_SET_TIMEOUT,
2099        .ops            = {
2100                .lookup         = nft_pipapo_lookup,
2101                .insert         = nft_pipapo_insert,
2102                .activate       = nft_pipapo_activate,
2103                .deactivate     = nft_pipapo_deactivate,
2104                .flush          = nft_pipapo_flush,
2105                .remove         = nft_pipapo_remove,
2106                .walk           = nft_pipapo_walk,
2107                .get            = nft_pipapo_get,
2108                .privsize       = nft_pipapo_privsize,
2109                .estimate       = nft_pipapo_estimate,
2110                .init           = nft_pipapo_init,
2111                .destroy        = nft_pipapo_destroy,
2112                .gc_init        = nft_pipapo_gc_init,
2113                .elemsize       = offsetof(struct nft_pipapo_elem, ext),
2114        },
2115};
2116