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

START=05

USE_PROCD=1

. /lib/functions/network.sh

omr_intf_del() {
	[ -z "$1" ] && return
	uci -q delete openmptcprouter.$1
}

omr_intf_check() {
	[ "$(uci -q get network.$1)" = "" ] && omr_intf_del $1
}

omr_intf_set() {
	local device
	local ifname
	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
	[ -z "$ifname" ] && ifname=$(ifstatus "$1" | jsonfilter -q -e '@["l3_device"]')
	#config_get devicename "$1" _modem_device
	devicename=${ifname}
	config_get type "$1" type

	if [ "$type" != "macvlan" ] && [ -n "$ifname" ] && [ -f /sys/class/net/${ifname}/device/uevent ]; then
		devicepath=$(readlink -f /sys/class/net/${ifname})
		if [ -n "$devicepath" ] && [ "$(echo ${devicepath} | grep virtual)" = "" ] && [ "$(cat /sys/class/net/${ifname}/device/uevent | grep PRODUCT)" != "" ] && [ "$(cat /sys/class/net/${ifname}/device/uevent | grep PCI_SLOT_NAME)" = "" ]; then
			mac=""
			if [ -f /sys/class/net/${ifname}/address ]; then
				mac="$(cat /sys/class/net/${ifname}/address | tr -d '\n')"
			fi
			uci -q set network.$1.modalias="$(cat /sys/class/net/${ifname}/device/uevent | grep MODALIAS | cut -d '=' -f2 | tr -d '\n')-$mac"
			uci -q set network.$1.product="$(cat /sys/class/net/${ifname}/device/uevent | grep PRODUCT | cut -d '=' -f2 | tr -d '\n')"
		elif [ -n "$devicepath" ] && ([ "$(echo ${devicepath} | grep virtual)" != "" ] || [ "$(echo ${devicepath} | grep virtual)" = "" ] && [ "$(cat /sys/class/net/${ifname}/device/uevent | grep PRODUCT)" = "" ] || [ "$(cat /sys/class/net/${ifname}/device/uevent | grep PCI_SLOT_NAME)" != "" ] || [ "$(cat /sys/class/net/${ifname}tmp/device/uevent)" != "" ]); then
			#uci -q delete network.$1.device
			uci -q delete network.$1.modalias
			uci -q delete network.$1.product
		fi
	elif [ "$type" != "macvlan" ] &&  [ -n "$devicename" ] && [ -f /sys/bus/usb-serial/devices/${devicename}/device/uevent ] && [ "$(cat /sys/class/net/${ifname}/device/uevent | grep PRODUCT)" != "" ]; then
		mac=""
		if [ -f /sys/class/net/${ifname}/address ]; then
			mac="$(cat /sys/class/net/${ifname}/address | tr -d '\n')"
		fi
		uci -q set network.$1.modalias="$(cat /sys/bus/usb-serial/devices/${devicename}/device/uevent | grep MODALIAS | cut -d '=' -f2 | tr -d '\n')-$mac"
		uci -q set network.$1.product="$(cat /sys/bus/usb-serial/devices/${devicename}/device/uevent | grep PRODUCT | cut -d '=' -f2 | tr -d '\n')"
	else
		uci -q delete network.$1.modalias
		uci -q delete network.$1.product
	fi
}

omr_set_settings() {
	local device
	local ifname
	local multipath
	config_get multipath "$1" multipath
	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 device "$1" device
	config_get proto "$1" proto
	config_get type "$1" type
	config_get ip6addr "$1" ip6addr
	config_get ipv6 "$1" ipv6
	config_get addlatency "$1" addlatency
	[ -z "$multipath" ] || [ "$multipath" = "off" ] && return
	[ "$1" = "omrvpn" ] || [ "$1" = "glorytun" ] && return

	uci -q set openmptcprouter.$1=interface
	uci -q set openmptcprouter.$1.multipath="$multipath"
	config_get disable_ipv6 settings disable_ipv6 "0"
	if [ "$disable_ipv6" = "1" ]; then
		uci -q set network.$1.ipv6=0
	fi

	[ -z "$addlatency" ] && addlatency=0
	devicename=$(echo "$device" | cut -d'/' -f3)

	ifname=$(ifstatus "$1" | jsonfilter -q -e '@["l3_device"]')
	[ -z "$ifname" ] && config_get ifname "$1" ifname
	[ -n "$(echo $ifname | grep '@')" ] && ifname=$(ifstatus "$1" | jsonfilter -q -e '@["device"]')

	if [ -n "$ifname" ]; then
		if [ "$proto" = "dhcpv6" ] || [ "$ipv6" = "1" ] || [ -n "$ip6addr" ]; then
			# Change interface name for sysctl in case of VLAN (eth0.2 => eth0/2)
			ifnamesys=$(echo $ifname | sed 's:\.:/:')
			sysctl -qw net.ipv6.conf.${ifnamesys}.disable_ipv6=0
			[ -z "$ip6addr" ] && sysctl -qw net.ipv6.conf.${ifnamesys}.accept_ra=1
		else
			ifnamesys=$(echo $ifname | sed 's:\.:/:')
			sysctl -qw net.ipv6.conf.${ifnamesys}.disable_ipv6=1
			sysctl -qw net.ipv6.conf.${ifnamesys}.accept_ra=0
		fi

		if [ "$addlatency" = "0" ] && [ "$(tc qdisc show dev $ifname | grep delay)" != "" ]; then
			tc qdisc del dev ${ifname} root netem 2>&1 >/dev/null
		fi
		if [ "$addlatency" != "0" ]; then
			if [ "$(tc qdisc show dev $ifname | grep delay)" = "" ]; then
				tc qdisc add dev ${ifname} root netem delay ${addlatency}ms 2>&1 >/dev/null
			elif [ "$(tc qdisc show dev $ifname | awk '/delay/ { print $10 }' | sed 's/ms//')" != "$addlatency" ]; then
				tc qdisc replace dev ${ifname} root netem delay ${addlatency}ms 2>&1 >/dev/null
			fi
		fi
	fi
}

start_service() {
	local scaling_min_freq scaling_max_freq scaling_governor

	config_load openmptcprouter
	config_foreach omr_intf_check interface
	config_load network
	config_foreach omr_set_settings interface
	uci -q commit network
	uci -q commit openmptcprouter

	[ -d /sys/devices/system/cpu/cpufreq/policy0 ] && {
		config_load openmptcprouter
		config_get scaling_min_freq settings scaling_min_freq
		[ -n "$scaling_min_freq" ] && {
			for c in $(ls -d /sys/devices/system/cpu/cpufreq/policy[0-9]*); do
				echo $scaling_min_freq > $c/scaling_min_freq
			done
		}
		config_get scaling_max_freq settings scaling_max_freq
		[ -n "$scaling_max_freq" ] && {
			for c in $(ls -d /sys/devices/system/cpu/cpufreq/policy[0-9]*); do
				echo $scaling_max_freq > $c/scaling_max_freq
			done
		}
		config_get scaling_governor settings scaling_governor
		[ -n "$scaling_governor" ] && {
			for c in $(ls -d /sys/devices/system/cpu/cpufreq/policy[0-9]*); do
				echo $scaling_governor > $c/scaling_governor
			done
		}
	}
	# remove sysctl already defined in /etc/sysctl.d/
	sed -i -e '/tcp_fin_timeout/d' -e '/tcp_keepalive_time/d' -e '/nf_conntrack_max/d' -e '/tcp_syn_retries/d' -e '/tcp_fastopen/d' -e '/tcp_retries2/d' -e '/tcp_retries1/d' -e '/ip_default_ttl/d' /etc/sysctl.conf
	sed -i -e '/tcp_fin_timeout/d' -e '/tcp_keepalive_time/d' -e '/nf_conntrack_max/d' -e '/tcp_syn_retries/d' -e '/tcp_fastopen/d' -e '/tcp_retries2/d' -e '/tcp_retries1/d' -e '/ip_default_ttl/d' /etc/sysctl.d/10-default.conf

	if [ "$(uci -q get openmptcprouter.settings.country)" = "china" ] && [ -n "$(uci -q get dhcp.@dnsmasq[0].server | grep '127.0.0.1#5353')" ]; then
		uci -q batch <<-EOF > /dev/null
			del_list dhcp.@dnsmasq[0].server='127.0.0.1#5353'
			add_list dhcp.@dnsmasq[0].server='114.114.114.114'
			set dhcp.@dnsmasq[0].dnsseccheckunsigned='0'
			delete dhcp.@dnsmasq[0].dnssec
			commit dhcp
		EOF
	elif [ "$(uci -q get openmptcprouter.settings.country)" = "world" ] && [ -n "$(uci -q get dhcp.@dnsmasq[0].server | grep '114.114.114.114')" ]; then
		uci -q batch <<-EOF > /dev/null
			add_list dhcp.@dnsmasq[0].server='127.0.0.1#5353'
			del_list dhcp.@dnsmasq[0].server='114.114.114.114'
			commit dhcp
		EOF
	fi

	if [ "$(uci -q get openmptcprouter.settings.sfe_enabled)" = "1" ]; then
		[ -z "$(lsmod | grep fast_classifier)" ] && modprobe -q fast_classifier 2>&1 >/dev/null
		if [ "$(uci -q get openmptcprouter.settings.sfe_bridge)" = "1" ]; then
			echo 1 >/sys/fast_classifier/skip_to_bridge_ingress
		else
			echo 0 >/sys/fast_classifier/skip_to_bridge_ingress
		fi
		if [ "$(uci -q get openmptcprouter.settings.disable_ipv6)" = "0" ]; then
			sfe_ipv6=$(cat /sys/sfe_ipv6/debug_dev)
			[ ! -f /dev/sfe_ipv6 ] && mknod /dev/sfe_ipv6 c $sfe_ipv6 0 2>&1 >/dev/null
		else
			rm -f /dev/sfe_ipv6
		fi
	elif [ -d "/sys/module/fast_classifier" ]; then
		rmmod fast_classifier 2>&1 >/dev/null
	fi

	if [ "$(uci -q get openmptcprouter.settings.sipalg)" = "0" ]; then
		uci -q batch <<-EOF >/dev/null
			set firewall.zone_lan.auto_helper='0'
			set firewall.zone_wan.auto_helper='0'
			set firewall.zone_vpn.auto_helper='0'
			commit firewall
		EOF
		[ -n "$(lsmod | grep nf_nat_sip)" ] && rmmod nf_nat_sip 2>&1 >/dev/null
		sleep 2
		[ -n "$(lsmod | grep nf_conntrack_sip)" ] && rmmod nf_conntrack_sip 2>&1 >/dev/null
	else
		uci -q batch <<-EOF >/dev/null
			set firewall.zone_lan.auto_helper='1'
			set firewall.zone_wan.auto_helper='1'
			set firewall.zone_vpn.auto_helper='1'
			commit firewall
		EOF
		modprobe -q nf_conntrack_sip 2>&1 >/dev/null
		sleep 2
		modprobe -q nf_nat_sip 2>&1 >/dev/null
	fi

	if [ "$(uci -q get rpcd.@rpcd[0].socket)" != "/var/run/ubus/ubus.sock" ]; then
		uci -q batch <<-EOF >/dev/null
			set rpcd.@rpcd[0].socket='/var/run/ubus/ubus.sock'
			commit rpcd
		EOF
		/etc/init.d/rpcd restart 2>&1 >/dev/null
	fi
	if [ "$(pgrep ModemManager)" = "" ] && [ -f /etc/init.d/modemmanager ] && [ -n "$(uci -q show network | grep modemmanager)" ] && [ "$(uci -q openmptcprouter.settings.modemmanager)" != "0" ]; then
		_log "Can't find ModemManager, start it..."
		/etc/init.d/modemmanager restart
	fi
}

stop_service() {
	config_load network
	config_foreach omr_intf_set interface
	uci -q commit network
	uci -q commit openmptcprouter
}

reload_service() {
	rc_procd start_service
	return 0
}

service_triggers() {
	procd_add_reload_trigger "openmptcprouter" "network"
}