#!/bin/sh
# Upstream (6.x) MPTCP exposes the scheduler list; the out-of-tree v0.9x
# stack (blest/roundrobin/redundant/ecf) does not. Pick a default the running
# kernel accepts: 'blest' on 6.x is rejected by the sysctl and /etc/init.d/mptcp
# then falls back to 'default' on every reload while logging about it.
if [ -f /proc/sys/net/mptcp/available_schedulers ]; then
	_omr_default_sched='default'
else
	_omr_default_sched='blest'
fi
if [ "$(uci -q get network.globals.mptcp_path_manager)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.multipath='enable'
	set network.globals.mptcp_path_manager='fullmesh'
	set network.globals.mptcp_scheduler='${_omr_default_sched}'
	set network.globals.congestion='bbr2'
	set network.globals.mptcp_checksum=0
	set network.globals.mptcp_debug=0
	set network.globals.mptcp_syn_retries=4
	set network.globals.mptcp_subflows=8
	set network.globals.mptcp_add_addr_accepted=1
	set network.globals.mptcp_add_addr_timeout=120
	set network.globals.mptcp_pm_type=0
	set network.globals.mptcp_disable_initial_config=0
	set network.globals.mptcp_force_multipath=1
	set network.globals.mptcpd_enable=0
	set network.globals.mptcp_close_timeout=60
	set network.globals.mptcp_blackhole_timeout=0
	set network.globals.mptcp_syn_retrans_before_tcp_fallback=2
	commit network
	EOF
fi
# Legacy v0.9x scheduler names carried over to a 6.x kernel: only the names
# that never exist upstream are migrated (BPF schedulers are not listed in
# available_schedulers until their object is registered, so they must not be
# touched here).
if [ -f /proc/sys/net/mptcp/available_schedulers ]; then
	case "$(uci -q get network.globals.mptcp_scheduler)" in
		blest|roundrobin|redundant|ecf)
			uci -q batch <<-EOF >/dev/null
			set network.globals.mptcp_scheduler='default'
			commit network
			EOF
			;;
	esac
fi
# BBRv2 is replaced by BBRv3
if [ "$(uci -q get network.globals.congestion)" = "bbr2" ] && [ -z "$(uname -a | grep 5.4)" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.congestion='bbr'
	commit network
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_syn_retries)" = "1" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_syn_retries=4
	commit network
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_checksum)" != "0" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_checksum=0
	commit network
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_checksum)" = "enable" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_checksum=1
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_checksum)" = "disable" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_checksum=0
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_debug)" != "0" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_debug=0
	EOF
fi

if [ "$(uci -q show network.globals | grep mptcp_fullmesh)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_fullmesh_num_subflows=1
	set network.globals.mptcp_fullmesh_create_on_err=1
	set network.globals.mptcp_ndiffports_num_subflows=1
	commit network
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_subflows)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_subflows=8
	set network.globals.mptcp_add_addr_accepted=8
	set network.globals.mptcp_add_addr_timeout=120
	commit network
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_add_addr_accepted)" = "1" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_add_addr_accepted=8
	commit network
	EOF
fi

uci -q batch <<-EOF >/dev/null
    delete ucitrack.@mptcp[-1]
    add ucitrack mptcp
    set ucitrack.@mptcp[-1].init=mptcp
    commit ucitrack
EOF
if [ "$(uci -q get ucitrack.@network[-1].affects | grep mptcp)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
	    add_list ucitrack.@network[-1].affects=mptcp
	    commit ucitrack
	EOF
fi
if [ "$(uci -q get network.globals.mptcp_close_timeout)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
	set network.globals.mptcp_close_timeout=60
	set network.globals.mptcp_blackhole_timeout=0
	set network.globals.mptcp_syn_retrans_before_tcp_fallback=2
	commit network
	EOF
fi
exit 0