#!/bin/sh /etc/rc.common
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
# Released under GPL 3. See LICENSE for the full terms.

START=50

USE_PROCD=1

. /usr/lib/unbound/iptools.sh
. /lib/functions/network.sh

global_multipath_settings() {
	local multipath mptcp_path_manager mptcp_scheduler mptcp_subflows mptcp_debug mptcp_add_addr_accepted congestion mptcp_checksum mptcp_syn_retries mptcp_fullmesh_num_subflows mptcp_fullmesh_create_on_err mptcp_ndiffports_num_subflows mptcp_rr_cwnd_limited mptcp_rr_num_segments
	local multipath_status=0
	config_load network
	config_get multipath globals multipath
	config_get mptcp_path_manager globals mptcp_path_manager
	config_get mptcp_scheduler globals mptcp_scheduler
	config_get mptcp_subflows globals mptcp_subflows "3"
	config_get mptcp_add_addr_accepted globals mptcp_add_addr_accepted "1"
	config_get mptcp_add_addr_timeout globals mptcp_add_addr_timeout "120"
	config_get mptcp_debug globals mptcp_debug
	config_get congestion globals congestion
	config_get mptcp_checksum globals mptcp_checksum
	config_get mptcp_syn_retries globals mptcp_syn_retries
	config_get mptcp_fullmesh_num_subflows globals mptcp_fullmesh_num_subflows
	config_get mptcp_fullmesh_create_on_err globals mptcp_fullmesh_create_on_err
	config_get mptcp_ndiffports_num_subflows globals mptcp_ndiffports_num_subflows
	config_get mptcp_rr_cwnd_limited globals mptcp_rr_cwnd_limited
	config_get mptcp_rr_num_segments globals mptcp_rr_num_segments
	config_get mptcp_version globals mptcp_version "0"
	config_get mptcp_stale_loss_cnt globals mptcp_stale_loss_cnt "4"
	config_get mptcp_pm_type globals mptcp_pm_type "1"
	
	[ "$multipath" = "enable" ] && multipath_status=1

	# Global MPTCP configuration
	if [ -f /proc/sys/net/mptcp/mptcp_enabled ]; then
		sysctl -qew net.mptcp.mptcp_enabled="$multipath_status"
		if [ -z "$(grep net.mptcp.mptcp_enabled /etc/sysctl.d/zzz_openmptcprouter.conf)" ]; then
			echo "net.mptcp.mptcp_enabled=${multipath_status}" >> /etc/sysctl.d/zzz_openmptcprouter.conf
		else
			sed -i "s:^net.mptcp.mptcp_enabled=[0-1]*:net.mptcp.mptcp_enabled=${multipath_status}:" /etc/sysctl.d/zzz_openmptcprouter.conf
		fi
		[ -z "$mptcp_path_manager" ] || sysctl -qew net.mptcp.mptcp_path_manager="$mptcp_path_manager"
		[ -z "$mptcp_scheduler" ] || sysctl -qew net.mptcp.mptcp_scheduler="$mptcp_scheduler"
		[ -z "$mptcp_checksum" ] || sysctl -qew net.mptcp.mptcp_checksum="$mptcp_checksum"
		[ -z "$mptcp_debug" ] || sysctl -qew net.mptcp.mptcp_debug="$mptcp_debug"
		[ -z "$mptcp_syn_retries" ] || sysctl -qew net.mptcp.mptcp_syn_retries="$mptcp_syn_retries"
		[ -z "$mptcp_fullmesh_num_subflows" ] || sysctl -qew /sys/module/mptcp_fullmesh/parameters/num_subflows="$mptcp_fullmesh_num_subflows"
		[ -z "$mptcp_fullmesh_create_on_err" ] || sysctl -qew /sys/module/mptcp_fullmesh/parameters/create_on_err="$mptcp_fullmesh_create_on_err"
		[ -z "$mptcp_ndiffports_num_subflows" ] || sysctl -qew /sys/module/mptcp_ndiffports/parameters/num_subflows="$mptcp_ndiffports_num_subflows"
		[ -z "$mptcp_rr_cwnd_limited" ] || sysctl -qew /sys/module/mptcp_rr/parameters/cwnd_limited="$mptcp_rr_cwnd_limited"
		[ -z "$mptcp_rr_num_segments" ] || sysctl -qew /sys/module/mptcp_rr/parameters/num_segments="$mptcp_rr_num_segments"
		[ -z "$mptcp_version" ] || sysctl -qew net.mptcp.mptcp_version="$mptcp_version"
	elif [ -f /proc/sys/net/mptcp/enabled ]; then
		sysctl -qew net.mptcp.enabled="$multipath_status"
		if [ -z "$(grep net.mptcp.enabled /etc/sysctl.d/zzz_openmptcprouter.conf)" ]; then
			echo "net.mptcp.enabled=${multipath_status}" >> /etc/sysctl.d/zzz_openmptcprouter.conf
		else
			sed -i "s:^net.mptcp.enabled=[0-1]*:net.mptcp.enabled=${multipath_status}:" /etc/sysctl.d/zzz_openmptcprouter.conf
		fi
		ip mptcp limits set add_addr_accepted $mptcp_add_addr_accepted subflows $mptcp_subflows 2>&1 >/dev/null
		#[ -z "$mptcp_debug" ] || sysctl -qew net.mptcp.mptcp_debug="$mptcp_debug"
		[ -z "$mptcp_checksum" ] || sysctl -qew net.mptcp.mptcp_checksum="$mptcp_checksum"
		[ -z "$mptcp_add_addr_timeout" ] || sysctl -qew net.mptcp.add_addr_timeout="$mptcp_add_addr_timeout"
		[ -z "$mptcp_checksum" ] || sysctl -qew net.mptcp.checksum_enabled="$mptcp_checksum"
		[ -z "$mptcp_stale_loss_cnt" ] || sysctl -qew net.mptcp.stale_loss_cnt="$mptcp_stale_loss_cnt"
		[ -z "$mptcp_pm_type" ] || sysctl -qew net.mptcp.pm_type="$mptcp_pm_type"
	fi
	[ -z "$congestion" ] || sysctl -qew net.ipv4.tcp_congestion_control="$congestion"
}

interface_macaddr_count() {
	local intf="$1"
	local dmacaddr="$2"
	config_get macaddr "$intf" macaddr
	[ "$macaddr" = "$dmacaddr" ] && [ -z "$(echo $intf | grep '\.')" ] && nbmac=$((nbmac+1))
}

interface_max_metric() {
	local config="$1"
	if [ "$1" != "omrvpn" ] && [ "$1" != "omr6in4" ] && [ "$1" != "lan" ]; then
		config_get metric "$config" metric
		if [ "$metric" -gt "$count" ]; then
			count=$metric
		fi
	elif [ "$1" = "omrvpn" ]; then
		uci -q batch <<-EOF >/dev/null
			set network.${config}.metric=1500
			commit network
			set openmptcprouter.${config}.metric=1500
			commit openmptcprouter
		EOF
	elif [ "$1" = "omr6in4" ]; then
		uci -q batch <<-EOF >/dev/null
			set network.${config}.metric=1201
			commit network
			set openmptcprouter.${config}.metric=1201
			commit openmptcprouter
		EOF
	fi
}

interface_multipath_settings() {
	local mode iface proto metric
	local config="$1"
	local intf="$2"
	local enabled

	config_get enabled "$config" auto "1"
	network_get_device iface $config
	[ -z "$iface" ] && network_get_physdev iface $config
	[ -z "$iface" ] && iface=$(ifstatus "$config" | jsonfilter -q -e '@["l3_device"]')
	[ -z "$iface" ] && config_get iface "$config" ifname
	config_get txqueuelen "$config" txqueuelen
	[ -n "$(echo $iface | grep '@')" ] && iface=$(ifstatus "$config" | jsonfilter -q -e '@["device"]')
	if [ "$(uci -q get openmptcprouter.${config}.metric)" = "" ] || [ "$(uci -q get openmptcprouter.${config}.metric)" = "1" ]; then
		count=$((count+1))
		metric=$count
	else
		metric="$(uci -q get openmptcprouter.${config}.metric)"
	fi
	[ "$metric" -gt 1000 ] && [ "${config}" != "omrvpn" ] && [ "${config}" != "omr6in4" ] && metric=$((metric-1000))
	if [ "${config}" = "lan" ] || [ "$(uci -q get network.${config}.ip4table)" = "lan" ]; then
		metric="9999"
	elif [ "${config}" = "omrvpn" ]; then
		metric="1500"
	elif [ "${config}" = "omr6in4" ]; then
		metric="1201"
	fi
	config_set "$config" metric $metric
	uci -q set network.${config}.metric=$metric
	uci -q set openmptcprouter.${config}.metric=$metric
	config_get mode "$config" multipath
	id=$metric
	[ -n "$iface" ] && {
		gro=$(uci -q get network.${config}.gro)
		[ "$gro" = "1" ] && ethtool -K $iface gro on 2>&1 >/dev/null
		[ "$gro" = "0" ] && ethtool -K $iface gro on 2>&1 >/dev/null
		gso=$(uci -q get network.${config}.gso)
		[ "$gso" = "1" ] && ethtool -K $iface gso on 2>&1 >/dev/null
		[ "$gso" = "0" ] && ethtool -K $iface gso on 2>&1 >/dev/null
		lro=$(uci -q get network.${config}.lro)
		[ "$lro" = "1" ] && ethtool -K $iface lro on 2>&1 >/dev/null
		[ "$lro" = "0" ] && ethtool -K $iface lro on 2>&1 >/dev/null
		ufo=$(uci -q get network.${config}.ufo)
		[ "$ufo" = "1" ] && ethtool -K $iface ufo on 2>&1 >/dev/null
		[ "$ufo" = "0" ] && ethtool -K $iface ufo on 2>&1 >/dev/null
		tso=$(uci -q get network.${config}.tso)
		[ "$tso" = "1" ] && ethtool -K $iface tso on 2>&1 >/dev/null
		[ "$tso" = "0" ] && ethtool -K $iface tso on 2>&1 >/dev/null
	}
	[ "$mode" = "" ] && {
		mode="$(uci -q get openmptcprouter.${config}.multipath)"
		[ -n "$mode" ] && uci -q set network.${config}.multipath="$mode"
	}
	[ "$mode" = "" ] && {
		[ "$config" = "lan" ] && mode="off"
		[ "$config" = "omrvpn" ] && mode="off"
		[ "$config" = "omr6in4" ] && mode="off"
		[ "$mode" = "" ] && mode="off"
		logger -t "MPTCP" "Multipath not defined for $config set to $mode"
		uci -q set network.${config}.multipath="$mode"
		uci -q set openmptcprouter.${config}.multipath="$mode"
	}
	[ "$mode" != "off" ] && {
		[ -n "$mptcpintf" ] && mptcpintf="$mptcpintf $iface"
		[ -z "$mptcpintf" ] && mptcpintf="$iface"
		[ -z "$mptcpmintf" ] && mptcpmintf="$config"
		uci -q set network.${config}.defaultroute=0
		uci -q set network.${config}.peerdns=0
		echo '' > /etc/resolv.conf 2>&1 >/dev/null
	}
	[ "$mode" = "master" ] && {
		# Force that only one interface is master
		if [ "$master" != "" ]; then
			logger -t "MPTCP" "Multipath master already set, disable master for $config"
			mode="on"
			config_set "$config" multipath "on"
			uci -q set network.${config}.multipath="on"
			uci -q set openmptcprouter.${config}.multipath="on"
		else
			master="$config"
		fi
	}
	uci -q set openmptcprouter.${config}="interface"
	uci -q set openmptcprouter.${config}.multipath="$mode"

	config_get macaddr "$config" macaddr ""
	[ -n "$macaddr" ] && {
		nbmac=0
		config_foreach interface_macaddr_count interface $macaddr
		[ "$nbmac" != "1" ] && {
			logger -t "MPTCP" "Duplicate MAC saved, removing"
			uci -q delete network.${config}.macaddr
		}
	}

	[ "$enabled" = "0" ] && return 0
	[ -n "$intf" ] && [ "$iface" != "$intf" ] && return 0
	[ -z "$iface" ] && return 0
	#[ "$config" = "omrvpn" ] && return 0
	[ "$config" = "omrvpn" ] && mode="off"
	[ -n "$(ifconfig | grep $iface)" ] || return 0
	[ "$(echo $iface | grep _dev)" != "" ] && return 0
	[ "$(echo $iface | grep '^if')" != "" ] && return 0
	[ "$iface" = "lo" ] && return 0
	
	#echo "îface: $iface"
	
	#[ "$mode" = "off" ] && {
	#	ip rule del table $id > /dev/null 2>&1
	#	ip route flush $id > /dev/null 2>&1
	#	return 1
	#}

	# IPv4 Updates:
	local ipaddr
	local gateway
	local network
	local netmask
	local proto
	config_get proto $config proto
	if [ "$proto" = "static" ]; then
		config_get ipaddr $config ipaddr
		config_get gateway $config gateway
		config_get netmask $config netmask
		[ -n "$ipaddr" ] && [ -n "$netmask" ] && netmask=`ipcalc.sh $ipaddr/$netmask | sed -n '/PREFIX=/{;s/.*=//;s/ .*//;p;}'`
		[ -n "$ipaddr" ] && [ -n "$netmask" ] && network=`ipcalc.sh $ipaddr/$netmask | sed -n '/NETWORK=/{;s/.*=//;s/ .*//;p;}'`
	elif [ "$proto" != "gre" ]; then
		network_get_ipaddr ipaddr $config
		[ -z "$ipaddr" ] && ipaddr=$(ip -4 addr show dev $iface | awk '/inet / {print $2; exit}' | cut -d/ -f1 | tr -d "\n")
		network_get_gateway gateway $config true
		[ -z "$gateway" ] && gateway=$(ip -4 r list dev $iface | grep -v default | awk '/proto static/ {print $1}' | tr -d "\n")
		[ -z "$gateway" ] && gateway=$(uci -q get "network.$config.gateway")
		[ -z "$gateway" ] && gateway=$(ubus call network.interface.$config status | jsonfilter -q -l 1 -e '@.inactive.route[@.target="0.0.0.0"].nexthop' | tr -d "\n")
		if [ -z "$gateway" ] || [ "$( valid_subnet4 $gateway )" != "ok" ]; then
			gateway=$(ubus call network.interface.$config status | jsonfilter -q -l 1 -e '@.route[@.target="0.0.0.0"].nexthop' | tr -d "\n")
		fi
		if [ -z "$gateway" ] || [ "$( valid_subnet4 $gateway )" != "ok" ]; then 
			gateway=$(ubus call network.interface.${config}_4 status 2>/dev/null | jsonfilter -q -l 1 -e '@.inactive.route[@.target="0.0.0.0"].nexthop' | tr -d "\n")
		fi
		if [ -z "$gateway" ] || [ "$( valid_subnet4 $gateway )" != "ok" ]; then 
			gateway=$(ubus call network.interface.${config}_4 status 2>/dev/null | jsonfilter -q -l 1 -e '@.route[@.target="0.0.0.0"].nexthop' | tr -d "\n")
		fi
		if [ -z "$gateway" ] || [ "$( valid_subnet4 $gateway )" != "ok" ]; then
			gateway=$(ip -4 r list dev "$iface" | awk '/via/ {print $3;exit}' | tr -d "\n")
		fi
		if [ -z "$gateway" ] || [ "$( valid_subnet4 $gateway )" != "ok" ]; then
			gateway=$(traceroute -m1 -i $iface 8.8.8.8 2>/dev/null | awk 'FNR==2{ print $2 }')
			[ "$gateway" = "*" ] && gateway=""
		fi
		network_get_subnet netmask $config
		[ -n "$netmask" ] && [ "$(echo $netmask | grep '/')" != "" ] && netmask=""
		[ -z "$netmask" ] && netmask=$(ip -4 addr show dev $iface | awk '/peer/ {print $4; exit}' | cut -d/ -f2 | tr -d "\n")
		[ -z "$netmask" ] && netmask=$(ip -4 addr show dev $iface | awk '/inet / {print $2;exit}' | cut -d/ -f2 | tr -d "\n")
		[ -n "$ipaddr" ] && ipaddr=$(echo $ipaddr | cut -d/ -f1 | tr -d "\n")
		[ -n "$ipaddr" ] && [ -n "$netmask" ] && netmask=`ipcalc.sh $ipaddr/$netmask | sed -n '/PREFIX=/{;s/.*=//;s/ .*//;p;}'`
		[ -n "$ipaddr" ] && [ -n "$netmask" ] && network=`ipcalc.sh $ipaddr/$netmask | sed -n '/NETWORK=/{;s/.*=//;s/ .*//;p;}'`
	fi
	if [ "$(uci -q get openmptcprouter.settings.uci_route)" = "1" ]; then
		uci -q batch <<-EOF >/dev/null
			delete network.${config}_rule
			delete network.${config}_route
			delete network.${config}_route_default
			commit network
		EOF
	else
		[ -n "$(ip rule list | grep $id)" ] && ip rule del table $id > /dev/null 2>&1
		ip route flush $id > /dev/null 2>&1
	fi

	if [ -n "$gateway" ] && [ -n "$network" ]; then
		if [ "$(uci -q get openmptcprouter.settings.uci_route)" = "1" ]; then
			uci -q batch <<-EOF >/dev/null
				delete network.${config}_rule
				set network.${config}_rule=rule
				set network.${config}_rule.lookup=${id}
				set network.${config}_rule.priority=0
				set network.${config}_rule.src="${ipaddr}/32"
				set network.${config}_rule.created=mptcp
				delete network.${config}_route
				set network.${config}_route=route
				set network.${config}_route.interface=${config}
				set network.${config}_route.target=${network}
				set network.${config}_route.netmask=${netmask}
				set network.${config}_route.table=${id}
				set network.${config}_route.created=mptcp
				delete network.${config}_route_default
				set network.${config}_route_default=route
				set network.${config}_route_default.interface=${config}
				set network.${config}_route_default.target='0.0.0.0'
				set network.${config}_route_default.netmask='0.0.0.0'
				set network.${config}_route_default.gateway=$gateway
				set network.${config}_route_default.table=${id}
				set network.${config}_route_default.created=mptcp
				commit network
			EOF
		else
			[ -n "$ipaddr" ] && [ -z "$(ip rule show from $ipaddr table $id)" ] && ip rule add from $ipaddr table $id pref 0
			[ -z "$(ip rule show oif $iface table $id)" ] && ip rule add oif $iface table $id pref 0
			ip route replace $network/$netmask dev $iface scope link metric $id $initcwrwnd 2>&1 >/dev/null
			ip route replace $network/$netmask dev $iface scope link table $id $initcwrwnd 2>&1 >/dev/null
			ip route replace default via $gateway dev $iface table $id $initcwrwnd 2>&1 >/dev/null
			[ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ] && ip route replace default via $gateway dev $iface metric $id $initcwrwnd 2>&1 >/dev/null
			#ip route flush cache $id 2>&1 >/dev/null
		fi

		#config_get mode "$config" multipath ""
		#[ "$mode" = "" ] && mode="$(uci -q get openmptcprouter.${config}.multipath)"
		#[ "$mode" = "master" ] && {
		#	#echo "ip route replace default via $gateway dev $iface"
		#	ip route replace default via $gateway dev $iface 2>&1 >/dev/null
		#}
		if [ "$txqueuelen" != "" ]; then
			ifconfig $iface txqueuelen $txqueuelen > /dev/null 2>&1
		else
			ifconfig $iface txqueuelen 1000 > /dev/null 2>&1
		fi
	fi
	if [ "$(uci -q get openmptcprouter.settings.disable_ipv6)" != "1" ] && [ "$config" != "omr6in4" ]; then
		# IPv6 Updates:
		local ip6addr
		local ipaddr6
		local gateway6
		local network6
		local netmask6
		config_get ipaddr6 $config ip6addr
		config_get gateway6 $config ip6gw
		if [ -n "$ipaddr6" ]; then
			ip6addr=`echo "$ipaddr6" | cut -d/ -f1 | tr -d "\n"`
			netmask6=`ipcalc -p $ipaddr6 | sed -n '/PREFIX=/{;s/.*=//;s/ .*//;p;}'`
			network6=`ipcalc -n $ip6addr | sed -n '/NETWORK=/{;s/.*=//;s/ .*//;p;}'`
		fi
		if [ -z "$ip6addr" ] || [ -z "$gateway6" ]; then
			[ -z "$ip6addr" ] && network_get_ipaddr6 ip6addr $config
			[ -z "$ip6addr" ] && ip6addr=$(ip -6 addr show dev $iface | grep -v 'scope link' | awk '/inet6/ {print $2; exit}' | cut -d/ -f1 | tr -d "\n")
			[ -z "$gateway6" ] && network_get_gateway6 gateway6 $config true
			[ -z "$gateway6" ] && gateway6=$(ip -6 r list dev $iface | grep -v default | awk '/proto static/ {print $1; exit}' | tr -d "\n")
			[ -z "$gateway6" ] && gateway6=$(uci -q get "network.$config.ip6gw")
			[ -z "$gateway6" ] && gateway6=$(ubus call network.interface.$config status | jsonfilter -q -l 1 -e '@.inactive.route[@.target="::"].nexthop' | tr -d "\n")
			#if [ -z "$gateway6" ] || [ "$( valid_subnet6 $gateway6 )" != "ok" ]; then 
			#	gateway6=$(ubus call network.interface.$config status | jsonfilter -q -l 1 -e '@.route[@.target="::"].nexthop' | tr -d "\n")
			#fi
			#if [ -z "$gateway6" ] || [ "$( valid_subnet6 $gateway6 )" != "ok" ]; then
			#	echo "ipv6 not ok"
			#	gateway6=$(ubus call network.interface.${config}_6 status 2>/dev/null | jsonfilter -q -l 1 -e '@.inactive.route[@.target="::"].nexthop' | tr -d "\n")
			#fi
			gateway6=$(echo $gateway6 | cut -d/ -f1 | tr -d "\n")
			[ -n "$ip6addr" ] && netmask6=$(ip -6 addr show dev $iface | grep $ip6addr | awk '/inet6/ {print $2; exit}' | cut -d/ -f2 | tr -d "\n")
			[ -n "$ip6addr" ] && [ -n "$netmask6" ] && network6=`/usr/sbin/ipcalc -n ${ip6addr}/${netmask6} | sed -n '/NETWORK=/{;s/.*=//;s/ .*//;p;}'`
		fi
		if [ "$(uci -q get openmptcprouter.settings.uci_route)" = "1" ]; then
			uci -q batch <<-EOF >/dev/null
				delete network.${config}_rule6
				delete network.${config}_route6
				delete network.${config}_route6_default
				commit network
			EOF
		else
			[ -n "$(ip -6 rule list | grep 6$id)" ] && ip -6 rule del table 6$id > /dev/null 2>&1
			ip -6 route flush 6$id > /dev/null 2>&1
		fi
		if [ -n "$gateway6" ] && [ -n "$network6" ]; then
			#echo "gateway6: $gateway6 - network6: $network6 -> ok"
			if [ "$(uci -q get openmptcprouter.settings.uci_route)" = "1" ]; then
				uci -q batch <<-EOF >/dev/null
					delete network.${config}_rule6
					set network.${config}_rule6=rule6
					set network.${config}_rule6.lookup=6${id}
					set network.${config}_rule6.priority=0
					set network.${config}_rule6.src="${ipaddr6}/127"
					set network.${config}_rule6.created=mptcp
					delete network.${config}_route6
					set network.${config}_route6=route6
					set network.${config}_route6.interface=${config}
					set network.${config}_route6.target=${network6}/${netmask6}
					set network.${config}_route6.table=6${id}
					set network.${config}_route6.created=mptcp
					delete network.${config}_route6_default
					set network.${config}_route6_default=route6
					set network.${config}_route6_default.interface=${config}
					set network.${config}_route6_default.target='::'
					set network.${config}_route6_default.gateway=$gateway6
					set network.${config}_route6_default.table=6${id}
					set network.${config}_route6_default.created=mptcp
					commit network
				EOF
			else
				[ -n "$ip6addr" ] && ip -6 rule add from $ip6addr table 6$id pref 0 2>&1 >/dev/null
				[ -z "$(ip rule show pref 0 table 6$id oif $iface)" ] && ip rule add oif $iface table 6$id pref 0
				ip -6 route replace $network6/$netmask6 dev $iface scope link metric 6$id $initcwrwnd 2>&1 >/dev/null
				ip -6 route replace $network6/$netmask6 dev $iface scope link table 6$id $initcwrwnd 2>&1 >/dev/null
				ip -6 route replace default via $gateway6 dev $iface table 6$id $initcwrwnd 2>&1 >/dev/null
				[ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ] && ip -6 route replace default via $gateway6 dev $iface metric 6$id $initcwrwnd 2>&1 >/dev/null
				#ip -6 route flush cache 6$id 2>&1 >/dev/null
			fi

			#config_get mode "$config" multipath "off"
			#[ "$mode" = "master" ] && {
			#	ip -6 route replace default via $gateway6 dev $iface 2>&1 >/dev/null
			#}
			#[ "$mode" = "off" ] && {
			#	ifconfig $iface txqueuelen 50 > /dev/null 2>&1
			#} || {
			#	ifconfig $iface txqueuelen 100 > /dev/null 2>&1
			#}
		fi
	fi

	if [ "$(uci -q get openmptcprouter.settings.force_multipath)" != "0" ]; then
		multipath "$iface" "off"
		if ([ "$mode" = "master" ] || [ "$mode" = "on" ]) && [ -z "$(multipath $iface | grep default)" ]; then
			logger -t "MPTCP" "Set $iface to $mode"
			multipath "$iface" "on"
		elif [ "$mode" = "off" ] && [ -z "$(multipath $iface | grep deactivated)" ]; then
			logger -t "MPTCP" "Set $iface to $mode"
			multipath "$iface" "$mode"
		elif [ "$mode" = "backup" ] && [ -z "$(multipath $iface | grep backup)" ]; then
			logger -t "MPTCP" "Set $iface to $mode"
			multipath "$iface" "$mode"
		fi
	fi

}

load_interfaces() {
	network_get_device ifname $1
	[ -z "$ifname" ] && network_get_physdev ifname $1
	[ -z "$ifname" ] && config_get ifname "$1" device
	[ -z "$ifname" ] && config_get ifname "$1" ifname
	config_get multipath "$1" multipath ""
	[ -z "$multipath" ] && multipath="$(uci -q get openmptcprouter.$1.multipath)"
	[ "$multipath" != "off" ] && [ "$multipath" != "" ] && interfaces=" ${ifname} ${interfaces}"
}

set_multipath() {
	ls -1 /sys/class/net/ | while read iface; do
		exist=0
		for ifacemptcp in $mptcpintf; do
			if [ "$iface" = "$ifacemptcp" ]; then
				exist=1
			fi
		done
		[ "$exist" = "0" ] && [ -z "$(multipath $iface | grep deactivated)" ] && [ "$iface" != "bonding_master" ] && [ -n "$(multipath $iface)" ] && {
			logger -t "MPTCP" "Disabling MPTCP on interface $iface not found in enabled multipath list"
			multipath $iface off
		}
	done
}

add_route() {
	config_get disabled "$1" disabled
	[ "$disabled" = "1" ] && return 0
	config_get target "$1" target
	routeset="$target"
	config_get netmask "$1" netmask
	[ -n "$target" ] && [ -n "$netmask" ] && {
		netmask=`ipcalc.sh $target/$netmask | sed -n '/PREFIX=/{;s/.*=//;s/ .*//;p;}'`
		network=`ipcalc.sh $target/$netmask | sed -n '/NETWORK=/{;s/.*=//;s/ .*//;p;}'`
		[ -n "$netmask" ] && [ "$target" = "$network" ] && routeset="$routeset/$netmask"
	}
	config_get gateway "$1" gateway
	[ -n "$gateway" ] && routeset="$routeset via $gateway"
	config_get metric "$1" metric
	[ -n "$metric" ] && routeset="$routeset metric $metric"
	config_get mtu "$1" mtu
	[ -n "$mtu" ] && routeset="$routeset mtu $mtu"
	config_get type "$1" type
	[ -n "$type" ] && routeset="$routeset type $type"
	config_get table "$1" table
	[ -n "$table" ] && routeset="table $table"
	config_get interface "$1" interface
	iface=$(ifstatus "$interface" | jsonfilter -q -e '@["l3_device"]')
	[ -n "$(echo $iface | grep '@')" ] && iface=$(ifstatus "$interface" | jsonfilter -q -e '@["device"]')
	[ -n "$iface" ] && routeset="$routeset dev $iface"
	logger -t "MPTCP" "Add route $routeset"
	[ -n "$routeset" ] && {
		ip route replace ${routeset} 2>&1 >/dev/null
	}
}

add_route6() {
	config_get disabled "$1" disabled
	[ "$disabled" = "1" ] && return 0
	config_get target "$1" target
	routeset="$target"
	config_get gateway "$1" gateway
	[ -n "$gateway" ] && routeset="$routeset via $gateway"
	config_get metric "$1" metric
	[ -n "$metric" ] && routeset="$routeset metric $metric"
	config_get mtu "$1" mtu
	[ -n "$mtu" ] && routeset="$routeset mtu $mtu"
	config_get type "$1" type
	[ -n "$type" ] && routeset="$routeset type $type"
	config_get table "$1" table
	[ -n "$table" ] && routeset="table $table"
	config_get interface "$1" interface
	iface=$(ifstatus "$interface" | jsonfilter -q -e '@["l3_device"]')
	[ -n "$(echo $iface | grep '@')" ] && iface=$(ifstatus "$interface" | jsonfilter -q -e '@["device"]')
	[ -n "$iface" ] && routeset="$routeset dev $iface"
	logger -t "MPTCP" "Add IPv6 route $routeset"
	[ -n "$routeset" ] && {
		ip -6 route replace ${routeset} 2>&1 >/dev/null
	}
}

remove() {
	logger -t "MPTCP" "Remove network.$1"
	uci -q delete network.$1
}

start_service() {
	local intf=$1
	local id intfmaster
	. /lib/functions.sh
	. /lib/functions/network.sh
	#[ -n "$intf" ] && multipath "${intf}" off 2>&1 >/dev/null
	global_multipath_settings

	[ -n "$(ubus call system board | jsonfilter -e '@.board_name' | grep '3-model-b')" ] && [ "$(ip link show eth0 | grep UP)" = "" ] && {
		# RPI 3 workaround no network at boot
		ethtool eth0 > /dev/null 2>&1
		ethtool -s eth0 autoneg off > /dev/null 2>&1
		ip link set eth0 up > /dev/null 2>&1
		ethtool -s eth0 autoneg on > /dev/null 2>&1
	}

	mptcpintf=""
	mptcpmintf=""
	master=""
	count=0
	config_load openmptcprouter
	config_foreach interface_max_metric interface
	
	initcwrwnd=""
	config_load network
	#config_foreach remove route
	#config_foreach remove route6
	#config_foreach remove rule
	#config_foreach remove rule6
	#[ -z "$intf" ] && [ -n "$(uci -q get network.@route[-1])" ] && {
	##	logger -t "MPTCP" "Flush main table"
	##	ip route flush table main
	##	ip -6 route flush table main
	#	logger -t "MPTCP" "Flush route cache"
	#	ip route flush cache
	#	ip -6 route flush cache
	#}
	if [ "$(uci -q get network.globals.mptcp_disable_initial_config)" != "1" ]; then
		config_foreach interface_multipath_settings interface $intf
		config_foreach add_route route
		config_foreach add_route6 route6
	fi
	set_multipath
	# If no master is defined, one interface is defined as master
	if [ "$master" = "" ] && [ "$intf" = "" ]; then
		intfmaster="$mptcpmintf"
		[ "$intfmaster" != "" ] && {
			logger -t "MPTCP" "No master multipath defined, setting it to $intfmaster"
			uci -q set network.${intfmaster}.multipath="master"
			uci -q set openmptcprouter.${intfmaster}.multipath="master"
		}
	fi
	uci -q commit network
	uci -q commit openmptcprouter
	[ -n "$(ubus call system board | jsonfilter -e '@.board_name' | grep raspberry)" ] && [ -z "$(ubus call system board | jsonfilter -e '@.board_name' | grep '4-model-b')" ] && {
		ethtool --offload eth0 rx off tx off > /dev/null 2>&1
	}
	[ -n "$(ubus call system board | jsonfilter -e '@.board_name' | grep -i r2s)" ] && {
		ethtool -K eth0 rx off tx off > /dev/null 2>&1
		ethtool -K eth1 rx off tx off > /dev/null 2>&1
	}
}

reload_service() {
	rc_procd start_service "$@"
	return 0
}

service_triggers_load_interface() { ifaces="${ifaces}${1} "; }

service_triggers() {
	local ifaces n
	config_load network; config_foreach service_triggers_load_interface 'interface';
	procd_add_reload_trigger "network"
	procd_open_trigger
		for n in $ifaces; do procd_add_reload_interface_trigger "$n"; procd_add_interface_trigger "interface.*" "$n" /etc/init.d/mptcp reload; done;
	procd_close_trigger
}
