reflection.hotplug 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. local net
  45. for net in $(find_networks "$dest"); do
  46. local lanip=$(uci -P/var/state get network.$net.ipaddr)
  47. local lanmk=$(uci -P/var/state get network.$net.netmask)
  48. local proto
  49. config_get proto "$cfg" proto
  50. local epmin epmax extport
  51. config_get extport "$cfg" src_dport
  52. [ -n "$extport" ] || return
  53. epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
  54. [ "$epmin" != "$epmax" ] || epmax=""
  55. local ipmin ipmax intport
  56. config_get intport "$cfg" dest_port "$extport"
  57. ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
  58. [ "$ipmin" != "$ipmax" ] || ipmax=""
  59. local exthost
  60. config_get exthost "$cfg" src_dip "$wanip"
  61. local inthost
  62. config_get inthost "$cfg" dest_ip
  63. [ -n "$inthost" ] || return
  64. [ "$proto" = tcpudp ] && proto="tcp udp"
  65. [ "${inthost#!}" = "$inthost" ] || return 0
  66. [ "${exthost#!}" = "$exthost" ] || return 0
  67. local p
  68. for p in ${proto:-tcp udp}; do
  69. case "$p" in
  70. tcp|udp)
  71. iptables -t nat -A nat_reflection_in \
  72. -s $lanip/$lanmk -d $exthost \
  73. -p $p --dport $epmin${epmax:+:$epmax} \
  74. -j DNAT --to $inthost:$ipmin${ipmax:+-$ipmax}
  75. iptables -t nat -A nat_reflection_out \
  76. -s $lanip/$lanmk -d $inthost \
  77. -p $p --dport $ipmin${ipmax:+:$ipmax} \
  78. -j SNAT --to-source $lanip
  79. iptables -t filter -A nat_reflection_fwd \
  80. -s $lanip/$lanmk -d $inthost \
  81. -p $p --dport $ipmin${ipmax:+:$ipmax} \
  82. -j ACCEPT
  83. ;;
  84. esac
  85. done
  86. done
  87. }
  88. }
  89. config_load firewall
  90. config_foreach setup_fwd redirect
  91. fi