| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * FIB expression. | ||
| 3 | * | ||
| 4 | * Copyright (c) Red Hat GmbH. Author: Florian Westphal <fw@strlen.de> | ||
| 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 <erec.h> | ||
| 15 | #include <expression.h> | ||
| 16 | #include <datatype.h> | ||
| 17 | #include <gmputil.h> | ||
| 18 | #include <utils.h> | ||
| 19 | #include <fib.h> | ||
| 20 | |||
| 21 | #include <linux/rtnetlink.h> | ||
| 22 | #include <net/if.h> | ||
| 23 | |||
| 24 | static const char *fib_result[NFT_FIB_RESULT_MAX + 1] = { | ||
| 25 | [NFT_FIB_RESULT_OIF] = "oif", | ||
| 26 | [NFT_FIB_RESULT_OIFNAME] = "oifname", | ||
| 27 | [NFT_FIB_RESULT_ADDRTYPE] = "type", | ||
| 28 | }; | ||
| 29 | |||
| 30 | static const struct symbol_table addrtype_tbl = { | ||
| 31 | .base = BASE_DECIMAL, | ||
| 32 | .symbols = { | ||
| 33 | SYMBOL("unspec", RTN_UNSPEC), | ||
| 34 | SYMBOL("unicast", RTN_UNICAST), | ||
| 35 | SYMBOL("local", RTN_LOCAL), | ||
| 36 | SYMBOL("broadcast", RTN_BROADCAST), | ||
| 37 | SYMBOL("anycast", RTN_ANYCAST), | ||
| 38 | SYMBOL("multicast", RTN_MULTICAST), | ||
| 39 | SYMBOL("blackhole", RTN_BLACKHOLE), | ||
| 40 | SYMBOL("unreachable", RTN_UNREACHABLE), | ||
| 41 | SYMBOL("prohibit", RTN_PROHIBIT), | ||
| 42 | SYMBOL_LIST_END | ||
| 43 | } | ||
| 44 | }; | ||
| 45 | |||
| 46 | const struct datatype fib_addr_type = { | ||
| 47 | .type = TYPE_FIB_ADDR, | ||
| 48 | .name = "fib_addrtype", | ||
| 49 | .desc = "fib address type", | ||
| 50 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 51 | .size = 4 * BITS_PER_BYTE, | ||
| 52 | .basetype = &integer_type, | ||
| 53 | .sym_tbl = &addrtype_tbl, | ||
| 54 | }; | ||
| 55 | |||
| 56 | 53 | const char *fib_result_str(const struct expr *expr, bool check) | |
| 57 | { | ||
| 58 | 53 | enum nft_fib_result result = expr->fib.result; | |
| 59 | 53 | uint32_t flags = expr->fib.flags; | |
| 60 | |||
| 61 | /* Exception: check if route exists. */ | ||
| 62 |
4/4✓ Branch 0 (2→3) taken 33 times.
✓ Branch 1 (2→6) taken 20 times.
✓ Branch 2 (3→4) taken 21 times.
✓ Branch 3 (3→6) taken 12 times.
|
53 | if (check && |
| 63 | 21 | result == NFT_FIB_RESULT_OIF && | |
| 64 |
2/2✓ Branch 0 (4→5) taken 18 times.
✓ Branch 1 (4→6) taken 3 times.
|
21 | flags & NFTA_FIB_F_PRESENT) |
| 65 | 18 | return "check"; | |
| 66 | |||
| 67 |
1/2✓ Branch 0 (6→7) taken 35 times.
✗ Branch 1 (6→8) not taken.
|
35 | if (result <= NFT_FIB_RESULT_MAX) |
| 68 | 35 | return fib_result[result]; | |
| 69 | |||
| 70 | ✗ | return "unknown"; | |
| 71 | } | ||
| 72 | |||
| 73 | − | static void __fib_expr_print_f(unsigned int *flags, unsigned int f, | |
| 74 | const char *s, struct output_ctx *octx) | ||
| 75 | { | ||
| 76 | − | if ((*flags & f) == 0) | |
| 77 | − | return; | |
| 78 | |||
| 79 | − | nft_print(octx, "%s", s); | |
| 80 | − | *flags &= ~f; | |
| 81 | − | if (*flags) | |
| 82 | − | nft_print(octx, " . "); | |
| 83 | } | ||
| 84 | |||
| 85 | 27 | static void fib_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 86 | { | ||
| 87 | 27 | unsigned int flags = expr->fib.flags & ~NFTA_FIB_F_PRESENT; | |
| 88 | |||
| 89 | 27 | nft_print(octx, "fib "); | |
| 90 | 27 | __fib_expr_print_f(&flags, NFTA_FIB_F_SADDR, "saddr", octx); | |
| 91 | 27 | __fib_expr_print_f(&flags, NFTA_FIB_F_DADDR, "daddr", octx); | |
| 92 | 27 | __fib_expr_print_f(&flags, NFTA_FIB_F_MARK, "mark", octx); | |
| 93 | 27 | __fib_expr_print_f(&flags, NFTA_FIB_F_IIF, "iif", octx); | |
| 94 | 27 | __fib_expr_print_f(&flags, NFTA_FIB_F_OIF, "oif", octx); | |
| 95 | |||
| 96 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 27 times.
|
27 | if (flags) |
| 97 | ✗ | nft_print(octx, "0x%x", flags); | |
| 98 | |||
| 99 | 27 | nft_print(octx, " %s", fib_result_str(expr, true)); | |
| 100 | 27 | } | |
| 101 | |||
| 102 | ✗ | static bool fib_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 103 | { | ||
| 104 | ✗ | return e1->fib.result == e2->fib.result && | |
| 105 | ✗ | e1->fib.flags == e2->fib.flags; | |
| 106 | } | ||
| 107 | |||
| 108 | 69 | static void fib_expr_clone(struct expr *new, const struct expr *expr) | |
| 109 | { | ||
| 110 | 69 | new->fib.result = expr->fib.result; | |
| 111 | 69 | new->fib.flags= expr->fib.flags; | |
| 112 | 69 | } | |
| 113 | |||
| 114 | #define NFTNL_UDATA_FIB_RESULT 0 | ||
| 115 | #define NFTNL_UDATA_FIB_FLAGS 1 | ||
| 116 | #define NFTNL_UDATA_FIB_MAX 2 | ||
| 117 | |||
| 118 | 10 | static int fib_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 119 | const struct expr *expr) | ||
| 120 | { | ||
| 121 | 10 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_FIB_RESULT, expr->fib.result); | |
| 122 | 10 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_FIB_FLAGS, expr->fib.flags); | |
| 123 | |||
| 124 | 10 | return 0; | |
| 125 | } | ||
| 126 | |||
| 127 | 28 | static int fib_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 128 | { | ||
| 129 | 28 | const struct nftnl_udata **ud = data; | |
| 130 | 28 | uint8_t type = nftnl_udata_type(attr); | |
| 131 | 28 | uint8_t len = nftnl_udata_len(attr); | |
| 132 | |||
| 133 |
1/2✓ Branch 0 (4→5) taken 28 times.
✗ Branch 1 (4→8) not taken.
|
28 | switch (type) { |
| 134 | 28 | case NFTNL_UDATA_FIB_RESULT: | |
| 135 | case NFTNL_UDATA_FIB_FLAGS: | ||
| 136 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 28 times.
|
28 | if (len != sizeof(uint32_t)) |
| 137 | ✗ | return -1; | |
| 138 | 28 | break; | |
| 139 | ✗ | default: | |
| 140 | ✗ | return 0; | |
| 141 | } | ||
| 142 | |||
| 143 | 28 | ud[type] = attr; | |
| 144 | 28 | return 0; | |
| 145 | } | ||
| 146 | |||
| 147 | 14 | static struct expr *fib_expr_parse_udata(const struct nftnl_udata *attr) | |
| 148 | { | ||
| 149 | 14 | const struct nftnl_udata *ud[NFTNL_UDATA_FIB_MAX + 1] = {}; | |
| 150 | uint32_t flags, result; | ||
| 151 | int err; | ||
| 152 | |||
| 153 | 14 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 154 | fib_parse_udata, ud); | ||
| 155 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 14 times.
|
14 | if (err < 0) |
| 156 | ✗ | return NULL; | |
| 157 | |||
| 158 |
1/2✓ Branch 0 (7→8) taken 14 times.
✗ Branch 1 (7→9) not taken.
|
14 | if (!ud[NFTNL_UDATA_FIB_RESULT] || |
| 159 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 14 times.
|
14 | !ud[NFTNL_UDATA_FIB_FLAGS]) |
| 160 | ✗ | return NULL; | |
| 161 | |||
| 162 | 14 | result = nftnl_udata_get_u32(ud[NFTNL_UDATA_FIB_RESULT]); | |
| 163 | 14 | flags = nftnl_udata_get_u32(ud[NFTNL_UDATA_FIB_FLAGS]); | |
| 164 | |||
| 165 | 14 | return fib_expr_alloc(&internal_location, flags, result); | |
| 166 | } | ||
| 167 | |||
| 168 | const struct expr_ops fib_expr_ops = { | ||
| 169 | .type = EXPR_FIB, | ||
| 170 | .name = "fib", | ||
| 171 | .print = fib_expr_print, | ||
| 172 | .json = fib_expr_json, | ||
| 173 | .cmp = fib_expr_cmp, | ||
| 174 | .clone = fib_expr_clone, | ||
| 175 | .parse_udata = fib_expr_parse_udata, | ||
| 176 | .build_udata = fib_expr_build_udata, | ||
| 177 | }; | ||
| 178 | |||
| 179 | 133 | struct expr *fib_expr_alloc(const struct location *loc, | |
| 180 | unsigned int flags, unsigned int result) | ||
| 181 | { | ||
| 182 | const struct datatype *type; | ||
| 183 | 133 | unsigned int len = 4 * BITS_PER_BYTE; | |
| 184 | struct expr *expr; | ||
| 185 | |||
| 186 |
3/4✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→4) taken 14 times.
✓ Branch 2 (2→5) taken 39 times.
✗ Branch 3 (2→6) not taken.
|
133 | switch (result) { |
| 187 | 80 | case NFT_FIB_RESULT_OIF: | |
| 188 | 80 | type = &ifindex_type; | |
| 189 | 80 | break; | |
| 190 | 14 | case NFT_FIB_RESULT_OIFNAME: | |
| 191 | 14 | type = &ifname_type; | |
| 192 | 14 | len = IFNAMSIZ * BITS_PER_BYTE; | |
| 193 | 14 | break; | |
| 194 | 39 | case NFT_FIB_RESULT_ADDRTYPE: | |
| 195 | 39 | type = &fib_addr_type; | |
| 196 | 39 | break; | |
| 197 | ✗ | default: | |
| 198 | ✗ | BUG("Unknown result %d", result); | |
| 199 | } | ||
| 200 | |||
| 201 |
2/2✓ Branch 0 (8→9) taken 52 times.
✓ Branch 1 (8→10) taken 81 times.
|
133 | if (flags & NFTA_FIB_F_PRESENT) { |
| 202 | 52 | type = &boolean_type; | |
| 203 | 52 | len = BITS_PER_BYTE; | |
| 204 | } | ||
| 205 | |||
| 206 | 133 | expr = expr_alloc(loc, EXPR_FIB, type, | |
| 207 | BYTEORDER_HOST_ENDIAN, len); | ||
| 208 | |||
| 209 | 133 | expr->fib.result = result; | |
| 210 | 133 | expr->fib.flags = flags; | |
| 211 | |||
| 212 | 133 | return expr; | |
| 213 | } | ||
| 214 |