]> johannes.truschnigg.info Git - netconsole-rx/commitdiff
Initial import of autosetup stuff
authorJohannes Truschnigg <johannes@truschnigg.info>
Thu, 13 Jul 2023 08:20:05 +0000 (10:20 +0200)
committerJohannes Truschnigg <johannes@truschnigg.info>
Thu, 13 Jul 2023 10:12:52 +0000 (12:12 +0200)
netconsole_autosetup [new file with mode: 0755]
netconsole_autosetup_test [new file with mode: 0755]

diff --git a/netconsole_autosetup b/netconsole_autosetup
new file mode 100755 (executable)
index 0000000..0f9331f
--- /dev/null
@@ -0,0 +1,114 @@
+#!/bin/sh
+[ -z "${1}" ] && exit 1
+
+nchost="${1}"
+dprt="${2:-6666}"
+sprt="${3:-6665}"
+
+ping -c 1 -w 1 "${nchost}" >/dev/null || exit 1
+
+tfile="$(mktemp -t netconsole_setup_XXXXXX)"
+
+_cleanup() {
+  rm -f -- "${tfile}"
+  exit "${1:-0}"
+}
+
+_gethwaddr() {
+  ping -c 1 -w 1 "${1}" >/dev/null 2>/dev/null
+  ip neigh show "${1}" | \
+  while read addr l_dev dev l_addr hwaddr _
+  do
+    if [ "${l_addr}" != lladdr ]
+    then
+      printf 'echo ERROR - UNEXPECTED OUTPUT >&2; _cleanup 1\n'
+      return
+    fi
+    printf 'eval_hwaddr=%s\neval_iface=%s\n' "${hwaddr}" "${dev}"
+  done
+}
+
+if ! ip route get "${nchost}" > "${tfile}"
+then
+  printf 'FATAL: Cannot determine route to target host.\n' >&2
+  _cleanup 1
+fi
+
+read tgt kind _ < "${tfile}"
+
+if [ "${tgt}" = local ]
+then
+  # OpenWrt:  local 127.0.0.1 dev lo  src 127.0.0.1
+  # iproute2: local 127.0.0.1 dev lo src 127.0.0.1 uid 1000
+  printf 'FATAL: Target address is local to this host.\n' >&2
+  _cleanup 1
+elif [ "${tgt}" != "${nchost}" ]
+then
+  printf 'FATAL: Mismatch between route target and given netconsole host.\n' >&2
+  _cleanup 1
+fi
+
+if [ "${kind}" = dev ]
+then
+  : IPv4 local
+  # OpenWrt:  172.18.3.199 dev br-lan  src 172.18.3.254
+  # iproute2: 172.18.3.199 dev enp3s0 src 172.18.3.138 uid 1000
+  read tgt kind dev l_src srcaddr _ < "${tfile}"
+  eval "$(_gethwaddr "${tgt}")"
+  mode='IPv4 local'
+elif [ "${kind}" = via ]
+then
+  : IPv4 routed
+  # OpenWrt:  8.8.8.8 via 100.126.0.243 dev pppoe-wan  src 193.238.236.243
+  # iproute2: 8.8.8.8 via 172.18.3.254 dev enp3s0 src 172.18.3.138 uid 1000
+  read tgt kind gw l_dev dev l_src srcaddr _ < "${tfile}"
+  eval "$(_gethwaddr "${gw}")"
+  mode='IPv4 routed'
+elif [ "${kind}" = from ]
+then
+  : IPv6
+  read tgt kind colons label_diff _ < "${tfile}"
+  if [ "${label_diff}" = dev ]
+  then
+    : IPv6 local
+    # OpenWrt:  2a02:1748:fafe:cf3f::111 from :: dev br-lan  src 2a02:1748:fafe:cf3f::1  metric 1024
+    # iproute2: 2a02:1748:fafe:cf3f::111 from :: dev enp3s0 proto ra src 2a02:1748:fafe:cf3f::127 metric 100 pref medium
+    read tgt kind colons l_dev dev l_proto proto l_src srcaddr _ < "${tfile}"
+    if [ "${l_proto}" != proto ]
+    then
+      echo 'FATAL: Incomplete platform support; please use IPv4.' >&2
+      _cleanup 2
+    fi
+    eval "$(_gethwaddr "${tgt}")"
+    mode='IPv6 local'
+  elif [ "${label_diff}" = via ]
+  then
+    : IPv6 routed
+    # OpenWrt:  2a03:4000:21:3::1:1337 from :: dev pppoe-wan  src 2a02:1748:fafe:cf30::1  metric 1024
+    # iproute2: 2a03:4000:21:3::1:1337 from :: via fe80::2e0:5cff:fe68:14ff dev enp3s0 proto ra src 2a02:1748:fafe:cf3f::127 metric 100 pref medium
+    read tgt kind colons l_via gw l_dev dev l_proto proto l_src srcaddr _ < "${tfile}"
+    if [ "${l_proto}" != proto ]
+    then
+      echo 'FATAL: Incomplete platform support; please use IPv4.' >&2
+      _cleanup 2
+    fi
+    eval "$(_gethwaddr "${gw}")"
+    mode='IPv6 routed'
+  fi
+else
+ :
+fi
+
+if [ -z "${eval_iface}" ] || [ -z "${eval_hwaddr}" ]
+then
+  printf 'FATAL: Cannot determine destination HWaddr.\n' >&2
+  _cleanup 3
+fi
+
+#netconsole=[+][src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]
+printf 'netconsole=+%d@%s/%s' "${sprt}" "${srcaddr}" "${eval_iface}"
+printf ',%d@%s/%s\n' "${dprt}" "${tgt}" "${eval_hwaddr}"
+
+#echo $tgt $mode / HWADDR IS ${eval_hwaddr} / DEV IS ${eval_iface}
+
+_cleanup
diff --git a/netconsole_autosetup_test b/netconsole_autosetup_test
new file mode 100755 (executable)
index 0000000..990c332
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh
+for i in 172.18.3.199 8.8.8.8 2a02:1748:fafe:cf3f::111 2a03:4000:21:3::1:1337
+do
+./netconsole_autosetup "$i"
+done