reflection.hotplug 3.1 KB

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