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.

1322 lines
52 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091,SC2018,SC2019,SC3043,SC3057,SC3060
  4. PKG_VERSION='dev-test'
  5. # sysctl net.ipv4.conf.default.rp_filter=1
  6. # sysctl net.ipv4.conf.all.rp_filter=1
  7. # shellcheck disable=SC2034
  8. START=94
  9. # shellcheck disable=SC2034
  10. USE_PROCD=1
  11. if type extra_command 1>/dev/null 2>&1; then
  12. extra_command 'support' "Generates output required to troubleshoot routing issues
  13. Use '-d' option for more detailed output
  14. Use '-p' option to automatically upload data under VPR paste.ee account
  15. WARNING: while paste.ee uploads are unlisted, they are still publicly available
  16. List domain names after options to include their lookup in report"
  17. extra_command 'version' 'Show version information'
  18. extra_command 'reload_interface' 'Reload specific interface only'
  19. else
  20. # shellcheck disable=SC2034
  21. EXTRA_COMMANDS='support version'
  22. # shellcheck disable=SC2034
  23. EXTRA_HELP=" support Generates output required to troubleshoot routing issues
  24. Use '-d' option for more detailed output
  25. Use '-p' option to automatically upload data under VPR paste.ee account
  26. WARNING: while paste.ee uploads are unlisted, they are still publicly available
  27. List domain names after options to include their lookup in report"
  28. fi
  29. readonly packageName='vpn-policy-routing'
  30. readonly serviceName="$packageName $PKG_VERSION"
  31. readonly PIDFile="/var/run/${packageName}.pid"
  32. readonly jsonFile="/var/run/${packageName}.json"
  33. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  34. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  35. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  36. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  37. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  38. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  39. readonly _ERROR_='\033[0;31mERROR\033[0m'
  40. readonly _WARNING_='\033[0;33mWARNING\033[0m'
  41. gatewaySummary=''; errorSummary=''; warningSummary='';
  42. serviceEnabled=''; verbosity=''; strictMode='';
  43. wanTableID=''; wanMark=''; fwMask='';
  44. ipv6Enabled=''; srcIpset=''; destIpset=''; resolverIpset='';
  45. wanIface4=''; wanIface6=''; ifaceMark=''; ifaceTableID='';
  46. ifAll=''; ifSupported=''; ignoredIfaces=''; supportedIfaces=''; icmpIface='';
  47. wanGW4=''; wanGW6=''; bootTimeout=''; insertOption='';
  48. webuiChainColumn=''; webuiShowIgnore=''; dnsmasqIpsetSupported='';
  49. procdReloadDelay='';
  50. usedChainsList='PREROUTING'
  51. ipsetSupported='true'
  52. configLoaded='false'
  53. version() { echo "$PKG_VERSION"; }
  54. output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
  55. output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
  56. output_fail() { s=1; output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
  57. output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
  58. str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
  59. str_replace() { echo "${1//$2/$3}"; }
  60. str_contains() { [ -n "$2" ] && [ "${1//$2}" != "$1" ]; }
  61. str_contains_word() { echo "$1" | grep -q -w "$2"; }
  62. str_to_lower() { echo "$1" | tr 'A-Z' 'a-z'; }
  63. str_extras_to_underscore() { echo "$1" | tr '[\. ~`!@#$%^&*()\+/,<>?//;:]' '_'; }
  64. str_extras_to_space() { echo "$1" | tr ';{}' ' '; }
  65. output() {
  66. # Can take a single parameter (text) to be output at any verbosity
  67. # Or target verbosity level and text to be output at specifc verbosity
  68. local msg memmsg logmsg
  69. if [ $# -ne 1 ]; then
  70. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  71. fi
  72. [ -t 1 ] && printf "%b" "$1"
  73. msg="${1//$serviceName /service }";
  74. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  75. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  76. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  77. logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
  78. rm -f "$sharedMemoryOutput"
  79. else
  80. printf "%b" "$msg" >> "$sharedMemoryOutput"
  81. fi
  82. }
  83. is_present() { command -v "$1" >/dev/null 2>&1; }
  84. is_installed() { [ -s "/usr/lib/opkg/info/${1}.control" ]; }
  85. is_variant_installed() { [ "$(echo /usr/lib/opkg/info/"${1}"*.control)" != "/usr/lib/opkg/info/${1}*.control" ]; }
  86. build_ifAll() { ifAll="${ifAll}${1} "; }
  87. build_ifSupported() { is_supported_interface "$1" && ifSupported="${ifSupported}${1} "; }
  88. vpr_find_iface() {
  89. local iface i param="$2"
  90. [ "$param" = 'wan6' ] || param='wan'
  91. "network_find_${param}" iface
  92. is_tunnel "$iface" && unset iface
  93. if [ -z "$iface" ]; then
  94. for i in $ifAll; do
  95. if "is_${param}" "$i"; then break; else unset i; fi
  96. done
  97. fi
  98. eval "$1"='${iface:-$i}'
  99. }
  100. vpr_get_gateway() {
  101. local iface="$2" dev="$3" gw
  102. network_get_gateway gw "$iface"
  103. if [ -z "$gw" ] || [ "$gw" = '0.0.0.0' ]; then
  104. gw="$(ip -4 a list dev "$dev" 2>/dev/null | grep inet | awk '{print $2}' | awk -F "/" '{print $1}')"
  105. fi
  106. eval "$1"='$gw'
  107. }
  108. vpr_get_gateway6() {
  109. local iface="$2" dev="$3" gw
  110. network_get_gateway6 gw "$iface"
  111. if [ -z "$gw" ] || [ "$gw" = '::/0' ] || [ "$gw" = '::0/0' ] || [ "$gw" = '::' ]; then
  112. gw="$(ip -6 a list dev "$dev" 2>/dev/null | grep inet6 | awk '{print $2}')"
  113. fi
  114. eval "$1"='$gw'
  115. }
  116. is_l2tp() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:4}" = "l2tp" ]; }
  117. is_oc() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:11}" = "openconnect" ]; }
  118. is_ovpn() { local dev i; for i in ifname device; do [ -z "$dev" ] && dev="$(uci -q get "network.${1}.${i}")"; done; [ "${dev:0:3}" = "tun" ] || [ "${dev:0:3}" = "tap" ] || [ -f "/sys/devices/virtual/net/${dev}/tun_flags" ]; }
  119. is_pptp() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:4}" = "pptp" ]; }
  120. is_tor() { [ "$(str_to_lower "$1")" = "tor" ]; }
  121. is_tor_running() {
  122. local ret=0
  123. if [ -s "/etc/tor/torrc" ]; then
  124. json_load "$(ubus call service list "{ 'name': 'tor' }")"
  125. json_select 'tor'; json_select 'instances'; json_select 'instance1';
  126. json_get_var ret 'running'; json_cleanup
  127. fi
  128. if [ "$ret" = "0" ]; then return 1; else return 0; fi
  129. }
  130. is_wg() { local proto; proto=$(uci -q get network."$1".proto); [ "${proto:0:9}" = "wireguard" ]; }
  131. is_tunnel() { is_l2tp "$1" || is_oc "$1" || is_ovpn "$1" || is_pptp "$1" || is_tor "$1" || is_wg "$1"; }
  132. is_wan() { [ "$1" = "$wanIface4" ] || { [ "${1##wan}" != "$1" ] && [ "${1##wan6}" = "$1" ]; } || [ "${1%%wan}" != "$1" ]; }
  133. is_wan6() { [ -n "$wanIface6" ] && [ "$1" = "$wanIface6" ] || [ "${1/#wan6}" != "$1" ] || [ "${1/%wan6}" != "$1" ]; }
  134. is_ignored_interface() { str_contains_word "$ignoredIfaces" "$1"; }
  135. is_supported_interface() { str_contains_word "$supportedIfaces" "$1" || { ! is_ignored_interface "$1" && { is_wan "$1" || is_wan6 "$1" || is_tunnel "$1"; }; }; }
  136. 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; }
  137. is_ipv4() { expr "$1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; }
  138. is_ipv6() { ! is_mac_address "$1" && str_contains "$1" ":"; }
  139. is_family_mismatch() { ( is_netmask "${1//!}" && is_ipv6 "${2//!}" ) || ( is_ipv6 "${1//!}" && is_netmask "${2//!}" ); }
  140. is_ipv6_link_local() { [ "${1:0:4}" = "fe80" ]; }
  141. is_ipv6_unique_local() { [ "${1:0:2}" = "fc" ] || [ "${1:0:2}" = "fd" ]; }
  142. is_ipv6_global() { [ "${1:0:4}" = "2001" ]; }
  143. # is_ipv6_global() { is_ipv6 "$1" && ! is_ipv6_link_local "$1" && ! is_ipv6_link_local "$1"; }
  144. is_netmask() { local ip="${1%/*}"; [ "$ip" != "$1" ] && is_ipv4 "$ip"; }
  145. is_domain() { str_contains "$1" '[a-zA-Z]'; }
  146. is_phys_dev() { [ "${1:0:1}" = "@" ] && ip l show | grep -E -q "^\\d+\\W+${1:1}"; }
  147. is_turris() { /bin/ubus -S call system board | /bin/grep 'Turris' | /bin/grep -q '15.05'; }
  148. is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
  149. dnsmasq_kill() { killall -q -s HUP dnsmasq; }
  150. dnsmasq_restart() { output 3 'Restarting DNSMASQ '; if /etc/init.d/dnsmasq restart >/dev/null 2>&1; then output_okn; else output_failn; fi; }
  151. is_default_dev() { [ "$1" = "$(ip -4 r | grep -m1 'dev' | grep -Eso 'dev [^ ]*' | awk '{print $2}')" ]; }
  152. is_supported_iface_dev() {
  153. for n in $ifSupported; do
  154. if [ "$1" = "$(uci -q get "network.${n}.ifname" || echo "$n")" ] || \
  155. [ "$1" = "$(uci -q get "network.${n}.device" || echo "$n")" ] || \
  156. [ "$1" = "$(uci -q get "network.${n}.proto")-${n}" ] ; then return 0; fi
  157. done
  158. return 1
  159. }
  160. is_supported_protocol () { grep -o '^[^#]*' /etc/protocols | grep -w -v '0' | grep . | awk '{print $1}' | grep -q "$1"; }
  161. append_chains_targets() {
  162. local chain iface name
  163. config_get name "$1" 'name' 'blank'
  164. config_get chain "$1" 'chain' 'PREROUTING'
  165. config_get iface "$1" 'interface'
  166. if ! str_contains_word "$usedChainsList" "$chain"; then
  167. usedChainsList="$usedChainsList $chain"
  168. if [ "$chain" != 'PREROUTING' ] && [ "$webuiChainColumn" != '1' ]; then
  169. warningSummary="${warningSummary}$_WARNING_: Chain '$chain' is used by a policy '$name', but a WebUI setting to show chains column (webui_chain_column) is disabled!\\n"
  170. fi
  171. fi
  172. if [ "$iface" = 'ignore' ] && ! str_contains_word "$supportedIfaces" 'ignore'; then
  173. supportedIfaces="$supportedIfaces ignore"
  174. if [ "$webuiShowIgnore" != '1' ]; then
  175. warningSummary="${warningSummary}$_WARNING_: The 'ignore' target is used by a policy '$name', but a WebUI setting to show 'ignore' target (webui_show_ignore_target) is disabled!\\n"
  176. fi
  177. fi
  178. }
  179. load_package_config() {
  180. [ "$configLoaded" = 'false' ] || return 0
  181. config_load "$packageName"
  182. config_get_bool serviceEnabled 'config' 'enabled' 0
  183. config_get_bool strictMode 'config' 'strict_enforcement' 1
  184. config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
  185. config_get_bool srcIpset 'config' 'src_ipset' 0
  186. config_get_bool destIpset 'config' 'dest_ipset' 0
  187. config_get resolverIpset 'config' 'resolver_ipset' 'dnsmasq.ipset'
  188. config_get verbosity 'config' 'verbosity' '2'
  189. config_get wanTableID 'config' 'wan_tid' '201'
  190. config_get wanMark 'config' 'wan_mark' '0x010000'
  191. config_get fwMask 'config' 'fw_mask' '0xff0000'
  192. config_get icmpIface 'config' 'icmp_interface'
  193. config_get ignoredIfaces 'config' 'ignored_interface'
  194. config_get supportedIfaces 'config' 'supported_interface'
  195. config_get bootTimeout 'config' 'boot_timeout' '30'
  196. config_get insertOption 'config' 'iptables_rule_option' 'append'
  197. config_get procdReloadDelay 'config' 'procd_reload_delay' '0'
  198. config_get_bool webuiChainColumn 'config' 'webui_chain_column' '0'
  199. config_get_bool webuiShowIgnore 'config' 'webui_show_ignore_target' '0'
  200. config_foreach append_chains_targets 'policy'
  201. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  202. verbosity=2
  203. fi
  204. . /lib/functions/network.sh
  205. . /usr/share/libubox/jshn.sh
  206. mkdir -p "${PIDFile%/*}"
  207. mkdir -p "${jsonFile%/*}"
  208. mkdir -p "${dnsmasqFile%/*}"
  209. if [ -n "$icmpIface" ] && ! str_contains_word "$usedChainsList" 'OUTPUT'; then
  210. usedChainsList="$usedChainsList OUTPUT"
  211. fi
  212. case $insertOption in
  213. insert|-i|-I) insertOption='-I';;
  214. append|-a|-A|*) insertOption='-A';;
  215. esac
  216. [ "$resolverIpset" = 'dnsmasq.ipset' ] && dnsmasqIpsetSupported='true'
  217. if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
  218. unset dnsmasqIpsetSupported
  219. if [ -n "$dnsmasqIpsetSupported" ]; then
  220. errorSummary="${errorSummary}${_ERROR_}: Resolver ipset support (dnsmasq.ipset) is enabled in $packageName, but DNSMASQ ipsets are not supported on this system!\\n"
  221. fi
  222. fi
  223. if ! ipset help hash:net >/dev/null 2>&1; then
  224. unset ipsetSupported
  225. if [ -n "$dnsmasqIpsetSupported" ]; then
  226. errorSummary="${errorSummary}${_ERROR_}: DNSMASQ ipsets are supported, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  227. unset dnsmasqIpsetSupported
  228. fi
  229. if [ "$destIpset" -ne 0 ]; then
  230. errorSummary="${errorSummary}${_ERROR_}: Destination ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  231. destIpset=0
  232. fi
  233. if [ "$srcIpset" -ne 0 ]; then
  234. errorSummary="${errorSummary}${_ERROR_}: Source ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  235. srcIpset=0
  236. fi
  237. fi
  238. if ! ipset help hash:mac >/dev/null 2>&1; then
  239. if [ "$srcIpset" -ne 0 ]; then
  240. errorSummary="${errorSummary}${_ERROR_}: Source ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:mac' type!\\n"
  241. srcIpset=0
  242. fi
  243. fi
  244. configLoaded='true'
  245. }
  246. is_enabled() {
  247. load_package_config
  248. if [ "$serviceEnabled" -eq 0 ]; then
  249. if [ "$1" = 'on_start' ]; then
  250. errorSummary="${errorSummary}${_ERROR_}: ${packageName} is currently disabled.\\n"
  251. errorSummary="${errorSummary}Enable ${packageName} from WebUI or run the following commands:\\n"
  252. errorSummary="${errorSummary}uci set $packageName.config.enabled='1'; uci commit $packageName;\\n"
  253. fi
  254. return 1
  255. fi
  256. }
  257. load_network() {
  258. if [ -z "$ifAll" ]; then
  259. config_load 'network'
  260. config_foreach build_ifAll 'interface'
  261. fi
  262. vpr_find_iface wanIface4 'wan'
  263. [ "$ipv6Enabled" -ne 0 ] && vpr_find_iface wanIface6 'wan6'
  264. [ -n "$wanIface4" ] && network_get_gateway wanGW4 "$wanIface4"
  265. [ -n "$wanIface6" ] && network_get_gateway6 wanGW6 "$wanIface6"
  266. wanGW="${wanGW4:-$wanGW6}"
  267. unset ifSupported
  268. config_load 'network'
  269. config_foreach build_ifSupported 'interface'
  270. }
  271. is_wan_up() {
  272. local sleepCount=1
  273. load_network
  274. while [ -z "$wanGW" ] ; do
  275. load_network
  276. if [ $((sleepCount)) -gt $((bootTimeout)) ] || [ -n "$wanGW" ]; then break; fi
  277. output "$serviceName waiting for wan gateway...\\n"
  278. sleep 1
  279. network_flush_cache
  280. sleepCount=$((sleepCount+1))
  281. done
  282. if [ -n "$wanGW" ]; then
  283. return 0
  284. else
  285. errorSummary="${errorSummary}${_ERROR_}: ${serviceName} failed to discover WAN gateway!\\n"
  286. return 1
  287. fi
  288. }
  289. ipt_cleanup() {
  290. local i
  291. for i in PREROUTING FORWARD INPUT OUTPUT; do
  292. while iptables -t mangle -D $i -m mark --mark 0x0/0xff0000 -j VPR_${i} >/dev/null 2>&1; do : ; done
  293. done
  294. for i in PREROUTING FORWARD INPUT OUTPUT; do
  295. while iptables -t mangle -D $i -j VPR_${i} >/dev/null 2>&1; do : ; done
  296. done
  297. }
  298. # shellcheck disable=SC2086
  299. ipt() {
  300. local d failFlagIpv4=1 failFlagIpv6=1
  301. for d in "${*//-A/-D}" "${*//-I/-D}" "${*//-N/-F}" "${*//-N/-X}"; do
  302. [ "$d" != "$*" ] && { iptables $d >/dev/null 2>&1; ip6tables $d >/dev/null 2>&1; }
  303. done
  304. d="$*"; iptables $d >/dev/null 2>&1 && failFlagIpv4=0;
  305. if [ "$ipv6Enabled" -gt 0 ]; then ip6tables $d >/dev/null 2>&1 && failFlagIpv6=0; fi
  306. [ "$failFlagIpv4" -eq 0 ] || [ "$failFlagIpv6" -eq 0 ]
  307. }
  308. # shellcheck disable=SC2086
  309. ips() {
  310. local command="$1" ipset="${2//-/_}" param="$3" comment="$4" appendix failFlag=0
  311. if str_contains "$ipset" '_ip'; then
  312. ipset="${ipset//_ip}"; appendix='_ip';
  313. elif str_contains "$ipset" '_mac'; then
  314. ipset="${ipset//_mac}"; appendix='_mac';
  315. fi
  316. case "$command" in
  317. add_dnsmasq)
  318. [ "$resolverIpset" = "dnsmasq.ipset" ] || return 1
  319. if [ -z "$dnsmasqIpsetSupported" ]; then
  320. warningSummary="${warningSummary}${_WARNING_}: The 'resolver_ipset' is set to 'dnsmasq.ipset', but DNSMASQ ipsets are not supported on this system!\\n"
  321. failFlag=1
  322. elif [ "$ipv6Enabled" -ne 0 ]; then
  323. echo "ipset=/${param}/${ipset},${ipset}6 # $comment" >> "$dnsmasqFile" || failFlag=1
  324. else
  325. echo "ipset=/${param}/${ipset} # $comment" >> "$dnsmasqFile" || failFlag=1
  326. fi
  327. ;;
  328. add)
  329. if [ -z "$appendix" ] && [ "$destIpset" -eq 0 ]; then return 1; fi
  330. if [ -n "$appendix" ] && [ "$srcIpset" -eq 0 ]; then return 1; fi
  331. if [ "$ipv6Enabled" -ne 0 ] && [ "$appendix" != "_mac" ]; then
  332. ipset -q -! $command "${ipset}6${appendix}" $param comment "$comment" || failFlag=1
  333. fi
  334. ipset -q -! $command "${ipset}${appendix}" $param comment "$comment" || failFlag=1
  335. ;;
  336. create)
  337. if [ "$ipv6Enabled" -ne 0 ] && [ "$appendix" != "_mac" ]; then
  338. ipset -q -! "$command" "${ipset}6${appendix}" $param family inet6 || failFlag=1
  339. fi
  340. ipset -q -! "$command" "${ipset}${appendix}" $param || failFlag=1
  341. ;;
  342. destroy|flush)
  343. ipset -q -! "$command" "${ipset}6${appendix}" 2>/dev/null || failFlag=1
  344. ipset -q -! "$command" "${ipset}${appendix}" 2>/dev/null || failFlag=1
  345. return 0
  346. ;;
  347. esac
  348. return $failFlag
  349. }
  350. insert_tor_policy() {
  351. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto chain
  352. proto="$(str_to_lower "$7")"
  353. chain="${8:-PREROUTING}"
  354. if [ -n "${laddr}${lport}${rport}" ]; then
  355. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'src_addr', 'src_port' and 'dest_port' for policy '$comment'\\n"
  356. fi
  357. if [ -n "$proto" ] && [ "$proto" != "all" ]; then
  358. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy '$comment'\\n"
  359. fi
  360. if [ "$chain" != "PREROUTING" ]; then
  361. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'chain' or set 'chain' to 'PREROUTING' for policy '$comment'\\n"
  362. fi
  363. ips 'add' "${iface}" "$raddr" "${comment}: $raddr" || processPolicyError="${processPolicyError}${_ERROR_}: ipset 'add' $iface $raddr\\n"
  364. return 0
  365. }
  366. insert_policy() {
  367. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto chain
  368. local mark param i valueNeg value dest ipInsertOption="-A"
  369. proto="$(str_to_lower "$7")"
  370. chain="${8:-PREROUTING}"
  371. mark=$(eval echo "\$mark_${iface//-/_}")
  372. if [ "$ipv6Enabled" -eq 0 ] && ( is_ipv6 "$laddr" || is_ipv6 "$raddr" ); then
  373. processPolicyError="${processPolicyError}${_ERROR_}: Skipping IPv6 policy '$comment' as IPv6 support is disabled\\n"
  374. return 1
  375. fi
  376. if [ -n "$mark" ]; then
  377. dest="-g VPR_MARK${mark}"
  378. elif [ "$iface" = "ignore" ]; then
  379. dest="-j RETURN"
  380. else
  381. processPolicyError="${processPolicyError}${_ERROR_}: Unknown fw_mark for ${iface}\\n"
  382. return 0
  383. fi
  384. if [ -z "$proto" ]; then
  385. if [ -n "$lport" ] || [ -n "$rport" ]; then
  386. proto='tcp udp'
  387. else
  388. proto='all'
  389. fi
  390. fi
  391. if is_family_mismatch "$laddr" "$raddr"; then
  392. processPolicyError="${processPolicyError}${_ERROR_}: Mismatched IP family between '$laddr' and '$raddr' in policy '$comment'\\n"
  393. return 0
  394. fi
  395. for i in $proto; do
  396. if [ "$i" = 'all' ]; then
  397. param="-t mangle ${ipInsertOption} VPR_${chain} $dest"
  398. elif ! is_supported_protocol "$i"; then
  399. processPolicyError="${processPolicyError}${_ERROR_}: Unknown protocol '$i' in policy '$comment'\\n"
  400. return 0
  401. else
  402. param="-t mangle ${ipInsertOption} VPR_${chain} $dest -p $i"
  403. fi
  404. if [ -n "$laddr" ]; then
  405. if [ "${laddr:0:1}" = "!" ]; then
  406. valueNeg='!'; value="${laddr:1}"
  407. else
  408. unset valueNeg; value="$laddr";
  409. fi
  410. if is_phys_dev "$value"; then
  411. param="$param $valueNeg -m physdev --physdev-in ${value:1}"
  412. elif is_mac_address "$value"; then
  413. param="$param -m mac $valueNeg --mac-source $value"
  414. else
  415. param="$param $valueNeg -s $value"
  416. fi
  417. fi
  418. if [ -n "$lport" ]; then
  419. if [ "${lport:0:1}" = "!" ]; then
  420. valueNeg='!'; value="${lport:1}"
  421. else
  422. unset valueNeg; value="$lport";
  423. fi
  424. param="$param -m multiport $valueNeg --sport ${value//-/:}"
  425. fi
  426. if [ -n "$raddr" ]; then
  427. if [ "${raddr:0:1}" = "!" ]; then
  428. valueNeg='!'; value="${raddr:1}"
  429. else
  430. unset valueNeg; value="$raddr";
  431. fi
  432. param="$param $valueNeg -d $value"
  433. fi
  434. if [ -n "$rport" ]; then
  435. if [ "${rport:0:1}" = "!" ]; then
  436. valueNeg='!'; value="${rport:1}"
  437. else
  438. unset valueNeg; value="$rport";
  439. fi
  440. param="$param -m multiport $valueNeg --dport ${value//-/:}"
  441. fi
  442. [ -n "$comment" ] && param="$param -m comment --comment $(str_extras_to_underscore "$comment")"
  443. ipt "$param" || processPolicyError="${processPolicyError}${_ERROR_}: iptables $param\\n"
  444. done
  445. return 0
  446. }
  447. r_process_policy(){
  448. local comment="$1" iface="$2" laddr="$3" lport="$4" raddr="$5" rport="$6" proto="$7" chain="$8" resolved_laddr resolved_raddr i ipsFailFlag
  449. if str_contains "$laddr" '[ ;\{\}]'; then
  450. for i in $(str_extras_to_space "$laddr"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$i" "$lport" "$raddr" "$rport" "$proto" "$chain"; done
  451. return 0
  452. elif str_contains "$lport" '[ ;\{\}]'; then
  453. for i in $(str_extras_to_space "$lport"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$i" "$raddr" "$rport" "$proto" "$chain"; done
  454. return 0
  455. elif str_contains "$raddr" '[ ;\{\}]'; then
  456. for i in $(str_extras_to_space "$raddr"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$lport" "$i" "$rport" "$proto" "$chain"; done
  457. return 0
  458. elif str_contains "$rport" '[ ;\{\}]'; then
  459. for i in $(str_extras_to_space "$rport"); do [ -n "$i" ] && r_process_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$i" "$proto" "$chain"; done
  460. return 0
  461. fi
  462. # start non-recursive processing
  463. # process TOR, netmask, physical device and mac-address separately, so we don't send them to resolveip
  464. if is_tor "$iface"; then
  465. insert_tor_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  466. elif is_phys_dev "$laddr"; then
  467. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  468. elif [ -n "$laddr" ] && [ -z "${lport}${raddr}${rport}" ] && [ "$chain" = 'PREROUTING' ]; then
  469. if is_mac_address "$laddr"; then
  470. if [ -n "$proto" ] && [ "$proto" != 'all' ] && [ "$srcIpset" -ne 0 ]; then
  471. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy: '$comment', mac-address: '$laddr'\\n"
  472. fi
  473. ips 'add' "${iface}_mac" "$laddr" "${comment}: $laddr" || ipsFailFlag=1
  474. else
  475. if [ -n "$proto" ] && [ "$proto" != "all" ] && [ "$srcIpset" -ne 0 ]; then
  476. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy: '$comment', source: '$laddr'\\n"
  477. fi
  478. ips 'add' "${iface}_ip" "$laddr" "${comment}: $laddr" || ipsFailFlag=1
  479. fi
  480. elif [ -n "$raddr" ] && [ -z "${laddr}${lport}${rport}" ] && [ "$chain" = 'PREROUTING' ]; then
  481. if [ -n "$proto" ] && [ "$proto" != 'all' ]; then
  482. processPolicyWarning="${processPolicyWarning}${_WARNING_}: Please unset 'proto' or set 'proto' to 'all' for policy: '$comment', destination: '$raddr'\\n"
  483. fi
  484. if is_domain "$raddr"; then
  485. ips 'add_dnsmasq' "${iface}" "$raddr" "${comment}" || ipsFailFlag=1
  486. else
  487. ips 'add' "${iface}" "$raddr" "${comment}: $raddr" || ipsFailFlag=1
  488. fi
  489. else
  490. ipsFailFlag=1
  491. fi
  492. [ -n "$ipsFailFlag" ] || return 0;
  493. if is_mac_address "$laddr"; then
  494. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  495. elif is_netmask "$laddr" || is_netmask "$raddr"; then
  496. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  497. else
  498. [ -n "$laddr" ] && resolved_laddr="$(resolveip "$laddr")"
  499. [ -n "$raddr" ] && resolved_raddr="$(resolveip "$raddr")"
  500. if [ -n "$resolved_laddr" ] && [ "$resolved_laddr" != "$laddr" ]; then
  501. for i in $resolved_laddr; do [ -n "$i" ] && r_process_policy "$comment $laddr" "$iface" "$i" "$lport" "$raddr" "$rport" "$proto" "$chain"; done
  502. elif [ -n "$resolved_raddr" ] && [ "$resolved_raddr" != "$raddr" ]; then
  503. for i in $resolved_raddr; do [ -n "$i" ] && r_process_policy "$comment $raddr" "$iface" "$laddr" "$lport" "$i" "$rport" "$proto" "$chain"; done
  504. else
  505. insert_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  506. fi
  507. fi
  508. }
  509. process_policy(){
  510. local name comment iface laddr lport raddr rport param mark processPolicyError processPolicyWarning proto chain enabled
  511. config_get comment "$1" 'comment'
  512. config_get name "$1" 'name' 'blank'
  513. config_get iface "$1" 'interface'
  514. config_get laddr "$1" 'src_addr'
  515. config_get lport "$1" 'src_port'
  516. config_get raddr "$1" 'dest_addr'
  517. config_get rport "$1" 'dest_port'
  518. config_get proto "$1" 'proto'
  519. config_get chain "$1" 'chain' 'PREROUTING'
  520. config_get_bool enabled "$1" 'enabled' 1
  521. [ "$enabled" -gt 0 ] || return 0
  522. proto="$(str_to_lower "$proto")"
  523. [ "$proto" = 'auto' ] && unset proto
  524. comment="${comment:-$name}"
  525. output 2 "Routing '$comment' via $iface "
  526. if [ -z "$comment" ]; then
  527. errorSummary="${errorSummary}${_ERROR_}: Policy name is empty\\n"
  528. output_fail; return 1;
  529. fi
  530. if [ -z "${laddr}${lport}${raddr}${rport}" ]; then
  531. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' missing all IPs/ports\\n"
  532. output_fail; return 1;
  533. fi
  534. if [ -z "$iface" ]; then
  535. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' has no assigned interface\\n"
  536. output_fail; return 1;
  537. fi
  538. if ! is_supported_interface "$iface"; then
  539. errorSummary="${errorSummary}${_ERROR_}: Policy '$comment' has unknown interface: '${iface}'\\n"
  540. output_fail; return 1;
  541. fi
  542. lport="${lport// / }"; lport="${lport// /,}"; lport="${lport//,\!/ !}";
  543. rport="${rport// / }"; rport="${rport// /,}"; rport="${rport//,\!/ !}";
  544. r_process_policy "$comment" "$iface" "$laddr" "$lport" "$raddr" "$rport" "$proto" "$chain"
  545. if [ -n "$processPolicyWarning" ]; then
  546. warningSummary="${warningSummary}${processPolicyWarning}\\n"
  547. fi
  548. if [ -n "$processPolicyError" ]; then
  549. output_fail
  550. errorSummary="${errorSummary}${processPolicyError}\\n"
  551. else
  552. output_ok
  553. fi
  554. }
  555. table_destroy(){
  556. local tid="$1" iface="$2" mark="$3"
  557. if [ -n "$tid" ] && [ -n "$iface" ] && [ -n "$mark" ]; then
  558. ipt -t mangle -F "VPR_MARK${mark}"
  559. ipt -t mangle -X "VPR_MARK${mark}"
  560. ip -4 rule del fwmark "$mark" table "$tid" >/dev/null 2>&1
  561. ip -6 rule del fwmark "$mark" table "$tid" >/dev/null 2>&1
  562. ip -4 rule del table "$tid" >/dev/null 2>&1
  563. ip -6 rule del table "$tid" >/dev/null 2>&1
  564. ip -4 route flush table "$tid" >/dev/null 2>&1
  565. ip -6 route flush table "$tid" >/dev/null 2>&1
  566. ips 'flush' "${iface}"; ips 'destroy' "${iface}";
  567. ips 'flush' "${iface}_ip"; ips 'destroy' "${iface}_ip";
  568. ips 'flush' "${iface}_mac"; ips 'destroy' "${iface}_mac";
  569. ip -4 route flush cache
  570. ip -6 route flush cache
  571. sed -i "/$iface/d" /etc/iproute2/rt_tables
  572. return 0
  573. else
  574. return 1
  575. fi
  576. }
  577. # shellcheck disable=SC2086
  578. table_create(){
  579. local tid="$1" mark="$2" iface="$3" gw4="$4" dev="$5" gw6="$6" dev6="$7" match="$8" dscp s=0 i ipv4_error=0 ipv6_error=1
  580. if [ -z "$tid" ] || [ -z "$mark" ] || [ -z "$iface" ]; then
  581. return 1
  582. fi
  583. table_destroy "$tid" "$iface" "$mark"
  584. if [ -n "$gw4" ] || [ "$strictMode" -ne 0 ]; then
  585. echo "$tid" "$iface" >> /etc/iproute2/rt_tables
  586. if [ -z "$gw4" ]; then
  587. ip -4 route add unreachable default table "$tid" >/dev/null 2>&1 || ipv4_error=1
  588. else
  589. ip -4 route add default via "$gw4" dev "$dev" table "$tid" >/dev/null 2>&1 || ipv4_error=1
  590. fi
  591. # ip -4 route list table main | grep -v 'br-lan' | while read -r i; do
  592. ip -4 route list table main | while read -r i; do
  593. idev="$(echo "$i" | grep -Eso 'dev [^ ]*' | awk '{print $2}')"
  594. if ! is_supported_iface_dev "$idev"; then
  595. ip -4 route add $i table "$tid" >/dev/null 2>&1 || ipv4_error=1
  596. fi
  597. done
  598. ip -4 route flush cache || ipv4_error=1
  599. ip -4 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv4_error=1
  600. ipt -t mangle -N "VPR_MARK${mark}" || ipv4_error=1
  601. ipt -t mangle -A "VPR_MARK${mark}" -j MARK --set-xmark "${mark}/${fwMask}" || ipv4_error=1
  602. ipt -t mangle -A "VPR_MARK${mark}" -j RETURN || ipv4_error=1
  603. fi
  604. if [ "$ipv6Enabled" -ne 0 ]; then
  605. ipv6_error=0
  606. if { [ -n "$gw6" ] && [ "$gw6" != "::/0" ]; } || [ "$strictMode" -ne 0 ]; then
  607. if [ -z "$gw6" ] || [ "$gw6" = "::/0" ]; then
  608. ip -6 route add unreachable default table "$tid" || ipv6_error=1
  609. else
  610. ip -6 route list table main | grep " dev $dev6 " | while read -r i; do
  611. ip -6 route add $i table "$tid" >/dev/null 2>&1 || ipv6_error=1
  612. done
  613. fi
  614. ip -6 route flush cache || ipv6_error=1
  615. ip -6 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv6_error=1
  616. fi
  617. fi
  618. if [ $ipv4_error -eq 0 ] || [ $ipv6_error -eq 0 ]; then
  619. dscp="$(uci -q get "${packageName}".config."${iface}"_dscp)"
  620. if [ "${dscp:-0}" -ge 1 ] && [ "${dscp:-0}" -le 63 ]; then
  621. ipt -t mangle -I VPR_PREROUTING -m dscp --dscp "${dscp}" -g "VPR_MARK${mark}" || s=1
  622. fi
  623. if [ -n "$ipsetSupported" ] && { [ -n "$dnsmasqIpsetSupported" ] || [ "$destIpset" -ne 0 ]; }; then
  624. if ips 'create' "${iface}" 'hash:net comment' && ips 'flush' "${iface}"; then
  625. for i in $usedChainsList; do
  626. ipt -t mangle -I VPR_${i} -m set --match-set "${iface}" dst -g "VPR_MARK${mark}" || s=1
  627. if [ "$ipv6Enabled" -ne 0 ]; then ipt -t mangle -I VPR_${i} -m set --match-set "${iface}6" dst -g "VPR_MARK${mark}" || s=1; fi
  628. done
  629. else
  630. s=1
  631. fi
  632. fi
  633. if [ -n "$ipsetSupported" ] && [ "$srcIpset" -ne 0 ]; then
  634. if ips 'create' "${iface}_ip" 'hash:net comment' && ips 'flush' "${iface}_ip"; then
  635. ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}_ip" src -g "VPR_MARK${mark}" || s=1
  636. if [ "$ipv6Enabled" -ne 0 ]; then ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}6_ip" src -g "VPR_MARK${mark}" || s=1; fi
  637. else
  638. s=1
  639. fi
  640. if ips 'create' "${iface}_mac" 'hash:mac comment' && ips 'flush' "${iface}_mac"; then
  641. ipt -t mangle -I VPR_PREROUTING -m set --match-set "${iface}_mac" src -g "VPR_MARK${mark}" || s=1
  642. else
  643. s=1
  644. fi
  645. fi
  646. if [ "$iface" = "$icmpIface" ]; then
  647. ipt -t mangle -I VPR_OUTPUT -p icmp -g "VPR_MARK${mark}" || s=1
  648. fi
  649. else
  650. s=1
  651. fi
  652. return $s
  653. }
  654. table_reload() {
  655. local tid="$1" mark="$2" iface="$3" gw4="$4" dev="$5" gw6="$6" dev6="$7" match="$8" dscp s=0 i ipv4_error=0 ipv6_error=1
  656. if [ -z "$tid" ] || [ -z "$mark" ] || [ -z "$iface" ]; then
  657. return 1
  658. fi
  659. ip -4 route del default table "$tid" >/dev/null 2>&1
  660. if [ -n "$gw4" ] || [ "$strictMode" -ne 0 ]; then
  661. if [ -z "$gw4" ]; then
  662. ip -4 route add unreachable default table "$tid" >/dev/null 2>&1 || ipv4_error=1
  663. else
  664. ip -4 route add default via "$gw4" dev "$dev" table "$tid" >/dev/null 2>&1 || ipv4_error=1
  665. fi
  666. ip -4 route flush cache || ipv4_error=1
  667. ip -4 rule del fwmark "${mark}/${fwMask}" table "$tid" >/dev/null 2>&1
  668. ip -4 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv4_error=1
  669. fi
  670. if [ "$ipv6Enabled" -ne 0 ]; then
  671. ip -6 route del default table "$tid" >/dev/null 2>&1
  672. ipv6_error=0
  673. if { [ -n "$gw6" ] && [ "$gw6" != "::/0" ]; } || [ "$strictMode" -ne 0 ]; then
  674. if [ -z "$gw6" ] || [ "$gw6" = "::/0" ]; then
  675. ip -6 route add unreachable default table "$tid" || ipv6_error=1
  676. else
  677. ip -6 route list table main | grep " dev $dev6 " | while read -r i; do
  678. ip -6 route add "$i" table "$tid" >/dev/null 2>&1 || ipv6_error=1
  679. done
  680. fi
  681. ip -6 route flush cache || ipv6_error=1
  682. ip -6 rule del fwmark "${mark}/${fwMask}" table "$tid" >/dev/null 2>&1
  683. ip -6 rule add fwmark "${mark}/${fwMask}" table "$tid" || ipv6_error=1
  684. fi
  685. fi
  686. if [ $ipv4_error -eq 0 ] || [ $ipv6_error -eq 0 ]; then
  687. dscp="$(uci -q get "${packageName}".config."${iface}"_dscp)"
  688. if [ "${dscp:-0}" -ge 1 ] && [ "${dscp:-0}" -le 63 ]; then
  689. ipt -t mangle -I VPR_PREROUTING -m dscp --dscp "${dscp}" -g "VPR_MARK${mark}" || s=1
  690. fi
  691. if [ "$iface" = "$icmpIface" ]; then
  692. ipt -t mangle -I VPR_OUTPUT -p icmp -g "VPR_MARK${mark}" || s=1
  693. fi
  694. else
  695. s=1
  696. fi
  697. return $s
  698. }
  699. process_interface(){
  700. local gw4 gw6 dev dev6 s=0 dscp iface="$1" action="$2" match="$3" displayText
  701. is_supported_interface "$iface" || return 0
  702. is_wan6 "$iface" && return 0
  703. [ $((ifaceMark)) -gt $((fwMask)) ] && return 1
  704. network_get_device dev "$iface"
  705. [ -z "$dev" ] && config_get dev "$iface" 'ifname'
  706. [ -z "$dev" ] && config_get dev "$iface" 'device'
  707. if is_wan "$iface" && [ -n "$wanIface6" ]; then
  708. network_get_device dev6 "$wanIface6"
  709. [ -z "$dev6" ] && config_get dev6 "$wanIface6" 'ifname'
  710. [ -z "$dev6" ] && config_get dev6 "$wanIface6" 'device'
  711. fi
  712. [ -z "$dev6" ] && dev6="$dev"
  713. [ -z "$ifaceTableID" ] && ifaceTableID="$wanTableID"; [ -z "$ifaceMark" ] && ifaceMark="$wanMark";
  714. case "$action" in
  715. destroy)
  716. table_destroy "${ifaceTableID}" "${iface}" "${ifaceMark}"
  717. ifaceTableID="$((ifaceTableID + 1))"; ifaceMark="$(printf '0x%06x' $((ifaceMark + wanMark)))";
  718. ;;
  719. create)
  720. eval "mark_${iface//-/_}"='$ifaceMark'
  721. eval "tid_${iface//-/_}"='$ifaceTableID'
  722. if [ -z "$match" ]; then
  723. table_destroy "$ifaceTableID" "$iface"
  724. fi
  725. vpr_get_gateway gw4 "$iface" "$dev"
  726. vpr_get_gateway6 gw6 "$iface" "$dev6"
  727. if [ "$iface" = "$dev" ]; then
  728. displayText="${iface}/${gw4:-0.0.0.0}"
  729. else
  730. displayText="${iface}/${dev}/${gw4:-0.0.0.0}"
  731. fi
  732. [ "$ipv6Enabled" -ne 0 ] && displayText="${displayText}/${gw6:-::/0}"
  733. if [ -z "$match" ]; then
  734. output 2 "Creating table '$displayText' "
  735. is_default_dev "$dev" && displayText="${displayText} ${__OK__}"
  736. if table_create "$ifaceTableID" "$ifaceMark" "$iface" "$gw4" "$dev" "$gw6" "$dev6" "$match"; then
  737. gatewaySummary="${gatewaySummary}${displayText}\\n"
  738. output_ok
  739. else
  740. errorSummary="${errorSummary}${_ERROR_}: Failed to set up '$displayText'\\n"
  741. output_fail
  742. fi
  743. elif [ "$iface" = "$match" ]; then
  744. output 2 "Reloading table '$displayText' "
  745. is_default_dev "$dev" && displayText="${displayText} ${__OK__}"
  746. if table_reload "$ifaceTableID" "$ifaceMark" "$iface" "$gw4" "$dev" "$gw6" "$dev6" "$match"; then
  747. gatewaySummary="${gatewaySummary}${displayText}\\n"
  748. output_ok
  749. else
  750. errorSummary="${errorSummary}${_ERROR_}: Failed to reload '$displayText'\\n"
  751. output_fail
  752. fi
  753. else
  754. is_default_dev "$dev" && displayText="${displayText} ${__OK__}"
  755. gatewaySummary="${gatewaySummary}${displayText}\\n"
  756. fi
  757. ifaceTableID="$((ifaceTableID + 1))"; ifaceMark="$(printf '0x%06x' $((ifaceMark + wanMark)))";
  758. ;;
  759. esac
  760. return $s
  761. }
  762. process_tor_interface(){
  763. local s=0 iface="$1" action="$2" displayText
  764. case "$action" in
  765. destroy)
  766. for i in PREROUTING FORWARD INPUT OUTPUT; do
  767. ipt -t nat -D "${i}" -m mark --mark "0x0/${fwMask}" -j "VPR_${i}"
  768. ipt -t nat -F "VPR_${i}"; ipt -t nat -X "VPR_${i}";
  769. done
  770. ;;
  771. create)
  772. output 2 "Creating TOR redirects "
  773. dnsPort="$(grep -m1 DNSPort /etc/tor/torrc | awk -F: '{print $2}')"
  774. transPort="$(grep -m1 TransPort /etc/tor/torrc | awk -F: '{print $2}')"
  775. dnsPort="${dnsPort:-9053}"; transPort="${transPort:-9040}";
  776. for i in $usedChainsList; do
  777. ipt -t nat -N "VPR_${i}"
  778. ipt -t nat "$insertOption" "$i" -m mark --mark "0x0/${fwMask}" -j "VPR_${i}"
  779. done
  780. if ips 'create' "${iface}" 'hash:net comment' && ips 'flush' "${iface}"; then
  781. for i in $usedChainsList; do
  782. ipt -t nat -I "VPR_${i}" -p udp -m udp --dport 53 -m set --match-set "${iface}" dst -j REDIRECT --to-ports "$dnsPort" -m comment --comment "TorDNS-UDP" || s=1
  783. ipt -t nat -I "VPR_${i}" -p tcp -m tcp --dport 80 -m set --match-set "${iface}" dst -j REDIRECT --to-ports "$transPort" -m comment --comment "TorHTTP-TCP" || s=1
  784. ipt -t nat -I "VPR_${i}" -p udp -m udp --dport 80 -m set --match-set "${iface}" dst -j REDIRECT --to-ports "$transPort" -m comment --comment "TorHTTP-UDP" || s=1
  785. ipt -t nat -I "VPR_${i}" -p tcp -m tcp --dport 443 -m set --match-set "${iface}" dst -j REDIRECT --to-ports "$transPort" -m comment --comment "TorHTTPS-TCP" || s=1
  786. ipt -t nat -I "VPR_${i}" -p udp -m udp --dport 443 -m set --match-set "${iface}" dst -j REDIRECT --to-ports "$transPort" -m comment --comment "TorHTTPS-UDP" || s=1
  787. done
  788. else
  789. s=1
  790. fi
  791. displayText="${iface}/53->${dnsPort}/80,443->${transPort}"
  792. if [ "$s" -eq "0" ]; then
  793. gatewaySummary="${gatewaySummary}${displayText}\\n"
  794. output_ok
  795. else
  796. errorSummary="${errorSummary}${_ERROR_}: Failed to set up '$displayText'\\n"
  797. output_fail
  798. fi
  799. ;;
  800. esac
  801. return $s
  802. }
  803. convert_config(){
  804. local i src_ipset dest_ipset resolver_ipset
  805. [ -s "/etc/config/${packageName}" ] || return 0
  806. grep -q "ignored_interfaces" "/etc/config/${packageName}" && sed -i 's/ignored_interfaces/ignored_interface/g' "/etc/config/${packageName}"
  807. grep -q "supported_interfaces" "/etc/config/${packageName}" && sed -i 's/supported_interfaces/supported_interface/g' "/etc/config/${packageName}"
  808. grep -q "local_addresses" "/etc/config/${packageName}" && sed -i 's/local_addresses/local_address/g' "/etc/config/${packageName}"
  809. grep -q "local_ports" "/etc/config/${packageName}" && sed -i 's/local_ports/local_port/g' "/etc/config/${packageName}"
  810. grep -q "remote_addresses" "/etc/config/${packageName}" && sed -i 's/remote_addresses/remote_address/g' "/etc/config/${packageName}"
  811. grep -q "remote_ports" "/etc/config/${packageName}" && sed -i 's/remote_ports/remote_port/g' "/etc/config/${packageName}"
  812. grep -q "ipset_enabled" "/etc/config/${packageName}" && sed -i 's/ipset_enabled/dest_ipset/g' "/etc/config/${packageName}"
  813. grep -q "dnsmasq_enabled" "/etc/config/${packageName}" && sed -i 's/dnsmasq_enabled/resolver_ipset/g' "/etc/config/${packageName}"
  814. grep -q "enable_control" "/etc/config/${packageName}" && sed -i 's/enable_control/webui_enable_column/g' "/etc/config/${packageName}"
  815. grep -q "proto_control" "/etc/config/${packageName}" && sed -i 's/proto_control/webui_protocol_column/g' "/etc/config/${packageName}"
  816. grep -q "chain_control" "/etc/config/${packageName}" && sed -i 's/chain_control/webui_chain_column/g' "/etc/config/${packageName}"
  817. grep -q "sort_control" "/etc/config/${packageName}" && sed -i 's/sort_control/webui_sorting/g' "/etc/config/${packageName}"
  818. grep -q "local_address" "/etc/config/${packageName}" && sed -i 's/local_address/src_addr/g' "/etc/config/${packageName}"
  819. grep -q "local_port" "/etc/config/${packageName}" && sed -i 's/local_port/src_port/g' "/etc/config/${packageName}"
  820. grep -q "remote_address" "/etc/config/${packageName}" && sed -i 's/remote_address/dest_addr/g' "/etc/config/${packageName}"
  821. grep -q "remote_port" "/etc/config/${packageName}" && sed -i 's/remote_port/dest_port/g' "/etc/config/${packageName}"
  822. grep -q "local_ipset" "/etc/config/${packageName}" && sed -i 's/local_ipset/src_ipset/g' "/etc/config/${packageName}"
  823. grep -q "remote_ipset" "/etc/config/${packageName}" && sed -i 's/remote_ipset/dest_ipset/g' "/etc/config/${packageName}"
  824. dest_ipset="$(uci -q get $packageName.config.dest_ipset)"
  825. src_ipset="$(uci -q get $packageName.config.src_ipset)"
  826. resolver_ipset="$(uci -q get $packageName.config.resolver_ipset)"
  827. if [ -n "$dest_ipset" ] && [ "$dest_ipset" != "0" ] && [ "$dest_ipset" != "1" ]; then
  828. uci set "$packageName".config.dest_ipset='0'
  829. if [ -z "$resolver_ipset" ]; then
  830. uci set "$packageName".config.resolver_ipset='dnsmasq.ipset'
  831. fi
  832. uci commit "$packageName"
  833. fi
  834. if [ -n "$src_ipset" ] && [ "$src_ipset" != "0" ] && [ "$src_ipset" != "1" ]; then
  835. uci set "$packageName".config.src_ipset='1'
  836. uci commit "$packageName"
  837. fi
  838. if [ -z "$(uci -q get $packageName.config.webui_supported_protocol)" ]; then
  839. uci add_list "$packageName".config.webui_supported_protocol='tcp'
  840. uci add_list "$packageName".config.webui_supported_protocol='udp'
  841. uci add_list "$packageName".config.webui_supported_protocol='tcp udp'
  842. uci add_list "$packageName".config.webui_supported_protocol='icmp'
  843. uci add_list "$packageName".config.webui_supported_protocol='all'
  844. uci commit "$packageName"
  845. fi
  846. for i in append_local_rules append_src_rules \
  847. append_remote_rules append_dest_rules; do
  848. if [ -n "$(uci -q get $packageName.config.$i)" ]; then
  849. warningSummary="${warningSummary}$_WARNING_: $i setting is not supported in ${serviceName}.\\n"
  850. fi
  851. done
  852. for i in udp_proto_enabled forward_chain_enabled input_chain_enabled \
  853. output_chain_enabled iprule_enabled; do
  854. if [ "$(uci -q get $packageName.config.$i)" = "1" ]; then
  855. warningSummary="${warningSummary}$_WARNING_: $i setting is not supported in ${serviceName}.\\n"
  856. fi
  857. done
  858. }
  859. check_config(){ local en; config_get_bool en "$1" 'enabled' 1; [ "$en" -gt 0 ] && _cfg_enabled=0; }
  860. is_config_enabled(){
  861. local cfg="$1" _cfg_enabled=1
  862. [ -n "$1" ] || return 1
  863. config_load "$packageName"
  864. config_foreach check_config "$cfg"
  865. return "$_cfg_enabled"
  866. }
  867. process_user_file(){
  868. local path enabled shellBin="${SHELL:-/bin/ash}"
  869. config_get_bool enabled "$1" 'enabled' 1
  870. config_get path "$1" 'path'
  871. [ "$enabled" -gt 0 ] || return 0
  872. if [ ! -s "$path" ]; then
  873. errorSummary="${errorSummary}${_ERROR_}: Custom user file '$path' not found or empty!\\n"
  874. output_fail
  875. return 1
  876. fi
  877. if ! $shellBin -n "$path"; then
  878. errorSummary="${errorSummary}${_ERROR_}: Syntax error in custom user file '$path'!\\n"
  879. output_fail
  880. return 1
  881. fi
  882. output 2 "Running $path "
  883. # shellcheck disable=SC1090
  884. if ! . "$path"; then
  885. errorSummary="${errorSummary}${_ERROR_}: Error running custom user file '$path'!\\n"
  886. if grep -q -w 'curl' "$path" && ! is_present 'curl'; then
  887. errorSummary="${errorSummary}${_ERROR_}: Use of 'curl' is detected in custom user file '$path', but 'curl' isn't installed!\\n"
  888. errorSummary="${errorSummary}${_ERROR_}: If 'curl' is needed, install it with 'opkg update; opkg install curl;' command in CLI.\\n"
  889. fi
  890. output_fail
  891. return 1
  892. else
  893. output_ok
  894. return 0
  895. fi
  896. }
  897. boot() { rc_procd start_service && rc_procd service_triggers; }
  898. start_service() {
  899. local dnsmasqStoredHash dnsmasqNewHash i modprobeStatus=0 reloadedIface="$1"
  900. convert_config
  901. is_enabled 'on_start' || return 1
  902. is_wan_up || return 1
  903. iptables -t 'mangle' --list 'VPR_PREROUTING' >/dev/null 2>&1 || unset reloadedIface
  904. [ -n "$(tmpfs get gateway)" ] || unset reloadedIface
  905. if [ -s "$dnsmasqFile" ]; then
  906. dnsmasqStoredHash="$(md5sum $dnsmasqFile | awk '{ print $1; }')"
  907. rm -f "$dnsmasqFile"
  908. fi
  909. for i in xt_set ip_set ip_set_hash_ip; do
  910. modprobe "$i" >/dev/null 2>/dev/null || modprobeStatus=$((modprobeStatus + 1))
  911. done
  912. if [ "$modprobeStatus" -gt 0 ] && ! is_chaos_calmer; then
  913. errorSummary="${errorSummary}${_ERROR_}: Failed to load kernel modules\\n"
  914. fi
  915. if [ -z "$reloadedIface" ]; then
  916. for i in $usedChainsList; do
  917. ipt -t mangle -N "VPR_${i}"
  918. ipt -t mangle "$insertOption" "$i" -m mark --mark "0x0/${fwMask}" -j "VPR_${i}"
  919. done
  920. fi
  921. if [ -z "$reloadedIface" ]; then
  922. output 1 'Processing Interfaces '
  923. config_load 'network'; config_foreach process_interface 'interface' 'create';
  924. process_tor_interface 'tor' 'destroy'; is_tor_running && process_tor_interface 'tor' 'create';
  925. output 1 '\n'
  926. if is_config_enabled 'policy'; then
  927. output 1 'Processing Policies '
  928. config_load "$packageName"; config_foreach process_policy 'policy' "$reloadedIface";
  929. output 1 '\n'
  930. fi
  931. if is_config_enabled 'include'; then
  932. output 1 'Processing User File(s) '
  933. config_load "$packageName"; config_foreach process_user_file 'include';
  934. output 1 '\n'
  935. fi
  936. else
  937. output 1 "Reloading Interface: $reloadedIface "
  938. config_load 'network'; config_foreach process_interface 'interface' 'create' "$reloadedIface";
  939. output 1 '\n'
  940. fi
  941. if [ -s "$dnsmasqFile" ]; then
  942. dnsmasqNewHash="$(md5sum $dnsmasqFile | awk '{ print $1; }')"
  943. fi
  944. [ "$dnsmasqNewHash" != "$dnsmasqStoredHash" ] && dnsmasq_restart
  945. if [ -z "$gatewaySummary" ]; then
  946. errorSummary="${errorSummary}${_ERROR_}: failed to set up any gateway!\\n"
  947. fi
  948. procd_open_instance "main"
  949. procd_set_param command /bin/true
  950. procd_set_param stdout 1
  951. procd_set_param stderr 1
  952. procd_open_data
  953. json_add_array 'status'
  954. json_add_object ''
  955. [ -n "$gatewaySummary" ] && json_add_string gateway "$gatewaySummary"
  956. [ -n "$errorSummary" ] && json_add_string error "$errorSummary"
  957. [ -n "$warningSummary" ] && json_add_string warning "$warningSummary"
  958. if [ "$strictMode" -ne 0 ] && str_contains "$gatewaySummary" '0.0.0.0'; then
  959. json_add_string mode "strict"
  960. fi
  961. json_close_object
  962. json_close_array
  963. procd_close_data
  964. procd_close_instance
  965. }
  966. tmpfs() {
  967. local action="$1" param="$2" value="$3"
  968. # shellcheck disable=SC2034
  969. local gateway error warning mode i
  970. if [ -s "$jsonFile" ]; then
  971. json_load_file "$jsonFile" 2>/dev/null
  972. json_select 'status' 2>/dev/null
  973. for i in gateway error warning mode; do
  974. json_get_var $i "$i" 2>/dev/null
  975. done
  976. fi
  977. case "$action" in
  978. get)
  979. printf "%b" "$(eval echo "\$$param")"; return;;
  980. add)
  981. eval "$param"='$(eval echo "\$$param")${value}';;
  982. del)
  983. case "$param" in
  984. all)
  985. unset gateway error warning mode;;
  986. *)
  987. unset "$param";;
  988. esac
  989. ;;
  990. set)
  991. eval "$param"='$value';;
  992. esac
  993. json_init
  994. json_add_object 'status'
  995. json_add_string version "$PKG_VERSION"
  996. for i in gateway error warning mode; do
  997. json_add_string "$i" "$(eval echo "\$$i")"
  998. done
  999. json_close_object
  1000. json_dump > "$jsonFile"
  1001. sync
  1002. }
  1003. service_started() {
  1004. tmpfs set 'gateway' "$gatewaySummary"
  1005. tmpfs set 'error' "$errorSummary"
  1006. tmpfs set 'warning' "$warningSummary"
  1007. if [ "$strictMode" -ne 0 ] && str_contains "$gatewaySummary" '0.0.0.0'; then
  1008. tmpfs set 'mode' 'strict'
  1009. fi
  1010. [ -n "$gatewaySummary" ] && output "$serviceName started with gateways:\\n${gatewaySummary}"
  1011. [ -n "$errorSummary" ] && output "${errorSummary}"
  1012. [ -n "$warningSummary" ] && output "${warningSummary}"
  1013. if [ -n "$errorSummary" ]; then
  1014. return 2
  1015. elif [ -n "$warningSummary" ]; then
  1016. return 1
  1017. else
  1018. return 0
  1019. fi
  1020. }
  1021. stop_service() {
  1022. local i
  1023. iptables -t mangle -L | grep -q VPR_PREROUTING || return 0
  1024. load_package_config
  1025. for i in PREROUTING FORWARD INPUT OUTPUT; do
  1026. ipt -t mangle -D "${i}" -m mark --mark "0x0/${fwMask}" -j "VPR_${i}"
  1027. ipt -t mangle -F "VPR_${i}"; ipt -t mangle -X "VPR_${i}";
  1028. done
  1029. config_load 'network'; config_foreach process_interface 'interface' 'destroy';
  1030. process_tor_interface 'tor' 'destroy'
  1031. unset ifaceTableID; unset ifaceMark;
  1032. if [ -s "$dnsmasqFile" ]; then
  1033. rm -f "$dnsmasqFile"
  1034. dnsmasq_restart
  1035. fi
  1036. if [ "$serviceEnabled" -ne 0 ]; then
  1037. output "$serviceName stopped "; output_okn;
  1038. fi
  1039. }
  1040. reload_interface() { rc_procd start_service "$1"; }
  1041. service_triggers() {
  1042. local n
  1043. is_enabled || return 1
  1044. if [ "$procdReloadDelay" -gt 0 ] && [ "$procdReloadDelay" -lt 100 ]; then
  1045. # shellcheck disable=SC2034
  1046. PROCD_RELOAD_DELAY=$(( procdReloadDelay * 1000 ))
  1047. fi
  1048. procd_open_validate
  1049. validate_config
  1050. validate_policy
  1051. validate_include
  1052. procd_close_validate
  1053. procd_open_trigger
  1054. procd_add_reload_trigger 'openvpn'
  1055. if type procd_add_service_trigger 1>/dev/null 2>&1; then
  1056. procd_add_service_trigger "service.restart" "firewall" /etc/init.d/${packageName} reload
  1057. fi
  1058. procd_add_config_trigger "config.change" "${packageName}" /etc/init.d/${packageName} reload
  1059. for n in $ifSupported; do
  1060. procd_add_interface_trigger "interface.*" "$n" /etc/init.d/${packageName} reload_interface "$n"
  1061. done
  1062. procd_close_trigger
  1063. output 3 "$serviceName monitoring interfaces: $ifSupported"; output_okn;
  1064. }
  1065. status_service() { support "$@"; }
  1066. support() {
  1067. local dist vers out id s param status set_d set_p tableCount i=0 dev dev6 j
  1068. readonly _SEPARATOR_='============================================================'
  1069. is_enabled
  1070. json_load "$(ubus call system board)"; json_select release; json_get_var dist distribution; json_get_var vers version
  1071. if [ -n "$wanIface4" ]; then
  1072. network_get_gateway wanGW4 "$wanIface4"
  1073. [ -z "$dev" ] && dev="$(uci -q get network."${wanIface4}".ifname)"
  1074. [ -z "$dev" ] && dev="$(uci -q get network."${wanIface4}".device)"
  1075. fi
  1076. if [ -n "$wanIface6" ]; then
  1077. [ -z "$dev6" ] && dev6="$(uci -q get network."${wanIface6}".ifname)"
  1078. [ -z "$dev6" ] && dev6="$(uci -q get network."${wanIface6}".device)"
  1079. wanGW6=$(ip -6 route show | grep -m1 " dev $dev6 " | awk '{print $1}')
  1080. [ "$wanGW6" = "default" ] && wanGW6=$(ip -6 route show | grep -m1 " dev $dev6 " | awk '{print $3}')
  1081. fi
  1082. while [ "${1:0:1}" = "-" ]; do param="${1//-/}"; eval "set_$param=1"; shift; done
  1083. [ -e "/var/${packageName}-support" ] && rm -f "/var/${packageName}-support"
  1084. status="$serviceName running on $dist $vers."
  1085. [ -n "$wanIface4" ] && status="$status WAN (IPv4): ${wanIface4}/${dev}/${wanGW4:-0.0.0.0}."
  1086. [ -n "$wanIface6" ] && status="$status WAN (IPv6): ${wanIface6}/${dev6}/${wanGW6:-::/0}."
  1087. {
  1088. echo "$status"
  1089. echo "$_SEPARATOR_"
  1090. dnsmasq --version 2>/dev/null | sed '/^$/,$d'
  1091. if [ -n "$1" ]; then
  1092. echo "$_SEPARATOR_"
  1093. echo "Resolving domains"
  1094. for i in $1; do
  1095. echo "$i: $(resolveip "$i" | tr '\n' ' ')"
  1096. done
  1097. fi
  1098. echo "$_SEPARATOR_"
  1099. echo "Routes/IP Rules"
  1100. tableCount=$(ip rule list | grep -c 'fwmark') || tableCount=0
  1101. if [ -n "$set_d" ]; then route; else route | grep '^default'; fi
  1102. if [ -n "$set_d" ]; then ip rule list; fi
  1103. i=0; while [ $i -lt $tableCount ]; do
  1104. echo ""
  1105. echo "IPv4 Table $((wanTableID + i)): $(ip -4 route show table $((wanTableID + i)))"
  1106. echo "IPv4 Table $((wanTableID + i)) Rules:"
  1107. ip -4 rule list table "$((wanTableID + i))"
  1108. i=$((i + 1))
  1109. done
  1110. if [ "$ipv6Enabled" -ne 0 ]; then
  1111. i=0; while [ $i -lt $tableCount ]; do
  1112. ip -6 route show table $((wanTableID + i)) | while read -r param; do
  1113. echo "IPv6 Table $((wanTableID + i)): $param"
  1114. done
  1115. i=$((i + 1))
  1116. done
  1117. fi
  1118. for j in Mangle NAT; do
  1119. if [ -z "$set_d" ]; then
  1120. for i in $usedChainsList; do
  1121. if iptables -v -t "$(str_to_lower $j)" -S "VPR_${i}" 1>/dev/null 2>&1; then
  1122. echo "$_SEPARATOR_"
  1123. echo "$j IP Table: $i"
  1124. iptables -v -t "$(str_to_lower $j)" -S "VPR_${i}"
  1125. if [ "$ipv6Enabled" -ne 0 ]; then
  1126. echo "$_SEPARATOR_"
  1127. echo "$j IPv6 Table: $i"
  1128. ip6tables -v -t "$(str_to_lower $j)" -S "VPR_${i}"
  1129. fi
  1130. fi
  1131. done
  1132. else
  1133. echo "$_SEPARATOR_"
  1134. echo "$j IP Table"
  1135. iptables -L -t "$(str_to_lower $j)"
  1136. if [ "$ipv6Enabled" -ne 0 ]; then
  1137. echo "$_SEPARATOR_"
  1138. echo "$j IPv6 Table"
  1139. ip6tables -L -t "$(str_to_lower $j)"
  1140. fi
  1141. fi
  1142. i=0; ifaceMark="$wanMark";
  1143. while [ $i -lt $tableCount ]; do
  1144. if iptables -v -t "$(str_to_lower $j)" -S "VPR_MARK${ifaceMark}" 1>/dev/null 2>&1; then
  1145. echo "$_SEPARATOR_"
  1146. echo "$j IP Table MARK Chain: VPR_MARK${ifaceMark}"
  1147. iptables -v -t "$(str_to_lower $j)" -S "VPR_MARK${ifaceMark}"
  1148. ifaceMark="$(printf '0x%06x' $((ifaceMark + wanMark)))";
  1149. fi
  1150. i=$((i + 1))
  1151. done
  1152. done
  1153. echo "$_SEPARATOR_"
  1154. echo "Current ipsets"
  1155. ipset save
  1156. if [ -s "$dnsmasqFile" ]; then
  1157. echo "$_SEPARATOR_"
  1158. echo "DNSMASQ ipsets"
  1159. cat "$dnsmasqFile"
  1160. fi
  1161. echo "$_SEPARATOR_"
  1162. } | tee -a /var/${packageName}-support
  1163. if [ -n "$set_p" ]; then
  1164. printf "%b" "Pasting to paste.ee... "
  1165. if is_present 'curl' && is_variant_installed 'libopenssl' && is_installed 'ca-bundle'; then
  1166. json_init; json_add_string "description" "${packageName}-support"
  1167. json_add_array "sections"; json_add_object '0'
  1168. json_add_string "name" "$(uci -q get system.@system[0].hostname)"
  1169. json_add_string "contents" "$(cat /var/${packageName}-support)"
  1170. json_close_object; json_close_array; payload=$(json_dump)
  1171. out=$(curl -s -k "https://api.paste.ee/v1/pastes" -X "POST" -H "Content-Type: application/json" -H "X-Auth-Token:uVOJt6pNqjcEWu7qiuUuuxWQafpHhwMvNEBviRV2B" -d "$payload")
  1172. json_load "$out"; json_get_var id id; json_get_var s success
  1173. [ "$s" = "1" ] && printf "%b" "https://paste.ee/p/$id $__OK__\\n" || printf "%b" "$__FAIL__\\n"
  1174. [ -e "/var/${packageName}-support" ] && rm -f "/var/${packageName}-support"
  1175. else
  1176. printf "%b" "$__FAIL__\\n"
  1177. printf "%b" "$_ERROR_: curl, libopenssl or ca-bundle were not found!\\nRun 'opkg update; opkg install curl libopenssl ca-bundle' to install them.\\n"
  1178. fi
  1179. else
  1180. printf "%b" "Your support details have been logged to '/var/${packageName}-support'. $__OK__\\n"
  1181. fi
  1182. }
  1183. # shellcheck disable=SC2120
  1184. validate_config() {
  1185. uci_validate_section "${packageName}" config "${1}" \
  1186. 'enabled:bool:0' \
  1187. 'strict_enforcement:bool:1' \
  1188. 'ipv6_enabled:bool:0' \
  1189. 'src_ipset:bool:0' \
  1190. 'dest_ipset:bool:0' \
  1191. 'resolver_ipset::or("", "none", "dnsmasq.ipset")' \
  1192. 'verbosity:range(0,2):1' \
  1193. 'wan_tid:integer:201' \
  1194. 'wan_fw_mark:hex(8)' \
  1195. 'fw_mask:hex(8)' \
  1196. 'icmp_interface:string' \
  1197. 'ignored_interface:list(string)' \
  1198. 'supported_interface:list(string)' \
  1199. 'boot_timeout:integer:30' \
  1200. 'iptables_rule_option:or("", "append", "insert")' \
  1201. 'procd_reload_delay:integer:0' \
  1202. 'webui_enable_column:bool:0' \
  1203. 'webui_protocol_column:bool:0' \
  1204. 'webui_supported_protocol:list(string)' \
  1205. 'webui_chain_column:bool:0' \
  1206. 'webui_sorting:bool:1' \
  1207. 'webui_show_ignore_target:bool:0'
  1208. }
  1209. # shellcheck disable=SC2120
  1210. validate_policy() {
  1211. uci_validate_section "${packageName}" policy "${1}" \
  1212. 'name:string' \
  1213. 'enabled:bool:0' \
  1214. 'interface:network' \
  1215. 'proto:or(string)' \
  1216. 'chain:or("", "PREROUTING", "FORWARD", "INPUT", "OUTPUT")' \
  1217. 'src_addr:list(neg(or(host,network,macaddr)))' \
  1218. 'src_port:list(neg(or(portrange, string)))' \
  1219. 'dest_addr:list(neg(host))' \
  1220. 'dest_port:list(neg(or(portrange, string)))'
  1221. }
  1222. # shellcheck disable=SC2120
  1223. validate_include() {
  1224. uci_validate_section "${packageName}" include "${1}" \
  1225. 'path:string' \
  1226. 'enabled:bool:0'
  1227. }