Basic Netfilter Function Block Diagram

This is an old revision of the document!


Both NFTables and IPTables use the Netfilter framework provided in the Linux kernal. NFtables was implemented to supersede IPTables, which due to the widespread use of IPTables, will probably take a long time.

The following is a basic block diagram of the Netfilter Filter and NAT (Network Address Translation) functions, which are the basic requirements for router.

       Incoming
       Packets
          |
    ┌────────────┐
    │ Prerouting │
    │ Rules      │
    └────────────┘
          |
     /----------\
     | Routing  |       NAT
     | Decision |-----------------|
     |  Rules   |       Filter    |
     \----------/                 |
           |                      |
    |------------|                |
    | Input      |                |
    | Rules      |                |
    |------------|                |
           |                      |
 |-------------------|      |----------|
 | Network Processes |      | Forward  |
 | within Router     |      | Rules    |
 |-------------------|      |----------|
           |                      |
    |------------|                |
    | Output     |                |
    | Rules      |                |
    |------------|                |
           |            FILTER    |
           |   |------------------|
           |   |        NAT
    |-------------|
    | Postrouting |
    | Rules       |
    |-------------|   
           |
       Outgoing
       Packets

IPTables and Netfilter

The following is taken from Digitalocean A Deep Dive into Iptables and Netfilter Architecture. While it it is focus on iptables the concepts are basically valid for nftables.

tldr

Available States

Connections tracked by the connection tracking system will be in one of the following states:

  • NEW: When a packet arrives that is not associated with an existing connection, but is not invalid as a first packet, a new connection will be added to the system with this label. This happens for both connection-aware protocols like TCP and for connectionless protocols like UDP.
  • ESTABLISHED: A connection is changed from NEW to ESTABLISHED when it receives a valid response in the opposite direction. For TCP connections, this means a SYN/ACK and for UDP and ICMP traffic, this means a response where source and destination of the original packet are switched.
  • RELATED: Packets that are not part of an existing connection, but are associated with a connection already in the system are labeled RELATED. This could mean a helper connection, as is the case with FTP data transmission connections, or it could be ICMP responses to connection attempts by other protocols.
  • INVALID: Packets can be marked INVALID if they are not associated with an existing connection and aren’t appropriate for opening a new connection, if they cannot be identified, or if they aren’t routable among other reasons.
  • UNTRACKED: Packets can be marked as UNTRACKED if they’ve been targeted in a raw table chain to bypass tracking.
  • SNAT: This is a virtual state set when the source address has been altered by NAT operations. This is used by the connection tracking system so that it knows to change the source addresses back in reply packets.
  • DNAT: This is a virtual state set when the destination address has been altered by NAT operations. This is used by the connection tracking system so that it knows to change the destination address back when routing reply packets.

The states tracked in the connection tracking system allow administrators to craft rules that target specific points in a connection’s lifetime. This provides the functionality needed for more thorough and secure rules.

Some references

PPPoE MTU Requirements

The PPPoE connection have various additional overhead to that in a standard Ethernet data field. The maximum length (MTU) of the data field of a standard Ethernet data field is limited 1500 bytes.

A standard PPPoE connection has an additional overhead of 8 bytes, which limits the MTU to 1492 bytes. However, some ISP (internet service providers) may have additional overheads. To determine the the largest MTU use the ping command. The ping command has a 28 bytes overhead (20 bytes IP header + 8 bytes for ICMP header). So the MTU is the greatest value that can be pinged without a fragmentation error, plus 28 bytes for the ping overhead. For a normal PPPoE connection this would be 1492 - 28 = 1464 bytes. (Note that a problem with this method is that it probably uses an existing modem router that sets the MTU, and it is possible that this setting acts as the limiter.) Some command examples:

  • ping -s 1464 -c1 google.com
  • tracepath vorash.stgraber.org

See references: How to Optimize your Internet Connection using MTU and RWIN, MTU and TCP MSS when using PPPoE, TCP Headers and UDP Headers Explained, Path MTU Discovery and Filtering ICMP Cisco Resolve IP Fragmentation, MTU, MSS, and PMTUD Issues with GRE and IPSEC, Understanding MTU for ADSL, and Wikipedia IPv4, Ethertype, IEEE 802.1Q, Maximum transmission unit, Point-to-point protocol over Ethernet, IPv6 packet, Internet Control Message Protocol version 6, and Path MTU Discovery.

The MSS is normally just 40 bytes less than the MTU. The MSS is used to avoid IP fragmentation at endpoints of TCP connections. The MSS is just the TCP data size and excludes the IP and TCP headers that are normally 20 bytes each. So normal mss would be 1492 - 40 = 1452 bytes

Some Ethernet data field overheads to consider:

  • PPPoE header = 8 bytes
  • IP header = 20 bytes, but can grow up to 60 bytes with options that are rarely used.
  • ICMP header = 8 bytes
  • TCP header = 20 bytes, but like IP can grow to 60 bytes long

The Ethernet datafield (MTU) is limited to 1500 bytes and the maximum Ethernet frame size must be 1536 bytes or greater. The following overheads in the Ethernet frame, over the MTU are given for information:

  • Preamble = 8 bytes
  • Destination MAC = 6 bytes
  • Source MAC = 6 bytes
  • VLAN header (optional) = 4 btyes
  • EtherType/Size = 2 bytes
  • Payload = maximum 1500 bytes (MTU)
  • CRC/FCS = 4 bytes
  • As can be seen above the Ethernet frame overhead is normally a minimum of 26 bytes and 30 bytes with VLAN (IEEE 802.1Q) tagging.

To set the PPPoE connection mtu edit the following file sudo vim /etc/ppp/ip-up and append the following to the end of the file: /sbin/ifconfig ppp0 mtu 1492.

ICMP Filtering

There seems to be a lot of conflicting information on filtering ICMP, too much!. ICMP is a fundamental component of IP protocal suite and simply blocking it in entirety is poor practice. In fact IPv6 will not function correctly without ICMP. Some judicious filtering and rate limiting seems the correct solution. The following is some reading on ICMP:

/app/www/public/data/attic/linux_router/netfilter.1719105040.txt.gz · Last modified: 2024-06-23 Sun wk25 09:10
CC Attribution-Share Alike 4.0 International Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International