reflection.hotplug 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/sh
  2. . /etc/functions.sh
  3. if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "wan" ]; then
  4. local wanip=$(uci -P/var/state get network.wan.ipaddr)
  5. iptables -t nat -F nat_reflection_in 2>/dev/null || {
  6. iptables -t nat -N nat_reflection_in
  7. iptables -t nat -A prerouting_rule -j nat_reflection_in
  8. }
  9. iptables -t nat -F nat_reflection_out 2>/dev/null || {
  10. iptables -t nat -N nat_reflection_out
  11. iptables -t nat -A postrouting_rule -j nat_reflection_out
  12. }
  13. iptables -t filter -F nat_reflection_fwd 2>/dev/null || {
  14. iptables -t filter -N nat_reflection_fwd
  15. iptables -t filter -A forwarding_rule -j nat_reflection_fwd
  16. }
  17. find_networks() {
  18. find_networks_cb() {
  19. local cfg="$1"
  20. local zone="$2"
  21. local name
  22. config_get name "$cfg" name
  23. [ "$name" = "$zone" ] && {
  24. local network
  25. config_get network "$cfg" network
  26. echo ${network:-$zone}
  27. return 1
  28. }
  29. }
  30. config_foreach find_networks_cb zone "$1"
  31. }
  32. setup_fwd() {
  33. local cfg="$1"
  34. local reflection
  35. config_get_bool reflection "$cfg" reflection 1
  36. [ "$reflection" == 1 ] || return
  37. local src
  38. config_get src "$cfg" src
  39. local target
  40. config_get target "$cfg" target DNAT
  41. [ "$src" = wan ] && [ "$target" = DNAT ] && {
  42. local dest
  43. config_get dest "$cfg" dest "lan"
  44. [ "$dest" != "*" ] || return
  45. local net
  46. for net in $(find_networks "$dest"); do
  47. local lanip=$(uci -P/var/state get network.$net.ipaddr)
  48. local lanmk=$(uci -P/var/state get network.$net.netmask)
  49. local proto
  50. config_get proto "$cfg" proto
  51. local epmin epmax extport
  52. config_get extport "$cfg" src_dport
  53. [ -n "$extport" ] || return
  54. epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
  55. [ "${epmin#!}" != "$epmax" ] || epmax=""
  56. local ipmin ipmax intport
  57. config_get intport "$cfg" dest_port "$extport"
  58. ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
  59. [ "${ipmin#!}" != "$ipmax" ] || ipmax=""
  60. local exthost
  61. config_get exthost "$cfg" src_dip "$wanip"
  62. local inthost
  63. config_get inthost "$cfg" dest_ip
  64. [ -n "$inthost" ] || return
  65. [ "$proto" = tcpudp ] && proto="tcp udp"
  66. [ "${inthost#!}" = "$inthost" ] || return 0
  67. [ "${exthost#!}" = "$exthost" ] || return 0
  68. [ "${epmin#!}" != "$epmin" ] && \
  69. extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
  70. extport="--dport $epmin${epmax:+:$epmax}"
  71. [ "${ipmin#!}" != "$ipmin" ] && \
  72. intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
  73. intport="--dport $ipmin${ipmax:+:$ipmax}"
  74. local p
  75. for p in ${proto:-tcp udp}; do
  76. case "$p" in
  77. tcp|udp)
  78. iptables -t nat -A nat_reflection_in \
  79. -s $lanip/$lanmk -d $exthost \
  80. -p $p $extport \
  81. -j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
  82. iptables -t nat -A nat_reflection_out \
  83. -s $lanip/$lanmk -d $inthost \
  84. -p $p $intport \
  85. -j SNAT --to-source $lanip
  86. iptables -t filter -A nat_reflection_fwd \
  87. -s $lanip/$lanmk -d $inthost \
  88. -p $p $intport \
  89. -j ACCEPT
  90. ;;
  91. esac
  92. done
  93. done
  94. }
  95. }
  96. config_load firewall
  97. config_foreach setup_fwd redirect
  98. fi