neulab/tldr huggingface dataset:
IPV4 HEADER EXPRESSION ip {version | hdrlength | dscp | ecn | length | id | frag-off | ttl | protocol | checksum | saddr | daddr }
Table 40. IPv4 header expression ┌──────────┬──────────────────────┬──────────────────┐ │Keyword   │ Description          │ Type             │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │version   │ IP header version    │ integer (4 bit)  │ │          │ (4)                  │                  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │hdrlength │ IP header length     │ integer (4 bit)  │ │          │ including options    │ FIXME scaling    │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │dscp      │ Differentiated       │ dscp             │ │          │ Services Code Point  │                  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │ecn       │ Explicit Congestion  │ ecn              │ │          │ Notification         │                  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │length    │ Total packet length  │ integer (16 bit) │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │id        │ IP ID                │ integer (16 bit) │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │frag-off  │ Fragment offset      │ integer (16 bit) │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │ttl       │ Time to live         │ integer (8 bit)  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │protocol  │ Upper layer protocol │ inet_proto       │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │checksum  │ IP header checksum   │ integer (16 bit) │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │saddr     │ Source address       │ ipv4_addr        │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │daddr     │ Destination address  │ ipv4_addr        │ └──────────┴──────────────────────┴──────────────────┘
ICMP HEADER EXPRESSION icmp {type | code | checksum | id | sequence | gateway | mtu}
This expression refers to ICMP header fields. When using it in inet, bridge or netdev families, it will cause an implicit dependency on IPv4 to be created. To match on unusual cases like ICMP over IPv6, one has to add an explicit meta protocol ip6 match to the rule.
Table 41. ICMP header expression ┌─────────┬──────────────────────┬──────────────────┐ │Keyword  │ Description          │ Type             │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │type     │ ICMP type field      │ icmp_type        │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │code     │ ICMP code field      │ integer (8 bit)  │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │checksum │ ICMP checksum field  │ integer (16 bit) │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │id       │ ID of echo           │ integer (16 bit) │ │         │ request/response     │                  │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │sequence │ sequence number of   │ integer (16 bit) │ │         │ echo                 │                  │ │         │ request/response     │                  │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │gateway  │ gateway of redirects │ integer (32 bit) │ ├─────────┼──────────────────────┼──────────────────┤ │         │                      │                  │ │mtu      │ MTU of path MTU      │ integer (16 bit) │ │         │ discovery            │                  │ └─────────┴──────────────────────┴──────────────────┘
IGMP HEADER EXPRESSION igmp {type | mrt | checksum | group}
This expression refers to IGMP header fields. When using it in inet, bridge or netdev families, it will cause an implicit dependency on IPv4 to be created. To match on unusual cases like IGMP over IPv6, one has to add an explicit meta protocol ip6 match to the rule.
Table 42. IGMP header expression ┌─────────┬─────────────────────┬──────────────────┐ │Keyword  │ Description         │ Type             │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │type     │ IGMP type field     │ igmp_type        │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │mrt      │ IGMP maximum        │ integer (8 bit)  │ │         │ response time field │                  │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │checksum │ IGMP checksum field │ integer (16 bit) │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │group    │ Group address       │ integer (32 bit) │ └─────────┴─────────────────────┴──────────────────┘
IPV6 HEADER EXPRESSION ip6 {version | dscp | ecn | flowlabel | length | nexthdr | hoplimit | saddr | daddr}
This expression refers to the ipv6 header fields. Caution when using ip6 nexthdr, the value only refers to the next header, i.e. ip6 nexthdr tcp will only match if the ipv6 packet does not contain any extension headers. Packets that are fragmented or e.g. contain a routing extension headers will not be matched. Please use meta l4proto if you wish to match the real transport header and ignore any additional extension headers instead.
Table 43. IPv6 header expression ┌──────────┬─────────────────────┬──────────────────┐ │Keyword   │ Description         │ Type             │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │version   │ IP header version   │ integer (4 bit)  │ │          │ (6)                 │                  │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │dscp      │ Differentiated      │ dscp             │ │          │ Services Code Point │                  │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │ecn       │ Explicit Congestion │ ecn              │ │          │ Notification        │                  │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │flowlabel │ Flow label          │ integer (20 bit) │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │length    │ Payload length      │ integer (16 bit) │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │nexthdr   │ Nexthdr protocol    │ inet_proto       │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │hoplimit  │ Hop limit           │ integer (8 bit)  │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │saddr     │ Source address      │ ipv6_addr        │ ├──────────┼─────────────────────┼──────────────────┤ │          │                     │                  │ │daddr     │ Destination address │ ipv6_addr        │ └──────────┴─────────────────────┴──────────────────┘
Using ip6 header expressions.
# matching if first extension header indicates a fragment ip6 nexthdr ipv6-frag
ICMPV6 HEADER EXPRESSION icmpv6 {type | code | checksum | parameter-problem | packet-too-big | id | sequence | max-delay}
This expression refers to ICMPv6 header fields. When using it in inet, bridge or netdev families, it will cause an implicit dependency on IPv6 to be created. To match on unusual cases like ICMPv6 over IPv4, one has to add an explicit meta protocol ip match to the rule.
Table 44. ICMPv6 header expression ┌──────────────────┬──────────────────────┬──────────────────┐ │Keyword           │ Description          │ Type             │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │type              │ ICMPv6 type field    │ icmpv6_type      │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │code              │ ICMPv6 code field    │ integer (8 bit)  │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │checksum          │ ICMPv6 checksum      │ integer (16 bit) │ │                  │ field                │                  │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │parameter-problem │ pointer to problem   │ integer (32 bit) │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │packet-too-big    │ oversized MTU        │ integer (32 bit) │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │id                │ ID of echo           │ integer (16 bit) │ │                  │ request/response     │                  │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │sequence          │ sequence number of   │ integer (16 bit) │ │                  │ echo                 │                  │ │                  │ request/response     │                  │ ├──────────────────┼──────────────────────┼──────────────────┤ │                  │                      │                  │ │max-delay         │ maximum response     │ integer (16 bit) │ │                  │ delay of MLD queries │                  │ └──────────────────┴──────────────────────┴──────────────────┘
TCP HEADER EXPRESSION tcp {sport | dport | sequence | ackseq | doff | reserved | flags | window | checksum | urgptr}
Table 45. TCP header expression ┌─────────┬──────────────────┬──────────────────┐ │Keyword  │ Description      │ Type             │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │sport    │ Source port      │ inet_service     │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │dport    │ Destination port │ inet_service     │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │sequence │ Sequence number  │ integer (32 bit) │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │ackseq   │ Acknowledgement  │ integer (32 bit) │ │         │ number           │                  │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │doff     │ Data offset      │ integer (4 bit)  │ │         │                  │ FIXME scaling    │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │reserved │ Reserved area    │ integer (4 bit)  │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │flags    │ TCP flags        │ tcp_flag         │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │window   │ Window           │ integer (16 bit) │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │checksum │ Checksum         │ integer (16 bit) │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │urgptr   │ Urgent pointer   │ integer (16 bit) │ └─────────┴──────────────────┴──────────────────┘
UDP HEADER EXPRESSION udp {sport | dport | length | checksum}
Table 46. UDP header expression ┌─────────┬─────────────────────┬──────────────────┐ │Keyword  │ Description         │ Type             │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │sport    │ Source port         │ inet_service     │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │dport    │ Destination port    │ inet_service     │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │length   │ Total packet length │ integer (16 bit) │ ├─────────┼─────────────────────┼──────────────────┤ │         │                     │                  │ │checksum │ Checksum            │ integer (16 bit) │ └─────────┴─────────────────────┴──────────────────┘
UDP-LITE HEADER EXPRESSION udplite {sport | dport | checksum}
Table 47. UDP-Lite header expression ┌─────────┬──────────────────┬──────────────────┐ │Keyword  │ Description      │ Type             │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │sport    │ Source port      │ inet_service     │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │dport    │ Destination port │ inet_service     │ ├─────────┼──────────────────┼──────────────────┤ │         │                  │                  │ │checksum │ Checksum         │ integer (16 bit) │ └─────────┴──────────────────┴──────────────────┘
SCTP HEADER EXPRESSION sctp {sport | dport | vtag | checksum} sctp chunk CHUNK [ FIELD ]
CHUNK := data | init | init-ack | sack | heartbeat | heartbeat-ack | abort | shutdown | shutdown-ack | error | cookie-echo | cookie-ack | ecne | cwr | shutdown-complete | asconf-ack | forward-tsn | asconf
FIELD := COMMON_FIELD | DATA_FIELD | INIT_FIELD | INIT_ACK_FIELD | SACK_FIELD | SHUTDOWN_FIELD | ECNE_FIELD | CWR_FIELD | ASCONF_ACK_FIELD | FORWARD_TSN_FIELD | ASCONF_FIELD
COMMON_FIELD := type | flags | length DATA_FIELD := tsn | stream | ssn | ppid INIT_FIELD := init-tag | a-rwnd | num-outbound-streams | num-inbound-streams | initial-tsn INIT_ACK_FIELD := INIT_FIELD SACK_FIELD := cum-tsn-ack | a-rwnd | num-gap-ack-blocks | num-dup-tsns SHUTDOWN_FIELD := cum-tsn-ack ECNE_FIELD := lowest-tsn CWR_FIELD := lowest-tsn ASCONF_ACK_FIELD := seqno FORWARD_TSN_FIELD := new-cum-tsn ASCONF_FIELD := seqno
Table 48. SCTP header expression ┌─────────┬──────────────────┬────────────────────┐ │Keyword  │ Description      │ Type               │ ├─────────┼──────────────────┼────────────────────┤ │         │                  │                    │ │sport    │ Source port      │ inet_service       │ ├─────────┼──────────────────┼────────────────────┤ │         │                  │                    │ │dport    │ Destination port │ inet_service       │ ├─────────┼──────────────────┼────────────────────┤ │         │                  │                    │ │vtag     │ Verification Tag │ integer (32 bit)   │ ├─────────┼──────────────────┼────────────────────┤ │         │                  │                    │ │checksum │ Checksum         │ integer (32 bit)   │ ├─────────┼──────────────────┼────────────────────┤ │         │                  │                    │ │chunk    │ Search chunk in  │ without FIELD,     │ │         │ packet           │ boolean indicating │ │         │                  │ existence          │ └─────────┴──────────────────┴────────────────────┘
DCCP HEADER EXPRESSION dccp {sport | dport | type}
Table 50. DCCP header expression ┌────────┬──────────────────┬──────────────┐ │Keyword │ Description      │ Type         │ ├────────┼──────────────────┼──────────────┤ │        │                  │              │ │sport   │ Source port      │ inet_service │ ├────────┼──────────────────┼──────────────┤ │        │                  │              │ │dport   │ Destination port │ inet_service │ ├────────┼──────────────────┼──────────────┤ │        │                  │              │ │type    │ Packet type      │ dccp_pkttype │ └────────┴──────────────────┴──────────────┘
AUTHENTICATION HEADER EXPRESSION ah {nexthdr | hdrlength | reserved | spi | sequence}
Table 51. AH header expression ┌──────────┬──────────────────────┬──────────────────┐ │Keyword   │ Description          │ Type             │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │nexthdr   │ Next header protocol │ inet_proto       │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │hdrlength │ AH Header length     │ integer (8 bit)  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │reserved  │ Reserved area        │ integer (16 bit) │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │spi       │ Security Parameter   │ integer (32 bit) │ │          │ Index                │                  │ ├──────────┼──────────────────────┼──────────────────┤ │          │                      │                  │ │sequence  │ Sequence number      │ integer (32 bit) │ └──────────┴──────────────────────┴──────────────────┘
ENCRYPTED SECURITY PAYLOAD HEADER EXPRESSION esp {spi | sequence}
Table 52. ESP header expression ┌─────────┬────────────────────┬──────────────────┐ │Keyword  │ Description        │ Type             │ ├─────────┼────────────────────┼──────────────────┤ │         │                    │                  │ │spi      │ Security Parameter │ integer (32 bit) │ │         │ Index              │                  │ ├─────────┼────────────────────┼──────────────────┤ │         │                    │                  │ │sequence │ Sequence number    │ integer (32 bit) │ └─────────┴────────────────────┴──────────────────┘
IPCOMP HEADER EXPRESSION comp {nexthdr | flags | cpi}
Table 53. IPComp header expression ┌────────┬──────────────────────┬──────────────────┐ │Keyword │ Description          │ Type             │ ├────────┼──────────────────────┼──────────────────┤ │        │                      │                  │ │nexthdr │ Next header protocol │ inet_proto       │ ├────────┼──────────────────────┼──────────────────┤ │        │                      │                  │ │flags   │ Flags                │ bitmask          │ ├────────┼──────────────────────┼──────────────────┤ │        │                      │                  │ │cpi     │ compression          │ integer (16 bit) │ │        │ Parameter Index      │                  │ └────────┴──────────────────────┴──────────────────┘
RAW PAYLOAD EXPRESSION @base,offset,length
The raw payload expression instructs to load length bits starting at offset bits. Bit 0 refers to the very first bit — in the C programming language, this corresponds to the topmost bit, i.e. 0x80 in case of an octet. They are useful to match headers that do not have a human-readable template expression yet. Note that nft will not add dependencies for Raw payload expressions. If you e.g. want to match protocol fields of a transport header with protocol number 5, you need to manually exclude packets that have a different transport header, for instance by using meta l4proto 5 before the raw expression.
Table 54. Supported payload protocol bases ┌─────┬─────────────────────────────┐ │Base │ Description                 │ ├─────┼─────────────────────────────┤ │     │                             │ │ll   │ Link layer, for example the │ │     │ Ethernet header             │ ├─────┼─────────────────────────────┤ │     │                             │ │nh   │ Network header, for example │ │     │ IPv4 or IPv6                │ ├─────┼─────────────────────────────┤ │     │                             │ │th   │ Transport Header, for       │ │     │ example TCP                 │ └─────┴─────────────────────────────┘
Matching destination port of both UDP and TCP.
inet filter input meta l4proto {tcp, udp} @th,16,16 { 53, 80 }
The above can also be written as
inet filter input meta l4proto {tcp, udp} th dport { 53, 80 }
it is more convenient, but like the raw expression notation no dependencies are created or checked. It is the users responsibility to restrict matching to those header types that have a notion of ports. Otherwise, rules using raw expressions will errnously match unrelated packets, e.g. mis-interpreting ESP packets SPI field as a port.
Rewrite arp packet target hardware address if target protocol address matches a given address.
input meta iifname enp2s0 arp ptype 0x0800 arp htype 1 arp hlen 6 arp plen 4 @nh,192,32 0xc0a88f10 @nh,144,48 set 0x112233445566 accept
EXTENSION HEADER EXPRESSIONS Extension header expressions refer to data from variable-sized protocol headers, such as IPv6 extension headers, TCP options and IPv4 options.
nftables currently supports matching (finding) a given ipv6 extension header, TCP option or IPv4 option.
hbh {nexthdr | hdrlength} frag {nexthdr | frag-off | more-fragments | id} rt {nexthdr | hdrlength | type | seg-left} dst {nexthdr | hdrlength} mh {nexthdr | hdrlength | checksum | type} srh {flags | tag | sid | seg-left} tcp option {eol | nop | maxseg | window | sack-perm | sack | sack0 | sack1 | sack2 | sack3 | timestamp} tcp_option_field ip option { lsrr | ra | rr | ssrr } ip_option_field
The following syntaxes are valid only in a relational expression with boolean type on right-hand side for checking header existence only:
exthdr {hbh | frag | rt | dst | mh} tcp option {eol | nop | maxseg | window | sack-perm | sack | sack0 | sack1 | sack2 | sack3 | timestamp} ip option { lsrr | ra | rr | ssrr }
Table 55. IPv6 extension headers ┌────────┬────────────────────────┐ │Keyword │ Description            │ ├────────┼────────────────────────┤ │        │                        │ │hbh     │ Hop by Hop             │ ├────────┼────────────────────────┤ │        │                        │ │rt      │ Routing Header         │ ├────────┼────────────────────────┤ │        │                        │ │frag    │ Fragmentation header   │ ├────────┼────────────────────────┤ │        │                        │ │dst     │ dst options            │ ├────────┼────────────────────────┤ │        │                        │ │mh      │ Mobility Header        │ ├────────┼────────────────────────┤ │        │                        │ │srh     │ Segment Routing Header │ └────────┴────────────────────────┘
Table 56. TCP Options ┌──────────┬─────────────────────┬──────────────────────┐ │Keyword   │ Description         │ TCP option fields    │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │eol       │ End if option list  │ kind                 │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │nop       │ 1 Byte TCP Nop      │ kind                 │ │          │ padding option      │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │maxseg    │ TCP Maximum Segment │ kind, length, size   │ │          │ Size                │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │window    │ TCP Window Scaling  │ kind, length, count  │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack-perm │ TCP SACK permitted  │ kind, length         │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack      │ TCP Selective       │ kind, length, left,  │ │          │ Acknowledgement     │ right                │ │          │ (alias of block 0)  │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack0     │ TCP Selective       │ kind, length, left,  │ │          │ Acknowledgement     │ right                │ │          │ (block 0)           │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack1     │ TCP Selective       │ kind, length, left,  │ │          │ Acknowledgement     │ right                │ │          │ (block 1)           │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack2     │ TCP Selective       │ kind, length, left,  │ │          │ Acknowledgement     │ right                │ │          │ (block 2)           │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │sack3     │ TCP Selective       │ kind, length, left,  │ │          │ Acknowledgement     │ right                │ │          │ (block 3)           │                      │ ├──────────┼─────────────────────┼──────────────────────┤ │          │                     │                      │ │timestamp │ TCP Timestamps      │ kind, length, tsval, │ │          │                     │ tsecr                │ └──────────┴─────────────────────┴──────────────────────┘
TCP option matching also supports raw expression syntax to access arbitrary options:
tcp option @number,offset,length
Table 57. IP Options ┌────────┬─────────────────────┬─────────────────────┐ │Keyword │ Description         │ IP option fields    │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │lsrr    │ Loose Source Route  │ type, length, ptr,  │ │        │                     │ addr                │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │ra      │ Router Alert        │ type, length, value │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │rr      │ Record Route        │ type, length, ptr,  │ │        │                     │ addr                │ ├────────┼─────────────────────┼─────────────────────┤ │        │                     │                     │ │ssrr    │ Strict Source Route │ type, length, ptr,  │ │        │                     │ addr                │ └────────┴─────────────────────┴─────────────────────┘
finding TCP options.
filter input tcp option sack-perm kind 1 counter
matching IPv6 exthdr.
ip6 filter input frag more-fragments 1 counter
finding IP option.
filter input ip option lsrr exists counter
CONNTRACK EXPRESSIONS Conntrack expressions refer to meta data of the connection tracking entry associated with a packet.
There are three types of conntrack expressions. Some conntrack expressions require the flow direction before the conntrack key, others must be used directly because they are direction agnostic. The packets, bytes and avgpkt keywords can be used with or without a direction. If the direction is omitted, the sum of the original and the reply direction is returned. The same is true for the zone, if a direction is given, the zone is only matched if the zone id is tied to the given direction.
ct {state | direction | status | mark | expiration | helper | label | count | id} ct [original | reply] {l3proto | protocol | bytes | packets | avgpkt | zone} ct {original | reply} {proto-src | proto-dst} ct {original | reply} {ip | ip6} {saddr | daddr}
The conntrack-specific types in this table are described in the sub-section CONNTRACK TYPES above.
restrict the number of parallel connections to a server.
nft add set filter ssh_flood '{ type ipv4_addr; flags dynamic; }' nft add rule filter input tcp dport 22 add @ssh_flood '{ ip saddr ct count over 2 }' reject
Statements represent actions to be performed. They can alter control flow (return, jump to a different chain, accept or drop the packet) or can perform actions, such as logging, rejecting a packet, etc.
Statements exist in two kinds. Terminal statements unconditionally terminate evaluation of the current rule, non-terminal statements either only conditionally or never terminate evaluation of the current rule, in other words, they are passive from the ruleset evaluation perspective. There can be an arbitrary amount of non-terminal statements in a rule, but only a single terminal statement as the final statement.
VERDICT STATEMENT The verdict statement alters control flow in the ruleset and issues policy decisions for packets.
{accept | drop | queue | continue | return} {jump | goto} chain
accept and drop are absolute verdicts — they terminate ruleset evaluation immediately.
accept       Terminate ruleset evaluation and accept the packet. The packet can still be dropped later by another hook, for instance accept in the forward hook still allows to drop the packet later in the postrouting hook, or another forward base chain that has a higher priority number and is evaluated afterwards in the processing pipeline.
drop         Terminate ruleset evaluation and drop the packet. The drop occurs instantly, no further chains or hooks are evaluated. It is not possible to accept the packet in a later chain again, as those are not evaluated anymore for the packet.
queue        Terminate ruleset evaluation and queue the packet to userspace. Userspace must provide a drop or accept verdict. In case of accept, processing resumes with the next base chain hook, not the rule following the queue verdict.
continue     Continue ruleset evaluation with the next rule. This is the default behaviour in case a rule issues no verdict.
return       Return from the current chain and continue evaluation at the next rule in the last chain. If issued in a base chain, it is equivalent to the base chain policy.
jump chain   Continue evaluation at the first rule in chain. The current position in the ruleset is pushed to a call stack and evaluation will continue there when the new chain is entirely evaluated or a return verdict is issued. In case an absolute verdict is issued by a rule in the chain, ruleset evaluation terminates immediately and the specific action is taken.
goto chain   Similar to jump, but the current position is not pushed to the call stack, meaning that after the new chain evaluation will continue at the last chain instead of the one containing the goto statement.
Using verdict statements.
# process packets from eth0 and the internal network in from_lan # chain, drop all packets from eth0 with different source addresses.
filter input iif eth0 ip saddr 192.168.0.0/24 jump from_lan filter input iif eth0 drop
PAYLOAD STATEMENT payload_expression set value
The payload statement alters packet content. It can be used for example to set ip DSCP (diffserv) header field or ipv6 flow labels.
route some packets instead of bridging.
# redirect tcp:http from 192.160.0.0/16 to local machine for routing instead of bridging # assumes 00:11:22:33:44:55 is local MAC address. bridge input meta iif eth0 ip saddr 192.168.0.0/16 tcp dport 80 meta pkttype set unicast ether daddr set 00:11:22:33:44:55
Set IPv4 DSCP header field.
ip forward ip dscp set 42
EXTENSION HEADER STATEMENT extension_header_expression set value
The extension header statement alters packet content in variable-sized headers. This can currently be used to alter the TCP Maximum segment size of packets, similar to TCPMSS.
change tcp mss.
tcp flags syn tcp option maxseg size set 1360 # set a size based on route information: tcp flags syn tcp option maxseg size set rt mtu
LOG STATEMENT log [prefix quoted_string] [level syslog-level] [flags log-flags] log group nflog_group [prefix quoted_string] [queue-threshold value] [snaplen size] log level audit
The log statement enables logging of matching packets. When this statement is used from a rule, the Linux kernel will print some information on all matching packets, such as header fields, via the kernel log (where it can be read with dmesg(1) or read in the syslog).
In the second form of invocation (if nflog_group is specified), the Linux kernel will pass the packet to nfnetlink_log which will send the log through a netlink socket to the specified group. One userspace process may subscribe to the group to receive the logs, see man(8) ulogd for the Netfilter userspace log daemon and libnetfilter_log documentation for details in case you would like to develop a custom program to digest your logs.
In the third form of invocation (if level audit is specified), the Linux kernel writes a message into the audit buffer suitably formatted for reading with auditd. Therefore no further formatting options (such as prefix or flags) are allowed in this mode.
This is a non-terminating statement, so the rule evaluation continues after the packet is logged.
Table 59. log statement options ┌────────────────┬──────────────────────┬──────────────────────┐ │Keyword         │ Description          │ Type                 │ ├────────────────┼──────────────────────┼──────────────────────┤ │                │                      │                      │ │prefix          │ Log message prefix   │ quoted string        │ ├────────────────┼──────────────────────┼──────────────────────┤ │                │                      │                      │ │level           │ Syslog level of      │ string: emerg,       │ │                │ logging              │ alert, crit, err,    │ │                │                      │ warn [default],      │ │                │                      │ notice, info, debug, │ │                │                      │ audit                │ ├────────────────┼──────────────────────┼──────────────────────┤ │                │                      │                      │ │group           │ NFLOG group to send  │ unsigned integer (16 │ │                │ messages to          │ bit)                 │ ├────────────────┼──────────────────────┼──────────────────────┤ │                │                      │                      │ │snaplen         │ Length of packet     │ unsigned integer (32 │ │                │ payload to include   │ bit)                 │ │                │ in netlink message   │                      │ ├────────────────┼──────────────────────┼──────────────────────┤ │                │                      │                      │ │queue-threshold │ Number of packets to │ unsigned integer (32 │ │                │ queue inside the     │ bit)                 │ │                │ kernel before        │                      │ │                │ sending them to      │                      │ │                │ userspace            │                      │ └────────────────┴──────────────────────┴──────────────────────┘
Table 60. log-flags ┌─────────────┬─────────────────────────────┐ │Flag         │ Description                 │ ├─────────────┼─────────────────────────────┤ │             │                             │ │tcp sequence │ Log TCP sequence numbers.   │ ├─────────────┼─────────────────────────────┤ │             │                             │ │tcp options  │ Log options from the TCP    │ │             │ packet header.              │ ├─────────────┼─────────────────────────────┤ │             │                             │ │ip options   │ Log options from the        │ │             │ IP/IPv6 packet header.      │ ├─────────────┼─────────────────────────────┤ │             │                             │ │skuid        │ Log the userid of the       │ │             │ process which generated the │ │             │ packet.                     │ ├─────────────┼─────────────────────────────┤ │             │                             │ │ether        │ Decode MAC addresses and    │ │             │ protocol.                   │ ├─────────────┼─────────────────────────────┤ │             │                             │ │all          │ Enable all log flags listed │ │             │ above.                      │ └─────────────┴─────────────────────────────┘
Using log statement.
# log the UID which generated the packet and ip options ip filter output log flags skuid flags ip options
# log the tcp sequence numbers and tcp options from the TCP packet ip filter output log flags tcp sequence,options
# enable all supported log flags ip6 filter output log flags all
REJECT STATEMENT reject [ with REJECT_WITH ]
REJECT_WITH := icmp icmp_code | icmpv6 icmpv6_code | icmpx icmpx_code | tcp reset
A reject statement is used to send back an error packet in response to the matched packet otherwise it is equivalent to drop so it is a terminating statement, ending rule traversal. This statement is only valid in base chains using the input, forward or output hooks, and user-defined chains which are only called from those chains.
Table 61. different ICMP reject variants are meant for use in different table families ┌────────┬────────┬─────────────┐ │Variant │ Family │ Type        │ ├────────┼────────┼─────────────┤ │        │        │             │ │icmp    │ ip     │ icmp_code   │ ├────────┼────────┼─────────────┤ │        │        │             │ │icmpv6  │ ip6    │ icmpv6_code │ ├────────┼────────┼─────────────┤ │        │        │             │ │icmpx   │ inet   │ icmpx_code  │ └────────┴────────┴─────────────┘
For a description of the different types and a list of supported keywords refer to DATA TYPES section above. The common default reject value is port-unreachable.
Note that in bridge family, reject statement is only allowed in base chains which hook into input or prerouting.
COUNTER STATEMENT A counter statement sets the hit count of packets along with the number of bytes.
counter packets number bytes number counter { packets number | bytes number }
CONNTRACK STATEMENT The conntrack statement can be used to set the conntrack mark and conntrack labels.
ct {mark | event | label | zone} set value
The ct statement sets meta data associated with a connection. The zone id has to be assigned before a conntrack lookup takes place, i.e. this has to be done in prerouting and possibly output (if locally generated packets need to be placed in a distinct zone), with a hook priority of raw (-300).
Unlike iptables, where the helper assignment happens in the raw table, the helper needs to be assigned after a conntrack entry has been found, i.e. it will not work when used with hook priorities equal or before -200.
Table 62. Conntrack statement types ┌────────┬──────────────────────┬──────────────────────┐ │Keyword │ Description          │ Value                │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │event   │ conntrack event bits │ bitmask, integer (32 │ │        │                      │ bit)                 │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │helper  │ name of ct helper    │ quoted string        │ │        │ object to assign to  │                      │ │        │ the connection       │                      │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │mark    │ Connection tracking  │ mark                 │ │        │ mark                 │                      │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │label   │ Connection tracking  │ label                │ │        │ label                │                      │ ├────────┼──────────────────────┼──────────────────────┤ │        │                      │                      │ │zone    │ conntrack zone       │ integer (16 bit)     │ └────────┴──────────────────────┴──────────────────────┘
save packet nfmark in conntrack.
ct mark set meta mark
set zone mapped via interface.
table inet raw { chain prerouting { type filter hook prerouting priority raw; ct zone set iif map { "eth1" : 1, "veth1" : 2 } } chain output { type filter hook output priority raw; ct zone set oif map { "eth1" : 1, "veth1" : 2 } } }
restrict events reported by ctnetlink.
ct event set new,related,destroy
NOTRACK STATEMENT The notrack statement allows to disable connection tracking for certain packets.
notrack
Note that for this statement to be effective, it has to be applied to packets before a conntrack lookup happens. Therefore, it needs to sit in a chain with either prerouting or output hook and a hook priority of -300 (raw) or less.
See SYNPROXY STATEMENT for an example usage.
META STATEMENT A meta statement sets the value of a meta expression. The existing meta fields are: priority, mark, pkttype, nftrace.
meta {mark | priority | pkttype | nftrace} set value
A meta statement sets meta data associated with a packet.
Table 63. Meta statement types ┌─────────┬─────────────────────┬───────────┐ │Keyword  │ Description         │ Value     │ ├─────────┼─────────────────────┼───────────┤ │         │                     │           │ │priority │ TC packet priority  │ tc_handle │ ├─────────┼─────────────────────┼───────────┤ │         │                     │           │ │mark     │ Packet mark         │ mark      │ ├─────────┼─────────────────────┼───────────┤ │         │                     │           │ │pkttype  │ packet type         │ pkt_type  │ ├─────────┼─────────────────────┼───────────┤ │         │                     │           │ │nftrace  │ ruleset packet      │ 0, 1      │ │         │ tracing on/off. Use │           │ │         │ monitor trace       │           │ │         │ command to watch    │           │ │         │ traces              │           │ └─────────┴─────────────────────┴───────────┘
LIMIT STATEMENT limit rate [over] packet_number / TIME_UNIT [burst packet_number packets] limit rate [over] byte_number BYTE_UNIT / TIME_UNIT [burst byte_number BYTE_UNIT]
TIME_UNIT := second | minute | hour | day BYTE_UNIT := bytes | kbytes | mbytes
A limit statement matches at a limited rate using a token bucket filter. A rule using this statement will match until this limit is reached. It can be used in combination with the log statement to give limited logging. The optional over keyword makes it match over the specified rate. Default burst is 5. if you specify burst, it must be non-zero value.
Table 64. limit statement values ┌──────────────┬───────────────────┬──────────────────────┐ │Value         │ Description       │ Type                 │ ├──────────────┼───────────────────┼──────────────────────┤ │              │                   │                      │ │packet_number │ Number of packets │ unsigned integer (32 │ │              │                   │ bit)                 │ ├──────────────┼───────────────────┼──────────────────────┤ │              │                   │                      │ │byte_number   │ Number of bytes   │ unsigned integer (32 │ │              │                   │ bit)                 │ └──────────────┴───────────────────┴──────────────────────┘
NAT STATEMENTS snat [[ip | ip6] to] ADDR_SPEC [:PORT_SPEC] [FLAGS] dnat [[ip | ip6] to] ADDR_SPEC [:PORT_SPEC] [FLAGS] masquerade [to :PORT_SPEC] [FLAGS] redirect [to :PORT_SPEC] [FLAGS]
ADDR_SPEC := address | address - address PORT_SPEC := port | port - port
FLAGS  := FLAG [, FLAGS] FLAG  := persistent | random | fully-random
The nat statements are only valid from nat chain types.
The snat and masquerade statements specify that the source address of the packet should be modified. While snat is only valid in the postrouting and input chains, masquerade makes sense only in postrouting. The dnat and redirect statements are only valid in the prerouting and output chains, they specify that the destination address of the packet should be modified. You can use non-base chains which are called from base chains of nat chain type too. All future packets in this connection will also be mangled, and rules should cease being examined.
The masquerade statement is a special form of snat which always uses the outgoing interface’s IP address to translate to. It is particularly useful on gateways with dynamic (public) IP addresses.
The redirect statement is a special form of dnat which always translates the destination address to the local host’s one. It comes in handy if one only wants to alter the destination port of incoming traffic on different interfaces.
When used in the inet family (available with kernel 5.2), the dnat and snat statements require the use of the ip and ip6 keyword in case an address is provided, see the examples below.
Before kernel 4.18 nat statements require both prerouting and postrouting base chains to be present since otherwise packets on the return path won’t be seen by netfilter and therefore no reverse translation will take place.
Table 65. NAT statement values ┌───────────┬──────────────────────┬──────────────────────┐ │Expression │ Description          │ Type                 │ ├───────────┼──────────────────────┼──────────────────────┤ │           │                      │                      │ │address    │ Specifies that the   │ ipv4_addr,           │ │           │ source/destination   │ ipv6_addr, e.g.      │ │           │ address of the       │ abcd::1234, or you   │ │           │ packet should be     │ can use a mapping,   │ │           │ modified. You may    │ e.g. meta mark map { │ │           │ specify a mapping to │ 10 : 192.168.1.2, 20 │ │           │ relate a list of     │ : 192.168.1.3 }      │ │           │ tuples composed of   │                      │ │           │ arbitrary expression │                      │ │           │ key with address     │                      │ │           │ value.               │                      │ ├───────────┼──────────────────────┼──────────────────────┤ │           │                      │                      │ │port       │ Specifies that the   │ port number (16 bit) │ │           │ source/destination   │                      │ │           │ address of the       │                      │ │           │ packet should be     │                      │ │           │ modified.            │                      │ └───────────┴──────────────────────┴──────────────────────┘
Table 66. NAT statement flags ┌─────────────┬─────────────────────────────┐ │Flag         │ Description                 │ ├─────────────┼─────────────────────────────┤ │             │                             │ │persistent   │ Gives a client the same     │ │             │ source-/destination-address │ │             │ for each connection.        │ ├─────────────┼─────────────────────────────┤ │             │                             │ │random       │ In kernel 5.0 and newer     │ │             │ this is the same as         │ │             │ fully-random. In earlier    │ │             │ kernels the port mapping    │ │             │ will be randomized using a  │ │             │ seeded MD5 hash mix using   │ │             │ source and destination      │ │             │ address and destination     │ │             │ port.                       │ ├─────────────┼─────────────────────────────┤ │             │                             │ │fully-random │ If used then port mapping   │ │             │ is generated based on a     │ │             │ 32-bit pseudo-random        │ │             │ algorithm.                  │ └─────────────┴─────────────────────────────┘
Using NAT statements.
# create a suitable table/chain setup for all further examples add table nat add chain nat prerouting { type nat hook prerouting priority dstnat; } add chain nat postrouting { type nat hook postrouting priority srcnat; }
# translate source addresses of all packets leaving via eth0 to address 1.2.3.4 add rule nat postrouting oif eth0 snat to 1.2.3.4
# redirect all traffic entering via eth0 to destination address 192.168.1.120 add rule nat prerouting iif eth0 dnat to 192.168.1.120
# translate source addresses of all packets leaving via eth0 to whatever # locally generated packets would use as source to reach the same destination add rule nat postrouting oif eth0 masquerade
# redirect incoming TCP traffic for port 22 to port 2222 add rule nat prerouting tcp dport 22 redirect to :2222
# inet family: # handle ip dnat: add rule inet nat prerouting dnat ip to 10.0.2.99 # handle ip6 dnat: add rule inet nat prerouting dnat ip6 to fe80::dead # this masquerades both ipv4 and ipv6: add rule inet nat postrouting meta oif ppp0 masquerade