nftables-napi 0.0.2 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -12
- package/binding.gyp +3 -1
- package/lib/index.d.ts +149 -34
- package/lib/index.js +2 -2
- package/package.json +4 -5
- package/prebuilds/linux-arm64/nftables-napi.node +0 -0
- package/prebuilds/linux-x64/nftables-napi.node +0 -0
- package/src/netlink/constants.h +25 -0
- package/src/netlink/nft_config.h +26 -26
- package/src/netlink/nftnl_raii.h +14 -4
- package/src/netlink/nl_batch.h +4 -4
- package/src/netlink/nl_result.h +1 -1
- package/src/netlink/nl_socket.cpp +20 -16
- package/src/netlink/nl_socket.h +2 -2
- package/src/netlink/operation.h +1 -1
- package/src/netlink/set_ops.cpp +103 -18
- package/src/netlink/set_ops.h +35 -4
- package/src/netlink/table_ops.cpp +203 -46
- package/src/nft_manager.cpp +413 -62
- package/src/nft_manager.h +5 -0
- package/src/validation.cpp +12 -0
- package/src/validation.h +10 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# nftables-napi
|
|
2
2
|
|
|
3
|
-
Native Node.js binding for nftables via libnftnl + libmnl. Manages IPv4/IPv6
|
|
3
|
+
Native Node.js binding for nftables via libnftnl + libmnl. Manages IPv4/IPv6 tables with dynamic sets and timeout support through direct netlink communication — no shell commands, no `nft` CLI.
|
|
4
4
|
|
|
5
5
|
Requires Linux with `CAP_NET_ADMIN` or root.
|
|
6
6
|
|
|
@@ -35,8 +35,7 @@ const { NftManager } = require("nftables-napi");
|
|
|
35
35
|
|
|
36
36
|
const nft = new NftManager({
|
|
37
37
|
tableName: "tablename",
|
|
38
|
-
|
|
39
|
-
droplistSetName: "droplist",
|
|
38
|
+
sets: ["blacklist", "droplist"],
|
|
40
39
|
});
|
|
41
40
|
|
|
42
41
|
await nft.createTable();
|
|
@@ -62,13 +61,10 @@ await nft.deleteTable();
|
|
|
62
61
|
|
|
63
62
|
### `new NftManager(options)`
|
|
64
63
|
|
|
65
|
-
| Option
|
|
66
|
-
|
|
|
67
|
-
| `tableName`
|
|
68
|
-
| `
|
|
69
|
-
| `droplistSetName` | `string` | Yes | Droplist set name (IPv6 auto-appends `'6'`) |
|
|
70
|
-
|
|
71
|
-
Log prefixes are auto-generated: `'{setName}: '` for each set.
|
|
64
|
+
| Option | Type | Required | Description |
|
|
65
|
+
| ----------- | ---------- | -------- | ------------------------------------------------------------------------------------------------ |
|
|
66
|
+
| `tableName` | `string` | Yes | Base table name (IPv6 table auto-appends `'6'`) |
|
|
67
|
+
| `sets` | `string[]` | Yes | Set names (1+, unique, non-empty). IPv6 sets auto-append `'6'`. Log prefix: `'{name}: '` |
|
|
72
68
|
|
|
73
69
|
### Methods
|
|
74
70
|
|
|
@@ -76,8 +72,8 @@ All methods return `Promise<void>`.
|
|
|
76
72
|
|
|
77
73
|
| Method | Description |
|
|
78
74
|
| -------------------------------------- | --------------------------------------------------------------------- |
|
|
79
|
-
| `createTable()` | Create IPv4/IPv6 tables with
|
|
80
|
-
| `deleteTable()` | Delete both tables. Idempotent.
|
|
75
|
+
| `createTable()` | Create IPv4/IPv6 tables with all configured sets and filter chains. Idempotent. |
|
|
76
|
+
| `deleteTable()` | Delete both tables. Idempotent. |
|
|
81
77
|
| `addAddress({ ip, set, timeout? })` | Add IP to set. `timeout` in seconds, omit for permanent. |
|
|
82
78
|
| `removeAddress({ ip, set })` | Remove IP from set. Idempotent. |
|
|
83
79
|
| `addAddresses({ ips, set, timeout? })` | Bulk add to set. Chunked for efficient netlink communication. |
|
package/binding.gyp
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Native nftables manager for Linux firewall.
|
|
3
|
-
* Manages IPv4/IPv6
|
|
3
|
+
* Manages IPv4/IPv6 tables with dynamic sets via libnftnl + libmnl (direct netlink, no nft CLI).
|
|
4
4
|
* Requires CAP_NET_ADMIN or root privileges.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
export type TargetSet = 'blacklist' | 'droplist';
|
|
9
|
-
|
|
10
|
-
/** Constructor options. All fields are required — no defaults. */
|
|
7
|
+
/** Constructor options. */
|
|
11
8
|
export interface NftManagerOptions {
|
|
12
9
|
/** Base table name. IPv6 table auto-appends '6'. */
|
|
13
10
|
tableName: string;
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Input/forward IP set names (≥1 required). Block by source address.
|
|
13
|
+
* Rules: log prefix "<setName>: " + named counter + drop on input and forward chains.
|
|
14
|
+
* IPv6 sets auto-append '6'.
|
|
15
|
+
*/
|
|
16
|
+
sets: string[];
|
|
17
|
+
/**
|
|
18
|
+
* Output IP set names (optional). Block by destination address.
|
|
19
|
+
* Rules: named counter + drop on output chain (no log).
|
|
20
|
+
* IPv6 sets auto-append '6'.
|
|
21
|
+
*/
|
|
22
|
+
outSets?: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Output port set names (optional). Block by tcp/udp destination port.
|
|
25
|
+
* Rules: single concatenated (proto . port) lookup + named counter + drop on output chain (no log).
|
|
26
|
+
* Port is added to BOTH IPv4 and IPv6 tables (ports are family-independent).
|
|
27
|
+
* IPv6 sets auto-append '6'.
|
|
28
|
+
*/
|
|
29
|
+
outPortSets?: string[];
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
/** Options for adding a single address. */
|
|
21
33
|
export interface AddAddressOptions {
|
|
22
34
|
/** IPv4 or IPv6 address (e.g., "1.2.3.4" or "2001:db8::1"). */
|
|
23
35
|
ip: string;
|
|
24
|
-
/** Target set
|
|
25
|
-
set:
|
|
26
|
-
/** Timeout in seconds. Omit for permanent
|
|
36
|
+
/** Target set name (must match one from constructor's sets or outSets). */
|
|
37
|
+
set: string;
|
|
38
|
+
/** Timeout in seconds. Omit for permanent. */
|
|
27
39
|
timeout?: number;
|
|
28
40
|
}
|
|
29
41
|
|
|
@@ -31,17 +43,17 @@ export interface AddAddressOptions {
|
|
|
31
43
|
export interface RemoveAddressOptions {
|
|
32
44
|
/** IPv4 or IPv6 address to remove. */
|
|
33
45
|
ip: string;
|
|
34
|
-
/** Target set
|
|
35
|
-
set:
|
|
46
|
+
/** Target set name (must match one from constructor's sets or outSets). */
|
|
47
|
+
set: string;
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
/** Options for bulk adding addresses. */
|
|
39
51
|
export interface AddAddressesOptions {
|
|
40
52
|
/** Array of IPv4/IPv6 addresses. */
|
|
41
53
|
ips: string[];
|
|
42
|
-
/** Target set
|
|
43
|
-
set:
|
|
44
|
-
/** Timeout in seconds. Omit for permanent
|
|
54
|
+
/** Target set name (must match one from constructor's sets or outSets). */
|
|
55
|
+
set: string;
|
|
56
|
+
/** Timeout in seconds. Omit for permanent. */
|
|
45
57
|
timeout?: number;
|
|
46
58
|
}
|
|
47
59
|
|
|
@@ -49,8 +61,52 @@ export interface AddAddressesOptions {
|
|
|
49
61
|
export interface RemoveAddressesOptions {
|
|
50
62
|
/** Array of IPv4/IPv6 addresses to remove. */
|
|
51
63
|
ips: string[];
|
|
52
|
-
/** Target set
|
|
53
|
-
set:
|
|
64
|
+
/** Target set name (must match one from constructor's sets or outSets). */
|
|
65
|
+
set: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Options for adding a single port. */
|
|
69
|
+
export interface AddPortOptions {
|
|
70
|
+
/** Port number (0-65535). */
|
|
71
|
+
port: number;
|
|
72
|
+
/** Target port set name (must match one from constructor's outPortSets). */
|
|
73
|
+
set: string;
|
|
74
|
+
/** Protocol: 'tcp', 'udp', or omit for both. Default: both. */
|
|
75
|
+
protocol?: 'tcp' | 'udp';
|
|
76
|
+
/** Timeout in seconds. Omit for permanent. */
|
|
77
|
+
timeout?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Options for removing a single port. */
|
|
81
|
+
export interface RemovePortOptions {
|
|
82
|
+
/** Port number (0-65535). */
|
|
83
|
+
port: number;
|
|
84
|
+
/** Target port set name (must match one from constructor's outPortSets). */
|
|
85
|
+
set: string;
|
|
86
|
+
/** Protocol: 'tcp', 'udp', or omit for both. Default: both. */
|
|
87
|
+
protocol?: 'tcp' | 'udp';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Options for bulk adding ports. */
|
|
91
|
+
export interface AddPortsOptions {
|
|
92
|
+
/** Array of port numbers (0-65535). */
|
|
93
|
+
ports: number[];
|
|
94
|
+
/** Target port set name (must match one from constructor's outPortSets). */
|
|
95
|
+
set: string;
|
|
96
|
+
/** Protocol: 'tcp', 'udp', or omit for both. Default: both. */
|
|
97
|
+
protocol?: 'tcp' | 'udp';
|
|
98
|
+
/** Timeout in seconds. Omit for permanent. */
|
|
99
|
+
timeout?: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Options for bulk removing ports. */
|
|
103
|
+
export interface RemovePortsOptions {
|
|
104
|
+
/** Array of port numbers (0-65535). */
|
|
105
|
+
ports: number[];
|
|
106
|
+
/** Target port set name (must match one from constructor's outPortSets). */
|
|
107
|
+
set: string;
|
|
108
|
+
/** Protocol: 'tcp', 'udp', or omit for both. Default: both. */
|
|
109
|
+
protocol?: 'tcp' | 'udp';
|
|
54
110
|
}
|
|
55
111
|
|
|
56
112
|
export class NftManager {
|
|
@@ -58,52 +114,65 @@ export class NftManager {
|
|
|
58
114
|
* Creates a new NftManager instance.
|
|
59
115
|
* Opens a netlink socket and validates configuration.
|
|
60
116
|
*
|
|
61
|
-
* @param options - Required configuration with table and set names.
|
|
117
|
+
* @param options - Required configuration with table name and set names.
|
|
62
118
|
* @throws {TypeError} if options are missing or have wrong types
|
|
63
119
|
* @throws {Error} if netlink socket cannot be opened (missing CAP_NET_ADMIN)
|
|
64
120
|
*/
|
|
65
121
|
constructor(options: NftManagerOptions);
|
|
66
122
|
|
|
67
123
|
/**
|
|
68
|
-
* Creates IPv4 and IPv6 tables with
|
|
124
|
+
* Creates IPv4 and IPv6 tables with all configured sets, chains, named counters, and filter rules.
|
|
69
125
|
* Idempotent — destroys existing tables first, then recreates.
|
|
126
|
+
*
|
|
127
|
+
* Creates:
|
|
128
|
+
* - Named counter "processed" (global traffic counter per chain)
|
|
129
|
+
* - Named counter per set (blocked traffic counter)
|
|
130
|
+
* - Input chain with log + counter + drop rules (for sets)
|
|
131
|
+
* - Forward chain with log + counter + drop rules (for sets)
|
|
132
|
+
* - Output chain with counter + drop rules (for outSets and outPortSets, no log)
|
|
133
|
+
* - Per-element counters on all sets
|
|
134
|
+
*
|
|
135
|
+
* @throws {Error} if nftables operation fails
|
|
70
136
|
*/
|
|
71
137
|
createTable(): Promise<void>;
|
|
72
138
|
|
|
73
139
|
/**
|
|
74
140
|
* Deletes both IPv4 and IPv6 tables.
|
|
75
141
|
* Idempotent — no error if tables don't exist.
|
|
142
|
+
*
|
|
143
|
+
* @throws {Error} if nftables operation fails
|
|
76
144
|
*/
|
|
77
145
|
deleteTable(): Promise<void>;
|
|
78
146
|
|
|
79
147
|
/**
|
|
80
148
|
* Adds an IP address to a set.
|
|
81
149
|
* Auto-detects IPv4 vs IPv6 and routes to the correct table/set.
|
|
150
|
+
* Works with both input sets (sets) and output sets (outSets).
|
|
82
151
|
*
|
|
83
|
-
* @param options - Address, target set, and optional timeout
|
|
84
|
-
* @throws {TypeError} if options
|
|
85
|
-
* @throws {Error} if IP is invalid or nftables operation fails
|
|
152
|
+
* @param options - Address, target set name, and optional timeout.
|
|
153
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
154
|
+
* @throws {Error} if IP is invalid, set name is unknown, or nftables operation fails
|
|
86
155
|
*/
|
|
87
156
|
addAddress(options: AddAddressOptions): Promise<void>;
|
|
88
157
|
|
|
89
158
|
/**
|
|
90
159
|
* Removes an IP address from a set.
|
|
91
160
|
* Idempotent — no error if IP is not in the set.
|
|
161
|
+
* Works with both input sets (sets) and output sets (outSets).
|
|
92
162
|
*
|
|
93
|
-
* @param options - Address and target set
|
|
94
|
-
* @throws {TypeError} if options
|
|
95
|
-
* @throws {Error} if IP is invalid or nftables operation fails
|
|
163
|
+
* @param options - Address and target set name.
|
|
164
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
165
|
+
* @throws {Error} if IP is invalid, set name is unknown, or nftables operation fails
|
|
96
166
|
*/
|
|
97
167
|
removeAddress(options: RemoveAddressOptions): Promise<void>;
|
|
98
168
|
|
|
99
169
|
/**
|
|
100
170
|
* Adds multiple IP addresses to a set in bulk.
|
|
101
|
-
* Addresses are chunked for efficient netlink communication.
|
|
102
171
|
* Empty arrays are a no-op.
|
|
103
172
|
*
|
|
104
|
-
* @param options -
|
|
105
|
-
* @throws {TypeError} if options
|
|
106
|
-
* @throws {Error} if any IP is invalid or nftables operation fails
|
|
173
|
+
* @param options - Array of addresses, target set name, and optional timeout.
|
|
174
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
175
|
+
* @throws {Error} if any IP is invalid, set name is unknown, or nftables operation fails
|
|
107
176
|
*/
|
|
108
177
|
addAddresses(options: AddAddressesOptions): Promise<void>;
|
|
109
178
|
|
|
@@ -112,9 +181,55 @@ export class NftManager {
|
|
|
112
181
|
* Idempotent — no error if IPs are not in the set.
|
|
113
182
|
* Empty arrays are a no-op.
|
|
114
183
|
*
|
|
115
|
-
* @param options -
|
|
116
|
-
* @throws {TypeError} if options
|
|
117
|
-
* @throws {Error} if any IP is invalid or nftables operation fails
|
|
184
|
+
* @param options - Array of addresses and target set name.
|
|
185
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
186
|
+
* @throws {Error} if any IP is invalid, set name is unknown, or nftables operation fails
|
|
118
187
|
*/
|
|
119
188
|
removeAddresses(options: RemoveAddressesOptions): Promise<void>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Adds a port to an output port set.
|
|
192
|
+
* Port is added to BOTH IPv4 and IPv6 tables (ports are family-independent).
|
|
193
|
+
* When protocol is omitted, two elements are added: tcp + udp.
|
|
194
|
+
* When protocol is 'tcp' or 'udp', one element with that protocol is added.
|
|
195
|
+
*
|
|
196
|
+
* @param options - Port number, target set name, and optional timeout.
|
|
197
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
198
|
+
* @throws {Error} if port is invalid, set name is unknown, or nftables operation fails
|
|
199
|
+
*/
|
|
200
|
+
addPort(options: AddPortOptions): Promise<void>;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Removes a port from an output port set.
|
|
204
|
+
* Idempotent — no error if port is not in the set.
|
|
205
|
+
* Port is removed from BOTH IPv4 and IPv6 tables.
|
|
206
|
+
*
|
|
207
|
+
* @param options - Port number and target set name.
|
|
208
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
209
|
+
* @throws {Error} if port is invalid, set name is unknown, or nftables operation fails
|
|
210
|
+
*/
|
|
211
|
+
removePort(options: RemovePortOptions): Promise<void>;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Adds multiple ports to an output port set in bulk.
|
|
215
|
+
* Ports are added to BOTH IPv4 and IPv6 tables.
|
|
216
|
+
* Empty arrays are a no-op.
|
|
217
|
+
*
|
|
218
|
+
* @param options - Array of port numbers, target set name, and optional timeout.
|
|
219
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
220
|
+
* @throws {Error} if any port is invalid, set name is unknown, or nftables operation fails
|
|
221
|
+
*/
|
|
222
|
+
addPorts(options: AddPortsOptions): Promise<void>;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Removes multiple ports from an output port set in bulk.
|
|
226
|
+
* Idempotent — no error if ports are not in the set.
|
|
227
|
+
* Ports are removed from BOTH IPv4 and IPv6 tables.
|
|
228
|
+
* Empty arrays are a no-op.
|
|
229
|
+
*
|
|
230
|
+
* @param options - Array of port numbers and target set name.
|
|
231
|
+
* @throws {TypeError} if options or fields have wrong types
|
|
232
|
+
* @throws {Error} if any port is invalid, set name is unknown, or nftables operation fails
|
|
233
|
+
*/
|
|
234
|
+
removePorts(options: RemovePortsOptions): Promise<void>;
|
|
120
235
|
}
|
package/lib/index.js
CHANGED
|
@@ -6,7 +6,7 @@ let binding;
|
|
|
6
6
|
try {
|
|
7
7
|
binding = require('node-gyp-build')(path.join(__dirname, '..'));
|
|
8
8
|
} catch (e) {
|
|
9
|
-
|
|
9
|
+
if (process.platform === 'linux') throw e;
|
|
10
10
|
binding = null;
|
|
11
11
|
}
|
|
12
12
|
|
|
@@ -16,7 +16,7 @@ if (binding) {
|
|
|
16
16
|
class NftManager {
|
|
17
17
|
constructor() {
|
|
18
18
|
throw new Error(
|
|
19
|
-
'
|
|
19
|
+
'nftables-napi only works on Linux with CAP_NET_ADMIN. Native binding failed to load.'
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nftables-napi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Native Node.js binding for nftables via libnftnl+libmnl — nftables firewall management",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kastov",
|
|
@@ -46,8 +46,7 @@
|
|
|
46
46
|
],
|
|
47
47
|
"gypfile": true,
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">=24.0.0"
|
|
50
|
-
"os": "linux"
|
|
49
|
+
"node": ">=24.0.0"
|
|
51
50
|
},
|
|
52
51
|
"scripts": {
|
|
53
52
|
"install": "node-gyp-build || true",
|
|
@@ -57,10 +56,10 @@
|
|
|
57
56
|
"prebuild:all": "./prebuild-all.sh"
|
|
58
57
|
},
|
|
59
58
|
"dependencies": {
|
|
60
|
-
"node-gyp-build": "^4.8.4"
|
|
61
|
-
"node-addon-api": "^8.5.0"
|
|
59
|
+
"node-gyp-build": "^4.8.4"
|
|
62
60
|
},
|
|
63
61
|
"devDependencies": {
|
|
62
|
+
"node-addon-api": "^8.5.0",
|
|
64
63
|
"node-gyp": "^12.2.0",
|
|
65
64
|
"prebuildify": "^6.0.1",
|
|
66
65
|
"@types/node": ">= 24 < 25"
|
|
Binary file
|
|
Binary file
|
package/src/netlink/constants.h
CHANGED
|
@@ -23,6 +23,10 @@ inline constexpr uint32_t IPV6_SRC_OFFSET = 8;
|
|
|
23
23
|
inline constexpr uint32_t IPV4_ADDR_LEN = 4;
|
|
24
24
|
inline constexpr uint32_t IPV6_ADDR_LEN = 16;
|
|
25
25
|
|
|
26
|
+
// Destination address offsets
|
|
27
|
+
inline constexpr uint32_t IPV4_DST_OFFSET = 16;
|
|
28
|
+
inline constexpr uint32_t IPV6_DST_OFFSET = 24;
|
|
29
|
+
|
|
26
30
|
// Chain priority
|
|
27
31
|
inline constexpr int32_t CHAIN_PRIORITY = -10;
|
|
28
32
|
|
|
@@ -36,4 +40,25 @@ inline constexpr uint32_t SEQ_BLOCK_SIZE = 256;
|
|
|
36
40
|
// Number of pages for default batch buffer (pagesize * 32 ≈ 128KB)
|
|
37
41
|
inline constexpr uint32_t DEFAULT_BUF_PAGES = 32;
|
|
38
42
|
|
|
43
|
+
// Output chain
|
|
44
|
+
inline constexpr const char* CHAIN_OUTPUT = "output";
|
|
45
|
+
|
|
46
|
+
// Transport header dport offset (bytes from transport header base)
|
|
47
|
+
inline constexpr uint32_t TRANSPORT_DPORT_OFFSET = 2;
|
|
48
|
+
inline constexpr uint32_t TRANSPORT_DPORT_LEN = 2;
|
|
49
|
+
|
|
50
|
+
// Concatenated type: concat_subtype_add(inet_proto, inet_service)
|
|
51
|
+
// nft CLI builds concat types with LAST subtype in low bits:
|
|
52
|
+
// concat_subtype_add(type, subtype) = type << TYPE_BITS | subtype
|
|
53
|
+
// For (inet_proto=12 . inet_service=13): 12 << 6 | 13 = 781
|
|
54
|
+
inline constexpr uint32_t DATATYPE_PROTO_SERVICE = (12 << 6 | 13);
|
|
55
|
+
inline constexpr uint32_t PROTO_SERVICE_KEY_LEN = 8;
|
|
56
|
+
|
|
57
|
+
// L4 protocol numbers
|
|
58
|
+
inline constexpr uint8_t PROTO_TCP = 6;
|
|
59
|
+
inline constexpr uint8_t PROTO_UDP = 17;
|
|
60
|
+
|
|
61
|
+
// Named counter
|
|
62
|
+
inline constexpr const char* COUNTER_NAME = "processed";
|
|
63
|
+
|
|
39
64
|
} // namespace nft
|
package/src/netlink/nft_config.h
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include <string>
|
|
4
|
+
#include <vector>
|
|
4
5
|
|
|
5
6
|
namespace nft {
|
|
6
7
|
|
|
7
|
-
enum class
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
enum class SetKind { InIP, OutIP, OutPort };
|
|
9
|
+
|
|
10
|
+
struct SetDef {
|
|
11
|
+
std::string name;
|
|
12
|
+
std::string name_v6;
|
|
13
|
+
std::string log_prefix; // non-empty for InIP only
|
|
14
|
+
SetKind kind;
|
|
10
15
|
};
|
|
11
16
|
|
|
12
17
|
struct NftConfig {
|
|
13
18
|
std::string table_v4;
|
|
14
19
|
std::string table_v6;
|
|
15
|
-
std::
|
|
16
|
-
std::string set_v6;
|
|
17
|
-
std::string drop_set_v4;
|
|
18
|
-
std::string drop_set_v6;
|
|
19
|
-
std::string blacklist_log_prefix;
|
|
20
|
-
std::string droplist_log_prefix;
|
|
20
|
+
std::vector<SetDef> sets; // all sets: InIP + OutIP + OutPort
|
|
21
21
|
|
|
22
22
|
static NftConfig from_names(const std::string& table_name,
|
|
23
|
-
const std::string
|
|
24
|
-
const std::string
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return
|
|
23
|
+
const std::vector<std::string>& in_sets,
|
|
24
|
+
const std::vector<std::string>& out_sets,
|
|
25
|
+
const std::vector<std::string>& out_port_sets) {
|
|
26
|
+
NftConfig cfg;
|
|
27
|
+
cfg.table_v4 = table_name;
|
|
28
|
+
cfg.table_v6 = table_name + "6";
|
|
29
|
+
cfg.sets.reserve(in_sets.size() + out_sets.size() + out_port_sets.size());
|
|
30
|
+
for (const auto& n : in_sets) {
|
|
31
|
+
cfg.sets.push_back({n, n + "6", n + ": ", SetKind::InIP});
|
|
32
|
+
}
|
|
33
|
+
for (const auto& n : out_sets) {
|
|
34
|
+
cfg.sets.push_back({n, n + "6", "", SetKind::OutIP});
|
|
35
|
+
}
|
|
36
|
+
for (const auto& n : out_port_sets) {
|
|
37
|
+
cfg.sets.push_back({n, n + "6", "", SetKind::OutPort});
|
|
38
|
+
}
|
|
39
|
+
return cfg;
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
42
|
|
package/src/netlink/nftnl_raii.h
CHANGED
|
@@ -24,6 +24,7 @@ extern "C" {
|
|
|
24
24
|
#include <libnftnl/chain.h>
|
|
25
25
|
#include <libnftnl/set.h>
|
|
26
26
|
#include <libnftnl/rule.h>
|
|
27
|
+
#include <libnftnl/object.h>
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
#include <memory>
|
|
@@ -37,7 +38,7 @@ struct NftnlTableDeleter {
|
|
|
37
38
|
};
|
|
38
39
|
using UniqueTable = std::unique_ptr<struct nftnl_table, NftnlTableDeleter>;
|
|
39
40
|
|
|
40
|
-
inline UniqueTable make_table() { return UniqueTable(nftnl_table_alloc()); }
|
|
41
|
+
[[nodiscard]] inline UniqueTable make_table() { return UniqueTable(nftnl_table_alloc()); }
|
|
41
42
|
|
|
42
43
|
// --- Chain -------------------------------------------------------------------
|
|
43
44
|
|
|
@@ -46,7 +47,7 @@ struct NftnlChainDeleter {
|
|
|
46
47
|
};
|
|
47
48
|
using UniqueChain = std::unique_ptr<struct nftnl_chain, NftnlChainDeleter>;
|
|
48
49
|
|
|
49
|
-
inline UniqueChain make_chain() { return UniqueChain(nftnl_chain_alloc()); }
|
|
50
|
+
[[nodiscard]] inline UniqueChain make_chain() { return UniqueChain(nftnl_chain_alloc()); }
|
|
50
51
|
|
|
51
52
|
// --- Set ---------------------------------------------------------------------
|
|
52
53
|
|
|
@@ -55,7 +56,7 @@ struct NftnlSetDeleter {
|
|
|
55
56
|
};
|
|
56
57
|
using UniqueSet = std::unique_ptr<struct nftnl_set, NftnlSetDeleter>;
|
|
57
58
|
|
|
58
|
-
inline UniqueSet make_set() { return UniqueSet(nftnl_set_alloc()); }
|
|
59
|
+
[[nodiscard]] inline UniqueSet make_set() { return UniqueSet(nftnl_set_alloc()); }
|
|
59
60
|
|
|
60
61
|
// --- Rule --------------------------------------------------------------------
|
|
61
62
|
|
|
@@ -64,6 +65,15 @@ struct NftnlRuleDeleter {
|
|
|
64
65
|
};
|
|
65
66
|
using UniqueRule = std::unique_ptr<struct nftnl_rule, NftnlRuleDeleter>;
|
|
66
67
|
|
|
67
|
-
inline UniqueRule make_rule() { return UniqueRule(nftnl_rule_alloc()); }
|
|
68
|
+
[[nodiscard]] inline UniqueRule make_rule() { return UniqueRule(nftnl_rule_alloc()); }
|
|
69
|
+
|
|
70
|
+
// --- Object (stateful counter/quota) ----------------------------------------
|
|
71
|
+
|
|
72
|
+
struct NftnlObjDeleter {
|
|
73
|
+
void operator()(struct nftnl_obj* o) const noexcept { nftnl_obj_free(o); }
|
|
74
|
+
};
|
|
75
|
+
using UniqueObj = std::unique_ptr<struct nftnl_obj, NftnlObjDeleter>;
|
|
76
|
+
|
|
77
|
+
[[nodiscard]] inline UniqueObj make_obj() { return UniqueObj(nftnl_obj_alloc()); }
|
|
68
78
|
|
|
69
79
|
} // namespace nft
|
package/src/netlink/nl_batch.h
CHANGED
|
@@ -24,10 +24,10 @@ public:
|
|
|
24
24
|
NlBatch(NlBatch&&) = delete;
|
|
25
25
|
NlBatch& operator=(NlBatch&&) = delete;
|
|
26
26
|
|
|
27
|
-
bool is_valid() const;
|
|
28
|
-
struct nlmsghdr* add_msg(uint16_t type, uint16_t family, uint16_t flags);
|
|
29
|
-
bool advance();
|
|
30
|
-
NlResult execute(NlSocket& sock, bool ignore_enoent = false);
|
|
27
|
+
[[nodiscard]] bool is_valid() const;
|
|
28
|
+
[[nodiscard]] struct nlmsghdr* add_msg(uint16_t type, uint16_t family, uint16_t flags);
|
|
29
|
+
[[nodiscard]] bool advance();
|
|
30
|
+
[[nodiscard]] NlResult execute(NlSocket& sock, bool ignore_enoent = false);
|
|
31
31
|
|
|
32
32
|
private:
|
|
33
33
|
std::unique_ptr<char[]> buf_;
|
package/src/netlink/nl_result.h
CHANGED
|
@@ -6,6 +6,7 @@ extern "C" {
|
|
|
6
6
|
|
|
7
7
|
#include <cerrno>
|
|
8
8
|
#include <cstring>
|
|
9
|
+
#include <sys/select.h>
|
|
9
10
|
#include <sys/socket.h>
|
|
10
11
|
|
|
11
12
|
static constexpr size_t RECV_BUF_SIZE = 16384;
|
|
@@ -84,31 +85,34 @@ NlResult NlSocket::send_batch(struct mnl_nlmsg_batch* batch, bool ignore_enoent)
|
|
|
84
85
|
|
|
85
86
|
BatchRecvCtx ctx{ignore_enoent, false, 0};
|
|
86
87
|
|
|
87
|
-
// Custom control callback array: override only NLMSG_ERROR (index 2).
|
|
88
|
-
// Array size = NLMSG_ERROR + 1 = 3, so NLMSG_DONE (3) and others
|
|
89
|
-
// fall through to default mnl handling.
|
|
90
88
|
mnl_cb_t cb_ctl[NLMSG_ERROR + 1] = {};
|
|
91
89
|
cb_ctl[NLMSG_ERROR] = batch_error_cb;
|
|
92
90
|
|
|
93
91
|
int fd = mnl_socket_get_fd(nl_);
|
|
94
92
|
char recv_buf[RECV_BUF_SIZE];
|
|
95
93
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
94
|
+
// Match nft CLI pattern: select() with zero timeout to check for data,
|
|
95
|
+
// then recv+process in a loop. Continue on errors to collect all ACKs.
|
|
96
|
+
fd_set readfds;
|
|
97
|
+
struct timeval tv;
|
|
98
|
+
|
|
99
|
+
for (;;) {
|
|
100
|
+
FD_ZERO(&readfds);
|
|
101
|
+
FD_SET(fd, &readfds);
|
|
102
|
+
tv = {0, 0};
|
|
103
|
+
|
|
104
|
+
int sel = select(fd + 1, &readfds, nullptr, nullptr, &tv);
|
|
105
|
+
if (sel <= 0)
|
|
104
106
|
break;
|
|
105
107
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
ssize_t ret = mnl_socket_recvfrom(nl_, recv_buf, sizeof(recv_buf));
|
|
109
|
+
if (ret < 0)
|
|
110
|
+
return {false, std::string("mnl_socket_recvfrom: ") + strerror(errno)};
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
mnl_cb_run2(recv_buf, static_cast<size_t>(ret), 0, portid_,
|
|
113
|
+
nullptr, &ctx, cb_ctl, NLMSG_ERROR + 1);
|
|
114
|
+
// Continue on error — collect all acknowledgments
|
|
115
|
+
}
|
|
112
116
|
|
|
113
117
|
if (ctx.has_error)
|
|
114
118
|
return {false, std::string("batch error: ") + strerror(ctx.error_code)};
|
package/src/netlink/nl_socket.h
CHANGED
|
@@ -18,9 +18,9 @@ public:
|
|
|
18
18
|
NlSocket(NlSocket&&) = delete;
|
|
19
19
|
NlSocket& operator=(NlSocket&&) = delete;
|
|
20
20
|
|
|
21
|
-
bool is_valid() const;
|
|
21
|
+
[[nodiscard]] bool is_valid() const;
|
|
22
22
|
|
|
23
|
-
NlResult send_batch(struct mnl_nlmsg_batch* batch, bool ignore_enoent = false);
|
|
23
|
+
[[nodiscard]] NlResult send_batch(struct mnl_nlmsg_batch* batch, bool ignore_enoent = false);
|
|
24
24
|
|
|
25
25
|
private:
|
|
26
26
|
struct mnl_socket* nl_;
|
package/src/netlink/operation.h
CHANGED