| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Routing expression related definition and types. | ||
| 3 | * | ||
| 4 | * Copyright (c) 2016 Anders K. Pedersen <akp@cohaesio.com> | ||
| 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 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <errno.h> | ||
| 14 | #include <stddef.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <arpa/inet.h> | ||
| 17 | #include <linux/netfilter.h> | ||
| 18 | |||
| 19 | #include <nftables.h> | ||
| 20 | #include <expression.h> | ||
| 21 | #include <datatype.h> | ||
| 22 | #include <rt.h> | ||
| 23 | #include <rule.h> | ||
| 24 | #include <json.h> | ||
| 25 | |||
| 26 | 13026 | void realm_table_rt_init(struct nft_ctx *ctx) | |
| 27 | { | ||
| 28 | 13026 | ctx->output.tbl.realm = rt_symbol_table_init("rt_realms"); | |
| 29 | 13026 | } | |
| 30 | |||
| 31 | 13026 | void realm_table_rt_exit(struct nft_ctx *ctx) | |
| 32 | { | ||
| 33 | 13026 | rt_symbol_table_free(ctx->output.tbl.realm); | |
| 34 | 13026 | } | |
| 35 | |||
| 36 | 20 | static void realm_type_print(const struct expr *expr, struct output_ctx *octx) | |
| 37 | { | ||
| 38 | 20 | return symbolic_constant_print(octx->tbl.realm, expr, true, octx); | |
| 39 | } | ||
| 40 | |||
| 41 | 20 | static struct error_record *realm_type_parse(struct parse_ctx *ctx, | |
| 42 | const struct expr *sym, | ||
| 43 | struct expr **res) | ||
| 44 | { | ||
| 45 | 20 | return symbolic_constant_parse(ctx, sym, ctx->tbl->realm, res); | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | static void realm_type_describe(struct output_ctx *octx) | |
| 49 | { | ||
| 50 | ✗ | rt_symbol_table_describe(octx, "rt_realms", | |
| 51 | octx->tbl.realm, &realm_type); | ||
| 52 | ✗ | } | |
| 53 | |||
| 54 | const struct datatype realm_type = { | ||
| 55 | .type = TYPE_REALM, | ||
| 56 | .name = "realm", | ||
| 57 | .desc = "routing realm", | ||
| 58 | .describe = realm_type_describe, | ||
| 59 | .byteorder = BYTEORDER_HOST_ENDIAN, | ||
| 60 | .size = 4 * BITS_PER_BYTE, | ||
| 61 | .basetype = &integer_type, | ||
| 62 | .print = realm_type_print, | ||
| 63 | .parse = realm_type_parse, | ||
| 64 | }; | ||
| 65 | |||
| 66 | const struct rt_template rt_templates[] = { | ||
| 67 | [NFT_RT_CLASSID] = RT_TEMPLATE("classid", | ||
| 68 | &realm_type, | ||
| 69 | 4 * BITS_PER_BYTE, | ||
| 70 | BYTEORDER_HOST_ENDIAN, | ||
| 71 | false), | ||
| 72 | [NFT_RT_NEXTHOP4] = RT_TEMPLATE("nexthop", | ||
| 73 | &ipaddr_type, | ||
| 74 | 4 * BITS_PER_BYTE, | ||
| 75 | BYTEORDER_BIG_ENDIAN, | ||
| 76 | true), | ||
| 77 | [NFT_RT_NEXTHOP6] = RT_TEMPLATE("nexthop", | ||
| 78 | &ip6addr_type, | ||
| 79 | 16 * BITS_PER_BYTE, | ||
| 80 | BYTEORDER_BIG_ENDIAN, | ||
| 81 | true), | ||
| 82 | [NFT_RT_TCPMSS] = RT_TEMPLATE("mtu", | ||
| 83 | &integer_type, | ||
| 84 | 2 * BITS_PER_BYTE, | ||
| 85 | BYTEORDER_HOST_ENDIAN, | ||
| 86 | false), | ||
| 87 | [NFT_RT_XFRM] = RT_TEMPLATE("ipsec", | ||
| 88 | &boolean_type, | ||
| 89 | BITS_PER_BYTE, | ||
| 90 | BYTEORDER_HOST_ENDIAN, | ||
| 91 | false), | ||
| 92 | }; | ||
| 93 | |||
| 94 | 21 | static void rt_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 95 | { | ||
| 96 | 21 | const char *ip = ""; | |
| 97 | |||
| 98 |
3/3✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 3 times.
✓ Branch 2 (2→5) taken 16 times.
|
21 | switch (expr->rt.key) { |
| 99 | 2 | case NFT_RT_NEXTHOP4: | |
| 100 | 2 | ip = "ip "; | |
| 101 | 2 | break; | |
| 102 | 3 | case NFT_RT_NEXTHOP6: | |
| 103 | 3 | ip = "ip6 "; | |
| 104 | 3 | break; | |
| 105 | 16 | default: | |
| 106 | 16 | break; | |
| 107 | } | ||
| 108 | |||
| 109 | 21 | nft_print(octx, "rt %s%s", ip, rt_templates[expr->rt.key].token); | |
| 110 | 21 | } | |
| 111 | |||
| 112 | ✗ | static bool rt_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 113 | { | ||
| 114 | ✗ | return e1->rt.key == e2->rt.key; | |
| 115 | } | ||
| 116 | |||
| 117 | 40 | static void rt_expr_clone(struct expr *new, const struct expr *expr) | |
| 118 | { | ||
| 119 | 40 | new->rt.key = expr->rt.key; | |
| 120 | 40 | } | |
| 121 | |||
| 122 | #define NFTNL_UDATA_RT_KEY 0 | ||
| 123 | #define NFTNL_UDATA_RT_MAX 1 | ||
| 124 | |||
| 125 | ✗ | static int rt_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 126 | const struct expr *expr) | ||
| 127 | { | ||
| 128 | ✗ | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_RT_KEY, expr->rt.key); | |
| 129 | |||
| 130 | ✗ | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | static int rt_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 134 | { | ||
| 135 | ✗ | const struct nftnl_udata **ud = data; | |
| 136 | ✗ | uint8_t type = nftnl_udata_type(attr); | |
| 137 | ✗ | uint8_t len = nftnl_udata_len(attr); | |
| 138 | |||
| 139 | ✗ | switch (type) { | |
| 140 | ✗ | case NFTNL_UDATA_RT_KEY: | |
| 141 | ✗ | if (len != sizeof(uint32_t)) | |
| 142 | ✗ | return -1; | |
| 143 | ✗ | break; | |
| 144 | ✗ | default: | |
| 145 | ✗ | return 0; | |
| 146 | } | ||
| 147 | |||
| 148 | ✗ | ud[type] = attr; | |
| 149 | ✗ | return 0; | |
| 150 | } | ||
| 151 | |||
| 152 | ✗ | static struct expr *rt_expr_parse_udata(const struct nftnl_udata *attr) | |
| 153 | { | ||
| 154 | ✗ | const struct nftnl_udata *ud[NFTNL_UDATA_RT_MAX + 1] = {}; | |
| 155 | uint32_t key; | ||
| 156 | int err; | ||
| 157 | |||
| 158 | ✗ | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 159 | rt_parse_udata, ud); | ||
| 160 | ✗ | if (err < 0) | |
| 161 | ✗ | return NULL; | |
| 162 | |||
| 163 | ✗ | if (!ud[NFTNL_UDATA_RT_KEY]) | |
| 164 | ✗ | return NULL; | |
| 165 | |||
| 166 | ✗ | key = nftnl_udata_get_u32(ud[NFTNL_UDATA_RT_KEY]); | |
| 167 | |||
| 168 | ✗ | return rt_expr_alloc(&internal_location, key, false); | |
| 169 | } | ||
| 170 | |||
| 171 | const struct expr_ops rt_expr_ops = { | ||
| 172 | .type = EXPR_RT, | ||
| 173 | .name = "rt", | ||
| 174 | .print = rt_expr_print, | ||
| 175 | .json = rt_expr_json, | ||
| 176 | .cmp = rt_expr_cmp, | ||
| 177 | .clone = rt_expr_clone, | ||
| 178 | .parse_udata = rt_expr_parse_udata, | ||
| 179 | .build_udata = rt_expr_build_udata, | ||
| 180 | }; | ||
| 181 | |||
| 182 | 78 | struct expr *rt_expr_alloc(const struct location *loc, enum nft_rt_keys key, | |
| 183 | bool invalid) | ||
| 184 | { | ||
| 185 | 78 | const struct rt_template *tmpl = &rt_templates[key]; | |
| 186 | struct expr *expr; | ||
| 187 | |||
| 188 |
4/4✓ Branch 0 (2→3) taken 34 times.
✓ Branch 1 (2→5) taken 44 times.
✓ Branch 2 (3→4) taken 8 times.
✓ Branch 3 (3→5) taken 26 times.
|
78 | if (invalid && tmpl->invalid) |
| 189 | 8 | expr = expr_alloc(loc, EXPR_RT, &invalid_type, | |
| 190 | 8 | tmpl->byteorder, 0); | |
| 191 | else | ||
| 192 | 70 | expr = expr_alloc(loc, EXPR_RT, tmpl->dtype, | |
| 193 | 70 | tmpl->byteorder, tmpl->len); | |
| 194 | 78 | expr->rt.key = key; | |
| 195 | |||
| 196 | 78 | return expr; | |
| 197 | } | ||
| 198 | |||
| 199 | 41 | void rt_expr_update_type(struct proto_ctx *ctx, struct expr *expr) | |
| 200 | { | ||
| 201 | const struct proto_desc *desc; | ||
| 202 | |||
| 203 |
2/2✓ Branch 0 (2→3) taken 12 times.
✓ Branch 1 (2→8) taken 29 times.
|
41 | switch (expr->rt.key) { |
| 204 | 12 | case NFT_RT_NEXTHOP4: | |
| 205 | 12 | desc = ctx->protocol[PROTO_BASE_NETWORK_HDR].desc; | |
| 206 |
2/2✓ Branch 0 (3→4) taken 7 times.
✓ Branch 1 (3→5) taken 5 times.
|
12 | if (desc == &proto_ip) |
| 207 | 7 | datatype_set(expr, &ipaddr_type); | |
| 208 |
2/2✓ Branch 0 (5→6) taken 2 times.
✓ Branch 1 (5→7) taken 3 times.
|
5 | else if (desc == &proto_ip6) { |
| 209 | 2 | expr->rt.key++; | |
| 210 | 2 | datatype_set(expr, &ip6addr_type); | |
| 211 | } | ||
| 212 | 12 | expr->len = expr->dtype->size; | |
| 213 | 12 | break; | |
| 214 | 29 | default: | |
| 215 | 29 | break; | |
| 216 | } | ||
| 217 | 41 | } | |
| 218 |