#!/usr/bin/bash
#
# Probe Interface Setup
# Copyright (C) 2019-2024 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no
#

# Bash options:
set -e


# Example:
#
# Probe-Interface-Setup add eth1 255 tdr1 11.254.3.1 11.254.1.50 0x00030001 192.168.3.1
# Probe-Interface-Setup add eth2 254 tdr2 11.254.3.1 11.254.1.50 0x00030002 192.168.3.2
#
# SNIFFING_INTERFACE="eth1"
# SNIFFING_INTERFACE_PROVIDER=255
# FORWARDING_INTERFACE="tdr1"
# FORWARDING_LOCAL="11.254.3.1"
# FORWARDING_REMOTE="11.254.1.50"
# FORWARDING_GRE_KEY="0x00030001"
# FORWARDING_TTL="64"
# FORWARDING_MTU="1280"
# COLLECTOR_ADDRESS="192.168.3.1"


if [ "$#" -ne 8 ] ; then
   echo >&2 "Usage: $0 add|remove sniffing_interface sniffing_provider_index fwd_interface fwd_local fwd_remote fwd_gre_key collector_addr"
   exit 1
fi

ACTION="$1"
if [ "$ACTION" != "add" -a "$ACTION" != "remove" ] ; then
   echo >&2 "ERROR: Bad action \"$ACTION\"."
   exit 1
fi

SNIFFING_INTERFACE="$2"
SNIFFING_INTERFACE_PROVIDER="$3"

FORWARDING_INTERFACE="$4"
FORWARDING_LOCAL="$5"
FORWARDING_REMOTE="$6"
FORWARDING_GRE_KEY="$7"
FORWARDING_TTL="64"
FORWARDING_MTU="1280"

COLLECTOR_ADDRESS="$8"

PRECEDENCE="100"


remove-sniffer ()
{
   ip rule del iif "$FORWARDING_INTERFACE" lookup "$table" || true
   ip route del "$COLLECTOR_ADDRESS/32" dev "$FORWARDING_INTERFACE" || true
   ip link set dev "$FORWARDING_INTERFACE" down || true

   # NOTE: Sometimes on system shutdown, "ip tunnel del ..." hangs with
   # unregister_netdevice: waiting for tunl1 to become free.
   # If this happens, kill "ip" and continue to let the system reboot.
   intTimeout=8
   killTimeout=10
   timeout -s INT -k $intTimeout $killTimeout ip tunnel del "$FORWARDING_INTERFACE" || true
}


install-sniffer ()
{
   remove-sniffer >/dev/null 2>&1

   ip tunnel add "$FORWARDING_INTERFACE" mode gre key "$FORWARDING_GRE_KEY" local "$FORWARDING_LOCAL" remote "$FORWARDING_REMOTE" ttl "$FORWARDING_TTL"
   ip link set dev "$FORWARDING_INTERFACE" up mtu "$FORWARDING_MTU"
   ip route add "$COLLECTOR_ADDRESS/32" dev "$FORWARDING_INTERFACE"
   ip rule add iif "$FORWARDING_INTERFACE" lookup "$table" pref "$PRECEDENCE"
}


# NOTE: Make sure that the provider ID is right
# If changing the table scheme in Make-Tunnelbox-Configuration,
# make sure to adapt the following line, too!
let table=1000000+$SNIFFING_INTERFACE_PROVIDER


if [ "$ACTION" = "add" ] ; then
   install-sniffer
else
   remove-sniffer
fi
ip -4 route flush cache
