| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Protocol header and type definitions and related functions. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2014 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 | */ | ||
| 11 | |||
| 12 | #include <nft.h> | ||
| 13 | |||
| 14 | #include <stddef.h> | ||
| 15 | #include <net/if_arp.h> | ||
| 16 | #include <arpa/inet.h> | ||
| 17 | #include <linux/netfilter.h> | ||
| 18 | |||
| 19 | #include <expression.h> | ||
| 20 | #include <headers.h> | ||
| 21 | #include <proto.h> | ||
| 22 | #include <gmputil.h> | ||
| 23 | #include <utils.h> | ||
| 24 | |||
| 25 | const char *proto_base_names[] = { | ||
| 26 | [PROTO_BASE_INVALID] = "invalid", | ||
| 27 | [PROTO_BASE_LL_HDR] = "link layer", | ||
| 28 | [PROTO_BASE_NETWORK_HDR] = "network layer", | ||
| 29 | [PROTO_BASE_TRANSPORT_HDR] = "transport layer", | ||
| 30 | [PROTO_BASE_INNER_HDR] = "payload data", | ||
| 31 | }; | ||
| 32 | |||
| 33 | const char *proto_base_tokens[] = { | ||
| 34 | [PROTO_BASE_INVALID] = "invalid", | ||
| 35 | [PROTO_BASE_LL_HDR] = "ll", | ||
| 36 | [PROTO_BASE_NETWORK_HDR] = "nh", | ||
| 37 | [PROTO_BASE_TRANSPORT_HDR] = "th", | ||
| 38 | [PROTO_BASE_INNER_HDR] = "ih", | ||
| 39 | }; | ||
| 40 | |||
| 41 | const struct proto_hdr_template proto_unknown_template = | ||
| 42 | PROTO_HDR_TEMPLATE("unknown", &invalid_type, BYTEORDER_INVALID, 0, 0); | ||
| 43 | |||
| 44 | const struct proto_desc proto_unknown = { | ||
| 45 | .name = "unknown", | ||
| 46 | .base = PROTO_BASE_INVALID, | ||
| 47 | }; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * proto_find_upper - find higher layer protocol description by protocol value | ||
| 51 | * linking it to the lower layer protocol | ||
| 52 | * | ||
| 53 | * @base: lower layer protocol description | ||
| 54 | * @num: protocol value | ||
| 55 | */ | ||
| 56 | const struct proto_desc * | ||
| 57 | 29250 | proto_find_upper(const struct proto_desc *base, unsigned int num) | |
| 58 | { | ||
| 59 | unsigned int i; | ||
| 60 | |||
| 61 |
1/2✓ Branch 0 (8→3) taken 116326 times.
✗ Branch 1 (8→9) not taken.
|
116326 | for (i = 0; i < array_size(base->protocols); i++) { |
| 62 |
2/2✓ Branch 0 (3→4) taken 1552 times.
✓ Branch 1 (3→5) taken 114774 times.
|
116326 | if (!base->protocols[i].desc) |
| 63 | 1552 | break; | |
| 64 |
2/2✓ Branch 0 (5→6) taken 27698 times.
✓ Branch 1 (5→7) taken 87076 times.
|
114774 | if (base->protocols[i].num == num) |
| 65 | 27698 | return base->protocols[i].desc; | |
| 66 | } | ||
| 67 | 1552 | return NULL; | |
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * proto_find_num - return protocol number linking two protocols together | ||
| 72 | * | ||
| 73 | * @base: lower layer protocol description | ||
| 74 | * @desc: upper layer protocol description | ||
| 75 | */ | ||
| 76 | 12854 | int proto_find_num(const struct proto_desc *base, | |
| 77 | const struct proto_desc *desc) | ||
| 78 | { | ||
| 79 | unsigned int i; | ||
| 80 | |||
| 81 |
1/2✓ Branch 0 (8→3) taken 57189 times.
✗ Branch 1 (8→9) not taken.
|
57189 | for (i = 0; i < array_size(base->protocols); i++) { |
| 82 |
2/2✓ Branch 0 (3→4) taken 2762 times.
✓ Branch 1 (3→5) taken 54427 times.
|
57189 | if (!base->protocols[i].desc) |
| 83 | 2762 | break; | |
| 84 |
2/2✓ Branch 0 (5→6) taken 10092 times.
✓ Branch 1 (5→7) taken 44335 times.
|
54427 | if (base->protocols[i].desc == desc) |
| 85 | 10092 | return base->protocols[i].num; | |
| 86 | } | ||
| 87 | 2762 | return -1; | |
| 88 | } | ||
| 89 | |||
| 90 | static const struct proto_desc *inner_protocols[] = { | ||
| 91 | &proto_vxlan, | ||
| 92 | &proto_geneve, | ||
| 93 | &proto_gre, | ||
| 94 | &proto_gretap, | ||
| 95 | }; | ||
| 96 | |||
| 97 | 766 | const struct proto_desc *proto_find_inner(uint32_t type, uint32_t hdrsize, | |
| 98 | uint32_t flags) | ||
| 99 | { | ||
| 100 | const struct proto_desc *desc; | ||
| 101 | unsigned int i; | ||
| 102 | |||
| 103 |
1/2✓ Branch 0 (8→3) taken 1856 times.
✗ Branch 1 (8→9) not taken.
|
1856 | for (i = 0; i < array_size(inner_protocols); i++) { |
| 104 | 1856 | desc = inner_protocols[i]; | |
| 105 |
2/2✓ Branch 0 (3→4) taken 766 times.
✓ Branch 1 (3→7) taken 1090 times.
|
1856 | if (desc->inner.type == type && |
| 106 |
1/2✓ Branch 0 (4→5) taken 766 times.
✗ Branch 1 (4→7) not taken.
|
766 | desc->inner.hdrsize == hdrsize && |
| 107 |
1/2✓ Branch 0 (5→6) taken 766 times.
✗ Branch 1 (5→7) not taken.
|
766 | desc->inner.flags == flags) |
| 108 | 766 | return inner_protocols[i]; | |
| 109 | } | ||
| 110 | |||
| 111 | ✗ | return &proto_unknown; | |
| 112 | } | ||
| 113 | |||
| 114 | static const struct dev_proto_desc dev_proto_desc[] = { | ||
| 115 | DEV_PROTO_DESC(ARPHRD_ETHER, &proto_eth), | ||
| 116 | }; | ||
| 117 | |||
| 118 | /** | ||
| 119 | * proto_dev_type - return arphrd type linking a device and a protocol together | ||
| 120 | * | ||
| 121 | * @desc: the protocol description | ||
| 122 | * @res: pointer to result | ||
| 123 | */ | ||
| 124 | 2962 | int proto_dev_type(const struct proto_desc *desc, uint16_t *res) | |
| 125 | { | ||
| 126 | const struct proto_desc *base; | ||
| 127 | unsigned int i, j; | ||
| 128 | |||
| 129 |
1/2✓ Branch 0 (13→3) taken 2962 times.
✗ Branch 1 (13→14) not taken.
|
2962 | for (i = 0; i < array_size(dev_proto_desc); i++) { |
| 130 | 2962 | base = dev_proto_desc[i].desc; | |
| 131 |
2/2✓ Branch 0 (3→4) taken 2802 times.
✓ Branch 1 (3→5) taken 160 times.
|
2962 | if (base == desc) { |
| 132 | 2802 | *res = dev_proto_desc[i].type; | |
| 133 | 2802 | return 0; | |
| 134 | } | ||
| 135 |
1/2✓ Branch 0 (11→6) taken 640 times.
✗ Branch 1 (11→12) not taken.
|
640 | for (j = 0; j < array_size(base->protocols); j++) { |
| 136 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 640 times.
|
640 | if (!base->protocols[j].desc) |
| 137 | ✗ | break; | |
| 138 |
2/2✓ Branch 0 (8→9) taken 160 times.
✓ Branch 1 (8→10) taken 480 times.
|
640 | if (base->protocols[j].desc == desc) { |
| 139 | 160 | *res = dev_proto_desc[i].type; | |
| 140 | 160 | return 0; | |
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | ✗ | return -1; | |
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * proto_dev_desc - return protocol description for an arphrd type | ||
| 149 | * | ||
| 150 | * @type: the arphrd type | ||
| 151 | */ | ||
| 152 | 3419 | const struct proto_desc *proto_dev_desc(uint16_t type) | |
| 153 | { | ||
| 154 | unsigned int i; | ||
| 155 | |||
| 156 |
2/2✓ Branch 0 (6→3) taken 3419 times.
✓ Branch 1 (6→7) taken 96 times.
|
3515 | for (i = 0; i < array_size(dev_proto_desc); i++) { |
| 157 |
2/2✓ Branch 0 (3→4) taken 3323 times.
✓ Branch 1 (3→5) taken 96 times.
|
3419 | if (dev_proto_desc[i].type == type) |
| 158 | 3323 | return dev_proto_desc[i].desc; | |
| 159 | } | ||
| 160 | 96 | return NULL; | |
| 161 | } | ||
| 162 | |||
| 163 | const struct hook_proto_desc hook_proto_desc[] = { | ||
| 164 | [NFPROTO_BRIDGE] = HOOK_PROTO_DESC(PROTO_BASE_LL_HDR, &proto_eth), | ||
| 165 | [NFPROTO_NETDEV] = HOOK_PROTO_DESC(PROTO_BASE_LL_HDR, &proto_netdev), | ||
| 166 | [NFPROTO_INET] = HOOK_PROTO_DESC(PROTO_BASE_LL_HDR, &proto_inet), | ||
| 167 | [NFPROTO_IPV4] = HOOK_PROTO_DESC(PROTO_BASE_NETWORK_HDR, &proto_ip), | ||
| 168 | [NFPROTO_IPV6] = HOOK_PROTO_DESC(PROTO_BASE_NETWORK_HDR, &proto_ip6), | ||
| 169 | [NFPROTO_ARP] = HOOK_PROTO_DESC(PROTO_BASE_NETWORK_HDR, &proto_arp), | ||
| 170 | }; | ||
| 171 | |||
| 172 | 368172 | static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base, | |
| 173 | unsigned int debug_mask) | ||
| 174 | { | ||
| 175 | unsigned int i; | ||
| 176 | |||
| 177 |
1/2✓ Branch 0 (2→3) taken 368172 times.
✗ Branch 1 (2→4) not taken.
|
368172 | if (!(debug_mask & NFT_DEBUG_PROTO_CTX)) |
| 178 | 368172 | return; | |
| 179 | |||
| 180 | ✗ | if (base == PROTO_BASE_LL_HDR && ctx->stacked_ll_count) { | |
| 181 | ✗ | pr_debug(" saved ll headers:"); | |
| 182 | ✗ | for (i = 0; i < ctx->stacked_ll_count; i++) | |
| 183 | ✗ | pr_debug(" %s", ctx->stacked_ll[i]->name); | |
| 184 | } | ||
| 185 | |||
| 186 | ✗ | pr_debug("update %s protocol context%s:\n", | |
| 187 | proto_base_names[base], ctx->inner ? " (inner)" : ""); | ||
| 188 | |||
| 189 | ✗ | for (i = PROTO_BASE_LL_HDR; i <= PROTO_BASE_MAX; i++) { | |
| 190 | ✗ | pr_debug(" %-20s: %s", | |
| 191 | proto_base_names[i], | ||
| 192 | ctx->protocol[i].desc ? ctx->protocol[i].desc->name : | ||
| 193 | "none"); | ||
| 194 | ✗ | if (i == base) | |
| 195 | ✗ | pr_debug(" <-"); | |
| 196 | ✗ | pr_debug("\n"); | |
| 197 | } | ||
| 198 | ✗ | pr_debug("\n"); | |
| 199 | } | ||
| 200 | |||
| 201 | /** | ||
| 202 | * proto_ctx_init - initialize protocol context for a given hook family | ||
| 203 | * | ||
| 204 | * @ctx: protocol context | ||
| 205 | * @family: hook family | ||
| 206 | * @debug_mask: display debugging information | ||
| 207 | */ | ||
| 208 | 337024 | void proto_ctx_init(struct proto_ctx *ctx, unsigned int family, | |
| 209 | unsigned int debug_mask, bool inner) | ||
| 210 | { | ||
| 211 | 337024 | const struct hook_proto_desc *h = &hook_proto_desc[family]; | |
| 212 | |||
| 213 | 337024 | memset(ctx, 0, sizeof(*ctx)); | |
| 214 | 337024 | ctx->family = family; | |
| 215 | 337024 | ctx->protocol[h->base].desc = h->desc; | |
| 216 | 337024 | ctx->debug_mask = debug_mask; | |
| 217 | 337024 | ctx->inner = inner; | |
| 218 | |||
| 219 | 337024 | proto_ctx_debug(ctx, h->base, debug_mask); | |
| 220 | 337024 | } | |
| 221 | |||
| 222 | /** | ||
| 223 | * proto_ctx_update: update protocol context for given protocol base | ||
| 224 | * | ||
| 225 | * @ctx: protocol context | ||
| 226 | * @base: protocol base | ||
| 227 | * @loc: location of the relational expression definiting the context | ||
| 228 | * @desc: protocol description for the given layer | ||
| 229 | */ | ||
| 230 | 31148 | void proto_ctx_update(struct proto_ctx *ctx, enum proto_bases base, | |
| 231 | const struct location *loc, | ||
| 232 | const struct proto_desc *desc) | ||
| 233 | { | ||
| 234 | 31148 | bool found = false; | |
| 235 | unsigned int i; | ||
| 236 | |||
| 237 |
3/4✓ Branch 0 (2→3) taken 15864 times.
✓ Branch 1 (2→4) taken 15043 times.
✓ Branch 2 (2→14) taken 241 times.
✗ Branch 3 (2→15) not taken.
|
31148 | switch (base) { |
| 238 | 15864 | case PROTO_BASE_LL_HDR: | |
| 239 | case PROTO_BASE_NETWORK_HDR: | ||
| 240 | 15864 | break; | |
| 241 | 15043 | case PROTO_BASE_TRANSPORT_HDR: | |
| 242 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 15043 times.
|
15043 | if (ctx->protocol[base].num_protos >= PROTO_CTX_NUM_PROTOS) |
| 243 | ✗ | break; | |
| 244 | |||
| 245 |
2/2✓ Branch 0 (10→7) taken 5711 times.
✓ Branch 1 (10→11) taken 10360 times.
|
16071 | for (i = 0; i < ctx->protocol[base].num_protos; i++) { |
| 246 |
2/2✓ Branch 0 (7→8) taken 4683 times.
✓ Branch 1 (7→9) taken 1028 times.
|
5711 | if (ctx->protocol[base].protos[i].desc == desc) { |
| 247 | 4683 | found = true; | |
| 248 | 4683 | break; | |
| 249 | } | ||
| 250 | } | ||
| 251 |
2/2✓ Branch 0 (11→12) taken 10360 times.
✓ Branch 1 (11→13) taken 4683 times.
|
15043 | if (!found) { |
| 252 | 10360 | i = ctx->protocol[base].num_protos++; | |
| 253 | 10360 | ctx->protocol[base].protos[i].desc = desc; | |
| 254 | 10360 | ctx->protocol[base].protos[i].location = *loc; | |
| 255 | } | ||
| 256 | 15043 | break; | |
| 257 | 241 | case PROTO_BASE_INNER_HDR: | |
| 258 | 241 | break; | |
| 259 | ✗ | default: | |
| 260 | ✗ | BUG("unknown protocol base %d", base); | |
| 261 | } | ||
| 262 | |||
| 263 | 31148 | ctx->protocol[base].location = *loc; | |
| 264 | 31148 | ctx->protocol[base].desc = desc; | |
| 265 | |||
| 266 | 31148 | proto_ctx_debug(ctx, base, ctx->debug_mask); | |
| 267 | 31148 | } | |
| 268 | |||
| 269 | 8826 | bool proto_ctx_is_ambiguous(struct proto_ctx *ctx, enum proto_bases base) | |
| 270 | { | ||
| 271 | 8826 | return ctx->protocol[base].num_protos > 1; | |
| 272 | } | ||
| 273 | |||
| 274 | 2 | const struct proto_desc *proto_ctx_find_conflict(struct proto_ctx *ctx, | |
| 275 | enum proto_bases base, | ||
| 276 | const struct proto_desc *desc) | ||
| 277 | { | ||
| 278 | unsigned int i; | ||
| 279 | |||
| 280 |
1/3✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→6) taken 2 times.
✗ Branch 2 (2→12) not taken.
|
2 | switch (base) { |
| 281 | ✗ | case PROTO_BASE_LL_HDR: | |
| 282 | case PROTO_BASE_NETWORK_HDR: | ||
| 283 | ✗ | if (desc != ctx->protocol[base].desc) | |
| 284 | ✗ | return ctx->protocol[base].desc; | |
| 285 | ✗ | break; | |
| 286 | 2 | case PROTO_BASE_TRANSPORT_HDR: | |
| 287 |
1/2✓ Branch 0 (10→7) taken 4 times.
✗ Branch 1 (10→11) not taken.
|
4 | for (i = 0; i < ctx->protocol[base].num_protos; i++) { |
| 288 |
2/2✓ Branch 0 (7→8) taken 2 times.
✓ Branch 1 (7→9) taken 2 times.
|
4 | if (desc != ctx->protocol[base].protos[i].desc) |
| 289 | 2 | return ctx->protocol[base].protos[i].desc; | |
| 290 | } | ||
| 291 | ✗ | break; | |
| 292 | ✗ | default: | |
| 293 | ✗ | BUG("unknown protocol base %d", base); | |
| 294 | } | ||
| 295 | |||
| 296 | ✗ | return NULL; | |
| 297 | } | ||
| 298 | |||
| 299 | #define HDR_TEMPLATE(__name, __dtype, __type, __member) \ | ||
| 300 | PROTO_HDR_TEMPLATE(__name, __dtype, \ | ||
| 301 | BYTEORDER_BIG_ENDIAN, \ | ||
| 302 | offsetof(__type, __member) * 8, \ | ||
| 303 | field_sizeof(__type, __member) * 8) | ||
| 304 | |||
| 305 | #define HDR_FIELD(__name, __struct, __member) \ | ||
| 306 | HDR_TEMPLATE(__name, &integer_type, __struct, __member) | ||
| 307 | #define HDR_HEX_FIELD(__name, __struct, __member) \ | ||
| 308 | HDR_TEMPLATE(__name, &xinteger_type, __struct, __member) | ||
| 309 | #define HDR_BITFIELD(__name, __dtype, __offset, __len) \ | ||
| 310 | PROTO_HDR_TEMPLATE(__name, __dtype, BYTEORDER_BIG_ENDIAN, \ | ||
| 311 | __offset, __len) | ||
| 312 | #define HDR_TYPE(__name, __dtype, __struct, __member) \ | ||
| 313 | HDR_TEMPLATE(__name, __dtype, __struct, __member) | ||
| 314 | |||
| 315 | #define INET_PROTOCOL(__name, __struct, __member) \ | ||
| 316 | HDR_TYPE(__name, &inet_protocol_type, __struct, __member) | ||
| 317 | #define INET_SERVICE(__name, __struct, __member) \ | ||
| 318 | HDR_TYPE(__name, &inet_service_type, __struct, __member) | ||
| 319 | |||
| 320 | /* | ||
| 321 | * AH | ||
| 322 | */ | ||
| 323 | |||
| 324 | #define AHHDR_FIELD(__name, __member) \ | ||
| 325 | HDR_FIELD(__name, struct ip_auth_hdr, __member) | ||
| 326 | |||
| 327 | const struct proto_desc proto_ah = { | ||
| 328 | .name = "ah", | ||
| 329 | .id = PROTO_DESC_AH, | ||
| 330 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 331 | .protocol_key = AHHDR_NEXTHDR, | ||
| 332 | .protocols = { | ||
| 333 | PROTO_LINK(IPPROTO_ESP, &proto_esp), | ||
| 334 | PROTO_LINK(IPPROTO_AH, &proto_ah), | ||
| 335 | PROTO_LINK(IPPROTO_COMP, &proto_comp), | ||
| 336 | PROTO_LINK(IPPROTO_UDP, &proto_udp), | ||
| 337 | PROTO_LINK(IPPROTO_UDPLITE, &proto_udplite), | ||
| 338 | PROTO_LINK(IPPROTO_TCP, &proto_tcp), | ||
| 339 | PROTO_LINK(IPPROTO_DCCP, &proto_dccp), | ||
| 340 | PROTO_LINK(IPPROTO_SCTP, &proto_sctp), | ||
| 341 | }, | ||
| 342 | .templates = { | ||
| 343 | [AHHDR_NEXTHDR] = INET_PROTOCOL("nexthdr", struct ip_auth_hdr, nexthdr), | ||
| 344 | [AHHDR_HDRLENGTH] = AHHDR_FIELD("hdrlength", hdrlen), | ||
| 345 | [AHHDR_RESERVED] = AHHDR_FIELD("reserved", reserved), | ||
| 346 | [AHHDR_SPI] = AHHDR_FIELD("spi", spi), | ||
| 347 | [AHHDR_SEQUENCE] = AHHDR_FIELD("sequence", seq_no), | ||
| 348 | }, | ||
| 349 | .format = { | ||
| 350 | .order = { | ||
| 351 | AHHDR_SPI, AHHDR_HDRLENGTH, AHHDR_NEXTHDR, | ||
| 352 | }, | ||
| 353 | .filter = (1 << AHHDR_RESERVED) | (1 << AHHDR_SEQUENCE) | ||
| 354 | }, | ||
| 355 | }; | ||
| 356 | |||
| 357 | /* | ||
| 358 | * ESP | ||
| 359 | */ | ||
| 360 | |||
| 361 | #define ESPHDR_FIELD(__name, __member) \ | ||
| 362 | HDR_FIELD(__name, struct ip_esp_hdr, __member) | ||
| 363 | |||
| 364 | const struct proto_desc proto_esp = { | ||
| 365 | .name = "esp", | ||
| 366 | .id = PROTO_DESC_ESP, | ||
| 367 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 368 | .templates = { | ||
| 369 | [ESPHDR_SPI] = ESPHDR_FIELD("spi", spi), | ||
| 370 | [ESPHDR_SEQUENCE] = ESPHDR_FIELD("sequence", seq_no), | ||
| 371 | }, | ||
| 372 | }; | ||
| 373 | |||
| 374 | /* | ||
| 375 | * IPCOMP | ||
| 376 | */ | ||
| 377 | |||
| 378 | #define COMPHDR_FIELD(__name, __member) \ | ||
| 379 | HDR_FIELD(__name, struct ip_comp_hdr, __member) | ||
| 380 | |||
| 381 | const struct proto_desc proto_comp = { | ||
| 382 | .name = "comp", | ||
| 383 | .id = PROTO_DESC_COMP, | ||
| 384 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 385 | .protocol_key = COMPHDR_NEXTHDR, | ||
| 386 | .protocols = { | ||
| 387 | PROTO_LINK(IPPROTO_ESP, &proto_esp), | ||
| 388 | PROTO_LINK(IPPROTO_AH, &proto_ah), | ||
| 389 | PROTO_LINK(IPPROTO_COMP, &proto_comp), | ||
| 390 | PROTO_LINK(IPPROTO_UDP, &proto_udp), | ||
| 391 | PROTO_LINK(IPPROTO_UDPLITE, &proto_udplite), | ||
| 392 | PROTO_LINK(IPPROTO_TCP, &proto_tcp), | ||
| 393 | PROTO_LINK(IPPROTO_DCCP, &proto_dccp), | ||
| 394 | PROTO_LINK(IPPROTO_SCTP, &proto_sctp), | ||
| 395 | }, | ||
| 396 | .templates = { | ||
| 397 | [COMPHDR_NEXTHDR] = INET_PROTOCOL("nexthdr", struct ip_comp_hdr, nexthdr), | ||
| 398 | [COMPHDR_FLAGS] = HDR_TEMPLATE("flags", &bitmask_type, struct ip_comp_hdr, flags), | ||
| 399 | [COMPHDR_CPI] = COMPHDR_FIELD("cpi", cpi), | ||
| 400 | }, | ||
| 401 | }; | ||
| 402 | |||
| 403 | /* | ||
| 404 | * ICMP | ||
| 405 | */ | ||
| 406 | |||
| 407 | #include <netinet/ip_icmp.h> | ||
| 408 | |||
| 409 | static const struct symbol_table icmp_type_tbl = { | ||
| 410 | .base = BASE_DECIMAL, | ||
| 411 | .symbols = { | ||
| 412 | SYMBOL("echo-reply", ICMP_ECHOREPLY), | ||
| 413 | SYMBOL("destination-unreachable", ICMP_DEST_UNREACH), | ||
| 414 | SYMBOL("source-quench", ICMP_SOURCE_QUENCH), | ||
| 415 | SYMBOL("redirect", ICMP_REDIRECT), | ||
| 416 | SYMBOL("echo-request", ICMP_ECHO), | ||
| 417 | SYMBOL("router-advertisement", ICMP_ROUTERADVERT), | ||
| 418 | SYMBOL("router-solicitation", ICMP_ROUTERSOLICIT), | ||
| 419 | SYMBOL("time-exceeded", ICMP_TIME_EXCEEDED), | ||
| 420 | SYMBOL("parameter-problem", ICMP_PARAMETERPROB), | ||
| 421 | SYMBOL("timestamp-request", ICMP_TIMESTAMP), | ||
| 422 | SYMBOL("timestamp-reply", ICMP_TIMESTAMPREPLY), | ||
| 423 | SYMBOL("info-request", ICMP_INFO_REQUEST), | ||
| 424 | SYMBOL("info-reply", ICMP_INFO_REPLY), | ||
| 425 | SYMBOL("address-mask-request", ICMP_ADDRESS), | ||
| 426 | SYMBOL("address-mask-reply", ICMP_ADDRESSREPLY), | ||
| 427 | SYMBOL_LIST_END | ||
| 428 | }, | ||
| 429 | }; | ||
| 430 | |||
| 431 | const struct datatype icmp_type_type = { | ||
| 432 | .type = TYPE_ICMP_TYPE, | ||
| 433 | .name = "icmp_type", | ||
| 434 | .desc = "ICMP type", | ||
| 435 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 436 | .size = BITS_PER_BYTE, | ||
| 437 | .basetype = &integer_type, | ||
| 438 | .sym_tbl = &icmp_type_tbl, | ||
| 439 | }; | ||
| 440 | |||
| 441 | #define ICMP46HDR_FIELD(__token, __dtype, __struct, __member, __dep) \ | ||
| 442 | { \ | ||
| 443 | .token = (__token), \ | ||
| 444 | .dtype = &__dtype, \ | ||
| 445 | .byteorder = BYTEORDER_BIG_ENDIAN, \ | ||
| 446 | .offset = offsetof(__struct, __member) * 8, \ | ||
| 447 | .len = field_sizeof(__struct, __member) * 8, \ | ||
| 448 | .icmp_dep = (__dep), \ | ||
| 449 | } | ||
| 450 | |||
| 451 | #define ICMPHDR_FIELD(__token, __member, __dep) \ | ||
| 452 | ICMP46HDR_FIELD(__token, integer_type, struct icmphdr, __member, __dep) | ||
| 453 | |||
| 454 | #define ICMPHDR_TYPE(__name, __type, __member) \ | ||
| 455 | HDR_TYPE(__name, __type, struct icmphdr, __member) | ||
| 456 | |||
| 457 | const struct proto_desc proto_icmp = { | ||
| 458 | .name = "icmp", | ||
| 459 | .id = PROTO_DESC_ICMP, | ||
| 460 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 461 | .protocol_key = ICMPHDR_TYPE, | ||
| 462 | .checksum_key = ICMPHDR_CHECKSUM, | ||
| 463 | .checksum_type = NFT_PAYLOAD_CSUM_INET, | ||
| 464 | .templates = { | ||
| 465 | [ICMPHDR_TYPE] = ICMPHDR_TYPE("type", &icmp_type_type, type), | ||
| 466 | [ICMPHDR_CODE] = ICMPHDR_TYPE("code", &icmp_code_type, code), | ||
| 467 | [ICMPHDR_CHECKSUM] = ICMPHDR_FIELD("checksum", checksum, PROTO_ICMP_ANY), | ||
| 468 | [ICMPHDR_ID] = ICMPHDR_FIELD("id", un.echo.id, PROTO_ICMP_ECHO), | ||
| 469 | [ICMPHDR_SEQ] = ICMPHDR_FIELD("sequence", un.echo.sequence, PROTO_ICMP_ECHO), | ||
| 470 | [ICMPHDR_GATEWAY] = ICMPHDR_FIELD("gateway", un.gateway, PROTO_ICMP_ADDRESS), | ||
| 471 | [ICMPHDR_MTU] = ICMPHDR_FIELD("mtu", un.frag.mtu, PROTO_ICMP_MTU), | ||
| 472 | }, | ||
| 473 | }; | ||
| 474 | |||
| 475 | /* | ||
| 476 | * IGMP | ||
| 477 | */ | ||
| 478 | |||
| 479 | #include <netinet/igmp.h> | ||
| 480 | |||
| 481 | #ifndef IGMP_V3_MEMBERSHIP_REPORT | ||
| 482 | #define IGMP_V3_MEMBERSHIP_REPORT 0x22 | ||
| 483 | #endif | ||
| 484 | |||
| 485 | static const struct symbol_table igmp_type_tbl = { | ||
| 486 | .base = BASE_DECIMAL, | ||
| 487 | .symbols = { | ||
| 488 | SYMBOL("membership-query", IGMP_MEMBERSHIP_QUERY), | ||
| 489 | SYMBOL("membership-report-v1", IGMP_V1_MEMBERSHIP_REPORT), | ||
| 490 | SYMBOL("membership-report-v2", IGMP_V2_MEMBERSHIP_REPORT), | ||
| 491 | SYMBOL("membership-report-v3", IGMP_V3_MEMBERSHIP_REPORT), | ||
| 492 | SYMBOL("leave-group", IGMP_V2_LEAVE_GROUP), | ||
| 493 | SYMBOL_LIST_END | ||
| 494 | }, | ||
| 495 | }; | ||
| 496 | |||
| 497 | const struct datatype igmp_type_type = { | ||
| 498 | .type = TYPE_IGMP_TYPE, | ||
| 499 | .name = "igmp_type", | ||
| 500 | .desc = "IGMP type", | ||
| 501 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 502 | .size = BITS_PER_BYTE, | ||
| 503 | .basetype = &integer_type, | ||
| 504 | .sym_tbl = &igmp_type_tbl, | ||
| 505 | }; | ||
| 506 | |||
| 507 | #define IGMPHDR_FIELD(__name, __member) \ | ||
| 508 | HDR_FIELD(__name, struct igmp, __member) | ||
| 509 | #define IGMPHDR_TYPE(__name, __type, __member) \ | ||
| 510 | HDR_TYPE(__name, __type, struct igmp, __member) | ||
| 511 | |||
| 512 | const struct proto_desc proto_igmp = { | ||
| 513 | .name = "igmp", | ||
| 514 | .id = PROTO_DESC_IGMP, | ||
| 515 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 516 | .checksum_key = IGMPHDR_CHECKSUM, | ||
| 517 | .checksum_type = NFT_PAYLOAD_CSUM_INET, | ||
| 518 | .templates = { | ||
| 519 | [IGMPHDR_TYPE] = IGMPHDR_TYPE("type", &igmp_type_type, igmp_type), | ||
| 520 | [IGMPHDR_MRT] = IGMPHDR_FIELD("mrt", igmp_code), | ||
| 521 | [IGMPHDR_CHECKSUM] = IGMPHDR_FIELD("checksum", igmp_cksum), | ||
| 522 | [IGMPHDR_GROUP] = IGMPHDR_FIELD("group", igmp_group), | ||
| 523 | }, | ||
| 524 | }; | ||
| 525 | |||
| 526 | /* | ||
| 527 | * UDP/UDP-Lite | ||
| 528 | */ | ||
| 529 | |||
| 530 | #include <netinet/udp.h> | ||
| 531 | #define UDPHDR_FIELD(__name, __member) \ | ||
| 532 | HDR_FIELD(__name, struct udphdr, __member) | ||
| 533 | |||
| 534 | const struct proto_desc proto_udp = { | ||
| 535 | .name = "udp", | ||
| 536 | .id = PROTO_DESC_UDP, | ||
| 537 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 538 | .templates = { | ||
| 539 | [UDPHDR_SPORT] = INET_SERVICE("sport", struct udphdr, source), | ||
| 540 | [UDPHDR_DPORT] = INET_SERVICE("dport", struct udphdr, dest), | ||
| 541 | [UDPHDR_LENGTH] = UDPHDR_FIELD("length", len), | ||
| 542 | [UDPHDR_CHECKSUM] = UDPHDR_FIELD("checksum", check), | ||
| 543 | }, | ||
| 544 | .protocols = { | ||
| 545 | PROTO_LINK(0, &proto_vxlan), | ||
| 546 | PROTO_LINK(0, &proto_geneve), | ||
| 547 | }, | ||
| 548 | }; | ||
| 549 | |||
| 550 | const struct proto_desc proto_udplite = { | ||
| 551 | .name = "udplite", | ||
| 552 | .id = PROTO_DESC_UDPLITE, | ||
| 553 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 554 | .templates = { | ||
| 555 | [UDPHDR_SPORT] = INET_SERVICE("sport", struct udphdr, source), | ||
| 556 | [UDPHDR_DPORT] = INET_SERVICE("dport", struct udphdr, dest), | ||
| 557 | [UDPHDR_CSUMCOV] = UDPHDR_FIELD("csumcov", len), | ||
| 558 | [UDPHDR_CHECKSUM] = UDPHDR_FIELD("checksum", check), | ||
| 559 | }, | ||
| 560 | }; | ||
| 561 | |||
| 562 | /* | ||
| 563 | * TCP | ||
| 564 | */ | ||
| 565 | |||
| 566 | #include <netinet/tcp.h> | ||
| 567 | |||
| 568 | static const struct symbol_table tcp_flag_tbl = { | ||
| 569 | .base = BASE_HEXADECIMAL, | ||
| 570 | .symbols = { | ||
| 571 | SYMBOL("fin", TCP_FLAG_FIN), | ||
| 572 | SYMBOL("syn", TCP_FLAG_SYN), | ||
| 573 | SYMBOL("rst", TCP_FLAG_RST), | ||
| 574 | SYMBOL("psh", TCP_FLAG_PSH), | ||
| 575 | SYMBOL("ack", TCP_FLAG_ACK), | ||
| 576 | SYMBOL("urg", TCP_FLAG_URG), | ||
| 577 | SYMBOL("ecn", TCP_FLAG_ECN), | ||
| 578 | SYMBOL("cwr", TCP_FLAG_CWR), | ||
| 579 | SYMBOL_LIST_END | ||
| 580 | }, | ||
| 581 | }; | ||
| 582 | |||
| 583 | const struct datatype tcp_flag_type = { | ||
| 584 | .type = TYPE_TCP_FLAG, | ||
| 585 | .name = "tcp_flag", | ||
| 586 | .desc = "TCP flag", | ||
| 587 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 588 | .size = BITS_PER_BYTE, | ||
| 589 | .basetype = &bitmask_type, | ||
| 590 | .sym_tbl = &tcp_flag_tbl, | ||
| 591 | }; | ||
| 592 | |||
| 593 | #define TCPHDR_FIELD(__name, __member) \ | ||
| 594 | HDR_FIELD(__name, struct tcphdr, __member) | ||
| 595 | |||
| 596 | const struct proto_desc proto_tcp = { | ||
| 597 | .name = "tcp", | ||
| 598 | .id = PROTO_DESC_TCP, | ||
| 599 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 600 | .checksum_key = TCPHDR_CHECKSUM, | ||
| 601 | .checksum_type = NFT_PAYLOAD_CSUM_INET, | ||
| 602 | .templates = { | ||
| 603 | [TCPHDR_SPORT] = INET_SERVICE("sport", struct tcphdr, source), | ||
| 604 | [TCPHDR_DPORT] = INET_SERVICE("dport", struct tcphdr, dest), | ||
| 605 | [TCPHDR_SEQ] = TCPHDR_FIELD("sequence", seq), | ||
| 606 | [TCPHDR_ACKSEQ] = TCPHDR_FIELD("ackseq", ack_seq), | ||
| 607 | [TCPHDR_DOFF] = HDR_BITFIELD("doff", &integer_type, | ||
| 608 | (12 * BITS_PER_BYTE), 4), | ||
| 609 | [TCPHDR_RESERVED] = HDR_BITFIELD("reserved", &integer_type, | ||
| 610 | (12 * BITS_PER_BYTE) + 4, 4), | ||
| 611 | [TCPHDR_FLAGS] = HDR_BITFIELD("flags", &tcp_flag_type, | ||
| 612 | 13 * BITS_PER_BYTE, | ||
| 613 | BITS_PER_BYTE), | ||
| 614 | [TCPHDR_WINDOW] = TCPHDR_FIELD("window", window), | ||
| 615 | [TCPHDR_CHECKSUM] = TCPHDR_FIELD("checksum", check), | ||
| 616 | [TCPHDR_URGPTR] = TCPHDR_FIELD("urgptr", urg_ptr), | ||
| 617 | }, | ||
| 618 | .format = { | ||
| 619 | .filter = (1 << TCPHDR_SEQ) | (1 << TCPHDR_ACKSEQ) | | ||
| 620 | (1 << TCPHDR_DOFF) | (1 << TCPHDR_RESERVED) | | ||
| 621 | (1 << TCPHDR_URGPTR), | ||
| 622 | }, | ||
| 623 | }; | ||
| 624 | |||
| 625 | /* | ||
| 626 | * DCCP | ||
| 627 | */ | ||
| 628 | |||
| 629 | static const struct symbol_table dccp_pkttype_tbl = { | ||
| 630 | .base = BASE_HEXADECIMAL, | ||
| 631 | .symbols = { | ||
| 632 | SYMBOL("request", DCCP_PKT_REQUEST), | ||
| 633 | SYMBOL("response", DCCP_PKT_RESPONSE), | ||
| 634 | SYMBOL("data", DCCP_PKT_DATA), | ||
| 635 | SYMBOL("ack", DCCP_PKT_ACK), | ||
| 636 | SYMBOL("dataack", DCCP_PKT_DATAACK), | ||
| 637 | SYMBOL("closereq", DCCP_PKT_CLOSEREQ), | ||
| 638 | SYMBOL("close", DCCP_PKT_CLOSE), | ||
| 639 | SYMBOL("reset", DCCP_PKT_RESET), | ||
| 640 | SYMBOL("sync", DCCP_PKT_SYNC), | ||
| 641 | SYMBOL("syncack", DCCP_PKT_SYNCACK), | ||
| 642 | SYMBOL_LIST_END | ||
| 643 | }, | ||
| 644 | }; | ||
| 645 | |||
| 646 | const struct datatype dccp_pkttype_type = { | ||
| 647 | .type = TYPE_DCCP_PKTTYPE, | ||
| 648 | .name = "dccp_pkttype", | ||
| 649 | .desc = "DCCP packet type", | ||
| 650 | .byteorder = BYTEORDER_INVALID, | ||
| 651 | .size = 4, | ||
| 652 | .basetype = &integer_type, | ||
| 653 | .sym_tbl = &dccp_pkttype_tbl, | ||
| 654 | }; | ||
| 655 | |||
| 656 | |||
| 657 | #define DCCPHDR_FIELD(__name, __member) \ | ||
| 658 | HDR_FIELD(__name, struct dccp_hdr, __member) | ||
| 659 | |||
| 660 | const struct proto_desc proto_dccp = { | ||
| 661 | .name = "dccp", | ||
| 662 | .id = PROTO_DESC_DCCP, | ||
| 663 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 664 | .templates = { | ||
| 665 | [DCCPHDR_SPORT] = INET_SERVICE("sport", struct dccp_hdr, dccph_sport), | ||
| 666 | [DCCPHDR_DPORT] = INET_SERVICE("dport", struct dccp_hdr, dccph_dport), | ||
| 667 | [DCCPHDR_TYPE] = HDR_BITFIELD("type", &dccp_pkttype_type, | ||
| 668 | (8 * BITS_PER_BYTE) + 3, 4), | ||
| 669 | }, | ||
| 670 | }; | ||
| 671 | |||
| 672 | /* | ||
| 673 | * SCTP | ||
| 674 | */ | ||
| 675 | |||
| 676 | #define SCTPHDR_FIELD(__name, __member) \ | ||
| 677 | HDR_FIELD(__name, struct sctphdr, __member) | ||
| 678 | |||
| 679 | const struct proto_desc proto_sctp = { | ||
| 680 | .name = "sctp", | ||
| 681 | .id = PROTO_DESC_SCTP, | ||
| 682 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 683 | .checksum_key = SCTPHDR_CHECKSUM, | ||
| 684 | .checksum_type = NFT_PAYLOAD_CSUM_SCTP, | ||
| 685 | .templates = { | ||
| 686 | [SCTPHDR_SPORT] = INET_SERVICE("sport", struct sctphdr, source), | ||
| 687 | [SCTPHDR_DPORT] = INET_SERVICE("dport", struct sctphdr, dest), | ||
| 688 | [SCTPHDR_VTAG] = SCTPHDR_FIELD("vtag", vtag), | ||
| 689 | [SCTPHDR_CHECKSUM] = SCTPHDR_FIELD("checksum", checksum), | ||
| 690 | }, | ||
| 691 | }; | ||
| 692 | |||
| 693 | /* | ||
| 694 | * Dummy Transpor Header (common udp/tcp/dccp/sctp fields) | ||
| 695 | */ | ||
| 696 | const struct proto_desc proto_th = { | ||
| 697 | .name = "th", | ||
| 698 | .id = PROTO_DESC_TH, | ||
| 699 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 700 | .templates = { | ||
| 701 | [THDR_SPORT] = INET_SERVICE("sport", struct udphdr, source), | ||
| 702 | [THDR_DPORT] = INET_SERVICE("dport", struct udphdr, dest), | ||
| 703 | }, | ||
| 704 | }; | ||
| 705 | |||
| 706 | /* | ||
| 707 | * IPv4 | ||
| 708 | */ | ||
| 709 | |||
| 710 | #include <netinet/ip.h> | ||
| 711 | |||
| 712 | static const struct symbol_table dscp_type_tbl = { | ||
| 713 | .base = BASE_HEXADECIMAL, | ||
| 714 | .symbols = { | ||
| 715 | SYMBOL("cs0", 0x00), | ||
| 716 | SYMBOL("cs1", 0x08), | ||
| 717 | SYMBOL("cs2", 0x10), | ||
| 718 | SYMBOL("cs3", 0x18), | ||
| 719 | SYMBOL("cs4", 0x20), | ||
| 720 | SYMBOL("cs5", 0x28), | ||
| 721 | SYMBOL("cs6", 0x30), | ||
| 722 | SYMBOL("cs7", 0x38), | ||
| 723 | SYMBOL("df", 0x00), | ||
| 724 | SYMBOL("be", 0x00), | ||
| 725 | SYMBOL("lephb", 0x01), | ||
| 726 | SYMBOL("af11", 0x0a), | ||
| 727 | SYMBOL("af12", 0x0c), | ||
| 728 | SYMBOL("af13", 0x0e), | ||
| 729 | SYMBOL("af21", 0x12), | ||
| 730 | SYMBOL("af22", 0x14), | ||
| 731 | SYMBOL("af23", 0x16), | ||
| 732 | SYMBOL("af31", 0x1a), | ||
| 733 | SYMBOL("af32", 0x1c), | ||
| 734 | SYMBOL("af33", 0x1e), | ||
| 735 | SYMBOL("af41", 0x22), | ||
| 736 | SYMBOL("af42", 0x24), | ||
| 737 | SYMBOL("af43", 0x26), | ||
| 738 | SYMBOL("va", 0x2c), | ||
| 739 | SYMBOL("ef", 0x2e), | ||
| 740 | SYMBOL_LIST_END | ||
| 741 | }, | ||
| 742 | }; | ||
| 743 | |||
| 744 | const struct datatype dscp_type = { | ||
| 745 | .type = TYPE_DSCP, | ||
| 746 | .name = "dscp", | ||
| 747 | .desc = "Differentiated Services Code Point", | ||
| 748 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 749 | .size = 6, | ||
| 750 | .basetype = &integer_type, | ||
| 751 | .basefmt = "0x%.2Zx", | ||
| 752 | .sym_tbl = &dscp_type_tbl, | ||
| 753 | }; | ||
| 754 | |||
| 755 | static const struct symbol_table ecn_type_tbl = { | ||
| 756 | .base = BASE_HEXADECIMAL, | ||
| 757 | .symbols = { | ||
| 758 | SYMBOL("not-ect", 0x00), | ||
| 759 | SYMBOL("ect1", 0x01), | ||
| 760 | SYMBOL("ect0", 0x02), | ||
| 761 | SYMBOL("ce", 0x03), | ||
| 762 | SYMBOL_LIST_END | ||
| 763 | }, | ||
| 764 | }; | ||
| 765 | |||
| 766 | const struct datatype ecn_type = { | ||
| 767 | .type = TYPE_ECN, | ||
| 768 | .name = "ecn", | ||
| 769 | .desc = "Explicit Congestion Notification", | ||
| 770 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 771 | .size = 2, | ||
| 772 | .basetype = &integer_type, | ||
| 773 | .basefmt = "0x%.1Zx", | ||
| 774 | .sym_tbl = &ecn_type_tbl, | ||
| 775 | }; | ||
| 776 | |||
| 777 | #define GREHDR_TEMPLATE(__name, __dtype, __member) \ | ||
| 778 | HDR_TEMPLATE(__name, __dtype, struct grehdr, __member) | ||
| 779 | #define GREHDR_TYPE(__name, __member) \ | ||
| 780 | GREHDR_TEMPLATE(__name, ðertype_type, __member) | ||
| 781 | |||
| 782 | const struct proto_desc proto_gre = { | ||
| 783 | .name = "gre", | ||
| 784 | .id = PROTO_DESC_GRE, | ||
| 785 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 786 | .templates = { | ||
| 787 | [0] = PROTO_META_TEMPLATE("l4proto", &inet_protocol_type, NFT_META_L4PROTO, 8), | ||
| 788 | [GREHDR_FLAGS] = HDR_BITFIELD("flags", &integer_type, 0, 5), | ||
| 789 | [GREHDR_VERSION] = HDR_BITFIELD("version", &integer_type, 13, 3), | ||
| 790 | [GREHDR_PROTOCOL] = HDR_BITFIELD("protocol", ðertype_type, 16, 16), | ||
| 791 | }, | ||
| 792 | .inner = { | ||
| 793 | .hdrsize = sizeof(struct grehdr), | ||
| 794 | .flags = NFT_INNER_NH | NFT_INNER_TH, | ||
| 795 | .type = NFT_INNER_GENEVE + 1, | ||
| 796 | }, | ||
| 797 | }; | ||
| 798 | |||
| 799 | const struct proto_desc proto_gretap = { | ||
| 800 | .name = "gretap", | ||
| 801 | .id = PROTO_DESC_GRETAP, | ||
| 802 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 803 | .templates = { | ||
| 804 | [0] = PROTO_META_TEMPLATE("l4proto", &inet_protocol_type, NFT_META_L4PROTO, 8), | ||
| 805 | }, | ||
| 806 | .inner = { | ||
| 807 | .hdrsize = sizeof(struct grehdr), | ||
| 808 | .flags = NFT_INNER_LL | NFT_INNER_NH | NFT_INNER_TH, | ||
| 809 | .type = NFT_INNER_GENEVE + 2, | ||
| 810 | }, | ||
| 811 | }; | ||
| 812 | |||
| 813 | #define IPHDR_FIELD(__name, __member) \ | ||
| 814 | HDR_FIELD(__name, struct iphdr, __member) | ||
| 815 | #define IPHDR_ADDR(__name, __member) \ | ||
| 816 | HDR_TYPE(__name, &ipaddr_type, struct iphdr, __member) | ||
| 817 | |||
| 818 | const struct proto_desc proto_ip = { | ||
| 819 | .name = "ip", | ||
| 820 | .id = PROTO_DESC_IP, | ||
| 821 | .base = PROTO_BASE_NETWORK_HDR, | ||
| 822 | .checksum_key = IPHDR_CHECKSUM, | ||
| 823 | .checksum_type = NFT_PAYLOAD_CSUM_INET, | ||
| 824 | .protocols = { | ||
| 825 | PROTO_LINK(IPPROTO_ICMP, &proto_icmp), | ||
| 826 | PROTO_LINK(IPPROTO_IGMP, &proto_igmp), | ||
| 827 | PROTO_LINK(IPPROTO_ICMPV6, &proto_icmp6), | ||
| 828 | PROTO_LINK(IPPROTO_ESP, &proto_esp), | ||
| 829 | PROTO_LINK(IPPROTO_AH, &proto_ah), | ||
| 830 | PROTO_LINK(IPPROTO_COMP, &proto_comp), | ||
| 831 | PROTO_LINK(IPPROTO_UDP, &proto_udp), | ||
| 832 | PROTO_LINK(IPPROTO_UDPLITE, &proto_udplite), | ||
| 833 | PROTO_LINK(IPPROTO_TCP, &proto_tcp), | ||
| 834 | PROTO_LINK(IPPROTO_DCCP, &proto_dccp), | ||
| 835 | PROTO_LINK(IPPROTO_SCTP, &proto_sctp), | ||
| 836 | PROTO_LINK(IPPROTO_GRE, &proto_gre), | ||
| 837 | PROTO_LINK(IPPROTO_GRE, &proto_gretap), | ||
| 838 | }, | ||
| 839 | .templates = { | ||
| 840 | [0] = PROTO_META_TEMPLATE("l4proto", &inet_protocol_type, NFT_META_L4PROTO, 8), | ||
| 841 | [IPHDR_VERSION] = HDR_BITFIELD("version", &integer_type, 0, 4), | ||
| 842 | [IPHDR_HDRLENGTH] = HDR_BITFIELD("hdrlength", &integer_type, 4, 4), | ||
| 843 | [IPHDR_DSCP] = HDR_BITFIELD("dscp", &dscp_type, 8, 6), | ||
| 844 | [IPHDR_ECN] = HDR_BITFIELD("ecn", &ecn_type, 14, 2), | ||
| 845 | [IPHDR_LENGTH] = IPHDR_FIELD("length", tot_len), | ||
| 846 | [IPHDR_ID] = IPHDR_FIELD("id", id), | ||
| 847 | [IPHDR_FRAG_OFF] = HDR_HEX_FIELD("frag-off", struct iphdr, frag_off), | ||
| 848 | [IPHDR_TTL] = IPHDR_FIELD("ttl", ttl), | ||
| 849 | [IPHDR_PROTOCOL] = INET_PROTOCOL("protocol", struct iphdr, protocol), | ||
| 850 | [IPHDR_CHECKSUM] = IPHDR_FIELD("checksum", check), | ||
| 851 | [IPHDR_SADDR] = IPHDR_ADDR("saddr", saddr), | ||
| 852 | [IPHDR_DADDR] = IPHDR_ADDR("daddr", daddr), | ||
| 853 | }, | ||
| 854 | .format = { | ||
| 855 | .order = { | ||
| 856 | IPHDR_SADDR, IPHDR_DADDR, IPHDR_DSCP, IPHDR_ECN, | ||
| 857 | IPHDR_TTL, IPHDR_ID, IPHDR_PROTOCOL, IPHDR_LENGTH, | ||
| 858 | }, | ||
| 859 | .filter = (1 << IPHDR_VERSION) | (1 << IPHDR_HDRLENGTH) | | ||
| 860 | (1 << IPHDR_FRAG_OFF), | ||
| 861 | }, | ||
| 862 | .pseudohdr = { | ||
| 863 | IPHDR_SADDR, IPHDR_DADDR, IPHDR_PROTOCOL, IPHDR_LENGTH, | ||
| 864 | }, | ||
| 865 | }; | ||
| 866 | |||
| 867 | /* | ||
| 868 | * ICMPv6 | ||
| 869 | */ | ||
| 870 | |||
| 871 | #include <netinet/icmp6.h> | ||
| 872 | |||
| 873 | #define IND_NEIGHBOR_SOLICIT 141 | ||
| 874 | #define IND_NEIGHBOR_ADVERT 142 | ||
| 875 | #define ICMPV6_MLD2_REPORT 143 | ||
| 876 | |||
| 877 | static const struct symbol_table icmp6_type_tbl = { | ||
| 878 | .base = BASE_DECIMAL, | ||
| 879 | .symbols = { | ||
| 880 | SYMBOL("destination-unreachable", ICMP6_DST_UNREACH), | ||
| 881 | SYMBOL("packet-too-big", ICMP6_PACKET_TOO_BIG), | ||
| 882 | SYMBOL("time-exceeded", ICMP6_TIME_EXCEEDED), | ||
| 883 | SYMBOL("parameter-problem", ICMP6_PARAM_PROB), | ||
| 884 | SYMBOL("echo-request", ICMP6_ECHO_REQUEST), | ||
| 885 | SYMBOL("echo-reply", ICMP6_ECHO_REPLY), | ||
| 886 | SYMBOL("mld-listener-query", MLD_LISTENER_QUERY), | ||
| 887 | SYMBOL("mld-listener-report", MLD_LISTENER_REPORT), | ||
| 888 | SYMBOL("mld-listener-done", MLD_LISTENER_REDUCTION), | ||
| 889 | SYMBOL("mld-listener-reduction", MLD_LISTENER_REDUCTION), | ||
| 890 | SYMBOL("nd-router-solicit", ND_ROUTER_SOLICIT), | ||
| 891 | SYMBOL("nd-router-advert", ND_ROUTER_ADVERT), | ||
| 892 | SYMBOL("nd-neighbor-solicit", ND_NEIGHBOR_SOLICIT), | ||
| 893 | SYMBOL("nd-neighbor-advert", ND_NEIGHBOR_ADVERT), | ||
| 894 | SYMBOL("nd-redirect", ND_REDIRECT), | ||
| 895 | SYMBOL("router-renumbering", ICMP6_ROUTER_RENUMBERING), | ||
| 896 | SYMBOL("ind-neighbor-solicit", IND_NEIGHBOR_SOLICIT), | ||
| 897 | SYMBOL("ind-neighbor-advert", IND_NEIGHBOR_ADVERT), | ||
| 898 | SYMBOL("mld2-listener-report", ICMPV6_MLD2_REPORT), | ||
| 899 | SYMBOL_LIST_END | ||
| 900 | }, | ||
| 901 | }; | ||
| 902 | |||
| 903 | const struct datatype icmp6_type_type = { | ||
| 904 | .type = TYPE_ICMP6_TYPE, | ||
| 905 | .name = "icmpv6_type", | ||
| 906 | .desc = "ICMPv6 type", | ||
| 907 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 908 | .size = BITS_PER_BYTE, | ||
| 909 | .basetype = &integer_type, | ||
| 910 | .sym_tbl = &icmp6_type_tbl, | ||
| 911 | }; | ||
| 912 | |||
| 913 | #define ICMP6HDR_FIELD(__token, __member, __dep) \ | ||
| 914 | ICMP46HDR_FIELD(__token, integer_type, struct icmp6_hdr, __member, __dep) | ||
| 915 | #define ICMP6HDR_TYPE(__name, __type, __member) \ | ||
| 916 | HDR_TYPE(__name, __type, struct icmp6_hdr, __member) | ||
| 917 | |||
| 918 | const struct proto_desc proto_icmp6 = { | ||
| 919 | .name = "icmpv6", | ||
| 920 | .id = PROTO_DESC_ICMPV6, | ||
| 921 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 922 | .protocol_key = ICMP6HDR_TYPE, | ||
| 923 | .checksum_key = ICMP6HDR_CHECKSUM, | ||
| 924 | .checksum_type = NFT_PAYLOAD_CSUM_INET, | ||
| 925 | .templates = { | ||
| 926 | [ICMP6HDR_TYPE] = ICMP6HDR_TYPE("type", &icmp6_type_type, icmp6_type), | ||
| 927 | [ICMP6HDR_CODE] = ICMP6HDR_TYPE("code", &icmpv6_code_type, icmp6_code), | ||
| 928 | [ICMP6HDR_CHECKSUM] = ICMP6HDR_FIELD("checksum", icmp6_cksum, PROTO_ICMP_ANY), | ||
| 929 | [ICMP6HDR_PPTR] = ICMP6HDR_FIELD("parameter-problem", icmp6_pptr, PROTO_ICMP6_PPTR), | ||
| 930 | [ICMP6HDR_MTU] = ICMP6HDR_FIELD("mtu", icmp6_mtu, PROTO_ICMP6_MTU), | ||
| 931 | [ICMP6HDR_ID] = ICMP6HDR_FIELD("id", icmp6_id, PROTO_ICMP6_ECHO), | ||
| 932 | [ICMP6HDR_SEQ] = ICMP6HDR_FIELD("sequence", icmp6_seq, PROTO_ICMP6_ECHO), | ||
| 933 | [ICMP6HDR_MAXDELAY] = ICMP6HDR_FIELD("max-delay", icmp6_maxdelay, PROTO_ICMP6_MGMQ), | ||
| 934 | [ICMP6HDR_TADDR] = ICMP46HDR_FIELD("taddr", ip6addr_type, | ||
| 935 | struct nd_neighbor_solicit, nd_ns_target, | ||
| 936 | PROTO_ICMP6_ADDRESS), | ||
| 937 | [ICMP6HDR_DADDR] = ICMP46HDR_FIELD("daddr", ip6addr_type, | ||
| 938 | struct nd_redirect, nd_rd_dst, | ||
| 939 | PROTO_ICMP6_REDIRECT), | ||
| 940 | }, | ||
| 941 | }; | ||
| 942 | |||
| 943 | /* | ||
| 944 | * IPv6 | ||
| 945 | */ | ||
| 946 | |||
| 947 | #define IP6HDR_FIELD(__name, __member) \ | ||
| 948 | HDR_FIELD(__name, struct ipv6hdr, __member) | ||
| 949 | #define IP6HDR_ADDR(__name, __member) \ | ||
| 950 | HDR_TYPE(__name, &ip6addr_type, struct ipv6hdr, __member) | ||
| 951 | #define IP6HDR_PROTOCOL(__name, __member) \ | ||
| 952 | HDR_TYPE(__name, &inet_service_type, struct ipv6hdr, __member) | ||
| 953 | |||
| 954 | const struct proto_desc proto_ip6 = { | ||
| 955 | .name = "ip6", | ||
| 956 | .id = PROTO_DESC_IP6, | ||
| 957 | .base = PROTO_BASE_NETWORK_HDR, | ||
| 958 | .protocols = { | ||
| 959 | PROTO_LINK(IPPROTO_ESP, &proto_esp), | ||
| 960 | PROTO_LINK(IPPROTO_AH, &proto_ah), | ||
| 961 | PROTO_LINK(IPPROTO_COMP, &proto_comp), | ||
| 962 | PROTO_LINK(IPPROTO_UDP, &proto_udp), | ||
| 963 | PROTO_LINK(IPPROTO_UDPLITE, &proto_udplite), | ||
| 964 | PROTO_LINK(IPPROTO_TCP, &proto_tcp), | ||
| 965 | PROTO_LINK(IPPROTO_DCCP, &proto_dccp), | ||
| 966 | PROTO_LINK(IPPROTO_SCTP, &proto_sctp), | ||
| 967 | PROTO_LINK(IPPROTO_ICMP, &proto_icmp), | ||
| 968 | PROTO_LINK(IPPROTO_IGMP, &proto_igmp), | ||
| 969 | PROTO_LINK(IPPROTO_ICMPV6, &proto_icmp6), | ||
| 970 | PROTO_LINK(IPPROTO_GRE, &proto_gre), | ||
| 971 | PROTO_LINK(IPPROTO_GRE, &proto_gretap), | ||
| 972 | }, | ||
| 973 | .templates = { | ||
| 974 | [0] = PROTO_META_TEMPLATE("l4proto", &inet_protocol_type, NFT_META_L4PROTO, 8), | ||
| 975 | [IP6HDR_VERSION] = HDR_BITFIELD("version", &integer_type, 0, 4), | ||
| 976 | [IP6HDR_DSCP] = HDR_BITFIELD("dscp", &dscp_type, 4, 6), | ||
| 977 | [IP6HDR_ECN] = HDR_BITFIELD("ecn", &ecn_type, 10, 2), | ||
| 978 | [IP6HDR_FLOWLABEL] = HDR_BITFIELD("flowlabel", &integer_type, 12, 20), | ||
| 979 | [IP6HDR_LENGTH] = IP6HDR_FIELD("length", payload_len), | ||
| 980 | [IP6HDR_NEXTHDR] = INET_PROTOCOL("nexthdr", struct ipv6hdr, nexthdr), | ||
| 981 | [IP6HDR_HOPLIMIT] = IP6HDR_FIELD("hoplimit", hop_limit), | ||
| 982 | [IP6HDR_SADDR] = IP6HDR_ADDR("saddr", saddr), | ||
| 983 | [IP6HDR_DADDR] = IP6HDR_ADDR("daddr", daddr), | ||
| 984 | }, | ||
| 985 | .format = { | ||
| 986 | .order = { | ||
| 987 | IP6HDR_SADDR, IP6HDR_DADDR, IP6HDR_DSCP, IP6HDR_ECN, | ||
| 988 | IP6HDR_HOPLIMIT, IP6HDR_FLOWLABEL, IP6HDR_NEXTHDR, | ||
| 989 | IP6HDR_LENGTH, | ||
| 990 | }, | ||
| 991 | .filter = (1 << IP6HDR_VERSION), | ||
| 992 | }, | ||
| 993 | .pseudohdr = { | ||
| 994 | IP6HDR_SADDR, IP6HDR_DADDR, IP6HDR_NEXTHDR, IP6HDR_LENGTH, | ||
| 995 | }, | ||
| 996 | }; | ||
| 997 | |||
| 998 | /* | ||
| 999 | * Dummy protocol for mixed IPv4/IPv6 tables. The protocol is set at the link | ||
| 1000 | * layer header, the upper layer protocols are IPv4 and IPv6. | ||
| 1001 | */ | ||
| 1002 | |||
| 1003 | const struct proto_desc proto_inet = { | ||
| 1004 | .name = "inet", | ||
| 1005 | .base = PROTO_BASE_LL_HDR, | ||
| 1006 | .protocols = { | ||
| 1007 | PROTO_LINK(NFPROTO_IPV4, &proto_ip), | ||
| 1008 | PROTO_LINK(NFPROTO_IPV6, &proto_ip6), | ||
| 1009 | }, | ||
| 1010 | .templates = { | ||
| 1011 | [0] = PROTO_META_TEMPLATE("nfproto", &nfproto_type, NFT_META_NFPROTO, 8), | ||
| 1012 | }, | ||
| 1013 | }; | ||
| 1014 | |||
| 1015 | /* | ||
| 1016 | * Dummy protocol for cases where the network layer protocol isn't known | ||
| 1017 | * (IPv4 or IPv6), The higher layer protocols are the protocols common to | ||
| 1018 | * both. | ||
| 1019 | */ | ||
| 1020 | |||
| 1021 | const struct proto_desc proto_inet_service = { | ||
| 1022 | .name = "inet-service", | ||
| 1023 | .base = PROTO_BASE_TRANSPORT_HDR, | ||
| 1024 | .protocol_key = 0, | ||
| 1025 | .protocols = { | ||
| 1026 | PROTO_LINK(IPPROTO_ESP, &proto_esp), | ||
| 1027 | PROTO_LINK(IPPROTO_AH, &proto_ah), | ||
| 1028 | PROTO_LINK(IPPROTO_COMP, &proto_comp), | ||
| 1029 | PROTO_LINK(IPPROTO_UDP, &proto_udp), | ||
| 1030 | PROTO_LINK(IPPROTO_UDPLITE, &proto_udplite), | ||
| 1031 | PROTO_LINK(IPPROTO_TCP, &proto_tcp), | ||
| 1032 | PROTO_LINK(IPPROTO_DCCP, &proto_dccp), | ||
| 1033 | PROTO_LINK(IPPROTO_SCTP, &proto_sctp), | ||
| 1034 | PROTO_LINK(IPPROTO_ICMP, &proto_icmp), | ||
| 1035 | PROTO_LINK(IPPROTO_IGMP, &proto_igmp), | ||
| 1036 | PROTO_LINK(IPPROTO_ICMPV6, &proto_icmp6), | ||
| 1037 | PROTO_LINK(IPPROTO_GRE, &proto_gre), | ||
| 1038 | PROTO_LINK(IPPROTO_GRE, &proto_gretap), | ||
| 1039 | }, | ||
| 1040 | .templates = { | ||
| 1041 | [0] = PROTO_META_TEMPLATE("l4proto", &inet_protocol_type, NFT_META_L4PROTO, 8), | ||
| 1042 | }, | ||
| 1043 | }; | ||
| 1044 | |||
| 1045 | /* | ||
| 1046 | * ARP | ||
| 1047 | */ | ||
| 1048 | |||
| 1049 | #include <net/if_arp.h> | ||
| 1050 | |||
| 1051 | static const struct symbol_table arpop_tbl = { | ||
| 1052 | .base = BASE_HEXADECIMAL, | ||
| 1053 | .symbols = { | ||
| 1054 | SYMBOL("request", __constant_htons(ARPOP_REQUEST)), | ||
| 1055 | SYMBOL("reply", __constant_htons(ARPOP_REPLY)), | ||
| 1056 | SYMBOL("rrequest", __constant_htons(ARPOP_RREQUEST)), | ||
| 1057 | SYMBOL("rreply", __constant_htons(ARPOP_RREPLY)), | ||
| 1058 | SYMBOL("inrequest", __constant_htons(ARPOP_InREQUEST)), | ||
| 1059 | SYMBOL("inreply", __constant_htons(ARPOP_InREPLY)), | ||
| 1060 | SYMBOL("nak", __constant_htons(ARPOP_NAK)), | ||
| 1061 | SYMBOL_LIST_END | ||
| 1062 | }, | ||
| 1063 | }; | ||
| 1064 | |||
| 1065 | const struct datatype arpop_type = { | ||
| 1066 | .type = TYPE_ARPOP, | ||
| 1067 | .name = "arp_op", | ||
| 1068 | .desc = "ARP operation", | ||
| 1069 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1070 | .size = 2 * BITS_PER_BYTE, | ||
| 1071 | .basetype = &integer_type, | ||
| 1072 | .sym_tbl = &arpop_tbl, | ||
| 1073 | }; | ||
| 1074 | |||
| 1075 | #define ARPHDR_TYPE(__name, __type, __member) \ | ||
| 1076 | HDR_TYPE(__name, __type, struct arp_hdr, __member) | ||
| 1077 | #define ARPHDR_FIELD(__name, __member) \ | ||
| 1078 | HDR_FIELD(__name, struct arp_hdr, __member) | ||
| 1079 | |||
| 1080 | const struct proto_desc proto_arp = { | ||
| 1081 | .name = "arp", | ||
| 1082 | .id = PROTO_DESC_ARP, | ||
| 1083 | .base = PROTO_BASE_NETWORK_HDR, | ||
| 1084 | .templates = { | ||
| 1085 | [ARPHDR_HRD] = ARPHDR_FIELD("htype", htype), | ||
| 1086 | [ARPHDR_PRO] = ARPHDR_TYPE("ptype", ðertype_type, ptype), | ||
| 1087 | [ARPHDR_HLN] = ARPHDR_FIELD("hlen", hlen), | ||
| 1088 | [ARPHDR_PLN] = ARPHDR_FIELD("plen", plen), | ||
| 1089 | [ARPHDR_OP] = ARPHDR_TYPE("operation", &arpop_type, oper), | ||
| 1090 | [ARPHDR_SADDR_ETHER] = ARPHDR_TYPE("saddr ether", ðeraddr_type, sha), | ||
| 1091 | [ARPHDR_SADDR_IP] = ARPHDR_TYPE("saddr ip", &ipaddr_type, spa), | ||
| 1092 | [ARPHDR_DADDR_ETHER] = ARPHDR_TYPE("daddr ether", ðeraddr_type, tha), | ||
| 1093 | [ARPHDR_DADDR_IP] = ARPHDR_TYPE("daddr ip", &ipaddr_type, tpa), | ||
| 1094 | }, | ||
| 1095 | .format = { | ||
| 1096 | .filter = (1 << ARPHDR_HRD) | (1 << ARPHDR_PRO) | | ||
| 1097 | (1 << ARPHDR_HLN) | (1 << ARPHDR_PLN) | | ||
| 1098 | (1 << ARPHDR_SADDR_ETHER) | (1 << ARPHDR_DADDR_ETHER) | | ||
| 1099 | (1 << ARPHDR_SADDR_IP) | (1 << ARPHDR_DADDR_IP), | ||
| 1100 | }, | ||
| 1101 | }; | ||
| 1102 | |||
| 1103 | /* | ||
| 1104 | * VLAN | ||
| 1105 | */ | ||
| 1106 | |||
| 1107 | #include <net/ethernet.h> | ||
| 1108 | |||
| 1109 | #define VLANHDR_BITFIELD(__name, __offset, __len) \ | ||
| 1110 | HDR_BITFIELD(__name, &integer_type, __offset, __len) | ||
| 1111 | #define VLANHDR_TYPE(__name, __type, __member) \ | ||
| 1112 | HDR_TYPE(__name, __type, struct vlan_hdr, __member) | ||
| 1113 | |||
| 1114 | const struct proto_desc proto_vlan = { | ||
| 1115 | .name = "vlan", | ||
| 1116 | .id = PROTO_DESC_VLAN, | ||
| 1117 | .base = PROTO_BASE_LL_HDR, | ||
| 1118 | .protocol_key = VLANHDR_TYPE, | ||
| 1119 | .length = sizeof(struct vlan_hdr) * BITS_PER_BYTE, | ||
| 1120 | .protocols = { | ||
| 1121 | PROTO_LINK(__constant_htons(ETH_P_IP), &proto_ip), | ||
| 1122 | PROTO_LINK(__constant_htons(ETH_P_ARP), &proto_arp), | ||
| 1123 | PROTO_LINK(__constant_htons(ETH_P_IPV6), &proto_ip6), | ||
| 1124 | PROTO_LINK(__constant_htons(ETH_P_8021Q), &proto_vlan), | ||
| 1125 | PROTO_LINK(__constant_htons(ETH_P_8021AD), &proto_vlan), | ||
| 1126 | |||
| 1127 | }, | ||
| 1128 | .templates = { | ||
| 1129 | [VLANHDR_PCP] = VLANHDR_BITFIELD("pcp", 0, 3), | ||
| 1130 | [VLANHDR_DEI] = VLANHDR_BITFIELD("dei", 3, 1), | ||
| 1131 | [VLANHDR_CFI] = VLANHDR_BITFIELD("cfi", 3, 1), | ||
| 1132 | [VLANHDR_VID] = VLANHDR_BITFIELD("id", 4, 12), | ||
| 1133 | [VLANHDR_TYPE] = VLANHDR_TYPE("type", ðertype_type, vlan_type), | ||
| 1134 | }, | ||
| 1135 | }; | ||
| 1136 | |||
| 1137 | /* | ||
| 1138 | * Ethernet | ||
| 1139 | */ | ||
| 1140 | |||
| 1141 | const struct datatype etheraddr_type = { | ||
| 1142 | .type = TYPE_ETHERADDR, | ||
| 1143 | .name = "ether_addr", | ||
| 1144 | .desc = "Ethernet address", | ||
| 1145 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1146 | .size = ETH_ALEN * BITS_PER_BYTE, | ||
| 1147 | .basetype = &lladdr_type, | ||
| 1148 | }; | ||
| 1149 | |||
| 1150 | static const struct symbol_table ethertype_tbl = { | ||
| 1151 | .base = BASE_HEXADECIMAL, | ||
| 1152 | .symbols = { | ||
| 1153 | SYMBOL("ip", __constant_htons(ETH_P_IP)), | ||
| 1154 | SYMBOL("arp", __constant_htons(ETH_P_ARP)), | ||
| 1155 | SYMBOL("ip6", __constant_htons(ETH_P_IPV6)), | ||
| 1156 | SYMBOL("8021q", __constant_htons(ETH_P_8021Q)), | ||
| 1157 | SYMBOL("8021ad", __constant_htons(ETH_P_8021AD)), | ||
| 1158 | |||
| 1159 | /* for compatibility with older versions */ | ||
| 1160 | SYMBOL("vlan", __constant_htons(ETH_P_8021Q)), | ||
| 1161 | SYMBOL_LIST_END | ||
| 1162 | }, | ||
| 1163 | }; | ||
| 1164 | |||
| 1165 | 159 | static void ethertype_print(const struct expr *expr, struct output_ctx *octx) | |
| 1166 | { | ||
| 1167 | 159 | return symbolic_constant_print(ðertype_tbl, expr, false, octx); | |
| 1168 | } | ||
| 1169 | |||
| 1170 | const struct datatype ethertype_type = { | ||
| 1171 | .type = TYPE_ETHERTYPE, | ||
| 1172 | .name = "ether_type", | ||
| 1173 | .desc = "Ethernet protocol", | ||
| 1174 | .byteorder = BYTEORDER_BIG_ENDIAN, | ||
| 1175 | .size = 2 * BITS_PER_BYTE, | ||
| 1176 | .basetype = &integer_type, | ||
| 1177 | .basefmt = "0x%.4Zx", | ||
| 1178 | .print = ethertype_print, | ||
| 1179 | .sym_tbl = ðertype_tbl, | ||
| 1180 | }; | ||
| 1181 | |||
| 1182 | #define ETHHDR_TEMPLATE(__name, __dtype, __member) \ | ||
| 1183 | HDR_TEMPLATE(__name, __dtype, struct ether_header, __member) | ||
| 1184 | #define ETHHDR_TYPE(__name, __member) \ | ||
| 1185 | ETHHDR_TEMPLATE(__name, ðertype_type, __member) | ||
| 1186 | #define ETHHDR_ADDR(__name, __member) \ | ||
| 1187 | PROTO_HDR_TEMPLATE(__name, ðeraddr_type, \ | ||
| 1188 | BYTEORDER_BIG_ENDIAN, \ | ||
| 1189 | offsetof(struct ether_header, __member) * 8, \ | ||
| 1190 | field_sizeof(struct ether_header, __member) * 8) | ||
| 1191 | |||
| 1192 | const struct proto_desc proto_eth = { | ||
| 1193 | .name = "ether", | ||
| 1194 | .id = PROTO_DESC_ETHER, | ||
| 1195 | .base = PROTO_BASE_LL_HDR, | ||
| 1196 | .protocol_key = ETHHDR_TYPE, | ||
| 1197 | .length = sizeof(struct ether_header) * BITS_PER_BYTE, | ||
| 1198 | .protocols = { | ||
| 1199 | PROTO_LINK(__constant_htons(ETH_P_IP), &proto_ip), | ||
| 1200 | PROTO_LINK(__constant_htons(ETH_P_ARP), &proto_arp), | ||
| 1201 | PROTO_LINK(__constant_htons(ETH_P_IPV6), &proto_ip6), | ||
| 1202 | PROTO_LINK(__constant_htons(ETH_P_8021Q), &proto_vlan), | ||
| 1203 | PROTO_LINK(__constant_htons(ETH_P_8021AD), &proto_vlan), | ||
| 1204 | }, | ||
| 1205 | .templates = { | ||
| 1206 | [ETHHDR_DADDR] = ETHHDR_ADDR("daddr", ether_dhost), | ||
| 1207 | [ETHHDR_SADDR] = ETHHDR_ADDR("saddr", ether_shost), | ||
| 1208 | [ETHHDR_TYPE] = ETHHDR_TYPE("type", ether_type), | ||
| 1209 | }, | ||
| 1210 | .format = { | ||
| 1211 | .order = { | ||
| 1212 | ETHHDR_SADDR, ETHHDR_DADDR, ETHHDR_TYPE, | ||
| 1213 | }, | ||
| 1214 | }, | ||
| 1215 | |||
| 1216 | }; | ||
| 1217 | |||
| 1218 | /* | ||
| 1219 | * VXLAN | ||
| 1220 | */ | ||
| 1221 | |||
| 1222 | const struct proto_desc proto_vxlan = { | ||
| 1223 | .name = "vxlan", | ||
| 1224 | .id = PROTO_DESC_VXLAN, | ||
| 1225 | .base = PROTO_BASE_INNER_HDR, | ||
| 1226 | .templates = { | ||
| 1227 | [VXLANHDR_FLAGS] = HDR_BITFIELD("flags", &bitmask_type, 0, 8), | ||
| 1228 | [VXLANHDR_VNI] = HDR_BITFIELD("vni", &integer_type, (4 * BITS_PER_BYTE), 24), | ||
| 1229 | }, | ||
| 1230 | .protocols = { | ||
| 1231 | PROTO_LINK(__constant_htons(ETH_P_IP), &proto_ip), | ||
| 1232 | PROTO_LINK(__constant_htons(ETH_P_ARP), &proto_arp), | ||
| 1233 | PROTO_LINK(__constant_htons(ETH_P_IPV6), &proto_ip6), | ||
| 1234 | PROTO_LINK(__constant_htons(ETH_P_8021Q), &proto_vlan), | ||
| 1235 | }, | ||
| 1236 | .inner = { | ||
| 1237 | .hdrsize = sizeof(struct vxlanhdr), | ||
| 1238 | .flags = NFT_INNER_HDRSIZE | NFT_INNER_LL | NFT_INNER_NH | NFT_INNER_TH, | ||
| 1239 | .type = NFT_INNER_VXLAN, | ||
| 1240 | }, | ||
| 1241 | }; | ||
| 1242 | |||
| 1243 | /* | ||
| 1244 | * GENEVE | ||
| 1245 | */ | ||
| 1246 | |||
| 1247 | const struct proto_desc proto_geneve = { | ||
| 1248 | .name = "geneve", | ||
| 1249 | .id = PROTO_DESC_GENEVE, | ||
| 1250 | .base = PROTO_BASE_INNER_HDR, | ||
| 1251 | .templates = { | ||
| 1252 | [GNVHDR_TYPE] = HDR_TYPE("type", ðertype_type, struct gnvhdr, type), | ||
| 1253 | [GNVHDR_VNI] = HDR_BITFIELD("vni", &integer_type, (4 * BITS_PER_BYTE), 24), | ||
| 1254 | }, | ||
| 1255 | .protocols = { | ||
| 1256 | PROTO_LINK(__constant_htons(ETH_P_IP), &proto_ip), | ||
| 1257 | PROTO_LINK(__constant_htons(ETH_P_ARP), &proto_arp), | ||
| 1258 | PROTO_LINK(__constant_htons(ETH_P_IPV6), &proto_ip6), | ||
| 1259 | PROTO_LINK(__constant_htons(ETH_P_8021Q), &proto_vlan), | ||
| 1260 | }, | ||
| 1261 | .inner = { | ||
| 1262 | .hdrsize = sizeof(struct gnvhdr), | ||
| 1263 | .flags = NFT_INNER_HDRSIZE | NFT_INNER_LL | NFT_INNER_NH | NFT_INNER_TH, | ||
| 1264 | .type = NFT_INNER_GENEVE, | ||
| 1265 | }, | ||
| 1266 | }; | ||
| 1267 | |||
| 1268 | |||
| 1269 | /* | ||
| 1270 | * Dummy protocol for netdev tables. | ||
| 1271 | */ | ||
| 1272 | const struct proto_desc proto_netdev = { | ||
| 1273 | .name = "netdev", | ||
| 1274 | .base = PROTO_BASE_LL_HDR, | ||
| 1275 | .protocols = { | ||
| 1276 | PROTO_LINK(__constant_htons(ETH_P_IP), &proto_ip), | ||
| 1277 | PROTO_LINK(__constant_htons(ETH_P_ARP), &proto_arp), | ||
| 1278 | PROTO_LINK(__constant_htons(ETH_P_IPV6), &proto_ip6), | ||
| 1279 | PROTO_LINK(__constant_htons(ETH_P_8021Q), &proto_vlan), | ||
| 1280 | PROTO_LINK(__constant_htons(ETH_P_8021AD), &proto_vlan), | ||
| 1281 | }, | ||
| 1282 | .templates = { | ||
| 1283 | [0] = PROTO_META_TEMPLATE("protocol", ðertype_type, NFT_META_PROTOCOL, 16), | ||
| 1284 | }, | ||
| 1285 | }; | ||
| 1286 | |||
| 1287 | static const struct proto_desc *const proto_definitions[PROTO_DESC_MAX + 1] = { | ||
| 1288 | [PROTO_DESC_AH] = &proto_ah, | ||
| 1289 | [PROTO_DESC_ESP] = &proto_esp, | ||
| 1290 | [PROTO_DESC_COMP] = &proto_comp, | ||
| 1291 | [PROTO_DESC_ICMP] = &proto_icmp, | ||
| 1292 | [PROTO_DESC_IGMP] = &proto_igmp, | ||
| 1293 | [PROTO_DESC_UDP] = &proto_udp, | ||
| 1294 | [PROTO_DESC_UDPLITE] = &proto_udplite, | ||
| 1295 | [PROTO_DESC_TCP] = &proto_tcp, | ||
| 1296 | [PROTO_DESC_DCCP] = &proto_dccp, | ||
| 1297 | [PROTO_DESC_SCTP] = &proto_sctp, | ||
| 1298 | [PROTO_DESC_TH] = &proto_th, | ||
| 1299 | [PROTO_DESC_IP] = &proto_ip, | ||
| 1300 | [PROTO_DESC_IP6] = &proto_ip6, | ||
| 1301 | [PROTO_DESC_ICMPV6] = &proto_icmp6, | ||
| 1302 | [PROTO_DESC_ARP] = &proto_arp, | ||
| 1303 | [PROTO_DESC_VLAN] = &proto_vlan, | ||
| 1304 | [PROTO_DESC_ETHER] = &proto_eth, | ||
| 1305 | [PROTO_DESC_VXLAN] = &proto_vxlan, | ||
| 1306 | [PROTO_DESC_GENEVE] = &proto_geneve, | ||
| 1307 | [PROTO_DESC_GRE] = &proto_gre, | ||
| 1308 | [PROTO_DESC_GRETAP] = &proto_gretap, | ||
| 1309 | }; | ||
| 1310 | |||
| 1311 | 6686 | const struct proto_desc *proto_find_desc(enum proto_desc_id desc_id) | |
| 1312 | { | ||
| 1313 |
1/2✓ Branch 0 (2→3) taken 6686 times.
✗ Branch 1 (2→4) not taken.
|
6686 | if (desc_id >= PROTO_DESC_UNKNOWN && |
| 1314 | desc_id <= PROTO_DESC_MAX) | ||
| 1315 | 6686 | return proto_definitions[desc_id]; | |
| 1316 | |||
| 1317 | ✗ | return NULL; | |
| 1318 | } | ||
| 1319 |