1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! iptables rule management for DNAT, SNAT, hairpin, and Docker mappings.
//!
//! All rules are installed in the `NATMAP` chain (a sub-chain of `PREROUTING`
//! in the `nat` table and `DOCKER-USER` in the `filter` table). This keeps
//! natmap rules separate from Docker's own rules and ensures clean crash
//! recovery via chain flush.
use std::ffi::OsStr;
use std::process::Command;
use color_eyre::Result;
use color_eyre::eyre::bail;
use crate::models::DnatConfig;
use crate::models::DockerPortMap;
use crate::models::HairpinConfig;
use crate::models::SnatConfig;
const NATMAP: &str = "NATMAP";
/// Manages the lifecycle of iptables rules used by natmap.
///
/// Creates the `NATMAP` chain in both the `nat` and `filter` tables,
/// inserts jumps from `PREROUTING` and `DOCKER-USER`, and provides
/// methods to install/remove individual rules.
pub struct IptablesManager;
impl Default for IptablesManager {
fn default() -> Self {
Self::new()
}
}
impl IptablesManager {
/// Creates a new [`IptablesManager`].
pub fn new() -> Self {
Self
}
/// Creates the `NATMAP` chains and inserts jump rules.
///
/// Operates on both `iptables` (IPv4) and `ip6tables` (IPv6).
/// This method is idempotent.
pub fn setup(&self) -> Result<()> {
tracing::info!("setting up iptables chains and jumps");
for &cmd in &["iptables", "ip6tables"] {
// Verify DOCKER-USER exists (it should, Docker makes it). Create if missing.
if !self.chain_exists(cmd, "filter", "DOCKER-USER") {
// create new DOCKER-USER chain
self.run_success(cmd, ["-t", "filter", "-N", "DOCKER-USER"])?;
// insert a jump rule on first position of FORWARD chain to DOCKER-USER
self.run_success(cmd, ["-t", "filter", "-I", "FORWARD", "-j", "DOCKER-USER"])?;
}
// Create NATMAP subchain in nat table (DNAT rules live here)
if !self.chain_exists(cmd, "nat", NATMAP) {
self.run_success(cmd, ["-t", "nat", "-N", NATMAP])?;
}
// Create NATMAP subchain in filter table (FORWARD ACCEPT rules live here)
if !self.chain_exists(cmd, "filter", NATMAP) {
self.run_success(cmd, ["-t", "filter", "-N", NATMAP])?;
}
// Jump from DOCKER-USER to NATMAP in filter table (if not exists)
if !self.rule_exists(cmd, &["-t", "filter", "-C", "DOCKER-USER", "-j", NATMAP]) {
self.run(cmd, ["-t", "filter", "-I", "DOCKER-USER", "-j", NATMAP])?;
}
// Jump from PREROUTING to NATMAP in nat table (if not exists)
if !self.rule_exists(cmd, &["-t", "nat", "-C", "PREROUTING", "-j", NATMAP]) {
self.run_success(cmd, ["-t", "nat", "-I", "PREROUTING", "-j", NATMAP])?;
}
}
Ok(())
}
/// Installs DNAT, FORWARD ACCEPT, MASQUERADE, and OUTPUT DNAT rules for a Docker mapping.
pub fn install_dockermap(&self, map: &DockerPortMap) -> Result<()> {
tracing::debug!(mapping = ?map, "installing mapping");
let cmd = self.cmd_for(map.request.is_ipv6());
let req = &map.request;
let host_ip = req.host_addr.ip();
let host_port = req.host_addr.port().to_string();
let container_addr = req.container_addr.to_string();
let container_ip = req.container_addr.ip().to_string();
let container_port = req.container_addr.port().to_string();
let proto = req.proto.to_string();
let comment = &map.rule_comment;
// 1. DNAT rule via nat NATMAP
self.run(
cmd,
[
"-t",
"nat",
"-A",
NATMAP,
"-p",
&proto,
"--dport",
&host_port,
"-j",
"DNAT",
"--to-destination",
&container_addr,
"-m",
"comment",
"--comment",
comment,
],
)?;
// 2. FORWARD ACCEPT rule in filter NATMAP
self.run(
cmd,
[
"-t",
"filter",
"-A",
NATMAP,
"-d",
&container_ip,
"-p",
&proto,
"--dport",
&container_port,
"-j",
"ACCEPT",
"-m",
"comment",
"--comment",
comment,
],
)?;
// 3. Masquerade (hairpin NAT)
self.run(
cmd,
[
"-t",
"nat",
"-A",
"POSTROUTING",
"-s",
&container_ip,
"-d",
&container_ip,
"-p",
&proto,
"--dport",
&container_port,
"-j",
"MASQUERADE",
"-m",
"comment",
"--comment",
comment,
],
)?;
// 4. OUTPUT DNAT rule — always needed for locally-generated traffic.
// PREROUTING only catches forwarded/ingress traffic; locally-generated
// packets (curl localhost, curl <host-ip>) go through OUTPUT.
let output_dst = if host_ip.is_unspecified() {
if map.request.is_ipv6() {
"::1"
} else {
"127.0.0.1"
}
} else {
&map.request.host_addr.ip().to_string()
};
self.run(
cmd,
[
"-t",
"nat",
"-A",
"OUTPUT",
"-d",
output_dst,
"-p",
&proto,
"--dport",
&host_port,
"-j",
"DNAT",
"--to-destination",
&container_addr,
"-m",
"comment",
"--comment",
comment,
],
)?;
Ok(())
}
/// Flushes and deletes the `NATMAP` chains and removes all natmap-commented
/// rules from `POSTROUTING`, `OUTPUT`, `PREROUTING`, and `FORWARD` in both
/// `iptables` (IPv4) and `ip6tables` (IPv6).
///
/// Used during crash recovery and clean shutdown to reset all natmap-managed rules.
pub fn flush_all_natmap(&self) -> Result<()> {
tracing::info!("flushing all NATMAP iptables rules");
for &cmd in &["iptables", "ip6tables"] {
let _ = self.flush_chain(cmd, "nat", NATMAP);
let _ = self.flush_chain(cmd, "filter", NATMAP);
let _ = self.delete_all_natmap(cmd, "nat", "POSTROUTING");
let _ = self.delete_all_natmap(cmd, "nat", "OUTPUT");
let _ = self.delete_all_natmap(cmd, "nat", "PREROUTING");
let _ = self.delete_all_natmap(cmd, "filter", "FORWARD");
}
Ok(())
}
/// Installs a static DNAT rule (PREROUTING + FORWARD ACCEPT).
pub fn install_dnat(&self, config: &DnatConfig) -> Result<()> {
let comment = config.rule_comment();
let multiport = config.ports.contains(',');
let port_args = if multiport {
vec!["-m", "multiport", "--dports", &config.ports]
} else {
vec!["--dport", &config.ports]
};
let mut pre_args = vec!["-t", "nat", "-A", "PREROUTING"];
if let Some(ref iface) = config.ext_if {
pre_args.extend(vec!["-i", iface]);
}
let proto = config.proto.to_lowercase();
pre_args.extend(vec!["-d", &config.ext_ip, "-p", proto]);
pre_args.extend(port_args.clone());
let dest = if multiport {
config.int_ip.clone()
} else {
format!("{}:{}", config.int_ip, config.ports)
};
pre_args.extend(vec!["-j", "DNAT", "--to-destination", &dest]);
pre_args.extend(vec!["-m", "comment", "--comment", &comment]);
self.run_success("iptables", &pre_args)?;
let mut fwd_args = vec!["-A", "FORWARD", "-p", proto, "-d", &config.int_ip];
fwd_args.extend(port_args);
fwd_args.extend(vec!["-j", "ACCEPT"]);
fwd_args.extend(vec!["-m", "comment", "--comment", &comment]);
self.run_success("iptables", &fwd_args)?;
Ok(())
}
/// Removes a static DNAT rule (PREROUTING + FORWARD ACCEPT).
///
/// Uses the rule comment to find and delete matching rules.
pub fn remove_dnat(&self, config: &DnatConfig) -> Result<()> {
let comment = config.rule_comment();
self.delete_all_matching("iptables", "nat", "PREROUTING", &comment)?;
self.delete_all_matching("iptables", "filter", "FORWARD", &comment)?;
Ok(())
}
/// Installs a static SNAT (source NAT) rule in the POSTROUTING chain.
pub fn install_snat(&self, config: &SnatConfig) -> Result<()> {
let comment = config.rule_comment();
let args = vec![
"-t",
"nat",
"-A",
"POSTROUTING",
"-s",
&config.int_ip,
"-o",
&config.ext_if,
"-j",
"SNAT",
"--to-source",
&config.ext_ip,
"-m",
"comment",
"--comment",
&comment,
];
self.run_success("iptables", &args)?;
Ok(())
}
/// Removes a static SNAT rule from the POSTROUTING chain.
///
/// Uses the rule comment to find and delete matching rules.
pub fn remove_snat(&self, config: &SnatConfig) -> Result<()> {
let comment = config.rule_comment();
self.delete_all_matching("iptables", "nat", "POSTROUTING", &comment)?;
Ok(())
}
/// Installs a hairpin NAT rule (PREROUTING DNAT + POSTROUTING MASQUERADE).
pub fn install_hairpin(&self, config: &HairpinConfig) -> Result<()> {
let comment = config.rule_comment();
let multiport = config.ports.contains(',');
let port_args: Vec<&str> = if multiport {
vec!["-m", "multiport", "--dports", &config.ports]
} else {
vec!["--dport", &config.ports]
};
let proto = config.proto.to_lowercase();
let mut pre_args = vec![
"-t",
"nat",
"-A",
"PREROUTING",
"-s",
&config.int_ip,
"-d",
&config.ext_ip,
"-p",
proto,
];
pre_args.extend(port_args.clone());
pre_args.extend(vec!["-j", "DNAT", "--to-destination", &config.int_ip]);
pre_args.extend(vec!["-m", "comment", "--comment", &comment]);
self.run_success("iptables", &pre_args)?;
let mut post_args = vec![
"-t",
"nat",
"-A",
"POSTROUTING",
"-s",
"0.0.0.0/0",
"-d",
&config.int_ip,
"-p",
proto,
];
post_args.extend(port_args);
post_args.extend(vec!["-j", "MASQUERADE"]);
post_args.extend(vec!["-m", "comment", "--comment", &comment]);
self.run_success("iptables", &post_args)?;
Ok(())
}
/// Removes a hairpin NAT rule (PREROUTING DNAT + POSTROUTING MASQUERADE).
///
/// Uses the rule comment to find and delete matching rules.
pub fn remove_hairpin(&self, config: &HairpinConfig) -> Result<()> {
let comment = config.rule_comment();
self.delete_all_matching("iptables", "nat", "PREROUTING", &comment)?;
self.delete_all_matching("iptables", "nat", "POSTROUTING", &comment)?;
Ok(())
}
/// Deletes all rules in a chain whose comment starts with "natmap:".
fn delete_all_natmap(&self, cmd: &str, table: &str, chain: &str) -> Result<()> {
loop {
let rules = self.get_rules(cmd, table, chain)?;
let mut deleted = false;
for (line_num, rule) in rules.iter().enumerate() {
if rule.contains("--comment \"natmap:") || rule.contains("--comment natmap:") {
let num = (line_num + 1).to_string();
self.run(cmd, ["-t", table, "-D", chain, &num])?;
deleted = true;
break;
}
}
if !deleted {
break;
}
}
Ok(())
}
/// Removes all iptables rules associated with a Docker mapping by its rule comment.
pub fn remove_mapping(&self, map: &DockerPortMap) -> Result<()> {
tracing::debug!(mapping = ?map, "removing mapping");
self.remove_by_comment(&map.rule_comment, map.request.is_ipv6())?;
Ok(())
}
/// Deletes rules matching the comment string across all relevant tables and chains.
fn remove_by_comment(&self, comment: &str, is_ipv6: bool) -> Result<()> {
let cmd = self.cmd_for(is_ipv6);
// Delete from NATMAP in nat table
self.delete_all_matching(cmd, "nat", NATMAP, comment)?;
// Delete from NATMAP in filter table
self.delete_all_matching(cmd, "filter", NATMAP, comment)?;
// Delete from POSTROUTING in nat table
self.delete_all_matching(cmd, "nat", "POSTROUTING", comment)?;
// Delete from OUTPUT in nat table (localhost DNAT)
self.delete_all_matching(cmd, "nat", "OUTPUT", comment)?;
Ok(())
}
/// Flushes and deletes a specific chain in a given table.
fn flush_chain(&self, cmd: &str, table: &str, chain: &str) -> Result<()> {
// flush chain
let _ = self.run(cmd, ["-t", table, "-F", chain]);
// delete chain
let _ = self.run(cmd, ["-t", table, "-X", chain]);
Ok(())
}
// --- Helper functions ---
/// Returns `"ip6tables"` or `"iptables"` based on address family.
fn cmd_for(&self, is_ipv6: bool) -> &'static str {
if is_ipv6 { "ip6tables" } else { "iptables" }
}
/// Runs a command. Fails and logs an error if the command returned a non-zero exit status.
fn run_success(
&self,
program: impl AsRef<OsStr>,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
) -> Result<std::process::Output> {
let args: Vec<_> = args.into_iter().collect();
let out = self.run(&program, &args)?;
if out.status.success() {
Ok(out)
} else {
let err = String::from_utf8_lossy(&out.stderr);
let args_str = args
.iter()
.map(|a| a.as_ref().to_string_lossy())
.collect::<Vec<_>>()
.join(" ");
let program = program.as_ref().to_string_lossy();
tracing::error!(program = %program, args = %args_str, error = %err, "command failed");
bail!("{program} failed: {err}");
}
}
/// Runs a command.
fn run(
&self,
program: impl AsRef<OsStr>,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
) -> Result<std::process::Output> {
let args_vec: Vec<_> = args.into_iter().map(|a| a.as_ref().to_owned()).collect();
let args_str = args_vec
.iter()
.map(|a| a.to_string_lossy())
.collect::<Vec<_>>()
.join(" ");
tracing::trace!(command = %program.as_ref().to_string_lossy(), args = %args_str, "raw iptables command");
Ok(Command::new(program.as_ref()).args(&args_vec).output()?)
}
/// Checks whether a chain exists in the given table.
fn chain_exists(&self, cmd: &str, table: &str, chain: &str) -> bool {
self.run(cmd, ["-t", table, "-L", chain, "-n"])
.map(|o| o.status.success())
.unwrap_or(false)
}
/// Checks whether a specific iptables rule already exists.
fn rule_exists(&self, cmd: &str, args: &[&str]) -> bool {
// cmd_success logs on fail
self.run(cmd, args)
.map(|o| o.status.success())
.unwrap_or(false)
}
/// Deletes all rules in a chain whose comment matches the given string.
fn delete_all_matching(
&self,
cmd: &str,
table: &str,
chain: &str,
comment: &str,
) -> Result<()> {
// Rules and delete by line numbers.
loop {
let rules = self.get_rules(cmd, table, chain)?;
let mut deleted = false;
for (line_num, rule) in rules.iter().enumerate() {
if rule.contains(&format!("--comment \"{comment}\""))
|| rule.contains(&format!("--comment {comment}"))
{
// Delete by line number from bottom up (or just one by one)
let num = (line_num + 1).to_string();
self.run_success(cmd, ["-t", table, "-D", chain, &num])?;
deleted = true;
break; // Start over since line numbers changed
}
}
if !deleted {
break;
}
}
Ok(())
}
/// Returns the list of active rules in a chain (lines starting with `-A` or `-I`).
fn get_rules(&self, cmd: &str, table: &str, chain: &str) -> Result<Vec<String>> {
// -S -- short for --list-rules
let out = self.run(cmd, ["-t", table, "-S", chain])?;
// Get only -A (append) and -I (insert)
// Ignore others, such as chain declarations
let rules = String::from_utf8_lossy(&out.stdout)
.lines()
.filter(|l| l.starts_with("-A ") || l.starts_with("-I "))
.map(|l| l.to_string())
.collect();
Ok(rules)
}
}