reflection.hotplug 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. local p
  69. for p in ${proto:-tcp udp}; do
  70. case "$p" in
  71. tcp|udp)
  72. iptables -t nat -A nat_reflection_in \
  73. -s $lanip/$lanmk -d $exthost \
  74. -p $p --dport $epmin${epmax:+:$epmax} \
  75. -j DNAT --to $inthost:$ipmin${ipmax:+-$ipmax}
  76. iptables -t nat -A nat_reflection_out \
  77. -s $lanip/$lanmk -d $inthost \
  78. -p $p --dport $ipmin${ipmax:+:$ipmax} \
  79. -j SNAT --to-source $lanip
  80. iptables -t filter -A nat_reflection_fwd \
  81. -s $lanip/$lanmk -d $inthost \
  82. -p $p --dport $ipmin${ipmax:+:$ipmax} \
  83. -j ACCEPT
  84. ;;
  85. esac
  86. done
  87. done
  88. }
  89. }
  90. config_load firewall
  91. config_foreach setup_fwd redirect
  92. fi