Secure CGW using a firewall
To secure the CGW pod against unwanted traffic, use iptables as a firewall. This is especially important if a public IP is attached to the pod.
Concepts
Below just some basics of the inner workings of iptables will be described.
The explanation of the syntax and semantics of iptables rules are out of the scope of this documentation. Please consider some of the following resources if you are not familiar with the rules yet:
- Suehring, Steve. Linux Firewalls : Enhancing Security with Nftables and Beyond. 4. ed. 2015.
- Netfilter projectOpens in a new tab
- Ubuntuusers wiki (german)Opens in a new tab
- Arch Linux wikiOpens in a new tab
Further you should get a basic understanding of the basic working of iptables before continuing with firewall configuration, including tables, chains and rules.
Available tables
The following diagram shows the flow and used tables of iptables:
( This drawing was taken from the Arch WikiOpens in a new tab )
If not considering NATting, the tables of importance are INPUT, OUTPUT and FORWARD.
The INPUT and OUTPUT table are used for the traffic coming from and going into process on the machine itself. Therefore for every software running in the pod, the rules for this specific software have to be added.
The FORWARD table is used for traffic only routed over this pod/node.
Attention: Protocols like VXLAN create a new device in the pod but use the underlying device for the encapsulated traffic itself. Because of this, the VXLAN traffic has to be allowed on the underlying interface and the traffic from and to the VXLAN interface has to be considered separately.
This also applies for other overlay protocols or encapsulations including IPSEC.
Dropping vs rejecting traffic
There are two different ways on blocking traffic: dropping or rejecting.
In the first case, the traffic is just silently dropped, in the second case, the firewall will send an ICMP message with a reason, why the target is not reachable.
Concerning RFC1122Opens in a new tab endpoints should send error messages when possible. Also debugging inside the cluster gets easier, if it is clear whether a port is blocked or the service behind it is not running.
On the contrary though, more traffic will be created when rejecting packets and it has been used in the past for attacking other hosts (DDOS).
Therefore I would advise to use rejection inside a network perimeter, where rouge services are not expected to be the rule but an exception and use dropping the edges to the public internet.
Setting policies and ICMP message types is described below.
Enable iptables firewall configuration
To enable iptables, you have to set the following values in your CGW configuration:
Configure iptables rules
The rules have to match the rules of the iptables-saveOpens in a new tab format, which will be used for the above mentioned file.
I will explain common parts of the configuration below.
Most rules can be applied to the configuration of both, IPv4 and IPv6, if not marked otherwise.
Note
You have to configure the firewall for both protocols to be effective!
Basic format
The format consists basically of three parts in a block, where each block can repeat.
The first part is the declaration of the table to be used in the format *<tablename>.
The second part are the rules themselves, each in a single line. These match the syntax you would normally use on the command line. The parameter of the table MUST be omitted though in the rules, because it is already defined by the first part.
The third part is the COMMIT statement, to commit the changes of the configuration.
These blocks can be repeated for different tables.
An example of a rules file:
Useful configuration blocks by example
Setting the default policies of the chain
To set the default policies of the firewalling chains, you can add the following:
Possible options are ACCEPT and DROP.
As the name default policy might suggest, for every packet, for which none of the defined rules match, the default policy will be applied and the packet either be accepted or dropped.
Because of erroneous human behaviour while writing rules it is advised to set the default policy to DROP for increased security. Never the less be careful with this option during development to not lock yourself out.
For readability it is though advised to explicitly reject or drop unwanted traffic due to rules.
Allow loopback traffic
To allow loopback traffic from local services add the following lines to the configuration
For IPv6 it will be:
Allow traffic from calico network
For pods on a Kubernetes cluster using calico you could add the following to allow traffic from the main interface:
If you want to close the traffic further, you could also add further restrictions on protocols or IP addresses (see below).
For general separation of namespaces and services it might though be useful to look into calicos native policy enforcement.
Allow traffic from and to your VTI interfaces
I you use route-based VPN with CGW you will create a virtual terminal interface to route the traffic to, you want to tunnel.
The following rules will accept all traffic coming from and going to the interfaces as well as routing:
If you though just need to have ICMP traffic from and to the CGW allowed for debugging and have a dedicated interface for connection to the connected service, for example through VXLAN, you can restrict the rules further:
Of course you could even limit the traffic to specific protocols, ports or IP addresses (see below).
Allow ICMP traffic on the public interfaces
On some deployment of CGW, there will an interface be attached, which provides a public IP address.
To allow ICMP traffic to and from the interface add the following:
Allow IPSEC traffic
This scenario assumes, that an interface with a public IP address is used (see above). If this is not the case, either the interfaces has to be changed or the rules can be omitted, if all traffic on the Calico interface is allowed.
IPSEC can use three different kinds of traffic in normal setups.
The first is the ESP protocol, which is mostly used between to sites for the data traffic.
The second is UDP port 500, which is used for IKE negotiations and NAT detection.
The third one is UDP port 4500, which will be used for IKE and data, when a NAT is detected or UDP encapsulation is enforced.
In cases, where ESP is known to be not working or no NATs exists, either the one or the other rule can be removed.
Allow ICMP on all interfaces
To enable ICMP on all interfaces, you can add the following rules:
Allow routing between two interfaces
The following example shows a symmetric allowed connection between two interfaces:
Of course the filtering can also be different in both directions and more specific concerning protocols and ports.
Rejecting traffic with a specific ICMP message
You can reject traffic with ICMP messages meeting more closely the reason for the blocking. One example is the following:
In this example, traffic reaching the host over interface eth1 from the outside will be rejected with a host prohibited message.
Mangle TCP packets to decrease packet size
When there are some problems with Path MTU discovery, you can mangle the TCP packets to decrease the Maximum Segment Size.
Note
This method should though not be a permanent fix due to the limitations to TCP and the necessity to use PMTUD in conjunction with IPv6.
BPF based Traffic Filtering
When there is the need for advanced Traffic Classification iptables bpf module can be used.
For this the BPF assembly code has to be compiled with bpfc to xt_bpf format.
To test the Functionality of the BPF assembly netsniff-ng can be used. (File has to be in pcap format)
This bytecode can be used by iptables to act based on that.
Examples
The following files will give you some examples of how to setup a useful configuration for iptables: