f-75: ebtables: confinement de interface_ip, ARP et DHCP en FORWARD et OUTPUT, retrait des seules règles présentes #75

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-23 00:36:09 +02:00
commit 8659aae7c6
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 89 additions and 0 deletions

View file

@ -1,8 +1,10 @@
package ebtables
import (
"errors"
"fmt"
"os/exec"
"strings"
)
func addRule(args ...string) error {
@ -89,3 +91,63 @@ func DeleteConfineMAC(iface, mac string) error {
}
return first
}
func gatewayRules(iface, ip string) [][]string {
dhcp := []string{"-p", "IPv4", "--ip-protocol", "udp", "--ip-source-port", "67:68", "--ip-destination-port", "67:68", "--ip-source", ip, "-j", "DROP"}
return [][]string{
{"-t", "filter", "FORWARD", "--out-interface", iface, "-p", "arp", "--arp-op", "Request", "--arp-ip-dst", ip, "-j", "DROP"},
{"-t", "filter", "OUTPUT", "--out-interface", iface, "-p", "arp", "--arp-ip-src", ip, "-j", "DROP"},
append([]string{"-t", "filter", "FORWARD", "--out-interface", iface}, dhcp...),
append([]string{"-t", "filter", "OUTPUT", "--out-interface", iface}, dhcp...),
}
}
func ruleExists(rule []string) (bool, error) {
err := exec.Command("ebtables", withAction("--check", rule)...).Run()
var exit *exec.ExitError
switch {
case err == nil:
return true, nil
case errors.As(err, &exit) && exit.ExitCode() == 1:
return false, nil
default:
return false, fmt.Errorf("ebtables --check %s: %w", strings.Join(rule, " "), err)
}
}
func ConfineGateway(iface, ip string) error {
for _, rule := range gatewayRules(iface, ip) {
if err := exec.Command("ebtables", withAction("-A", rule)...).Run(); err != nil {
return fmt.Errorf("ebtables confine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
}
}
return nil
}
func DeleteConfineGateway(iface, ip string) error {
var first error
for _, rule := range gatewayRules(iface, ip) {
exists, err := ruleExists(rule)
if err == nil && exists {
err = exec.Command("ebtables", withAction("-D", rule)...).Run()
}
if err != nil && first == nil {
first = fmt.Errorf("ebtables unconfine gateway %s on %s (%s): %w", ip, iface, strings.Join(rule, " "), err)
}
}
return first
}
func MissingGatewayRules(iface, ip string) ([]string, error) {
var missing []string
for _, rule := range gatewayRules(iface, ip) {
exists, err := ruleExists(rule)
if err != nil {
return nil, err
}
if !exists {
missing = append(missing, strings.Join(rule, " "))
}
}
return missing, nil
}