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.

159 lines
4.2 KiB

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