From patchwork Wed Jan 2 13:52:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Dedecker X-Patchwork-Id: 1019984 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="WeXgyaIx"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="tt1NMhOG"; 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 43VCDf1rknz9s4s for ; Thu, 3 Jan 2019 00:53:05 +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=5+gBzAxPh97YF/O/+EERBhPyPWO65fGRS4vJr/bCLuw=; b=WeXgyaIxpSaHuu FGeie+v59IW2B7+jF21XzD65Yrb/7WmYkCaPKYAGSjOqvtR0FrVFPtS9lzJ6dNgdYsw/PPAbouRnU Th7mNYOm759Xcy6bzvf3NpBXRIGi2bsadRWIM+hFEqszwNGPKmq1UB3qUt/YvVIOSIHDw+oWBUszx VfAHw7Gyrzgei1yopATESFpuyScotUDavHExBzhZeMuS2uOjnY4S3a7HcrrTydIB9gCR/KwQ4Jbp4 QB2KBUfqvBSDRvbqIFdr6i9Tp8adY01jUpReWdMnaBHtH7UXoNyx9D+ECqteD1gT9BZMdTHttdXUE V3iRLMcCxRqwiSGhjg/Q==; 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 1gegwp-00056U-Sn; Wed, 02 Jan 2019 13:52:39 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gegwm-00055i-40 for openwrt-devel@lists.openwrt.org; Wed, 02 Jan 2019 13:52:37 +0000 Received: by mail-ed1-x541.google.com with SMTP id p6so26282216eds.0 for ; Wed, 02 Jan 2019 05:52:35 -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=FoqfhRKIwpLFCu9vQkSq+yuZAyYknGPERZ5Onh4e86E=; b=tt1NMhOGL5ZiDdUE+SOQd4EAtP8x4bDLg9aRc7DZtSwgt6NeZWNIGKYUvTVKXgXtO9 51dL96S3KZXZ+cy+hlp/pQiK65IxB8A7qXwo0tms8O/gthk686ChyouOI74uV854tGF2 x96JCl6QcPq0rJZS05Ij714Cag8fSHPDHw9CkKk/XGikcb8xYvVqvNFEDRpLOKC30qPy tAJ0j8O2sTKYrxxcEpt8lup9kLn74qf1ZCrj4OT5Qr5caJwd99m2cLIyzGqzysGGdrfm 6PNGbtF9iBsa9Asg0nSKKwejt/ZkOVFaXvj3D74WlglsqwGGJG1qhXbAWi/QzHDQ+Ixb m8GQ== 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=FoqfhRKIwpLFCu9vQkSq+yuZAyYknGPERZ5Onh4e86E=; b=fq21UhqKHwnLQC0yM23UdvUy6HkJxXbkeo77nmKEnaZPqR1gEGuO2NH5PM5Fgidlvw 0EeHJzTrU26z/G2oSxrwSW8CNbt3DM3Sb1DH1+U7oR7EoCb2YGcgTN4nE001jZ0Ppenx p4XH5fNicah2P+ZWjeXVDkRjKX47+WuHJxujFlSMwGYSx7Kp0U1nq9OkEzXJ7olCBtov mR2yjZ152uMZ3RQYNyF4po9VgEzaYPgc9BFbknV74ZeNZ7jIrPXPjcWhJX03LRi4YbRV pTynUH1Mkf05mL8ca0YPxwTfRCWqmoOqYcis5ubtlYn/1XedIvSCWtrpVL887aFo9Z7y /U7A== X-Gm-Message-State: AA+aEWZ2D8bMFf/HeaHHlgmEuD7pW862h/g/0FpejTvhgv0CJY2PVW93 K/aWp0u5Cv0jcNkhvcNIzIy+VNRn X-Google-Smtp-Source: AFSGD/V1SO/fbhAEw1mqamNTtMr/MF3fqAuGiGRc+puFRjsMdc3Ss70XbKeusWZnjGveXN93RBqX7g== X-Received: by 2002:aa7:d394:: with SMTP id x20mr38784738edq.193.1546437153521; Wed, 02 Jan 2019 05:52:33 -0800 (PST) Received: from dedeckeh-X556UV.lan (ptr-fvl7vtazsv6ysbri0pb.18120a2.ip6.access.telenet.be. [2a02:1812:1128:fb00:25b3:9db7:4632:1b0f]) by smtp.gmail.com with ESMTPSA id f35sm41366236edd.80.2019.01.02.05.52.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Jan 2019 05:52:32 -0800 (PST) From: Hans Dedecker To: openwrt-devel@lists.openwrt.org Date: Wed, 2 Jan 2019 14:52:28 +0100 Message-Id: <20190102135228.15814-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-20190102_055236_186833_90E4B8E8 X-CRM114-Status: GOOD ( 12.87 ) 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:541 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (dedeckeh[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 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 Subject: [OpenWrt-Devel] [PATCH v2 firewall3] 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 --- v2: move check functions to util.c utils.c | 38 ++++++++++++++++++++++++++++++++++++++ utils.h | 5 +++++ zones.c | 14 +++++++++----- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/utils.c b/utils.c index 4f892a7..e5908f9 100644 --- a/utils.c +++ b/utils.c @@ -17,6 +17,10 @@ */ #define _GNU_SOURCE + +#include +#include + #include "utils.h" #include "options.h" @@ -933,3 +937,37 @@ fw3_protoname(void *proto) return pe->p_name; } + +bool +fw3_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; +} + +bool +fw3_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; +} diff --git a/utils.h b/utils.h index c8ab0e5..1ada0dd 100644 --- a/utils.h +++ b/utils.h @@ -44,6 +44,8 @@ extern bool fw3_pr_debug; +struct fw3_address; + void warn_elem(struct uci_element *e, const char *format, ...); void warn(const char *format, ...); void error(const char *format, ...); @@ -120,4 +122,7 @@ bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const const char * fw3_protoname(void *proto); +bool fw3_check_loopback_dev(const char *name); + +bool fw3_check_loopback_addr(struct fw3_address *addr); #endif diff --git a/zones.c b/zones.c index 505ab20..4f2b1e4 100644 --- a/zones.c +++ b/zones.c @@ -98,7 +98,6 @@ const struct fw3_option fw3_zone_opts[] = { { } }; - static void check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def, const char *name) @@ -557,22 +556,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 && fw3_check_loopback_dev(dev->name)); + char *chain = loopback_dev || (sub != NULL && !sub->invert && fw3_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); } } }