From patchwork Thu Jun 3 10:14:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Banshchikov X-Patchwork-Id: 12296837 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB0ACC47096 for ; Thu, 3 Jun 2021 10:15:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AEA5A61287 for ; Thu, 3 Jun 2021 10:15:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229718AbhFCKQw (ORCPT ); Thu, 3 Jun 2021 06:16:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbhFCKQw (ORCPT ); Thu, 3 Jun 2021 06:16:52 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5592DC06175F for ; Thu, 3 Jun 2021 03:14:59 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id h3so3046215wmq.3 for ; Thu, 03 Jun 2021 03:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ubique-spb-ru.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jkMzs1gZP+Wiqtomowse5B6kMYYJOzZY6aRSXqIG0JI=; b=BP/RfwVZGL+MhCm8VqSNAn55REi0aIYV2Kcns2mEiX4rlB304AV6bA0avduvHA2ugd tQYNCkJhRzMcfuRyIxfdJLfXD8WDfBtY3GF0ZIWy0tnocK0MDMpf3AGgsJfh0pvWRdQB rzwloZoBNngrnmsYp1QLusYiPu/39qN7Y+/pKT1jDkIMqSRoQhrJz/1IrkGy5inB9SI8 Jpwpk1uU1XIeilRPBAqFv27nApn1WsktdjvuJhlVeDbMzoiyp2vZO4if8jPI4WwtCG1P mul3/zbII50GwBWZKV3azd7YsQ/IlslkMmU7kR2vVvO+R09Gjq3HJuGHejd+ez3tA8mr EjfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jkMzs1gZP+Wiqtomowse5B6kMYYJOzZY6aRSXqIG0JI=; b=SHhAdxkV91DW+815WF4mxWvn4ug+tjaaX+bh64dwPQ+fcXxQ4zn9bdutq64V7RFF1g nkF2AXBVgNLEvniv3Mqxm+gOygA9cPDlfDtUeXb5AuyhkfcX/W9sQGRhcAZvL+tsa8rb xjAznwdXrwQNJ5N7YGXZMSkhvT3I0XKVLUhUL85gEZrim+G0fOc7BBn0/dM72FOwqV3l ey0tJxkpB2KLhIQ0BB1TYrBODYGQMICIvJ6qorxxgd9x6zOA/5Q/EuGkQBfEx06/jgHl rEXAnsA3oL+1SBtrsCCwBnNaVob4L7QsMip14Lb4jqGt3PiiPgGiTLoI+FE8hlSfkgNh fblA== X-Gm-Message-State: AOAM533tvj/bG//3FET56A/dHeHIsot+vILChMUeqGFOC6Hr08cJqRYq qlUkjLqrtiR3DD6LxuGe2XUDH5tzAr02f0J7wJI= X-Google-Smtp-Source: ABdhPJxTdC/yusBsxx+f4anipFDmYksJN/Jq/7hqvQ9LgHWeydhYim9rEbsUbTSji2FChKlxWnC/9g== X-Received: by 2002:a1c:7907:: with SMTP id l7mr20748100wme.147.1622715297711; Thu, 03 Jun 2021 03:14:57 -0700 (PDT) Received: from localhost ([154.21.15.43]) by smtp.gmail.com with ESMTPSA id y6sm5371063wmy.23.2021.06.03.03.14.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Jun 2021 03:14:57 -0700 (PDT) From: Dmitrii Banshchikov To: bpf@vger.kernel.org Cc: Dmitrii Banshchikov , ast@kernel.org, davem@davemloft.net, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, rdna@fb.com Subject: [PATCH bpf-next v1 05/10] bpfilter: Add struct match Date: Thu, 3 Jun 2021 14:14:20 +0400 Message-Id: <20210603101425.560384-6-me@ubique.spb.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210603101425.560384-1-me@ubique.spb.ru> References: <20210603101425.560384-1-me@ubique.spb.ru> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net struct match_ops defines polymorphic interface for matches. A match consists of pointers to struct match_ops and struct xt_entry_match which contains a payload for the match's type. All match_ops are kept in a map by their name. Signed-off-by: Dmitrii Banshchikov --- net/bpfilter/Makefile | 3 +- net/bpfilter/context.c | 44 +++++++++++++ net/bpfilter/context.h | 5 ++ net/bpfilter/match.c | 49 +++++++++++++++ net/bpfilter/match.h | 33 ++++++++++ net/bpfilter/xt_udp.c | 33 ++++++++++ .../testing/selftests/bpf/bpfilter/.gitignore | 1 + tools/testing/selftests/bpf/bpfilter/Makefile | 7 +++ .../selftests/bpf/bpfilter/test_match.c | 63 +++++++++++++++++++ 9 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 net/bpfilter/context.c create mode 100644 net/bpfilter/match.c create mode 100644 net/bpfilter/match.h create mode 100644 net/bpfilter/xt_udp.c create mode 100644 tools/testing/selftests/bpf/bpfilter/test_match.c diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile index 1809759d08c4..59f2d35c1627 100644 --- a/net/bpfilter/Makefile +++ b/net/bpfilter/Makefile @@ -4,7 +4,8 @@ # userprogs := bpfilter_umh -bpfilter_umh-objs := main.o map-common.o +bpfilter_umh-objs := main.o map-common.o match.o context.o +bpfilter_umh-objs += xt_udp.o userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi ifeq ($(CONFIG_BPFILTER_UMH), y) diff --git a/net/bpfilter/context.c b/net/bpfilter/context.c new file mode 100644 index 000000000000..6b6203dd22a7 --- /dev/null +++ b/net/bpfilter/context.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 Telegram FZ-LLC + */ + +#define _GNU_SOURCE + +#include "context.h" + +#include +#include + +#include "map-common.h" +#include "match.h" + +static int init_match_ops_map(struct context *ctx) +{ + const struct match_ops *match_ops[] = { &xt_udp }; + int i, err; + + err = create_map(&ctx->match_ops_map, ARRAY_SIZE(match_ops)); + if (err) + return err; + + for (i = 0; i < ARRAY_SIZE(match_ops); ++i) { + const struct match_ops *m = match_ops[i]; + + err = map_insert(&ctx->match_ops_map, m->name, (void *)m); + if (err) + return err; + } + + return 0; +} + +int create_context(struct context *ctx) +{ + return init_match_ops_map(ctx); +} + +void free_context(struct context *ctx) +{ + free_map(&ctx->match_ops_map); +} diff --git a/net/bpfilter/context.h b/net/bpfilter/context.h index e7bc27ee1ace..60bb525843b0 100644 --- a/net/bpfilter/context.h +++ b/net/bpfilter/context.h @@ -10,9 +10,11 @@ #include #include +#include struct context { FILE *log_file; + struct hsearch_data match_ops_map; }; #define BFLOG_IMPL(ctx, level, fmt, ...) \ @@ -34,4 +36,7 @@ struct context { #define BFLOG_DEBUG(ctx, fmt, ...) #endif +int create_context(struct context *ctx); +void free_context(struct context *ctx); + #endif // NET_BPFILTER_CONTEXT_H diff --git a/net/bpfilter/match.c b/net/bpfilter/match.c new file mode 100644 index 000000000000..3b49196efabf --- /dev/null +++ b/net/bpfilter/match.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 Telegram FZ-LLC + */ + +#define _GNU_SOURCE + +#include "match.h" + +#include + +#include +#include + +#include "context.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 err; + + if (strnlen(ipt_match->u.user.name, maxlen) == maxlen) { + BFLOG_DEBUG(ctx, "cannot init match: too long match name\n"); + return -EINVAL; + } + + found = map_find(&ctx->match_ops_map, ipt_match->u.user.name); + if (IS_ERR(found)) { + BFLOG_DEBUG(ctx, "cannot find match by name: '%s'\n", 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_DEBUG(ctx, "invalid match: '%s'\n", ipt_match->u.user.name); + return -EINVAL; + } + + err = found->check(ctx, ipt_match); + if (err) + return err; + + match->match_ops = found; + match->ipt_match = ipt_match; + + return 0; +} diff --git a/net/bpfilter/match.h b/net/bpfilter/match.h new file mode 100644 index 000000000000..9879a3670711 --- /dev/null +++ b/net/bpfilter/match.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2021 Telegram FZ-LLC + */ + +#ifndef NET_BPFILTER_MATCH_H +#define NET_BPFILTER_MATCH_H + +#include "../../include/uapi/linux/bpfilter.h" + +#include + +struct bpfilter_ipt_match; +struct context; + +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); +}; + +struct match { + const struct match_ops *match_ops; + const struct bpfilter_ipt_match *ipt_match; +}; + +extern const struct match_ops xt_udp; + +int init_match(struct context *ctx, const struct bpfilter_ipt_match *ipt_match, + struct match *match); + +#endif // NET_BPFILTER_MATCH_H diff --git a/net/bpfilter/xt_udp.c b/net/bpfilter/xt_udp.c new file mode 100644 index 000000000000..a7fbe77a53cc --- /dev/null +++ b/net/bpfilter/xt_udp.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021 Telegram FZ-LLC + */ + +#define _GNU_SOURCE + +#include +#include + +#include + +#include "context.h" +#include "match.h" + +static int xt_udp_check(struct context *ctx, const struct bpfilter_ipt_match *ipt_match) +{ + const struct xt_udp *udp; + + udp = (const struct xt_udp *)&ipt_match->data; + + if (udp->invflags & XT_UDP_INV_MASK) { + BFLOG_DEBUG(ctx, "cannot check match 'udp': invalid flags\n"); + return -EINVAL; + } + + return 0; +} + +const struct match_ops xt_udp = { .name = "udp", + .size = XT_ALIGN(sizeof(struct xt_udp)), + .revision = 0, + .check = xt_udp_check }; diff --git a/tools/testing/selftests/bpf/bpfilter/.gitignore b/tools/testing/selftests/bpf/bpfilter/.gitignore index 983fd06cbefa..9250411fa7aa 100644 --- a/tools/testing/selftests/bpf/bpfilter/.gitignore +++ b/tools/testing/selftests/bpf/bpfilter/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only test_map +test_match diff --git a/tools/testing/selftests/bpf/bpfilter/Makefile b/tools/testing/selftests/bpf/bpfilter/Makefile index 647229a0596c..0ef156cdb198 100644 --- a/tools/testing/selftests/bpf/bpfilter/Makefile +++ b/tools/testing/selftests/bpf/bpfilter/Makefile @@ -9,9 +9,16 @@ BPFILTERSRCDIR := $(top_srcdir)/net/bpfilter CFLAGS += -Wall -g -pthread -I$(TOOLSINCDIR) -I$(APIDIR) -I$(BPFILTERSRCDIR) TEST_GEN_PROGS += test_map +TEST_GEN_PROGS += test_match KSFT_KHDR_INSTALL := 1 include ../../lib.mk +BPFILTER_MATCH_SRCS := $(BPFILTERSRCDIR)/match.c $(BPFILTERSRCDIR)/xt_udp.c + +BPFILTER_COMMON_SRCS := $(BPFILTERSRCDIR)/map-common.c $(BPFILTERSRCDIR)/context.c +BPFILTER_COMMON_SRCS += $(BPFILTER_MATCH_SRCS) + $(OUTPUT)/test_map: test_map.c $(BPFILTERSRCDIR)/map-common.c +$(OUTPUT)/test_match: test_match.c $(BPFILTER_COMMON_SRCS) $(BPFILTER_MATCH_SRCS) diff --git a/tools/testing/selftests/bpf/bpfilter/test_match.c b/tools/testing/selftests/bpf/bpfilter/test_match.c new file mode 100644 index 000000000000..3a56d79ed24c --- /dev/null +++ b/tools/testing/selftests/bpf/bpfilter/test_match.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE + +#include "context.h" +#include "match.h" + +#include +#include + +#include +#include +#include + +#include + +#include "../../kselftest_harness.h" + +struct udp_match { + struct xt_entry_match ipt_match; + struct xt_udp udp; +}; + +FIXTURE(test_udp_match) +{ + struct context ctx; + struct udp_match udp_match; + struct match match; +}; + +FIXTURE_SETUP(test_udp_match) +{ + ASSERT_EQ(0, create_context(&self->ctx)); + self->ctx.log_file = stderr; + + memset(&self->udp_match, 0, sizeof(self->udp_match)); + snprintf(self->udp_match.ipt_match.u.user.name, + sizeof(self->udp_match.ipt_match.u.user.name), "udp"); + self->udp_match.ipt_match.u.user.match_size = sizeof(struct udp_match); + self->udp_match.ipt_match.u.user.revision = 0; +}; + +FIXTURE_TEARDOWN(test_udp_match) +{ + free_context(&self->ctx); +} + +TEST_F(test_udp_match, init) +{ + self->udp_match.udp.spts[0] = 1; + self->udp_match.udp.spts[1] = 2; + self->udp_match.udp.dpts[0] = 3; + self->udp_match.udp.dpts[1] = 4; + self->udp_match.udp.invflags = 0; + + ASSERT_EQ(init_match(&self->ctx, + (const struct bpfilter_ipt_match *)&self->udp_match + .ipt_match, + &self->match), + 0); +} + +TEST_HARNESS_MAIN