| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2018 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <errno.h> | ||
| 12 | #include <limits.h> | ||
| 13 | #include <stddef.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdbool.h> | ||
| 17 | #include <stdint.h> | ||
| 18 | #include <string.h> | ||
| 19 | #include <net/if.h> | ||
| 20 | #include <net/if_arp.h> | ||
| 21 | #include <pwd.h> | ||
| 22 | #include <grp.h> | ||
| 23 | #include <arpa/inet.h> | ||
| 24 | #include <linux/netfilter.h> | ||
| 25 | #include <linux/pkt_sched.h> | ||
| 26 | #include <linux/if_packet.h> | ||
| 27 | |||
| 28 | #include <nftables.h> | ||
| 29 | #include <expression.h> | ||
| 30 | #include <datatype.h> | ||
| 31 | #include <tunnel.h> | ||
| 32 | #include <gmputil.h> | ||
| 33 | #include <utils.h> | ||
| 34 | #include <erec.h> | ||
| 35 | |||
| 36 | const struct tunnel_template tunnel_templates[] = { | ||
| 37 | [NFT_TUNNEL_PATH] = META_TEMPLATE("path", &boolean_type, | ||
| 38 | BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN), | ||
| 39 | [NFT_TUNNEL_ID] = META_TEMPLATE("id", &integer_type, | ||
| 40 | 4 * 8, BYTEORDER_HOST_ENDIAN), | ||
| 41 | }; | ||
| 42 | |||
| 43 | 3 | struct error_record *tunnel_key_parse(const struct location *loc, | |
| 44 | const char *str, | ||
| 45 | unsigned int *value) | ||
| 46 | { | ||
| 47 | unsigned int i; | ||
| 48 | |||
| 49 |
1/2✓ Branch 0 (8→3) taken 4 times.
✗ Branch 1 (8→9) not taken.
|
4 | for (i = 0; i < array_size(tunnel_templates); i++) { |
| 50 |
1/2✓ Branch 0 (3→4) taken 4 times.
✗ Branch 1 (3→5) not taken.
|
4 | if (!tunnel_templates[i].token || |
| 51 |
2/2✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 3 times.
|
4 | strcmp(tunnel_templates[i].token, str)) |
| 52 | 1 | continue; | |
| 53 | |||
| 54 | 3 | *value = i; | |
| 55 | 3 | return NULL; | |
| 56 | } | ||
| 57 | |||
| 58 | ✗ | return error(loc, "syntax error, unexpected %s", str); | |
| 59 | } | ||
| 60 | |||
| 61 | 4 | static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 62 | { | ||
| 63 | 4 | uint32_t key = expr->tunnel.key; | |
| 64 | 4 | const char *token = "unknown"; | |
| 65 | |||
| 66 |
1/2✓ Branch 0 (2→3) taken 4 times.
✗ Branch 1 (2→4) not taken.
|
4 | if (key < array_size(tunnel_templates)) |
| 67 | 4 | token = tunnel_templates[key].token; | |
| 68 | |||
| 69 | 4 | nft_print(octx, "tunnel %s", token); | |
| 70 | 4 | } | |
| 71 | |||
| 72 | ✗ | static bool tunnel_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 73 | { | ||
| 74 | ✗ | return e1->tunnel.key == e2->tunnel.key; | |
| 75 | } | ||
| 76 | |||
| 77 | 7 | static void tunnel_expr_clone(struct expr *new, const struct expr *expr) | |
| 78 | { | ||
| 79 | 7 | new->tunnel.key = expr->tunnel.key; | |
| 80 | 7 | } | |
| 81 | |||
| 82 | const struct expr_ops tunnel_expr_ops = { | ||
| 83 | .type = EXPR_TUNNEL, | ||
| 84 | .name = "tunnel", | ||
| 85 | .print = tunnel_expr_print, | ||
| 86 | .json = tunnel_expr_json, | ||
| 87 | .cmp = tunnel_expr_cmp, | ||
| 88 | .clone = tunnel_expr_clone, | ||
| 89 | }; | ||
| 90 | |||
| 91 | 13 | struct expr *tunnel_expr_alloc(const struct location *loc, | |
| 92 | enum nft_tunnel_keys key) | ||
| 93 | { | ||
| 94 | 13 | const struct tunnel_template *tmpl = &tunnel_templates[key]; | |
| 95 | struct expr *expr; | ||
| 96 | |||
| 97 | 13 | expr = expr_alloc(loc, EXPR_TUNNEL, tmpl->dtype, tmpl->byteorder, | |
| 98 | 13 | tmpl->len); | |
| 99 | 13 | expr->tunnel.key = key; | |
| 100 | |||
| 101 | 13 | return expr; | |
| 102 | } | ||
| 103 |