Difference between revisions of "Logging traffic"

From nftables wiki
Jump to: navigation, search
(Created page with "'''Note''': Full logging support is available starting Linux kernel 3.17. If you run an older kernel, you have to modprobe ipt_LOG to enable logging. You can log packets usin...")
 
(add some bits about log flags)
Line 40: Line 40:
  
 
And you'll start seeing log messages for each new ''ssh'' connection.
 
And you'll start seeing log messages for each new ''ssh'' connection.
 +
 +
= Log flags =
 +
 +
Since nftables v0.7, log flags are supported.
 +
 +
Enable logging of TCP sequence and options:
 +
 +
<source>
 +
% nft add rule x y log flags tcp sequence,options
 +
</source>
 +
 +
Enable IP options:
 +
 +
<source>
 +
% nft add rule x y log flags ip options
 +
</source>
 +
 +
Enable socket UID:
 +
 +
<source>
 +
% nft add rule x y log flags skuid
 +
</source>
 +
 +
Enable ethernet link layer address:
 +
 +
<source>
 +
% nft add rule x y log flags ether
 +
</source>
 +
 +
Enable all flags:
 +
 +
<source>
 +
% nft add rule x y log flags all
 +
</source>

Revision as of 13:13, 5 January 2017

Note: Full logging support is available starting Linux kernel 3.17. If you run an older kernel, you have to modprobe ipt_LOG to enable logging.

You can log packets using the log action. The most simple rule to log all incoming traffic is:

% nft add rule filter input log

A typical rule match, log and accept incoming ssh traffic looks like:

% nft add rule filter input tcp dport 22 ct state new log prefix \"New SSH connection: \" accept

The prefix indicates the initial string that is used as prefix for the log message.

Note that nftables allows to perform two actions in one single rule, contrary to iptables which required two rules for this.

Also note that the rule is evaluated from the left to the right. So the following rule:

nft add rule filter input iif lo log tcp dport 22 accept

will log all packets coming on lo interface and not only the ones with destination port 22.

Queueing logging to userspace

As in iptables, you can use the existing nflog infrastructure to send log messages to ulogd2 or your custom userspace application based on libnetfilter_log.

To do so, you only have to indicate the nflog group:

% nft add rule filter input tcp dport 22 ct state new log prefix \"New SSH connection: \" group 0 accept

Then, run the example test application:

libnetfilter_log/utils% ./nfulnl_test

And you'll start seeing log messages for each new ssh connection.

Log flags

Since nftables v0.7, log flags are supported.

Enable logging of TCP sequence and options:

% nft add rule x y log flags tcp sequence,options

Enable IP options:

% nft add rule x y log flags ip options

Enable socket UID:

% nft add rule x y log flags skuid

Enable ethernet link layer address:

% nft add rule x y log flags ether

Enable all flags:

% nft add rule x y log flags all