| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 3 | * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License version 2 as | ||
| 7 | * published by the Free Software Foundation. | ||
| 8 | * | ||
| 9 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <nft.h> | ||
| 13 | |||
| 14 | #include <linux/netfilter/nf_tables.h> | ||
| 15 | #include <linux/netfilter/nf_log.h> | ||
| 16 | |||
| 17 | #include <rule.h> | ||
| 18 | #include <statement.h> | ||
| 19 | #include <expression.h> | ||
| 20 | #include <netlink.h> | ||
| 21 | #include <gmputil.h> | ||
| 22 | #include <utils.h> | ||
| 23 | #include <netinet/in.h> | ||
| 24 | |||
| 25 | #include <linux/netfilter.h> | ||
| 26 | #include <libnftnl/udata.h> | ||
| 27 | |||
| 28 | 251904 | struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle, | |
| 29 | struct netlink_linearize_ctx *ctx) | ||
| 30 | { | ||
| 31 | struct nft_expr_loc *eloc; | ||
| 32 | uint32_t hash; | ||
| 33 | |||
| 34 | 251904 | hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE; | |
| 35 |
2/2✓ Branch 0 (6→3) taken 283026 times.
✓ Branch 1 (6→7) taken 85 times.
|
283111 | list_for_each_entry(eloc, &ctx->expr_loc_htable[hash], hlist) { |
| 36 |
2/2✓ Branch 0 (3→4) taken 251819 times.
✓ Branch 1 (3→5) taken 31207 times.
|
283026 | if (eloc->nle == nle) |
| 37 | 251819 | return eloc; | |
| 38 | } | ||
| 39 | |||
| 40 | 85 | return NULL; | |
| 41 | } | ||
| 42 | |||
| 43 | 251819 | static void nft_expr_loc_add(const struct nftnl_expr *nle, | |
| 44 | const struct location *loc, | ||
| 45 | struct netlink_linearize_ctx *ctx) | ||
| 46 | { | ||
| 47 | struct nft_expr_loc *eloc; | ||
| 48 | uint32_t hash; | ||
| 49 | |||
| 50 | 251819 | eloc = xmalloc(sizeof(*eloc)); | |
| 51 | 251819 | eloc->nle = nle; | |
| 52 | 251819 | eloc->loc = loc; | |
| 53 | 251819 | hash = (uint64_t)nle % NFT_EXPR_LOC_HSIZE; | |
| 54 | 251819 | list_add_tail(&eloc->hlist, &ctx->expr_loc_htable[hash]); | |
| 55 | 251819 | } | |
| 56 | |||
| 57 | 254116 | static void netlink_put_register(struct nftnl_expr *nle, | |
| 58 | uint32_t attr, uint32_t reg) | ||
| 59 | { | ||
| 60 | /* Convert to 128 bit register numbers if possible for compatibility */ | ||
| 61 |
2/2✓ Branch 0 (2→3) taken 239277 times.
✓ Branch 1 (2→6) taken 14839 times.
|
254116 | if (reg != NFT_REG_VERDICT) { |
| 62 | 239277 | reg -= NFT_REG_1; | |
| 63 |
2/2✓ Branch 0 (3→4) taken 234737 times.
✓ Branch 1 (3→5) taken 4540 times.
|
239277 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
| 64 | 234737 | reg = NFT_REG_1 + reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
| 65 | else | ||
| 66 | 4540 | reg += NFT_REG32_00; | |
| 67 | } | ||
| 68 | |||
| 69 | 254116 | nftnl_expr_set_u32(nle, attr, reg); | |
| 70 | 254116 | } | |
| 71 | |||
| 72 | − | static enum nft_registers __get_register(struct netlink_linearize_ctx *ctx, | |
| 73 | unsigned int size) | ||
| 74 | { | ||
| 75 | unsigned int reg, n; | ||
| 76 | |||
| 77 | − | n = netlink_register_space(size); | |
| 78 | − | if (ctx->reg_low + n > NFT_REG_1 + NFT_REG32_15 - NFT_REG32_00 + 1) | |
| 79 | − | BUG("register reg_low %u invalid", ctx->reg_low); | |
| 80 | |||
| 81 | − | reg = ctx->reg_low; | |
| 82 | − | ctx->reg_low += n; | |
| 83 | − | return reg; | |
| 84 | } | ||
| 85 | |||
| 86 | − | static void __release_register(struct netlink_linearize_ctx *ctx, | |
| 87 | unsigned int size) | ||
| 88 | { | ||
| 89 | unsigned int n; | ||
| 90 | |||
| 91 | − | n = netlink_register_space(size); | |
| 92 | − | if (ctx->reg_low < NFT_REG_1 + n) | |
| 93 | − | BUG("register reg_low %u invalid", ctx->reg_low); | |
| 94 | |||
| 95 | − | ctx->reg_low -= n; | |
| 96 | − | } | |
| 97 | |||
| 98 | 113353 | static enum nft_registers get_register(struct netlink_linearize_ctx *ctx, | |
| 99 | const struct expr *expr) | ||
| 100 | { | ||
| 101 |
4/4✓ Branch 0 (2→3) taken 112718 times.
✓ Branch 1 (2→6) taken 635 times.
✓ Branch 2 (3→4) taken 1782 times.
✓ Branch 3 (3→6) taken 110936 times.
|
113353 | if (expr && expr->etype == EXPR_CONCAT) |
| 102 | 1782 | return __get_register(ctx, expr->len); | |
| 103 | else | ||
| 104 | 111571 | return __get_register(ctx, NFT_REG_SIZE * BITS_PER_BYTE); | |
| 105 | } | ||
| 106 | |||
| 107 | 113353 | static void release_register(struct netlink_linearize_ctx *ctx, | |
| 108 | const struct expr *expr) | ||
| 109 | { | ||
| 110 |
4/4✓ Branch 0 (2→3) taken 112718 times.
✓ Branch 1 (2→5) taken 635 times.
✓ Branch 2 (3→4) taken 1782 times.
✓ Branch 3 (3→5) taken 110936 times.
|
113353 | if (expr && expr->etype == EXPR_CONCAT) |
| 111 | 1782 | __release_register(ctx, expr->len); | |
| 112 | else | ||
| 113 | 111571 | __release_register(ctx, NFT_REG_SIZE * BITS_PER_BYTE); | |
| 114 | 113353 | } | |
| 115 | |||
| 116 | static void netlink_gen_expr(struct netlink_linearize_ctx *ctx, | ||
| 117 | const struct expr *expr, | ||
| 118 | enum nft_registers dreg); | ||
| 119 | |||
| 120 | 1963 | static void netlink_gen_concat(struct netlink_linearize_ctx *ctx, | |
| 121 | const struct expr *expr, | ||
| 122 | enum nft_registers dreg) | ||
| 123 | { | ||
| 124 | const struct expr *i; | ||
| 125 | |||
| 126 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1963 times.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 6214 times.
✓ Branch 4 (10→5) taken 4251 times.
✓ Branch 5 (10→11) taken 1963 times.
|
6214 | list_for_each_entry(i, &expr_concat(expr)->expressions, list) { |
| 127 | 4251 | netlink_gen_expr(ctx, i, dreg); | |
| 128 | 4251 | dreg += netlink_register_space(i->len); | |
| 129 | } | ||
| 130 | 1963 | } | |
| 131 | |||
| 132 | 251819 | static void nft_rule_add_expr(struct netlink_linearize_ctx *ctx, | |
| 133 | struct nftnl_expr *nle, | ||
| 134 | const struct location *loc) | ||
| 135 | { | ||
| 136 | 251819 | nft_expr_loc_add(nle, loc, ctx); | |
| 137 | 251819 | nftnl_rule_add_expr(ctx->nlr, nle); | |
| 138 | 251819 | } | |
| 139 | |||
| 140 | 64 | static void netlink_gen_fib(struct netlink_linearize_ctx *ctx, | |
| 141 | const struct expr *expr, | ||
| 142 | enum nft_registers dreg) | ||
| 143 | { | ||
| 144 | struct nftnl_expr *nle; | ||
| 145 | |||
| 146 | 64 | nle = alloc_nft_expr("fib"); | |
| 147 | 64 | netlink_put_register(nle, NFTNL_EXPR_FIB_DREG, dreg); | |
| 148 | 64 | nftnl_expr_set_u32(nle, NFTNL_EXPR_FIB_RESULT, expr->fib.result); | |
| 149 | 64 | nftnl_expr_set_u32(nle, NFTNL_EXPR_FIB_FLAGS, expr->fib.flags); | |
| 150 | |||
| 151 | 64 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 152 | 64 | } | |
| 153 | |||
| 154 | 40 | static void netlink_gen_hash(struct netlink_linearize_ctx *ctx, | |
| 155 | const struct expr *expr, | ||
| 156 | enum nft_registers dreg) | ||
| 157 | { | ||
| 158 | enum nft_registers sreg; | ||
| 159 | struct nftnl_expr *nle; | ||
| 160 | |||
| 161 | 40 | nle = alloc_nft_expr("hash"); | |
| 162 | |||
| 163 |
2/2✓ Branch 0 (3→4) taken 24 times.
✓ Branch 1 (3→9) taken 16 times.
|
40 | if (expr->hash.expr) { |
| 164 | 24 | sreg = get_register(ctx, expr->hash.expr); | |
| 165 | 24 | netlink_gen_expr(ctx, expr->hash.expr, sreg); | |
| 166 | 24 | release_register(ctx, expr->hash.expr); | |
| 167 | 24 | netlink_put_register(nle, NFTNL_EXPR_HASH_SREG, sreg); | |
| 168 | |||
| 169 | 24 | nftnl_expr_set_u32(nle, NFTNL_EXPR_HASH_LEN, | |
| 170 | 24 | div_round_up(expr->hash.expr->len, | |
| 171 | BITS_PER_BYTE)); | ||
| 172 | } | ||
| 173 | 40 | netlink_put_register(nle, NFTNL_EXPR_HASH_DREG, dreg); | |
| 174 | 40 | nftnl_expr_set_u32(nle, NFTNL_EXPR_HASH_MODULUS, expr->hash.mod); | |
| 175 |
2/2✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→13) taken 32 times.
|
40 | if (expr->hash.seed_set) |
| 176 | 8 | nftnl_expr_set_u32(nle, NFTNL_EXPR_HASH_SEED, expr->hash.seed); | |
| 177 | 40 | nftnl_expr_set_u32(nle, NFTNL_EXPR_HASH_OFFSET, expr->hash.offset); | |
| 178 | 40 | nftnl_expr_set_u32(nle, NFTNL_EXPR_HASH_TYPE, expr->hash.type); | |
| 179 | 40 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 180 | 40 | } | |
| 181 | |||
| 182 | static struct nftnl_expr * | ||
| 183 | − | __netlink_gen_payload(const struct expr *expr, enum nft_registers dreg) | |
| 184 | { | ||
| 185 | struct nftnl_expr *nle; | ||
| 186 | |||
| 187 | − | nle = alloc_nft_expr("payload"); | |
| 188 | − | netlink_put_register(nle, NFTNL_EXPR_PAYLOAD_DREG, dreg); | |
| 189 | − | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_BASE, | |
| 190 | − | expr->payload.base - 1); | |
| 191 | − | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_OFFSET, | |
| 192 | − | expr->payload.offset / BITS_PER_BYTE); | |
| 193 | − | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_LEN, | |
| 194 | − | div_round_up(expr->len, BITS_PER_BYTE)); | |
| 195 | |||
| 196 | − | return nle; | |
| 197 | } | ||
| 198 | |||
| 199 | static struct nftnl_expr * | ||
| 200 | − | __netlink_gen_meta(const struct expr *expr, enum nft_registers dreg) | |
| 201 | { | ||
| 202 | struct nftnl_expr *nle; | ||
| 203 | |||
| 204 | − | nle = alloc_nft_expr("meta"); | |
| 205 | − | netlink_put_register(nle, NFTNL_EXPR_META_DREG, dreg); | |
| 206 | − | nftnl_expr_set_u32(nle, NFTNL_EXPR_META_KEY, expr->meta.key); | |
| 207 | |||
| 208 | − | return nle; | |
| 209 | } | ||
| 210 | |||
| 211 | 784 | static struct nftnl_expr *netlink_gen_inner_expr(const struct expr *expr, | |
| 212 | enum nft_registers dreg) | ||
| 213 | { | ||
| 214 | 784 | struct expr *_expr = (struct expr *)expr; | |
| 215 | struct nftnl_expr *nle; | ||
| 216 | |||
| 217 |
2/3✓ Branch 0 (2→3) taken 481 times.
✓ Branch 1 (2→6) taken 303 times.
✗ Branch 2 (2→8) not taken.
|
784 | switch (expr->etype) { |
| 218 | 481 | case EXPR_PAYLOAD: | |
| 219 |
2/2✓ Branch 0 (3→4) taken 20 times.
✓ Branch 1 (3→5) taken 461 times.
|
481 | if (expr->payload.base == NFT_PAYLOAD_INNER_HEADER + 1) |
| 220 | 20 | _expr->payload.base = NFT_PAYLOAD_TUN_HEADER + 1; | |
| 221 | |||
| 222 | 481 | nle = __netlink_gen_payload(expr, dreg); | |
| 223 | 481 | break; | |
| 224 | 303 | case EXPR_META: | |
| 225 | 303 | nle = __netlink_gen_meta(expr, dreg); | |
| 226 | 303 | break; | |
| 227 | ✗ | default: | |
| 228 | ✗ | assert(0); | |
| 229 | break; | ||
| 230 | } | ||
| 231 | |||
| 232 | 784 | return nle; | |
| 233 | } | ||
| 234 | |||
| 235 | 784 | static void netlink_gen_inner(struct netlink_linearize_ctx *ctx, | |
| 236 | const struct expr *expr, | ||
| 237 | enum nft_registers dreg, | ||
| 238 | const struct proto_desc *desc) | ||
| 239 | { | ||
| 240 | struct nftnl_expr *nle; | ||
| 241 | |||
| 242 | 784 | nle = alloc_nft_expr("inner"); | |
| 243 | 784 | nftnl_expr_set_u32(nle, NFTNL_EXPR_INNER_HDRSIZE, desc->inner.hdrsize); | |
| 244 | 784 | nftnl_expr_set_u32(nle, NFTNL_EXPR_INNER_FLAGS, desc->inner.flags); | |
| 245 | 784 | nftnl_expr_set_u32(nle, NFTNL_EXPR_INNER_TYPE, desc->inner.type); | |
| 246 | 784 | nftnl_expr_set(nle, NFTNL_EXPR_INNER_EXPR, netlink_gen_inner_expr(expr, dreg), 0); | |
| 247 | 784 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 248 | 784 | } | |
| 249 | |||
| 250 | 16056 | static void netlink_gen_payload(struct netlink_linearize_ctx *ctx, | |
| 251 | const struct expr *expr, | ||
| 252 | enum nft_registers dreg) | ||
| 253 | { | ||
| 254 | struct nftnl_expr *nle; | ||
| 255 | |||
| 256 |
2/2✓ Branch 0 (2→3) taken 481 times.
✓ Branch 1 (2→5) taken 15575 times.
|
16056 | if (expr->payload.inner_desc) { |
| 257 | 481 | netlink_gen_inner(ctx, expr, dreg, expr->payload.inner_desc); | |
| 258 | 481 | return; | |
| 259 | } | ||
| 260 | |||
| 261 | 15575 | nle = __netlink_gen_payload(expr, dreg); | |
| 262 | 15575 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 263 | } | ||
| 264 | |||
| 265 | 1306 | static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx, | |
| 266 | const struct expr *expr, | ||
| 267 | enum nft_registers dreg) | ||
| 268 | { | ||
| 269 | 1306 | unsigned int offset = expr->exthdr.offset; | |
| 270 | struct nftnl_expr *nle; | ||
| 271 | |||
| 272 | 1306 | nle = alloc_nft_expr("exthdr"); | |
| 273 | 1306 | netlink_put_register(nle, NFTNL_EXPR_EXTHDR_DREG, dreg); | |
| 274 | 1306 | nftnl_expr_set_u8(nle, NFTNL_EXPR_EXTHDR_TYPE, | |
| 275 | 1306 | expr->exthdr.raw_type); | |
| 276 | 1306 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OFFSET, offset / BITS_PER_BYTE); | |
| 277 | 1306 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_LEN, | |
| 278 | 1306 | div_round_up(expr->len, BITS_PER_BYTE)); | |
| 279 | 1306 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OP, expr->exthdr.op); | |
| 280 | 1306 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_FLAGS, expr->exthdr.flags); | |
| 281 | 1306 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 282 | 1306 | } | |
| 283 | |||
| 284 | 95931 | static void netlink_gen_meta(struct netlink_linearize_ctx *ctx, | |
| 285 | const struct expr *expr, | ||
| 286 | enum nft_registers dreg) | ||
| 287 | { | ||
| 288 | struct nftnl_expr *nle; | ||
| 289 | |||
| 290 |
2/2✓ Branch 0 (2→3) taken 303 times.
✓ Branch 1 (2→5) taken 95628 times.
|
95931 | if (expr->meta.inner_desc) { |
| 291 | 303 | netlink_gen_inner(ctx, expr, dreg, expr->meta.inner_desc); | |
| 292 | 303 | return; | |
| 293 | } | ||
| 294 | |||
| 295 | 95628 | nle = __netlink_gen_meta(expr, dreg); | |
| 296 | 95628 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 297 | } | ||
| 298 | |||
| 299 | 35 | static void netlink_gen_rt(struct netlink_linearize_ctx *ctx, | |
| 300 | const struct expr *expr, | ||
| 301 | enum nft_registers dreg) | ||
| 302 | { | ||
| 303 | struct nftnl_expr *nle; | ||
| 304 | |||
| 305 | 35 | nle = alloc_nft_expr("rt"); | |
| 306 | 35 | netlink_put_register(nle, NFTNL_EXPR_RT_DREG, dreg); | |
| 307 | 35 | nftnl_expr_set_u32(nle, NFTNL_EXPR_RT_KEY, expr->rt.key); | |
| 308 | 35 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 309 | 35 | } | |
| 310 | |||
| 311 | 30 | static void netlink_gen_socket(struct netlink_linearize_ctx *ctx, | |
| 312 | const struct expr *expr, | ||
| 313 | enum nft_registers dreg) | ||
| 314 | { | ||
| 315 | struct nftnl_expr *nle; | ||
| 316 | |||
| 317 | 30 | nle = alloc_nft_expr("socket"); | |
| 318 | 30 | netlink_put_register(nle, NFTNL_EXPR_SOCKET_DREG, dreg); | |
| 319 | 30 | nftnl_expr_set_u32(nle, NFTNL_EXPR_SOCKET_KEY, expr->socket.key); | |
| 320 | 30 | nftnl_expr_set_u32(nle, NFTNL_EXPR_SOCKET_LEVEL, expr->socket.level); | |
| 321 | 30 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 322 | 30 | } | |
| 323 | |||
| 324 | 57 | static void netlink_gen_osf(struct netlink_linearize_ctx *ctx, | |
| 325 | const struct expr *expr, | ||
| 326 | enum nft_registers dreg) | ||
| 327 | { | ||
| 328 | struct nftnl_expr *nle; | ||
| 329 | |||
| 330 | 57 | nle = alloc_nft_expr("osf"); | |
| 331 | 57 | netlink_put_register(nle, NFTNL_EXPR_OSF_DREG, dreg); | |
| 332 | 57 | nftnl_expr_set_u8(nle, NFTNL_EXPR_OSF_TTL, expr->osf.ttl); | |
| 333 | 57 | nftnl_expr_set_u32(nle, NFTNL_EXPR_OSF_FLAGS, expr->osf.flags); | |
| 334 | 57 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 335 | 57 | } | |
| 336 | |||
| 337 | 6 | static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx, | |
| 338 | const struct expr *expr, | ||
| 339 | enum nft_registers dreg) | ||
| 340 | { | ||
| 341 | struct nftnl_expr *nle; | ||
| 342 | |||
| 343 | 6 | nle = alloc_nft_expr("tunnel"); | |
| 344 | 6 | netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg); | |
| 345 | 6 | nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key); | |
| 346 | 6 | nftnl_rule_add_expr(ctx->nlr, nle); | |
| 347 | 6 | } | |
| 348 | |||
| 349 | 56 | static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx, | |
| 350 | const struct expr *expr, | ||
| 351 | enum nft_registers dreg) | ||
| 352 | { | ||
| 353 | struct nftnl_expr *nle; | ||
| 354 | |||
| 355 | 56 | nle = alloc_nft_expr("numgen"); | |
| 356 | 56 | netlink_put_register(nle, NFTNL_EXPR_NG_DREG, dreg); | |
| 357 | 56 | nftnl_expr_set_u32(nle, NFTNL_EXPR_NG_TYPE, expr->numgen.type); | |
| 358 | 56 | nftnl_expr_set_u32(nle, NFTNL_EXPR_NG_MODULUS, expr->numgen.mod); | |
| 359 | 56 | nftnl_expr_set_u32(nle, NFTNL_EXPR_NG_OFFSET, expr->numgen.offset); | |
| 360 | 56 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 361 | 56 | } | |
| 362 | |||
| 363 | 960 | static void netlink_gen_ct(struct netlink_linearize_ctx *ctx, | |
| 364 | const struct expr *expr, | ||
| 365 | enum nft_registers dreg) | ||
| 366 | { | ||
| 367 | struct nftnl_expr *nle; | ||
| 368 | |||
| 369 | 960 | nle = alloc_nft_expr("ct"); | |
| 370 | 960 | netlink_put_register(nle, NFTNL_EXPR_CT_DREG, dreg); | |
| 371 | 960 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CT_KEY, expr->ct.key); | |
| 372 |
2/2✓ Branch 0 (5→6) taken 74 times.
✓ Branch 1 (5→7) taken 886 times.
|
960 | if (expr->ct.direction >= 0) |
| 373 | 74 | nftnl_expr_set_u8(nle, NFTNL_EXPR_CT_DIR, | |
| 374 | 74 | expr->ct.direction); | |
| 375 | |||
| 376 | 960 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 377 | 960 | } | |
| 378 | |||
| 379 | 1296 | static void netlink_gen_map(struct netlink_linearize_ctx *ctx, | |
| 380 | const struct expr *expr, | ||
| 381 | enum nft_registers dreg) | ||
| 382 | { | ||
| 383 | struct nftnl_expr *nle; | ||
| 384 | enum nft_registers sreg; | ||
| 385 | 1296 | int regspace = 0; | |
| 386 | |||
| 387 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1296 times.
|
1296 | assert(expr->mappings->etype == EXPR_SET_REF); |
| 388 | |||
| 389 |
2/2✓ Branch 0 (4→5) taken 790 times.
✓ Branch 1 (4→6) taken 506 times.
|
1296 | if (dreg == NFT_REG_VERDICT) |
| 390 | 790 | sreg = get_register(ctx, expr->map); | |
| 391 | else | ||
| 392 | 506 | sreg = dreg; | |
| 393 | |||
| 394 | /* suppress assert in netlink_gen_expr */ | ||
| 395 |
2/2✓ Branch 0 (7→8) taken 306 times.
✓ Branch 1 (7→10) taken 990 times.
|
1296 | if (expr->map->etype == EXPR_CONCAT) { |
| 396 | 306 | regspace = netlink_register_space(expr->map->len); | |
| 397 | 306 | ctx->reg_low += regspace; | |
| 398 | } | ||
| 399 | |||
| 400 | 1296 | netlink_gen_expr(ctx, expr->map, sreg); | |
| 401 | 1296 | ctx->reg_low -= regspace; | |
| 402 | |||
| 403 | 1296 | nle = alloc_nft_expr("lookup"); | |
| 404 | 1296 | netlink_put_register(nle, NFTNL_EXPR_LOOKUP_SREG, sreg); | |
| 405 | 1296 | netlink_put_register(nle, NFTNL_EXPR_LOOKUP_DREG, dreg); | |
| 406 | 1296 | nftnl_expr_set_str(nle, NFTNL_EXPR_LOOKUP_SET, | |
| 407 | 1296 | expr->mappings->set->handle.set.name); | |
| 408 | 1296 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOOKUP_SET_ID, | |
| 409 | 1296 | expr->mappings->set->handle.set_id); | |
| 410 | |||
| 411 |
2/2✓ Branch 0 (16→17) taken 790 times.
✓ Branch 1 (16→18) taken 506 times.
|
1296 | if (dreg == NFT_REG_VERDICT) |
| 412 | 790 | release_register(ctx, expr->map); | |
| 413 | |||
| 414 | 1296 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 415 | 1296 | } | |
| 416 | |||
| 417 | 4830 | static void netlink_gen_lookup(struct netlink_linearize_ctx *ctx, | |
| 418 | const struct expr *expr, | ||
| 419 | enum nft_registers dreg) | ||
| 420 | { | ||
| 421 | struct nftnl_expr *nle; | ||
| 422 | enum nft_registers sreg; | ||
| 423 | |||
| 424 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4830 times.
|
4830 | assert(expr->right->etype == EXPR_SET_REF); |
| 425 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 4830 times.
|
4830 | assert(dreg == NFT_REG_VERDICT); |
| 426 | |||
| 427 | 4830 | sreg = get_register(ctx, expr->left); | |
| 428 | 4830 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 429 | |||
| 430 | 4830 | nle = alloc_nft_expr("lookup"); | |
| 431 | 4830 | netlink_put_register(nle, NFTNL_EXPR_LOOKUP_SREG, sreg); | |
| 432 | 4830 | nftnl_expr_set_str(nle, NFTNL_EXPR_LOOKUP_SET, | |
| 433 | 4830 | expr->right->set->handle.set.name); | |
| 434 | 4830 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOOKUP_SET_ID, | |
| 435 | 4830 | expr->right->set->handle.set_id); | |
| 436 |
2/2✓ Branch 0 (12→13) taken 1069 times.
✓ Branch 1 (12→14) taken 3761 times.
|
4830 | if (expr->op == OP_NEQ) |
| 437 | 1069 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOOKUP_FLAGS, NFT_LOOKUP_F_INV); | |
| 438 | |||
| 439 | 4830 | release_register(ctx, expr->left); | |
| 440 | 4830 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 441 | 4830 | } | |
| 442 | |||
| 443 | 103853 | static enum nft_cmp_ops netlink_gen_cmp_op(enum ops op) | |
| 444 | { | ||
| 445 |
5/7✓ Branch 0 (2→3) taken 101577 times.
✓ Branch 1 (2→4) taken 2024 times.
✓ Branch 2 (2→5) taken 100 times.
✓ Branch 3 (2→6) taken 144 times.
✗ Branch 4 (2→7) not taken.
✓ Branch 5 (2→8) taken 8 times.
✗ Branch 6 (2→9) not taken.
|
103853 | switch (op) { |
| 446 | 101577 | case OP_EQ: | |
| 447 | case OP_IMPLICIT: | ||
| 448 | 101577 | return NFT_CMP_EQ; | |
| 449 | 2024 | case OP_NEQ: | |
| 450 | 2024 | return NFT_CMP_NEQ; | |
| 451 | 100 | case OP_LT: | |
| 452 | 100 | return NFT_CMP_LT; | |
| 453 | 144 | case OP_GT: | |
| 454 | 144 | return NFT_CMP_GT; | |
| 455 | ✗ | case OP_LTE: | |
| 456 | ✗ | return NFT_CMP_LTE; | |
| 457 | 8 | case OP_GTE: | |
| 458 | 8 | return NFT_CMP_GTE; | |
| 459 | ✗ | default: | |
| 460 | ✗ | BUG("invalid comparison operation %u", op); | |
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | 35 | static struct expr *netlink_gen_prefix(struct netlink_linearize_ctx *ctx, | |
| 465 | const struct expr *expr, | ||
| 466 | enum nft_registers sreg) | ||
| 467 | { | ||
| 468 | 35 | struct nft_data_linearize nld, zero = {}; | |
| 469 | struct nftnl_expr *nle; | ||
| 470 | mpz_t mask; | ||
| 471 | |||
| 472 | 35 | mpz_init(mask); | |
| 473 | 35 | mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len); | |
| 474 | 35 | netlink_gen_raw_data(mask, expr->right->byteorder, | |
| 475 | 35 | div_round_up(expr->right->len, BITS_PER_BYTE), | |
| 476 | &nld); | ||
| 477 | 35 | mpz_clear(mask); | |
| 478 | |||
| 479 | 35 | zero.len = nld.len; | |
| 480 | |||
| 481 | 35 | nle = alloc_nft_expr("bitwise"); | |
| 482 | 35 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, sreg); | |
| 483 | 35 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, sreg); | |
| 484 | 35 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, nld.len); | |
| 485 | 35 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_MASK, nld.value, nld.len); | |
| 486 | 35 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_XOR, zero.value, zero.len); | |
| 487 | 35 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 488 | |||
| 489 | 35 | return expr->right->prefix; | |
| 490 | } | ||
| 491 | |||
| 492 | 1477 | static void netlink_gen_range(struct netlink_linearize_ctx *ctx, | |
| 493 | const struct expr *expr, | ||
| 494 | enum nft_registers dreg) | ||
| 495 | { | ||
| 496 | 1477 | struct expr *range = expr->right; | |
| 497 | struct nftnl_expr *nle; | ||
| 498 | enum nft_registers sreg; | ||
| 499 | struct nft_data_linearize nld; | ||
| 500 | |||
| 501 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1477 times.
|
1477 | assert(dreg == NFT_REG_VERDICT); |
| 502 | |||
| 503 | 1477 | sreg = get_register(ctx, expr->left); | |
| 504 | 1477 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 505 | |||
| 506 |
1/2✓ Branch 0 (6→7) taken 1477 times.
✗ Branch 1 (6→16) not taken.
|
1477 | switch (expr->op) { |
| 507 | 1477 | case OP_NEQ: | |
| 508 | case OP_EQ: | ||
| 509 | case OP_IMPLICIT: | ||
| 510 | 1477 | nle = alloc_nft_expr("range"); | |
| 511 | 1477 | netlink_put_register(nle, NFTNL_EXPR_RANGE_SREG, sreg); | |
| 512 | 1477 | nftnl_expr_set_u32(nle, NFTNL_EXPR_RANGE_OP, netlink_gen_cmp_op(expr->op)); | |
| 513 | 1477 | netlink_gen_data(range->left, &nld); | |
| 514 | 1477 | nftnl_expr_set(nle, NFTNL_EXPR_RANGE_FROM_DATA, | |
| 515 | nld.value, nld.len); | ||
| 516 | 1477 | netlink_gen_data(range->right, &nld); | |
| 517 | 1477 | nftnl_expr_set(nle, NFTNL_EXPR_RANGE_TO_DATA, | |
| 518 | nld.value, nld.len); | ||
| 519 | 1477 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 520 | 1477 | break; | |
| 521 | ✗ | default: | |
| 522 | ✗ | BUG("invalid range operation %u", expr->op); | |
| 523 | |||
| 524 | } | ||
| 525 | |||
| 526 | 1477 | release_register(ctx, expr->left); | |
| 527 | 1477 | } | |
| 528 | |||
| 529 | 426 | static void netlink_gen_flagcmp(struct netlink_linearize_ctx *ctx, | |
| 530 | const struct expr *expr, | ||
| 531 | enum nft_registers dreg) | ||
| 532 | { | ||
| 533 | struct nftnl_expr *nle; | ||
| 534 | struct nft_data_linearize nld, nld2; | ||
| 535 | enum nft_registers sreg; | ||
| 536 | unsigned int len; | ||
| 537 | mpz_t zero; | ||
| 538 | |||
| 539 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 426 times.
|
426 | assert(dreg == NFT_REG_VERDICT); |
| 540 | |||
| 541 | 426 | sreg = get_register(ctx, expr->left); | |
| 542 | 426 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 543 | 426 | len = div_round_up(expr->left->len, BITS_PER_BYTE); | |
| 544 | |||
| 545 | 426 | mpz_init_set_ui(zero, 0); | |
| 546 | |||
| 547 | 426 | netlink_gen_raw_data(zero, expr->right->byteorder, len, &nld); | |
| 548 | 426 | netlink_gen_data(expr->right, &nld2); | |
| 549 | |||
| 550 |
2/2✓ Branch 0 (9→10) taken 10 times.
✓ Branch 1 (9→15) taken 416 times.
|
426 | if (expr->left->etype == EXPR_BINOP) { |
| 551 | 10 | nle = alloc_nft_expr("cmp"); | |
| 552 | 10 | netlink_put_register(nle, NFTNL_EXPR_CMP_SREG, sreg); | |
| 553 | 10 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CMP_OP, NFT_CMP_EQ); | |
| 554 | 10 | nftnl_expr_set(nle, NFTNL_EXPR_CMP_DATA, nld2.value, nld2.len); | |
| 555 | 10 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 556 | } else { | ||
| 557 | 416 | nle = alloc_nft_expr("bitwise"); | |
| 558 | 416 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, sreg); | |
| 559 | 416 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, sreg); | |
| 560 | 416 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, len); | |
| 561 | 416 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_MASK, nld2.value, nld2.len); | |
| 562 | 416 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_XOR, nld.value, nld.len); | |
| 563 | 416 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 564 | |||
| 565 | 416 | nle = alloc_nft_expr("cmp"); | |
| 566 | 416 | netlink_put_register(nle, NFTNL_EXPR_CMP_SREG, sreg); | |
| 567 |
2/2✓ Branch 0 (24→25) taken 36 times.
✓ Branch 1 (24→26) taken 380 times.
|
416 | if (expr->op == OP_NEG) |
| 568 | 36 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CMP_OP, NFT_CMP_EQ); | |
| 569 | else | ||
| 570 | 380 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CMP_OP, NFT_CMP_NEQ); | |
| 571 | |||
| 572 | 416 | nftnl_expr_set(nle, NFTNL_EXPR_CMP_DATA, nld.value, nld.len); | |
| 573 | 416 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 574 | } | ||
| 575 | |||
| 576 | 426 | mpz_clear(zero); | |
| 577 | 426 | release_register(ctx, expr->left); | |
| 578 | 426 | } | |
| 579 | |||
| 580 | 109109 | static void netlink_gen_relational(struct netlink_linearize_ctx *ctx, | |
| 581 | const struct expr *expr, | ||
| 582 | enum nft_registers dreg) | ||
| 583 | { | ||
| 584 | struct nft_data_linearize nld; | ||
| 585 | struct nftnl_expr *nle; | ||
| 586 | enum nft_registers sreg; | ||
| 587 | struct expr *right; | ||
| 588 | int len; | ||
| 589 | |||
| 590 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 109109 times.
|
109109 | assert(dreg == NFT_REG_VERDICT); |
| 591 | |||
| 592 |
1/2✓ Branch 0 (4→5) taken 109109 times.
✗ Branch 1 (4→6) not taken.
|
109109 | switch (expr->op) { |
| 593 | 109109 | case OP_IMPLICIT: | |
| 594 | case OP_EQ: | ||
| 595 | case OP_NEQ: | ||
| 596 | case OP_LT: | ||
| 597 | case OP_GT: | ||
| 598 | case OP_LTE: | ||
| 599 | case OP_GTE: | ||
| 600 | case OP_NEG: | ||
| 601 | 109109 | break; | |
| 602 | ✗ | default: | |
| 603 | ✗ | BUG("invalid relational operation %u", expr->op); | |
| 604 | } | ||
| 605 | |||
| 606 |
4/5✓ Branch 0 (8→9) taken 1477 times.
✓ Branch 1 (8→10) taken 4830 times.
✗ Branch 2 (8→11) not taken.
✓ Branch 3 (8→12) taken 239 times.
✓ Branch 4 (8→22) taken 102563 times.
|
109109 | switch (expr->right->etype) { |
| 607 | 1477 | case EXPR_RANGE: | |
| 608 | 6733 | return netlink_gen_range(ctx, expr, dreg); | |
| 609 | 4830 | case EXPR_SET: | |
| 610 | case EXPR_SET_REF: | ||
| 611 | 4830 | return netlink_gen_lookup(ctx, expr, dreg); | |
| 612 | ✗ | case EXPR_LIST: | |
| 613 | ✗ | return netlink_gen_flagcmp(ctx, expr, dreg); | |
| 614 | 239 | case EXPR_PREFIX: | |
| 615 | 239 | sreg = get_register(ctx, expr->left); | |
| 616 |
2/2✓ Branch 0 (14→15) taken 196 times.
✓ Branch 1 (14→20) taken 43 times.
|
239 | if (expr_basetype(expr->left)->type != TYPE_STRING && |
| 617 |
2/2✓ Branch 0 (15→16) taken 189 times.
✓ Branch 1 (15→18) taken 7 times.
|
196 | (expr->right->byteorder != BYTEORDER_BIG_ENDIAN || |
| 618 |
2/2✓ Branch 0 (16→17) taken 187 times.
✓ Branch 1 (16→18) taken 2 times.
|
189 | !expr->right->prefix_len || |
| 619 |
2/2✓ Branch 0 (17→18) taken 26 times.
✓ Branch 1 (17→20) taken 161 times.
|
187 | expr->right->prefix_len % BITS_PER_BYTE)) { |
| 620 | 35 | len = div_round_up(expr->right->len, BITS_PER_BYTE); | |
| 621 | 35 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 622 | 35 | right = netlink_gen_prefix(ctx, expr, sreg); | |
| 623 | } else { | ||
| 624 | 204 | len = div_round_up(expr->right->prefix_len, BITS_PER_BYTE); | |
| 625 | 204 | right = expr->right->prefix; | |
| 626 | 204 | expr->left->len = expr->right->prefix_len; | |
| 627 | 204 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 628 | } | ||
| 629 | 239 | break; | |
| 630 | 102563 | default: | |
| 631 |
4/4✓ Branch 0 (22→23) taken 57393 times.
✓ Branch 1 (22→24) taken 45170 times.
✓ Branch 2 (23→24) taken 36 times.
✓ Branch 3 (23→28) taken 57357 times.
|
102563 | if ((expr->op == OP_IMPLICIT || expr->op == OP_NEG) && |
| 632 |
2/2✓ Branch 0 (24→25) taken 44342 times.
✓ Branch 1 (24→28) taken 864 times.
|
45206 | expr->right->dtype->basetype != NULL && |
| 633 |
2/2✓ Branch 0 (25→26) taken 426 times.
✓ Branch 1 (25→28) taken 43916 times.
|
44342 | expr->right->dtype->basetype->type == TYPE_BITMASK) |
| 634 | 426 | return netlink_gen_flagcmp(ctx, expr, dreg); | |
| 635 | |||
| 636 | 102137 | sreg = get_register(ctx, expr->left); | |
| 637 | 102137 | len = div_round_up(expr->right->len, BITS_PER_BYTE); | |
| 638 | 102137 | right = expr->right; | |
| 639 | 102137 | netlink_gen_expr(ctx, expr->left, sreg); | |
| 640 | 102137 | break; | |
| 641 | } | ||
| 642 | |||
| 643 | 102376 | nle = alloc_nft_expr("cmp"); | |
| 644 | 102376 | netlink_put_register(nle, NFTNL_EXPR_CMP_SREG, sreg); | |
| 645 | 102376 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CMP_OP, | |
| 646 | 102376 | netlink_gen_cmp_op(expr->op)); | |
| 647 | 102376 | netlink_gen_data(right, &nld); | |
| 648 | 102376 | nftnl_expr_set(nle, NFTNL_EXPR_CMP_DATA, nld.value, len); | |
| 649 | 102376 | release_register(ctx, expr->left); | |
| 650 | |||
| 651 | 102376 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 652 | } | ||
| 653 | |||
| 654 | 3463 | static void combine_binop(mpz_t mask, mpz_t xor, const mpz_t m, const mpz_t x) | |
| 655 | { | ||
| 656 | /* xor = x ^ (xor & m) */ | ||
| 657 | 3463 | mpz_and(xor, xor, m); | |
| 658 | 3463 | mpz_xor(xor, x, xor); | |
| 659 | /* mask &= m */ | ||
| 660 | 3463 | mpz_and(mask, mask, m); | |
| 661 | 3463 | } | |
| 662 | |||
| 663 | 225 | static void netlink_gen_bitwise_shift(struct netlink_linearize_ctx *ctx, | |
| 664 | const struct expr *expr, | ||
| 665 | enum nft_registers dreg) | ||
| 666 | { | ||
| 667 | 225 | enum nft_bitwise_ops op = expr->op == OP_LSHIFT ? | |
| 668 |
2/2✓ Branch 0 (2→3) taken 62 times.
✓ Branch 1 (2→4) taken 163 times.
|
225 | NFT_BITWISE_LSHIFT : NFT_BITWISE_RSHIFT; |
| 669 | 225 | unsigned int len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 670 | struct nft_data_linearize nld; | ||
| 671 | struct nftnl_expr *nle; | ||
| 672 | |||
| 673 | 225 | netlink_gen_expr(ctx, expr->left, dreg); | |
| 674 | |||
| 675 | 225 | nle = alloc_nft_expr("bitwise"); | |
| 676 | 225 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, dreg); | |
| 677 | 225 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, dreg); | |
| 678 | 225 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_OP, op); | |
| 679 | 225 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, len); | |
| 680 | |||
| 681 | 225 | netlink_gen_raw_data(expr->right->value, expr->right->byteorder, | |
| 682 | sizeof(uint32_t), &nld); | ||
| 683 | |||
| 684 | 225 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_DATA, nld.value, | |
| 685 | nld.len); | ||
| 686 | |||
| 687 | 225 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 688 | 225 | } | |
| 689 | |||
| 690 | 2903 | static void netlink_gen_bitwise_mask_xor(struct netlink_linearize_ctx *ctx, | |
| 691 | const struct expr *expr, | ||
| 692 | enum nft_registers dreg) | ||
| 693 | { | ||
| 694 | struct expr *binops[NFT_MAX_EXPR_RECURSION]; | ||
| 695 | struct nftnl_expr *nle; | ||
| 696 | struct nft_data_linearize nld; | ||
| 697 | struct expr *left, *i; | ||
| 698 | mpz_t mask, xor, val, tmp; | ||
| 699 | unsigned int len; | ||
| 700 | 2903 | int n = 0; | |
| 701 | |||
| 702 | 2903 | mpz_init(mask); | |
| 703 | 2903 | mpz_init(xor); | |
| 704 | 2903 | mpz_init(val); | |
| 705 | 2903 | mpz_init(tmp); | |
| 706 | |||
| 707 | 2903 | binops[n++] = left = (struct expr *) expr; | |
| 708 |
5/6✓ Branch 0 (11→12) taken 3565 times.
✓ Branch 1 (11→18) taken 2801 times.
✓ Branch 2 (12→13) taken 3565 times.
✗ Branch 3 (12→18) not taken.
✓ Branch 4 (14→15) taken 3551 times.
✓ Branch 5 (14→18) taken 14 times.
|
6366 | while (left->etype == EXPR_BINOP && left->left != NULL && expr_is_constant(left->right) && |
| 709 |
6/6✓ Branch 0 (15→7) taken 3068 times.
✓ Branch 1 (15→16) taken 483 times.
✓ Branch 2 (16→7) taken 236 times.
✓ Branch 3 (16→17) taken 247 times.
✓ Branch 4 (17→7) taken 159 times.
✓ Branch 5 (17→18) taken 88 times.
|
3551 | (left->op == OP_AND || left->op == OP_OR || left->op == OP_XOR)) { |
| 710 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 3463 times.
|
3463 | if (n == array_size(binops)) |
| 711 | ✗ | BUG("NFT_MAX_EXPR_RECURSION limit reached"); | |
| 712 | 3463 | binops[n++] = left = left->left; | |
| 713 | } | ||
| 714 | |||
| 715 | 2903 | netlink_gen_expr(ctx, binops[--n], dreg); | |
| 716 | |||
| 717 | 2903 | mpz_bitmask(mask, expr->len); | |
| 718 | 2903 | mpz_set_ui(xor, 0); | |
| 719 |
2/2✓ Branch 0 (36→22) taken 3463 times.
✓ Branch 1 (36→37) taken 2903 times.
|
6366 | while (n > 0) { |
| 720 | 3463 | i = binops[--n]; | |
| 721 | 3463 | mpz_set(val, i->right->value); | |
| 722 | |||
| 723 |
3/4✓ Branch 0 (23→24) taken 3068 times.
✓ Branch 1 (23→26) taken 236 times.
✓ Branch 2 (23→29) taken 159 times.
✗ Branch 3 (23→32) not taken.
|
3463 | switch (i->op) { |
| 724 | 3068 | case OP_AND: | |
| 725 | 3068 | mpz_set_ui(tmp, 0); | |
| 726 | 3068 | combine_binop(mask, xor, val, tmp); | |
| 727 | 3068 | break; | |
| 728 | 236 | case OP_OR: | |
| 729 | 236 | mpz_com(tmp, val); | |
| 730 | 236 | combine_binop(mask, xor, tmp, val); | |
| 731 | 236 | break; | |
| 732 | 159 | case OP_XOR: | |
| 733 | 159 | mpz_bitmask(tmp, expr->len); | |
| 734 | 159 | combine_binop(mask, xor, tmp, val); | |
| 735 | 159 | break; | |
| 736 | ✗ | default: | |
| 737 | ✗ | BUG("invalid binary operation %u", i->op); | |
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | 2903 | len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 742 | |||
| 743 | 2903 | nle = alloc_nft_expr("bitwise"); | |
| 744 | 2903 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, dreg); | |
| 745 | 2903 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, dreg); | |
| 746 | 2903 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_OP, NFT_BITWISE_MASK_XOR); | |
| 747 | 2903 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, len); | |
| 748 | |||
| 749 | 2903 | netlink_gen_raw_data(mask, expr->byteorder, len, &nld); | |
| 750 | 2903 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_MASK, nld.value, nld.len); | |
| 751 | 2903 | netlink_gen_raw_data(xor, expr->byteorder, len, &nld); | |
| 752 | 2903 | nftnl_expr_set(nle, NFTNL_EXPR_BITWISE_XOR, nld.value, nld.len); | |
| 753 | |||
| 754 | 2903 | mpz_clear(tmp); | |
| 755 | 2903 | mpz_clear(val); | |
| 756 | 2903 | mpz_clear(xor); | |
| 757 | 2903 | mpz_clear(mask); | |
| 758 | |||
| 759 | 2903 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 760 | 2903 | } | |
| 761 | |||
| 762 | 79 | static void netlink_gen_bitwise_bool(struct netlink_linearize_ctx *ctx, | |
| 763 | const struct expr *expr, | ||
| 764 | enum nft_registers dreg) | ||
| 765 | { | ||
| 766 | enum nft_registers sreg2; | ||
| 767 | struct nftnl_expr *nle; | ||
| 768 | unsigned int len; | ||
| 769 | |||
| 770 | 79 | nle = alloc_nft_expr("bitwise"); | |
| 771 | |||
| 772 |
3/4✓ Branch 0 (3→4) taken 6 times.
✓ Branch 1 (3→5) taken 2 times.
✓ Branch 2 (3→7) taken 71 times.
✗ Branch 3 (3→9) not taken.
|
79 | switch (expr->op) { |
| 773 | 6 | case OP_XOR: | |
| 774 | 6 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_OP, NFT_BITWISE_XOR); | |
| 775 | 6 | break; | |
| 776 | 2 | case OP_AND: | |
| 777 | 2 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_OP, NFT_BITWISE_AND); | |
| 778 | 2 | break; | |
| 779 | 71 | case OP_OR: | |
| 780 | 71 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_OP, NFT_BITWISE_OR); | |
| 781 | 71 | break; | |
| 782 | ✗ | default: | |
| 783 | ✗ | BUG("invalid binary operation %u", expr->op); | |
| 784 | } | ||
| 785 | |||
| 786 | 79 | netlink_gen_expr(ctx, expr->left, dreg); | |
| 787 | 79 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG, dreg); | |
| 788 | 79 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_DREG, dreg); | |
| 789 | |||
| 790 | 79 | sreg2 = get_register(ctx, expr->right); | |
| 791 | 79 | netlink_gen_expr(ctx, expr->right, sreg2); | |
| 792 | 79 | netlink_put_register(nle, NFTNL_EXPR_BITWISE_SREG2, sreg2); | |
| 793 | 79 | release_register(ctx, expr->right); | |
| 794 | |||
| 795 | 79 | len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 796 | 79 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BITWISE_LEN, len); | |
| 797 | |||
| 798 | 79 | nftnl_rule_add_expr(ctx->nlr, nle); | |
| 799 | 79 | } | |
| 800 | |||
| 801 | 3207 | static void netlink_gen_binop(struct netlink_linearize_ctx *ctx, | |
| 802 | const struct expr *expr, | ||
| 803 | enum nft_registers dreg) | ||
| 804 | { | ||
| 805 |
2/2✓ Branch 0 (2→3) taken 225 times.
✓ Branch 1 (2→5) taken 2982 times.
|
3207 | switch(expr->op) { |
| 806 | 225 | case OP_LSHIFT: | |
| 807 | case OP_RSHIFT: | ||
| 808 | 225 | netlink_gen_bitwise_shift(ctx, expr, dreg); | |
| 809 | 225 | break; | |
| 810 | 2982 | default: | |
| 811 |
2/2✓ Branch 0 (6→7) taken 2903 times.
✓ Branch 1 (6→8) taken 79 times.
|
2982 | if (expr_is_constant(expr->right)) |
| 812 | 2903 | netlink_gen_bitwise_mask_xor(ctx, expr, dreg); | |
| 813 | else | ||
| 814 | 79 | netlink_gen_bitwise_bool(ctx, expr, dreg); | |
| 815 | 2982 | break; | |
| 816 | } | ||
| 817 | 3207 | } | |
| 818 | |||
| 819 | 948 | static enum nft_byteorder_ops netlink_gen_unary_op(enum ops op) | |
| 820 | { | ||
| 821 |
2/3✓ Branch 0 (2→3) taken 888 times.
✓ Branch 1 (2→4) taken 60 times.
✗ Branch 2 (2→5) not taken.
|
948 | switch (op) { |
| 822 | 888 | case OP_HTON: | |
| 823 | 888 | return NFT_BYTEORDER_HTON; | |
| 824 | 60 | case OP_NTOH: | |
| 825 | 60 | return NFT_BYTEORDER_NTOH; | |
| 826 | ✗ | default: | |
| 827 | ✗ | BUG("invalid unary operation %u", op); | |
| 828 | } | ||
| 829 | } | ||
| 830 | |||
| 831 | 948 | static void netlink_gen_unary(struct netlink_linearize_ctx *ctx, | |
| 832 | const struct expr *expr, | ||
| 833 | enum nft_registers dreg) | ||
| 834 | { | ||
| 835 | struct nftnl_expr *nle; | ||
| 836 | int byte_size; | ||
| 837 | |||
| 838 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 948 times.
|
948 | assert(div_round_up(expr->arg->len, BITS_PER_BYTE) != 1); |
| 839 | |||
| 840 |
2/2✓ Branch 0 (4→5) taken 110 times.
✓ Branch 1 (4→6) taken 838 times.
|
948 | if ((expr->arg->len % 64) == 0) |
| 841 | 110 | byte_size = 8; | |
| 842 |
2/2✓ Branch 0 (6→7) taken 770 times.
✓ Branch 1 (6→8) taken 68 times.
|
838 | else if ((expr->arg->len % 32) == 0) |
| 843 | 770 | byte_size = 4; | |
| 844 | else | ||
| 845 | 68 | byte_size = 2; | |
| 846 | |||
| 847 | 948 | netlink_gen_expr(ctx, expr->arg, dreg); | |
| 848 | |||
| 849 | 948 | nle = alloc_nft_expr("byteorder"); | |
| 850 | 948 | netlink_put_register(nle, NFTNL_EXPR_BYTEORDER_SREG, dreg); | |
| 851 | 948 | netlink_put_register(nle, NFTNL_EXPR_BYTEORDER_DREG, dreg); | |
| 852 | 948 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BYTEORDER_LEN, | |
| 853 | 948 | div_round_up(expr->len, BITS_PER_BYTE)); | |
| 854 | 948 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BYTEORDER_SIZE, | |
| 855 | byte_size); | ||
| 856 | 948 | nftnl_expr_set_u32(nle, NFTNL_EXPR_BYTEORDER_OP, | |
| 857 | 948 | netlink_gen_unary_op(expr->op)); | |
| 858 | 948 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 859 | 948 | } | |
| 860 | |||
| 861 | 15078 | static void netlink_gen_immediate(struct netlink_linearize_ctx *ctx, | |
| 862 | const struct expr *expr, | ||
| 863 | enum nft_registers dreg) | ||
| 864 | { | ||
| 865 | 15078 | const struct location *loc = &expr->location; | |
| 866 | struct nft_data_linearize nld; | ||
| 867 | struct nftnl_expr *nle; | ||
| 868 | |||
| 869 | 15078 | nle = alloc_nft_expr("immediate"); | |
| 870 | 15078 | netlink_put_register(nle, NFTNL_EXPR_IMM_DREG, dreg); | |
| 871 | 15078 | netlink_gen_data(expr, &nld); | |
| 872 |
2/3✓ Branch 0 (5→6) taken 1029 times.
✓ Branch 1 (5→8) taken 14049 times.
✗ Branch 2 (5→15) not taken.
|
15078 | switch (expr->etype) { |
| 873 | 1029 | case EXPR_VALUE: | |
| 874 | 1029 | nftnl_expr_set(nle, NFTNL_EXPR_IMM_DATA, nld.value, nld.len); | |
| 875 | 1029 | break; | |
| 876 | 14049 | case EXPR_VERDICT: | |
| 877 |
2/2✓ Branch 0 (8→9) taken 2595 times.
✓ Branch 1 (8→11) taken 11454 times.
|
14049 | if (expr->chain) { |
| 878 | 2595 | nftnl_expr_set_str(nle, NFTNL_EXPR_IMM_CHAIN, | |
| 879 | nld.chain); | ||
| 880 | 2595 | loc = &expr->chain->location; | |
| 881 |
2/2✓ Branch 0 (11→12) taken 62 times.
✓ Branch 1 (11→13) taken 11392 times.
|
11454 | } else if (expr->chain_id) { |
| 882 | 62 | nftnl_expr_set_u32(nle, NFTNL_EXPR_IMM_CHAIN_ID, | |
| 883 | nld.chain_id); | ||
| 884 | } | ||
| 885 | 14049 | nftnl_expr_set_u32(nle, NFTNL_EXPR_IMM_VERDICT, nld.verdict); | |
| 886 | 14049 | break; | |
| 887 | ✗ | default: | |
| 888 | ✗ | break; | |
| 889 | } | ||
| 890 | 15078 | nft_rule_add_expr(ctx, nle, loc); | |
| 891 | 15078 | } | |
| 892 | |||
| 893 | 71 | static void netlink_gen_xfrm(struct netlink_linearize_ctx *ctx, | |
| 894 | const struct expr *expr, | ||
| 895 | enum nft_registers dreg) | ||
| 896 | { | ||
| 897 | struct nftnl_expr *nle; | ||
| 898 | |||
| 899 | 71 | nle = alloc_nft_expr("xfrm"); | |
| 900 | 71 | netlink_put_register(nle, NFTNL_EXPR_XFRM_DREG, dreg); | |
| 901 | 71 | nftnl_expr_set_u32(nle, NFTNL_EXPR_XFRM_KEY, expr->xfrm.key); | |
| 902 | 71 | nftnl_expr_set_u8(nle, NFTNL_EXPR_XFRM_DIR, expr->xfrm.direction); | |
| 903 | 71 | nftnl_expr_set_u32(nle, NFTNL_EXPR_XFRM_SPNUM, expr->xfrm.spnum); | |
| 904 | 71 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 905 | 71 | } | |
| 906 | |||
| 907 | 246213 | static void netlink_gen_expr(struct netlink_linearize_ctx *ctx, | |
| 908 | const struct expr *expr, | ||
| 909 | enum nft_registers dreg) | ||
| 910 | { | ||
| 911 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 246213 times.
|
246213 | assert(dreg < ctx->reg_low); |
| 912 | |||
| 913 |
18/20✓ Branch 0 (4→5) taken 15078 times.
✓ Branch 1 (4→6) taken 948 times.
✓ Branch 2 (4→7) taken 3207 times.
✓ Branch 3 (4→8) taken 109109 times.
✓ Branch 4 (4→9) taken 1963 times.
✓ Branch 5 (4→10) taken 1296 times.
✓ Branch 6 (4→11) taken 16056 times.
✓ Branch 7 (4→12) taken 1306 times.
✓ Branch 8 (4→13) taken 95931 times.
✓ Branch 9 (4→14) taken 35 times.
✓ Branch 10 (4→15) taken 960 times.
✗ Branch 11 (4→16) not taken.
✓ Branch 12 (4→17) taken 56 times.
✓ Branch 13 (4→18) taken 40 times.
✓ Branch 14 (4→19) taken 64 times.
✓ Branch 15 (4→20) taken 30 times.
✓ Branch 16 (4→21) taken 6 times.
✓ Branch 17 (4→22) taken 57 times.
✓ Branch 18 (4→23) taken 71 times.
✗ Branch 19 (4→24) not taken.
|
246213 | switch (expr->etype) { |
| 914 | 15078 | case EXPR_VERDICT: | |
| 915 | case EXPR_VALUE: | ||
| 916 | 15078 | return netlink_gen_immediate(ctx, expr, dreg); | |
| 917 | 948 | case EXPR_UNARY: | |
| 918 | 948 | return netlink_gen_unary(ctx, expr, dreg); | |
| 919 | 3207 | case EXPR_BINOP: | |
| 920 | 3207 | return netlink_gen_binop(ctx, expr, dreg); | |
| 921 | 109109 | case EXPR_RELATIONAL: | |
| 922 | 109109 | return netlink_gen_relational(ctx, expr, dreg); | |
| 923 | 1963 | case EXPR_CONCAT: | |
| 924 | 1963 | return netlink_gen_concat(ctx, expr, dreg); | |
| 925 | 1296 | case EXPR_MAP: | |
| 926 | 1296 | return netlink_gen_map(ctx, expr, dreg); | |
| 927 | 16056 | case EXPR_PAYLOAD: | |
| 928 | 16056 | return netlink_gen_payload(ctx, expr, dreg); | |
| 929 | 1306 | case EXPR_EXTHDR: | |
| 930 | 1306 | return netlink_gen_exthdr(ctx, expr, dreg); | |
| 931 | 95931 | case EXPR_META: | |
| 932 | 95931 | return netlink_gen_meta(ctx, expr, dreg); | |
| 933 | 35 | case EXPR_RT: | |
| 934 | 35 | return netlink_gen_rt(ctx, expr, dreg); | |
| 935 | 960 | case EXPR_CT: | |
| 936 | 960 | return netlink_gen_ct(ctx, expr, dreg); | |
| 937 | ✗ | case EXPR_SET_ELEM: | |
| 938 | ✗ | return netlink_gen_expr(ctx, expr->key, dreg); | |
| 939 | 56 | case EXPR_NUMGEN: | |
| 940 | 56 | return netlink_gen_numgen(ctx, expr, dreg); | |
| 941 | 40 | case EXPR_HASH: | |
| 942 | 40 | return netlink_gen_hash(ctx, expr, dreg); | |
| 943 | 64 | case EXPR_FIB: | |
| 944 | 64 | return netlink_gen_fib(ctx, expr, dreg); | |
| 945 | 30 | case EXPR_SOCKET: | |
| 946 | 30 | return netlink_gen_socket(ctx, expr, dreg); | |
| 947 | 6 | case EXPR_TUNNEL: | |
| 948 | 6 | return netlink_gen_tunnel(ctx, expr, dreg); | |
| 949 | 57 | case EXPR_OSF: | |
| 950 | 57 | return netlink_gen_osf(ctx, expr, dreg); | |
| 951 | 71 | case EXPR_XFRM: | |
| 952 | 71 | return netlink_gen_xfrm(ctx, expr, dreg); | |
| 953 | ✗ | default: | |
| 954 | ✗ | BUG("unknown expression type %s", expr_name(expr)); | |
| 955 | } | ||
| 956 | } | ||
| 957 | |||
| 958 | 315 | static void netlink_gen_objref_stmt(struct netlink_linearize_ctx *ctx, | |
| 959 | const struct stmt *stmt) | ||
| 960 | { | ||
| 961 | 315 | struct expr *expr = stmt->objref.expr; | |
| 962 | struct nft_data_linearize nld; | ||
| 963 | struct nftnl_expr *nle; | ||
| 964 | uint32_t sreg_key; | ||
| 965 | |||
| 966 | 315 | nle = alloc_nft_expr("objref"); | |
| 967 |
2/3✓ Branch 0 (3→4) taken 95 times.
✓ Branch 1 (3→10) taken 220 times.
✗ Branch 2 (3→14) not taken.
|
315 | switch (expr->etype) { |
| 968 | 95 | case EXPR_MAP: | |
| 969 | 95 | sreg_key = get_register(ctx, expr->map); | |
| 970 | 95 | netlink_gen_expr(ctx, expr->map, sreg_key); | |
| 971 | 95 | release_register(ctx, expr->map); | |
| 972 | |||
| 973 | 95 | nftnl_expr_set_u32(nle, NFTNL_EXPR_OBJREF_SET_SREG, sreg_key); | |
| 974 | 95 | nftnl_expr_set_str(nle, NFTNL_EXPR_OBJREF_SET_NAME, | |
| 975 | 95 | expr->mappings->set->handle.set.name); | |
| 976 | 95 | nftnl_expr_set_u32(nle, NFTNL_EXPR_OBJREF_SET_ID, | |
| 977 | 95 | expr->mappings->set->handle.set_id); | |
| 978 | 95 | break; | |
| 979 | 220 | case EXPR_VALUE: | |
| 980 | 220 | netlink_gen_data(stmt->objref.expr, &nld); | |
| 981 | 220 | nftnl_expr_set(nle, NFTNL_EXPR_OBJREF_IMM_NAME, | |
| 982 | nld.value, nld.len); | ||
| 983 | 220 | nftnl_expr_set_u32(nle, NFTNL_EXPR_OBJREF_IMM_TYPE, | |
| 984 | 220 | stmt->objref.type); | |
| 985 | 220 | break; | |
| 986 | ✗ | default: | |
| 987 | ✗ | BUG("unsupported expression %u", expr->etype); | |
| 988 | } | ||
| 989 | 315 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 990 | 315 | } | |
| 991 | |||
| 992 | 28 | static struct nftnl_expr *netlink_gen_connlimit_stmt(const struct stmt *stmt) | |
| 993 | { | ||
| 994 | struct nftnl_expr *nle; | ||
| 995 | |||
| 996 | 28 | nle = alloc_nft_expr("connlimit"); | |
| 997 | 28 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CONNLIMIT_COUNT, | |
| 998 | 28 | stmt->connlimit.count); | |
| 999 | 28 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CONNLIMIT_FLAGS, | |
| 1000 | 28 | stmt->connlimit.flags); | |
| 1001 | |||
| 1002 | 28 | return nle; | |
| 1003 | } | ||
| 1004 | |||
| 1005 | 2176 | static struct nftnl_expr *netlink_gen_counter_stmt(const struct stmt *stmt) | |
| 1006 | { | ||
| 1007 | struct nftnl_expr *nle; | ||
| 1008 | |||
| 1009 | 2176 | nle = alloc_nft_expr("counter"); | |
| 1010 |
2/2✓ Branch 0 (3→4) taken 103 times.
✓ Branch 1 (3→5) taken 2073 times.
|
2176 | if (stmt->counter.packets) { |
| 1011 | 103 | nftnl_expr_set_u64(nle, NFTNL_EXPR_CTR_PACKETS, | |
| 1012 | 103 | stmt->counter.packets); | |
| 1013 | } | ||
| 1014 |
2/2✓ Branch 0 (5→6) taken 103 times.
✓ Branch 1 (5→7) taken 2073 times.
|
2176 | if (stmt->counter.bytes) { |
| 1015 | 103 | nftnl_expr_set_u64(nle, NFTNL_EXPR_CTR_BYTES, | |
| 1016 | 103 | stmt->counter.bytes); | |
| 1017 | } | ||
| 1018 | |||
| 1019 | 2176 | return nle; | |
| 1020 | } | ||
| 1021 | |||
| 1022 | 701 | static struct nftnl_expr *netlink_gen_limit_stmt(const struct stmt *stmt) | |
| 1023 | { | ||
| 1024 | struct nftnl_expr *nle; | ||
| 1025 | |||
| 1026 | 701 | nle = alloc_nft_expr("limit"); | |
| 1027 | 701 | nftnl_expr_set_u64(nle, NFTNL_EXPR_LIMIT_RATE, stmt->limit.rate); | |
| 1028 | 701 | nftnl_expr_set_u64(nle, NFTNL_EXPR_LIMIT_UNIT, stmt->limit.unit); | |
| 1029 | 701 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LIMIT_TYPE, stmt->limit.type); | |
| 1030 |
2/2✓ Branch 0 (6→7) taken 490 times.
✓ Branch 1 (6→8) taken 211 times.
|
701 | if (stmt->limit.burst > 0) |
| 1031 | 490 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LIMIT_BURST, | |
| 1032 | 490 | stmt->limit.burst); | |
| 1033 | 701 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LIMIT_FLAGS, stmt->limit.flags); | |
| 1034 | |||
| 1035 | 701 | return nle; | |
| 1036 | } | ||
| 1037 | |||
| 1038 | 211 | static struct nftnl_expr *netlink_gen_quota_stmt(const struct stmt *stmt) | |
| 1039 | { | ||
| 1040 | struct nftnl_expr *nle; | ||
| 1041 | |||
| 1042 | 211 | nle = alloc_nft_expr("quota"); | |
| 1043 | 211 | nftnl_expr_set_u64(nle, NFTNL_EXPR_QUOTA_BYTES, stmt->quota.bytes); | |
| 1044 | 211 | nftnl_expr_set_u64(nle, NFTNL_EXPR_QUOTA_CONSUMED, stmt->quota.used); | |
| 1045 | 211 | nftnl_expr_set_u32(nle, NFTNL_EXPR_QUOTA_FLAGS, stmt->quota.flags); | |
| 1046 | |||
| 1047 | 211 | return nle; | |
| 1048 | } | ||
| 1049 | |||
| 1050 | 41 | static struct nftnl_expr *netlink_gen_last_stmt(const struct stmt *stmt) | |
| 1051 | { | ||
| 1052 | struct nftnl_expr *nle; | ||
| 1053 | |||
| 1054 | 41 | nle = alloc_nft_expr("last"); | |
| 1055 | 41 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LAST_SET, stmt->last.set); | |
| 1056 | 41 | nftnl_expr_set_u64(nle, NFTNL_EXPR_LAST_MSECS, stmt->last.used); | |
| 1057 | |||
| 1058 | 41 | return nle; | |
| 1059 | } | ||
| 1060 | |||
| 1061 | 3157 | struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt) | |
| 1062 | { | ||
| 1063 |
5/6✓ Branch 0 (2→3) taken 28 times.
✓ Branch 1 (2→4) taken 2176 times.
✓ Branch 2 (2→5) taken 701 times.
✓ Branch 3 (2→6) taken 211 times.
✓ Branch 4 (2→7) taken 41 times.
✗ Branch 5 (2→8) not taken.
|
3157 | switch (stmt->type) { |
| 1064 | 28 | case STMT_CONNLIMIT: | |
| 1065 | 28 | return netlink_gen_connlimit_stmt(stmt); | |
| 1066 | 2176 | case STMT_COUNTER: | |
| 1067 | 2176 | return netlink_gen_counter_stmt(stmt); | |
| 1068 | 701 | case STMT_LIMIT: | |
| 1069 | 701 | return netlink_gen_limit_stmt(stmt); | |
| 1070 | 211 | case STMT_QUOTA: | |
| 1071 | 211 | return netlink_gen_quota_stmt(stmt); | |
| 1072 | 41 | case STMT_LAST: | |
| 1073 | 41 | return netlink_gen_last_stmt(stmt); | |
| 1074 | ✗ | default: | |
| 1075 | ✗ | BUG("unknown stateful statement type %d", stmt->type); | |
| 1076 | } | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | 14777 | static void netlink_gen_verdict_stmt(struct netlink_linearize_ctx *ctx, | |
| 1080 | const struct stmt *stmt) | ||
| 1081 | { | ||
| 1082 | 14777 | return netlink_gen_expr(ctx, stmt->expr, NFT_REG_VERDICT); | |
| 1083 | } | ||
| 1084 | |||
| 1085 | 268 | static bool payload_needs_l4csum_update_pseudohdr(const struct expr *expr, | |
| 1086 | const struct proto_desc *desc) | ||
| 1087 | { | ||
| 1088 | int i; | ||
| 1089 | |||
| 1090 |
2/2✓ Branch 0 (7→3) taken 4722 times.
✓ Branch 1 (7→8) taken 232 times.
|
4954 | for (i = 0; i < PROTO_HDRS_MAX; i++) { |
| 1091 |
2/2✓ Branch 0 (4→5) taken 36 times.
✓ Branch 1 (4→6) taken 4686 times.
|
4722 | if (payload_hdr_field(expr) == desc->pseudohdr[i]) |
| 1092 | 36 | return true; | |
| 1093 | } | ||
| 1094 | 232 | return false; | |
| 1095 | } | ||
| 1096 | |||
| 1097 | 12 | static void netlink_gen_exthdr_stmt(struct netlink_linearize_ctx *ctx, | |
| 1098 | const struct stmt *stmt) | ||
| 1099 | { | ||
| 1100 | struct nftnl_expr *nle; | ||
| 1101 | const struct expr *expr; | ||
| 1102 | enum nft_registers sreg; | ||
| 1103 | unsigned int offset; | ||
| 1104 | |||
| 1105 | 12 | sreg = get_register(ctx, stmt->exthdr.val); | |
| 1106 | 12 | netlink_gen_expr(ctx, stmt->exthdr.val, sreg); | |
| 1107 | 12 | release_register(ctx, stmt->exthdr.val); | |
| 1108 | |||
| 1109 | 12 | expr = stmt->exthdr.expr; | |
| 1110 | |||
| 1111 | 12 | offset = expr->exthdr.offset; | |
| 1112 | |||
| 1113 | 12 | nle = alloc_nft_expr("exthdr"); | |
| 1114 | 12 | netlink_put_register(nle, NFTNL_EXPR_EXTHDR_SREG, sreg); | |
| 1115 | 12 | nftnl_expr_set_u8(nle, NFTNL_EXPR_EXTHDR_TYPE, | |
| 1116 | 12 | expr->exthdr.raw_type); | |
| 1117 | 12 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OFFSET, offset / BITS_PER_BYTE); | |
| 1118 | 12 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_LEN, | |
| 1119 | 12 | div_round_up(expr->len, BITS_PER_BYTE)); | |
| 1120 | 12 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OP, expr->exthdr.op); | |
| 1121 | 12 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 1122 | 12 | } | |
| 1123 | |||
| 1124 | 419 | static void netlink_gen_payload_stmt(struct netlink_linearize_ctx *ctx, | |
| 1125 | const struct stmt *stmt) | ||
| 1126 | { | ||
| 1127 | struct nftnl_expr *nle; | ||
| 1128 | const struct proto_desc *desc; | ||
| 1129 | const struct expr *expr; | ||
| 1130 | enum nft_registers sreg; | ||
| 1131 | unsigned int csum_off; | ||
| 1132 | |||
| 1133 | 419 | sreg = get_register(ctx, stmt->payload.val); | |
| 1134 | 419 | netlink_gen_expr(ctx, stmt->payload.val, sreg); | |
| 1135 | 419 | release_register(ctx, stmt->payload.val); | |
| 1136 | |||
| 1137 | 419 | expr = stmt->payload.expr; | |
| 1138 | |||
| 1139 | 419 | csum_off = 0; | |
| 1140 | 419 | desc = expr->payload.desc; | |
| 1141 |
3/4✓ Branch 0 (5→6) taken 419 times.
✗ Branch 1 (5→8) not taken.
✓ Branch 2 (6→7) taken 251 times.
✓ Branch 3 (6→8) taken 168 times.
|
419 | if (desc != NULL && desc->checksum_key) |
| 1142 | 251 | csum_off = desc->templates[desc->checksum_key].offset; | |
| 1143 | |||
| 1144 | 419 | nle = alloc_nft_expr("payload"); | |
| 1145 | 419 | netlink_put_register(nle, NFTNL_EXPR_PAYLOAD_SREG, sreg); | |
| 1146 | 419 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_BASE, | |
| 1147 | 419 | expr->payload.base - 1); | |
| 1148 | 419 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_OFFSET, | |
| 1149 | 419 | expr->payload.offset / BITS_PER_BYTE); | |
| 1150 | 419 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_LEN, | |
| 1151 | 419 | expr->len / BITS_PER_BYTE); | |
| 1152 |
2/2✓ Branch 0 (13→14) taken 251 times.
✓ Branch 1 (13→16) taken 168 times.
|
419 | if (csum_off) { |
| 1153 | 251 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_CSUM_TYPE, | |
| 1154 | 251 | desc->checksum_type); | |
| 1155 | 251 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_CSUM_OFFSET, | |
| 1156 | 251 | csum_off / BITS_PER_BYTE); | |
| 1157 | } | ||
| 1158 |
5/6✓ Branch 0 (16→17) taken 268 times.
✓ Branch 1 (16→20) taken 151 times.
✓ Branch 2 (17→18) taken 268 times.
✗ Branch 3 (17→20) not taken.
✓ Branch 4 (19→20) taken 232 times.
✓ Branch 5 (19→24) taken 36 times.
|
687 | if ((expr->payload.base == PROTO_BASE_NETWORK_HDR && desc && |
| 1159 | 268 | payload_needs_l4csum_update_pseudohdr(expr, desc)) || | |
| 1160 |
5/6✓ Branch 0 (20→21) taken 89 times.
✓ Branch 1 (20→23) taken 294 times.
✓ Branch 2 (21→22) taken 89 times.
✗ Branch 3 (21→23) not taken.
✓ Branch 4 (22→23) taken 33 times.
✓ Branch 5 (22→24) taken 56 times.
|
383 | (expr->payload.base == PROTO_BASE_TRANSPORT_HDR && desc && |
| 1161 | 327 | desc == &proto_udp) || | |
| 1162 |
2/2✓ Branch 0 (23→24) taken 45 times.
✓ Branch 1 (23→25) taken 282 times.
|
327 | expr->payload.base == PROTO_BASE_INNER_HDR) |
| 1163 | 137 | nftnl_expr_set_u32(nle, NFTNL_EXPR_PAYLOAD_FLAGS, | |
| 1164 | NFT_PAYLOAD_L4CSUM_PSEUDOHDR); | ||
| 1165 | |||
| 1166 | 419 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 1167 | 419 | } | |
| 1168 | |||
| 1169 | 512 | static void netlink_gen_meta_stmt(struct netlink_linearize_ctx *ctx, | |
| 1170 | const struct stmt *stmt) | ||
| 1171 | { | ||
| 1172 | struct nftnl_expr *nle; | ||
| 1173 | enum nft_registers sreg; | ||
| 1174 | |||
| 1175 | 512 | sreg = get_register(ctx, stmt->meta.expr); | |
| 1176 | 512 | netlink_gen_expr(ctx, stmt->meta.expr, sreg); | |
| 1177 | 512 | release_register(ctx, stmt->meta.expr); | |
| 1178 | |||
| 1179 | 512 | nle = alloc_nft_expr("meta"); | |
| 1180 | 512 | netlink_put_register(nle, NFTNL_EXPR_META_SREG, sreg); | |
| 1181 | 512 | nftnl_expr_set_u32(nle, NFTNL_EXPR_META_KEY, stmt->meta.key); | |
| 1182 | 512 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1183 | 512 | } | |
| 1184 | |||
| 1185 | 256 | static void netlink_gen_log_stmt(struct netlink_linearize_ctx *ctx, | |
| 1186 | const struct stmt *stmt) | ||
| 1187 | { | ||
| 1188 | struct nftnl_expr *nle; | ||
| 1189 | |||
| 1190 | 256 | nle = alloc_nft_expr("log"); | |
| 1191 |
2/2✓ Branch 0 (3→4) taken 50 times.
✓ Branch 1 (3→5) taken 206 times.
|
256 | if (stmt->log.prefix != NULL) |
| 1192 | 50 | nftnl_expr_set_str(nle, NFTNL_EXPR_LOG_PREFIX, stmt->log.prefix); | |
| 1193 | |||
| 1194 |
2/2✓ Branch 0 (5→6) taken 50 times.
✓ Branch 1 (5→11) taken 206 times.
|
256 | if (stmt->log.flags & STMT_LOG_GROUP) { |
| 1195 | 50 | nftnl_expr_set_u16(nle, NFTNL_EXPR_LOG_GROUP, stmt->log.group); | |
| 1196 |
2/2✓ Branch 0 (7→8) taken 25 times.
✓ Branch 1 (7→9) taken 25 times.
|
50 | if (stmt->log.flags & STMT_LOG_SNAPLEN) |
| 1197 | 25 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOG_SNAPLEN, | |
| 1198 | 25 | stmt->log.snaplen); | |
| 1199 |
2/2✓ Branch 0 (9→10) taken 10 times.
✓ Branch 1 (9→15) taken 40 times.
|
50 | if (stmt->log.flags & STMT_LOG_QTHRESHOLD) |
| 1200 | 10 | nftnl_expr_set_u16(nle, NFTNL_EXPR_LOG_QTHRESHOLD, | |
| 1201 | 10 | stmt->log.qthreshold); | |
| 1202 | } else { | ||
| 1203 |
2/2✓ Branch 0 (11→12) taken 101 times.
✓ Branch 1 (11→13) taken 105 times.
|
206 | if (stmt->log.flags & STMT_LOG_LEVEL) |
| 1204 | 101 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOG_LEVEL, | |
| 1205 | 101 | stmt->log.level); | |
| 1206 |
2/2✓ Branch 0 (13→14) taken 62 times.
✓ Branch 1 (13→15) taken 144 times.
|
206 | if (stmt->log.logflags) |
| 1207 | 62 | nftnl_expr_set_u32(nle, NFTNL_EXPR_LOG_FLAGS, | |
| 1208 | 62 | stmt->log.logflags); | |
| 1209 | } | ||
| 1210 | 256 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1211 | 256 | } | |
| 1212 | |||
| 1213 | 300 | static void netlink_gen_reject_stmt(struct netlink_linearize_ctx *ctx, | |
| 1214 | const struct stmt *stmt) | ||
| 1215 | { | ||
| 1216 | struct nftnl_expr *nle; | ||
| 1217 | |||
| 1218 | 300 | nle = alloc_nft_expr("reject"); | |
| 1219 | 300 | nftnl_expr_set_u32(nle, NFTNL_EXPR_REJECT_TYPE, stmt->reject.type); | |
| 1220 |
1/2✓ Branch 0 (4→5) taken 300 times.
✗ Branch 1 (4→6) not taken.
|
300 | if (stmt->reject.icmp_code != -1) |
| 1221 | 300 | nftnl_expr_set_u8(nle, NFTNL_EXPR_REJECT_CODE, | |
| 1222 | 300 | stmt->reject.icmp_code); | |
| 1223 | |||
| 1224 | 300 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1225 | 300 | } | |
| 1226 | |||
| 1227 | 154 | static unsigned int nat_addrlen(uint8_t family) | |
| 1228 | { | ||
| 1229 |
2/3✓ Branch 0 (2→3) taken 126 times.
✓ Branch 1 (2→4) taken 28 times.
✗ Branch 2 (2→5) not taken.
|
154 | switch (family) { |
| 1230 | 126 | case NFPROTO_IPV4: return 32; | |
| 1231 | 28 | case NFPROTO_IPV6: return 128; | |
| 1232 | } | ||
| 1233 | |||
| 1234 | ✗ | BUG("invalid nat family %u", family); | |
| 1235 | return 0; | ||
| 1236 | } | ||
| 1237 | |||
| 1238 | 628 | static void netlink_gen_nat_stmt(struct netlink_linearize_ctx *ctx, | |
| 1239 | const struct stmt *stmt) | ||
| 1240 | { | ||
| 1241 | struct nftnl_expr *nle; | ||
| 1242 | enum nft_registers amin_reg, amax_reg; | ||
| 1243 | enum nft_registers pmin_reg, pmax_reg; | ||
| 1244 | 628 | uint8_t family = 0; | |
| 1245 | 628 | int registers = 0; | |
| 1246 | int nftnl_flag_attr; | ||
| 1247 | int nftnl_reg_pmin, nftnl_reg_pmax; | ||
| 1248 | |||
| 1249 |
3/4✓ Branch 0 (2→3) taken 362 times.
✓ Branch 1 (2→7) taken 118 times.
✓ Branch 2 (2→9) taken 148 times.
✗ Branch 3 (2→11) not taken.
|
628 | switch (stmt->nat.type) { |
| 1250 | 362 | case NFT_NAT_SNAT: | |
| 1251 | case NFT_NAT_DNAT: | ||
| 1252 | 362 | nle = alloc_nft_expr("nat"); | |
| 1253 | 362 | nftnl_expr_set_u32(nle, NFTNL_EXPR_NAT_TYPE, stmt->nat.type); | |
| 1254 | |||
| 1255 | 362 | family = stmt->nat.family; | |
| 1256 | 362 | nftnl_expr_set_u32(nle, NFTNL_EXPR_NAT_FAMILY, family); | |
| 1257 | |||
| 1258 | 362 | nftnl_flag_attr = NFTNL_EXPR_NAT_FLAGS; | |
| 1259 | 362 | nftnl_reg_pmin = NFTNL_EXPR_NAT_REG_PROTO_MIN; | |
| 1260 | 362 | nftnl_reg_pmax = NFTNL_EXPR_NAT_REG_PROTO_MAX; | |
| 1261 | 362 | break; | |
| 1262 | 118 | case NFT_NAT_MASQ: | |
| 1263 | 118 | nle = alloc_nft_expr("masq"); | |
| 1264 | |||
| 1265 | 118 | nftnl_flag_attr = NFTNL_EXPR_MASQ_FLAGS; | |
| 1266 | 118 | nftnl_reg_pmin = NFTNL_EXPR_MASQ_REG_PROTO_MIN; | |
| 1267 | 118 | nftnl_reg_pmax = NFTNL_EXPR_MASQ_REG_PROTO_MAX; | |
| 1268 | 118 | break; | |
| 1269 | 148 | case NFT_NAT_REDIR: | |
| 1270 | 148 | nle = alloc_nft_expr("redir"); | |
| 1271 | |||
| 1272 | 148 | nftnl_flag_attr = NFTNL_EXPR_REDIR_FLAGS; | |
| 1273 | 148 | nftnl_reg_pmin = NFTNL_EXPR_REDIR_REG_PROTO_MIN; | |
| 1274 | 148 | nftnl_reg_pmax = NFTNL_EXPR_REDIR_REG_PROTO_MAX; | |
| 1275 | 148 | break; | |
| 1276 | ✗ | default: | |
| 1277 | ✗ | BUG("unknown nat type %d", stmt->nat.type); | |
| 1278 | break; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 |
2/2✓ Branch 0 (13→14) taken 278 times.
✓ Branch 1 (13→15) taken 350 times.
|
628 | if (stmt->nat.flags != 0) |
| 1282 | 278 | nftnl_expr_set_u32(nle, nftnl_flag_attr, stmt->nat.flags); | |
| 1283 | |||
| 1284 |
2/2✓ Branch 0 (15→16) taken 359 times.
✓ Branch 1 (15→44) taken 269 times.
|
628 | if (stmt->nat.addr) { |
| 1285 | 359 | amin_reg = get_register(ctx, NULL); | |
| 1286 | 359 | registers++; | |
| 1287 | |||
| 1288 |
2/2✓ Branch 0 (17→18) taken 28 times.
✓ Branch 1 (17→23) taken 331 times.
|
359 | if (stmt->nat.addr->etype == EXPR_RANGE) { |
| 1289 | 28 | amax_reg = get_register(ctx, NULL); | |
| 1290 | 28 | registers++; | |
| 1291 | |||
| 1292 | 28 | netlink_gen_expr(ctx, stmt->nat.addr->left, amin_reg); | |
| 1293 | 28 | netlink_gen_expr(ctx, stmt->nat.addr->right, amax_reg); | |
| 1294 | 28 | netlink_put_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MIN, | |
| 1295 | amin_reg); | ||
| 1296 | 28 | netlink_put_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MAX, | |
| 1297 | amax_reg); | ||
| 1298 | } else { | ||
| 1299 | 331 | netlink_gen_expr(ctx, stmt->nat.addr, amin_reg); | |
| 1300 | 331 | netlink_put_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MIN, | |
| 1301 | amin_reg); | ||
| 1302 |
2/2✓ Branch 0 (25→26) taken 203 times.
✓ Branch 1 (25→32) taken 128 times.
|
331 | if (stmt->nat.addr->etype == EXPR_MAP && |
| 1303 |
2/2✓ Branch 0 (26→27) taken 48 times.
✓ Branch 1 (26→32) taken 155 times.
|
203 | stmt->nat.addr->mappings->set->data->flags & EXPR_F_INTERVAL) { |
| 1304 | 48 | amin_reg += netlink_register_space(nat_addrlen(family)); | |
| 1305 |
2/2✓ Branch 0 (29→30) taken 18 times.
✓ Branch 1 (29→31) taken 30 times.
|
48 | if (stmt->nat.type_flags & STMT_NAT_F_CONCAT) { |
| 1306 | 18 | netlink_put_register(nle, nftnl_reg_pmin, | |
| 1307 | amin_reg); | ||
| 1308 | } else { | ||
| 1309 | 30 | netlink_put_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MAX, | |
| 1310 | amin_reg); | ||
| 1311 | } | ||
| 1312 | } | ||
| 1313 | } | ||
| 1314 | |||
| 1315 |
2/2✓ Branch 0 (32→33) taken 88 times.
✓ Branch 1 (32→44) taken 271 times.
|
359 | if (stmt->nat.type_flags & STMT_NAT_F_CONCAT) { |
| 1316 | /* nat_stmt evaluation step doesn't allow | ||
| 1317 | * STMT_NAT_F_CONCAT && stmt->nat.proto. | ||
| 1318 | */ | ||
| 1319 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 88 times.
|
88 | assert(stmt->nat.proto == NULL); |
| 1320 | |||
| 1321 | 88 | pmin_reg = amin_reg; | |
| 1322 | |||
| 1323 |
2/2✓ Branch 0 (35→36) taken 18 times.
✓ Branch 1 (35→39) taken 70 times.
|
88 | if (stmt->nat.type_flags & STMT_NAT_F_INTERVAL) { |
| 1324 | 18 | pmin_reg += netlink_register_space(nat_addrlen(family)); | |
| 1325 | 18 | netlink_put_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MAX, | |
| 1326 | pmin_reg); | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | /* if STMT_NAT_F_CONCAT is set, the mapped type is a | ||
| 1330 | * concatenation of 'addr . inet_service'. | ||
| 1331 | * The map lookup will then return the | ||
| 1332 | * concatenated value, so we need to skip | ||
| 1333 | * the address and use the register that | ||
| 1334 | * will hold the inet_service part. | ||
| 1335 | */ | ||
| 1336 | 88 | pmin_reg += netlink_register_space(nat_addrlen(family)); | |
| 1337 |
2/2✓ Branch 0 (41→42) taken 18 times.
✓ Branch 1 (41→43) taken 70 times.
|
88 | if (stmt->nat.type_flags & STMT_NAT_F_INTERVAL) |
| 1338 | 18 | netlink_put_register(nle, nftnl_reg_pmax, pmin_reg); | |
| 1339 | else | ||
| 1340 | 70 | netlink_put_register(nle, nftnl_reg_pmin, pmin_reg); | |
| 1341 | } | ||
| 1342 | } | ||
| 1343 | |||
| 1344 |
2/2✓ Branch 0 (44→45) taken 158 times.
✓ Branch 1 (44→54) taken 470 times.
|
628 | if (stmt->nat.proto) { |
| 1345 | 158 | pmin_reg = get_register(ctx, NULL); | |
| 1346 | 158 | registers++; | |
| 1347 | |||
| 1348 |
2/2✓ Branch 0 (46→47) taken 18 times.
✓ Branch 1 (46→52) taken 140 times.
|
158 | if (stmt->nat.proto->etype == EXPR_RANGE) { |
| 1349 | 18 | pmax_reg = get_register(ctx, NULL); | |
| 1350 | 18 | registers++; | |
| 1351 | |||
| 1352 | 18 | netlink_gen_expr(ctx, stmt->nat.proto->left, pmin_reg); | |
| 1353 | 18 | netlink_gen_expr(ctx, stmt->nat.proto->right, pmax_reg); | |
| 1354 | 18 | netlink_put_register(nle, nftnl_reg_pmin, pmin_reg); | |
| 1355 | 18 | netlink_put_register(nle, nftnl_reg_pmax, pmax_reg); | |
| 1356 | } else { | ||
| 1357 | 140 | netlink_gen_expr(ctx, stmt->nat.proto, pmin_reg); | |
| 1358 | 140 | netlink_put_register(nle, nftnl_reg_pmin, pmin_reg); | |
| 1359 | } | ||
| 1360 | } | ||
| 1361 | |||
| 1362 |
2/2✓ Branch 0 (57→55) taken 563 times.
✓ Branch 1 (57→58) taken 628 times.
|
1191 | while (registers > 0) { |
| 1363 | 563 | release_register(ctx, NULL); | |
| 1364 | 563 | registers--; | |
| 1365 | } | ||
| 1366 | |||
| 1367 | 628 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1368 | 628 | } | |
| 1369 | |||
| 1370 | 56 | static void netlink_gen_tproxy_stmt(struct netlink_linearize_ctx *ctx, | |
| 1371 | const struct stmt *stmt) | ||
| 1372 | { | ||
| 1373 | struct nftnl_expr *nle; | ||
| 1374 | enum nft_registers addr_reg; | ||
| 1375 | enum nft_registers port_reg; | ||
| 1376 | 56 | int registers = 0; | |
| 1377 | 56 | const int family = stmt->tproxy.family; | |
| 1378 | int nftnl_reg_port; | ||
| 1379 | |||
| 1380 | 56 | nle = alloc_nft_expr("tproxy"); | |
| 1381 | |||
| 1382 | 56 | nftnl_expr_set_u32(nle, NFTNL_EXPR_TPROXY_FAMILY, family); | |
| 1383 | |||
| 1384 | 56 | nftnl_reg_port = NFTNL_EXPR_TPROXY_REG_PORT; | |
| 1385 | |||
| 1386 |
2/2✓ Branch 0 (4→5) taken 30 times.
✓ Branch 1 (4→8) taken 26 times.
|
56 | if (stmt->tproxy.addr) { |
| 1387 | 30 | addr_reg = get_register(ctx, NULL); | |
| 1388 | 30 | registers++; | |
| 1389 | 30 | netlink_gen_expr(ctx, stmt->tproxy.addr, addr_reg); | |
| 1390 | 30 | netlink_put_register(nle, NFTNL_EXPR_TPROXY_REG_ADDR, | |
| 1391 | addr_reg); | ||
| 1392 | } | ||
| 1393 | |||
| 1394 |
2/2✓ Branch 0 (8→9) taken 42 times.
✓ Branch 1 (8→12) taken 14 times.
|
56 | if (stmt->tproxy.port) { |
| 1395 | 42 | port_reg = get_register(ctx, NULL); | |
| 1396 | 42 | registers++; | |
| 1397 | 42 | netlink_gen_expr(ctx, stmt->tproxy.port, port_reg); | |
| 1398 | 42 | netlink_put_register(nle, nftnl_reg_port, port_reg); | |
| 1399 | } | ||
| 1400 | |||
| 1401 |
2/2✓ Branch 0 (15→13) taken 72 times.
✓ Branch 1 (15→16) taken 56 times.
|
128 | while (registers > 0) { |
| 1402 | 72 | release_register(ctx, NULL); | |
| 1403 | 72 | registers--; | |
| 1404 | } | ||
| 1405 | |||
| 1406 | 56 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1407 | 56 | } | |
| 1408 | |||
| 1409 | 41 | static void netlink_gen_synproxy_stmt(struct netlink_linearize_ctx *ctx, | |
| 1410 | const struct stmt *stmt) | ||
| 1411 | { | ||
| 1412 | struct nftnl_expr *nle; | ||
| 1413 | |||
| 1414 | 41 | nle = alloc_nft_expr("synproxy"); | |
| 1415 | 41 | nftnl_expr_set_u16(nle, NFTNL_EXPR_SYNPROXY_MSS, stmt->synproxy.mss); | |
| 1416 | 41 | nftnl_expr_set_u8(nle, NFTNL_EXPR_SYNPROXY_WSCALE, | |
| 1417 | 41 | stmt->synproxy.wscale); | |
| 1418 | 41 | nftnl_expr_set_u32(nle, NFTNL_EXPR_SYNPROXY_FLAGS, | |
| 1419 | 41 | stmt->synproxy.flags); | |
| 1420 | |||
| 1421 | 41 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1422 | 41 | } | |
| 1423 | |||
| 1424 | 20 | static void netlink_gen_dup_stmt(struct netlink_linearize_ctx *ctx, | |
| 1425 | const struct stmt *stmt) | ||
| 1426 | { | ||
| 1427 | struct nftnl_expr *nle; | ||
| 1428 | enum nft_registers sreg1, sreg2; | ||
| 1429 | |||
| 1430 | 20 | nle = alloc_nft_expr("dup"); | |
| 1431 | |||
| 1432 |
1/2✓ Branch 0 (3→4) taken 20 times.
✗ Branch 1 (3→11) not taken.
|
20 | if (stmt->dup.to != NULL) { |
| 1433 |
2/2✓ Branch 0 (4→5) taken 8 times.
✓ Branch 1 (4→8) taken 12 times.
|
20 | if (stmt->dup.to->dtype == &ifindex_type) { |
| 1434 | 8 | sreg1 = get_register(ctx, stmt->dup.to); | |
| 1435 | 8 | netlink_gen_expr(ctx, stmt->dup.to, sreg1); | |
| 1436 | 8 | netlink_put_register(nle, NFTNL_EXPR_DUP_SREG_DEV, sreg1); | |
| 1437 | } else { | ||
| 1438 | 12 | sreg1 = get_register(ctx, stmt->dup.to); | |
| 1439 | 12 | netlink_gen_expr(ctx, stmt->dup.to, sreg1); | |
| 1440 | 12 | netlink_put_register(nle, NFTNL_EXPR_DUP_SREG_ADDR, sreg1); | |
| 1441 | } | ||
| 1442 | } | ||
| 1443 |
2/2✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→16) taken 12 times.
|
20 | if (stmt->dup.dev != NULL) { |
| 1444 | 8 | sreg2 = get_register(ctx, stmt->dup.dev); | |
| 1445 | 8 | netlink_gen_expr(ctx, stmt->dup.dev, sreg2); | |
| 1446 | 8 | netlink_put_register(nle, NFTNL_EXPR_DUP_SREG_DEV, sreg2); | |
| 1447 | 8 | release_register(ctx, stmt->dup.dev); | |
| 1448 | } | ||
| 1449 |
1/2✓ Branch 0 (16→17) taken 20 times.
✗ Branch 1 (16→18) not taken.
|
20 | if (stmt->dup.to != NULL) |
| 1450 | 20 | release_register(ctx, stmt->dup.to); | |
| 1451 | |||
| 1452 | 20 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1453 | 20 | } | |
| 1454 | |||
| 1455 | 12 | static void netlink_gen_fwd_stmt(struct netlink_linearize_ctx *ctx, | |
| 1456 | const struct stmt *stmt) | ||
| 1457 | { | ||
| 1458 | enum nft_registers sreg1, sreg2; | ||
| 1459 | struct nftnl_expr *nle; | ||
| 1460 | |||
| 1461 | 12 | nle = alloc_nft_expr("fwd"); | |
| 1462 | |||
| 1463 | 12 | sreg1 = get_register(ctx, stmt->fwd.dev); | |
| 1464 | 12 | netlink_gen_expr(ctx, stmt->fwd.dev, sreg1); | |
| 1465 | 12 | netlink_put_register(nle, NFTNL_EXPR_FWD_SREG_DEV, sreg1); | |
| 1466 | |||
| 1467 |
2/2✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→11) taken 8 times.
|
12 | if (stmt->fwd.addr != NULL) { |
| 1468 | 4 | sreg2 = get_register(ctx, stmt->fwd.addr); | |
| 1469 | 4 | netlink_gen_expr(ctx, stmt->fwd.addr, sreg2); | |
| 1470 | 4 | netlink_put_register(nle, NFTNL_EXPR_FWD_SREG_ADDR, sreg2); | |
| 1471 | 4 | release_register(ctx, stmt->fwd.addr); | |
| 1472 | } | ||
| 1473 | 12 | release_register(ctx, stmt->fwd.dev); | |
| 1474 | |||
| 1475 |
2/2✓ Branch 0 (12→13) taken 4 times.
✓ Branch 1 (12→14) taken 8 times.
|
12 | if (stmt->fwd.family) |
| 1476 | 4 | nftnl_expr_set_u32(nle, NFTNL_EXPR_FWD_NFPROTO, | |
| 1477 | 4 | stmt->fwd.family); | |
| 1478 | |||
| 1479 | 12 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1480 | 12 | } | |
| 1481 | |||
| 1482 | 26 | static void netlink_gen_optstrip_stmt(struct netlink_linearize_ctx *ctx, | |
| 1483 | const struct stmt *stmt) | ||
| 1484 | { | ||
| 1485 | 26 | struct nftnl_expr *nle = alloc_nft_expr("exthdr"); | |
| 1486 | 26 | struct expr *expr = stmt->optstrip.expr; | |
| 1487 | |||
| 1488 | 26 | nftnl_expr_set_u8(nle, NFTNL_EXPR_EXTHDR_TYPE, | |
| 1489 | 26 | expr->exthdr.raw_type); | |
| 1490 | 26 | nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OP, expr->exthdr.op); | |
| 1491 | 26 | nft_rule_add_expr(ctx, nle, &expr->location); | |
| 1492 | 26 | } | |
| 1493 | |||
| 1494 | 206 | static void netlink_gen_queue_stmt(struct netlink_linearize_ctx *ctx, | |
| 1495 | const struct stmt *stmt) | ||
| 1496 | { | ||
| 1497 | 206 | enum nft_registers sreg = 0; | |
| 1498 | struct nftnl_expr *nle; | ||
| 1499 | uint16_t total_queues; | ||
| 1500 | struct expr *expr; | ||
| 1501 | mpz_t low, high; | ||
| 1502 | |||
| 1503 | 206 | mpz_init2(low, 16); | |
| 1504 | 206 | mpz_init2(high, 16); | |
| 1505 | |||
| 1506 | 206 | expr = stmt->queue.queue; | |
| 1507 | |||
| 1508 |
2/2✓ Branch 0 (4→5) taken 198 times.
✓ Branch 1 (4→12) taken 8 times.
|
206 | if (expr) { |
| 1509 |
4/4✓ Branch 0 (5→6) taken 98 times.
✓ Branch 1 (5→7) taken 100 times.
✓ Branch 2 (6→7) taken 56 times.
✓ Branch 3 (6→9) taken 42 times.
|
198 | if (expr->etype == EXPR_RANGE || expr->etype == EXPR_VALUE) { |
| 1510 | 156 | range_expr_value_low(low, stmt->queue.queue); | |
| 1511 | 156 | range_expr_value_high(high, stmt->queue.queue); | |
| 1512 | } else { | ||
| 1513 | 42 | sreg = get_register(ctx, expr); | |
| 1514 | 42 | netlink_gen_expr(ctx, expr, sreg); | |
| 1515 | 42 | release_register(ctx, expr); | |
| 1516 | } | ||
| 1517 | } | ||
| 1518 | |||
| 1519 | 206 | total_queues = mpz_get_uint16(high) - mpz_get_uint16(low) + 1; | |
| 1520 | |||
| 1521 | 206 | nle = alloc_nft_expr("queue"); | |
| 1522 | |||
| 1523 |
2/2✓ Branch 0 (15→16) taken 42 times.
✓ Branch 1 (15→17) taken 164 times.
|
206 | if (sreg) { |
| 1524 | 42 | netlink_put_register(nle, NFTNL_EXPR_QUEUE_SREG_QNUM, sreg); | |
| 1525 | } else { | ||
| 1526 | 164 | nftnl_expr_set_u16(nle, NFTNL_EXPR_QUEUE_NUM, mpz_get_uint16(low)); | |
| 1527 | 164 | nftnl_expr_set_u16(nle, NFTNL_EXPR_QUEUE_TOTAL, total_queues); | |
| 1528 | } | ||
| 1529 | |||
| 1530 |
2/2✓ Branch 0 (20→21) taken 98 times.
✓ Branch 1 (20→22) taken 108 times.
|
206 | if (stmt->queue.flags) |
| 1531 | 98 | nftnl_expr_set_u16(nle, NFTNL_EXPR_QUEUE_FLAGS, | |
| 1532 | 98 | stmt->queue.flags); | |
| 1533 | |||
| 1534 | 206 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1535 | |||
| 1536 | 206 | mpz_clear(low); | |
| 1537 | 206 | mpz_clear(high); | |
| 1538 | 206 | } | |
| 1539 | |||
| 1540 | 274 | static void netlink_gen_ct_stmt(struct netlink_linearize_ctx *ctx, | |
| 1541 | const struct stmt *stmt) | ||
| 1542 | { | ||
| 1543 | struct nftnl_expr *nle; | ||
| 1544 | enum nft_registers sreg; | ||
| 1545 | |||
| 1546 | 274 | sreg = get_register(ctx, stmt->ct.expr); | |
| 1547 | 274 | netlink_gen_expr(ctx, stmt->ct.expr, sreg); | |
| 1548 | 274 | release_register(ctx, stmt->ct.expr); | |
| 1549 | |||
| 1550 | 274 | nle = alloc_nft_expr("ct"); | |
| 1551 | 274 | netlink_put_register(nle, NFTNL_EXPR_CT_SREG, sreg); | |
| 1552 | 274 | nftnl_expr_set_u32(nle, NFTNL_EXPR_CT_KEY, stmt->ct.key); | |
| 1553 |
2/2✓ Branch 0 (8→9) taken 12 times.
✓ Branch 1 (8→10) taken 262 times.
|
274 | if (stmt->ct.direction >= 0) |
| 1554 | 12 | nftnl_expr_set_u8(nle, NFTNL_EXPR_CT_DIR, | |
| 1555 | 12 | stmt->ct.direction); | |
| 1556 | |||
| 1557 | 274 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1558 | 274 | } | |
| 1559 | |||
| 1560 | 6 | static void netlink_gen_notrack_stmt(struct netlink_linearize_ctx *ctx, | |
| 1561 | const struct stmt *stmt) | ||
| 1562 | { | ||
| 1563 | struct nftnl_expr *nle; | ||
| 1564 | |||
| 1565 | 6 | nle = alloc_nft_expr("notrack"); | |
| 1566 | 6 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1567 | 6 | } | |
| 1568 | |||
| 1569 | 18 | static void netlink_gen_flow_offload_stmt(struct netlink_linearize_ctx *ctx, | |
| 1570 | const struct stmt *stmt) | ||
| 1571 | { | ||
| 1572 | struct nftnl_expr *nle; | ||
| 1573 | |||
| 1574 | 18 | nle = alloc_nft_expr("flow_offload"); | |
| 1575 | 18 | nftnl_expr_set_str(nle, NFTNL_EXPR_FLOW_TABLE_NAME, | |
| 1576 | 18 | stmt->flow.table_name); | |
| 1577 | 18 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1578 | 18 | } | |
| 1579 | |||
| 1580 | 1167 | static void netlink_gen_set_stmt(struct netlink_linearize_ctx *ctx, | |
| 1581 | const struct stmt *stmt) | ||
| 1582 | { | ||
| 1583 | 1167 | struct set *set = stmt->meter.set->set; | |
| 1584 | enum nft_registers sreg_key; | ||
| 1585 | struct nftnl_expr *nle; | ||
| 1586 | 1167 | int num_stmts = 0; | |
| 1587 | struct stmt *this; | ||
| 1588 | |||
| 1589 | 1167 | sreg_key = get_register(ctx, stmt->set.key->key); | |
| 1590 | 1167 | netlink_gen_expr(ctx, stmt->set.key->key, sreg_key); | |
| 1591 | 1167 | release_register(ctx, stmt->set.key->key); | |
| 1592 | |||
| 1593 | 1167 | nle = alloc_nft_expr("dynset"); | |
| 1594 | 1167 | netlink_put_register(nle, NFTNL_EXPR_DYNSET_SREG_KEY, sreg_key); | |
| 1595 |
2/2✓ Branch 0 (7→8) taken 1068 times.
✓ Branch 1 (7→9) taken 99 times.
|
1167 | if (stmt->set.key->timeout > 0) |
| 1596 | 1068 | nftnl_expr_set_u64(nle, NFTNL_EXPR_DYNSET_TIMEOUT, | |
| 1597 | 1068 | stmt->set.key->timeout); | |
| 1598 | 1167 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_OP, stmt->set.op); | |
| 1599 | 1167 | nftnl_expr_set_str(nle, NFTNL_EXPR_DYNSET_SET_NAME, set->handle.set.name); | |
| 1600 | 1167 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_SET_ID, set->handle.set_id); | |
| 1601 | 1167 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1602 | |||
| 1603 |
2/2✓ Branch 0 (15→14) taken 42 times.
✓ Branch 1 (15→16) taken 1167 times.
|
1209 | list_for_each_entry(this, &stmt->set.stmt_list, list) |
| 1604 | 42 | num_stmts++; | |
| 1605 | |||
| 1606 |
2/2✓ Branch 0 (16→17) taken 34 times.
✓ Branch 1 (16→22) taken 1133 times.
|
1167 | if (num_stmts == 1) { |
| 1607 |
2/2✓ Branch 0 (21→18) taken 34 times.
✓ Branch 1 (21→29) taken 34 times.
|
68 | list_for_each_entry(this, &stmt->set.stmt_list, list) { |
| 1608 | 34 | nftnl_expr_set(nle, NFTNL_EXPR_DYNSET_EXPR, | |
| 1609 | 34 | netlink_gen_stmt_stateful(this), 0); | |
| 1610 | } | ||
| 1611 |
2/2✓ Branch 0 (22→23) taken 4 times.
✓ Branch 1 (22→29) taken 1129 times.
|
1133 | } else if (num_stmts > 1) { |
| 1612 |
2/2✓ Branch 0 (27→24) taken 8 times.
✓ Branch 1 (27→28) taken 4 times.
|
12 | list_for_each_entry(this, &stmt->set.stmt_list, list) { |
| 1613 | 8 | nftnl_expr_add_expr(nle, NFTNL_EXPR_DYNSET_EXPRESSIONS, | |
| 1614 | netlink_gen_stmt_stateful(this)); | ||
| 1615 | } | ||
| 1616 | 4 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_FLAGS, | |
| 1617 | NFT_DYNSET_F_EXPR); | ||
| 1618 | } | ||
| 1619 | 1167 | } | |
| 1620 | |||
| 1621 | 72 | static void netlink_gen_map_stmt(struct netlink_linearize_ctx *ctx, | |
| 1622 | const struct stmt *stmt) | ||
| 1623 | { | ||
| 1624 | 72 | struct set *set = stmt->map.set->set; | |
| 1625 | enum nft_registers sreg_data; | ||
| 1626 | enum nft_registers sreg_key; | ||
| 1627 | struct nftnl_expr *nle; | ||
| 1628 | 72 | int num_stmts = 0; | |
| 1629 | struct stmt *this; | ||
| 1630 | |||
| 1631 | 72 | sreg_key = get_register(ctx, stmt->map.key->key); | |
| 1632 | 72 | netlink_gen_expr(ctx, stmt->map.key->key, sreg_key); | |
| 1633 | |||
| 1634 | 72 | sreg_data = get_register(ctx, stmt->map.data->key); | |
| 1635 | 72 | netlink_gen_expr(ctx, stmt->map.data->key, sreg_data); | |
| 1636 | |||
| 1637 | 72 | release_register(ctx, stmt->map.key->key); | |
| 1638 | 72 | release_register(ctx, stmt->map.data->key); | |
| 1639 | |||
| 1640 | 72 | nle = alloc_nft_expr("dynset"); | |
| 1641 | 72 | netlink_put_register(nle, NFTNL_EXPR_DYNSET_SREG_KEY, sreg_key); | |
| 1642 | 72 | netlink_put_register(nle, NFTNL_EXPR_DYNSET_SREG_DATA, sreg_data); | |
| 1643 | |||
| 1644 | 72 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_OP, stmt->map.op); | |
| 1645 | 72 | nftnl_expr_set_str(nle, NFTNL_EXPR_DYNSET_SET_NAME, set->handle.set.name); | |
| 1646 | 72 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_SET_ID, set->handle.set_id); | |
| 1647 | 72 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1648 | |||
| 1649 |
2/2✓ Branch 0 (15→16) taken 16 times.
✓ Branch 1 (15→17) taken 56 times.
|
72 | if (stmt->map.key->timeout > 0) |
| 1650 | 16 | nftnl_expr_set_u64(nle, NFTNL_EXPR_DYNSET_TIMEOUT, | |
| 1651 | 16 | stmt->map.key->timeout); | |
| 1652 | |||
| 1653 |
1/2✗ Branch 0 (19→18) not taken.
✓ Branch 1 (19→20) taken 72 times.
|
72 | list_for_each_entry(this, &stmt->map.stmt_list, list) |
| 1654 | ✗ | num_stmts++; | |
| 1655 | |||
| 1656 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→26) taken 72 times.
|
72 | if (num_stmts == 1) { |
| 1657 | ✗ | list_for_each_entry(this, &stmt->map.stmt_list, list) { | |
| 1658 | ✗ | nftnl_expr_set(nle, NFTNL_EXPR_DYNSET_EXPR, | |
| 1659 | ✗ | netlink_gen_stmt_stateful(this), 0); | |
| 1660 | } | ||
| 1661 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→32) taken 72 times.
|
72 | } else if (num_stmts > 1) { |
| 1662 | ✗ | list_for_each_entry(this, &stmt->map.stmt_list, list) { | |
| 1663 | ✗ | nftnl_expr_add_expr(nle, NFTNL_EXPR_DYNSET_EXPRESSIONS, | |
| 1664 | netlink_gen_stmt_stateful(this)); | ||
| 1665 | } | ||
| 1666 | } | ||
| 1667 | 72 | } | |
| 1668 | |||
| 1669 | 7 | static void netlink_gen_meter_stmt(struct netlink_linearize_ctx *ctx, | |
| 1670 | const struct stmt *stmt) | ||
| 1671 | { | ||
| 1672 | struct nftnl_expr *nle; | ||
| 1673 | enum nft_registers sreg_key; | ||
| 1674 | enum nft_dynset_ops op; | ||
| 1675 | struct set *set; | ||
| 1676 | |||
| 1677 | 7 | sreg_key = get_register(ctx, stmt->meter.key->key); | |
| 1678 | 7 | netlink_gen_expr(ctx, stmt->meter.key->key, sreg_key); | |
| 1679 | 7 | release_register(ctx, stmt->meter.key->key); | |
| 1680 | |||
| 1681 | 7 | set = stmt->meter.set->set; | |
| 1682 |
2/2✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→7) taken 4 times.
|
7 | if (stmt->meter.key->timeout) |
| 1683 | 3 | op = NFT_DYNSET_OP_UPDATE; | |
| 1684 | else | ||
| 1685 | 4 | op = NFT_DYNSET_OP_ADD; | |
| 1686 | |||
| 1687 | 7 | nle = alloc_nft_expr("dynset"); | |
| 1688 | 7 | netlink_put_register(nle, NFTNL_EXPR_DYNSET_SREG_KEY, sreg_key); | |
| 1689 |
2/2✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→12) taken 4 times.
|
7 | if (stmt->meter.key->timeout) |
| 1690 | 3 | nftnl_expr_set_u64(nle, NFTNL_EXPR_DYNSET_TIMEOUT, | |
| 1691 | 3 | stmt->meter.key->timeout); | |
| 1692 | 7 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_OP, op); | |
| 1693 | 7 | nftnl_expr_set_str(nle, NFTNL_EXPR_DYNSET_SET_NAME, set->handle.set.name); | |
| 1694 | 7 | nftnl_expr_set_u32(nle, NFTNL_EXPR_DYNSET_SET_ID, set->handle.set_id); | |
| 1695 | 7 | nftnl_expr_set(nle, NFTNL_EXPR_DYNSET_EXPR, | |
| 1696 | 7 | netlink_gen_stmt_stateful(stmt->meter.stmt), 0); | |
| 1697 | 7 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1698 | 7 | } | |
| 1699 | |||
| 1700 | 62 | static void netlink_gen_chain_stmt(struct netlink_linearize_ctx *ctx, | |
| 1701 | const struct stmt *stmt) | ||
| 1702 | { | ||
| 1703 | 62 | return netlink_gen_expr(ctx, stmt->chain.expr, NFT_REG_VERDICT); | |
| 1704 | } | ||
| 1705 | |||
| 1706 | 131151 | static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx, | |
| 1707 | const struct stmt *stmt) | ||
| 1708 | { | ||
| 1709 | struct nftnl_expr *nle; | ||
| 1710 | |||
| 1711 |
23/24✓ Branch 0 (2→3) taken 109109 times.
✓ Branch 1 (2→4) taken 14777 times.
✓ Branch 2 (2→5) taken 7 times.
✓ Branch 3 (2→6) taken 12 times.
✓ Branch 4 (2→7) taken 419 times.
✓ Branch 5 (2→8) taken 512 times.
✓ Branch 6 (2→9) taken 256 times.
✓ Branch 7 (2→10) taken 300 times.
✓ Branch 8 (2→11) taken 628 times.
✓ Branch 9 (2→12) taken 56 times.
✓ Branch 10 (2→13) taken 41 times.
✓ Branch 11 (2→14) taken 20 times.
✓ Branch 12 (2→15) taken 206 times.
✓ Branch 13 (2→16) taken 274 times.
✓ Branch 14 (2→17) taken 1167 times.
✓ Branch 15 (2→18) taken 12 times.
✓ Branch 16 (2→19) taken 2856 times.
✓ Branch 17 (2→21) taken 6 times.
✓ Branch 18 (2→22) taken 18 times.
✓ Branch 19 (2→23) taken 315 times.
✓ Branch 20 (2→24) taken 72 times.
✓ Branch 21 (2→25) taken 62 times.
✓ Branch 22 (2→26) taken 26 times.
✗ Branch 23 (2→27) not taken.
|
131151 | switch (stmt->type) { |
| 1712 | 109109 | case STMT_EXPRESSION: | |
| 1713 | 109109 | return netlink_gen_expr(ctx, stmt->expr, NFT_REG_VERDICT); | |
| 1714 | 14777 | case STMT_VERDICT: | |
| 1715 | 14777 | return netlink_gen_verdict_stmt(ctx, stmt); | |
| 1716 | 7 | case STMT_METER: | |
| 1717 | 7 | return netlink_gen_meter_stmt(ctx, stmt); | |
| 1718 | 12 | case STMT_EXTHDR: | |
| 1719 | 12 | return netlink_gen_exthdr_stmt(ctx, stmt); | |
| 1720 | 419 | case STMT_PAYLOAD: | |
| 1721 | 419 | return netlink_gen_payload_stmt(ctx, stmt); | |
| 1722 | 512 | case STMT_META: | |
| 1723 | 512 | return netlink_gen_meta_stmt(ctx, stmt); | |
| 1724 | 256 | case STMT_LOG: | |
| 1725 | 256 | return netlink_gen_log_stmt(ctx, stmt); | |
| 1726 | 300 | case STMT_REJECT: | |
| 1727 | 300 | return netlink_gen_reject_stmt(ctx, stmt); | |
| 1728 | 628 | case STMT_NAT: | |
| 1729 | 628 | return netlink_gen_nat_stmt(ctx, stmt); | |
| 1730 | 56 | case STMT_TPROXY: | |
| 1731 | 56 | return netlink_gen_tproxy_stmt(ctx, stmt); | |
| 1732 | 41 | case STMT_SYNPROXY: | |
| 1733 | 41 | return netlink_gen_synproxy_stmt(ctx, stmt); | |
| 1734 | 20 | case STMT_DUP: | |
| 1735 | 20 | return netlink_gen_dup_stmt(ctx, stmt); | |
| 1736 | 206 | case STMT_QUEUE: | |
| 1737 | 206 | return netlink_gen_queue_stmt(ctx, stmt); | |
| 1738 | 274 | case STMT_CT: | |
| 1739 | 274 | return netlink_gen_ct_stmt(ctx, stmt); | |
| 1740 | 1167 | case STMT_SET: | |
| 1741 | 1167 | return netlink_gen_set_stmt(ctx, stmt); | |
| 1742 | 12 | case STMT_FWD: | |
| 1743 | 12 | return netlink_gen_fwd_stmt(ctx, stmt); | |
| 1744 | 2856 | case STMT_CONNLIMIT: | |
| 1745 | case STMT_COUNTER: | ||
| 1746 | case STMT_LIMIT: | ||
| 1747 | case STMT_QUOTA: | ||
| 1748 | case STMT_LAST: | ||
| 1749 | 2856 | nle = netlink_gen_stmt_stateful(stmt); | |
| 1750 | 2856 | nft_rule_add_expr(ctx, nle, &stmt->location); | |
| 1751 | 2856 | break; | |
| 1752 | 6 | case STMT_NOTRACK: | |
| 1753 | 6 | return netlink_gen_notrack_stmt(ctx, stmt); | |
| 1754 | 18 | case STMT_FLOW_OFFLOAD: | |
| 1755 | 18 | return netlink_gen_flow_offload_stmt(ctx, stmt); | |
| 1756 | 315 | case STMT_OBJREF: | |
| 1757 | 315 | return netlink_gen_objref_stmt(ctx, stmt); | |
| 1758 | 72 | case STMT_MAP: | |
| 1759 | 72 | return netlink_gen_map_stmt(ctx, stmt); | |
| 1760 | 62 | case STMT_CHAIN: | |
| 1761 | 62 | return netlink_gen_chain_stmt(ctx, stmt); | |
| 1762 | 26 | case STMT_OPTSTRIP: | |
| 1763 | 26 | return netlink_gen_optstrip_stmt(ctx, stmt); | |
| 1764 | ✗ | default: | |
| 1765 | ✗ | BUG("unknown statement type %d", stmt->type); | |
| 1766 | } | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | 108372 | void netlink_linearize_init(struct netlink_linearize_ctx *lctx, | |
| 1770 | struct nftnl_rule *nlr) | ||
| 1771 | { | ||
| 1772 | int i; | ||
| 1773 | |||
| 1774 | 108372 | memset(lctx, 0, sizeof(*lctx)); | |
| 1775 | 108372 | lctx->reg_low = NFT_REG_1; | |
| 1776 | 108372 | lctx->nlr = nlr; | |
| 1777 | 108372 | lctx->expr_loc_htable = | |
| 1778 | 108372 | xmalloc(sizeof(struct list_head) * NFT_EXPR_LOC_HSIZE); | |
| 1779 |
2/2✓ Branch 0 (6→4) taken 13871616 times.
✓ Branch 1 (6→7) taken 108372 times.
|
13979988 | for (i = 0; i < NFT_EXPR_LOC_HSIZE; i++) |
| 1780 | 13871616 | init_list_head(&lctx->expr_loc_htable[i]); | |
| 1781 | 108372 | } | |
| 1782 | |||
| 1783 | 108372 | void netlink_linearize_fini(struct netlink_linearize_ctx *lctx) | |
| 1784 | { | ||
| 1785 | struct nft_expr_loc *eloc, *next; | ||
| 1786 | int i; | ||
| 1787 | |||
| 1788 |
2/2✓ Branch 0 (7→3) taken 13871616 times.
✓ Branch 1 (7→8) taken 108372 times.
|
13979988 | for (i = 0; i < NFT_EXPR_LOC_HSIZE; i++) { |
| 1789 |
2/2✓ Branch 0 (5→4) taken 251819 times.
✓ Branch 1 (5→6) taken 13871616 times.
|
14123435 | list_for_each_entry_safe(eloc, next, &lctx->expr_loc_htable[i], hlist) |
| 1790 | 251819 | free(eloc); | |
| 1791 | } | ||
| 1792 | 108372 | free(lctx->expr_loc_htable); | |
| 1793 | 108372 | } | |
| 1794 | |||
| 1795 | 108372 | void netlink_linearize_rule(struct netlink_ctx *ctx, | |
| 1796 | const struct rule *rule, | ||
| 1797 | struct netlink_linearize_ctx *lctx) | ||
| 1798 | { | ||
| 1799 | const struct stmt *stmt; | ||
| 1800 | |||
| 1801 |
2/2✓ Branch 0 (5→3) taken 131151 times.
✓ Branch 1 (5→6) taken 108372 times.
|
239523 | list_for_each_entry(stmt, &rule->stmts, list) |
| 1802 | 131151 | netlink_gen_stmt(lctx, stmt); | |
| 1803 | |||
| 1804 |
2/2✓ Branch 0 (6→7) taken 6138 times.
✓ Branch 1 (6→17) taken 102234 times.
|
108372 | if (rule->comment) { |
| 1805 | struct nftnl_udata_buf *udata; | ||
| 1806 | |||
| 1807 | 6138 | udata = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); | |
| 1808 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 6138 times.
|
6138 | if (!udata) |
| 1809 | ✗ | memory_allocation_error(); | |
| 1810 | |||
| 1811 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 6138 times.
|
6138 | if (!nftnl_udata_put_strz(udata, NFTNL_UDATA_RULE_COMMENT, |
| 1812 | 6138 | rule->comment)) | |
| 1813 | ✗ | memory_allocation_error(); | |
| 1814 | 12276 | nftnl_rule_set_data(lctx->nlr, NFTNL_RULE_USERDATA, | |
| 1815 | 6138 | nftnl_udata_buf_data(udata), | |
| 1816 | nftnl_udata_buf_len(udata)); | ||
| 1817 | |||
| 1818 | 6138 | nftnl_udata_buf_free(udata); | |
| 1819 | } | ||
| 1820 | |||
| 1821 |
2/2✓ Branch 0 (17→18) taken 13109 times.
✓ Branch 1 (17→24) taken 95263 times.
|
108372 | if (ctx->nft->debug_mask & NFT_DEBUG_NETLINK) { |
| 1822 | 13109 | nftnl_rule_set_str(lctx->nlr, NFTNL_RULE_TABLE, | |
| 1823 | 13109 | rule->handle.table.name); | |
| 1824 |
1/2✓ Branch 0 (19→20) taken 13109 times.
✗ Branch 1 (19→21) not taken.
|
13109 | if (rule->handle.chain.name) |
| 1825 | 13109 | nftnl_rule_set_str(lctx->nlr, NFTNL_RULE_CHAIN, | |
| 1826 | 13109 | rule->handle.chain.name); | |
| 1827 | |||
| 1828 | 13109 | netlink_dump_rule(lctx->nlr, ctx); | |
| 1829 | |||
| 1830 | 13109 | nftnl_rule_unset(lctx->nlr, NFTNL_RULE_CHAIN); | |
| 1831 | 13109 | nftnl_rule_unset(lctx->nlr, NFTNL_RULE_TABLE); | |
| 1832 | } | ||
| 1833 | 108372 | } | |
| 1834 |