OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.0 KiB

  1. # --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # T2 SDE: package/.../iptables/rocknet_iptables.sh
  5. # Copyright (C) 2004 - 2006 The T2 SDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; version 2 of the License. A copy of the
  12. # GNU General Public License can be found in the file COPYING.
  13. # --- T2-COPYRIGHT-NOTE-END ---
  14. iptables_init_if() {
  15. if isfirst "iptables_$if"; then
  16. # prepare INPUT
  17. addcode up 1 1 "iptables -N firewall_$if"
  18. addcode up 1 2 "iptables -A INPUT -i $if `
  19. `-m state --state ESTABLISHED,RELATED -j ACCEPT"
  20. addcode up 1 3 "iptables -A INPUT -i $if -j firewall_$if"
  21. # prepare FORWARD
  22. addcode up 1 1 "iptables -N forward_$if"
  23. addcode up 1 2 "iptables -A FORWARD -i $if `
  24. `-m state --state ESTABLISHED,RELATED -j ACCEPT"
  25. addcode up 1 3 "iptables -A FORWARD -i $if -j forward_$if"
  26. # clean INPUT
  27. addcode down 1 3 "iptables -F firewall_$if"
  28. addcode down 1 2 "iptables -D INPUT -i $if -j firewall_$if"
  29. addcode down 1 2 "iptables -D INPUT -i $if `
  30. `-m state --state ESTABLISHED,RELATED -j ACCEPT"
  31. addcode down 1 1 "iptables -X firewall_$if"
  32. # clean FORWARD
  33. addcode down 1 3 "iptables -F forward_$if"
  34. addcode down 1 2 "iptables -D FORWARD -i $if -j forward_$if"
  35. addcode down 1 2 "iptables -D FORWARD -i $if `
  36. `-m state --state ESTABLISHED,RELATED -j ACCEPT"
  37. addcode down 1 1 "iptables -X forward_$if"
  38. fi
  39. }
  40. iptables_parse_conditions() {
  41. iptables_cond=""
  42. while [ -n "$1" ]
  43. do
  44. case "$1" in
  45. all)
  46. shift
  47. ;;
  48. tcp|udp)
  49. iptables_cond="$iptables_cond -p $1 --dport $2"
  50. shift; shift
  51. ;;
  52. icmp)
  53. iptables_cond="$iptables_cond -p icmp --icmp-type $2"
  54. shift; shift
  55. ;;
  56. ip)
  57. iptables_cond="$iptables_cond -s $2"
  58. shift; shift
  59. ;;
  60. *)
  61. error "Unkown accept/reject/drop condition: $1"
  62. shift
  63. esac
  64. done
  65. }
  66. public_accept() {
  67. iptables_parse_conditions "$@"
  68. local level=6; [ "$ip" ] && level=5
  69. addcode up 1 $level "iptables -A firewall_$if ${ip:+-d $ip} $iptables_cond -j ACCEPT"
  70. iptables_init_if
  71. }
  72. public_reject() {
  73. iptables_parse_conditions "$@"
  74. local level=6; [ "$ip" ] && level=5
  75. addcode up 1 $level "iptables -A firewall_$if ${ip:+-d $ip} $iptables_cond -j REJECT"
  76. iptables_init_if
  77. }
  78. public_drop() {
  79. iptables_parse_conditions "$@"
  80. local level=6; [ "$ip" ] && level=5
  81. addcode up 1 $level "iptables -A firewall_$if ${ip:+-d $ip} $iptables_cond -j DROP"
  82. iptables_init_if
  83. }
  84. public_restrict() {
  85. iptables_parse_conditions "$@"
  86. local level=6; [ "$ip" ] && level=5
  87. addcode up 1 $level "iptables -A forward_$if ${ip:+-d $ip} $iptables_cond -j DROP"
  88. iptables_init_if
  89. }
  90. public_conduit() {
  91. # conduit (tcp|udp) port targetip[:targetport]
  92. #
  93. local proto=$1 port=$2
  94. local targetip=$3 targetport=$2
  95. if [ "${targetip/:/}" != "$targetip" ]; then
  96. targetport=${targetip#*:}
  97. targetip=${targetip%:*}
  98. fi
  99. addcode up 1 4 "iptables -t nat -A PREROUTING -i $if ${ip:+-d $ip} -p $proto \
  100. --dport $port -j DNAT --to $targetip:$targetport"
  101. addcode up 1 4 "iptables -A forward_$if -p $proto -d $targetip \
  102. --dport $targetport -j ACCEPT"
  103. iptables_init_if
  104. }
  105. public_clamp_mtu() {
  106. addcode up 1 1 "iptables -A FORWARD ${if:+-o $if} -p tcp --tcp-flags SYN,RST SYN \
  107. -j TCPMSS --clamp-mss-to-pmtu"
  108. addcode down 9 1 "iptables -D FORWARD ${if:+-o $if} -p tcp --tcp-flags SYN,RST SYN \
  109. -j TCPMSS --clamp-mss-to-pmtu"
  110. }
  111. public_masquerade() {
  112. if [ "$ip" ]; then
  113. addcode up 1 6 "iptables -t nat -A POSTROUTING ${1:+-s $1} -o $if \
  114. -j SNAT --to $ip"
  115. addcode down 9 6 "iptables -t nat -D POSTROUTING ${1:+-s $1} -o $if \
  116. -j SNAT --to $ip"
  117. else
  118. addcode up 1 6 "iptables -t nat -A POSTROUTING ${1:+-s $1} -o $if \
  119. -j MASQUERADE"
  120. addcode down 9 6 "iptables -t nat -D POSTROUTING ${1:+-s $1} -o $if \
  121. -j MASQUERADE"
  122. fi
  123. }