You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1033 lines
41 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091,SC2018,SC2019
  4. PKG_VERSION='dev-test'
  5. export START=94
  6. export USE_PROCD=1
  7. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  8. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  9. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  10. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  11. readonly __PASS__='\033[0;33m[-]\033[0m'
  12. readonly _ERROR_='\033[0;31mERROR\033[0m'
  13. readonly _WARNING_='\033[0;33mWARNING\033[0m'
  14. # readonly readmeURL="https://github.com/openwrt/packages/tree/master/net/vpn-policy-routing/files/README.md"
  15. readonly readmeURL="https://github.com/stangri/openwrt_packages/blob/master/vpn-policy-routing/files/README.md"
  16. export EXTRA_COMMANDS='support'
  17. export EXTRA_HELP=" support Generates output required to troubleshoot routing issues
  18. Use '-d' option for more detailed output
  19. Use '-p' option to automatically upload data under VPR paste.ee account
  20. WARNING: while paste.ee uploads are unlisted, they are still publicly available
  21. List domain names after options to include their lookup in report"
  22. readonly packageName='vpn-policy-routing'
  23. readonly serviceName="$packageName $PKG_VERSION"
  24. readonly PID="/var/run/${packageName}.pid"
  25. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  26. readonly userFile="/etc/${packageName}.user"
  27. create_lock() { [ -e "$PID" ] && return 1; touch "$PID"; }
  28. remove_lock() { [ -e "$PID" ] && rm -f "$PID"; }
  29. trap remove_lock EXIT
  30. output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
  31. output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
  32. output_fail() { s=1; output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
  33. output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
  34. # str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
  35. # str_contains() { [ "$1" != "$(str_replace "$1" "$2" "")" ]; }
  36. # shellcheck disable=SC2018,SC2019
  37. str_to_lower() { echo "$1" | tr 'A-Z' 'a-z'; }
  38. str_extras_to_underscore() { echo "$1" | tr '[\. ~`!@#$%^&*()\+/,<>?//;:]' '_'; }
  39. str_extras_to_space() { echo "$1" | tr ';{}' ' '; }
  40. output() {
  41. # Can take a single parameter (text) to be output at any verbosity
  42. # Or target verbosity level and text to be output at specifc verbosity
  43. local msg
  44. if [ $# -ne 1 ]; then
  45. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  46. fi
  47. [ -t 1 ] && printf "%b" "$1"
  48. msg="${1//$serviceName /service }";
  49. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  50. logger -t "${packageName:-service} [$$]" "$(printf "%b" "${logmsg}${msg}")"
  51. logmsg=''
  52. else
  53. logmsg="${logmsg}${msg}"
  54. fi
  55. }
  56. is_installed() { [ -s "/usr/lib/opkg/info/${1}.control" ]; }
  57. export serviceEnabled verbosity strictMode wanTableID wanMark fwMask
  58. export ipv6Enabled localIpset remoteIpset ipruleEnabled icmpIface
  59. export ignoredIfaces="" supportedIfaces=""
  60. export appendLocalPolicy="" appendRemotePolicy=""
  61. export wanIface4 wanIface6 ifaceMark ifaceTableID ifAll ifSupported wanGW4 wanGW6
  62. export bootTimeout insertOption
  63. list_iface() { ifAll="${ifAll}${1} "; }
  64. list_supported_iface() { is_supported_interface "$1" && ifSupported="${ifSupported}${1} "; }
  65. vpr_find_true() {
  66. local iface i param="$2"
  67. [ "$param" = 'wan6' ] || param='wan'
  68. "network_find_${param}" iface
  69. is_tunnel "$iface" && unset iface
  70. if [ -z "$iface" ]; then
  71. unset ifAll; config_load 'network';
  72. config_foreach list_iface 'interface'
  73. for i in $ifAll; do
  74. if "is_${param}" "$i"; then break; else unset i; fi
  75. done
  76. fi
  77. export "$1=${iface:-$i}"
  78. }
  79. vpr_get_gateway() {
  80. local iface="$2" dev="$3" gw
  81. network_get_gateway gw "$iface"
  82. if [ -z "$gw" ] || [ "$gw" = '0.0.0.0' ]; then
  83. gw="$(ip -4 a list dev "$dev" 2>/dev/null | grep inet | awk '{print $2}' | awk -F "/" '{print $1}')"
  84. fi
  85. export "$1=$gw"
  86. }
  87. vpr_get_gateway6() {
  88. local iface="$2" dev="$3" gw
  89. network_get_gateway6 gw "$iface"
  90. if [ -z "$gw" ] || [ "$gw" = '::/0' ] || [ "$gw" = '::0/0' ] || [ "$gw" = '::' ]; then
  91. gw="$(ip -6 a list dev "$dev" 2>/dev/null | grep inet6 | awk '{print $2}')"
  92. fi
  93. export "$1=$gw"
  94. }
  95. is_l2tp() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:4}" = "l2tp" ]; }
  96. is_oc() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:11}" = "openconnect" ]; }
  97. is_ovpn() { local dev; dev=$(uci -q get network."$1".ifname); [ "${dev:0:3}" = "tun" ] || [ "${dev:0:3}" = "tap" ] || [ -f "/sys/devices/virtual/net/${dev}/tun_flags" ]; }
  98. is_pptp() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:4}" = "pptp" ]; }
  99. is_tor() { local dev; dev=$(uci -q get network."$1".ifname); [ "${dev:0:3}" = "tor" ]; }
  100. is_wg() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:9}" = "wireguard" ]; }
  101. is_tunnel() { is_l2tp "$1" || is_oc "$1" || is_ovpn "$1" || is_pptp "$1" || is_tor "$1" || is_wg "$1"; }
  102. is_wan() { [ "$1" = "$wanIface4" ] || { [ "${1##wan}" != "$1" ] && [ "${1##wan6}" = "$1" ]; } || [ "${1%%wan}" != "$1" ]; }
  103. is_wan6() { [ -n "$wanIface6" ] && [ "$1" = "$wanIface6" ] || [ "${1/#wan6}" != "$1" ] || [ "${1/%wan6}" != "$1" ]; }
  104. string_match_word() { echo "$1" | grep -q -w "$2"; }
  105. is_ignored_interface() { string_match_word "$ignoredIfaces" "$1"; }
  106. is_supported_interface() { string_match_word "$supportedIfaces" "$1" || { ! is_ignored_interface "$1" && { is_wan "$1" || is_wan6 "$1" || is_tunnel "$1"; }; }; }
  107. is_mac_address() { expr "$1" : '[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]$' >/dev/null; }
  108. is_ipv4() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; }
  109. is_ipv6() { ! is_mac_address "$1" && [ "${1//:}" != "$1" ]; }
  110. is_ipv6_link_local() { [ "${1:0:4}" = "fe80" ]; }
  111. is_ipv6_unique_local() { [ "${1:0:2}" = "fc" ] || [ "${1:0:2}" = "fd" ]; }
  112. is_ipv6_global() { [ "${1:0:4}" = "2001" ]; }
  113. # is_ipv6_global() { is_ipv6 "$1" && ! is_ipv6_link_local "$1" && ! is_ipv6_link_local "$1"; }
  114. is_netmask() { local ip="${1%/*}"; [ "$ip" != "$1" ] && is_ipv4 "$ip"; }
  115. is_domain() { [ "${1//[a-zA-Z-]}" != "$1" ]; }
  116. is_phys_dev() { [ "${1:0:1}" = "@" ] && ip -4 r | grep -q "^${1:1}"; }
  117. is_turris() { /bin/ubus -S call system board | /bin/grep 'Turris' | /bin/grep -q '15.05'; }
  118. is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
  119. dnsmasq_kill() { killall -q -HUP dnsmasq; }
  120. dnsmasq_restart() { output 1 'Restarting DNSMASQ '; if /etc/init.d/dnsmasq restart >/dev/null 2>&1; then output_okn; else output_failn; fi; }
  121. is_default_dev() { [ "$1" = "$(ip -4 r | grep -m1 'dev' | grep -Eso 'dev [^ ]*' | awk '{print $2}')" ]; }
  122. is_supported_iface_dev() {
  123. for n in $ifSupported; do
  124. if [ "$1" = "$(uci -q get "network.${n}.ifname" || echo "$n")" ] || [ "$1" = "$(uci -q get "network.${n}.proto")-${n}" ] ; then return 0; fi
  125. done
  126. return 1
  127. }
  128. is_supported_protocol () { grep -o '^[^#]*' /etc/protocols | grep -w -v '0' | grep . | awk '{print $1}' | grep -q "$1"; }
  129. load_package_config() {
  130. config_load "$packageName"
  131. config_get_bool serviceEnabled 'config' 'enabled' 0
  132. config_get_bool strictMode 'config' 'strict_enforcement' 1
  133. config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
  134. config_get_bool localIpset 'config' 'src_ipset' 0
  135. config_get_bool ipruleEnabled 'config' 'iprule_enabled' 0
  136. config_get remoteIpset 'config' 'dest_ipset'
  137. config_get appendLocalPolicy 'config' 'append_src_rules'
  138. config_get appendRemotePolicy 'config' 'append_dest_rules'
  139. config_get verbosity 'config' 'verbosity' '2'
  140. config_get wanTableID 'config' 'wan_tid' '201'
  141. config_get wanMark 'config' 'wan_mark' '0x010000'
  142. config_get fwMask 'config' 'fw_mask' '0xff0000'
  143. config_get icmpIface 'config' 'icmp_interface'
  144. config_get ignoredIfaces 'config' 'ignored_interface'
  145. config_get supportedIfaces 'config' 'supported_interface'
  146. config_get bootTimeout 'config' 'boot_timeout' '30'
  147. config_get insertOption 'config' 'iptables_rule_option' 'append'
  148. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  149. verbosity=1
  150. fi
  151. . /lib/functions/network.sh
  152. . /usr/share/libubox/jshn.sh
  153. vpr_find_true wanIface4 'wan'
  154. [ "$ipv6Enabled" -ne 0 ] && vpr_find_true wanIface6 'wan6'
  155. [ -n "$wanIface4" ] && network_get_gateway wanGW4 "$wanIface4"
  156. [ -n "$wanIface6" ] && network_get_gateway6 wanGW6 "$wanIface6"
  157. wanGW="${wanGW4:-$wanGW6}"
  158. }
  159. is_enabled() {
  160. load_package_config
  161. if [ "$serviceEnabled" -eq 0 ]; then
  162. if [ "$1" = 'on_start' ]; then
  163. output "$packageName is currently disabled.\\n"
  164. output "Run the following commands before starting service again:\\n"
  165. output "uci set $packageName.config.enabled='1'; uci commit;\\n"
  166. fi
  167. return 1
  168. fi
  169. case $insertOption in
  170. insert|-i|-I) insertOption='-I';;
  171. append|-a|-A|*) insertOption='-A';;
  172. esac
  173. case $remoteIpset in
  174. ipset)
  175. if ! ipset help hash:net >/dev/null 2>&1; then
  176. output "$_ERROR_: ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  177. unset remoteIpset
  178. fi
  179. ;;
  180. dnsmasq.ipset)
  181. if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
  182. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
  183. unset remoteIpset
  184. fi
  185. if ! ipset help hash:net >/dev/null 2>&1; then
  186. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  187. unset remoteIpset
  188. fi
  189. ;;
  190. *) unset remoteIpset;;
  191. esac
  192. if [ "$localIpset" -ne 0 ]; then
  193. if ! ipset help hash:net >/dev/null 2>&1; then
  194. output "$_ERROR_: Local ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  195. unset localIpset
  196. fi
  197. if ! ipset help hash:mac >/dev/null 2>&1; then
  198. output "$_ERROR_: Local ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:mac' type!\\n"
  199. unset localIpset
  200. fi
  201. fi
  202. }
  203. is_wan_up() {
  204. local sleepCount=1
  205. while [ -z "$wanGW" ] ; do
  206. vpr_find_true wanIface4 'wan'
  207. [ "$ipv6Enabled" -ne 0 ] && vpr_find_true wanIface6 'wan6'
  208. [ -n "$wanIface4" ] && network_get_gateway wanGW4 "$wanIface4"
  209. [ -n "$wanIface6" ] && network_get_gateway6 wanGW6 "$wanIface6"
  210. wanGW="${wanGW4:-$wanGW6}"
  211. if [ $((sleepCount)) -gt $((bootTimeout)) ] || [ -n "$wanGW" ]; then break; fi
  212. output "$serviceName waiting for wan gateway...\\n"; sleep 1; network_flush_cache; sleepCount=$((sleepCount+1));
  213. done
  214. mkdir -p "${PID%/*}"; mkdir -p "${dnsmasqFile%/*}";
  215. unset ifSupported
  216. config_load 'network'
  217. config_foreach list_supported_iface 'interface'
  218. if [ -n "$wanGW" ]; then
  219. return 0
  220. else
  221. output "$_ERROR_: $serviceName failed to discover WAN gateway!\\n"
  222. return 1
  223. fi
  224. }
  225. ipt_cleanup() {
  226. local i
  227. for i in PREROUTING FORWARD INPUT OUTPUT; do
  228. while iptables -t mangle -D $i -m mark --mark 0x0/0xff0000 -j VPR_${i} >/dev/null 2>&1; do : ; done
  229. done
  230. }
  231. # shellcheck disable=SC2086
  232. ipt() {
  233. local d failFlagIpv4=1 failFlagIpv6=1
  234. for d in "${*//-A/-D}" "${*//-I/-D}" "${*//-N/-F}" "${*//-N/-X}"; do
  235. [ "$d" != "$*" ] && { iptables $d >/dev/null 2>&1; ip6tables $d >/dev/null 2>&1; }
  236. done
  237. d="$*"; iptables $d >/dev/null 2>&1 && failFlagIpv4=0;
  238. if [ "$ipv6Enabled" -gt 0 ]; then ip6tables $d >/dev/null 2>&1 && failFlagIpv6=0; fi
  239. [ "$failFlagIpv4" -eq 0 ] || [ "$failFlagIpv6" -eq 0 ]
  240. }
  241. # shellcheck disable=SC2086
  242. ips() {
  243. local command="$1" ipset="${2//-/_}" param="$3" comment="$4" appendix failFlag=0
  244. if [ "${ipset//_ip}" != "${ipset}" ]; then
  245. ipset="${ipset//_ip}"; appendix='_ip';
  246. elif [ "${ipset//_mac}" != "${ipset}" ]; then
  247. ipset="${ipset//_mac}"; appendix='_mac';
  248. fi
  249. if [ "$command" = "add_dnsmasq" ]; then
  250. [ "$remoteIpset" != 'dnsmasq.ipset' ] && return 1
  251. # elif [ "$command" = "add_unbound" ]; then
  252. # [ "$remoteIpset" != 'unbound.ipset' ] && return 1
  253. else
  254. if [[ -z "$appendix" && -z "$remoteIpset" ]] || \
  255. [[ -n "$appendix" && "$localIpset" -eq 0 ]]; then
  256. return 1
  257. fi
  258. fi
  259. case "$command" in
  260. add_dnsmasq)
  261. echo "ipset=/${param}/${ipset} # $comment" >> "$dnsmasqFile" || failFlag=1
  262. ;;
  263. add)
  264. ipset -q -! $command "${ipset}${appendix}" $param comment "$comment" || failFlag=1
  265. ;;
  266. create)
  267. ipset -q -! "$command" "${ipset}${appendix}" $param || failFlag=1
  268. ;;
  269. destroy|flush)
  270. ipset -q -! "$command" "${ipset}${appendix}" 2>/dev/null || failFlag=1
  271. return 0
  272. ;;
  273. esac
  274. return $failFlag
  275. }
  276. ipr()
  277. {
  278. [ "$ipruleEnabled" -ne 0 ] || return 1
  279. local comment="$1" tid=$(eval echo "\$tid_${2//-/_}") laddr="$3" failFlagIpv4=0 failFlagIpv6=1
  280. ip -4 rule del from "$laddr" table "$tid" >/dev/null 2>&1
  281. ip -4 rule add from "$laddr" table "$tid" >/dev/null 2>&1 || failFlagIpv4=1
  282. if [ "$ipv6Enabled" -ne 0 ]; then
  283. ip -6 rule del from "$laddr" table "$tid" >/dev/null 2>&1
  284. ip -6 rule add from "$laddr" table "$tid" >/dev/null 2>&1 && failFlagIpv6=0
  285. fi
  286. if [ "$failFlagIpv4" -eq 0 ] || [ "$failFlagIpv6" -eq 0 ]; then return 0; else return 1; fi
  287. }
  288. insert_tor_policy() {
  289. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto="$7" chain="${8:-PREROUTING}"
  290. local mark=$(eval echo "\$mark_${iface//-/_}")
  291. [ -z "$mark" ] && processPolicyError="${processPolicyError}${_ERROR_}: Unknown fw_mark for ${iface}##"
  292. param="-t mangle $insertOption VPR_${chain} 1 -j MARK --set-xmark ${mark}/${fwMask}"
  293. [ -n "$laddr" ] && param="$param -s $laddr"
  294. [ -n "$lport" ] && param="$param -p tcp -m multiport --sport ${lport//-/:}"
  295. [ -n "$raddr" ] && param="$param -d $raddr"
  296. [ -n "$rport" ] && param="$param -p $proto -m multiport --dport ${rport//-/:}"
  297. [ -n "$comment" ] && param="$param -m comment --comment $(str_extras_to_underscore "$comment")"
  298. # Here be dragons
  299. return 0
  300. }
  301. insert_policy() {
  302. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto="$(str_to_lower "$7")" chain="${8:-PREROUTING}"
  303. local mark=$(eval echo "\$mark_${iface//-/_}") param i valueNeg value
  304. if [ "$ipv6Enabled" -eq 0 ]; then
  305. is_ipv6 "$laddr" && return 0
  306. is_ipv6 "$raddr" && return 0
  307. fi
  308. if [ -z "$mark" ]; then
  309. processPolicyError="${processPolicyError}${_ERROR_}: Unknown fw_mark for ${iface}##"
  310. return 0
  311. fi
  312. if [ -z "$proto" ]; then
  313. if [ -n "$lport" ] || [ -n "$rport" ]; then
  314. proto='tcp udp'
  315. else
  316. proto='all'
  317. fi
  318. fi
  319. for i in $proto; do
  320. if [ "$i" = 'all' ]; then
  321. param="-t mangle -I VPR_${chain} -j MARK --set-xmark ${mark}/${fwMask}"
  322. elif ! is_supported_protocol "$i"; then
  323. processPolicyError="${processPolicyError}${_ERROR_}: Unknown protocol '$i' in policy '$comment'##"
  324. return 0
  325. else
  326. param="-t mangle -I VPR_${chain} -j MARK --set-xmark ${mark}/${fwMask} -p $i"
  327. fi
  328. if [ -n "$laddr" ]; then
  329. if [ "${laddr:0:1}" = "!" ]; then
  330. valueNeg='!'; value="${laddr:1}"
  331. else
  332. unset valueNeg; value="$laddr";
  333. fi
  334. if is_phys_dev "$value"; then
  335. param="$param $valueNeg -i ${value:1}"
  336. elif is_mac_address "$value"; then
  337. param="$param -m mac $valueNeg --mac-source $value"
  338. elif [ "${appendLocalPolicy//-d}" != "$appendLocalPolicy" ] && [ -n "$raddr" ]; then
  339. param="$param $valueNeg -s $value"
  340. processPolicyError="${processPolicyError}${_ERROR_}: Cannot append '$comment' policy with '$appendLocalPolicy' as destination is already set to '$raddr'##"
  341. else
  342. param="$param $valueNeg -s $value $appendLocalPolicy"
  343. fi
  344. fi
  345. if [ -n "$lport" ]; then
  346. if [ "${lport:0:1}" = "!" ]; then
  347. valueNeg='!'; value="${lport:1}"
  348. else
  349. unset valueNeg; value="$lport";
  350. fi
  351. param="$param -m multiport $valueNeg --sport ${value//-/:}"
  352. fi
  353. if [ -n "$raddr" ]; then
  354. if [ "${raddr:0:1}" = "!" ]; then
  355. valueNeg='!'; value="${raddr:1}"
  356. else
  357. unset valueNeg; value="$raddr";
  358. fi
  359. if [ "${appendRemotePolicy//-s}" != "$appendRemotePolicy" ] && [ -n "$laddr" ]; then
  360. param="$param $valueNeg -d $value"
  361. processPolicyError="${processPolicyError}${_ERROR_}: Cannot append '$comment' policy with '$appendRemotePolicy' as source is already set to '$laddr'\\n"
  362. else
  363. param="$param $valueNeg -d $value $appendRemotePolicy"
  364. fi
  365. fi
  366. if [ -n "$rport" ]; then
  367. if [ "${rport:0:1}" = "!" ]; then
  368. valueNeg='!'; value="${rport:1}"
  369. else
  370. unset valueNeg; value="$rport";
  371. fi
  372. param="$param -m multiport $valueNeg --dport ${value//-/:}"
  373. fi
  374. [ -n "$comment" ] && param="$param -m comment --comment $(str_extras_to_underscore "$comment")"
  375. ipt "$param" || processPolicyError="${processPolicyError}${_ERROR_}: iptables $param\\n"
  376. done
  377. return 0
  378. }
  379. r_process_policy(){
  380. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto="$7" chain="$8" resolved_laddr resolved_raddr i ipsFailFlag
  381. if [ "${laddr//[ ;\{\}]/}" != "$laddr" ]; then
  382. for i in $(str_extras_to_space "$laddr"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$i" "$lport" "$raddr" "$rport" "$proto" "$chain"; done
  383. return 0
  384. elif [ "${lport//[ ;\{\}]/}" != "$lport" ]; then
  385. for i in $(str_extras_to_space "$lport"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$i" "$raddr" "$rport" "$proto" "$chain"; done
  386. return 0
  387. elif [ "${raddr//[ ;\{\}]/}" != "$raddr" ]; then
  388. for i in $(str_extras_to_space "$raddr"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$lport" "$i" "$rport" "$proto" "$chain"; done
  389. return 0
  390. elif [ "${rport//[ ;\{\}]/}" != "$rport" ]; then
  391. for i in $(str_extras_to_space "$rport"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$i" "$proto" "$chain"; done
  392. return 0
  393. fi
  394. # start non-recursive processing
  395. # process TOR, netmask, physical device and mac-address separately, so we don't send them to resolveip
  396. if is_tor "$iface"; then
  397. insert_tor_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  398. elif is_phys_dev "$laddr"; then
  399. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  400. elif [ -n "$laddr" ] && [ -z "${lport}${raddr}${rport}" ] && [ "$chain" = 'PREROUTING' ]; then
  401. if is_mac_address "$laddr"; then
  402. if [ -n "$proto" ] && [ "$proto" != 'all' ] && [ "$localIpset" -ne 0 ]; then
  403. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy '$comment', mac-address '$laddr'\\n"
  404. fi
  405. ips 'add' "${iface}_mac" "$laddr" "${comment}: $laddr" || ipsFailFlag=1
  406. else
  407. if [ -n "$proto" ] && [ "$proto" != "all" ] && [ "$localIpset" -ne 0 ]; then
  408. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy '$comment', address '$laddr'\\n"
  409. fi
  410. if ! ips 'add' "${iface}_ip" "$laddr" "${comment}: $laddr"; then
  411. ipr "$comment" "$iface" "$i" || ipsFailFlag=1
  412. fi
  413. fi
  414. elif [ -n "$raddr" ] && [ -z "${laddr}${lport}${rport}" ] && [ "$chain" = 'PREROUTING' ] && [ -n "$remoteIpset" ]; then
  415. if [ -n "$proto" ] && [ "$proto" != 'all' ]; then
  416. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy '$comment', domain '$raddr'\\n"
  417. fi
  418. case "$remoteIpset" in
  419. ipset)
  420. ips 'add' "${iface}" "$raddr" "${comment}: $raddr" || ipsFailFlag=1;;
  421. dnsmasq.ipset)
  422. if is_domain "$raddr"; then ips 'add_dnsmasq' "${iface}" "$raddr" "${comment}" || ipsFailFlag=1
  423. else ips 'add' "${iface}" "$raddr" "${comment}: $raddr" || ipsFailFlag=1; fi;;
  424. esac
  425. else
  426. ipsFailFlag=1
  427. fi
  428. if [ -n "$ipsFailFlag" ]; then
  429. if is_mac_address "$laddr"; then
  430. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  431. elif is_netmask "$laddr" || is_netmask "$raddr"; then
  432. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  433. else
  434. [ -n "$laddr" ] && resolved_laddr="$(resolveip "$laddr")"
  435. [ -n "$raddr" ] && resolved_raddr="$(resolveip "$raddr")"
  436. if [ -n "$resolved_laddr" ] && [ "$resolved_laddr" != "$laddr" ]; then
  437. for i in $resolved_laddr; do [ -n "$i" ] && r_process_policy "$comment $laddr" "$iface" "$i" "$lport" "$raddr" "$rport" "$proto" "$chain"; done
  438. elif [ -n "$resolved_raddr" ] && [ "$resolved_raddr" != "$raddr" ]; then
  439. for i in $resolved_raddr; do [ -n "$i" ] && r_process_policy "$comment $raddr" "$iface" "$laddr" "$lport" "$i" "$rport" "$proto" "$chain"; done
  440. else
  441. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  442. fi
  443. fi
  444. fi
  445. }
  446. process_policy(){
  447. local name comment iface laddr lport raddr rport param mark processPolicyError processPolicyWarning proto chain enabled
  448. config_get comment "$1" 'comment'
  449. config_get name "$1" 'name' 'blank'
  450. config_get iface "$1" 'interface'
  451. config_get laddr "$1" 'src_addr'
  452. config_get lport "$1" 'src_port'
  453. config_get raddr "$1" 'dest_addr'
  454. config_get rport "$1" 'dest_port'
  455. config_get proto "$1" 'proto'
  456. config_get chain "$1" 'chain' 'PREROUTING'
  457. config_get_bool enabled "$1" 'enabled' 1
  458. [ "$enabled" -gt 0 ] || return 0
  459. proto="$(str_to_lower "$proto")"
  460. [ "$proto" = 'auto' ] && unset proto
  461. comment="${comment:-$name}"
  462. output 2 "Routing '$comment' via $iface "
  463. if [ -z "$comment" ]; then
  464. errorSummary="${errorSummary}${_ERROR_}: Policy name is empty\\n"
  465. output_fail; return 1;
  466. fi
  467. if [ -z "${laddr}${lport}${raddr}${rport}" ]; then
  468. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' missing all IPs/ports\\n"
  469. output_fail; return 1;
  470. fi
  471. if [ -z "$iface" ]; then
  472. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' has no assigned interface\\n"
  473. output_fail; return 1;
  474. fi
  475. if ! is_supported_interface "$iface"; then
  476. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' has unknown interface: '${iface}'\\n"
  477. output_fail; return 1;
  478. fi
  479. lport="${lport// / }"; lport="${lport// /,}"; lport="${lport//,\!/ !}";
  480. rport="${rport// / }"; rport="${rport// /,}"; rport="${rport//,\!/ !}";
  481. r_process_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  482. if [ -n "$processPolicyWarning" ]; then
  483. warningSummary="${warningSummary}${processPolicyWarning}\\n"
  484. fi
  485. if [ -n "$processPolicyError" ]; then
  486. output_fail
  487. errorSummary="${errorSummary}${processPolicyError}\\n"
  488. else
  489. output_ok
  490. fi
  491. }
  492. table_destroy(){
  493. local tid="$1" iface="$2" mark="$3"
  494. if [ -n "$tid" ] && [ -n "$iface" ] && [ -n "$mark" ]; then
  495. ip -4 rule del fwmark "$mark" table "$tid" >/dev/null 2>&1
  496. ip -6 rule del fwmark "$mark" table "$tid" >/dev/null 2>&1
  497. ip -4 rule del table "$tid" >/dev/null 2>&1
  498. ip -6 rule del table "$tid" >/dev/null 2>&1
  499. ip -4 route flush table "$tid";
  500. ip -6 route flush table "$tid";
  501. ips 'flush' "${iface}"; ips 'destroy' "${iface}";
  502. ips 'flush' "${iface}_ip"; ips 'destroy' "${iface}_ip";
  503. ips 'flush' "${iface}_mac"; ips 'destroy' "${iface}_mac";
  504. ip -4 route flush cache
  505. ip -6 route flush cache
  506. return 0
  507. else
  508. return 1
  509. fi
  510. }
  511. # shellcheck disable=SC2086
  512. table_create(){
  513. local tid="$1" mark="$2" iface="$3" gw4="$4" dev="$5" gw6="$6" dev6="$7" dscp s=0 i ipv4_error=0 ipv6_error=0
  514. if [ -z "$tid" ] || [ -z "$mark" ] || [ -z "$iface" ]; then
  515. return 1
  516. fi
  517. table_destroy "$tid" "$iface" "$mark"
  518. if [ -n "$gw4" ] || [ "$strictMode" -ne 0 ]; then
  519. if [ -z "$gw4" ]; then
  520. ip -4 route add unreachable default table "$tid" >/dev/null 2>&1 || ipv4_error=1
  521. else
  522. ip -4 route add default via "$gw4" dev "$dev" table "$tid" >/dev/null 2>&1 || ipv4_error=1
  523. fi
  524. ip -4 route ls table main | grep -v 'br-lan' | while read -r i; do
  525. idev="$(echo "$i" | grep -Eso 'dev [^ ]*' | awk '{print $2}')"
  526. if ! is_supported_iface_dev "$idev"; then
  527. ip -4 route add $i table "$tid" >/dev/null 2>&1 || ipv4_error=1
  528. fi
  529. done
  530. ip -4 route flush cache || ipv4_error=1
  531. ip -4 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv4_error=1
  532. fi
  533. if [ "$ipv6Enabled" -ne 0 ]; then
  534. if { [ -n "$gw6" ] && [ "$gw6" != "::/0" ]; } || [ "$strictMode" -ne 0 ]; then
  535. if [ -z "$gw6" ] || [ "$gw6" = "::/0" ]; then
  536. ip -6 route add unreachable default table "$tid" || ipv6_error=1
  537. else
  538. ip -6 route ls table main | grep " dev $dev6 " | while read -r i; do
  539. ip -6 route add $i table "$tid" >/dev/null 2>&1 || ipv6_error=1
  540. done
  541. fi
  542. ip -6 route flush cache || ipv6_error=1
  543. ip -6 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv6_error=1
  544. fi
  545. fi
  546. if [ $ipv4_error -eq 0 ] || [ $ipv6_error -eq 0 ]; then
  547. dscp="$(uci -q get "${packageName}".config."${iface}"_dscp)"
  548. if [ "${dscp:-0}" -ge 1 ] && [ "${dscp:-0}" -le 63 ]; then
  549. ipt -t mangle -I VPR_PREROUTING -m dscp --dscp "${dscp}" -j MARK --set-xmark "${mark}/${fwMask}" || s=1
  550. fi
  551. if [ -n "$remoteIpset" ]; then
  552. if ips 'create' "${iface}" 'hash:net comment' && ips 'flush' "${iface}"; then
  553. ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}" dst $appendRemotePolicy -j MARK --set-xmark "${mark}/${fwMask}" || s=1
  554. else
  555. s=1
  556. fi
  557. fi
  558. if [ "$localIpset" -ne 0 ]; then
  559. if ips 'create' "${iface}_ip" 'hash:net comment' && ips 'flush' "${iface}_ip"; then
  560. ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}_ip" src $appendLocalPolicy -j MARK --set-mark "${mark}/${fwMask}" || s=1
  561. else
  562. s=1
  563. fi
  564. if ips 'create' "${iface}_mac" 'hash:mac comment' && ips 'flush' "${iface}_mac"; then
  565. ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}_mac" src $appendLocalPolicy -j MARK --set-mark "${mark}/${fwMask}" || s=1
  566. else
  567. s=1
  568. fi
  569. fi
  570. if [ "$iface" = "$icmpIface" ]; then
  571. ipt -t mangle -I VPR_OUTPUT -p icmp -j MARK --set-xmark "${mark}/${fwMask}" || s=1
  572. fi
  573. else
  574. s=1
  575. fi
  576. return $s
  577. }
  578. process_interface(){
  579. local gw4 gw6 dev dev6 s=0 dscp iface="$1" action="$2" displayText
  580. is_supported_interface "$iface" || return 0
  581. is_wan6 "$iface" && return 0
  582. [ $((ifaceMark)) -gt $((fwMask)) ] && return 1
  583. network_get_device dev "$iface"
  584. [ -z "$dev" ] && config_get dev "$iface" 'ifname'
  585. if is_wan "$iface" && [ -n "$wanIface6" ]; then
  586. network_get_device dev6 "$wanIface6"
  587. [ -z "$dev6" ] && config_get dev6 "$wanIface6" 'ifname'
  588. fi
  589. [ -z "$dev6" ] && dev6="$dev"
  590. [ -z "$ifaceTableID" ] && ifaceTableID="$wanTableID"; [ -z "$ifaceMark" ] && ifaceMark="$wanMark";
  591. case "$action" in
  592. destroy)
  593. table_destroy "${ifaceTableID}" "${iface}" "${ifaceMark}"
  594. ifaceTableID="$((ifaceTableID + 1))"; ifaceMark="$(printf '0x%06x' $((ifaceMark + wanMark)))";
  595. ;;
  596. create)
  597. export "mark_${iface//-/_}=$ifaceMark"; export "tid_${iface//-/_}=$ifaceTableID";
  598. table_destroy "${ifaceTableID}" "${iface}"
  599. vpr_get_gateway gw4 "$iface" "$dev"
  600. vpr_get_gateway6 gw6 "$iface" "$dev6"
  601. if [ "$iface" = "$dev" ]; then
  602. displayText="${iface}/${gw4:-0.0.0.0}"
  603. else
  604. displayText="${iface}/${dev}/${gw4:-0.0.0.0}"
  605. fi
  606. [ "$ipv6Enabled" -ne 0 ] && displayText="${displayText}/${gw6:-::/0}"
  607. output 2 "Creating table '$displayText' "
  608. is_default_dev "$dev" && displayText="${displayText} ${__OK__}"
  609. if table_create "$ifaceTableID" "$ifaceMark" "$iface" "$gw4" "$dev" "$gw6" "$dev6"; then
  610. gatewaySummary="${gatewaySummary}${displayText}\\n"
  611. output_ok
  612. else
  613. errorSummary="${errorSummary}${_ERROR_}: Failed to set up '$displayText'\\n"
  614. output_fail
  615. fi
  616. ifaceTableID="$((ifaceTableID + 1))"; ifaceMark="$(printf '0x%06x' $((ifaceMark + wanMark)))";
  617. ;;
  618. esac
  619. return $s
  620. }
  621. convert_config(){
  622. local i
  623. [ -s "/etc/config/${packageName}" ] || return 0
  624. sed -i 's/ignored_interfaces/ignored_interface/g' "/etc/config/${packageName}"
  625. sed -i 's/supported_interfaces/supported_interface/g' "/etc/config/${packageName}"
  626. sed -i 's/local_addresses/local_address/g' "/etc/config/${packageName}"
  627. sed -i 's/local_ports/local_port/g' "/etc/config/${packageName}"
  628. sed -i 's/remote_addresses/remote_address/g' "/etc/config/${packageName}"
  629. sed -i 's/remote_ports/remote_port/g' "/etc/config/${packageName}"
  630. sed -i 's/ipset_enabled/remote_ipset/g' "/etc/config/${packageName}"
  631. sed -i 's/dnsmasq_enabled/dnsmasq_ipset/g' "/etc/config/${packageName}"
  632. sed -i 's/enable_control/webui_enable_column/g' "/etc/config/${packageName}"
  633. sed -i 's/proto_control/webui_protocol_column/g' "/etc/config/${packageName}"
  634. sed -i 's/chain_control/webui_chain_column/g' "/etc/config/${packageName}"
  635. sed -i 's/sort_control/webui_sorting/g' "/etc/config/${packageName}"
  636. sed -i 's/local_address/src_addr/g' "/etc/config/${packageName}"
  637. sed -i 's/local_port/src_port/g' "/etc/config/${packageName}"
  638. sed -i 's/remote_address/dest_addr/g' "/etc/config/${packageName}"
  639. sed -i 's/remote_port/dest_port/g' "/etc/config/${packageName}"
  640. sed -i 's/append_local_rules/append_src_rules/g' "/etc/config/${packageName}"
  641. sed -i 's/append_remote_rules/append_dest_rules/g' "/etc/config/${packageName}"
  642. sync
  643. config_load "$packageName"
  644. config_get_bool dnsmasqIpset 'config' 'dnsmasq_ipset' 0
  645. config_get remoteIpset 'config' 'remote_ipset'
  646. config_get webuiProtocol 'config' 'webui_supported_protocol'
  647. # shellcheck disable=SC2154
  648. if [ "$dnsmasqIpset" = "1" ]; then
  649. remoteIpset="dnsmasq.ipset";
  650. elif [ "$remoteIpset" = "1" ]; then
  651. remoteIpset="ipset";
  652. elif [ "$remoteIpset" = "0" ]; then
  653. remoteIpset=""
  654. fi
  655. uci -q del "$packageName.config.dnsmasq_ipset"
  656. uci -q set "$packageName".config.remote_ipset="$remoteIpset"
  657. # shellcheck disable=SC2154
  658. if [ -z "$webuiProtocol" ]; then
  659. uci add_list "$packageName".config.webui_supported_protocol='tcp'
  660. uci add_list "$packageName".config.webui_supported_protocol='udp'
  661. uci add_list "$packageName".config.webui_supported_protocol='tcp udp'
  662. uci add_list "$packageName".config.webui_supported_protocol='icmp'
  663. uci add_list "$packageName".config.webui_supported_protocol='all'
  664. fi
  665. uci commit "$packageName"
  666. sed -i 's/local_ipset/src_ipset/g' "/etc/config/${packageName}"
  667. sed -i 's/remote_ipset/dest_ipset/g' "/etc/config/${packageName}"
  668. for i in udp_proto_enabled forward_chain_enabled input_chain_enabled output_chain_enabled; do
  669. grep -q "$i" "/etc/config/${packageName}" && output "${_WARNING_}: $i setting is not supported in ${serviceName}.\\n"
  670. done
  671. }
  672. check_config(){ local en; config_get_bool en "$1" 'enabled' 1; [ "$en" -gt 0 ] && _cfg_enabled=0; }
  673. is_config_enabled(){
  674. local cfg="$1" _cfg_enabled=1
  675. [ -n "$1" ] || return 1
  676. config_load "$packageName"
  677. config_foreach check_config "$cfg"
  678. return "$_cfg_enabled"
  679. }
  680. process_user_file(){
  681. local path enabled shellBin="${SHELL:-/bin/ash}"
  682. config_get_bool enabled "$1" 'enabled' 1
  683. config_get path "$1" 'path'
  684. [ "$enabled" -gt 0 ] || return 0
  685. if [ ! -s "$path" ]; then
  686. errorSummary="${errorSummary}${_ERROR_}: Custom user file '$path' not found or empty\\n"
  687. output_fail
  688. return 1
  689. fi
  690. if ! $shellBin -n "$path"; then
  691. errorSummary="${errorSummary}${_ERROR_}: Syntax error in custom user file '$path'\\n"
  692. output_fail
  693. return 1
  694. fi
  695. # shellcheck disable=SC1090
  696. if ! . "$path"; then
  697. errorSummary="${errorSummary}${_ERROR_}: Error running custom user file '$path'\\n"
  698. output_fail
  699. return 1
  700. else
  701. output 2 "Running $path "
  702. output_ok
  703. return 0
  704. fi
  705. }
  706. start_service() {
  707. local gatewaySummary errorSummary warningSummary dnsmasqStoredHash dnsmasqNewHash i modprobeStatus=0
  708. convert_config
  709. is_enabled 'on_start' || return 1
  710. is_wan_up || return 0
  711. if create_lock; then
  712. if [ -s "$dnsmasqFile" ]; then
  713. dnsmasqStoredHash="$(md5sum $dnsmasqFile | awk '{ print $1; }')"
  714. rm -f "$dnsmasqFile"
  715. fi
  716. for i in xt_set ip_set ip_set_hash_ip; do
  717. modprobe "$i" >/dev/null 2>/dev/null || modprobeStatus=$((modprobeStatus + 1))
  718. done
  719. if [ "$modprobeStatus" -gt 0 ] && ! is_chaos_calmer; then
  720. errorSummary="${errorSummary}${_ERROR_}: Failed to load kernel modules\\n"
  721. fi
  722. for i in PREROUTING FORWARD INPUT OUTPUT; do
  723. ipt -t mangle -N "VPR_${i}"
  724. ipt -t mangle "$insertOption" "$i" -m mark --mark "0x00/${fwMask}" -j "VPR_${i}"
  725. done
  726. output 1 'Processing Interfaces '
  727. config_load 'network'; config_foreach process_interface 'interface' 'create';
  728. output 1 '\n'
  729. if is_config_enabled 'policy'; then
  730. output 1 'Processing Policies '
  731. config_load "$packageName"; config_foreach process_policy 'policy';
  732. output 1 '\n'
  733. fi
  734. if is_config_enabled 'include'; then
  735. output 1 'Processing User File(s) '
  736. config_load "$packageName"; config_foreach process_user_file 'include';
  737. output 1 '\n'
  738. fi
  739. if [ -s "$dnsmasqFile" ]; then
  740. dnsmasqNewHash="$(md5sum $dnsmasqFile | awk '{ print $1; }')"
  741. fi
  742. [ "$dnsmasqNewHash" != "$dnsmasqStoredHash" ] && dnsmasq_restart
  743. if [ -z "$gatewaySummary" ]; then
  744. errorSummary="${errorSummary}${_ERROR_}: failed to set up any gateway\\n"
  745. else
  746. output "$serviceName started with gateways:\\n${gatewaySummary}"
  747. [ -n "$errorSummary" ] && output "${errorSummary}"
  748. [ -n "$warningSummary" ] && output "${warningSummary}"
  749. fi
  750. procd_open_instance "main"
  751. procd_set_param command /bin/true
  752. procd_set_param stdout 1
  753. procd_set_param stderr 1
  754. procd_open_data
  755. json_add_array 'status'
  756. json_add_object ''
  757. [ -n "$gatewaySummary" ] && json_add_string gateway "$gatewaySummary"
  758. [ -n "$errorSummary" ] && json_add_string error "$errorSummary"
  759. [ -n "$warningSummary" ] && json_add_string warning "$warningSummary"
  760. if [ "$strictMode" -ne 0 ] && [ "${gatewaySummary//0.0.0.0}" != "${gatewaySummary}" ]; then
  761. json_add_string mode "strict"
  762. fi
  763. json_close_object
  764. json_close_array
  765. procd_close_data
  766. procd_close_instance
  767. remove_lock
  768. else
  769. output "$serviceName: another instance of ${packageName} is currently running "
  770. output_failn
  771. return 1
  772. fi
  773. }
  774. restart() { reload; }
  775. restart_service() { reload; }
  776. stop_service() {
  777. local i
  778. iptables -t mangle -L | grep -q VPR_PREROUTING || return 0
  779. if create_lock; then
  780. load_package_config
  781. for i in PREROUTING FORWARD INPUT OUTPUT; do
  782. ipt -t mangle -D "${i}" -m mark --mark "0x00/${fwMask}" -j "VPR_${i}"
  783. ipt -t mangle -F "VPR_${i}"; ipt -t mangle -X "VPR_${i}";
  784. done
  785. config_load 'network'; config_foreach process_interface 'interface' 'destroy'
  786. unset ifaceTableID; unset ifaceMark;
  787. if [ -s "$dnsmasqFile" ]; then
  788. rm -f "$dnsmasqFile"
  789. dnsmasq_restart
  790. fi
  791. if [ "$serviceEnabled" -ne 0 ]; then
  792. output "$serviceName stopped "; output_okn;
  793. fi
  794. remove_lock
  795. else
  796. output "$serviceName: another instance of ${packageName} is currently running "; output_failn;
  797. return 1
  798. fi
  799. }
  800. # shellcheck disable=SC2119
  801. service_triggers() {
  802. local n
  803. is_enabled || return 1
  804. procd_open_validate
  805. validate_config
  806. validate_policy
  807. validate_include
  808. procd_close_validate
  809. procd_add_reload_trigger 'firewall' 'openvpn' 'vpn-policy-routing'
  810. procd_open_trigger
  811. for n in $ifSupported; do procd_add_reload_interface_trigger "$n"; procd_add_interface_trigger "interface.*" "$n" /etc/init.d/${packageName} reload; done;
  812. # output "$serviceName monitoring interfaces: $ifSupported\\n"; # output_okn;
  813. # for n in $ifAll; do procd_add_reload_interface_trigger "$n"; procd_add_interface_trigger "interface.*" "$n" /etc/init.d/${packageName} reload; done;
  814. # output "$serviceName monitoring ALL interfaces: $ifAll"; output_okn;
  815. procd_close_trigger
  816. }
  817. input() { local data; while read -r data; do echo "$data" | tee -a /var/${packageName}-support; done; }
  818. status_service() { support "$@"; }
  819. support() {
  820. local dist vers out id s param status set_d set_p tableCount i=0 dev dev6
  821. is_enabled
  822. json_load "$(ubus call system board)"; json_select release; json_get_var dist distribution; json_get_var vers version
  823. if [ -n "$wanIface4" ]; then
  824. network_get_gateway wanGW4 "$wanIface4"
  825. dev="$(uci -q get network."${wanIface4}".ifname)"
  826. fi
  827. if [ -n "$wanIface6" ]; then
  828. dev6="$(uci -q get network."${wanIface6}".ifname)"
  829. wanGW6=$(ip -6 route show | grep -m1 " dev $dev6 " | awk '{print $1}')
  830. [ "$wanGW6" = "default" ] && wanGW6=$(ip -6 route show | grep -m1 " dev $dev6 " | awk '{print $3}')
  831. fi
  832. while [ "${1:0:1}" = "-" ]; do param="${1//-/}"; export "set_$param=1"; shift; done
  833. [ -e "/var/${packageName}-support" ] && rm -f "/var/${packageName}-support"
  834. status="$serviceName running on $dist $vers."
  835. [ -n "$wanIface4" ] && status="$status WAN (IPv4): $wanIface4/dev/${wanGW4:-0.0.0.0}."
  836. [ -n "$wanIface6" ] && status="$status WAN (IPv6): $wanIface6/dev6/${wanGW6:-::/0}."
  837. {
  838. echo "$status"
  839. echo "============================================================"
  840. dnsmasq --version 2>/dev/null | sed '/^$/,$d'
  841. [ -n "$1" ] && {
  842. echo "============================================================"
  843. echo "Resolving domains"
  844. while [ -n "$1" ]; do echo "$1: $(resolveip "$1" | tr '\n' ' ')"; shift; done; }
  845. echo "============================================================"
  846. echo "Routes/IP Rules"
  847. tableCount=$(ip rule list | grep -c 'fwmark') || tableCount=0
  848. if [ -n "$set_d" ]; then route; else route | grep '^default'; fi
  849. if [ -n "$set_d" ]; then ip rule list; fi # || ip rule list | grep 'fwmark'
  850. i=0; while [ $i -lt $tableCount ]; do echo "IPv4 Table $((wanTableID + i)): $(ip route show table $((wanTableID + i)))"; echo "IPv4 Table $((wanTableID + i)) Rules:"; ip rule list | grep $((wanTableID + i)); i=$((i + 1)); done
  851. [ "$ipv6Enabled" -ne 0 ] && {
  852. i=0; while [ $i -lt $tableCount ]; do
  853. ip -6 route show table $((wanTableID + i)) | while read -r param; do echo "IPv6 Table $((wanTableID + i)): $param"; done
  854. i=$((i + 1))
  855. done; }
  856. echo "============================================================"
  857. if [ -z "$set_d" ]; then echo "IP Tables PREROUTING"; else echo "IP Tables"; fi
  858. if [ -z "$set_d" ]; then iptables -v -t mangle -S VPR_PREROUTING; else iptables -L -t mangle; fi
  859. [ "$ipv6Enabled" -ne 0 ] && {
  860. echo "============================================================"
  861. if [ -z "$set_d" ]; then echo "IP6 Tables PREROUTING"; else echo "IP6 Tables"; fi
  862. if [ -z "$set_d" ]; then ip6tables -v -t mangle -S VPR_PREROUTING; else ip6tables -L -t mangle; fi
  863. }
  864. [ -z "$set_d" ] && { echo "============================================================"
  865. echo "IP Tables FORWARD"
  866. iptables -v -t mangle -S VPR_FORWARD
  867. [ "$ipv6Enabled" -ne 0 ] && {
  868. echo "============================================================"
  869. echo "IPv6 Tables FORWARD"
  870. ip6tables -v -t mangle -S VPR_FORWARD
  871. };}
  872. [ -z "$set_d" ] && { echo "============================================================"
  873. echo "IP Tables INPUT"
  874. iptables -v -t mangle -S VPR_INPUT
  875. [ "$ipv6Enabled" -ne 0 ] && {
  876. echo "============================================================"
  877. echo "IPv6 Tables INPUT"
  878. ip6tables -v -t mangle -S VPR_INPUT
  879. };}
  880. [ -z "$set_d" ] && { echo "============================================================"
  881. echo "IP Tables OUTPUT"
  882. iptables -v -t mangle -S VPR_OUTPUT
  883. [ "$ipv6Enabled" -ne 0 ] && {
  884. echo "============================================================"
  885. echo "IPv6 Tables OUTPUT"
  886. ip6tables -v -t mangle -S VPR_OUTPUT
  887. };}
  888. echo "============================================================"
  889. echo "Current ipsets"
  890. ipset save
  891. if [ -s "$dnsmasqFile" ]; then
  892. echo "============================================================"
  893. echo "DNSMASQ ipsets"
  894. cat "$dnsmasqFile"
  895. fi
  896. echo "============================================================"
  897. } | input
  898. if [ -n "$set_p" ]; then
  899. printf "%b" "Pasting to paste.ee... "
  900. if is_installed 'curl' && is_installed 'libopenssl' && is_installed 'ca-bundle'; then
  901. json_init; json_add_string "description" "${packageName}-support"
  902. json_add_array "sections"; json_add_object '0'
  903. json_add_string "name" "$(uci -q get system.@system[0].hostname)"
  904. json_add_string "contents" "$(cat /var/${packageName}-support)"
  905. json_close_object; json_close_array; payload=$(json_dump)
  906. out=$(curl -s -k "https://api.paste.ee/v1/pastes" -X "POST" -H "Content-Type: application/json" -H "X-Auth-Token:uVOJt6pNqjcEWu7qiuUuuxWQafpHhwMvNEBviRV2B" -d "$payload")
  907. json_load "$out"; json_get_var id id; json_get_var s success
  908. [ "$s" = "1" ] && printf "%b" "https://paste.ee/p/$id $__OK__" || printf "%b" "$__FAIL__"
  909. [ -e "/var/${packageName}-support" ] && rm -f "/var/${packageName}-support"
  910. else
  911. printf "%b" "$__FAIL__\\n"
  912. printf "%b" "$_ERROR_: curl, libopenssl or ca-bundle were not found!\\nRun 'opkg update; opkg install curl libopenssl ca-bundle' to install them.\\n"
  913. fi
  914. else
  915. printf "%b" "Your support details have been logged to '/var/${packageName}-support'. $__OK__\\n"
  916. fi
  917. }
  918. # shellcheck disable=SC2120
  919. validate_config() {
  920. uci_validate_section "${packageName}" config "${1}" \
  921. 'enabled:bool:0' \
  922. 'verbosity:range(0,2):1' \
  923. 'strict_enforcement:bool:1' \
  924. 'src_ipset:bool:0' \
  925. 'dest_ipset:string' \
  926. 'ipv6_enabled:bool:0' \
  927. 'supported_interface:list(string)' \
  928. 'ignored_interface:list(string)' \
  929. 'boot_timeout:integer:30' \
  930. 'iptables_rule_option:or("", "append", "insert")' \
  931. 'iprule_enabled:bool:0' \
  932. 'webui_enable_column:bool:0' \
  933. 'webui_protocol_column:bool:0' \
  934. 'webui_supported_protocol:list(string)' \
  935. 'webui_chain_column:bool:0' \
  936. 'webui_sorting:bool:1' \
  937. 'icmp_interface:string' \
  938. 'wan_tid:integer:201' \
  939. 'wan_fw_mark:hex(8)' \
  940. 'fw_mask:hex(8)'
  941. }
  942. # shellcheck disable=SC2120
  943. validate_policy() {
  944. uci_validate_section "${packageName}" policy "${1}" \
  945. 'name:string' \
  946. 'enabled:bool:0' \
  947. 'interface:network' \
  948. 'proto:or(string)' \
  949. 'chain:or("", "PREROUTING", "FORWARD", "INPUT", "OUTPUT")' \
  950. 'src_addr:list(neg(or(host,network,macaddr)))' \
  951. 'src_port:list(neg(or(portrange, string)))' \
  952. 'dest_addr:list(neg(host))' \
  953. 'dest_port:list(neg(or(portrange, string)))'
  954. }
  955. # shellcheck disable=SC2120
  956. validate_include() {
  957. uci_validate_section "${packageName}" include "${1}" \
  958. 'path:string' \
  959. 'enabled:bool:0'
  960. }