#!/bin/sh
#
# Update the MP-TCP flags without the pached iproute2
#
# Author: Mario Krueger <openwrt at xedp3x.de>
# Released under GPL 3 or later

if [ -d "/proc/sys/net/mptcp" ]; then
        if [ `cat /proc/sys/net/mptcp/mptcp_enabled` = 0 ]; then
                echo "MPTCP is disabled!"
                echo "Please set net.mptcp.mptcp_enabled = 1"
                exit 1
        fi
else
        echo "Your device don't support multipath-TCP."
        echo "You have to install the pached kernel to use MPTCP."
        echo "See http://multipath-tcp.org/ for details"
        exit 1
fi

case $1 in
   "-h")
        echo "          Multipath-TCP configuration tool"
        echo "show/update flags:"
        echo "  multipath [device]"
        echo "  multipath device {on | off | backup | handover}"
        echo
        echo "show established conections: -c"
        echo "show fullmesh info: -f"
        echo "show kernel config: -k"
        echo
        echo "Flag on the device, to enable/disable MPTCP for this interface. The backup-flag"
        echo "will allow a subflow to be established across this interface, but only be used"
        echo "as backup. Handover-flag indicates that his interface is not used at all (even "
        echo "no subflow being established), as long as there are other interfaces available."
        echo "See http://multipath-tcp.org/ for details"
        echo
        exit 0 ;;
   "-c")
        cat /proc/net/mptcp_net/mptcp
        exit 0;;
   "-f")
        cat /proc/net/mptcp_fullmesh
        exit 0;;
   "-k")
        echo Enabled: `cat /proc/sys/net/mptcp/mptcp_enabled`
        echo Path Manager: `cat /proc/sys/net/mptcp/mptcp_path_manager`
        echo Use checksum: `cat /proc/sys/net/mptcp/mptcp_checksum`
        echo Scheduler: `cat /proc/sys/net/mptcp/mptcp_scheduler`
        echo Syn retries: `cat /proc/sys/net/mptcp/mptcp_syn_retries`
        echo Debugmode: `cat /proc/sys/net/mptcp/mptcp_debug`
        echo
        echo See http://multipath-tcp.org/ for details
        exit 0 ;;
   "")
        for ifpath in /sys/class/net/*; do
                $0 ${ifpath##*/}
        done
        exit 0;;
   *);;
esac

DEVICE="$1"
TYPE="$2"
#FLAG_PATH=`find /sys/devices/ -path "*/net/$DEVICE/flags"`

[ -d "/sys/class/net/$DEVICE/" ] || {
        echo "Device '$DEVICE' can't found!"
        echo "Use the hardware name like in ifconfig"
        exit 1
}

FLAG_PATH="/sys/class/net/$DEVICE/flags"
IFF=`cat $FLAG_PATH`

IFF_OFF="0x80000"
IFF_ON="0x00"
IFF_BACKUP="0x100000"
IFF_HANDOVER="0x200000"
IFF_MASK="0x380000"

case $TYPE in
        "off")          FLAG=$IFF_OFF;;
        "on")           FLAG=$IFF_ON;;
        "backup")       FLAG=$IFF_BACKUP;;
        "handover")     FLAG=$IFF_HANDOVER;;
        "")
                IFF=`printf "0x%02x" $(($IFF&$IFF_MASK))`
                case "$IFF" in
                        $IFF_OFF)       echo $DEVICE is deactivated;;
                        $IFF_ON)        echo $DEVICE is in default mode;;
                        $IFF_BACKUP)    echo $DEVICE is in backup mode;;
                        $IFF_HANDOVER)  echo $DEVICE is in handover mode;;
                        *) echo "Unkown state!" && exit 1;;
                esac
                exit 0;;
        *) echo "Unkown flag! Use 'multipath -h' for help" && exit 1;;
esac

printf "0x%02x" $(($(($IFF^$(($IFF&$IFF_MASK))))|$FLAG)) > $FLAG_PATH


