Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion .agents/skills/netifyd/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ until the next reload and then vanishes. Change the generator or its UCI input i
| `table inet netifyd` (nftables) | `/usr/sbin/ns-netifyd-configure` (on start and reload) | `/etc/config/netifyd` → `ns_config` |
| `netify-application-catalog.json` and the other catalog/category files | `dpi-data-update` (nightly cron) | Netify's data service |
| `/usr/share/nftables.d/table-pre/` DPI chains | `/usr/sbin/dpi-nft` | `/etc/config/dpi` |
| `address-groups.d/30-*.conf` | `nethsec.dpi.write_address_groups` (on commit, by `ns.ovpnrw`, by `/usr/sbin/dpi`) | `/etc/config/objects`, `dhcp`, `users` |

`/usr/sbin/dpi` is the whole DPI apply path: `dpi-config` → `dpi-nft` → `/etc/init.d/netifyd reload`.
`/usr/sbin/dpi` is the whole DPI apply path: `dpi-migrate` → `write-address-groups` → `dpi-config` → `dpi-nft` →
`/etc/init.d/netifyd reload`.
Run it after changing `/etc/config/dpi`; don't reproduce its steps by hand.

---
Expand Down Expand Up @@ -204,6 +206,25 @@ Two operational facts:

MAC members only match via `mac ==`, so don't put them in a group a `local_ip` rule references.

The `30-` prefix belongs to `nethsec.dpi.write_address_groups`: one `30-<database>_<id>.conf` group
per firewall object resolving to addresses, used by a DPI rule or not, with a `# <object name>` and a
`# <id>, generated from the firewall objects: do not edit` header, and every other `30-*.conf` file is
removed. It runs from the `update-address-groups.py` post-commit hook (`objects`, `dhcp`, `users`), from
`ns.ovpnrw` (reserved IP of a VPN user) and from `/usr/sbin/dpi`, before `dpi-config`: at boot the `dpi`
service (`START=40`) runs it before netifyd (`START=99`); `dpi-config` only references the groups. The `30-*.conf` files are in
`/lib/upgrade/keep.d/ns-dpi`, so backups and the HA sync carry them. The agent keeps the groups it loaded
in memory, a reload doesn't drop the ones whose file was removed (it drops their single addresses, not
their networks), and on stop it saves all of them back, without comments: that's why ownership goes by
prefix. The group of a deleted object therefore comes back as a file on the next agent restart
(unreferenced, so harmless) until the next write removes it; the boot write runs between the stop that
saved it and the start that would load it, so after a reboot it is gone from memory too. The
`flush-address-group` socket command drops a group from memory and deletes its file. A `kill -9` saves nothing. Verified on a device with agent 5.2.9: a reload re-reads the groups
without restarting the agent; a range line is silently ignored, so ranges are written as CIDR blocks;
a `/0` matches nothing (in a group and inline), so it is written as two `/1`; an IPv4-mapped IPv6 address
never matches an IPv4 flow; an empty group or a missing tag matches nothing without errors. IPv6
literals written inline in a criteria, especially the ones starting with `::`, were seen to trigger an
intermittent `criteria parse error` that takes the agent down: a group reference never does.

---

## Verify on a live device
Expand Down
1 change: 1 addition & 0 deletions packages/ns-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ define Package/ns-api/install
$(INSTALL_BIN) ./files/post-commit/update-timezone.py $(1)/usr/libexec/ns-api/post-commit
$(INSTALL_BIN) ./files/post-commit/restart-wireguard.py $(1)/usr/libexec/ns-api/post-commit
$(INSTALL_BIN) ./files/post-commit/clear-user-tokens.py $(1)/usr/libexec/ns-api/post-commit
$(INSTALL_BIN) ./files/post-commit/update-address-groups.py $(1)/usr/libexec/ns-api/post-commit
$(INSTALL_BIN) ./files/pre-commit/clean-network.py $(1)/usr/libexec/ns-api/pre-commit
$(INSTALL_BIN) ./files/remove-pppoe-keepalive $(1)/usr/share/ns-api
$(INSTALL_DIR) $(1)/etc/uci-defaults
Expand Down
61 changes: 58 additions & 3 deletions packages/ns-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,8 @@ The APIs can raise the following validation errors:
- reserved_ip_must_be_in_server_network
- reserverd_ip_already_used
- reserved_ip_must_be_in_server_network
- reserved_ip_is_used: the reserved IP can't be removed while the user is used as an object, e.g. by a
DPI rule, a firewall rule or a host set; the error value lists where it is used

### disconnect-user

Expand Down Expand Up @@ -3801,6 +3803,9 @@ Rules with `ns_visible '0'` are left out.
previous schema, carry their raw `criteria` and can be renamed, enabled, disabled, reordered and
deleted, but not edited
- `match_all`: the rule matches every flow instead of naming application groups
- `source_objects`: objects the source references, with their `name`, `type` and `subtype` (the same
values `ns.objects list-hosts` reports). An object gone from the configuration is reported with its id
as name and `null` type
- `index`: position of the rule in the list

Example response:
Expand All @@ -3813,7 +3818,15 @@ Example response:
"name": "Block streaming",
"enabled": true,
"action": "block",
"source": ["192.168.1.0/24"],
"source": [],
"source_objects": [
{
"id": "objects/ns_16faa17f",
"name": "office",
"type": "host_set",
"subtype": "host_set"
}
],
"appgroups": [
{
"id": "ns_1a2b3c4d5",
Expand All @@ -3830,6 +3843,7 @@ Example response:
"enabled": true,
"action": "allow",
"source": [],
"source_objects": [],
"appgroups": [],
"match_all": true,
"managed": true,
Expand All @@ -3841,6 +3855,7 @@ Example response:
"enabled": true,
"action": "block",
"source": [],
"source_objects": [],
"appgroups": [],
"match_all": false,
"managed": false,
Expand All @@ -3860,7 +3875,8 @@ api-cli ns.dpi add-rule --data '{
"name": "Block streaming",
"enabled": true,
"action": "block",
"source": ["192.168.1.0/24"],
"source": [],
"source_objects": ["objects/ns_16faa17f"],
"appgroups": ["ns_1a2b3c4d5"],
"match_all": false,
"position": "bottom"
Expand All @@ -3872,7 +3888,12 @@ api-cli ns.dpi add-rule --data '{
- `action`: mandatory, `block` or `allow`. `allow` stops the evaluation, so the rules below it don't apply
to the matching flows
- `source`: addresses, networks or ranges, IPv4 or IPv6, e.g.
`["192.168.1.1", "192.168.1.0/24", "10.0.0.10-10.0.0.20"]`. Empty or omitted matches every host
`["192.168.1.1", "192.168.1.0/24", "10.0.0.10-10.0.0.20"]`. Empty or omitted, together with
`source_objects`, matches every host
- `source_objects`: ids of objects whose addresses the rule is narrowed to, see `list-source-objects`:
host sets, DHCP static leases, DNS records and VPN users with a reserved IP. Domain sets are refused.
Alternative to `source`: a rule can't have both (`source_objects_with_source`). A change to a
referenced object applies to the rule on commit, without editing it
- `appgroups`: ids of the application groups to match, see `list-appgroups`. Mandatory unless
`match_all` is `true`
- `match_all`: match every flow, optionally narrowed by `source`. `appgroups` must be empty. Defaults to
Expand All @@ -3899,6 +3920,7 @@ api-cli ns.dpi edit-rule --data '{
"enabled": false,
"action": "block",
"source": [],
"source_objects": [],
"appgroups": ["ns_1a2b3c4d5"],
"match_all": false
}'
Expand All @@ -3912,6 +3934,34 @@ Example response:
}
```

### list-source-objects

List the objects a rule source can reference: the same objects as `ns.objects list-hosts`, domain sets
excluded.

```bash
api-cli ns.dpi list-source-objects
```

Example response:

```json
{
"values": [
{
"name": "office",
"family": "ipv4",
"id": "objects/ns_16faa17f",
"singleton": false,
"subtype": "host_set",
"used": true,
"matches": ["dpi/ns_3869dc35"],
"type": "host_set"
}
]
}
```

### delete-rule

Delete a rule. The remaining rules are renumbered:
Expand Down Expand Up @@ -7995,6 +8045,11 @@ Example response:

Manage domain sets and host sets.

Every object resolving to addresses (host sets, DHCP static leases, DNS records, VPN users with a reserved
IP) has a netifyd address group, `/etc/netifyd/address-groups.d/30-<database>_<id>.conf`, used by the DPI
rules. The `update-address-groups.py` post-commit hook writes them when `objects`, `dhcp` or `users` are
committed, see the `ns-dpi` package.

### list-domain-sets

List all domain sets:
Expand Down
12 changes: 9 additions & 3 deletions packages/ns-api/files/ns.dpi
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import json
import sys

from euci import EUci
from nethsec import dpi, utils
from nethsec import dpi, objects, utils

cmd = sys.argv[1]

if cmd == 'list':
print(json.dumps({
'list-rules': {},
'list-source-objects': {},
'add-rule': {
'name': 'str',
'enabled': False,
'action': 'str',
'source': [],
'source_objects': [],
'appgroups': [],
'match_all': False,
'position': 'str'
Expand All @@ -31,6 +33,7 @@ if cmd == 'list':
'enabled': False,
'action': 'str',
'source': [],
'source_objects': [],
'appgroups': [],
'match_all': False
},
Expand Down Expand Up @@ -102,17 +105,20 @@ elif cmd == 'call':
print(json.dumps(utils.generic_error('catalog_not_available')))
elif action == 'list-rules':
print(json.dumps({'values': dpi.list_rules(e_uci)}))
elif action == 'list-source-objects':
print(json.dumps({'values': objects.list_objects(e_uci, include_domain_sets=False)}))
elif action == 'add-rule':
data = json.load(sys.stdin)
config_name = dpi.add_rule(e_uci, data['name'], data['enabled'], data['action'],
data.get('source', []), data.get('appgroups', []),
data.get('position', 'bottom'), data.get('match_all', False))
data.get('position', 'bottom'), data.get('match_all', False),
data.get('source_objects', []))
print(json.dumps({'id': config_name}))
elif action == 'edit-rule':
data = json.load(sys.stdin)
config_name = dpi.edit_rule(e_uci, data['id'], data['name'], data['enabled'], data['action'],
data.get('source', []), data.get('appgroups', []),
data.get('match_all', False))
data.get('match_all', False), data.get('source_objects', []))
print(json.dumps({'id': config_name}))
elif action == 'delete-rule':
data = json.load(sys.stdin)
Expand Down
14 changes: 13 additions & 1 deletion packages/ns-api/files/ns.ovpnrw
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import pyotp
import ipaddress
import subprocess
from euci import EUci
from nethsec import utils, ovpn, firewall, users, objects
from nethsec import utils, ovpn, firewall, users, objects, dpi
from datetime import datetime, timezone
import tarfile
import io
Expand Down Expand Up @@ -807,6 +807,8 @@ def add_user(args):
else:
users.edit_local_user(u, args['username'], extra_fields=ovpn_config)
u.commit("users")
# add related address groups used by DPI rules
dpi.update_address_groups(u)
return {"result": "success"}

def import_users(ovpninstance):
Expand Down Expand Up @@ -841,6 +843,12 @@ def edit_user(args):
if user_id == None:
return utils.validation_error("username", "user_not_found", args["username"])

# an object needs its reserved IP: removing it would leave the rules using the user without an address
if not args["ipaddr"] and u.get("users", user_id, 'openvpn_ipaddr', default=""):
used, matches = objects.is_used_object(u, f'users/{user_id}')
if used:
return utils.validation_error("ipaddr", "reserved_ip_is_used", matches)

if args["ipaddr"] and u.get("users", user_id, 'openvpn_ipaddr', default="") != args["ipaddr"]:
valid, error = is_valid_ip(ovpninstance, args["ipaddr"])
if not valid:
Expand All @@ -856,6 +864,8 @@ def edit_user(args):
pass
u.set("users", user_id, "openvpn_enabled", args["enabled"])
u.commit("users")
# update related address groups used by DPI rules
dpi.update_address_groups(u)
return {"result": "success"}

def delete_user(ovpninstance, username):
Expand Down Expand Up @@ -889,6 +899,8 @@ def delete_user(ovpninstance, username):
u.commit("users")
except:
pass
# delete related address groups used by DPI rules
dpi.update_address_groups(u)
try:
revoke_user_certificate(ovpninstance, username)
except:
Expand Down
16 changes: 16 additions & 0 deletions packages/ns-api/files/post-commit/update-address-groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python

#
# Copyright (C) 2026 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

# This script writes the netifyd address groups of the firewall objects once their changes are committed:
# one file per object resolving to addresses, the ones of objects that are gone removed, and netifyd
# reloaded when a file changed. DPI rules match an object through its group.

from euci import EUci
from nethsec import dpi

if "objects" in changes or "dhcp" in changes or "users" in changes: # noqa: F821
dpi.update_address_groups(EUci())
5 changes: 4 additions & 1 deletion packages/ns-dpi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fi
exit 0
endef

define Package/ns-plug/conffiles
define Package/ns-dpi/conffiles
/etc/config/dpi
endef

Expand All @@ -68,6 +68,7 @@ define Package/ns-dpi/install
$(INSTALL_DIR) $(1)/etc/uci-defaults
$(INSTALL_DIR) $(1)/usr/share/ns-plug/hooks/unregister
$(INSTALL_DIR) $(1)/etc/xtables
$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
$(INSTALL_CONF) ./files/connlabel.conf $(1)/etc/connlabel.conf
$(LN) /etc/connlabel.conf $(1)/etc/xtables/connlabel.conf
$(INSTALL_BIN) ./files/dpi.init $(1)/etc/init.d/dpi
Expand All @@ -81,6 +82,8 @@ define Package/ns-dpi/install
$(INSTALL_BIN) ./files/dpi-data-update.py $(1)/usr/sbin/dpi-data-update
$(INSTALL_BIN) ./files/dpi-update.py $(1)/usr/sbin/dpi-update
$(INSTALL_BIN) ./files/dpi-migrate.py $(1)/usr/libexec/ns-dpi/dpi-migrate
$(INSTALL_BIN) ./files/write-address-groups $(1)/usr/libexec/ns-dpi/write-address-groups
$(INSTALL_CONF) ./files/address-groups.keep $(1)/lib/upgrade/keep.d/ns-dpi
$(INSTALL_CONF) ./files/20_dpi $(1)/etc/uci-defaults
$(INSTALL_BIN) ./files/21_dpi_migrate $(1)/etc/uci-defaults/21_dpi_migrate
$(INSTALL_BIN) ./files/23_dpi_drop_options $(1)/etc/uci-defaults/23_dpi_drop_options
Expand Down
Loading
Loading