| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * XFRM (ipsec) expression | ||
| 3 | * | ||
| 4 | * Copyright (c) Red Hat GmbH. Author: Florian Westphal <fw@strlen.de> | ||
| 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 (or any | ||
| 8 | * later) as published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | |||
| 13 | #include <nftables.h> | ||
| 14 | #include <erec.h> | ||
| 15 | #include <expression.h> | ||
| 16 | #include <xfrm.h> | ||
| 17 | #include <datatype.h> | ||
| 18 | #include <gmputil.h> | ||
| 19 | #include <utils.h> | ||
| 20 | |||
| 21 | #include <netinet/ip.h> | ||
| 22 | #include <linux/netfilter.h> | ||
| 23 | #include <linux/xfrm.h> | ||
| 24 | |||
| 25 | #define XFRM_TEMPLATE_BE(__token, __dtype, __len) { \ | ||
| 26 | .token = (__token), \ | ||
| 27 | .dtype = (__dtype), \ | ||
| 28 | .len = (__len), \ | ||
| 29 | .byteorder = BYTEORDER_BIG_ENDIAN, \ | ||
| 30 | } | ||
| 31 | |||
| 32 | #define XFRM_TEMPLATE_HE(__token, __dtype, __len) { \ | ||
| 33 | .token = (__token), \ | ||
| 34 | .dtype = (__dtype), \ | ||
| 35 | .len = (__len), \ | ||
| 36 | .byteorder = BYTEORDER_HOST_ENDIAN, \ | ||
| 37 | } | ||
| 38 | |||
| 39 | const struct xfrm_template xfrm_templates[] = { | ||
| 40 | [NFT_XFRM_KEY_DADDR_IP4] = XFRM_TEMPLATE_BE("daddr", &ipaddr_type, 4 * BITS_PER_BYTE), | ||
| 41 | [NFT_XFRM_KEY_SADDR_IP4] = XFRM_TEMPLATE_BE("saddr", &ipaddr_type, 4 * BITS_PER_BYTE), | ||
| 42 | [NFT_XFRM_KEY_DADDR_IP6] = XFRM_TEMPLATE_BE("daddr", &ip6addr_type, 16 * BITS_PER_BYTE), | ||
| 43 | [NFT_XFRM_KEY_SADDR_IP6] = XFRM_TEMPLATE_BE("saddr", &ip6addr_type, 16 * BITS_PER_BYTE), | ||
| 44 | [NFT_XFRM_KEY_REQID] = XFRM_TEMPLATE_HE("reqid", &integer_type, 4 * BITS_PER_BYTE), | ||
| 45 | [NFT_XFRM_KEY_SPI] = XFRM_TEMPLATE_BE("spi", &integer_type, 4 * BITS_PER_BYTE), | ||
| 46 | }; | ||
| 47 | |||
| 48 | 39 | static void xfrm_expr_print(const struct expr *expr, struct output_ctx *octx) | |
| 49 | { | ||
| 50 |
2/3✓ Branch 0 (2→3) taken 24 times.
✓ Branch 1 (2→5) taken 15 times.
✗ Branch 2 (2→7) not taken.
|
39 | switch (expr->xfrm.direction) { |
| 51 | 24 | case XFRM_POLICY_IN: | |
| 52 | 24 | nft_print(octx, "ipsec in"); | |
| 53 | 24 | break; | |
| 54 | 15 | case XFRM_POLICY_OUT: | |
| 55 | 15 | nft_print(octx, "ipsec out"); | |
| 56 | 15 | break; | |
| 57 | ✗ | default: | |
| 58 | ✗ | nft_print(octx, "ipsec (unknown dir %d)", expr->xfrm.direction); | |
| 59 | ✗ | break; | |
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 (9→10) taken 3 times.
✓ Branch 1 (9→11) taken 36 times.
|
39 | if (expr->xfrm.spnum) |
| 63 | 3 | nft_print(octx, " spnum %u", expr->xfrm.spnum); | |
| 64 | |||
| 65 |
3/4✓ Branch 0 (11→12) taken 9 times.
✓ Branch 1 (11→13) taken 6 times.
✓ Branch 2 (11→15) taken 24 times.
✗ Branch 3 (11→16) not taken.
|
39 | switch (expr->xfrm.key) { |
| 66 | 9 | case NFT_XFRM_KEY_DADDR_IP4: | |
| 67 | case NFT_XFRM_KEY_SADDR_IP4: | ||
| 68 | 9 | nft_print(octx, " ip"); | |
| 69 | 9 | break; | |
| 70 | 6 | case NFT_XFRM_KEY_DADDR_IP6: | |
| 71 | case NFT_XFRM_KEY_SADDR_IP6: | ||
| 72 | 6 | nft_print(octx, " ip6"); | |
| 73 | 6 | break; | |
| 74 | 24 | case NFT_XFRM_KEY_REQID: | |
| 75 | case NFT_XFRM_KEY_SPI: | ||
| 76 | 24 | break; | |
| 77 | ✗ | default: | |
| 78 | ✗ | nft_print(octx, " (unknown key 0x%x)", expr->xfrm.key); | |
| 79 | ✗ | return; | |
| 80 | } | ||
| 81 | |||
| 82 | 39 | nft_print(octx, " %s", xfrm_templates[expr->xfrm.key].token); | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | static bool xfrm_expr_cmp(const struct expr *e1, const struct expr *e2) | |
| 86 | { | ||
| 87 | ✗ | return e1->xfrm.key == e2->xfrm.key && | |
| 88 | ✗ | e1->xfrm.direction == e2->xfrm.direction && | |
| 89 | ✗ | e1->xfrm.spnum == e2->xfrm.spnum; | |
| 90 | } | ||
| 91 | |||
| 92 | 72 | static void xfrm_expr_clone(struct expr *new, const struct expr *expr) | |
| 93 | { | ||
| 94 | 72 | memcpy(&new->xfrm, &expr->xfrm, sizeof(new->xfrm)); | |
| 95 | 72 | } | |
| 96 | |||
| 97 | #define NFTNL_UDATA_XFRM_KEY 0 | ||
| 98 | #define NFTNL_UDATA_XFRM_SPNUM 1 | ||
| 99 | #define NFTNL_UDATA_XFRM_DIR 2 | ||
| 100 | #define NFTNL_UDATA_XFRM_MAX 3 | ||
| 101 | |||
| 102 | 22 | static int xfrm_expr_build_udata(struct nftnl_udata_buf *udbuf, | |
| 103 | const struct expr *expr) | ||
| 104 | { | ||
| 105 | 22 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_XFRM_KEY, expr->xfrm.key); | |
| 106 | 22 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_XFRM_SPNUM, expr->xfrm.spnum); | |
| 107 | 22 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_XFRM_DIR, expr->xfrm.direction); | |
| 108 | |||
| 109 | 22 | return 0; | |
| 110 | } | ||
| 111 | |||
| 112 | 84 | static int xfrm_parse_udata(const struct nftnl_udata *attr, void *data) | |
| 113 | { | ||
| 114 | 84 | const struct nftnl_udata **ud = data; | |
| 115 | 84 | uint8_t type = nftnl_udata_type(attr); | |
| 116 | 84 | uint8_t len = nftnl_udata_len(attr); | |
| 117 | |||
| 118 |
1/2✓ Branch 0 (4→5) taken 84 times.
✗ Branch 1 (4→8) not taken.
|
84 | switch (type) { |
| 119 | 84 | case NFTNL_UDATA_XFRM_KEY: | |
| 120 | case NFTNL_UDATA_XFRM_SPNUM: | ||
| 121 | case NFTNL_UDATA_XFRM_DIR: | ||
| 122 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 84 times.
|
84 | if (len != sizeof(uint32_t)) |
| 123 | ✗ | return -1; | |
| 124 | 84 | break; | |
| 125 | ✗ | default: | |
| 126 | ✗ | return 0; | |
| 127 | } | ||
| 128 | |||
| 129 | 84 | ud[type] = attr; | |
| 130 | 84 | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | 28 | static struct expr *xfrm_expr_parse_udata(const struct nftnl_udata *attr) | |
| 134 | { | ||
| 135 | 28 | const struct nftnl_udata *ud[NFTNL_UDATA_XFRM_MAX + 1] = {}; | |
| 136 | uint32_t key, dir, spnum; | ||
| 137 | int err; | ||
| 138 | |||
| 139 | 28 | err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr), | |
| 140 | xfrm_parse_udata, ud); | ||
| 141 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 28 times.
|
28 | if (err < 0) |
| 142 | ✗ | return NULL; | |
| 143 | |||
| 144 |
1/2✓ Branch 0 (7→8) taken 28 times.
✗ Branch 1 (7→10) not taken.
|
28 | if (!ud[NFTNL_UDATA_XFRM_KEY] || |
| 145 |
1/2✓ Branch 0 (8→9) taken 28 times.
✗ Branch 1 (8→10) not taken.
|
28 | !ud[NFTNL_UDATA_XFRM_DIR] || |
| 146 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 28 times.
|
28 | !ud[NFTNL_UDATA_XFRM_SPNUM]) |
| 147 | ✗ | return NULL; | |
| 148 | |||
| 149 | 28 | key = nftnl_udata_get_u32(ud[NFTNL_UDATA_XFRM_KEY]); | |
| 150 | 28 | dir = nftnl_udata_get_u32(ud[NFTNL_UDATA_XFRM_DIR]); | |
| 151 | 28 | spnum = nftnl_udata_get_u32(ud[NFTNL_UDATA_XFRM_SPNUM]); | |
| 152 | |||
| 153 | 28 | return xfrm_expr_alloc(&internal_location, dir, spnum, key); | |
| 154 | } | ||
| 155 | |||
| 156 | const struct expr_ops xfrm_expr_ops = { | ||
| 157 | .type = EXPR_XFRM, | ||
| 158 | .name = "xfrm", | ||
| 159 | .print = xfrm_expr_print, | ||
| 160 | .json = xfrm_expr_json, | ||
| 161 | .cmp = xfrm_expr_cmp, | ||
| 162 | .clone = xfrm_expr_clone, | ||
| 163 | .parse_udata = xfrm_expr_parse_udata, | ||
| 164 | .build_udata = xfrm_expr_build_udata, | ||
| 165 | }; | ||
| 166 | |||
| 167 | 176 | struct expr *xfrm_expr_alloc(const struct location *loc, | |
| 168 | uint8_t direction, | ||
| 169 | uint8_t spnum, | ||
| 170 | enum nft_xfrm_keys key) | ||
| 171 | { | ||
| 172 | struct expr *expr; | ||
| 173 | |||
| 174 | 176 | expr = expr_alloc(loc, EXPR_XFRM, | |
| 175 | 176 | xfrm_templates[key].dtype, | |
| 176 | 176 | xfrm_templates[key].byteorder, | |
| 177 | 176 | xfrm_templates[key].len); | |
| 178 | |||
| 179 | 176 | expr->xfrm.direction = direction; | |
| 180 | 176 | expr->xfrm.spnum = spnum; | |
| 181 | 176 | expr->xfrm.key = key; | |
| 182 | |||
| 183 | 176 | return expr; | |
| 184 | } | ||
| 185 |