#!/bin/bash
#
#------------------------------------------------
MAKE_IP_DRIVER_TARGET="naslite_netlink_ether"
MAKE_LTE_ACCESS_STRATUM_TARGET="oaisim"
MAKE_REMSERIAL_TARGET="remserial"
IP_DRIVER_NAME=oai_nw_drv
###########################################################
#  CONFIGURE OPTIONS
EMULIF="eth0"
LTEIF="oai0"

INTIF="ppp0"
EXTIF=$LTEIF
#----------------------------------------------------------
LTE_NETWORK_CIDR="10.0.1.0/24"
ENB_IPv4="10.0.1.1"
ENB_IPv6="2001:1::1"
#----------------------------------------------------------
UE_IPv4="10.0.1.2"
UE_IPv6="2001:1::2"
UE_IPv6_CIDR=$UE_IPv6"/64"
UE_IPv4_CIDR=$UE_IPv4"/24"
UE_IMEI="3,9,1,8,3,6,7,3,0,0,0,0,0,0"
#----------------------------------------------------------
USE_PPP="yes"
PPP_OPTIONS="proxyarp passive nodetach ipcp-accept-remote"
PPP_DEVICE="ttyPPP_DROID"
PPP_SPEED="460800"
UE_PPP_IPv4="192.168.15.1"
TERMINAL_PPP_IPv4="192.168.15.175"
REMSERIAL_UDP_PORT="23001"
#----------------------------------------------------------

#         +-------+                     +-------+                     +-------+
#         | eNB   |              EMULIF | UE    |INTIF                |ANDROID|
#         |       +---------------------+       +---------------------+ TERMIN|
#         |       |                     |       |   ppp link over     |  AL   |
#         |       |                     |       |   UDP over ethernet |       |
#         |       |                     |       |    (remserial)      |       |
#         |       |                     |       |                     |       |
#         |       |                     |       +---serial link-------+       |
#         |       |                     |       |   for AT commands   |       |
#         |       |               LTEIF |       |                     |       |
#         |       +.................... +       |                     |       |
#         |       |ENB_IPv4 UE_IPv4_CIDR|       |                     |       |
#         +-------+    LTE link over    +-------+                     +-------+

###########################################################
IPTABLES=/sbin/iptables
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
declare -x OPENAIR_DIR=""
declare -x OPENAIR1_DIR=""
declare -x OPENAIR2_DIR=""
declare -x OPENAIR3_DIR=""
declare -x OPENAIR_TARGETS=""
###########################################################


black='\E[30m'
red='\E[31m'
green='\E[32m'
yellow='\E[33m'
blue='\E[34m'
magenta='\E[35m'
cyan='\E[36m'
white='\E[37m'

ROOT_UID=0
E_NOTROOT=67


cecho()   # Color-echo
          # arg1 = message
          # arg2 = color
{
local default_msg="No Message."
    message=${1:-$default_msg}
    color=${2:-$black}
    echo -e "$color"
    echo -n "$message"
    tput sgr0
    echo
    return
}

echo_error() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $red
}

echo_warning() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $yellow
}

echo_success() {
local my_string=""
  until [ -z "$1" ]
  do
    my_string="$my_string$1"
    shift
  done
  cecho "$my_string" $green
}

set_openair() {
    path=`pwd`
    declare -i length_path
    declare -i index
    length_path=${#path}

    index=`echo $path | grep -b -o 'targets' | cut -d: -f1`
    #echo ${path%$token*}
    if [[ $index -lt $length_path  && index -gt 0 ]]
       then
           declare -x OPENAIR_DIR
           index=`expr $index - 1`
           openair_path=`echo $path | cut -c1-$index`
           #openair_path=`echo ${path:0:$index}`
           export OPENAIR_DIR=$openair_path
           export OPENAIR1_DIR=$openair_path/openair1
           export OPENAIR2_DIR=$openair_path/openair2
           export OPENAIR3_DIR=$openair_path/openair3
           export OPENAIR_TARGETS=$openair_path/targets
           return 0
    fi
    return -1
}

bash_exec() {
  output=$($1 2>&1)
  result=$?
  if [ $result -eq 0 ]
     then
        echo_success "$1"
     else
        echo_error "$1: $output"
  fi
}

wait_process_started () {
  if  [ -z "$1" ]
  then
    echo_error "WAITING FOR PROCESS START: NO PROCESS"
    return 1
  fi
  ps -C $1 > /dev/null 2>&1
  while [ $? -ne 0 ]; do
    echo_warning "WAITING FOR $1 START"
    sleep 2
    ps -C $1 > /dev/null 2>&1
  done
  echo_success "PROCESS $1 STARTED"
  return 0
}

assert() {
  # If condition false
  # exit from script with error message
  E_PARAM_ERR=98
  E_PARAM_FAILED=99
  
  if [ -z "$2" ] # Not enought parameters passed.
  then
    return $E_PARAM_ERR
  fi
  
  lineno=$2
  if [ ! $1 ]
  then
    echo "Assertion failed:  \"$1\""
    echo "File \"$0\", line $lineno"
    exit $E_ASSERT_FAILED
  fi
}

ctrl_c() {
  bash_exec "pkill oaisim"
  if [ "$USE_PPP"x = yesx ]; then
      bash_exec "pkill remserial.bash"
      bash_exec "pkill ppp_android.bash"
  fi
  bash_exec "pkill AT_serial_bouchon.py"
  bash_exec "ip link set $LTEIF down"
  
  bash_exec "ip route del default        via $ENB_IPv4     dev $EXTIF table int2lte"
  bash_exec "ip route flush table  int2lte"
  bash_exec "ip rule del  prio 1000"
  
  bash_exec "rmmod $IP_DRIVER_NAME"

  bash_exec "$IPTABLES -P INPUT ACCEPT"
  bash_exec "$IPTABLES -F INPUT"
  bash_exec "$IPTABLES -P OUTPUT ACCEPT"
  bash_exec "$IPTABLES -F OUTPUT"
  bash_exec "$IPTABLES -P FORWARD ACCEPT"
  bash_exec "$IPTABLES -F FORWARD"
  bash_exec "$IPTABLES -t nat    -F"
  bash_exec "$IPTABLES -t mangle -F"
  bash_exec "$IPTABLES -t filter -F"
  bash_exec "ip route flush cache"
}

#bash_exec "set_openair"
set_openair
cecho "OPENAIR_DIR     = $OPENAIR_DIR" $green
cecho "OPENAIR1_DIR    = $OPENAIR1_DIR" $green
cecho "OPENAIR2_DIR    = $OPENAIR2_DIR" $green
cecho "OPENAIR3_DIR    = $OPENAIR3_DIR" $green
cecho "OPENAIR_TARGETS = $OPENAIR_TARGETS" $green

echo "Bringup UE interface"
bash_exec "rmmod $IP_DRIVER_NAME"
cecho "make $MAKE_IP_DRIVER_TARGET $MAKE_LTE_ACCESS_STRATUM_TARGET $MAKE_REMSERIAL_TARGET....." $green
bash_exec "make $MAKE_IP_DRIVER_TARGET $MAKE_LTE_ACCESS_STRATUM_TARGET $MAKE_REMSERIAL_TARGET"
bash_exec "insmod  $OPENAIR2_DIR/NAS/DRIVER/LITE/$IP_DRIVER_NAME.ko oai_nw_drv_IMEI=$UE_IMEI"


echo "   Enabling ipv4 forwarding.."
bash_exec "sysctl -w net.ipv4.ip_forward=1"
assert "`sysctl -n net.ipv4.ip_forward` -eq 1" $LINENO

bash_exec "sysctl -w net.ipv4.conf.all.forwarding=1"
assert "`sysctl -n net.ipv4.conf.all.forwarding` -eq 1" $LINENO

echo "   Enabling ipv6 forwarding.."
bash_exec "sysctl -w net.ipv6.conf.all.forwarding=1"
assert "`sysctl -n net.ipv6.conf.all.forwarding` -eq 1" $LINENO


if [ "$USE_PPP"x = yesx ]; then
  echo "   PPP enabled.."
  command -v pppd > /dev/null 2>&1 || { echo_error "pppd executable needed - please install it"; exit 1;}
  command -v remserial.udp > /dev/null 2>&1 || { echo_error "remserial.udp executable needed - please install it"; exit 1;}
  #generate pppd script
  rm -f /tmp/ppp_android.bash
  bash_exec "touch  /tmp/ppp_android.bash"
  if [ ! -f /tmp/ppp_android.bash ]; then
    echo_error "Cannot generate /tmp/ppp_android.bash file, exiting"
    exit 1
  fi
  echo "#!/bin/bash" >> /tmp/ppp_android.bash 
  echo "pppd $PPP_OPTIONS $UE_PPP_IPv4:$TERMINAL_PPP_IPv4 /dev/$PPP_DEVICE $PPP_SPEED" >> /tmp/ppp_android.bash
  bash_exec "chmod 777 /tmp/ppp_android.bash"
  assert " -x /tmp/ppp_android.bash " $LINENO
  
  #generate remserial script
  rm -f /tmp/remserial.bash
  bash_exec "touch /tmp/remserial.bash"
  if [ ! -f /tmp/remserial.bash ]; then
    echo_error "Cannot generate /tmp/remserial.bash file, exiting"
    exit 1
  fi
  echo "#!/bin/bash" >> /tmp/remserial.bash
  echo "remserial.udp  -s \"$PPP_SPEED raw\" -p $REMSERIAL_UDP_PORT -l /dev/$PPP_DEVICE /dev/ptmx" >> /tmp/remserial.bash
  bash_exec "chmod 777 /tmp/remserial.bash"
  assert " -x /tmp/remserial.bash " $LINENO
fi

xterm -e $THIS_SCRIPT_PATH/AT_serial_bouchon.py   &
wait_process_started AT_serial_bouchon.py
  
if [ "$USE_PPP"x = yesx ]; then
  xterm -e /tmp/remserial.bash &
  wait_process_started remserial.bash
  
  xterm -e /tmp/ppp_android.bash  &
  wait_process_started ppp_android.bash
fi


assert " -x $IPTABLES " $LINENO

bash_exec "ip route del default        via $ENB_IPv4     dev $EXTIF table int2lte"
bash_exec "ip route flush table  int2lte"
bash_exec "ip rule del  prio 1000"

bash_exec "$IPTABLES -P INPUT ACCEPT"
bash_exec "$IPTABLES -F INPUT"
bash_exec "$IPTABLES -P OUTPUT ACCEPT"
bash_exec "$IPTABLES -F OUTPUT"
bash_exec "$IPTABLES -P FORWARD DROP"
bash_exec "$IPTABLES -F FORWARD"
bash_exec "$IPTABLES -t nat    -F"
bash_exec "$IPTABLES -t mangle -F"
bash_exec "$IPTABLES -t filter -F"

bash_exec "ip route flush cache"
bash_exec "ip route add 239.0.0.160/28 dev $EMULIF"

bash_exec "ip link set $LTEIF broadcast ff:ff:ff:ff:ff:ff"
bash_exec "ip link set $LTEIF up"
sleep 1
bash_exec "ip addr add dev $LTEIF $UE_IPv4_CIDR"
bash_exec "ip addr add dev $LTEIF $UE_IPv6_CIDR"

echo "   Enabling proxy ARP.."
bash_exec "sysctl -w net.ipv4.conf.all.proxy_arp=1"
assert "`sysctl -n net.ipv4.conf.all.proxy_arp` -eq 1" $LINENO


# -a     -> Add RB
# -d    -> Delete RB
# -cxx  -> lcr
# -ixx  -> instance
# -zxx  -> dscp
# -fxxx -> classref (uid of a classifier entry) if fn is used , fn is used for send classifier and n+1 for receive classifier
# -sxxx -> source ipv4 address
# -txxx -> destination ipv4 address
# -x    -> source ipv6 address
# -y    -> destination ipv6 address
# -r    -> radio bearer id
bash_exec "$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -f0 -i0 -z0  -x $UE_IPv6     -y $ENB_IPv6     -r 3"
bash_exec "$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -f2 -i0 -z64 -s $UE_IPv4/32  -t $ENB_IPv4/32  -r 3"
bash_exec "$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -f4 -i0 -z64 -s 0.0.0.0/32   -t 0.0.0.0/32   -r 3"
sleep 1

echo "   External Interface:  $EXTIF"
echo "   Internal Interface:  $INTIF"

bash_exec "modprobe ip_tables"
bash_exec "modprobe ip_conntrack"
bash_exec "modprobe ip_conntrack_ftp"
bash_exec "modprobe nf_conntrack_h323"
bash_exec "modprobe nf_conntrack_irc"
bash_exec "modprobe nf_conntrack_pptp"
bash_exec "modprobe nf_conntrack_proto_gre"
bash_exec "modprobe nf_conntrack_proto_sctp"
bash_exec "modprobe nf_conntrack_tftp"
bash_exec "modprobe nf_conntrack_sip"
bash_exec "modprobe iptable_nat"
bash_exec "modprobe x_tables"

echo "   Enabling DynamicAddr.."
bash_exec "sysctl -w net.ipv4.ip_dynaddr=1"
assert "  `sysctl -n net.ipv4.ip_dynaddr` -eq 1" $LINENO

bash_exec "sysctl -w net.ipv4.conf.all.log_martians=1"
assert "  `sysctl -n net.ipv4.conf.all.log_martians` -eq 1" $LINENO

bash_exec "sysctl -w net.ipv4.conf.$LTEIF.log_martians=1"
assert "  `sysctl -n net.ipv4.conf.$LTEIF.log_martians` -eq 1" $LINENO

bash_exec "sysctl -w net.ipv4.conf.$EMULIF.log_martians=1"
assert "  `sysctl -n net.ipv4.conf.$EMULIF.log_martians` -eq 1" $LINENO

if [ ! "$USE_PPP"x = yesx ]; then
  bash_exec "sysctl -w net.ipv4.conf.$INTIF.log_martians=1"
  assert "  `sysctl -n net.ipv4.conf.$INTIF.log_martians` -eq 1" $LINENO
fi
echo "   Disabling reverse path filtering"
bash_exec "sysctl -w net.ipv4.conf.all.rp_filter=0"
assert "  `sysctl -n net.ipv4.conf.all.rp_filter` -eq 0" $LINENO

bash_exec "sysctl -w net.ipv4.conf.$LTEIF.rp_filter=0"
assert "  `sysctl -n net.ipv4.conf.$LTEIF.rp_filter` -eq 0" $LINENO

bash_exec "sysctl -w net.ipv4.conf.$EMULIF.rp_filter=0"
assert "  `sysctl -n net.ipv4.conf.$EMULIF.rp_filter` -eq 0" $LINENO

if [ ! "$USE_PPP"x = yesx ]; then
  bash_exec "sysctl -w net.ipv4.conf.$INTIF.rp_filter=0"
  assert "  `sysctl -n net.ipv4.conf.$INTIF.rp_filter` -eq 0" $LINENO
fi

bash_exec "$IPTABLES -t mangle -A PREROUTING -i $INTIF -j MARK --set-mark 22"

bash_exec "$IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT"
bash_exec "$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT"
bash_exec "$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE"

bash_exec "ip route add 127.0.0.0/8                        dev lo     table int2lte"
bash_exec "ip route add $LTE_NETWORK_CIDR                  dev $EXTIF table int2lte scope link"
bash_exec "ip route add default            via $ENB_IPv4   dev $EXTIF table int2lte scope global"

bash_exec "ip route flush cache"

bash_exec "ip rule add fwmark 22   lookup int2lte prio 1000"

echo "#####################################################################"
echo "iptables -t nat -nvL"
echo "---------------------------------------------------------------------"
iptables -t nat -nvL
echo "#####################################################################"
echo "iptables -t mangle -nvL"
echo "---------------------------------------------------------------------"
iptables -t mangle -nvL
echo "#####################################################################"
echo "iptables -t filter -nvL"
echo "---------------------------------------------------------------------"
iptables -t filter -nvL
echo "#####################################################################"
echo "ip rule show"
echo "---------------------------------------------------------------------"
ip rule show
echo "#####################################################################"
echo "ip route show table int2lte"
echo "---------------------------------------------------------------------"
ip route show table int2lte
echo "#####################################################################"
echo "ip route show table main"
echo "---------------------------------------------------------------------"
ip route show table main

trap ctrl_c INT

echo "to see UE stats, please run : watch_ue script"
echo "$OPENAIR_TARGETS/SIMU/USER/oaisim  -b0 -M1 -p2 -g3 -l3  > /dev/null"
nice -10 $OPENAIR_TARGETS/SIMU/USER/oaisim  -b0 -M1 -p2 -g3 -l3 > /dev/null
#gdb $OPENAIR_TARGETS/SIMU/USER/oaisim
echo "End"
ctrl_c



