<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.nftables.org/wiki-nftables/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paulobruck</id>
	<title>nftables wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.nftables.org/wiki-nftables/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Paulobruck"/>
	<link rel="alternate" type="text/html" href="http://wiki.nftables.org/wiki-nftables/index.php/Special:Contributions/Paulobruck"/>
	<updated>2026-04-05T18:27:17Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://wiki.nftables.org/wiki-nftables/index.php?title=Sets&amp;diff=231</id>
		<title>Sets</title>
		<link rel="alternate" type="text/html" href="http://wiki.nftables.org/wiki-nftables/index.php?title=Sets&amp;diff=231"/>
		<updated>2017-12-28T12:22:25Z</updated>

		<summary type="html">&lt;p&gt;Paulobruck: cut and paste part of Eric Leblond to anonymous sets&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;nftables&#039;&#039; comes with a built-in generic set infrastructure that allows you to use &#039;&#039;&#039;any&#039;&#039;&#039; supported selector to build sets. This infrastructure makes possible the representation of [[dictionaries]] and [[maps]].&lt;br /&gt;
&lt;br /&gt;
The set elements are internally represented using performance data structures such as hashtables and red-black trees.&lt;br /&gt;
&lt;br /&gt;
= Anonymous sets =&lt;br /&gt;
&lt;br /&gt;
Anonymous sets are those that are:&lt;br /&gt;
&lt;br /&gt;
* Bound to a rule, if the rule is removed, that set is released too.&lt;br /&gt;
* They have no specific name, the kernel internally allocates an identifier.&lt;br /&gt;
* They cannot be updated. So you cannot add and delete elements from it once it is bound to a rule.&lt;br /&gt;
&lt;br /&gt;
The following example shows how to create a simple set.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add rule filter output tcp dport { 22, 23 } counter&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This rule above catches all traffic going to TCP ports 22 and 23, in case of matching the counters are updated.&lt;br /&gt;
&lt;br /&gt;
Eric Leblond in his [https://home.regit.org/2014/01/why-you-will-love-nftables/ Why you will love nftables] article shows a very simple example to compare iptables with nftables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ip6tables -A INPUT -p tcp -m multiport --dports 23,80,443 -j ACCEPT&lt;br /&gt;
ip6tables -A INPUT -p icmpv6 --icmpv6-type neighbor-solicitation -j ACCEPT&lt;br /&gt;
ip6tables -A INPUT -p icmpv6 --icmpv6-type echo-request -j ACCEPT&lt;br /&gt;
ip6tables -A INPUT -p icmpv6 --icmpv6-type router-advertisement -j ACCEPT&lt;br /&gt;
ip6tables -A INPUT -p icmpv6 --icmpv6-type neighbor-advertisement -j ACCEPT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which can be expressed in &#039;&#039;nftables&#039;&#039; with a couple of rules that provide a set:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add rule ip6 filter input tcp dport {telnet, http, https} accept&lt;br /&gt;
% nft add rule ip6 filter input icmpv6 type { nd-neighbor-solicit, echo-request, nd-router-advert, nd-neighbor-advert } accept&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Named sets =&lt;br /&gt;
&lt;br /&gt;
You can create the named sets with the following command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add set filter blackhole { type ipv4_addr\;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that &#039;&#039;blackhole&#039;&#039; is the name of the set in this case. The &#039;&#039;type&#039;&#039; option indicates the data type that this set stores, which is an IPv4 address in this case. Current maximum name length is 16 characters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add element filter blackhole { 192.168.3.4 }&lt;br /&gt;
% nft add element filter blackhole { 192.168.1.4, 192.168.1.5 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you can use it from the rule:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add rule ip input ip saddr @blackhole drop&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Named sets can be updated anytime, so you can add and delete element from them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Named sets specifications =&lt;br /&gt;
&lt;br /&gt;
Sets specifications are:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;type&#039;&#039;&#039;, is obligatory and determines the data type of the set elements. Supported data types currently are:&lt;br /&gt;
** &#039;&#039;ipv4_addr&#039;&#039;: IPv4 address&lt;br /&gt;
** &#039;&#039;ipv6_addr&#039;&#039;: IPv6 address.&lt;br /&gt;
** &#039;&#039;ether_addr&#039;&#039;: Ethernet address.&lt;br /&gt;
** &#039;&#039;inet_proto&#039;&#039;: Inet protocol type.&lt;br /&gt;
** &#039;&#039;inet_service&#039;&#039;: Internet service (read tcp port for example)&lt;br /&gt;
** &#039;&#039;mark&#039;&#039;: Mark type.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;timeout&#039;&#039;&#039;, it determines how long an element stays in the set. The time string respects the format: &#039;&#039;&amp;quot;v&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;dv&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;hv&amp;lt;sub&amp;gt;3&amp;lt;/sub&amp;gt;mv&amp;lt;sub&amp;gt;4&amp;lt;/sub&amp;gt;s&amp;quot;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add table filter&lt;br /&gt;
% nft add set filter ports {type inet_service \; timeout 3h45s \;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These commands create a table named &#039;&#039;filter&#039;&#039; and add a set named &#039;&#039;ports&#039;&#039; to it, where elements are deleted after 3 hours and 45 seconds of being added.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;flags&#039;&#039;&#039;, the available flags are:&lt;br /&gt;
** &#039;&#039;constant&#039;&#039; - set content may not change while bound&lt;br /&gt;
** &#039;&#039;interval&#039;&#039; - set contains intervals&lt;br /&gt;
** &#039;&#039;timeout&#039;&#039; - elements can be added with a timeout&lt;br /&gt;
&lt;br /&gt;
Multiple flags should be separated by comma:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add set filter flags_set {type ipv4_addr\; flags constant, interval\;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;gc-interval&#039;&#039;&#039;, stands for garbage collection interval, can only be used if &#039;&#039;timeout&#039;&#039; or &#039;&#039;flags timeout&#039;&#039; are active. The interval follows the same format of &#039;&#039;timeouts&#039;&#039; time string &#039;&#039;&amp;quot;v&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;dv&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;hv&amp;lt;sub&amp;gt;3&amp;lt;/sub&amp;gt;mv&amp;lt;sub&amp;gt;4&amp;lt;/sub&amp;gt;s&amp;quot;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;elements&#039;&#039;&#039;, initialize the set with some elements in it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add set filter daddrs {type ipv4_addr \; flags timeout \; elements={192.168.1.1 timeout 10s, 192.168.1.2 timeout 30s} \;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This command creates a set name &#039;&#039;daddrs&#039;&#039; with elements &#039;&#039;192.168.1.1&#039;&#039;, which stays in it for 10s, and &#039;&#039;192.168.1.2&#039;&#039;, which stays for 30s.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;size&#039;&#039;&#039;, limits the maximum number of elements of the set. To create a set with maximum 2 elements type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft add set filter saddrs {type ipv4_addr \; size 2 \;}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;policy&#039;&#039;&#039;, determines set selection policy. Available values are:&lt;br /&gt;
** &#039;&#039;performance&#039;&#039; [default]&lt;br /&gt;
** &#039;&#039;memory&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
= Listing named sets =&lt;br /&gt;
&lt;br /&gt;
You can list the content of a named set via:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
% nft list set filter myset&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Paulobruck</name></author>
	</entry>
</feed>