From patchwork Wed Jun 30 18:40:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 4479 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4GFVXW1r95z3x1K for ; Wed, 30 Jun 2021 18:40:43 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4GFVXT29jwz191; Wed, 30 Jun 2021 18:40:41 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4GFVXS5zK8z2yK4; Wed, 30 Jun 2021 18:40:40 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4GFVXR1PVRz2xLF for ; Wed, 30 Jun 2021 18:40:39 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4GFVXQ0Wg0zg7; Wed, 30 Jun 2021 18:40:37 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1625078438; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ZZXZKvIrJLeLXxKauSBkfaDEWBIbHx8sOtWpIKWV4uI=; b=fenWNjReRy8YfvtQY1YPFzjSsFfJ4wrKmyPGeUiZT58lNlVfnm7RoQmAob1swJAjaiXfJY KPMaOo2I+YUJKIBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1625078438; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ZZXZKvIrJLeLXxKauSBkfaDEWBIbHx8sOtWpIKWV4uI=; b=EIYWrW3wwd90TPzhYO2MQvUQBp7swYmzsdcjEnisxT+Dd+LyBQL5qcJdAAcuSL/O2VTWbF bIIhagck+0VyBWsssieaeYFYtU4kaUny8tmShcW/nR2qcb5D4vJKDiSyheyNYpquiFnWwQ VDuqnCNzQdR0JlGI9o+DH6PjoDeMH4MpPKcOyvcaOKUiBZGyYrgTrpTcYqN3P2aCKxXXKS QeZDJ7vQpmN799s5mSjNpdNK74HEs/TBP47xAHE3rRN/m3qYOaKDyCzMYh5bY+VMiXKzBr 50RwRfh1Mxr38AO+vVAIuS3YjRkKe41TOvj+j5eL+n4WI7U5hGZYK8w8rMK+sg== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH] Firewall: Proper allow to create REDIRECT rules. Date: Wed, 30 Jun 2021 20:40:31 +0200 Message-Id: <20210630184031.7726-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This patch now proper allows to create rules for redirecting requests of a given host, group or network(s) to a specified port or service to the local IPFire system. So it implements a very generic and easy to use feature to redirect (for example all DNS, NTP, or whatever) requests to the a local running instance and so to force usage of that local hosted service. * The feature supports specifiying a single port and redirect the requests to another given one. ( For example requests to UDP 123 can be redirected to local UDP 1234 if you run an NTP server on that port.) * It also supports direct usage of services or even service groups. ( So you can create a service group for DNS and redirect them to the local recursor, or create a "redirected services" group which easily can be managed...) Signed-off-by: Stefan Schantl --- config/firewall/rules.pl | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl index bc6187052..0dd1c9024 100644 --- a/config/firewall/rules.pl +++ b/config/firewall/rules.pl @@ -449,16 +449,28 @@ sub buildrules { my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1); push(@nat_options, @nat_protocol_options); } + + # Add time options. push(@nat_options, @time_options); + # Determine if a REDIRECT rule should be created. + my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports); + # Make port-forwardings useable from the internal networks. - my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1); - unless ($nat_address ~~ @internal_addresses) { - &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options); + if (!$use_redirect) { + my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1); + unless ($nat_address ~~ @internal_addresses) { + &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options); + } } + # Add source options. push(@nat_options, @source_options); - push(@nat_options, ("-d", $nat_address)); + + # Add NAT address. + if (!$use_redirect) { + push(@nat_options, ("-d", $nat_address)); + } my $dnat_port; if ($protocol_has_ports) { @@ -468,9 +480,13 @@ sub buildrules { my @nat_action_options = (); # Use iptables REDIRECT - my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port); if ($use_redirect) { - push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port)); + push(@nat_action_options, ("-j", "REDIRECT")); + + # Redirect to specified port if one has given. + if ($dnat_port) { + push(@nat_action_options, ("--to-ports", $dnat_port)); + } # Use iptables DNAT } else {