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 are some basics of the inner workings of iptables.
The explanation of the syntax and semantics of iptables rules are out of the scope of this documentation. 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
Refer to this diagram from the Arch WikiOpens in a new tabto see the flow and used tables of iptables.
If not considering NATting, the tables of importance are INPUT, OUTPUT, and FORWARD.
The INPUT and OUTPUT tables 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 must be added.
The FORWARD table is only used for traffic routed over this pod or node.
Caution
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 must be allowed on the underlying interface and the traffic from and to the VXLAN interface must be considered separately.
This also applies for other overlay protocols or encapsulations including IPSEC.
Dropping vs rejecting traffic
There are two different ways of blocking traffic: dropping or rejecting.
In the first case, the traffic is just silently dropped. In the second case, the firewall sends 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 is created when rejecting packets and it has been used in the past for attacking other hosts (DDOS).
Therefore, it is advised 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 are described below.
Enable iptables firewall configuration
To enable iptables, set the following values in your CGW configuration:
Configure iptables rules
The rules must match the rules of the iptables-saveOpens in a new tab format, which is used for the above mentioned file.
Common parts of the configuration are explained below.
Most rules can be applied to the configuration of both IPv4 and IPv6, if not marked otherwise.
Note
You must 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
Set 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 is applied and the packet either is accepted or dropped.
Because of erroneous human behaviour while writing rules, it is advised to set the default policy to DROP for increased security. Be careful with this option during development to not lock yourself out.
For readability it is 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 is:
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 be useful to look into Calico native policy enforcement.
Allow traffic from and to your VTI interfaces
I you use route-based VPN with CGW, you 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 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:
You can even limit the traffic to specific protocols, ports, or IP addresses (see below).
Allow ICMP traffic on public interfaces
On some deployment of CGW, there will be an interface 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 must 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:
- ESP protocol, which is mostly used between two sites for the data traffic
- UDP port 500, which is used for IKE negotiations and NAT detection
- UDP port 4500, which is 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 one or the other rule can be removed.
Allow ICMP on all interfaces
To enable ICMP on all interfaces, add the following rules:
Allow routing between two interfaces
The following example shows a symmetric allowed connection between two interfaces:
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 is 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 a need for advanced Traffic Classification iptables, you can use bpf module.
For this, the BPF assembly code must be compiled with bpfc to xt_bpf format.
To test the Functionality of the BPF assembly, use netsniff-ng. (The file must be in the 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 set up a useful configuration for iptables: