Commit graph

68 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
6622df9d38
allow to configure nfqueue bypass flag
Nfqueue bypass option skips the enqueue of packets to userspace
if no application is listening to the queue.
https://wiki.nftables.org/wiki-nftables/index.php/Queueing_to_userspace

If this flag is not specified, and for example the daemon dies
unexpectedly, all the outbound traffic will be blocked.

Up until now we've been using this flag by default not to block network
traffic if the daemon dies or is killed for some reason. But some users
want to use precisely this behaviour (#884, #1183, #1201).

Now you can configure it, to block connections if the daemon
unexpectedly dies.

The option is on by default in the configuration (QueueBypass: true).
If this item is not present in the daemon config file, then it'll be
false.
2024-10-19 10:51:40 +02:00
Gustavo Iñiguez Goia
f882cf428b
fw: fixed adding 'counter' obj to rules
https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes#Counter
2024-07-03 14:26:11 +02:00
Gustavo Iñiguez Goia
efc05663eb
fw: allow to configure interception queue number
- Added new configuration field to allow configure fw interception
   number queue (default to 0):
   "FwOptions": {
      "QueueNum": 0
   }
   (we still need to reconfigure nfqueue queues in order for this to
take effect).
 - If the fw configuration path is not supplied, default to
   /etc/opensnitchd/system-fw.json
2024-05-14 23:41:25 +02:00
Gustavo Iñiguez Goia
c9ad9005e3
fw minor changes
use struct{} instead of bool for exit channels, func parms cosmetic
change.
2024-05-13 01:38:57 +02:00
Gustavo Iñiguez Goia
8935bfe6b0
do not flush conns when adding the inteception rules
part of previous commit.
2024-05-13 01:33:28 +02:00
Gustavo Iñiguez Goia
e5787aef65
structs fields reorganized
Structs' fields reorganized based on fieldalignment tool output
2024-01-14 20:44:49 +01:00
Gustavo Iñiguez Goia
090bb0e2a4
updated sys-fw tests 2023-12-20 23:02:45 +01:00
Gustavo Iñiguez Goia
54ac5a3549
fw: allow to configure config file/ check interval
- Allow to configure system firewall configuration file path:
   * via cli (-fw-config-file).
   * via global configuration file.
 - Allow to configure fw rules check interval.

The system fw config file contains regular iptables/nftables rules.
Previously it was hardcoded to /etc/opensnitchd/system-fw.json

The interval to check if the interception rules were added was also
hardcoded to 10 seconds. Now it's possible to configure it.
A value of "0s" disables the interval, while "" defaults to 10 seconds.
2023-12-20 21:32:45 +01:00
Gustavo Iñiguez Goia
9cee3b3a73
fixed leak checking fw status 2023-12-15 11:49:16 +01:00
Gustavo Iñiguez Goia
26b8415925
force to reestablish non-local connections on start
When we start to intercept connections, we flush out the conntrack
table, to force already established connections reconnect again so we
can intercept them, and let the user choose if allow or deny them.

Since we no longer use conntrack states to intercept TCP connections, we
now close existing connections, leaving to the applications reestablish
them again.
Local connections are excluded, because it may cause problems with some
local servers.

Both options interfere with the established connections, so you may
experience ocasional network interruptions when enabling the
interception for the first time.

Discussion: #995
2023-07-25 01:42:54 +02:00
Gustavo Iñiguez Goia
d1598fdf67
sys fw: allow to use wildcards to match interfaces
Now it's possible to use "br*" to match all network interfaces that
start with "br".

Closes #996
2023-07-24 16:11:12 +02:00
Gustavo Iñiguez Goia
e090833d29
intercept packets only with the SYN flag set
Using "ct state NEW" to intercept packets causes some undesired effects:
  We intercept packets that not only have the SYN flag set, like ACK,
  ACK+PSH or SYN+ACK. Mainly response packets.

  This means that the IPs are not always in the expected order:
  443:1.1.1.1 -> 192.168.1.123:12345
  which causes sometimes not to obtain the process of the connection,
  because the connection in the system appears as
  12345:192.168.1.123 -> 1.1.1.1:443

Intercepting packets with *only* the SYN flag set seems to resolve
this problem.
2023-07-23 15:14:05 +02:00
Gustavo Iñiguez Goia
aa7b1e1a02
misc: removed debug msg, fixed warning msg
- Fixed displaying verdict warning message.
 - Removed debug message.
2023-07-17 16:48:44 +02:00
Gustavo Iñiguez Goia
8740755f64
sys fw: report errors to the GUI after reloading
- Send errors to the server (GUI) if there's any error when reloading
   the system fw rules (far from being perfect/optimal, needs a
   rewrite).
 - Don't load the configuration after saving it, let the watcher reload
   it on write change to avoid double reload/duplicated errors.
2023-07-15 20:32:42 +02:00
Gustavo Iñiguez Goia
3de24febf8
sys fw: improved rules errors logs 2023-07-13 17:24:38 +02:00
Gustavo Iñiguez Goia
ad1cdd4015
tests: better sys-fw log,meta and ip tests 2023-07-12 12:53:13 +02:00
Gustavo Iñiguez Goia
eaec4a825c
tests: added sys-fw conntrack tests 2023-07-11 15:32:15 +02:00
Gustavo Iñiguez Goia
3ca5645c7e
tests: added quota tests 2023-07-11 13:53:01 +02:00
Gustavo Iñiguez Goia
7441aa2ccc
tests: rewritten nat tests
Move common nat checks to its own file.
2023-07-11 13:15:26 +02:00
Gustavo Iñiguez Goia
c2e6032a86
tests: added needed helpers 2023-07-11 01:16:23 +02:00
Gustavo Iñiguez Goia
dad8c0c65d
tests: added queue, nat tests
- Added queue, snat, dnat, redirect, tproxy and masquerade tests.
- Return errors if verdict parameters are not formatted as expected.
2023-07-11 00:57:15 +02:00
Gustavo Iñiguez Goia
6ddd4dcee6
tests: added meta tests, improved ip tests 2023-07-09 23:05:12 +02:00
Gustavo Iñiguez Goia
b1ebc0eb22
sys fw: fixed setting marks on packets
Fix for meta mark set 666
2023-07-09 23:03:50 +02:00
Gustavo Iñiguez Goia
a18404fba9
tests: added sys-fw ip and iface tests
- Added ip daddr, saddr tests.
- Added basic iface tests.
2023-07-08 21:50:46 +02:00
Gustavo Iñiguez Goia
603c4ab39a
tests: added sys-fw verdict tests
- Added accept, drop, return and jump tests.
- Added reject tests.
2023-07-07 13:56:25 +02:00
Gustavo Iñiguez Goia
1a063d10e2
tests: added new sys fw tests
- Added counter tests.
- Added ethernet addresses tests.

Added comments to some functions.
2023-07-07 00:46:09 +02:00
Gustavo Iñiguez Goia
e268a88cff
tests: added sys fw ports tests 2023-07-06 17:59:30 +02:00
Gustavo Iñiguez Goia
28fab440da
sys,fw: check for errors when adding ports rules
Verify that ports are correctly converted to int, for single and range
ports.
2023-07-06 17:58:17 +02:00
Gustavo Iñiguez Goia
84df5135ff
tests: added log and protocol expressions 2023-07-06 16:02:14 +02:00
Gustavo Iñiguez Goia
aa8e793dc4
sys,fw: fixed compilation 2023-07-06 14:33:56 +02:00
Gustavo Iñiguez Goia
18e583d20e
tests: updated sys fw tests 2023-07-06 14:27:54 +02:00
Gustavo Iñiguez Goia
344819eb30
sys,fw: export more internal funcs 2023-07-06 14:09:54 +02:00
Gustavo Iñiguez Goia
d474e7f57a
sys,fw: export some internal utils. 2023-07-06 13:36:57 +02:00
Gustavo Iñiguez Goia
f1ef00a5fd
tests: added nftables/system tests
- test add/delete system fw rules
- test fw config disabled.
2023-07-01 22:41:04 +02:00
Gustavo Iñiguez Goia
0a2dcf6406
tests: added basic nftables/rules tests 2023-06-30 16:19:50 +02:00
Gustavo Iñiguez Goia
e345d61076
tests: added nftables/monitor tests 2023-06-30 10:51:18 +02:00
Gustavo Iñiguez Goia
78ac6c0f6b
tests: disable some tests by default, added utils.go tests
- These tests require permissions for creating new namespaces.
  By default disable them for now.
- Added utils.go tests.
2023-06-29 16:19:47 +02:00
Gustavo Iñiguez Goia
d3b40108c7
tests: added sys fw basic tables/chains tests 2023-06-29 11:27:18 +02:00
Gustavo Iñiguez Goia
f7eec83367
sys fw: allow to add icmp types by commas
- Previously we only supported multiple ICMP types on the same rule
by adding multiple keys:
 Key: type
 Value: echo-request
 Key: type
 Value: echo-reply

Now it's possible to specify them using ',':
 Key: type
 Value: echo-request,echo-reply

- Validate ICMP types before adding them.
2023-06-20 01:14:57 +02:00
Gustavo Iñiguez Goia
9bad34bb16
changed default config permissions on saving
- Changed default permissions of default-config.json, system-fw.json
 - Changed bool values by constants.
2023-06-14 12:10:19 +02:00
Gustavo Iñiguez Goia
9c483b1a59
sys,fw: allow to create multiprotocol rules
Now you can add rules to allow multiple protocols.
For example you can add a rule to allow dport/sport for both TCP
and UDP.

There're two options to allow a port:

Statement {
 Name: tcp
 Values:
   Key: dport
   Value: 1234
}

Statement {
 Name: meta
 Values:
   Key: l4proto
   Value: tcp,udp

   Key: dport
   Value: 1234
}

Closes #951.
2023-05-27 22:02:14 +02:00
Gustavo Iñiguez Goia
810b785b3a
sys,fw: put dns rule always at the top of the chain
The DNS rule to intercept DNS responses must always be at the top of
the (input-filter) rules, otherwise we won't receive DNS resolutions.

Adding, removing or changing system fw rules was removing the rule from 1st
position.

Another approach to this problem could be to remove&&add only the dns rule,
instead of disable-enable interception+rules monitor.
2023-05-25 01:20:53 +02:00
Gustavo Iñiguez Goia
9f71c15e57
sys,fw: fixed race condition creating system rules
Hard to reproduce, but not impossible
2023-05-24 14:26:58 +02:00
Gustavo Iñiguez Goia
6c456aefba
sys fw: fixed race condition reloading rules 2023-05-19 01:35:20 +02:00
Gustavo Iñiguez Goia
fe88841640
sys fw: support ports range operators
Allow to use operators (==, !=) when adding firewall rules with port
ranges (444-555).
2023-04-15 00:33:47 +02:00
Gustavo Iñiguez Goia
e32881c03f
sys fw: allow to apply operators on Meta and Ct expressions
Allow to apply operators (==, !=) on Meta (mark, skuid, skgid, etc) and
Conntrack Mark expressions.
2023-04-13 01:36:59 +02:00
Gustavo Iñiguez Goia
b7c6c8b8db
fw: minor changes for better code reading 2023-01-30 13:43:44 +01:00
Gustavo Iñiguez Goia
b546fb9e7a
Bump nftables-go lib version to 0.1.0 2023-01-22 14:40:08 +01:00
Gustavo Iñiguez Goia
9dfcca295a
fixed adding interception chains on old kernels (4.x) 2023-01-07 22:28:52 +01:00
Gustavo Iñiguez Goia
d31c4e86bf
fixed adding interception rule on some kernels
On some kernels (4.19), adding the interception rule to the
inet-mangle-output chain failed.

According to the nftables wiki, the mangle-output chain have (must?) to
be of type Route:
"route type: ... mangle table ... for the output hook (for other
hooks use type filter instead)."

https://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains#Base_chain_types

So if we fail adding the interception rule, we retry it with type Filter
instead of Route.

Related: #781 , ced9a24
2023-01-04 21:44:46 +01:00