reflection.hotplug 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. . /etc/functions.sh
  3. . /usr/share/libubox/jshn.sh
  4. find_iface_address()
  5. {
  6. local iface="$1"
  7. local ipaddr="$2"
  8. local prefix="$3"
  9. local idx=1
  10. local tmp="$(ubus call network.interface."$iface" status 2>/dev/null)"
  11. json_load "${tmp:-{}}"
  12. json_get_type tmp address
  13. if [ "$tmp" = array ]; then
  14. json_select address
  15. while true; do
  16. json_get_type tmp $idx
  17. [ "$tmp" = object ] || break
  18. json_select $((idx++))
  19. json_get_var tmp address
  20. case "$tmp" in
  21. *:*) json_select .. ;;
  22. *)
  23. [ -n "$ipaddr" ] && json_get_var $ipaddr address
  24. [ -n "$prefix" ] && json_get_var $prefix mask
  25. return 0
  26. ;;
  27. esac
  28. done
  29. fi
  30. return 1
  31. }
  32. if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
  33. local wanip
  34. find_iface_address wan wanip
  35. [ -n "$wanip" ] || return
  36. iptables -t nat -F nat_reflection_in 2>/dev/null || {
  37. iptables -t nat -N nat_reflection_in
  38. iptables -t nat -A prerouting_rule -j nat_reflection_in
  39. }
  40. iptables -t nat -F nat_reflection_out 2>/dev/null || {
  41. iptables -t nat -N nat_reflection_out
  42. iptables -t nat -A postrouting_rule -j nat_reflection_out
  43. }
  44. iptables -t filter -F nat_reflection_fwd 2>/dev/null || {
  45. iptables -t filter -N nat_reflection_fwd
  46. iptables -t filter -A forwarding_rule -j nat_reflection_fwd
  47. }
  48. find_networks() {
  49. find_networks_cb() {
  50. local cfg="$1"
  51. local zone="$2"
  52. local name
  53. config_get name "$cfg" name
  54. [ "$name" = "$zone" ] && {
  55. local network
  56. config_get network "$cfg" network
  57. echo ${network:-$zone}
  58. return 1
  59. }
  60. }
  61. config_foreach find_networks_cb zone "$1"
  62. }
  63. setup_fwd() {
  64. local cfg="$1"
  65. local reflection
  66. config_get_bool reflection "$cfg" reflection 1
  67. [ "$reflection" == 1 ] || return
  68. local src
  69. config_get src "$cfg" src
  70. local target
  71. config_get target "$cfg" target DNAT
  72. [ "$src" = wan ] && [ "$target" = DNAT ] && {
  73. local dest
  74. config_get dest "$cfg" dest "lan"
  75. [ "$dest" != "*" ] || return
  76. local net
  77. for net in $(find_networks "$dest"); do
  78. local lanip lanmk
  79. find_iface_address "$net" lanip lanmk
  80. [ -n "$lanip" ] || return
  81. local proto
  82. config_get proto "$cfg" proto
  83. local epmin epmax extport
  84. config_get extport "$cfg" src_dport
  85. [ -n "$extport" ] || return
  86. epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
  87. [ "${epmin#!}" != "$epmax" ] || epmax=""
  88. local ipmin ipmax intport
  89. config_get intport "$cfg" dest_port "$extport"
  90. ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
  91. [ "${ipmin#!}" != "$ipmax" ] || ipmax=""
  92. local exthost
  93. config_get exthost "$cfg" src_dip "$wanip"
  94. local inthost
  95. config_get inthost "$cfg" dest_ip
  96. [ -n "$inthost" ] || return
  97. [ "$proto" = tcpudp ] && proto="tcp udp"
  98. [ "${inthost#!}" = "$inthost" ] || return 0
  99. [ "${exthost#!}" = "$exthost" ] || return 0
  100. [ "${epmin#!}" != "$epmin" ] && \
  101. extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
  102. extport="--dport $epmin${epmax:+:$epmax}"
  103. [ "${ipmin#!}" != "$ipmin" ] && \
  104. intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
  105. intport="--dport $ipmin${ipmax:+:$ipmax}"
  106. local p
  107. for p in ${proto:-tcp udp}; do
  108. case "$p" in
  109. tcp|udp|6|17)
  110. iptables -t nat -A nat_reflection_in \
  111. -s $lanip/$lanmk -d $exthost \
  112. -p $p $extport \
  113. -j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
  114. iptables -t nat -A nat_reflection_out \
  115. -s $lanip/$lanmk -d $inthost \
  116. -p $p $intport \
  117. -j SNAT --to-source $lanip
  118. iptables -t filter -A nat_reflection_fwd \
  119. -s $lanip/$lanmk -d $inthost \
  120. -p $p $intport \
  121. -j ACCEPT
  122. ;;
  123. esac
  124. done
  125. done
  126. }
  127. }
  128. config_load firewall
  129. config_foreach setup_fwd redirect
  130. fi