#!/bin/sh # # Update the MPTCP flags with or without the pached iproute2 # # Author: Mario Krueger # Ycarus (Yannick Chabanois) for OpenMPTCProuter # Released under GPL 3 or later if [ -d "/proc/sys/net/mptcp" ]; then # read, not `cat` in two subshells: this runs on every call if { [ -f /proc/sys/net/mptcp/mptcp_enabled ] && read -r mptcp_enabled < /proc/sys/net/mptcp/mptcp_enabled && [ "$mptcp_enabled" = 0 ]; } || { [ -f /proc/sys/net/mptcp/enabled ] && read -r mptcp_enabled < /proc/sys/net/mptcp/enabled && [ "$mptcp_enabled" = 0 ]; }; then echo "MPTCP is disabled!" exit 1 fi else echo "Your device don't support multipath-TCP." exit 1 fi # From an "ip mptcp endpoint show" listing ($1), set id_used when an endpoint # has the id $2 and id_holder to its device, and dev_ids to the endpoint ids # of device $3. Lines are "ADDR [port P] id ID [FLAGS...] [dev DEV]". endpoint_ids() { local listing="$1" want="$2" dev="$3" ep_line ep_id ep_dev id_used="" id_holder="" dev_ids="" while IFS= read -r ep_line; do ep_id="" ep_dev="" set -- $ep_line while [ "$#" -gt 1 ]; do case "$1" in id) ep_id="$2" ;; dev) ep_dev="$2" ;; esac shift done [ -n "$ep_id" ] || continue [ "$ep_id" = "$want" ] && { id_used=1 id_holder="$ep_dev" } [ -n "$dev" ] && [ "$ep_dev" = "$dev" ] && dev_ids="$dev_ids $ep_id" done <' instead of letting the" echo "kernel auto-assign one. Needed for a stable id per WAN across reconnects, e.g." echo "so a peer (VPS) can pin traffic to this WAN via the subflow's remote_id." echo "It goes to the first address of the device, the next ones get id+128 and" echo "id+192 (IPv6 on a dual-stack WAN)." echo exit 0 ;; "-c") if [ -f /proc/net/mptcp_net/mptcp ]; then cat /proc/net/mptcp_net/mptcp else ss -M fi exit 0;; "-f") if [ -f /proc/net/mptcp_fullmesh ]; then cat /proc/net/mptcp_fullmesh else ip mptcp endpoint | grep fullmesh fi exit 0;; "-k") if [ -f /proc/sys/net/mptcp/mptcp_enabled ]; then echo Enabled: `cat /proc/sys/net/mptcp/mptcp_enabled` elif [ -f /proc/sys/net/mptcp/enabled ]; then echo Enabled: `cat /proc/sys/net/mptcp/enabled` fi if [ -f /proc/sys/net/mptcp/mptcp_path_manager ]; then echo Path Manager: `cat /proc/sys/net/mptcp/mptcp_path_manager` fi if [ -f /proc/sys/net/mptcp/mptcp_checksum ]; then echo Use checksum: `cat /proc/sys/net/mptcp/mptcp_checksum` elif [ -f /proc/sys/net/mptcp/checksum_enabled ]; then echo Use checksum: `cat /proc/sys/net/mptcp/checksum_enabled` fi if [ -f /proc/sys/net/mptcp/mptcp_scheduler ]; then echo Scheduler: `cat /proc/sys/net/mptcp/mptcp_scheduler` fi if [ -f /proc/sys/net/mptcp/mptcp_syn_retries ]; then echo Syn retries: `cat /proc/sys/net/mptcp/mptcp_syn_retries` fi if [ -f /proc/sys/net/mptcp/mptcp_debug ]; then echo Debugmode: `cat /proc/sys/net/mptcp/mptcp_debug` fi echo exit 0 ;; "-m") if [ -f /proc/net/mptcp_net/snmp ]; then cat /proc/net/mptcp_net/snmp else nstat -z | grep -i mptcp fi exit 0 ;; "-v") if [ -f /proc/sys/net/mptcp/enabled ]; then DEVICE="$2" endpoint="$(ip mptcp endpoint show)" if [ -f /usr/bin/jsonfilter ]; then IP=$(ip -j a show $DEVICE scope global | jsonfilter -e '@[0].addr_info[*].local') elif [ -f /usr/bin/jq ]; then IP=$(ip -j a show $DEVICE scope global | jq -r '.[0].addr_info[].local') else echo "jsonfilter or jq are required" exit 1 fi if [ -n "$IP" ] && [ -n "$endpoint" ]; then for i in $IP; do ID=$(echo "${endpoint}" | grep -F "$i " | awk '{print $3}') [ -n "$ID" ] && break done fi fi if [ -n "$ID" ]; then echo "OK" exit 0 else echo "NOK" exit 1 fi ;; "-i") # Exit 1 when the endpoint of device $2 doesn't have the explicit id $3 # although no other endpoint holds it, so that recreating it gives that # id back. An endpoint that kept an old id is never fixed by the mode # checks of the callers, and it blocks the WAN that now has this id. [ -f /proc/sys/net/mptcp/enabled ] || exit 0 case "$3" in ''|*[!0-9]*) exit 0 ;; esac [ "$3" -le 255 ] || exit 0 endpoint_ids "$(ip mptcp endpoint show)" "$3" "$2" if [ -z "$dev_ids" ] || [ -n "$id_used" ]; then exit 0 fi exit 1 ;; "") for ifpath in /sys/class/net/*; do $0 ${ifpath##*/} done exit 0;; *);; esac DEVICE="$1" TYPE="$2" # Optional explicit MPTCP endpoint id (stable across delete/recreate, unlike # the kernel's auto-assigned one) so a peer can key on subflow->remote_id. # Anything that isn't a plain 0-255 integer is ignored (kernel auto-assigns). ENDPOINT_ID="$3" case "$ENDPOINT_ID" in ''|*[!0-9]*) ENDPOINT_ID="" ;; *) [ "$ENDPOINT_ID" -ge 0 ] 2>/dev/null && [ "$ENDPOINT_ID" -le 255 ] 2>/dev/null || ENDPOINT_ID="" ;; esac #FLAG_PATH=`find /sys/devices/ -path "*/net/$DEVICE/flags"` [ -e "/sys/class/net/$DEVICE/" ] || { #echo "Device '$DEVICE' can't found!" #echo "Use the hardware name like in ifconfig" exit 1 } if [ -f /proc/sys/net/mptcp/mptcp_enabled ]; then 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 else endpoint="$(ip mptcp endpoint show)" if [ -n "$TYPE" ]; then # Remove not needed if* interfaces in MPTCP [ -n "$endpoint" ] && oldids=$(echo "${endpoint}" | grep "dev if" | awk '{ print $3 }') [ -n "$oldids" ] && { for oldid in $oldids; do ip mptcp endpoint delete id $oldid >/dev/null 2>&1 done endpoint="$(ip mptcp endpoint show)" } fi if [ -n "$endpoint" ]; then # The ids of the device's endpoints, and the flags of its first # endpoint in sorted order. A sort|grep|awk pipeline for each of the # three cost 15 processes per call, and a status query runs for every # device ("multipath" alone lists them all): one pass over the list # gives the same values without any when the device has one endpoint. ep_count=0 while IFS= read -r ep_line; do case "$ep_line" in *"dev $DEVICE "*) ep_first="$ep_line" ep_count=$((ep_count + 1)) ;; esac done </dev/null 2>&1 fi case $TYPE in "off") [ -n "$ID" ] && { for i in $ID; do ip mptcp endpoint delete id $i >/dev/null 2>&1 done } exit 0;; "on") [ -z "$IP" ] && { echo "No global IP found on $DEVICE" exit 1 } [ -n "$ID" ] && { for i in $ID; do ip mptcp endpoint delete id $i >/dev/null 2>&1 done } add_endpoints subflow fullmesh exit 0;; "signal") [ -z "$IP" ] && { echo "No global IP found on $DEVICE" exit 1 } [ -n "$ID" ] && { for i in $ID; do ip mptcp endpoint delete id $i >/dev/null 2>&1 done } #add_endpoints signal subflow fullmesh add_endpoints signal exit 0;; "backup") [ -z "$IP" ] && { echo "No global IP found on $DEVICE" exit 1 } [ -n "$ID" ] && { for i in $ID; do ip mptcp endpoint delete id $i >/dev/null 2>&1 done } add_endpoints subflow backup fullmesh exit 0;; "") case "$IFF" in "") echo $DEVICE is deactivated;; "subflow") case "$IFF2" in "backup") echo $DEVICE is in backup mode;; "fullmesh") echo $DEVICE is in default mode;; "") echo $DEVICE is in subflow mode;; esac ;; "signal") echo $DEVICE is in signal mode;; *) echo "$DEVICE Unkown state!" && exit 1;; esac exit 0;; *) echo "Unkown flag! Use 'multipath -h' for help" && exit 1;; esac fi