From patchwork Thu Dec 27 13:16:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Dedecker X-Patchwork-Id: 1018867 X-Patchwork-Delegate: dedeckeh@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="txQMAZ7b"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="t3t24EqC"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43QVk86MRqz9s8r for ; Fri, 28 Dec 2018 00:17:20 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:Message-Id:Date:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=DCXCd0mYjB/zxPQtX3Mg3QfJeqruViTFlUqM1qabfS4=; b=txQMAZ7b+TrTOd lxgOcV9MK1KWs1r44svrfQLoIHWwn/BQinCWAbjD8PNmDDGfWeTvD9xawMdozZ2TT4pwAR/IISkjY K3R7Vo8OzuyNexpLkApDLKY3b07Iy/Pg4ywImv5zbV1B/G7/ljPGlBxWKeKjptlz4X9UarEiS519f bYeMi3edmEQfd3ZKVL1ME/xRYGiG5gD9OukgvLJxyz+pphavf2xeLGHyc/vVvqu5wZmF7SjnvIPmS 5yP8nPfPrdArD3Ej5HTbEPaurQpqd4wymloNT++h/GDh3i+WoDZZoHY0OLH5NIH/NAzhmfpfcbRAi e67gi4ece0gDXxos2kIA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gcVXG-0000Hg-J3; Thu, 27 Dec 2018 13:17:14 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gcVXD-0000Gy-Gs for openwrt-devel@lists.openwrt.org; Thu, 27 Dec 2018 13:17:12 +0000 Received: by mail-ed1-x543.google.com with SMTP id h15so15226342edb.4 for ; Thu, 27 Dec 2018 05:16:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=c22uFyxMhYQqFgAyEdAsy3rOgaNiAxxeNBB4mF6WOSo=; b=t3t24EqClHMeA+UCzjxG3MOJrOSJLj6M7gZDOvzLjF1hxi7eKGMsHzjiIUbi/L3RjR 9STv6bYyQqveUAK2wNS0xr7EniCow7P+QAve23aAYoNABOVei2yAOEu1Xi6hvd4PDRUp /uRrOvSw350A1hWzBg1tm9Qsx+RtGqAs2km7wvWHMl/M94TVBL5VImmMOjj74SfHp0VB b7wwCBB7NMkH61+aBnvQoyxRSiaJ8Fo17LzKDQ7s3F3gzthBTTB25fhgb5Ag4v1lw2oJ Lu047EyXWBPBzGRbbgT6q20/A46wWk9+G9bfMbMyTrMklVh+iHVfx70KftdccuiAcQfp FR5Q== 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:mime-version :content-transfer-encoding; bh=c22uFyxMhYQqFgAyEdAsy3rOgaNiAxxeNBB4mF6WOSo=; b=XeKj5CHgJw9Mtz/HMN1L7ign5yRpEo6IBmsEFRfEcZUqyoNi78WiL+S4Iqvh4HrSTy +XZN8yhyWWKAZD73yxh65h0fKYtBp+hpdawmvja9ZPiJoBGpE5cY9DHAtcKvzSj7dvfs L+0aMQDfZ15k8u/i2wkmF25GrdEHlsYrDjNPeFHkQ++axHtyU4+plN/uZ3hLWUVQ1byH 2OSi73f0i68jg3b6j5tnvScUjMiNOquLHx/oK5YZeg6MKOjrLGhRiz9Zm1wU1K07jrse ImuxXkMaXLw3RzWa0itRi/QBxNSs7IiX/NHDVmwBV9rfVQihCpUHnwZ6fNv7Slj7a2cu P9PQ== X-Gm-Message-State: AA+aEWbAMWuDlgGALiLpBgZDR3TC7ZHDPb64uQmsYqQ/OkV4+RhVFnq4 d8+psLijuJa7OUUk8OYURuZCGnUC+4k= X-Google-Smtp-Source: AFSGD/Xm3HDV8swb7NtlLc66g1quNYENvtttnAXCH2pyJcooRpFFOyMFt7+Grlh4YYiDtLOM4+udjA== X-Received: by 2002:a50:9feb:: with SMTP id c98mr18664904edf.253.1545916617858; Thu, 27 Dec 2018 05:16:57 -0800 (PST) Received: from dedeckeh-X556UV.lan (ptr-fvl7vtcildxss5ozsaz.18120a2.ip6.access.telenet.be. [2a02:1812:1128:fb00:89c9:3f76:d6bf:860b]) by smtp.gmail.com with ESMTPSA id 49sm11463589edz.59.2018.12.27.05.16.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 05:16:57 -0800 (PST) From: Hans Dedecker To: openwrt-devel@lists.openwrt.org Date: Thu, 27 Dec 2018 14:16:53 +0100 Message-Id: <20181227131653.11270-1-dedeckeh@gmail.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181227_051711_593887_A44FFACB X-CRM114-Status: GOOD ( 12.82 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:543 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (dedeckeh[at]gmail.com) 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH firewall] zones: add zone identifying local traffic in raw OUTPUT chain X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hans Dedecker , Alin Nastac , jo@mein.io Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Add zone chains identifying local generated traffic; either by configuring a loopback device or as subnet a loopback address; in the raw OUTPUT chain as local generated traffic is passing this chain. This allows helpers to be used for local generated traffic. Signed-off-by: Alin Nastac Signed-off-by: Hans Dedecker --- zones.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/zones.c b/zones.c index 505ab20..9ac6551 100644 --- a/zones.c +++ b/zones.c @@ -16,6 +16,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include + #include "zones.h" #include "ubus.h" #include "helpers.h" @@ -98,6 +101,39 @@ const struct fw3_option fw3_zone_opts[] = { { } }; +static bool +check_loopback_addr(struct fw3_address *addr) +{ + if (addr->family == FW3_FAMILY_V4 && + (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) + return true; + + if (addr->family == FW3_FAMILY_V6 && !addr->range && + fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 && + IN6_IS_ADDR_LOOPBACK(&addr->address.v6)) + return true; + + return false; +} + +static bool +check_loopback_dev(const char *name) +{ + struct ifreq ifr; + int s = socket(AF_LOCAL, SOCK_DGRAM, 0); + bool rv = false; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1); + + if (s < 0 || ioctl(s, SIOCGIFFLAGS, &ifr) < 0) + goto out; + + if (ifr.ifr_flags & IFF_LOOPBACK) + rv = true; +out: + return rv; +} static void check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def, @@ -557,22 +593,27 @@ print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state, } else if (handle->table == FW3_TABLE_RAW) { + bool loopback_dev = (dev != NULL && !dev->any && + !dev->invert && check_loopback_dev(dev->name)); + char *chain = loopback_dev || (sub != NULL && !sub->invert && check_loopback_addr(sub)) ? + "OUTPUT" : "PREROUTING"; + if (has(zone->flags, handle->family, FW3_FLAG_HELPER)) { - r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL); + r = fw3_ipt_rule_create(handle, NULL, loopback_dev ? NULL : dev, NULL, sub, NULL); fw3_ipt_rule_comment(r, "%s CT helper assignment", zone->name); fw3_ipt_rule_target(r, "zone_%s_helper", zone->name); fw3_ipt_rule_extra(r, zone->extra_src); - fw3_ipt_rule_replace(r, "PREROUTING"); + fw3_ipt_rule_replace(r, chain); } if (has(zone->flags, handle->family, FW3_FLAG_NOTRACK)) { - r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL); + r = fw3_ipt_rule_create(handle, NULL, loopback_dev ? NULL : dev, NULL, sub, NULL); fw3_ipt_rule_comment(r, "%s CT bypass", zone->name); fw3_ipt_rule_target(r, "zone_%s_notrack", zone->name); fw3_ipt_rule_extra(r, zone->extra_src); - fw3_ipt_rule_replace(r, "PREROUTING"); + fw3_ipt_rule_replace(r, chain); } } }