| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Exthdr expression protocol and type definitions and related functions. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | ||
| 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 as | ||
| 8 | * published by the Free Software Foundation. | ||
| 9 | * | ||
| 10 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <nft.h> | ||
| 14 | |||
| 15 | #include <stddef.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <netinet/in.h> | ||
| 18 | #include <netinet/ip6.h> | ||
| 19 | |||
| 20 | #include <utils.h> | ||
| 21 | #include <headers.h> | ||
| 22 | #include <expression.h> | ||
| 23 | #include <statement.h> | ||
| 24 | #include <sctp_chunk.h> | ||
| 25 | |||
| 26 | static const struct exthdr_desc *exthdr_definitions[PROTO_DESC_MAX + 1] = { | ||
| 27 | [EXTHDR_DESC_HBH] = &exthdr_hbh, | ||
| 28 | [EXTHDR_DESC_RT] = &exthdr_rt, | ||
| 29 | [EXTHDR_DESC_RT0] = &exthdr_rt0, | ||
| 30 | [EXTHDR_DESC_RT2] = &exthdr_rt2, | ||
| 31 | [EXTHDR_DESC_SRH] = &exthdr_rt4, | ||
| 32 | [EXTHDR_DESC_FRAG] = &exthdr_frag, | ||
| 33 | [EXTHDR_DESC_DST] = &exthdr_dst, | ||
| 34 | [EXTHDR_DESC_MH] = &exthdr_mh, | ||
| 35 | }; | ||
| 36 | |||
| 37 | 442 | static const struct exthdr_desc *exthdr_find_desc(enum exthdr_desc_id desc_id) | |
| 38 | { | ||
| 39 |
1/2✓ Branch 0 (2→3) taken 442 times.
✗ Branch 1 (2→4) not taken.
|
442 | if (desc_id >= EXTHDR_DESC_UNKNOWN && |
| 40 | desc_id <= EXTHDR_DESC_MAX) | ||
| 41 | 442 | return exthdr_definitions[desc_id]; | |
| 42 | |||
| 43 | ✗ | return NULL; | |
| 44 | } | ||
| 45 | |||
| 46 | 656 | static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 47 | { | ||
| 48 | 13 | const char *name = expr->exthdr.desc ? | |
| 49 |
2/2✓ Branch 0 (2→3) taken 643 times.
✓ Branch 1 (2→4) taken 13 times.
|
656 | expr->exthdr.desc->name : "unknown-exthdr"; |
| 50 | |||
| 51 |
2/2✓ Branch 0 (5→6) taken 149 times.
✓ Branch 1 (5→19) taken 507 times.
|
656 | if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) { |
| 52 | /* Offset calculation is a bit hacky at this point. | ||
| 53 | * There might be a tcp option one day with another | ||
| 54 | * multiplicator | ||
| 55 | */ | ||
| 56 | 149 | unsigned int offset = expr->exthdr.offset / 64; | |
| 57 | |||
| 58 |
2/2✓ Branch 0 (6→7) taken 13 times.
✓ Branch 1 (6→13) taken 136 times.
|
149 | if (expr->exthdr.desc == NULL) { |
| 59 |
2/2✓ Branch 0 (7→8) taken 9 times.
✓ Branch 1 (7→11) taken 4 times.
|
13 | if (expr->exthdr.offset == 0 && |
| 60 |
1/2✓ Branch 0 (8→9) taken 9 times.
✗ Branch 1 (8→11) not taken.
|
9 | expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) { |
| 61 | 9 | nft_print(octx, "tcp option %d", expr->exthdr.raw_type); | |
| 62 | 9 | return; | |
| 63 | } | ||
| 64 | |||
| 65 | 4 | nft_print(octx, "tcp option @%u,%u,%u", expr->exthdr.raw_type, | |
| 66 | 4 | expr->exthdr.offset, expr->len); | |
| 67 | 4 | return; | |
| 68 | } | ||
| 69 | |||
| 70 | 136 | nft_print(octx, "tcp option %s", name); | |
| 71 |
2/2✓ Branch 0 (14→15) taken 44 times.
✓ Branch 1 (14→16) taken 92 times.
|
136 | if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) |
| 72 | 44 | return; | |
| 73 |
2/2✓ Branch 0 (16→17) taken 18 times.
✓ Branch 1 (16→18) taken 74 times.
|
92 | if (offset) |
| 74 | 18 | nft_print(octx, "%d", offset); | |
| 75 | 92 | nft_print(octx, " %s", expr->exthdr.tmpl->token); | |
| 76 |
2/2✓ Branch 0 (19→20) taken 4 times.
✓ Branch 1 (19→24) taken 503 times.
|
507 | } else if (expr->exthdr.op == NFT_EXTHDR_OP_IPV4) { |
| 77 | 4 | nft_print(octx, "ip option %s", name); | |
| 78 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 4 times.
|
4 | if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) |
| 79 | ✗ | return; | |
| 80 | 4 | nft_print(octx, " %s", expr->exthdr.tmpl->token); | |
| 81 |
2/2✓ Branch 0 (24→25) taken 163 times.
✓ Branch 1 (24→29) taken 340 times.
|
503 | } else if (expr->exthdr.op == NFT_EXTHDR_OP_SCTP) { |
| 82 | 163 | nft_print(octx, "sctp chunk %s", expr->exthdr.desc->name); | |
| 83 |
2/2✓ Branch 0 (26→27) taken 90 times.
✓ Branch 1 (26→28) taken 73 times.
|
163 | if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) |
| 84 | 90 | return; | |
| 85 | 73 | nft_print(octx, " %s", expr->exthdr.tmpl->token); | |
| 86 |
1/2✗ Branch 0 (29→30) not taken.
✓ Branch 1 (29→32) taken 340 times.
|
340 | } else if (expr->exthdr.op == NFT_EXTHDR_OP_DCCP) { |
| 87 | ✗ | nft_print(octx, "dccp option %d", expr->exthdr.raw_type); | |
| 88 | ✗ | return; | |
| 89 | } else { | ||
| 90 |
2/2✓ Branch 0 (32→33) taken 13 times.
✓ Branch 1 (32→34) taken 327 times.
|
340 | if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) |
| 91 | 13 | nft_print(octx, "exthdr %s", name); | |
| 92 | else { | ||
| 93 | 327 | nft_print(octx, "%s %s", name, | |
| 94 | 327 | expr->exthdr.tmpl->token); | |
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | ✗ | static bool exthdr_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 100 | { | ||
| 101 | ✗ | return e1->exthdr.desc == e2->exthdr.desc && | |
| 102 | ✗ | e1->exthdr.tmpl == e2->exthdr.tmpl && | |
| 103 | ✗ | e1->exthdr.op == e2->exthdr.op && | |
| 104 | ✗ | e1->exthdr.raw_type == e2->exthdr.raw_type && | |
| 105 | ✗ | e1->exthdr.flags == e2->exthdr.flags; | |
| 106 | } | ||
| 107 | |||
| 108 | 1359 | static void exthdr_expr_clone(struct expr *new, const struct expr *expr) | |
| 109 | { | ||
| 110 | 1359 | new->exthdr.desc = expr->exthdr.desc; | |
| 111 | 1359 | new->exthdr.tmpl = expr->exthdr.tmpl; | |
| 112 | 1359 | new->exthdr.offset = expr->exthdr.offset; | |
| 113 | 1359 | new->exthdr.op = expr->exthdr.op; | |
| 114 | 1359 | new->exthdr.flags = expr->exthdr.flags; | |
| 115 | 1359 | new->exthdr.raw_type = expr->exthdr.raw_type; | |
| 116 | 1359 | } | |
| 117 | |||
| 118 | #define NFTNL_UDATA_EXTHDR_DESC 0 | ||
| 119 | #define NFTNL_UDATA_EXTHDR_TYPE 1 | ||
| 120 | #define NFTNL_UDATA_EXTHDR_OP 2 | ||
| 121 | #define NFTNL_UDATA_EXTHDR_MAX 3 | ||
| 122 | |||
| 123 | 983 | static int exthdr_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 124 | { | ||
| 125 | 983 | const struct nftnl_udata **ud = data; | |
| 126 | 983 | uint8_t type = nftnl_udata_type(attr); | |
| 127 | 983 | uint8_t len = nftnl_udata_len(attr); | |
| 128 | |||
| 129 |
1/2✓ Branch 0 (4→5) taken 983 times.
✗ Branch 1 (4→8) not taken.
|
983 | switch (type) { |
| 130 | 983 | case NFTNL_UDATA_EXTHDR_DESC: | |
| 131 | case NFTNL_UDATA_EXTHDR_TYPE: | ||
| 132 | case NFTNL_UDATA_EXTHDR_OP: | ||
| 133 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 983 times.
|
983 | if (len != sizeof(uint32_t)) |
| 134 | ✗ | return -1; | |
| 135 | 983 | break; | |
| 136 | ✗ | default: | |
| 137 | ✗ | return 0; | |
| 138 | } | ||
| 139 | |||
| 140 | 983 | ud[type] = attr; | |
| 141 | 983 | return 0; | |
| 142 | } | ||
| 143 | |||
| 144 | 475 | static struct expr *exthdr_expr_parse_udata(const struct nftnl_udata *attr) | |
| 145 | { | ||
| 146 | 475 | const struct nftnl_udata *ud[NFTNL_UDATA_EXTHDR_MAX + 1] = {}; | |
| 147 | 475 | enum nft_exthdr_op op = NFT_EXTHDR_OP_IPV6; | |
| 148 | const struct exthdr_desc *desc; | ||
| 149 | unsigned int type; | ||
| 150 | uint32_t desc_id; | ||
| 151 | int err; | ||
| 152 | |||
| 153 | 475 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 154 | exthdr_parse_udata, ud); | ||
| 155 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 475 times.
|
475 | if (err < 0) |
| 156 | ✗ | return NULL; | |
| 157 | |||
| 158 |
1/2✓ Branch 0 (7→8) taken 475 times.
✗ Branch 1 (7→9) not taken.
|
475 | if (!ud[NFTNL_UDATA_EXTHDR_DESC] || |
| 159 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 475 times.
|
475 | !ud[NFTNL_UDATA_EXTHDR_TYPE]) |
| 160 | ✗ | return NULL; | |
| 161 | |||
| 162 |
2/2✓ Branch 0 (10→11) taken 33 times.
✓ Branch 1 (10→12) taken 442 times.
|
475 | if (ud[NFTNL_UDATA_EXTHDR_OP]) |
| 163 | 33 | op = nftnl_udata_get_u32(ud[NFTNL_UDATA_EXTHDR_OP]); | |
| 164 | |||
| 165 | 475 | desc_id = nftnl_udata_get_u32(ud[NFTNL_UDATA_EXTHDR_DESC]); | |
| 166 | 475 | type = nftnl_udata_get_u32(ud[NFTNL_UDATA_EXTHDR_TYPE]); | |
| 167 | |||
| 168 |
4/7✓ Branch 0 (14→15) taken 442 times.
✓ Branch 1 (14→17) taken 27 times.
✓ Branch 2 (14→18) taken 3 times.
✓ Branch 3 (14→19) taken 3 times.
✗ Branch 4 (14→20) not taken.
✗ Branch 5 (14→21) not taken.
✗ Branch 6 (14→22) not taken.
|
475 | switch (op) { |
| 169 | 442 | case NFT_EXTHDR_OP_IPV6: | |
| 170 | 442 | desc = exthdr_find_desc(desc_id); | |
| 171 | |||
| 172 | 442 | return exthdr_expr_alloc(&internal_location, desc, type); | |
| 173 | 27 | case NFT_EXTHDR_OP_TCPOPT: | |
| 174 | 27 | return tcpopt_expr_alloc(&internal_location, | |
| 175 | desc_id, type); | ||
| 176 | 3 | case NFT_EXTHDR_OP_IPV4: | |
| 177 | 3 | return ipopt_expr_alloc(&internal_location, | |
| 178 | desc_id, type); | ||
| 179 | 3 | case NFT_EXTHDR_OP_SCTP: | |
| 180 | 3 | return sctp_chunk_expr_alloc(&internal_location, | |
| 181 | desc_id, type); | ||
| 182 | ✗ | case NFT_EXTHDR_OP_DCCP: | |
| 183 | ✗ | return dccpopt_expr_alloc(&internal_location, type); | |
| 184 | ✗ | case __NFT_EXTHDR_OP_MAX: | |
| 185 | ✗ | return NULL; | |
| 186 | } | ||
| 187 | |||
| 188 | ✗ | return NULL; | |
| 189 | } | ||
| 190 | |||
| 191 | 237 | static unsigned int expr_exthdr_type(const struct exthdr_desc *desc, | |
| 192 | const struct proto_hdr_template *tmpl) | ||
| 193 | { | ||
| 194 | 237 | return (unsigned int)(tmpl - &desc->templates[0]); | |
| 195 | } | ||
| 196 | |||
| 197 | 237 | static int exthdr_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 198 | const struct expr *expr) | ||
| 199 | { | ||
| 200 | 237 | const struct proto_hdr_template *tmpl = expr->exthdr.tmpl; | |
| 201 | 237 | const struct exthdr_desc *desc = expr->exthdr.desc; | |
| 202 | 237 | unsigned int type = expr_exthdr_type(desc, tmpl); | |
| 203 | 237 | enum nft_exthdr_op op = expr->exthdr.op; | |
| 204 | |||
| 205 | 237 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_EXTHDR_TYPE, type); | |
| 206 |
2/3✓ Branch 0 (4→5) taken 211 times.
✓ Branch 1 (4→6) taken 26 times.
✗ Branch 2 (4→9) not taken.
|
237 | switch (op) { |
| 207 | 211 | case NFT_EXTHDR_OP_IPV6: | |
| 208 | 211 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_EXTHDR_DESC, desc->id); | |
| 209 | 211 | break; | |
| 210 | 26 | case NFT_EXTHDR_OP_TCPOPT: | |
| 211 | case NFT_EXTHDR_OP_IPV4: | ||
| 212 | case NFT_EXTHDR_OP_SCTP: | ||
| 213 | case NFT_EXTHDR_OP_DCCP: | ||
| 214 | 26 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_EXTHDR_OP, op); | |
| 215 | 26 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_EXTHDR_DESC, expr->exthdr.raw_type); | |
| 216 | 26 | break; | |
| 217 | ✗ | default: | |
| 218 | ✗ | return -1; | |
| 219 | } | ||
| 220 | |||
| 221 | 237 | return 0; | |
| 222 | } | ||
| 223 | |||
| 224 | const struct expr_ops exthdr_expr_ops = { | ||
| 225 | .type = EXPR_EXTHDR, | ||
| 226 | .name = "exthdr", | ||
| 227 | .print = exthdr_expr_print, | ||
| 228 | .json = exthdr_expr_json, | ||
| 229 | .cmp = exthdr_expr_cmp, | ||
| 230 | .clone = exthdr_expr_clone, | ||
| 231 | .build_udata = exthdr_expr_build_udata, | ||
| 232 | .parse_udata = exthdr_expr_parse_udata, | ||
| 233 | }; | ||
| 234 | |||
| 235 | static const struct proto_hdr_template exthdr_unknown_template = | ||
| 236 | PROTO_HDR_TEMPLATE("unknown", &invalid_type, BYTEORDER_INVALID, 0, 0); | ||
| 237 | |||
| 238 | 2423 | struct expr *exthdr_expr_alloc(const struct location *loc, | |
| 239 | const struct exthdr_desc *desc, | ||
| 240 | uint8_t type) | ||
| 241 | { | ||
| 242 | const struct proto_hdr_template *tmpl; | ||
| 243 | struct expr *expr; | ||
| 244 | |||
| 245 |
2/2✓ Branch 0 (2→3) taken 1162 times.
✓ Branch 1 (2→4) taken 1261 times.
|
2423 | if (desc != NULL) |
| 246 | 1162 | tmpl = &desc->templates[type]; | |
| 247 | else | ||
| 248 | 1261 | tmpl = &exthdr_unknown_template; | |
| 249 | |||
| 250 | 2423 | expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype, | |
| 251 | 2423 | BYTEORDER_BIG_ENDIAN, tmpl->len); | |
| 252 | 2423 | expr->exthdr.desc = desc; | |
| 253 |
2/2✓ Branch 0 (6→7) taken 1162 times.
✓ Branch 1 (6→8) taken 1261 times.
|
2423 | expr->exthdr.raw_type = desc ? desc->type : 0; |
| 254 | 2423 | expr->exthdr.tmpl = tmpl; | |
| 255 | 2423 | expr->exthdr.offset = tmpl->offset; | |
| 256 | 2423 | return expr; | |
| 257 | } | ||
| 258 | |||
| 259 | 7 | static void exthdr_stmt_print(const struct stmt *stmt, struct output_ctx *octx) | |
| 260 | { | ||
| 261 | 7 | expr_print(stmt->exthdr.expr, octx); | |
| 262 | 7 | nft_print(octx, " set "); | |
| 263 | 7 | expr_print(stmt->exthdr.val, octx); | |
| 264 | 7 | } | |
| 265 | |||
| 266 | 24 | static void exthdr_stmt_destroy(struct stmt *stmt) | |
| 267 | { | ||
| 268 | 24 | expr_free(stmt->exthdr.expr); | |
| 269 | 24 | expr_free(stmt->exthdr.val); | |
| 270 | 24 | } | |
| 271 | |||
| 272 | const struct stmt_ops exthdr_stmt_ops = { | ||
| 273 | .type = STMT_EXTHDR, | ||
| 274 | .name = "exthdr", | ||
| 275 | .print = exthdr_stmt_print, | ||
| 276 | .json = exthdr_stmt_json, | ||
| 277 | .destroy = exthdr_stmt_destroy, | ||
| 278 | }; | ||
| 279 | |||
| 280 | 24 | struct stmt *exthdr_stmt_alloc(const struct location *loc, | |
| 281 | struct expr *expr, struct expr *val) | ||
| 282 | { | ||
| 283 | struct stmt *stmt; | ||
| 284 | |||
| 285 | 24 | stmt = stmt_alloc(loc, &exthdr_stmt_ops); | |
| 286 | 24 | stmt->exthdr.expr = expr; | |
| 287 | 24 | stmt->exthdr.val = val; | |
| 288 | 24 | return stmt; | |
| 289 | } | ||
| 290 | |||
| 291 | static const struct exthdr_desc *exthdr_protocols[UINT8_MAX + 1] = { | ||
| 292 | [IPPROTO_HOPOPTS] = &exthdr_hbh, | ||
| 293 | [IPPROTO_ROUTING] = &exthdr_rt, | ||
| 294 | [IPPROTO_FRAGMENT] = &exthdr_frag, | ||
| 295 | [IPPROTO_DSTOPTS] = &exthdr_dst, | ||
| 296 | [IPPROTO_MH] = &exthdr_mh, | ||
| 297 | }; | ||
| 298 | |||
| 299 | 16 | const struct exthdr_desc *exthdr_find_proto(uint8_t proto) | |
| 300 | { | ||
| 301 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 16 times.
|
16 | assert(exthdr_protocols[proto]); |
| 302 | |||
| 303 | 16 | return exthdr_protocols[proto]; | |
| 304 | } | ||
| 305 | |||
| 306 | static const struct proto_hdr_template * | ||
| 307 | 22 | exthdr_rt_find(struct expr *expr, const struct exthdr_desc *desc) | |
| 308 | { | ||
| 309 | const struct proto_hdr_template *tmpl; | ||
| 310 | unsigned int i; | ||
| 311 | |||
| 312 |
1/2✓ Branch 0 (7→3) taken 76 times.
✗ Branch 1 (7→8) not taken.
|
76 | for (i = 0; i < array_size(desc->templates); i++) { |
| 313 | 76 | tmpl = &desc->templates[i]; | |
| 314 |
2/2✓ Branch 0 (3→4) taken 22 times.
✓ Branch 1 (3→6) taken 54 times.
|
76 | if (tmpl->offset == expr->exthdr.offset && |
| 315 |
1/2✓ Branch 0 (4→5) taken 22 times.
✗ Branch 1 (4→6) not taken.
|
22 | tmpl->len == expr->len) { |
| 316 | 22 | expr->exthdr.desc = desc; | |
| 317 | 22 | return tmpl; | |
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | ✗ | return NULL; | |
| 322 | } | ||
| 323 | |||
| 324 | 1336 | void exthdr_init_raw(struct expr *expr, uint8_t type, | |
| 325 | unsigned int offset, unsigned int len, | ||
| 326 | enum nft_exthdr_op op, uint32_t flags) | ||
| 327 | { | ||
| 328 | 1336 | const struct proto_hdr_template *tmpl = &exthdr_unknown_template; | |
| 329 | unsigned int i; | ||
| 330 | |||
| 331 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1336 times.
|
1336 | assert(expr->etype == EXPR_EXTHDR); |
| 332 | 1336 | expr->exthdr.raw_type = type; | |
| 333 | |||
| 334 |
2/2✓ Branch 0 (4→5) taken 278 times.
✓ Branch 1 (4→7) taken 1058 times.
|
1336 | if (op == NFT_EXTHDR_OP_TCPOPT) |
| 335 | 278 | return tcpopt_init_raw(expr, type, offset, len, flags); | |
| 336 |
2/2✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→10) taken 1055 times.
|
1058 | if (op == NFT_EXTHDR_OP_IPV4) |
| 337 | 3 | return ipopt_init_raw(expr, type, offset, len, flags, true); | |
| 338 |
2/2✓ Branch 0 (10→11) taken 317 times.
✓ Branch 1 (10→13) taken 738 times.
|
1055 | if (op == NFT_EXTHDR_OP_SCTP) |
| 339 | 317 | return sctp_chunk_init_raw(expr, type, offset, len, flags); | |
| 340 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 738 times.
|
738 | if (op == NFT_EXTHDR_OP_DCCP) |
| 341 | ✗ | return dccpopt_init_raw(expr, type, offset, len); | |
| 342 | |||
| 343 | 738 | expr->len = len; | |
| 344 | 738 | expr->exthdr.flags = flags; | |
| 345 | 738 | expr->exthdr.offset = offset; | |
| 346 | 738 | expr->exthdr.desc = NULL; | |
| 347 | |||
| 348 | 738 | expr->exthdr.desc = exthdr_protocols[type]; | |
| 349 | |||
| 350 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 738 times.
|
738 | if (expr->exthdr.desc == NULL) |
| 351 | ✗ | goto out; | |
| 352 | |||
| 353 |
2/2✓ Branch 0 (23→19) taken 3209 times.
✓ Branch 1 (23→24) taken 97 times.
|
3306 | for (i = 0; i < array_size(expr->exthdr.desc->templates); i++) { |
| 354 | 3209 | tmpl = &expr->exthdr.desc->templates[i]; | |
| 355 |
4/4✓ Branch 0 (19→20) taken 943 times.
✓ Branch 1 (19→22) taken 2266 times.
✓ Branch 2 (20→21) taken 641 times.
✓ Branch 3 (20→22) taken 302 times.
|
3209 | if (tmpl->offset == offset && tmpl->len == len) |
| 356 | 641 | goto out; | |
| 357 | } | ||
| 358 | |||
| 359 |
2/2✓ Branch 0 (24→25) taken 22 times.
✓ Branch 1 (24→34) taken 75 times.
|
97 | if (expr->exthdr.desc == &exthdr_rt) { |
| 360 | 22 | tmpl = exthdr_rt_find(expr, &exthdr_rt4); | |
| 361 |
1/2✓ Branch 0 (26→27) taken 22 times.
✗ Branch 1 (26→28) not taken.
|
22 | if (tmpl) |
| 362 | 22 | goto out; | |
| 363 | ✗ | tmpl = exthdr_rt_find(expr, &exthdr_rt0); | |
| 364 | ✗ | if (tmpl) | |
| 365 | ✗ | goto out; | |
| 366 | ✗ | tmpl = exthdr_rt_find(expr, &exthdr_rt2); | |
| 367 | ✗ | if (tmpl) | |
| 368 | ✗ | goto out; | |
| 369 | } | ||
| 370 | |||
| 371 | 75 | tmpl = &exthdr_unknown_template; | |
| 372 | 738 | out: | |
| 373 | 738 | expr->exthdr.tmpl = tmpl; | |
| 374 |
2/2✓ Branch 0 (35→36) taken 25 times.
✓ Branch 1 (35→37) taken 713 times.
|
738 | if (flags & NFT_EXTHDR_F_PRESENT) |
| 375 | 25 | datatype_set(expr, &boolean_type); | |
| 376 | else | ||
| 377 | 713 | datatype_set(expr, tmpl->dtype); | |
| 378 | } | ||
| 379 | |||
| 380 | 105 | static unsigned int mask_length(const struct expr *mask) | |
| 381 | { | ||
| 382 | 105 | unsigned long off = mpz_scan1(mask->value, 0); | |
| 383 | |||
| 384 | 105 | return mpz_scan0(mask->value, off + 1); | |
| 385 | } | ||
| 386 | |||
| 387 | 105 | bool exthdr_find_template(struct expr *expr, const struct expr *mask, unsigned int *shift) | |
| 388 | { | ||
| 389 | unsigned int off, mask_offset, mask_len; | ||
| 390 | bool found; | ||
| 391 | |||
| 392 | 105 | mask_offset = mpz_scan1(mask->value, 0); | |
| 393 | 105 | mask_len = mask_length(mask); | |
| 394 | |||
| 395 | 105 | off = expr->exthdr.offset; | |
| 396 | 105 | off += round_up(mask->len, BITS_PER_BYTE) - mask_len; | |
| 397 | |||
| 398 | /* Handle ip options after the offset and mask have been calculated. */ | ||
| 399 |
2/4✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 30 times.
✓ Branch 2 (3→8) taken 75 times.
✗ Branch 3 (3→12) not taken.
|
105 | switch (expr->exthdr.op) { |
| 400 | ✗ | case NFT_EXTHDR_OP_IPV4: | |
| 401 | ✗ | found = ipopt_find_template(expr, off, mask_len - mask_offset); | |
| 402 | ✗ | break; | |
| 403 | 30 | case NFT_EXTHDR_OP_TCPOPT: | |
| 404 | 30 | found = tcpopt_find_template(expr, off, mask_len - mask_offset); | |
| 405 | 30 | break; | |
| 406 | 75 | case NFT_EXTHDR_OP_IPV6: | |
| 407 | 75 | exthdr_init_raw(expr, expr->exthdr.raw_type, | |
| 408 | off, mask_len - mask_offset, expr->exthdr.op, 0); | ||
| 409 | |||
| 410 | /* still failed to find a template... Bug. */ | ||
| 411 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 75 times.
|
75 | if (expr->exthdr.tmpl == &exthdr_unknown_template) |
| 412 | ✗ | return false; | |
| 413 | 75 | found = true; | |
| 414 | 75 | break; | |
| 415 | ✗ | default: | |
| 416 | ✗ | found = false; | |
| 417 | ✗ | break; | |
| 418 | } | ||
| 419 | |||
| 420 |
1/2✓ Branch 0 (13→14) taken 105 times.
✗ Branch 1 (13→15) not taken.
|
105 | if (found) |
| 421 | 105 | *shift = mask_offset; | |
| 422 | |||
| 423 | 105 | return found; | |
| 424 | } | ||
| 425 | |||
| 426 | #define HDR_TEMPLATE(__name, __dtype, __type, __member) \ | ||
| 427 | PROTO_HDR_TEMPLATE(__name, __dtype, \ | ||
| 428 | BYTEORDER_BIG_ENDIAN, \ | ||
| 429 | offsetof(__type, __member) * 8, \ | ||
| 430 | field_sizeof(__type, __member) * 8) | ||
| 431 | |||
| 432 | /* | ||
| 433 | * Hop-by-hop options | ||
| 434 | */ | ||
| 435 | |||
| 436 | #define HBH_FIELD(__name, __member, __dtype) \ | ||
| 437 | HDR_TEMPLATE(__name, __dtype, struct ip6_hbh, __member) | ||
| 438 | |||
| 439 | const struct exthdr_desc exthdr_hbh = { | ||
| 440 | .name = "hbh", | ||
| 441 | .id = EXTHDR_DESC_HBH, | ||
| 442 | .type = IPPROTO_HOPOPTS, | ||
| 443 | .templates = { | ||
| 444 | [HBHHDR_NEXTHDR] = HBH_FIELD("nexthdr", ip6h_nxt, &inet_protocol_type), | ||
| 445 | [HBHHDR_HDRLENGTH] = HBH_FIELD("hdrlength", ip6h_len, &integer_type), | ||
| 446 | }, | ||
| 447 | }; | ||
| 448 | |||
| 449 | /* | ||
| 450 | * Routing header | ||
| 451 | */ | ||
| 452 | |||
| 453 | /* similar to uapi/linux/ipv6.h */ | ||
| 454 | struct ip6_rt2_hdr { | ||
| 455 | struct ip6_rthdr rt_hdr; | ||
| 456 | uint32_t reserved; | ||
| 457 | struct in6_addr addr; | ||
| 458 | }; | ||
| 459 | |||
| 460 | #define RT2_FIELD(__name, __member, __dtype) \ | ||
| 461 | HDR_TEMPLATE(__name, __dtype, struct ip6_rt2_hdr, __member) | ||
| 462 | |||
| 463 | const struct exthdr_desc exthdr_rt2 = { | ||
| 464 | .name = "rt2", | ||
| 465 | .id = EXTHDR_DESC_RT2, | ||
| 466 | .type = IPPROTO_ROUTING, | ||
| 467 | .templates = { | ||
| 468 | [RT2HDR_RESERVED] = RT2_FIELD("reserved", reserved, &integer_type), | ||
| 469 | [RT2HDR_ADDR] = RT2_FIELD("addr", addr, &ip6addr_type), | ||
| 470 | }, | ||
| 471 | }; | ||
| 472 | |||
| 473 | #define RT0_FIELD(__name, __member, __dtype) \ | ||
| 474 | HDR_TEMPLATE(__name, __dtype, struct ip6_rthdr0, __member) | ||
| 475 | |||
| 476 | const struct exthdr_desc exthdr_rt0 = { | ||
| 477 | .name = "rt0", | ||
| 478 | .id = EXTHDR_DESC_RT0, | ||
| 479 | .type = IPPROTO_ROUTING, | ||
| 480 | .templates = { | ||
| 481 | [RT0HDR_RESERVED] = RT0_FIELD("reserved", ip6r0_reserved, &integer_type), | ||
| 482 | [RT0HDR_ADDR_1] = RT0_FIELD("addr[1]", ip6r0_addr[0], &ip6addr_type), | ||
| 483 | [RT0HDR_ADDR_1 + 1] = RT0_FIELD("addr[2]", ip6r0_addr[1], &ip6addr_type), | ||
| 484 | // ... | ||
| 485 | }, | ||
| 486 | }; | ||
| 487 | |||
| 488 | #define RT4_FIELD(__name, __member, __dtype) \ | ||
| 489 | HDR_TEMPLATE(__name, __dtype, struct ip6_rt4, __member) | ||
| 490 | |||
| 491 | const struct exthdr_desc exthdr_rt4 = { | ||
| 492 | .name = "srh", | ||
| 493 | .id = EXTHDR_DESC_SRH, | ||
| 494 | .type = IPPROTO_ROUTING, | ||
| 495 | .templates = { | ||
| 496 | [RT4HDR_LASTENT] = RT4_FIELD("last-entry", ip6r4_last_entry, &integer_type), | ||
| 497 | [RT4HDR_FLAGS] = RT4_FIELD("flags", ip6r4_flags, &integer_type), | ||
| 498 | [RT4HDR_TAG] = RT4_FIELD("tag", ip6r4_tag, &integer_type), | ||
| 499 | [RT4HDR_SID_1] = RT4_FIELD("sid[1]", ip6r4_segments[0], &ip6addr_type), | ||
| 500 | [RT4HDR_SID_1 + 1] = RT4_FIELD("sid[2]", ip6r4_segments[1], &ip6addr_type), | ||
| 501 | // ... | ||
| 502 | }, | ||
| 503 | }; | ||
| 504 | |||
| 505 | |||
| 506 | #define RT_FIELD(__name, __member, __dtype) \ | ||
| 507 | HDR_TEMPLATE(__name, __dtype, struct ip6_rthdr, __member) | ||
| 508 | |||
| 509 | const struct exthdr_desc exthdr_rt = { | ||
| 510 | .name = "rt", | ||
| 511 | .id = EXTHDR_DESC_RT, | ||
| 512 | .type = IPPROTO_ROUTING, | ||
| 513 | #if 0 | ||
| 514 | .protocol_key = RTHDR_TYPE, | ||
| 515 | .protocols = { | ||
| 516 | [0] = &exthdr_rt0, | ||
| 517 | [2] = &exthdr_rt2, | ||
| 518 | }, | ||
| 519 | #endif | ||
| 520 | .templates = { | ||
| 521 | [RTHDR_NEXTHDR] = RT_FIELD("nexthdr", ip6r_nxt, &inet_protocol_type), | ||
| 522 | [RTHDR_HDRLENGTH] = RT_FIELD("hdrlength", ip6r_len, &integer_type), | ||
| 523 | [RTHDR_TYPE] = RT_FIELD("type", ip6r_type, &integer_type), | ||
| 524 | [RTHDR_SEG_LEFT] = RT_FIELD("seg-left", ip6r_segleft, &integer_type), | ||
| 525 | }, | ||
| 526 | }; | ||
| 527 | |||
| 528 | /* | ||
| 529 | * Fragment header | ||
| 530 | */ | ||
| 531 | |||
| 532 | #define FRAG_FIELD(__name, __member, __dtype) \ | ||
| 533 | HDR_TEMPLATE(__name, __dtype, struct ip6_frag, __member) | ||
| 534 | |||
| 535 | const struct exthdr_desc exthdr_frag = { | ||
| 536 | .name = "frag", | ||
| 537 | .id = EXTHDR_DESC_FRAG, | ||
| 538 | .type = IPPROTO_FRAGMENT, | ||
| 539 | .templates = { | ||
| 540 | [FRAGHDR_NEXTHDR] = FRAG_FIELD("nexthdr", ip6f_nxt, &inet_protocol_type), | ||
| 541 | [FRAGHDR_RESERVED] = FRAG_FIELD("reserved", ip6f_reserved, &integer_type), | ||
| 542 | [FRAGHDR_FRAG_OFF] = PROTO_HDR_TEMPLATE("frag-off", &integer_type, | ||
| 543 | BYTEORDER_BIG_ENDIAN, | ||
| 544 | 16, 13), | ||
| 545 | [FRAGHDR_RESERVED2] = PROTO_HDR_TEMPLATE("reserved2", &integer_type, | ||
| 546 | BYTEORDER_BIG_ENDIAN, | ||
| 547 | 29, 2), | ||
| 548 | [FRAGHDR_MFRAGS] = PROTO_HDR_TEMPLATE("more-fragments", &integer_type, | ||
| 549 | BYTEORDER_BIG_ENDIAN, | ||
| 550 | 31, 1), | ||
| 551 | [FRAGHDR_ID] = FRAG_FIELD("id", ip6f_ident, &integer_type), | ||
| 552 | }, | ||
| 553 | }; | ||
| 554 | |||
| 555 | /* | ||
| 556 | * DST options | ||
| 557 | */ | ||
| 558 | |||
| 559 | #define DST_FIELD(__name, __member, __dtype) \ | ||
| 560 | HDR_TEMPLATE(__name, __dtype, struct ip6_dest, __member) | ||
| 561 | |||
| 562 | const struct exthdr_desc exthdr_dst = { | ||
| 563 | .name = "dst", | ||
| 564 | .id = EXTHDR_DESC_DST, | ||
| 565 | .type = IPPROTO_DSTOPTS, | ||
| 566 | .templates = { | ||
| 567 | [DSTHDR_NEXTHDR] = DST_FIELD("nexthdr", ip6d_nxt, &inet_protocol_type), | ||
| 568 | [DSTHDR_HDRLENGTH] = DST_FIELD("hdrlength", ip6d_len, &integer_type), | ||
| 569 | }, | ||
| 570 | }; | ||
| 571 | |||
| 572 | /* | ||
| 573 | * Mobility header | ||
| 574 | */ | ||
| 575 | |||
| 576 | #define MH_FIELD(__name, __member, __dtype) \ | ||
| 577 | HDR_TEMPLATE(__name, __dtype, struct ip6_mh, __member) | ||
| 578 | |||
| 579 | static const struct symbol_table mh_type_tbl = { | ||
| 580 | .base = BASE_DECIMAL, | ||
| 581 | .symbols = { | ||
| 582 | SYMBOL("binding-refresh-request", IP6_MH_TYPE_BRR), | ||
| 583 | SYMBOL("home-test-init", IP6_MH_TYPE_HOTI), | ||
| 584 | SYMBOL("careof-test-init", IP6_MH_TYPE_COTI), | ||
| 585 | SYMBOL("home-test", IP6_MH_TYPE_HOT), | ||
| 586 | SYMBOL("careof-test", IP6_MH_TYPE_COT), | ||
| 587 | SYMBOL("binding-update", IP6_MH_TYPE_BU), | ||
| 588 | SYMBOL("binding-acknowledgement", IP6_MH_TYPE_BACK), | ||
| 589 | SYMBOL("binding-error", IP6_MH_TYPE_BERROR), | ||
| 590 | SYMBOL("fast-binding-update", IP6_MH_TYPE_FBU), | ||
| 591 | SYMBOL("fast-binding-acknowledgement", IP6_MH_TYPE_FBACK), | ||
| 592 | SYMBOL("fast-binding-advertisement", IP6_MH_TYPE_FNA), | ||
| 593 | SYMBOL("experimental-mobility-header", IP6_MH_TYPE_EMH), | ||
| 594 | SYMBOL("home-agent-switch-message", IP6_MH_TYPE_HASM), | ||
| 595 | SYMBOL_LIST_END | ||
| 596 | }, | ||
| 597 | }; | ||
| 598 | |||
| 599 | const struct datatype mh_type_type = { | ||
| 600 | .type = TYPE_MH_TYPE, | ||
| 601 | .name = "mh_type", | ||
| 602 | .desc = "Mobility Header Type", | ||
| 603 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 604 | .size = BITS_PER_BYTE, | ||
| 605 | .basetype = &integer_type, | ||
| 606 | .sym_tbl = &mh_type_tbl, | ||
| 607 | }; | ||
| 608 | |||
| 609 | const struct exthdr_desc exthdr_mh = { | ||
| 610 | .name = "mh", | ||
| 611 | .id = EXTHDR_DESC_MH, | ||
| 612 | .type = IPPROTO_MH, | ||
| 613 | .templates = { | ||
| 614 | [MHHDR_NEXTHDR] = MH_FIELD("nexthdr", ip6mh_proto, &inet_protocol_type), | ||
| 615 | [MHHDR_HDRLENGTH] = MH_FIELD("hdrlength", ip6mh_hdrlen, &integer_type), | ||
| 616 | [MHHDR_TYPE] = MH_FIELD("type", ip6mh_type, &mh_type_type), | ||
| 617 | [MHHDR_RESERVED] = MH_FIELD("reserved", ip6mh_reserved, &integer_type), | ||
| 618 | [MHHDR_CHECKSUM] = MH_FIELD("checksum", ip6mh_cksum, &integer_type), | ||
| 619 | }, | ||
| 620 | }; | ||
| 621 |