mirror of the now-defunct rocklinux.org
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.

235 lines
4.5 KiB

  1. iptables_init_if() {
  2. if isfirst "iptables_$if"; then
  3. addcode up 1 1 "iptables -N firewall_$if"
  4. addcode up 2 2 "iptables -A INPUT -i $if -j firewall_$if"
  5. addcode up 1 3 "iptables -A firewall_$if `
  6. `-m state --state ESTABLISHED,RELATED -j ACCEPT"
  7. addcode down 1 3 "iptables -F firewall_$if"
  8. addcode down 1 2 "iptables -D INPUT -i $if -j firewall_$if"
  9. addcode down 1 1 "iptables -X firewall_$if"
  10. fi
  11. }
  12. iptables_parse_conditions() {
  13. iptables_cond=""
  14. while [ -n "$1" ]
  15. do
  16. case "$1" in
  17. all)
  18. shift
  19. ;;
  20. icmp)
  21. iptables_cond="$iptables_cond -p icmp --icmp-type $2"
  22. shift; shift
  23. ;;
  24. from)
  25. case "$2" in
  26. all)
  27. shift; shift;
  28. ;;
  29. tcp|udp)
  30. if [[ "$iptables_cond" == *-p* ]] ; then
  31. if [[ "$iptables_cond" != *"-p $2"* ]] ; then
  32. error "Specify either tcp or udp rules"
  33. else
  34. iptables_cond="$iptables_cond --sport $3"
  35. fi
  36. else
  37. iptables_cond="$iptables_cond -p $2 --sport $3"
  38. fi
  39. shift; shift; shift;
  40. ;;
  41. ip)
  42. iptables_cond="$iptables_cond -s $3"
  43. shift; shift; shift;
  44. ;;
  45. *)
  46. error "Unknown source type $2";
  47. shift; shift;
  48. ;;
  49. esac
  50. ;;
  51. to)
  52. case "$2" in
  53. all)
  54. shift; shift;
  55. ;;
  56. tcp|udp)
  57. if [[ "$iptables_cond" == *-p* ]] ; then
  58. if [[ "$iptables_cond" != *"-p $2"* ]] ; then
  59. error "Specify either tcp or udp rules"
  60. else
  61. iptables_cond="$iptables_cond --dport $3"
  62. fi
  63. else
  64. iptables_cond="$iptables_cond -p $2 --dport $3"
  65. fi
  66. shift; shift; shift;
  67. ;;
  68. ip)
  69. iptables_cond="$iptables_cond -d $3"
  70. shift; shift; shift;
  71. ;;
  72. *)
  73. error "Unknown destination type $2";
  74. shift; shift;
  75. ;;
  76. esac
  77. ;;
  78. *)
  79. error "Unkown firewall condition: $1"
  80. shift
  81. esac
  82. done
  83. }
  84. public_accept() {
  85. iptables_parse_conditions "$@"
  86. addcode up 1 5 "iptables -A firewall_$if $iptables_cond -j ACCEPT"
  87. iptables_init_if
  88. }
  89. public_reject() {
  90. iptables_parse_conditions "$@"
  91. addcode up 1 5 "iptables -A firewall_$if $iptables_cond -j REJECT"
  92. iptables_init_if
  93. }
  94. public_drop() {
  95. iptables_parse_conditions "$@"
  96. addcode up 1 5 "iptables -A firewall_$if $iptables_cond -j DROP"
  97. iptables_init_if
  98. }
  99. public_log() {
  100. iptables_parse_conditions "$@"
  101. addcode up 1 5 "iptables -A firewall_$if $iptables_cond -j LOG"
  102. iptables_init_if
  103. }
  104. public_clamp_mtu() {
  105. addcode up 1 6 "iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN \
  106. -j TCPMSS --clamp-mss-to-pmtu"
  107. addcode down 9 6 "iptables -D FORWARD -p tcp --tcp-flags SYN,RST SYN \
  108. -j TCPMSS --clamp-mss-to-pmtu"
  109. }
  110. public_masquerade() {
  111. iptables_parse_conditions "$@"
  112. addcode up 1 6 "iptables -t nat -A POSTROUTING $iptables_cond -o $if \
  113. -j MASQUERADE"
  114. addcode down 9 6 "iptables -t nat -D POSTROUTING $iptables_cond -o $if \
  115. -j MASQUERADE"
  116. }
  117. public_fw() {
  118. # fw <policy> <chainspec> <iptables_cond>
  119. # fw accept in from ip
  120. # fw reject out to ip jumpto BLUBBER
  121. # fw accept forward jump MYCHAIN to ip
  122. case "$1" in
  123. accept)
  124. target=ACCEPT
  125. shift
  126. ;;
  127. reject)
  128. target=REJECT
  129. shift
  130. ;;
  131. drop)
  132. target=DROP
  133. shift
  134. ;;
  135. return)
  136. target=RETURN
  137. shift
  138. ;;
  139. log)
  140. target=LOG
  141. shift
  142. ;;
  143. *)
  144. target=$1
  145. shift
  146. ;;
  147. esac
  148. if [ "$if" != "none" ]; then
  149. case "$1" in
  150. out|outgoing)
  151. chain=OUTPUT
  152. dir=o
  153. shift
  154. ;;
  155. in|incoming)
  156. chain=INPUT
  157. dir=i
  158. shift
  159. ;;
  160. *)
  161. if [ "$1" == "forward" ]; then
  162. chain=FORWARD
  163. else
  164. chain=$1
  165. fi
  166. shift
  167. case "$1" in
  168. in|incoming)
  169. dir=i
  170. shift;
  171. ;;
  172. out|outgoing)
  173. dir=o
  174. shift;
  175. ;;
  176. *)
  177. error "Not a forward interface specification: $1"
  178. ;;
  179. esac
  180. ;;
  181. esac
  182. else
  183. case "$1" in
  184. out|outgoing)
  185. chain=OUTPUT
  186. shift
  187. ;;
  188. in|incoming)
  189. chain=INPUT
  190. shift
  191. ;;
  192. forward|FORWARD)
  193. chain=FORWARD
  194. shift
  195. ;;
  196. *)
  197. chain=$1
  198. shift
  199. ;;
  200. esac
  201. if [ "$1" == "policy" ]; then
  202. addcode up 1 6 "iptables -P $chain $target"
  203. shift
  204. return
  205. fi
  206. fi
  207. iptables_parse_conditions "$@"
  208. $iptables -L $chain -n >/dev/null 2>/dev/null || {
  209. if isfirst "iptables_$chain" && \
  210. [ "$chain" != "INPUT" ] && [ "$chain" != "OUTPUT" ] && \
  211. [ "$chain" != "FORWARD" ] ; then
  212. addcode up 1 1 "iptables -N $chain"
  213. addcode down 1 1 "iptables -X $chain"
  214. fi
  215. }
  216. if [ "$if" == "none" -o "$dir" == "" ]; then
  217. ifspec=""
  218. else
  219. iptables_init_if
  220. ifspec="-$dir $if"
  221. fi
  222. addcode up 1 6 "iptables -A $chain $iptables_cond $ifspec -j $target"
  223. addcode down 9 6 "iptables -D $chain $iptables_cond $ifspec -j $target"
  224. }