| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <nft.h> | ||
| 2 | |||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <netinet/in.h> | ||
| 6 | #include <netinet/ip6.h> | ||
| 7 | #include <netinet/tcp.h> | ||
| 8 | |||
| 9 | #include <utils.h> | ||
| 10 | #include <headers.h> | ||
| 11 | #include <expression.h> | ||
| 12 | #include <tcpopt.h> | ||
| 13 | |||
| 14 | static const struct proto_hdr_template tcpopt_unknown_template = | ||
| 15 | PROTO_HDR_TEMPLATE("unknown", &invalid_type, BYTEORDER_INVALID, 0, 0); | ||
| 16 | |||
| 17 | #define PHT(__token, __offset, __len) \ | ||
| 18 | PROTO_HDR_TEMPLATE(__token, &integer_type, BYTEORDER_BIG_ENDIAN, \ | ||
| 19 | __offset, __len) | ||
| 20 | static const struct exthdr_desc tcpopt_eol = { | ||
| 21 | .name = "eol", | ||
| 22 | .type = TCPOPT_KIND_EOL, | ||
| 23 | .templates = { | ||
| 24 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 25 | }, | ||
| 26 | }; | ||
| 27 | |||
| 28 | static const struct exthdr_desc tcpopt_nop = { | ||
| 29 | .name = "nop", | ||
| 30 | .type = TCPOPT_KIND_NOP, | ||
| 31 | .templates = { | ||
| 32 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | |||
| 36 | static const struct exthdr_desc tcptopt_maxseg = { | ||
| 37 | .name = "maxseg", | ||
| 38 | .type = TCPOPT_KIND_MAXSEG, | ||
| 39 | .templates = { | ||
| 40 | [TCPOPT_MAXSEG_KIND] = PHT("kind", 0, 8), | ||
| 41 | [TCPOPT_MAXSEG_LENGTH] = PHT("length", 8, 8), | ||
| 42 | [TCPOPT_MAXSEG_SIZE] = PHT("size", 16, 16), | ||
| 43 | }, | ||
| 44 | }; | ||
| 45 | |||
| 46 | static const struct exthdr_desc tcpopt_window = { | ||
| 47 | .name = "window", | ||
| 48 | .type = TCPOPT_KIND_WINDOW, | ||
| 49 | .templates = { | ||
| 50 | [TCPOPT_WINDOW_KIND] = PHT("kind", 0, 8), | ||
| 51 | [TCPOPT_WINDOW_LENGTH] = PHT("length", 8, 8), | ||
| 52 | [TCPOPT_WINDOW_COUNT] = PHT("count", 16, 8), | ||
| 53 | }, | ||
| 54 | }; | ||
| 55 | |||
| 56 | static const struct exthdr_desc tcpopt_sack_permitted = { | ||
| 57 | .name = "sack-perm", | ||
| 58 | .type = TCPOPT_KIND_SACK_PERMITTED, | ||
| 59 | .templates = { | ||
| 60 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 61 | [TCPOPT_COMMON_LENGTH] = PHT("length", 8, 8), | ||
| 62 | }, | ||
| 63 | }; | ||
| 64 | |||
| 65 | static const struct exthdr_desc tcpopt_sack = { | ||
| 66 | .name = "sack", | ||
| 67 | .type = TCPOPT_KIND_SACK, | ||
| 68 | .templates = { | ||
| 69 | [TCPOPT_SACK_KIND] = PHT("kind", 0, 8), | ||
| 70 | [TCPOPT_SACK_LENGTH] = PHT("length", 8, 8), | ||
| 71 | [TCPOPT_SACK_LEFT] = PHT("left", 16, 32), | ||
| 72 | [TCPOPT_SACK_RIGHT] = PHT("right", 48, 32), | ||
| 73 | [TCPOPT_SACK_LEFT1] = PHT("left", 80, 32), | ||
| 74 | [TCPOPT_SACK_RIGHT1] = PHT("right", 112, 32), | ||
| 75 | [TCPOPT_SACK_LEFT2] = PHT("left", 144, 32), | ||
| 76 | [TCPOPT_SACK_RIGHT2] = PHT("right", 176, 32), | ||
| 77 | [TCPOPT_SACK_LEFT3] = PHT("left", 208, 32), | ||
| 78 | [TCPOPT_SACK_RIGHT3] = PHT("right", 240, 32), | ||
| 79 | }, | ||
| 80 | }; | ||
| 81 | |||
| 82 | static const struct exthdr_desc tcpopt_timestamp = { | ||
| 83 | .name = "timestamp", | ||
| 84 | .type = TCPOPT_KIND_TIMESTAMP, | ||
| 85 | .templates = { | ||
| 86 | [TCPOPT_TS_KIND] = PHT("kind", 0, 8), | ||
| 87 | [TCPOPT_TS_LENGTH] = PHT("length", 8, 8), | ||
| 88 | [TCPOPT_TS_TSVAL] = PHT("tsval", 16, 32), | ||
| 89 | [TCPOPT_TS_TSECR] = PHT("tsecr", 48, 32), | ||
| 90 | }, | ||
| 91 | }; | ||
| 92 | |||
| 93 | static const struct exthdr_desc tcpopt_fastopen = { | ||
| 94 | .name = "fastopen", | ||
| 95 | .type = TCPOPT_KIND_FASTOPEN, | ||
| 96 | .templates = { | ||
| 97 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 98 | [TCPOPT_COMMON_LENGTH] = PHT("length", 8, 8), | ||
| 99 | }, | ||
| 100 | }; | ||
| 101 | |||
| 102 | static const struct exthdr_desc tcpopt_md5sig = { | ||
| 103 | .name = "md5sig", | ||
| 104 | .type = TCPOPT_KIND_MD5SIG, | ||
| 105 | .templates = { | ||
| 106 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 107 | [TCPOPT_COMMON_LENGTH] = PHT("length", 8, 8), | ||
| 108 | }, | ||
| 109 | }; | ||
| 110 | |||
| 111 | static const struct symbol_table mptcp_subtype_tbl = { | ||
| 112 | .base = BASE_DECIMAL, | ||
| 113 | .symbols = { | ||
| 114 | SYMBOL("mp-capable", 0), | ||
| 115 | SYMBOL("mp-join", 1), | ||
| 116 | SYMBOL("dss", 2), | ||
| 117 | SYMBOL("add-addr", 3), | ||
| 118 | SYMBOL("remove-addr", 4), | ||
| 119 | SYMBOL("mp-prio", 5), | ||
| 120 | SYMBOL("mp-fail", 6), | ||
| 121 | SYMBOL("mp-fastclose", 7), | ||
| 122 | SYMBOL("mp-tcprst", 8), | ||
| 123 | SYMBOL_LIST_END | ||
| 124 | }, | ||
| 125 | }; | ||
| 126 | |||
| 127 | /* alias of integer_type to parse mptcp subtypes */ | ||
| 128 | const struct datatype mptcpopt_subtype = { | ||
| 129 | .type = TYPE_INTEGER, | ||
| 130 | .name = "integer", | ||
| 131 | .desc = "mptcp option subtype", | ||
| 132 | .size = 4, | ||
| 133 | .basetype = &integer_type, | ||
| 134 | .sym_tbl = &mptcp_subtype_tbl, | ||
| 135 | }; | ||
| 136 | |||
| 137 | static const struct exthdr_desc tcpopt_mptcp = { | ||
| 138 | .name = "mptcp", | ||
| 139 | .type = TCPOPT_KIND_MPTCP, | ||
| 140 | .templates = { | ||
| 141 | [TCPOPT_MPTCP_KIND] = PHT("kind", 0, 8), | ||
| 142 | [TCPOPT_MPTCP_LENGTH] = PHT("length", 8, 8), | ||
| 143 | [TCPOPT_MPTCP_SUBTYPE] = PROTO_HDR_TEMPLATE("subtype", | ||
| 144 | &mptcpopt_subtype, | ||
| 145 | BYTEORDER_BIG_ENDIAN, | ||
| 146 | 16, 4), | ||
| 147 | }, | ||
| 148 | }; | ||
| 149 | |||
| 150 | static const struct exthdr_desc tcpopt_fallback = { | ||
| 151 | .templates = { | ||
| 152 | [TCPOPT_COMMON_KIND] = PHT("kind", 0, 8), | ||
| 153 | [TCPOPT_COMMON_LENGTH] = PHT("length", 8, 8), | ||
| 154 | }, | ||
| 155 | }; | ||
| 156 | #undef PHT | ||
| 157 | |||
| 158 | const struct exthdr_desc *tcpopt_protocols[] = { | ||
| 159 | [TCPOPT_KIND_EOL] = &tcpopt_eol, | ||
| 160 | [TCPOPT_KIND_NOP] = &tcpopt_nop, | ||
| 161 | [TCPOPT_KIND_MAXSEG] = &tcptopt_maxseg, | ||
| 162 | [TCPOPT_KIND_WINDOW] = &tcpopt_window, | ||
| 163 | [TCPOPT_KIND_SACK_PERMITTED] = &tcpopt_sack_permitted, | ||
| 164 | [TCPOPT_KIND_SACK] = &tcpopt_sack, | ||
| 165 | [TCPOPT_KIND_TIMESTAMP] = &tcpopt_timestamp, | ||
| 166 | [TCPOPT_KIND_MD5SIG] = &tcpopt_md5sig, | ||
| 167 | [TCPOPT_KIND_MPTCP] = &tcpopt_mptcp, | ||
| 168 | [TCPOPT_KIND_FASTOPEN] = &tcpopt_fastopen, | ||
| 169 | }; | ||
| 170 | |||
| 171 | 353 | static void tcpopt_assign_tmpl(struct expr *expr, | |
| 172 | const struct proto_hdr_template *tmpl, | ||
| 173 | const struct exthdr_desc *desc) | ||
| 174 | { | ||
| 175 | 353 | expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT; | |
| 176 | |||
| 177 | 353 | expr->exthdr.desc = desc; | |
| 178 | 353 | expr->exthdr.tmpl = tmpl; | |
| 179 | 353 | expr->exthdr.offset = tmpl->offset; | |
| 180 | 353 | } | |
| 181 | |||
| 182 | /** | ||
| 183 | * tcpopt_expr_alloc - allocate tcp option extension expression | ||
| 184 | * | ||
| 185 | * @loc: location from parser | ||
| 186 | * @kind: raw tcp option value to find in packet | ||
| 187 | * @field: highlevel field to find in the option if @kind is present in packet | ||
| 188 | * | ||
| 189 | * Allocate a new tcp option expression. | ||
| 190 | * @kind is the raw option value to find in the packet. | ||
| 191 | * Exception: SACK may use extra OOB data that is mangled here. | ||
| 192 | * | ||
| 193 | * @field is the optional field to extract from the @type option. | ||
| 194 | */ | ||
| 195 | 354 | struct expr *tcpopt_expr_alloc(const struct location *loc, | |
| 196 | unsigned int kind, | ||
| 197 | unsigned int field) | ||
| 198 | { | ||
| 199 | const struct proto_hdr_template *tmpl; | ||
| 200 | 354 | const struct exthdr_desc *desc = NULL; | |
| 201 | struct expr *expr; | ||
| 202 | |||
| 203 |
4/4✓ Branch 0 (2→3) taken 12 times.
✓ Branch 1 (2→8) taken 12 times.
✓ Branch 2 (2→13) taken 12 times.
✓ Branch 3 (2→18) taken 318 times.
|
354 | switch (kind) { |
| 204 | 12 | case TCPOPT_KIND_SACK1: | |
| 205 | 12 | kind = TCPOPT_KIND_SACK; | |
| 206 |
2/2✓ Branch 0 (3→4) taken 3 times.
✓ Branch 1 (3→5) taken 9 times.
|
12 | if (field == TCPOPT_SACK_LEFT) |
| 207 | 3 | field = TCPOPT_SACK_LEFT1; | |
| 208 |
2/2✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→7) taken 6 times.
|
9 | else if (field == TCPOPT_SACK_RIGHT) |
| 209 | 3 | field = TCPOPT_SACK_RIGHT1; | |
| 210 | 12 | break; | |
| 211 | 12 | case TCPOPT_KIND_SACK2: | |
| 212 | 12 | kind = TCPOPT_KIND_SACK; | |
| 213 |
2/2✓ Branch 0 (8→9) taken 3 times.
✓ Branch 1 (8→10) taken 9 times.
|
12 | if (field == TCPOPT_SACK_LEFT) |
| 214 | 3 | field = TCPOPT_SACK_LEFT2; | |
| 215 |
2/2✓ Branch 0 (10→11) taken 3 times.
✓ Branch 1 (10→12) taken 6 times.
|
9 | else if (field == TCPOPT_SACK_RIGHT) |
| 216 | 3 | field = TCPOPT_SACK_RIGHT2; | |
| 217 | 12 | break; | |
| 218 | 12 | case TCPOPT_KIND_SACK3: | |
| 219 | 12 | kind = TCPOPT_KIND_SACK; | |
| 220 |
2/2✓ Branch 0 (13→14) taken 3 times.
✓ Branch 1 (13→15) taken 9 times.
|
12 | if (field == TCPOPT_SACK_LEFT) |
| 221 | 3 | field = TCPOPT_SACK_LEFT3; | |
| 222 |
2/2✓ Branch 0 (15→16) taken 3 times.
✓ Branch 1 (15→17) taken 6 times.
|
9 | else if (field == TCPOPT_SACK_RIGHT) |
| 223 | 3 | field = TCPOPT_SACK_RIGHT3; | |
| 224 | 12 | break; | |
| 225 | } | ||
| 226 | |||
| 227 |
2/2✓ Branch 0 (18→19) taken 329 times.
✓ Branch 1 (18→20) taken 25 times.
|
354 | if (kind < array_size(tcpopt_protocols)) |
| 228 | 329 | desc = tcpopt_protocols[kind]; | |
| 229 | |||
| 230 |
2/2✓ Branch 0 (20→21) taken 31 times.
✓ Branch 1 (20→29) taken 323 times.
|
354 | if (!desc) { |
| 231 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 31 times.
|
31 | if (kind > 255) |
| 232 | ✗ | return NULL; | |
| 233 | |||
| 234 | 31 | desc = &tcpopt_fallback; | |
| 235 | |||
| 236 |
1/2✓ Branch 0 (23→24) taken 31 times.
✗ Branch 1 (23→25) not taken.
|
31 | switch (field) { |
| 237 | 31 | case TCPOPT_COMMON_KIND: | |
| 238 | case TCPOPT_COMMON_LENGTH: | ||
| 239 | 31 | tmpl = &desc->templates[field]; | |
| 240 | 31 | break; | |
| 241 | ✗ | default: | |
| 242 | ✗ | tmpl = &tcpopt_unknown_template; | |
| 243 | ✗ | break; | |
| 244 | } | ||
| 245 | |||
| 246 | 31 | expr = expr_alloc(loc, EXPR_EXTHDR, &integer_type, | |
| 247 | BYTEORDER_BIG_ENDIAN, 8); | ||
| 248 | |||
| 249 | 31 | expr->exthdr.raw_type = kind; | |
| 250 | 31 | tcpopt_assign_tmpl(expr, tmpl, desc); | |
| 251 | 31 | return expr; | |
| 252 | } | ||
| 253 | |||
| 254 | 323 | tmpl = &desc->templates[field]; | |
| 255 |
3/4✓ Branch 0 (29→30) taken 323 times.
✗ Branch 1 (29→31) not taken.
✓ Branch 2 (30→31) taken 1 times.
✓ Branch 3 (30→32) taken 322 times.
|
323 | if (!tmpl || !tmpl->dtype) |
| 256 | 1 | return NULL; | |
| 257 | |||
| 258 | 322 | expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype, | |
| 259 | 322 | BYTEORDER_BIG_ENDIAN, tmpl->len); | |
| 260 | |||
| 261 | 322 | expr->exthdr.raw_type = desc->type; | |
| 262 | 322 | tcpopt_assign_tmpl(expr, tmpl, desc); | |
| 263 | |||
| 264 | 322 | return expr; | |
| 265 | } | ||
| 266 | |||
| 267 | 329 | void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int off, | |
| 268 | unsigned int len, uint32_t flags) | ||
| 269 | { | ||
| 270 | const struct proto_hdr_template *tmpl; | ||
| 271 | unsigned int i; | ||
| 272 | |||
| 273 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 329 times.
|
329 | assert(expr->etype == EXPR_EXTHDR); |
| 274 | |||
| 275 | 329 | expr->len = len; | |
| 276 | 329 | expr->exthdr.flags = flags; | |
| 277 | 329 | expr->exthdr.offset = off; | |
| 278 | 329 | expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT; | |
| 279 | 329 | expr->exthdr.tmpl = &tcpopt_unknown_template; | |
| 280 | |||
| 281 |
2/2✓ Branch 0 (4→5) taken 92 times.
✓ Branch 1 (4→6) taken 237 times.
|
329 | if (flags & NFT_EXTHDR_F_PRESENT) |
| 282 | 92 | datatype_set(expr, &boolean_type); | |
| 283 | else | ||
| 284 | 237 | datatype_set(expr, &integer_type); | |
| 285 | |||
| 286 |
2/2✓ Branch 0 (7→8) taken 291 times.
✓ Branch 1 (7→9) taken 38 times.
|
329 | if (type >= array_size(tcpopt_protocols) || |
| 287 |
2/2✓ Branch 0 (8→9) taken 9 times.
✓ Branch 1 (8→10) taken 282 times.
|
291 | !tcpopt_protocols[type]) |
| 288 | 47 | return; | |
| 289 | |||
| 290 | 282 | expr->exthdr.desc = tcpopt_protocols[type]; | |
| 291 | 282 | expr->exthdr.flags = flags; | |
| 292 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 282 times.
|
282 | assert(expr->exthdr.desc != NULL); |
| 293 | |||
| 294 |
2/2✓ Branch 0 (20→13) taken 1068 times.
✓ Branch 1 (20→21) taken 30 times.
|
1098 | for (i = 0; i < array_size(expr->exthdr.desc->templates); ++i) { |
| 295 | 1068 | tmpl = &expr->exthdr.desc->templates[i]; | |
| 296 |
4/4✓ Branch 0 (13→14) taken 296 times.
✓ Branch 1 (13→15) taken 772 times.
✓ Branch 2 (14→15) taken 44 times.
✓ Branch 3 (14→16) taken 252 times.
|
1068 | if (tmpl->offset != off || tmpl->len != len) |
| 297 | 816 | continue; | |
| 298 | |||
| 299 |
2/2✓ Branch 0 (16→17) taken 178 times.
✓ Branch 1 (16→18) taken 74 times.
|
252 | if ((flags & NFT_EXTHDR_F_PRESENT) == 0) |
| 300 | 178 | datatype_set(expr, tmpl->dtype); | |
| 301 | |||
| 302 | 252 | expr->exthdr.tmpl = tmpl; | |
| 303 | 252 | break; | |
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | 30 | bool tcpopt_find_template(struct expr *expr, unsigned int offset, unsigned int len) | |
| 308 | { | ||
| 309 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 30 times.
|
30 | if (expr->exthdr.tmpl != &tcpopt_unknown_template) |
| 310 | ✗ | return false; | |
| 311 | |||
| 312 | 30 | tcpopt_init_raw(expr, expr->exthdr.desc->type, offset, len, 0); | |
| 313 | |||
| 314 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 30 times.
|
30 | if (expr->exthdr.tmpl == &tcpopt_unknown_template) |
| 315 | ✗ | return false; | |
| 316 | |||
| 317 | 30 | return true; | |
| 318 | } | ||
| 319 |