reflection.hotplug 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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
  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" = tcpudp ] && proto="tcp udp"
  68. [ "${inthost#!}" = "$inthost" ] || return 0
  69. [ "${exthost#!}" = "$exthost" ] || return 0
  70. [ "${epmin#!}" != "$epmin" ] && \
  71. extport="! --dport ${epmin#!}${epmax:+:$epmax}" || \
  72. extport="--dport $epmin${epmax:+:$epmax}"
  73. [ "${ipmin#!}" != "$ipmin" ] && \
  74. intport="! --dport ${ipmin#!}${ipmax:+:$ipmax}" || \
  75. intport="--dport $ipmin${ipmax:+:$ipmax}"
  76. local p
  77. for p in ${proto:-tcp udp}; do
  78. case "$p" in
  79. tcp|udp|6|17)
  80. iptables -t nat -A nat_reflection_in \
  81. -s $lannet -d $exthost \
  82. -p $p $extport \
  83. -j DNAT --to $inthost:${ipmin#!}${ipmax:+-$ipmax}
  84. iptables -t nat -A nat_reflection_out \
  85. -s $lannet -d $inthost \
  86. -p $p $intport \
  87. -j SNAT --to-source ${lannet%%/*}
  88. iptables -t filter -A nat_reflection_fwd \
  89. -s $lannet -d $inthost \
  90. -p $p $intport \
  91. -j ACCEPT
  92. ;;
  93. esac
  94. done
  95. done
  96. }
  97. }
  98. config_load firewall
  99. config_foreach setup_fwd redirect
  100. fi