| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * Copyright (c) 2018 Máté Eckl <[email protected]> |
| 4 | */ |
| 5 | |
| 6 | #include "internal.h" |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdint.h> |
| 10 | #include <limits.h> |
| 11 | #include <string.h> |
| 12 | #include <errno.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <libmnl/libmnl.h> |
| 15 | #include <linux/netfilter/nf_tables.h> |
| 16 | #include <libnftnl/expr.h> |
| 17 | #include <libnftnl/rule.h> |
| 18 | |
| 19 | struct nftnl_expr_tproxy { |
| 20 | enum nft_registers sreg_addr; |
| 21 | enum nft_registers sreg_port; |
| 22 | int family; |
| 23 | }; |
| 24 | |
| 25 | static int |
| 26 | nftnl_expr_tproxy_set(struct nftnl_expr *e, uint16_t type, |
| 27 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 28 | { |
| 29 | struct nftnl_expr_tproxy *tproxy = nftnl_expr_data(e); |
| 30 | |
| 31 | switch(type) { |
| 32 | case NFTNL_EXPR_TPROXY_FAMILY: |
| 33 | memcpy(&tproxy->family, data, data_len); |
| 34 | break; |
| 35 | case NFTNL_EXPR_TPROXY_REG_ADDR: |
| 36 | memcpy(&tproxy->sreg_addr, data, data_len); |
| 37 | break; |
| 38 | case NFTNL_EXPR_TPROXY_REG_PORT: |
| 39 | memcpy(&tproxy->sreg_port, data, data_len); |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static const void * |
| 47 | nftnl_expr_tproxy_get(const struct nftnl_expr *e, uint16_t type, |
| 48 | uint32_t *data_len) |
| 49 | { |
| 50 | struct nftnl_expr_tproxy *tproxy = nftnl_expr_data(e); |
| 51 | |
| 52 | switch(type) { |
| 53 | case NFTNL_EXPR_TPROXY_FAMILY: |
| 54 | *data_len = sizeof(tproxy->family); |
| 55 | return &tproxy->family; |
| 56 | case NFTNL_EXPR_TPROXY_REG_ADDR: |
| 57 | *data_len = sizeof(tproxy->sreg_addr); |
| 58 | return &tproxy->sreg_addr; |
| 59 | case NFTNL_EXPR_TPROXY_REG_PORT: |
| 60 | *data_len = sizeof(tproxy->sreg_port); |
| 61 | return &tproxy->sreg_port; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | static int nftnl_expr_tproxy_cb(const struct nlattr *attr, void *data) |
| 67 | { |
| 68 | const struct nlattr **tb = data; |
| 69 | int type = mnl_attr_get_type(attr); |
| 70 | |
| 71 | if (mnl_attr_type_valid(attr, NFTA_TPROXY_MAX) < 0) |
| 72 | return MNL_CB_OK; |
| 73 | |
| 74 | switch(type) { |
| 75 | case NFTA_TPROXY_FAMILY: |
| 76 | case NFTA_TPROXY_REG_ADDR: |
| 77 | case NFTA_TPROXY_REG_PORT: |
| 78 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 79 | abi_breakage(); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | tb[type] = attr; |
| 84 | return MNL_CB_OK; |
| 85 | } |
| 86 | |
| 87 | static int |
| 88 | nftnl_expr_tproxy_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 89 | { |
| 90 | struct nftnl_expr_tproxy *tproxy = nftnl_expr_data(e); |
| 91 | struct nlattr *tb[NFTA_TPROXY_MAX + 1] = {}; |
| 92 | |
| 93 | if (mnl_attr_parse_nested(attr, nftnl_expr_tproxy_cb, tb) < 0) |
| 94 | return -1; |
| 95 | |
| 96 | if (tb[NFTA_TPROXY_FAMILY]) { |
| 97 | tproxy->family = ntohl(mnl_attr_get_u32(tb[NFTA_TPROXY_FAMILY])); |
| 98 | e->flags |= (1 << NFTNL_EXPR_TPROXY_FAMILY); |
| 99 | } |
| 100 | if (tb[NFTA_TPROXY_REG_ADDR]) { |
| 101 | tproxy->sreg_addr = |
| 102 | ntohl(mnl_attr_get_u32(tb[NFTA_TPROXY_REG_ADDR])); |
| 103 | e->flags |= (1 << NFTNL_EXPR_TPROXY_REG_ADDR); |
| 104 | } |
| 105 | if (tb[NFTA_TPROXY_REG_PORT]) { |
| 106 | tproxy->sreg_port = |
| 107 | ntohl(mnl_attr_get_u32(tb[NFTA_TPROXY_REG_PORT])); |
| 108 | e->flags |= (1 << NFTNL_EXPR_TPROXY_REG_PORT); |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | nftnl_expr_tproxy_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 116 | { |
| 117 | struct nftnl_expr_tproxy *tproxy = nftnl_expr_data(e); |
| 118 | |
| 119 | if (e->flags & (1 << NFTNL_EXPR_TPROXY_FAMILY)) |
| 120 | mnl_attr_put_u32(nlh, NFTA_TPROXY_FAMILY, htonl(tproxy->family)); |
| 121 | |
| 122 | if (e->flags & (1 << NFTNL_EXPR_TPROXY_REG_ADDR)) |
| 123 | mnl_attr_put_u32(nlh, NFTA_TPROXY_REG_ADDR, |
| 124 | htonl(tproxy->sreg_addr)); |
| 125 | |
| 126 | if (e->flags & (1 << NFTNL_EXPR_TPROXY_REG_PORT)) |
| 127 | mnl_attr_put_u32(nlh, NFTA_TPROXY_REG_PORT, |
| 128 | htonl(tproxy->sreg_port)); |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | nftnl_expr_tproxy_snprintf(char *buf, size_t remain, |
| 133 | uint32_t flags, const struct nftnl_expr *e) |
| 134 | { |
| 135 | struct nftnl_expr_tproxy *tproxy = nftnl_expr_data(e); |
| 136 | int offset = 0, ret = 0; |
| 137 | |
| 138 | if (tproxy->family != NFTA_TPROXY_UNSPEC) { |
| 139 | ret = snprintf(buf + offset, remain, "%s ", |
| 140 | nftnl_family2str(tproxy->family)); |
| 141 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 142 | } |
| 143 | |
| 144 | if (e->flags & (1 << NFTNL_EXPR_TPROXY_REG_ADDR)) { |
| 145 | ret = snprintf(buf + offset, remain, |
| 146 | "addr reg %u ", tproxy->sreg_addr); |
| 147 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 148 | } |
| 149 | |
| 150 | if (e->flags & (1 << NFTNL_EXPR_TPROXY_REG_PORT)) { |
| 151 | ret = snprintf(buf + offset, remain, |
| 152 | "port reg %u ", tproxy->sreg_port); |
| 153 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 154 | } |
| 155 | |
| 156 | return offset; |
| 157 | } |
| 158 | |
| 159 | static struct attr_policy tproxy_attr_policy[__NFTNL_EXPR_TPROXY_MAX] = { |
| 160 | [NFTNL_EXPR_TPROXY_FAMILY] = { .maxlen = sizeof(uint32_t) }, |
| 161 | [NFTNL_EXPR_TPROXY_REG_ADDR] = { .maxlen = sizeof(uint32_t) }, |
| 162 | [NFTNL_EXPR_TPROXY_REG_PORT] = { .maxlen = sizeof(uint32_t) }, |
| 163 | }; |
| 164 | |
| 165 | struct expr_ops expr_ops_tproxy = { |
| 166 | .name = "tproxy", |
| 167 | .alloc_len = sizeof(struct nftnl_expr_tproxy), |
| 168 | .nftnl_max_attr = __NFTNL_EXPR_TPROXY_MAX - 1, |
| 169 | .attr_policy = tproxy_attr_policy, |
| 170 | .set = nftnl_expr_tproxy_set, |
| 171 | .get = nftnl_expr_tproxy_get, |
| 172 | .parse = nftnl_expr_tproxy_parse, |
| 173 | .build = nftnl_expr_tproxy_build, |
| 174 | .output = nftnl_expr_tproxy_snprintf, |
| 175 | }; |