This patch adds synproxy stateful object support. Signed-off-by: Fernando Fernandez Mancera <ffmancera@xxxxxxxxxx> --- include/libnftnl/object.h | 6 ++ include/linux/netfilter/nf_tables.h | 3 +- include/obj.h | 6 ++ src/Makefile.am | 1 + src/obj/synproxy.c | 161 ++++++++++++++++++++++++++++ src/object.c | 1 + 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/obj/synproxy.c diff --git a/include/libnftnl/object.h b/include/libnftnl/object.h index cce0713..c5ea88e 100644 --- a/include/libnftnl/object.h +++ b/include/libnftnl/object.h @@ -86,6 +86,12 @@ enum { NFTNL_OBJ_LIMIT_FLAGS, }; +enum { + NFTNL_OBJ_SYNPROXY_MSS = NFTNL_OBJ_BASE, + NFTNL_OBJ_SYNPROXY_WSCALE, + NFTNL_OBJ_SYNPROXY_FLAGS, +}; + enum { NFTNL_OBJ_TUNNEL_ID = NFTNL_OBJ_BASE, NFTNL_OBJ_TUNNEL_IPV4_SRC, diff --git a/include/linux/netfilter/nf_tables.h b/include/linux/netfilter/nf_tables.h index 75e083e..2e49bc6 100644 --- a/include/linux/netfilter/nf_tables.h +++ b/include/linux/netfilter/nf_tables.h @@ -1481,7 +1481,8 @@ enum nft_ct_expectation_attributes { #define NFT_OBJECT_CT_TIMEOUT 7 #define NFT_OBJECT_SECMARK 8 #define NFT_OBJECT_CT_EXPECT 9 -#define __NFT_OBJECT_MAX 10 +#define NFT_OBJECT_SYNPROXY 10 +#define __NFT_OBJECT_MAX 11 #define NFT_OBJECT_MAX (__NFT_OBJECT_MAX - 1) /** diff --git a/include/obj.h b/include/obj.h index 9394d79..10f806c 100644 --- a/include/obj.h +++ b/include/obj.h @@ -56,6 +56,11 @@ struct nftnl_obj { uint32_t type; uint32_t flags; } limit; + struct nftnl_obj_synproxy { + uint16_t mss; + uint8_t wscale; + uint32_t flags; + } synproxy; struct nftnl_obj_tunnel { uint32_t id; uint32_t src_v4; @@ -108,6 +113,7 @@ extern struct obj_ops obj_ops_ct_helper; extern struct obj_ops obj_ops_ct_timeout; extern struct obj_ops obj_ops_ct_expect; extern struct obj_ops obj_ops_limit; +extern struct obj_ops obj_ops_synproxy; extern struct obj_ops obj_ops_tunnel; extern struct obj_ops obj_ops_secmark; diff --git a/src/Makefile.am b/src/Makefile.am index f16422c..90b1967 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,6 +64,7 @@ libnftnl_la_SOURCES = utils.c \ obj/quota.c \ obj/tunnel.c \ obj/limit.c \ + obj/synproxy.c \ obj/ct_timeout.c \ obj/secmark.c \ obj/ct_expect.c \ diff --git a/src/obj/synproxy.c b/src/obj/synproxy.c new file mode 100644 index 0000000..56ebc85 --- /dev/null +++ b/src/obj/synproxy.c @@ -0,0 +1,161 @@ +#include <stdio.h> +#include <stdint.h> +#include <arpa/inet.h> +#include <errno.h> +#include <inttypes.h> + +#include <linux/netfilter/nf_tables.h> + +#include "internal.h" +#include <libmnl/libmnl.h> +#include <libnftnl/object.h> + +#include "obj.h" + +static int nftnl_obj_synproxy_set(struct nftnl_obj *e, uint16_t type, + const void *data, uint32_t data_len) +{ + struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e); + + switch (type) { + case NFTNL_OBJ_SYNPROXY_MSS: + synproxy->mss = *((uint16_t *)data); + break; + case NFTNL_OBJ_SYNPROXY_WSCALE: + synproxy->wscale = *((uint8_t *)data); + break; + case NFTNL_OBJ_SYNPROXY_FLAGS: + synproxy->flags = *((uint32_t *)data); + break; + default: + return -1; + } + return 0; +} + +static const void *nftnl_obj_synproxy_get(const struct nftnl_obj *e, + uint16_t type, uint32_t *data_len) +{ + struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e); + + switch (type) { + case NFTNL_OBJ_SYNPROXY_MSS: + *data_len = sizeof(synproxy->mss); + return &synproxy->mss; + case NFTNL_OBJ_SYNPROXY_WSCALE: + *data_len = sizeof(synproxy->wscale); + return &synproxy->wscale; + case NFTNL_OBJ_SYNPROXY_FLAGS: + *data_len = sizeof(synproxy->flags); + return &synproxy->flags; + } + return NULL; +} + +static int nftnl_obj_synproxy_cb(const struct nlattr *attr, void *data) +{ + int type = mnl_attr_get_type(attr); + const struct nlattr **tb = data; + + if (mnl_attr_type_valid(attr, NFTA_SYNPROXY_MAX) < 0) + return MNL_CB_OK; + + switch (type) { + case NFTA_SYNPROXY_MSS: + if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) + abi_breakage(); + break; + case NFTA_SYNPROXY_WSCALE: + if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) + abi_breakage(); + break; + case NFTA_SYNPROXY_FLAGS: + if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) + abi_breakage(); + break; + } + + tb[type] = attr; + return MNL_CB_OK; +} + +static void nftnl_obj_synproxy_build(struct nlmsghdr *nlh, + const struct nftnl_obj *e) +{ + struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e); + + if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_MSS)) + mnl_attr_put_u16(nlh, NFTA_SYNPROXY_MSS, htons(synproxy->mss)); + if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_WSCALE)) + mnl_attr_put_u8(nlh, NFTA_SYNPROXY_WSCALE, synproxy->wscale); + if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_FLAGS)) + mnl_attr_put_u32(nlh, NFTA_SYNPROXY_FLAGS, + htonl(synproxy->flags)); +} + +static int nftnl_obj_synproxy_parse(struct nftnl_obj *e, struct nlattr *attr) +{ + struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e); + struct nlattr *tb[NFTA_SYNPROXY_MAX + 1] = {}; + + if (mnl_attr_parse_nested(attr, nftnl_obj_synproxy_cb, tb) < 0) + return -1; + + if (tb[NFTA_SYNPROXY_MSS]) { + synproxy->mss = ntohs(mnl_attr_get_u16(tb[NFTA_SYNPROXY_MSS])); + e->flags |= (1 << NFTNL_OBJ_SYNPROXY_MSS); + } + if (tb[NFTA_SYNPROXY_WSCALE]) { + synproxy->wscale = mnl_attr_get_u8(tb[NFTA_SYNPROXY_WSCALE]); + e->flags |= (1 << NFTNL_OBJ_SYNPROXY_WSCALE); + } + if (tb[NFTA_SYNPROXY_FLAGS]) { + synproxy->flags = ntohl(mnl_attr_get_u32(tb[NFTA_SYNPROXY_FLAGS])); + e->flags |= (1 << NFTNL_OBJ_SYNPROXY_FLAGS); + } + + return 0; +} + +static int nftnl_obj_synproxy_snprintf_default(char *buf, size_t size, + const struct nftnl_obj *e) +{ + struct nftnl_obj_synproxy *synproxy = nftnl_obj_data(e); + int ret, offset = 0, len = size; + + if (e->flags & (1 << NFTNL_OBJ_SYNPROXY_MSS) && + e->flags & (1 << NFTNL_OBJ_SYNPROXY_WSCALE)) { + ret = snprintf(buf, len, "mss %u wscale %u ", synproxy->mss, + synproxy->wscale); + SNPRINTF_BUFFER_SIZE(ret, len, offset); + } + + return offset; +} + +static int nftnl_obj_synproxy_snprintf(char *buf, size_t len, uint32_t type, + uint32_t flags, + const struct nftnl_obj *e) +{ + switch (type) { + case NFTNL_OUTPUT_DEFAULT: + return nftnl_obj_synproxy_snprintf_default(buf, len, e); + case NFTNL_OUTPUT_XML: + case NFTNL_OUTPUT_JSON: + default: + break; + } + return -1; +} + +struct obj_ops obj_ops_synproxy = { + .name = "synproxy", + .type = NFT_OBJECT_SYNPROXY, + .alloc_len = sizeof(struct nftnl_obj_synproxy), + .max_attr = NFTA_SYNPROXY_MAX, + .set = nftnl_obj_synproxy_set, + .get = nftnl_obj_synproxy_get, + .parse = nftnl_obj_synproxy_parse, + .build = nftnl_obj_synproxy_build, + .snprintf = nftnl_obj_synproxy_snprintf, +}; diff --git a/src/object.c b/src/object.c index d8c87ee..ed8e36d 100644 --- a/src/object.c +++ b/src/object.c @@ -34,6 +34,7 @@ static struct obj_ops *obj_ops[__NFT_OBJECT_MAX] = { [NFT_OBJECT_CT_TIMEOUT] = &obj_ops_ct_timeout, [NFT_OBJECT_SECMARK] = &obj_ops_secmark, [NFT_OBJECT_CT_EXPECT] = &obj_ops_ct_expect, + [NFT_OBJECT_SYNPROXY] = &obj_ops_synproxy, }; static struct obj_ops *nftnl_obj_ops_lookup(uint32_t type) -- 2.20.1