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.

1034 lines
41 KiB

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