diff options
author | Pablo Neira Ayuso <pablo@netfilter.org> | 2013-02-05 14:01:39 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2013-02-07 22:47:17 +0100 |
commit | 1cef3296c64244736a000ab0d146d05ca1ac4b8b (patch) | |
tree | e66c2e5756a29a1c8c1a64830b097667f2da1428 /src/expr/lookup.c | |
parent | d2a0036dd3e16d40f4039a11677241827ea057c5 (diff) |
expr: add lookup expression for set-based lookups
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/expr/lookup.c')
-rw-r--r-- | src/expr/lookup.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/expr/lookup.c b/src/expr/lookup.c new file mode 100644 index 0000000..8e533c4 --- /dev/null +++ b/src/expr/lookup.c @@ -0,0 +1,175 @@ +/* + * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This code has been sponsored by Sophos Astaro <http://www.sophos.com> + */ + +#include "internal.h" + +#include <stdio.h> +#include <stdint.h> +#include <string.h> /* for memcpy */ +#include <arpa/inet.h> +#include <libmnl/libmnl.h> +#include <linux/netfilter/nf_tables.h> +#include <libnftables/expr.h> +#include "data_reg.h" +#include "expr_ops.h" + +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 +#endif + +struct nft_expr_lookup { + enum nft_registers sreg; + enum nft_registers dreg; + char set_name[IFNAMSIZ]; +}; + +static int +nft_rule_expr_lookup_set(struct nft_rule_expr *e, uint16_t type, + const void *data, size_t data_len) +{ + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + + switch(type) { + case NFT_EXPR_LOOKUP_SREG: + lookup->sreg = *((uint32_t *)data); + break; + case NFT_EXPR_LOOKUP_DREG: + lookup->dreg = *((uint32_t *)data); + break; + case NFT_EXPR_LOOKUP_SET: + memcpy(lookup->set_name, data, IFNAMSIZ); + lookup->set_name[IFNAMSIZ-1] = '\0'; + break; + default: + return -1; + } + return 0; +} + +static const void * +nft_rule_expr_lookup_get(struct nft_rule_expr *e, uint16_t type, size_t *data_len) +{ + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + + switch(type) { + case NFT_EXPR_LOOKUP_SREG: + if (e->flags & (1 << NFT_EXPR_LOOKUP_SREG)) { + *data_len = sizeof(lookup->sreg); + return &lookup->sreg; + } + break; + case NFT_EXPR_LOOKUP_DREG: + if (e->flags & (1 << NFT_EXPR_LOOKUP_DREG)) { + *data_len = sizeof(lookup->dreg); + return &lookup->dreg; + } + break; + case NFT_EXPR_LOOKUP_SET: + if (e->flags & (1 << NFT_EXPR_LOOKUP_SET)) + return lookup->set_name; + break; + default: + break; + } + return NULL; +} + +static int nft_rule_expr_lookup_cb(const struct nlattr *attr, void *data) +{ + const struct nlattr **tb = data; + int type = mnl_attr_get_type(attr); + + if (mnl_attr_type_valid(attr, NFTA_LOOKUP_MAX) < 0) + return MNL_CB_OK; + + switch(type) { + case NFTA_LOOKUP_SREG: + case NFTA_LOOKUP_DREG: + if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { + perror("mnl_attr_validate"); + return MNL_CB_ERROR; + } + break; + case NFTA_LOOKUP_SET: + if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { + perror("mnl_attr_validate"); + return MNL_CB_ERROR; + } + break; + } + + tb[type] = attr; + return MNL_CB_OK; +} + +static void +nft_rule_expr_lookup_build(struct nlmsghdr *nlh, struct nft_rule_expr *e) +{ + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + + if (e->flags & (1 << NFT_EXPR_LOOKUP_SREG)) + mnl_attr_put_u32(nlh, NFTA_LOOKUP_SREG, htonl(lookup->sreg)); + if (e->flags & (1 << NFT_EXPR_LOOKUP_DREG)) + mnl_attr_put_u32(nlh, NFTA_LOOKUP_DREG, htonl(lookup->dreg)); + if (e->flags & (1 << NFT_EXPR_LOOKUP_SET)) + mnl_attr_put_strz(nlh, NFTA_LOOKUP_SET, lookup->set_name); +} + +static int +nft_rule_expr_lookup_parse(struct nft_rule_expr *e, struct nlattr *attr) +{ + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + struct nlattr *tb[NFTA_LOOKUP_MAX+1] = {}; + int ret = 0; + + if (mnl_attr_parse_nested(attr, nft_rule_expr_lookup_cb, tb) < 0) + return -1; + + if (tb[NFTA_LOOKUP_SREG]) { + lookup->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_LOOKUP_SREG])); + e->flags |= (1 << NFT_EXPR_LOOKUP_SREG); + } + if (tb[NFTA_LOOKUP_DREG]) { + lookup->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_LOOKUP_DREG])); + e->flags |= (1 << NFT_EXPR_LOOKUP_DREG); + } + if (tb[NFTA_LOOKUP_SET]) { + strcpy(lookup->set_name, mnl_attr_get_str(tb[NFTA_LOOKUP_SET])); + e->flags |= (1 << NFT_EXPR_LOOKUP_SET); + } + + return ret; +} + +static int +nft_rule_expr_lookup_snprintf(char *buf, size_t size, uint32_t type, + uint32_t flags, struct nft_rule_expr *e) +{ + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + int len = size, offset = 0, ret; + + ret = snprintf(buf, len, "set=%s sreg=%u dreg=%u\n", + lookup->set_name, lookup->sreg, lookup->dreg); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + return offset; +} + +struct expr_ops expr_ops_lookup = { + .name = "lookup", + .alloc_len = sizeof(struct nft_expr_lookup), + .max_attr = NFTA_LOOKUP_MAX, + .set = nft_rule_expr_lookup_set, + .get = nft_rule_expr_lookup_get, + .parse = nft_rule_expr_lookup_parse, + .build = nft_rule_expr_lookup_build, + .snprintf = nft_rule_expr_lookup_snprintf, +}; |