|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# 37-host-optimization.sh: Apply and validate network optimizations (ARP flux fix) |
| 4 | +# |
| 5 | +# This script improves detection accuracy by ensuring proper ARP behavior. |
| 6 | +# It attempts to apply sysctl settings and warns if not possible. |
| 7 | + |
| 8 | +# --- Color Codes --- |
| 9 | +RED=$(printf '\033[1;31m') |
| 10 | +YELLOW=$(printf '\033[1;33m') |
| 11 | +RESET=$(printf '\033[0m') |
| 12 | + |
| 13 | +# --- Skip flag --- |
| 14 | +if [ -n "${SKIP_OPTIMIZATIONS:-}" ]; then |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +# --- Helpers --- |
| 19 | + |
| 20 | +get_sysctl() { |
| 21 | + sysctl -n "$1" 2>/dev/null || echo "unknown" |
| 22 | +} |
| 23 | + |
| 24 | +set_sysctl_if_needed() { |
| 25 | + key="$1" |
| 26 | + expected="$2" |
| 27 | + |
| 28 | + current="$(get_sysctl "$key")" |
| 29 | + |
| 30 | + # Already correct |
| 31 | + if [ "$current" = "$expected" ]; then |
| 32 | + return 0 |
| 33 | + fi |
| 34 | + |
| 35 | + # Try to apply |
| 36 | + if sysctl -w "$key=$expected" >/dev/null 2>&1; then |
| 37 | + return 0 |
| 38 | + fi |
| 39 | + |
| 40 | + # Failed |
| 41 | + return 1 |
| 42 | +} |
| 43 | + |
| 44 | +# --- Apply Settings (best effort) --- |
| 45 | + |
| 46 | +failed=0 |
| 47 | + |
| 48 | +set_sysctl_if_needed net.ipv4.conf.all.arp_ignore 1 || failed=1 |
| 49 | +set_sysctl_if_needed net.ipv4.conf.all.arp_announce 2 || failed=1 |
| 50 | +set_sysctl_if_needed net.ipv4.conf.default.arp_ignore 1 || failed=1 |
| 51 | +set_sysctl_if_needed net.ipv4.conf.default.arp_announce 2 || failed=1 |
| 52 | + |
| 53 | +# --- Validate final state --- |
| 54 | + |
| 55 | +all_ignore="$(get_sysctl net.ipv4.conf.all.arp_ignore)" |
| 56 | +all_announce="$(get_sysctl net.ipv4.conf.all.arp_announce)" |
| 57 | + |
| 58 | +# --- Warning Output --- |
| 59 | + |
| 60 | +if [ "$all_ignore" != "1" ] || [ "$all_announce" != "2" ]; then |
| 61 | + >&2 printf "%s" "${YELLOW}" |
| 62 | + >&2 cat <<EOF |
| 63 | +══════════════════════════════════════════════════════════════════════════════ |
| 64 | +⚠️ ATTENTION: ARP flux protection not enabled. |
| 65 | +
|
| 66 | + NetAlertX relies on ARP for device detection. Your system currently allows |
| 67 | + ARP replies from incorrect interfaces (ARP flux), which may result in: |
| 68 | +
|
| 69 | + • False devices being detected |
| 70 | + • IP/MAC mismatches |
| 71 | + • Flapping device states |
| 72 | + • Incorrect network topology |
| 73 | +
|
| 74 | + This is common when running in Docker or multi-interface environments. |
| 75 | +
|
| 76 | + ────────────────────────────────────────────────────────────────────────── |
| 77 | + Recommended fix (Docker Compose): |
| 78 | +
|
| 79 | + sysctls: |
| 80 | + net.ipv4.conf.all.arp_ignore: 1 |
| 81 | + net.ipv4.conf.all.arp_announce: 2 |
| 82 | +
|
| 83 | + ────────────────────────────────────────────────────────────────────────── |
| 84 | + Alternatively, apply on the host: |
| 85 | +
|
| 86 | + net.ipv4.conf.all.arp_ignore=1 |
| 87 | + net.ipv4.conf.all.arp_announce=2 |
| 88 | +
|
| 89 | + Detection accuracy may be reduced until this is configured. |
| 90 | +══════════════════════════════════════════════════════════════════════════════ |
| 91 | +EOF |
| 92 | + >&2 printf "%s" "${RESET}" |
| 93 | +fi |
0 commit comments