From be6ce3c31959da3bf8b41450ebcc2542a67a3d16 Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Sat, 5 Sep 2020 00:35:04 +0200 Subject: [PATCH 1/4] Add executor for managing B.A.T.M.A.N. adv. interfaces Signed-off-by: Maximilian Wilhelm --- executor-scripts/linux/batman | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 executor-scripts/linux/batman diff --git a/executor-scripts/linux/batman b/executor-scripts/linux/batman new file mode 100644 index 0000000..886377e --- /dev/null +++ b/executor-scripts/linux/batman @@ -0,0 +1,96 @@ +#!/bin/sh +# +# Maximilian Wilhelm +# -- Wed 26 Aug 2020 08:15:58 PM CEST +# +# This executor is responsible for setting up the main B.A.T.M.A.N. adv. interface (eg. bat0) +# as well as managing settings of the underlying interfaces (hardifs). +# +# Know options for the main interface are: +# IF_BATMAN_IFACES Space separated list of interfaces to be part of this B.A.T.M.A.N. adv. instance +# IF_BATMAN_IFACES_IGNORE_REGEX Interfaces to ignore when verifying configuration (regexp) +# IF_BATMAN_DISTRIBUTED_ARP_TABLE 'enable' or 'disable' DAT of this B.A.T.M.A.N. adv. instance +# IF_BATMAN_MULTICAST_MODE 'enable' or 'disable' the multicast mode of this B.A.T.M.A.N. adv. instance +# IF_BATMAN_GW_MODE Set the gateway mode of this B.A.T.M.A.N. adv. instance +# IF_BATMAN_HOP_PENALTY Set the hop penalty of this B.A.T.M.A.N. adv. instance +# +# Known options for underlying interfaces are: +# IF_BATMAN_IFACE_PENALTY Set the hop penalty of this B.A.T.M.A.N. adv. interface +# +set -e + +if [ "$VERBOSE" ]; then + set -x +fi + +if [ "${IF_BATMAN_IFACES}" -o "${IF_BATMAN_IFACE_PENALTY}" ]; then + if ! which batctl >/dev/null 2>&1; then + echo "Error: batctl utility not found!" >&2 + exit 1 + fi +fi + +# +# Compatiblity glue: Newer versions of batctl print a deprecation +# warning when called with -m . Avoid spamming the log and +# producting SPAM by silently handling this here. +mesh_if_param="-m" +if batctl -h 2>&1 | grep -q "meshif"; then + mesh_if_param="meshif" +fi + +# +# Functions to manage main B.A.T.M.A.N. adv. interface +batctl_if () { + for iface in ${IF_BATMAN_IFACES}; do + ${MOCK} batctl "${mesh_if_param}" "${IFACE}" interface "$1" "${iface}" + done +} + +set_batmam_params () { + [ "${IF_BATMAN_DISTRIBUTED_ARP_TABLE}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" distributed_arp_table "${IF_BATMAN_DISTRIBUTED_ARP_TABLE}" + [ "${IF_BATMAN_MULTICAST_MODE}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" multicast_mode "${IF_BATMAN_MULTICAST_MODE}" + [ "${IF_BATMAN_HOP_PENALTY}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" hop_penalty "${IF_BATMAN_HOP_PENALTY}" + [ "${IF_BATMAN_GW_MODE}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" gw_mode "${IF_BATMAN_GW_MODE}" +} + +# +# Functions to manage B.A.T.M.A.N. adv. underlying interfaces +set_iface_penalty () { + ${MOCK} batctl hardif "${IFACE}" hop_penalty "${IF_BATMAN_IFACE_PENALTY}" +} + + +case "${PHASE}" in + depend) + if [ "${IF_BATMAN_IFACES}" ]; then + echo "${IF_BATMAN_IFACES}" + fi + ;; + + pre-up) + # Main B.A.T.M.A.N. adv. interface + if [ "${IF_BATMAN_IFACES}" ]; then + batctl "${mesh_if_param}" "${IFACE}" interface create || true + batctl_if add + set_batmam_params + + # B.A.T.M.A.N. adv. underlying interface + elif [ "${IF_BATMAN_IFACE_PENALTY}" ]; then + set_iface_penalty + fi + ;; + + post-down) + if [ "${IF_BATMAN_IFACES}" ]; then + # Newer versions of batctl provide the "interface destroy" API, try to use it + if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then + # Fall back to old style member interface removal + batctl_if del + fi + fi + ;; + + *) exit 0 + ;; +esac From 017a12760ceac40975da9d27343a7c2a84c8ea09 Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Sat, 5 Sep 2020 01:52:43 +0200 Subject: [PATCH 2/4] batman executor: Only create/delete iface if (not) present. Signed-off-by: Maximilian Wilhelm --- executor-scripts/linux/batman | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/executor-scripts/linux/batman b/executor-scripts/linux/batman index 886377e..dbc826b 100644 --- a/executor-scripts/linux/batman +++ b/executor-scripts/linux/batman @@ -71,7 +71,10 @@ case "${PHASE}" in pre-up) # Main B.A.T.M.A.N. adv. interface if [ "${IF_BATMAN_IFACES}" ]; then - batctl "${mesh_if_param}" "${IFACE}" interface create || true + if [ ! -d "/sys/class/net/${IFACE}" ]; then + batctl "${mesh_if_param}" "${IFACE}" interface create || true + fi + batctl_if add set_batmam_params @@ -82,7 +85,7 @@ case "${PHASE}" in ;; post-down) - if [ "${IF_BATMAN_IFACES}" ]; then + if [ "${IF_BATMAN_IFACES}" -a -d "/sys/class/net/${IFACE}" ]; then # Newer versions of batctl provide the "interface destroy" API, try to use it if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then # Fall back to old style member interface removal @@ -91,6 +94,7 @@ case "${PHASE}" in fi ;; - *) exit 0 + *) + exit 0 ;; esac From 35e03475e44c8db1275b6b3436e8d990a1431fde Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Thu, 10 Sep 2020 00:40:57 +0200 Subject: [PATCH 3/4] batman executor: Use create/destroy phases Signed-off-by: Maximilian Wilhelm --- executor-scripts/linux/batman | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/executor-scripts/linux/batman b/executor-scripts/linux/batman index dbc826b..87ae989 100644 --- a/executor-scripts/linux/batman +++ b/executor-scripts/linux/batman @@ -68,13 +68,18 @@ case "${PHASE}" in fi ;; - pre-up) + create) # Main B.A.T.M.A.N. adv. interface if [ "${IF_BATMAN_IFACES}" ]; then if [ ! -d "/sys/class/net/${IFACE}" ]; then batctl "${mesh_if_param}" "${IFACE}" interface create || true fi + fi + ;; + pre-up) + # Main B.A.T.M.A.N. adv. interface + if [ "${IF_BATMAN_IFACES}" ]; then batctl_if add set_batmam_params @@ -84,7 +89,7 @@ case "${PHASE}" in fi ;; - post-down) + destroy) if [ "${IF_BATMAN_IFACES}" -a -d "/sys/class/net/${IFACE}" ]; then # Newer versions of batctl provide the "interface destroy" API, try to use it if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then From 0674a70c351892525ff1a0ddce0ea372dd1adf16 Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Thu, 10 Sep 2020 00:42:35 +0200 Subject: [PATCH 4/4] batman executor: Fix typo Signed-off-by: Maximilian Wilhelm --- executor-scripts/linux/batman | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/executor-scripts/linux/batman b/executor-scripts/linux/batman index 87ae989..c041634 100644 --- a/executor-scripts/linux/batman +++ b/executor-scripts/linux/batman @@ -6,7 +6,7 @@ # This executor is responsible for setting up the main B.A.T.M.A.N. adv. interface (eg. bat0) # as well as managing settings of the underlying interfaces (hardifs). # -# Know options for the main interface are: +# Known options for the main interface are: # IF_BATMAN_IFACES Space separated list of interfaces to be part of this B.A.T.M.A.N. adv. instance # IF_BATMAN_IFACES_IGNORE_REGEX Interfaces to ignore when verifying configuration (regexp) # IF_BATMAN_DISTRIBUTED_ARP_TABLE 'enable' or 'disable' DAT of this B.A.T.M.A.N. adv. instance @@ -47,7 +47,7 @@ batctl_if () { done } -set_batmam_params () { +set_batman_params () { [ "${IF_BATMAN_DISTRIBUTED_ARP_TABLE}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" distributed_arp_table "${IF_BATMAN_DISTRIBUTED_ARP_TABLE}" [ "${IF_BATMAN_MULTICAST_MODE}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" multicast_mode "${IF_BATMAN_MULTICAST_MODE}" [ "${IF_BATMAN_HOP_PENALTY}" ] && ${MOCK} batctl "${mesh_if_param}" "${IFACE}" hop_penalty "${IF_BATMAN_HOP_PENALTY}" @@ -81,7 +81,7 @@ case "${PHASE}" in # Main B.A.T.M.A.N. adv. interface if [ "${IF_BATMAN_IFACES}" ]; then batctl_if add - set_batmam_params + set_batman_params # B.A.T.M.A.N. adv. underlying interface elif [ "${IF_BATMAN_IFACE_PENALTY}" ]; then