APmanageur Wi-Fi V1.0

This commit is contained in:
yoannmultigaming committed 2026-11-18 03:39:29 +01:00
1 parent f291ef2df8
commit e54d1b2a7b
2 files changed
+91 -46

No files matched your search

+88 -46
View File
@@ -6714,17 +6714,25 @@ def get_all_mac_to_ips():
_last_broute_log_sig = None
def _cleanup_ebtables_vpn():
"""Nettoie proprement nos règles ebtables (chaîne dédiée AP_VPN_NAT en table nat + BROUTING en table broute)."""
for eb in [b for b in (find_bin('ebtables'), find_bin('ebtables-legacy')) if b]:
if os.path.exists('/sys/module/ebtable_broute'):
subprocess.run([eb, '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
while subprocess.run([eb, '-t', 'nat', '-D', 'PREROUTING', '-j', 'AP_VPN_NAT'], capture_output=True).returncode == 0:
pass
subprocess.run([eb, '-t', 'nat', '-F', 'AP_VPN_NAT'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-X', 'AP_VPN_NAT'], capture_output=True)
def setup_nftables_broute(assigned_macs, local_subnets=None):
"""Active le brouting L2 (mode Pont uniquement) via nftables (meta broute) avec repli automatique sur ebtables si le noyau n'inclut pas nft_meta_bridge."""
"""Active le détournement L2->L3 (mode Pont uniquement) via nftables (meta broute ou ether daddr + pkttype host) avec repli automatique sur ebtables -t nat redirect."""
global _last_broute_log_sig
nft_bin = find_bin('nft')
if not assigned_macs or not is_bridge_mode():
# Aucun client assigné à un VPN ou mode AP (pas de pont) : supprimer les tables broute pour ne jamais interférer avec l'hôte
# Aucun client assigné à un VPN ou mode AP (pas de pont) : supprimer les tables pour ne jamais interférer avec l'hôte
if nft_bin:
subprocess.run([nft_bin, 'delete', 'table', 'bridge', 'ap_vpn_broute'], capture_output=True)
for eb in [b for b in (find_bin('ebtables'), find_bin('ebtables-legacy')) if b]:
if os.path.exists('/sys/module/ebtable_broute') or eb.endswith('ebtables'):
subprocess.run([eb, '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
_cleanup_ebtables_vpn()
_last_broute_log_sig = None
return True
@@ -6758,7 +6766,7 @@ def setup_nftables_broute(assigned_macs, local_subnets=None):
r_ch = subprocess.run([nft_bin, 'add', 'chain', 'bridge', 'ap_vpn_broute', 'prerouting', '{ type filter hook prerouting priority -300; policy accept; }'], capture_output=True, text=True)
if r_ch.returncode == 0:
# 1. Exemptions ABSOLUES du brouting :
# 1. Exemptions ABSOLUES du détournement L2 :
# DHCP (ports 67/68 UDP) et Broadcast (ff:ff:ff:ff:ff:ff et 255.255.255.255).
# Ces paquets DOIVENT être bridgés en L2 pour que les clients reçoivent leur IP locale de la box !
subprocess.run([nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting', 'ether', 'daddr', 'ff:ff:ff:ff:ff:ff', 'accept'], capture_output=True)
@@ -6772,28 +6780,28 @@ def setup_nftables_broute(assigned_macs, local_subnets=None):
if not clean_mac: continue
if mac.strip().upper() in blocked_macs:
# Client bloqué : DROP absolu, pas de broute VPN !
# Client bloqué : DROP absolu, pas de routage VPN !
subprocess.run([nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting',
'ether', 'saddr', clean_mac, 'drop'], capture_output=True)
continue
# Essai 1 : meta broute set 1 + meta pkttype set host
cmd_v4_a = [nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting',
# Essai 1 : meta broute set 1 + meta pkttype set host (noyaux avec CONFIG_NFT_BRIDGE_META)
cmd_v4_1 = [nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting',
'ether', 'saddr', clean_mac, 'ether', 'type', 'ip',
'meta', 'broute', 'set', '1', 'meta', 'pkttype', 'set', 'host', 'accept']
res = subprocess.run(cmd_v4_a, capture_output=True, text=True)
res = subprocess.run(cmd_v4_1, capture_output=True, text=True)
if res.returncode == 0:
nft_mode = 'nftables (meta broute + pkttype host)'
else:
# Essai 2 : réécriture ether daddr vers MAC du pont + meta broute set 1 (noyaux où meta pkttype set host n'est pas supporté en bridge)
# Essai 2 : réécriture ether daddr vers MAC du pont + meta pkttype set host (sans meta broute, compatible Raspberry Pi OS sans CONFIG_NFT_BRIDGE_META)
res2 = None
if br_mac:
cmd_v4_b = [nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting',
cmd_v4_2 = [nft_bin, 'add', 'rule', 'bridge', 'ap_vpn_broute', 'prerouting',
'ether', 'saddr', clean_mac, 'ether', 'type', 'ip',
'ether', 'daddr', 'set', br_mac, 'meta', 'broute', 'set', '1', 'accept']
res2 = subprocess.run(cmd_v4_b, capture_output=True, text=True)
'ether', 'daddr', 'set', br_mac, 'meta', 'pkttype', 'set', 'host', 'accept']
res2 = subprocess.run(cmd_v4_2, capture_output=True, text=True)
if res2 is not None and res2.returncode == 0:
nft_mode = f'nftables (ether daddr {br_mac} + meta broute)'
nft_mode = f'nftables (ether daddr {br_mac} + pkttype host)'
else:
nft_ok = False
break
@@ -6804,22 +6812,18 @@ def setup_nftables_broute(assigned_macs, local_subnets=None):
subprocess.run(cmd_v6, capture_output=True)
if not nft_ok:
# Noyau sans support nft_meta_bridge : supprimer la table ap_vpn_broute et basculer proprement sur ebtables
subprocess.run([nft_bin, 'delete', 'table', 'bridge', 'ap_vpn_broute'], capture_output=True)
if nft_ok:
# Nettoyer ebtables pour éviter les doublons si nftables fonctionne
for eb in [b for b in (find_bin('ebtables'), find_bin('ebtables-legacy')) if b]:
if os.path.exists('/sys/module/ebtable_broute') or eb.endswith('ebtables'):
subprocess.run([eb, '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
_cleanup_ebtables_vpn()
sig = ('nft', nft_mode, tuple(sorted(m.strip().lower() for m in assigned_macs if m.strip())), tuple(sorted(blocked_macs)))
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] [NFTABLES] Broute actif via {nft_mode} pour {len(assigned_macs)} client(s).", flush=True)
print(f"[ap-vpn] [NFTABLES] Détournement L2->L3 actif via {nft_mode} pour {len(assigned_macs)} client(s).", flush=True)
return True
# Repli natif ebtables (-t broute -j redirect --redirect-target DROP) : fonctionne sur Raspberry Pi OS même sans CONFIG_NFT_BRIDGE_META
for mod in ['ebtable_broute', 'ebt_redirect']:
# Essai 3 : ebtables -t nat PREROUTING -j redirect --redirect-target ACCEPT (valide sous ebtables-nft v1.8.11 ET ebtables-legacy, sans table broute)
for mod in ['ebtable_nat', 'ebt_redirect']:
subprocess.run(['modprobe', mod], capture_output=True)
eb_candidates = []
@@ -6828,40 +6832,79 @@ def setup_nftables_broute(assigned_macs, local_subnets=None):
if b and b not in eb_candidates:
eb_candidates.append(b)
last_err = ''
for eb in eb_candidates:
r_fl = subprocess.run([eb, '-t', 'broute', '-F', 'BROUTING'], capture_output=True, text=True)
while subprocess.run([eb, '-t', 'nat', '-D', 'PREROUTING', '-j', 'AP_VPN_NAT'], capture_output=True).returncode == 0:
pass
subprocess.run([eb, '-t', 'nat', '-N', 'AP_VPN_NAT', '-P', 'RETURN'], capture_output=True)
r_fl = subprocess.run([eb, '-t', 'nat', '-F', 'AP_VPN_NAT'], capture_output=True, text=True)
if r_fl.returncode != 0:
last_err = (r_fl.stderr or '').strip()
continue
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-d', 'Broadcast', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67:68', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-sport', '67:68', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-d', 'Broadcast', '-j', 'RETURN'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67:68', '-j', 'RETURN'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-sport', '67:68', '-j', 'RETURN'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'RETURN'], capture_output=True)
eb_ok = True
eb_err = ''
for mac in assigned_macs:
clean_mac = mac.strip().lower()
if not clean_mac: continue
if mac.strip().upper() in blocked_macs:
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
continue
r_v4 = subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '-s', clean_mac, '-j', 'redirect', '--redirect-target', 'DROP'], capture_output=True, text=True)
r_v4 = subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-p', 'IPv4', '-s', clean_mac, '-j', 'redirect', '--redirect-target', 'ACCEPT'], capture_output=True, text=True)
if r_v4.returncode != 0:
eb_ok = False
eb_err = (r_v4.stderr or '').strip()
last_err = (r_v4.stderr or '').strip()
break
subprocess.run([eb, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv6', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-A', 'AP_VPN_NAT', '-p', 'IPv6', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
if eb_ok:
sig = ('ebtables', os.path.basename(eb), tuple(sorted(m.strip().lower() for m in assigned_macs if m.strip())), tuple(sorted(blocked_macs)))
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] [EBTABLES] Broute actif ({os.path.basename(eb)} redirect->host) pour {len(assigned_macs)} client(s).", flush=True)
return True
else:
subprocess.run([eb, '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
sig = ('err', os.path.basename(eb), eb_err)
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] Avertissement broute ({os.path.basename(eb)}) : {eb_err}", flush=True)
r_jmp = subprocess.run([eb, '-t', 'nat', '-I', 'PREROUTING', '1', '-j', 'AP_VPN_NAT'], capture_output=True, text=True)
if r_jmp.returncode == 0:
sig = ('ebtables-nat', os.path.basename(eb), tuple(sorted(m.strip().lower() for m in assigned_macs if m.strip())), tuple(sorted(blocked_macs)))
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] [EBTABLES-NAT] Détournement L2->L3 actif ({os.path.basename(eb)} -t nat redirect) pour {len(assigned_macs)} client(s).", flush=True)
return True
last_err = (r_jmp.stderr or '').strip()
subprocess.run([eb, '-t', 'nat', '-F', 'AP_VPN_NAT'], capture_output=True)
subprocess.run([eb, '-t', 'nat', '-X', 'AP_VPN_NAT'], capture_output=True)
# Essai 4 : ebtables-legacy -t broute (ultime repli si uniquement le moteur legacy est présent)
eb_leg = find_bin('ebtables-legacy')
if eb_leg:
subprocess.run(['modprobe', 'ebtable_broute'], capture_output=True)
r_fl = subprocess.run([eb_leg, '-t', 'broute', '-F', 'BROUTING'], capture_output=True, text=True)
if r_fl.returncode == 0:
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-d', 'Broadcast', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67:68', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-sport', '67:68', '-j', 'ACCEPT'], capture_output=True)
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '--ip-dst', '255.255.255.255', '-j', 'ACCEPT'], capture_output=True)
leg_ok = True
for mac in assigned_macs:
clean_mac = mac.strip().lower()
if not clean_mac: continue
if mac.strip().upper() in blocked_macs:
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
continue
r_v4 = subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv4', '-s', clean_mac, '-j', 'redirect', '--redirect-target', 'DROP'], capture_output=True, text=True)
if r_v4.returncode != 0:
leg_ok = False
last_err = (r_v4.stderr or '').strip()
break
subprocess.run([eb_leg, '-t', 'broute', '-A', 'BROUTING', '-p', 'IPv6', '-s', clean_mac, '-j', 'DROP'], capture_output=True)
if leg_ok:
sig = ('ebtables-legacy', 'broute', tuple(sorted(m.strip().lower() for m in assigned_macs if m.strip())), tuple(sorted(blocked_macs)))
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] [EBTABLES-LEGACY] Broute actif pour {len(assigned_macs)} client(s).", flush=True)
return True
subprocess.run([eb_leg, '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
sig = ('err', last_err)
if sig != _last_broute_log_sig:
_last_broute_log_sig = sig
print(f"[ap-vpn] Avertissement détournement L2->L3 (mode Pont) : {last_err or 'aucun moteur L2 compatible'}", flush=True)
return False
def sync_vpn_client_rules():
@@ -6887,8 +6930,7 @@ def sync_vpn_client_rules():
subprocess.run(['iptables', '-t', 'nat', '-F', 'AP_VPN_DNS'], capture_output=True)
while subprocess.run(['iptables', '-t', 'mangle', '-D', 'PREROUTING', '-j', 'AP_VPN_MANGLE'], capture_output=True).returncode == 0: pass
subprocess.run(['iptables', '-t', 'mangle', '-F', 'AP_VPN_MANGLE'], capture_output=True)
if os.path.exists('/sys/module/ebtable_broute'):
subprocess.run(['ebtables', '-t', 'broute', '-F', 'BROUTING'], capture_output=True)
_cleanup_ebtables_vpn()
nft_b = find_bin('nft')
if nft_b: subprocess.run([nft_b, 'delete', 'table', 'bridge', 'ap_vpn_broute'], capture_output=True)
return
+3
View File
@@ -364,6 +364,9 @@ cleanup() {
# Nettoyage des règles ebtables et iptables VPN
nft delete table bridge ap_vpn_broute 2>/dev/null || true
ebtables -t broute -F BROUTING 2>/dev/null || true
while ebtables -t nat -D PREROUTING -j AP_VPN_NAT 2>/dev/null; do :; done
ebtables -t nat -F AP_VPN_NAT 2>/dev/null || true
ebtables -t nat -X AP_VPN_NAT 2>/dev/null || true
iptables -t mangle -F AP_VPN_MANGLE 2>/dev/null || true
while iptables -t mangle -D PREROUTING -j AP_VPN_MANGLE 2>/dev/null; do :; done
iptables -t mangle -X AP_VPN_MANGLE 2>/dev/null || true