@@ -13,6 +13,7 @@ $(LIBBPF_A):
userprogs := bpfilter_umh
bpfilter_umh-objs := main.o logger.o map-common.o
bpfilter_umh-objs += context.o codegen.o
+bpfilter_umh-objs += match.o
bpfilter_umh-userldlibs := $(LIBBPF_A) -lelf -lz
userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
@@ -8,11 +8,54 @@
#include "context.h"
+#include <linux/kernel.h>
+
+#include <string.h>
+
+#include "logger.h"
+#include "map-common.h"
+#include "match.h"
+
+static const struct match_ops *match_ops[] = { };
+
+static int init_match_ops_map(struct context *ctx)
+{
+ int r;
+
+ r = create_map(&ctx->match_ops_map, ARRAY_SIZE(match_ops));
+ if (r) {
+ BFLOG_ERR("failed to create matches map: %s", STRERR(r));
+ return r;
+ }
+
+ for (int i = 0; i < ARRAY_SIZE(match_ops); ++i) {
+ const struct match_ops *m = match_ops[i];
+
+ r = map_upsert(&ctx->match_ops_map, m->name, (void *)m);
+ if (r) {
+ BFLOG_ERR("failed to upsert in matches map: %s",
+ STRERR(r));
+ return r;
+ }
+ }
+
+ return 0;
+}
+
int create_context(struct context *ctx)
{
+ int r;
+
+ r = init_match_ops_map(ctx);
+ if (r) {
+ BFLOG_ERR("failed to initialize matches map: %s", STRERR(r));
+ return r;
+ }
+
return 0;
}
void free_context(struct context *ctx)
{
+ free_map(&ctx->match_ops_map);
}
@@ -7,7 +7,10 @@
#ifndef NET_BPFILTER_CONTEXT_H
#define NET_BPFILTER_CONTEXT_H
+#include <search.h>
+
struct context {
+ struct hsearch_data match_ops_map;
};
int create_context(struct context *ctx);
new file mode 100644
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 Telegram FZ-LLC
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#define _GNU_SOURCE
+
+#include "match.h"
+
+#include <linux/err.h>
+
+#include <errno.h>
+#include <string.h>
+
+#include "context.h"
+#include "logger.h"
+#include "map-common.h"
+
+int init_match(struct context *ctx, const struct bpfilter_ipt_match *ipt_match,
+ struct match *match)
+{
+ const size_t maxlen = sizeof(ipt_match->u.user.name);
+ const struct match_ops *found;
+ int r;
+
+ if (strnlen(ipt_match->u.user.name, maxlen) == maxlen) {
+ BFLOG_ERR("failed to init match: name too long");
+ return -EINVAL;
+ }
+
+ found = map_find(&ctx->match_ops_map, ipt_match->u.user.name);
+ if (IS_ERR(found)) {
+ BFLOG_ERR("failed to find match by name: '%s'",
+ ipt_match->u.user.name);
+ return PTR_ERR(found);
+ }
+
+ if (found->size + sizeof(*ipt_match) != ipt_match->u.match_size ||
+ found->revision != ipt_match->u.user.revision) {
+ BFLOG_ERR("invalid match: '%s'", ipt_match->u.user.name);
+ return -EINVAL;
+ }
+
+ r = found->check(ctx, ipt_match);
+ if (r) {
+ BFLOG_ERR("match check failed: %s", STRERR(r));
+ return r;
+ }
+
+ match->match_ops = found;
+ match->ipt_match = ipt_match;
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2021 Telegram FZ-LLC
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#ifndef NET_BPFILTER_MATCH_H
+#define NET_BPFILTER_MATCH_H
+
+#include "../../include/uapi/linux/bpfilter.h"
+
+#include <stdint.h>
+
+struct bpfilter_ipt_match;
+struct codegen;
+struct context;
+struct match;
+
+struct match_ops {
+ char name[BPFILTER_EXTENSION_MAXNAMELEN];
+ uint8_t revision;
+ uint16_t size;
+ int (*check)(struct context *ctx, const struct bpfilter_ipt_match *ipt_match);
+ int (*gen_inline)(struct codegen *ctx, const struct match *match);
+};
+
+struct match {
+ const struct match_ops *match_ops;
+ const struct bpfilter_ipt_match *ipt_match;
+};
+
+int init_match(struct context *ctx, const struct bpfilter_ipt_match *ipt_match,
+ struct match *match);
+
+#endif // NET_BPFILTER_MATCH_H
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
tools/**
test_map
+test_match
@@ -11,6 +11,7 @@ BPFDIR := $(LIBDIR)/bpf
CFLAGS += -Wall -g -pthread -I$(TOOLSINCDIR) -I$(APIDIR) -I$(BPFILTERSRCDIR)
TEST_GEN_PROGS += test_map
+TEST_GEN_PROGS += test_match
KSFT_KHDR_INSTALL := 1
@@ -34,5 +35,11 @@ $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
BPFILTER_MAP_SRCS := $(BPFILTERSRCDIR)/map-common.c
BPFILTER_CODEGEN_SRCS := $(BPFILTERSRCDIR)/codegen.c $(BPFOBJ) -lelf -lz
+BPFILTER_MATCH_SRCS := $(BPFILTERSRCDIR)/match.c
+
+BPFILTER_COMMON_SRCS := $(BPFILTER_MAP_SRCS)
+BPFILTER_COMMON_SRCS += $(BPFILTERSRCDIR)/context.c $(BPFILTERSRCDIR)/logger.c
+BPFILTER_COMMON_SRCS += $(BPFILTER_MATCH_SRCS)
$(OUTPUT)/test_map: test_map.c $(BPFILTER_MAP_SRCS)
+$(OUTPUT)/test_match: test_match.c $(BPFILTER_COMMON_SRCS)
new file mode 100644
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef BPFILTER_UTIL_H
+#define BPFILTER_UTIL_H
+
+#include <linux/netfilter/x_tables.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+static inline void init_entry_match(struct xt_entry_match *match,
+ uint16_t size, uint8_t revision,
+ const char *name)
+{
+ memset(match, 0, sizeof(*match));
+ sprintf(match->u.user.name, "%s", name);
+ match->u.user.match_size = size;
+ match->u.user.revision = revision;
+}
+
+#endif // BPFILTER_UTIL_H
new file mode 100644
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter/xt_tcpudp.h>
+
+#include "../../kselftest_harness.h"
+
+#include "context.h"
+#include "logger.h"
+#include "match.h"
+
+#include "bpfilter_util.h"
+
+/**
+ * struct udp_match - Dummy test structure.
+ *
+ * This structure provides enough space to allow for name too long, so it
+ * doesn't overwrite anything.
+ */
+struct udp_match {
+ struct xt_entry_match ipt_match;
+ char placeholder[32];
+};
+
+FIXTURE(test_match_init)
+{
+ struct context ctx;
+ struct udp_match udp_match;
+ struct match match;
+};
+
+FIXTURE_SETUP(test_match_init)
+{
+ logger_set_file(stderr);
+ ASSERT_EQ(0, create_context(&self->ctx));
+};
+
+FIXTURE_TEARDOWN(test_match_init)
+{
+ free_context(&self->ctx);
+}
+
+TEST_F(test_match_init, name_too_long)
+{
+ init_entry_match(&self->udp_match.ipt_match, sizeof(self->udp_match), 0,
+ "this match name is supposed to be way too long...");
+
+ ASSERT_EQ(init_match(&self->ctx,
+ (const struct bpfilter_ipt_match *)&self->udp_match
+ .ipt_match,
+ &self->match),
+ -EINVAL);
+}
+
+TEST_F(test_match_init, not_found)
+{
+ init_entry_match(&self->udp_match.ipt_match, sizeof(self->udp_match), 0,
+ "doesn't exist");
+
+ ASSERT_EQ(init_match(&self->ctx,
+ (const struct bpfilter_ipt_match *)&self->udp_match
+ .ipt_match,
+ &self->match),
+ -ENOENT);
+}
+
+TEST_HARNESS_MAIN