| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Number generator expression definitions. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 8 | * later) as published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <nftables.h> | ||
| 14 | #include <expression.h> | ||
| 15 | #include <datatype.h> | ||
| 16 | #include <gmputil.h> | ||
| 17 | #include <numgen.h> | ||
| 18 | #include <utils.h> | ||
| 19 | |||
| 20 | static const char *numgen_type[NFT_NG_RANDOM + 1] = { | ||
| 21 | [NFT_NG_INCREMENTAL] = "inc", | ||
| 22 | [NFT_NG_RANDOM] = "random", | ||
| 23 | }; | ||
| 24 | |||
| 25 | 28 | static const char *numgen_type_str(enum nft_ng_types type) | |
| 26 | { | ||
| 27 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 28 times.
|
28 | if (type > NFT_NG_RANDOM) |
| 28 | ✗ | return "[unknown numgen]"; | |
| 29 | |||
| 30 | 28 | return numgen_type[type]; | |
| 31 | } | ||
| 32 | |||
| 33 | 28 | static void numgen_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 34 | { | ||
| 35 | 28 | nft_print(octx, "numgen %s mod %u", | |
| 36 | 28 | numgen_type_str(expr->numgen.type), | |
| 37 | 28 | expr->numgen.mod); | |
| 38 |
2/2✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→6) taken 24 times.
|
28 | if (expr->numgen.offset) |
| 39 | 4 | nft_print(octx, " offset %u", expr->numgen.offset); | |
| 40 | 28 | } | |
| 41 | |||
| 42 | ✗ | static bool numgen_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 43 | { | ||
| 44 | ✗ | return e1->numgen.type == e2->numgen.type && | |
| 45 | ✗ | e1->numgen.mod == e2->numgen.mod && | |
| 46 | ✗ | e1->numgen.offset == e2->numgen.offset; | |
| 47 | } | ||
| 48 | |||
| 49 | 70 | static void numgen_expr_clone(struct expr *new, const struct expr *expr) | |
| 50 | { | ||
| 51 | 70 | new->numgen.type = expr->numgen.type; | |
| 52 | 70 | new->numgen.mod = expr->numgen.mod; | |
| 53 | 70 | new->numgen.offset = expr->numgen.offset; | |
| 54 | 70 | } | |
| 55 | |||
| 56 | #define NFTNL_UDATA_NUMGEN_TYPE 0 | ||
| 57 | #define NFTNL_UDATA_NUMGEN_MOD 1 | ||
| 58 | #define NFTNL_UDATA_NUMGEN_OFFSET 2 | ||
| 59 | #define NFTNL_UDATA_NUMGEN_MAX 3 | ||
| 60 | |||
| 61 | 32 | static int numgen_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 62 | const struct expr *expr) | ||
| 63 | { | ||
| 64 | 32 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_NUMGEN_TYPE, expr->numgen.type); | |
| 65 | 32 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_NUMGEN_MOD, expr->numgen.mod); | |
| 66 | 32 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_NUMGEN_OFFSET, expr->numgen.offset); | |
| 67 | |||
| 68 | 32 | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | 318 | static int numgen_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 72 | { | ||
| 73 | 318 | const struct nftnl_udata **ud = data; | |
| 74 | 318 | uint8_t type = nftnl_udata_type(attr); | |
| 75 | 318 | uint8_t len = nftnl_udata_len(attr); | |
| 76 | |||
| 77 |
1/2✓ Branch 0 (4→5) taken 318 times.
✗ Branch 1 (4→8) not taken.
|
318 | switch (type) { |
| 78 | 318 | case NFTNL_UDATA_NUMGEN_TYPE: | |
| 79 | case NFTNL_UDATA_NUMGEN_MOD: | ||
| 80 | case NFTNL_UDATA_NUMGEN_OFFSET: | ||
| 81 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 318 times.
|
318 | if (len != sizeof(uint32_t)) |
| 82 | ✗ | return -1; | |
| 83 | 318 | break; | |
| 84 | ✗ | default: | |
| 85 | ✗ | return 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 318 | ud[type] = attr; | |
| 89 | 318 | return 0; | |
| 90 | } | ||
| 91 | |||
| 92 | 106 | static struct expr *numgen_expr_parse_udata(const struct nftnl_udata *attr) | |
| 93 | { | ||
| 94 | 106 | const struct nftnl_udata *ud[NFTNL_UDATA_NUMGEN_MAX + 1] = {}; | |
| 95 | enum nft_ng_types type; | ||
| 96 | uint32_t mod, offset; | ||
| 97 | int err; | ||
| 98 | |||
| 99 | 106 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 100 | numgen_parse_udata, ud); | ||
| 101 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 106 times.
|
106 | if (err < 0) |
| 102 | ✗ | return NULL; | |
| 103 | |||
| 104 |
1/2✓ Branch 0 (7→8) taken 106 times.
✗ Branch 1 (7→10) not taken.
|
106 | if (!ud[NFTNL_UDATA_NUMGEN_TYPE] || |
| 105 |
1/2✓ Branch 0 (8→9) taken 106 times.
✗ Branch 1 (8→10) not taken.
|
106 | !ud[NFTNL_UDATA_NUMGEN_MOD] || |
| 106 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 106 times.
|
106 | !ud[NFTNL_UDATA_NUMGEN_OFFSET]) |
| 107 | ✗ | return NULL; | |
| 108 | |||
| 109 | 106 | type = nftnl_udata_get_u32(ud[NFTNL_UDATA_NUMGEN_TYPE]); | |
| 110 | 106 | mod = nftnl_udata_get_u32(ud[NFTNL_UDATA_NUMGEN_MOD]); | |
| 111 | 106 | offset = nftnl_udata_get_u32(ud[NFTNL_UDATA_NUMGEN_OFFSET]); | |
| 112 | |||
| 113 | 106 | return numgen_expr_alloc(&internal_location, type, mod, offset); | |
| 114 | } | ||
| 115 | |||
| 116 | const struct expr_ops numgen_expr_ops = { | ||
| 117 | .type = EXPR_NUMGEN, | ||
| 118 | .name = "numgen", | ||
| 119 | .print = numgen_expr_print, | ||
| 120 | .json = numgen_expr_json, | ||
| 121 | .cmp = numgen_expr_cmp, | ||
| 122 | .clone = numgen_expr_clone, | ||
| 123 | .parse_udata = numgen_expr_parse_udata, | ||
| 124 | .build_udata = numgen_expr_build_udata, | ||
| 125 | }; | ||
| 126 | |||
| 127 | 235 | struct expr *numgen_expr_alloc(const struct location *loc, | |
| 128 | enum nft_ng_types type, uint32_t mod, | ||
| 129 | uint32_t offset) | ||
| 130 | { | ||
| 131 | struct expr *expr; | ||
| 132 | |||
| 133 | 235 | expr = expr_alloc(loc, EXPR_NUMGEN, &integer_type, | |
| 134 | BYTEORDER_HOST_ENDIAN, 4 * BITS_PER_BYTE); | ||
| 135 | 235 | expr->numgen.type = type; | |
| 136 | 235 | expr->numgen.mod = mod; | |
| 137 | 235 | expr->numgen.offset = offset; | |
| 138 | |||
| 139 | 235 | return expr; | |
| 140 | } | ||
| 141 |