f-86: agent: dummy, DNAT et open par VPC, DHCP 6 et 42, watchdog #86

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-25 20:45:24 +01:00
commit 27dda545cf
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
16 changed files with 565 additions and 13 deletions

View file

@ -0,0 +1,60 @@
package iptables
import (
"errors"
"fmt"
"os/exec"
)
const HostSvcChain = "TWO-HOSTSVC"
var hostSvcExcluded = []string{"br-public", "l3-+"}
func hostSvcJump() []string {
return []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", HostSvcChain}
}
func hostSvcRules(target string) [][]string {
var rules [][]string
for _, iface := range hostSvcExcluded {
rules = append(rules, []string{HostSvcChain, "-i", iface, "-j", "RETURN"})
}
for _, p := range []struct{ proto, port string }{{"udp", "53"}, {"tcp", "53"}, {"udp", "123"}} {
rules = append(rules, []string{HostSvcChain,
"-p", p.proto, "-m", p.proto, "--dport", p.port,
"-j", "DNAT", "--to-destination", target,
})
}
return rules
}
func nat(args ...string) *exec.Cmd {
return exec.Command("iptables", append([]string{"-t", "nat"}, args...)...)
}
func AddHostSvcRedirect(target string) error {
if out, err := nat("-N", HostSvcChain).CombinedOutput(); err != nil {
return fmt.Errorf("iptables create chain %s: %w: %s", HostSvcChain, err, out)
}
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
if out, err := nat(append([]string{"-A"}, rule...)...).CombinedOutput(); err != nil {
return fmt.Errorf("iptables hostsvc redirect %v: %w: %s", rule, err, out)
}
}
return nil
}
func HasHostSvcRedirect(target string) (bool, error) {
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
err := nat(append([]string{"-C"}, rule...)...).Run()
var exit *exec.ExitError
switch {
case err == nil:
case errors.As(err, &exit) && exit.ExitCode() == 1:
return false, nil
default:
return false, fmt.Errorf("iptables check %v: %w", rule, err)
}
}
return true, nil
}

View file

@ -0,0 +1,48 @@
package iptables
import (
"reflect"
"strings"
"testing"
)
func TestHostSvcJump_OnlyLocalDestinations(t *testing.T) {
want := []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "TWO-HOSTSVC"}
if got := hostSvcJump(); !reflect.DeepEqual(got, want) {
t.Errorf("jump = %v, want %v: traffic to an outside resolver must not be hijacked", got, want)
}
}
func TestHostSvcRules_ExclusionsComeFirst(t *testing.T) {
rules := hostSvcRules("169.254.169.253")
if len(rules) != 5 {
t.Fatalf("%d rules, want 2 exclusions and 3 redirections: %v", len(rules), rules)
}
for i, iface := range []string{"br-public", "l3-+"} {
want := []string{"TWO-HOSTSVC", "-i", iface, "-j", "RETURN"}
if !reflect.DeepEqual(rules[i], want) {
t.Errorf("rule %d = %v, want %v: the public segment and the vrf link must never reach the relay", i, rules[i], want)
}
}
}
func TestHostSvcRules_RedirectDNSAndNTPOnly(t *testing.T) {
var got []string
for _, rule := range hostSvcRules("169.254.169.253") {
if rule[len(rule)-3] != "DNAT" {
continue
}
if rule[len(rule)-1] != "169.254.169.253" {
t.Errorf("rule %v does not target 169.254.169.253", rule)
}
got = append(got, strings.Join(rule[1:7], " "))
}
want := []string{
"-p udp -m udp --dport 53",
"-p tcp -m tcp --dport 53",
"-p udp -m udp --dport 123",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("redirections = %v, want %v", got, want)
}
}