#!/bin/sh
echo "Bringup eNB interface"

ip link set oai0 down
rmmod nasmesh
make  naslite_netlink_ether oaisim
insmod $OPENAIR2_DIR/NAS/DRIVER/LITE/nasmesh.ko nas_IMEI=3,9,1,8,3,6,6,2,0,0,0,0,0,0

#avoid conflict with reserved multicast addresses (224.0.0.11,224.0.0.13, 224.0.0.16)
#route add -net 224.0.0.0 netmask 240.0.0.0 dev eth2
#ip addr add dev eth1 192.168.14.145/24 broadcast 192.168.14.255
ip link set oai0 broadcast ff:ff:ff:ff:ff:ff
#ip route add 192.168.14.0/24 dev eth2
ip route add 224.0.0.160/28 dev eth1
ip link set oai0 up
sleep 1

EXTIF="eth0"
INTIF="oai0"
IPTABLES=/sbin/iptables
#IPTABLES=/usr/local/sbin/iptables
DEPMOD=/sbin/depmod
MODPROBE=/sbin/modprobe
echo "   External Interface:  $EXTIF"
echo "   Internal Interface:  $INTIF"
echo "----------------------------------------------------------------------"
echo -en "ip_tables, "
$MODPROBE ip_tables
echo -en "ip_conntrack, "
$MODPROBE ip_conntrack
echo -en "ip_conntrack_irc, "
$MODPROBE ip_conntrack_irc
echo -en "iptable_nat, "
$MODPROBE iptable_nat
echo -e "   Done loading modules.\n"
echo "   Enabling forwarding.."
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "   Enabling DynamicAddr.."
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
echo "   Clearing any existing rules and setting default policy.."
$IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT 
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT 
$IPTABLES -P FORWARD DROP
$IPTABLES -F FORWARD 
$IPTABLES -t nat    -F
$IPTABLES -t mangle -F
$IPTABLES -t filter -F
echo "   FWD: Allow all connections OUT and only existing and related ones IN"
$IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
#$IPTABLES -A FORWARD -j LOG
echo "   Enabling SNAT (MASQUERADE) functionality on $EXTIF"
$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE



ifconfig oai0 10.0.1.1 netmask 255.255.255.0 broadcast 10.0.1.255
sleep 1
#$OPENAIR2_DIR/NAS/DRIVER/MESH/RB_TOOL/rb_tool -a -c0 -i0 -z0 -s 10.0.1.1 -t 10.0.1.2 -r 3
ip addr add dev oai0 2001:1::1/64
# -a    -> Add RB
# -d    -> Delete RB
# -cxx  -> lcr (uid of a cx_entity)
# -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
echo "rb_tool -a -c0 -i0 -f0 -z0 -x 2001:1::1/128  -y 2001:1::2/128 -r 1"
$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -i0 -f0 -z0 -x 2001:1::1/128  -y 2001:1::2/128 -r 1

echo "rb_tool -a -c0 -i0 -f2 -z63 -s 10.0.1.1/32   -t 10.0.1.2/32   -r 1"
$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -i0 -f2 -z64 -s 10.0.1.1/32   -t 10.0.1.2/32   -r 1

echo "rb_tool -a -c0 -i0 -f2 -z63 -s 10.0.1.1/32   -t 10.0.1.2/32   -r 1"
$OPENAIR2_DIR/NAS/DRIVER/LITE/RB_TOOL/rb_tool -a -c0 -i0 -f4 -z64 -s 0.0.0.0/32   -t 0.0.0.0/32   -r 1

echo "to see eNB stats, please run : watch_enb script"
echo "$OPENAIR_TARGETS/SIMU/USER/oaisim  -u0 -M0 -p2  -g3 -l3  > /dev/null"
nice -10 $OPENAIR_TARGETS/SIMU/USER/oaisim  -u0 -M0 -p2  -g3 -l3 | grep RLC
echo "End"
