reflection.hotplug 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  2. . /etc/functions.sh
  3. if [ "$ACTION" = "ifup" ] && [ "$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. find_networks() {
  14. find_networks_cb() {
  15. local cfg="$1"
  16. local zone="$2"
  17. local name
  18. config_get name "$cfg" name
  19. [ "$name" = "$zone" ] && {
  20. local network
  21. config_get network "$cfg" network
  22. echo ${network:-$zone}
  23. return 1
  24. }
  25. }
  26. config_foreach find_networks_cb zone "$1"
  27. }
  28. setup_fwd() {
  29. local cfg="$1"
  30. local src
  31. config_get src "$cfg" src
  32. [ "$src" = wan ] && {
  33. local dest
  34. config_get dest "$cfg" dest "lan"
  35. local net
  36. for net in $(find_networks "$dest"); do
  37. local lanip=$(uci -P/var/state get network.$net.ipaddr)
  38. local lanmk=$(uci -P/var/state get network.$net.netmask)
  39. local proto
  40. config_get proto "$cfg" proto
  41. local epmin epmax extport
  42. config_get extport "$cfg" src_dport
  43. [ -n "$extport" ] || return
  44. epmin="${extport%[-:]*}"; epmax="${extport#*[-:]}"
  45. [ "$epmin" != "$epmax" ] || epmax=""
  46. local ipmin ipmax intport
  47. config_get intport "$cfg" dest_port "$extport"
  48. ipmin="${intport%[-:]*}"; ipmax="${intport#*[-:]}"
  49. [ "$ipmin" != "$ipmax" ] || ipmax=""
  50. local exthost
  51. config_get exthost "$cfg" src_dip "$wanip"
  52. local inthost
  53. config_get inthost "$cfg" dest_ip
  54. [ -n "$inthost" ] || return
  55. [ "$proto" = tcpudp ] && proto="tcp udp"
  56. local p
  57. for p in ${proto:-tcp udp}; do
  58. case "$p" in
  59. tcp|udp)
  60. iptables -t nat -A nat_reflection_in \
  61. -s $lanip/$lanmk -d $exthost \
  62. -p $p --dport $epmin${epmax:+:$epmax} \
  63. -j DNAT --to $inthost:$ipmin${ipmax:+-$ipmax}
  64. iptables -t nat -A nat_reflection_out \
  65. -s $lanip/$lanmk -d $inthost \
  66. -p $p --dport $ipmin${ipmax:+:$ipmax} \
  67. -j SNAT --to-source $lanip
  68. ;;
  69. esac
  70. done
  71. done
  72. }
  73. }
  74. config_load firewall
  75. config_foreach setup_fwd redirect
  76. fi