Skip to content

Instantly share code, notes, and snippets.

@majek
Created August 18, 2023 10:38
Show Gist options
  • Save majek/bc7cd3c897a2da2eb2280f870b5711d5 to your computer and use it in GitHub Desktop.
Save majek/bc7cd3c897a2da2eb2280f870b5711d5 to your computer and use it in GitHub Desktop.
Manage `ip mptcp endpoint` state on linux
#!/bin/bash
set -e
echo "[+] Clearling old mptcp endpoints"
for i in `ip mptcp endpoint show|cut -d " " -f 3`; do
ip mptcp endpoint delete id $i;
done
declare -A OLD
ID=1
(echo YES; ip monitor address) | while read LINE; do
echo "[.] Update"
declare -A NEW
while read -r IFACE ADDR; do
L="$ADDR dev $IFACE"
if [ "${OLD[$L]}" == "" ] && [ "${NEW[$L]}" == "" ]; then
NEW["$L"]="$ID"
echo "ip mptcp endpoint add $L id $ID subflow"
ip mptcp endpoint add $L id $ID subflow
ID=$[$ID+1]
elif [ "${NEW[$L]}" != "" ]; then
NEW["$L"]="${OLD["$L"]}"
unset OLD["$L"]
# already there
fi
done <<< $(\
ip -j address \
| jq -r \
".[] \
| select( .operstate | contains(\"UP\") ) \
| .ifname as \$ifname \
| .addr_info[] \
| select(.scope | contains(\"global\")) \
| select(.temporary | . == null or . == \"\") \
| \$ifname + \" \" + .local" \
)
for OLDL in "${!OLD[@]}"; do
DID="${OLD["$OLDL"]}"
echo "ip mptcp endpoint delete id $DID # $OLDL"
ip mptcp endpoint delete id $DID
unset OLD["$OLDL"]
unset NEW["$OLDL"]
done
for L in "${!NEW[@]}"; do
OLD["$L"]="${NEW["$L"]}"
done
done
@matttbe
Copy link

matttbe commented Aug 18, 2023

Nice script!

Note that you can replace the first for loop to remove the old MPTCP endpoints by:

ip mptcp endpoint flush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment