]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
expr: add bitwise
authorPablo Neira Ayuso <pablo@netfilter.org>
Sun, 28 Oct 2012 17:07:14 +0000 (18:07 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 29 Oct 2012 22:41:26 +0000 (23:41 +0100)
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/libnftables/expr.h
src/Makefile.am
src/expr/bitwise.c [new file with mode: 0644]
src/expr_ops.c

index 09cd2e0e5ce0c631c6d28f67094a81ea966090ea..cddfbcf8cb0abdbe79586a1630f08bb36be9f60a 100644 (file)
@@ -61,6 +61,14 @@ enum {
        NFT_EXPR_CTR_BYTES,
 };
 
+enum {
+       NFT_EXPR_BITWISE_SREG   = NFT_RULE_EXPR_ATTR_BASE,
+       NFT_EXPR_BITWISE_DREG,
+       NFT_EXPR_BITWISE_LEN,
+       NFT_EXPR_BITWISE_MASK,
+       NFT_EXPR_BITWISE_XOR,
+};
+
 enum {
        NFT_EXPR_TG_NAME        = NFT_RULE_EXPR_ATTR_BASE,
        NFT_EXPR_TG_REV,
index b1c104428e760bfa8c1c43a9d52f3bd6d05d0dd7..4cca790ab0bfc6006dcec09ad7e69e271fb1a644 100644 (file)
@@ -9,6 +9,7 @@ libnftables_la_SOURCES = table.c                \
                         rule.c                 \
                         expr.c                 \
                         expr_ops.c             \
+                        expr/bitwise.c         \
                         expr/cmp.c             \
                         expr/counter.c         \
                         expr/data_reg.c        \
diff --git a/src/expr/bitwise.c b/src/expr/bitwise.c
new file mode 100644 (file)
index 0000000..8275758
--- /dev/null
@@ -0,0 +1,235 @@
+/*
+ * (C) 2012 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"
+
+struct nft_expr_bitwise {
+       enum nft_registers      sreg;
+       enum nft_registers      dreg;
+       unsigned int            len;
+       union nft_data_reg      mask;
+       union nft_data_reg      xor;
+};
+
+static int
+nft_rule_expr_bitwise_set(struct nft_rule_expr *e, uint16_t type,
+                         const void *data, size_t data_len)
+{
+       struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
+
+       switch(type) {
+       case NFT_EXPR_BITWISE_SREG:
+               bitwise->sreg = *((uint32_t *)data);
+               break;
+       case NFT_EXPR_BITWISE_DREG:
+               bitwise->dreg = *((uint32_t *)data);
+               break;
+       case NFT_EXPR_BITWISE_LEN:
+               bitwise->len = *((unsigned int *)data);
+               break;
+       case NFT_EXPR_BITWISE_MASK:
+               memcpy(&bitwise->mask.val, data, data_len);
+               bitwise->mask.len = data_len;
+               break;
+       case NFT_EXPR_BITWISE_XOR:
+               memcpy(&bitwise->xor.val, data, data_len);
+               bitwise->xor.len = data_len;
+               break;
+       default:
+               return -1;
+       }
+       return 0;
+}
+
+static const void *
+nft_rule_expr_bitwise_get(struct nft_rule_expr *e, uint16_t type, size_t *data_len)
+{
+       struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
+
+       switch(type) {
+       case NFT_EXPR_BITWISE_SREG:
+               if (e->flags & (1 << NFT_EXPR_BITWISE_SREG)) {
+                       *data_len = sizeof(bitwise->sreg);
+                       return &bitwise->sreg;
+               }
+               break;
+       case NFT_EXPR_BITWISE_DREG:
+               if (e->flags & (1 << NFT_EXPR_BITWISE_DREG)) {
+                       *data_len = sizeof(bitwise->dreg);
+                       return &bitwise->dreg;
+               }
+               break;
+       case NFT_EXPR_BITWISE_LEN:
+               if (e->flags & (1 << NFT_EXPR_BITWISE_LEN)) {
+                       *data_len = sizeof(bitwise->len);
+                       return &bitwise->len;
+               }
+               break;
+       case NFT_EXPR_BITWISE_MASK:
+               if (e->flags & (1 << NFT_EXPR_BITWISE_MASK)) {
+                       *data_len = bitwise->mask.len;
+                       return &bitwise->mask.val;
+               }
+               break;
+       case NFT_EXPR_BITWISE_XOR:
+               if (e->flags & (1 << NFT_EXPR_BITWISE_XOR)) {
+                       *data_len = bitwise->xor.len;
+                       return &bitwise->xor.val;
+               }
+               break;
+       default:
+               break;
+       }
+       return NULL;
+}
+
+static int nft_rule_expr_bitwise_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_BITWISE_MAX) < 0)
+               return MNL_CB_OK;
+
+       switch(type) {
+       case NFTA_BITWISE_SREG:
+       case NFTA_BITWISE_DREG:
+       case NFTA_BITWISE_LEN:
+               if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
+                       perror("mnl_attr_validate");
+                       return MNL_CB_ERROR;
+               }
+               break;
+       case NFTA_BITWISE_MASK:
+       case NFTA_BITWISE_XOR:
+               if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) {
+                       perror("mnl_attr_validate");
+                       return MNL_CB_ERROR;
+               }
+               break;
+       }
+
+       tb[type] = attr;
+       return MNL_CB_OK;
+}
+
+static void
+nft_rule_expr_bitwise_build(struct nlmsghdr *nlh, struct nft_rule_expr *e)
+{
+       struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
+
+       if (e->flags & (1 << NFT_EXPR_BITWISE_SREG))
+               mnl_attr_put_u32(nlh, NFTA_BITWISE_SREG, htonl(bitwise->sreg));
+       if (e->flags & (1 << NFT_EXPR_BITWISE_DREG))
+               mnl_attr_put_u32(nlh, NFTA_BITWISE_DREG, htonl(bitwise->dreg));
+       if (e->flags & (1 << NFT_EXPR_BITWISE_LEN))
+               mnl_attr_put_u32(nlh, NFTA_BITWISE_LEN, htonl(bitwise->len));
+       if (e->flags & (1 << NFT_EXPR_BITWISE_MASK)) {
+               struct nlattr *nest;
+
+               nest = mnl_attr_nest_start(nlh, NFTA_BITWISE_MASK);
+               mnl_attr_put(nlh, NFTA_DATA_VALUE, bitwise->mask.len,
+                               bitwise->mask.val);
+               mnl_attr_nest_end(nlh, nest);
+       }
+       if (e->flags & (1 << NFT_EXPR_BITWISE_XOR)) {
+               struct nlattr *nest;
+
+               nest = mnl_attr_nest_start(nlh, NFTA_BITWISE_XOR);
+               mnl_attr_put(nlh, NFTA_DATA_VALUE, bitwise->xor.len,
+                               bitwise->xor.val);
+               mnl_attr_nest_end(nlh, nest);
+       }
+}
+
+static int
+nft_rule_expr_bitwise_parse(struct nft_rule_expr *e, struct nlattr *attr)
+{
+       struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
+       struct nlattr *tb[NFTA_BITWISE_MAX+1] = {};
+       int ret = 0;
+
+       if (mnl_attr_parse_nested(attr, nft_rule_expr_bitwise_cb, tb) < 0)
+               return -1;
+
+       if (tb[NFTA_BITWISE_SREG]) {
+               bitwise->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_SREG]));
+               e->flags |= (1 << NFT_EXPR_BITWISE_SREG);
+       }
+       if (tb[NFTA_BITWISE_DREG]) {
+               bitwise->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_DREG]));
+               e->flags |= (1 << NFT_EXPR_BITWISE_DREG);
+       }
+       if (tb[NFTA_BITWISE_LEN]) {
+               bitwise->len = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_LEN]));
+               e->flags |= (1 << NFT_EXPR_BITWISE_LEN);
+       }
+       if (tb[NFTA_BITWISE_MASK]) {
+               ret = nft_parse_data(&bitwise->mask, tb[NFTA_BITWISE_MASK], NULL);
+               e->flags |= (1 << NFTA_BITWISE_MASK);
+       }
+       if (tb[NFTA_BITWISE_XOR]) {
+               ret = nft_parse_data(&bitwise->xor, tb[NFTA_BITWISE_XOR], NULL);
+               e->flags |= (1 << NFTA_BITWISE_XOR);
+       }
+
+       return ret;
+}
+
+static int
+nft_rule_expr_bitwise_snprintf(char *buf, size_t size, struct nft_rule_expr *e)
+{
+       struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
+       int len = size, offset = 0, ret, i;
+
+       ret = snprintf(buf, len, "sreg=%u dreg=%u ",
+                       bitwise->sreg, bitwise->dreg);
+       SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+       ret = snprintf(buf+offset, len, " mask=");
+       SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+       for (i=0; i<bitwise->mask.len/sizeof(uint32_t); i++) {
+               ret = snprintf(buf+offset, len, "%.8x ", bitwise->mask.val[i]);
+               SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+       }
+
+       ret = snprintf(buf+offset, len, " xor=");
+       SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+       for (i=0; i<bitwise->xor.len/sizeof(uint32_t); i++) {
+               ret = snprintf(buf+offset, len, "%.8x ", bitwise->xor.val[i]);
+               SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+       }
+
+       return offset;
+}
+
+struct expr_ops expr_ops_bitwise = {
+       .name           = "bitwise",
+       .alloc_len      = sizeof(struct nft_expr_bitwise),
+       .max_attr       = NFTA_BITWISE_MAX,
+       .set            = nft_rule_expr_bitwise_set,
+       .get            = nft_rule_expr_bitwise_get,
+       .parse          = nft_rule_expr_bitwise_parse,
+       .build          = nft_rule_expr_bitwise_build,
+       .snprintf       = nft_rule_expr_bitwise_snprintf,
+};
index 2b21d96bdca6707574824ece3c58972da60eadd6..9d121a07e31a88c7df7f73b3084a3cbb218173e7 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "expr_ops.h"
 
+extern struct expr_ops expr_ops_bitwise;
 extern struct expr_ops expr_ops_cmp;
 extern struct expr_ops expr_ops_counter;
 extern struct expr_ops expr_ops_immediate;
@@ -11,6 +12,7 @@ extern struct expr_ops expr_ops_payload;
 extern struct expr_ops expr_ops_target;
 
 struct expr_ops *expr_ops[] = {
+       &expr_ops_bitwise,
        &expr_ops_cmp,
        &expr_ops_counter,
        &expr_ops_immediate,