#!/usr/bin/bash
#
# Reset Networking under Debian/Ubuntu or Fedora Core
# Copyright (C) 2012-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
#


# ###### Write log information ##############################################
log ()
{
   echo -e "\x1b[1;34m`date +%FT%H:%M:%S`: $@\x1b[0m"
}


if [ -e /etc/init.d/network ] ; then
   log "Stopping networking (Fedora Core)"
   service network stop
elif [ -e /etc/init.d/networking ] ; then
   log "Stopping networking (Debian/Ubuntu)"
   service networking stop 2>&1 | grep -v "Unknown instance"
fi


log "Unconfiguring remaining tunnels"
TUNNELS=`( ip tunnel show && ip -6 tunnel show ) | awk '/[a-zA-Z0-9\-]*:/ { print $1 }' | sed -e "s/://g" | sort`
for tunnel in $TUNNELS ; do
   echo "Unconfiguring $tunnel ..."
   # echo "ip tunnel del $tunnel"
   ip tunnel del $tunnel
done


log "Unconfiguring remaining interfaces"
# Possibly, some interfaces are still up. Get rid of them!
INTERFACES=`ip link show | awk '/^([0-9]*:) ([a-zA-Z0-9\-]+):/ { print $2 }' | sed -e "s/:$//" | sort`
for interface in $INTERFACES ; do
   echo "Unconfiguring $interface ..."

   shutdown=1

   ADDRESSES=`ip -4 addr show dev $interface | awk '/[ \t]*inet ([0-9]+).([0-9]+).([0-9]+).([0-9]+)\/([0-9]+) / { print $2 }'`
   for address in $ADDRESSES ; do
      if [ "$address" != "127.0.0.1/8" ] ; then
         # echo ip -4 addr del $address dev $interface
         ip -4 addr del $address dev $interface
      else
         shutdown=0
      fi
   done

   ADDRESSES=`ip -6 addr show dev $interface | awk '/[ \t]*inet6 [0-9a-fA-F:]*\/[0-9]+ scope [gsh]/ { print $2 }'`
   for address in $ADDRESSES ; do
      if [ "$address" != "::1/128" ] ; then
         # echo ip -6 addr del $address dev $interface
         ip -6 addr del $address dev $interface
      else
         shutdown=0
      fi
   done

   if [ $shutdown -eq 1 ] ; then
      ip link set dev $interface down
   fi
done


log "Unconfiguring rules and routes"
for version in 4 6 ; do
   ip -${version} rule flush
   ip -${version} rule del lookup local || true
   ip -${version} rule add from all lookup local   pref 0
   ip -${version} rule add from all lookup main    pref 32766
   ip -${version} rule add from all lookup default pref 32767
done

iptables -t nat -F POSTROUTING
iptables -t nat -F PREROUTING

ip -4 route flush cache
ip -6 route flush cache


log "This is the current, clean setup:"
ip addr show
ip route show
ip -6 route show


log "Cleaning /var/run/network/ifstate"
echo "" >/var/run/network/ifstate
find /var/run/network/ -name 'ifup.*' | xargs --no-run-if-empty rm -f

if [ -e /var/run/nornet-tunnelbox ] ; then
   log "Clearing /var/run/nornet-tunnelbox"
   find /var/run/nornet-tunnelbox/ -name "*.provider" | xargs --no-run-if-empty rm -f
   find /var/run/nornet-tunnelbox/ -name "*.interface*" | xargs --no-run-if-empty rm -f
fi

if [ -e /etc/init.d/network ] ; then
  log "Restarting networking (Fedora Core)"
  service network stop || true
  service network start
elif [ -e /etc/init.d/networking ] ; then
  log "Restarting networking (Debian/Ubuntu)"
  service networking stop 2>&1 | grep -v "Unknown instance" || true
  service networking start
  if [ -e /etc/init.d/iptables-persistent ] ; then
     service iptables-persistent stop || true
     service iptables-persistent start
  fi
fi
