uci_firewall.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/bin/sh
  2. # Copyright (C) 2008 John Crispin <[email protected]>
  3. . /etc/functions.sh
  4. IPTABLES="echo iptables"
  5. IPTABLES=iptables
  6. config_clear
  7. include /lib/network
  8. scan_interfaces
  9. CONFIG_APPEND=1
  10. config_load firewall
  11. config fw_zones
  12. ZONE_LIST=$CONFIG_SECTION
  13. DEF_INPUT=DROP
  14. DEF_OUTPUT=DROP
  15. DEF_FORWARD=DROP
  16. load_policy() {
  17. config_get input $1 input
  18. config_get output $1 output
  19. config_get forward $1 forward
  20. [ -z "$input" ] && input=$DEF_INPUT
  21. [ -z "$output" ] && output=$DEF_OUTPUT
  22. [ -z "$forward" ] && forward=$DEF_FORWARD
  23. }
  24. create_zone() {
  25. local exists
  26. [ "$1" == "loopback" ] && return
  27. config_get exists $ZONE_LIST $1
  28. [ -n "$exists" ] && return
  29. config_set $ZONE_LIST $1 1
  30. $IPTABLES -N zone_$1
  31. $IPTABLES -N zone_$1_ACCEPT
  32. $IPTABLES -N zone_$1_DROP
  33. $IPTABLES -N zone_$1_REJECT
  34. $IPTABLES -N zone_$1_forward
  35. $IPTABLES -A zone_$1_forward -j zone_$1_$5
  36. $IPTABLES -A zone_$1 -j zone_$1_$3
  37. $IPTABLES -A OUTPUT -j zone_$1_$4
  38. $IPTABLES -N zone_$1_nat -t nat
  39. $IPTABLES -N zone_$1_prerouting -t nat
  40. [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$2_nat
  41. }
  42. addif() {
  43. local dev
  44. config_get dev core $2
  45. [ -n "$dev" -a "$dev" != "$1" ] && delif "$dev" "$2"
  46. [ -n "$dev" -a "$dev" == "$1" ] && return
  47. logger "adding $1 to firewall zone $2"
  48. $IPTABLES -A INPUT -i $1 -j zone_$2
  49. $IPTABLES -I zone_$2_ACCEPT 1 -o $1 -j ACCEPT
  50. $IPTABLES -I zone_$2_DROP 1 -o $1 -j DROP
  51. $IPTABLES -I zone_$2_REJECT 1 -o $1 -j REJECT
  52. $IPTABLES -I zone_$2_ACCEPT 1 -i $1 -j ACCEPT
  53. $IPTABLES -I zone_$2_DROP 1 -i $1 -j DROP
  54. $IPTABLES -I zone_$2_REJECT 1 -i $1 -j REJECT
  55. $IPTABLES -I zone_$2_nat 1 -t nat -o $1 -j MASQUERADE
  56. $IPTABLES -I PREROUTING 1 -t nat -i $1 -j zone_$2_prerouting
  57. $IPTABLES -A FORWARD -i $1 -j zone_$2_forward
  58. uci_set_state firewall core "$2" "$1"
  59. }
  60. delif() {
  61. logger "removing $1 from firewall zone $2"
  62. $IPTABLES -D INPUT -i $1 -j zone_$2
  63. $IPTABLES -D zone_$2_ACCEPT -o $1 -j ACCEPT
  64. $IPTABLES -D zone_$2_DROP -o $1 -j DROP
  65. $IPTABLES -D zone_$2_REJECT -o $1 -j REJECT
  66. $IPTABLES -D zone_$2_ACCEPT -i $1 -j ACCEPT
  67. $IPTABLES -D zone_$2_DROP -i $1 -j DROP
  68. $IPTABLES -D zone_$2_REJECT -i $1 -j REJECT
  69. $IPTABLES -D zone_$2_nat -t nat -o $1 -j MASQUERADE
  70. $IPTABLES -D PREROUTING -t nat -i $1 -j zone_$2_prerouting
  71. $IPTABLES -D FORWARD -i $1 -j zone_$2_forward
  72. uci_revert_state firewall core "$2"
  73. }
  74. load_synflood() {
  75. echo "Loading synflood protection"
  76. $IPTABLES -N SYN_FLOOD
  77. $IPTABLES -A SYN_FLOOD -p tcp --syn -m limit --limit ${1}/second --limit-burst $2 -j RETURN
  78. $IPTABLES -A SYN_FLOOD -p ! tcp -j RETURN
  79. $IPTABLES -A SYN_FLOOD -p tcp ! --syn -j RETURN
  80. $IPTABLES -A SYN_FLOOD -j LOG --log-prefix "syn_flood: "
  81. $IPTABLES -A SYN_FLOOD -j DROP
  82. $IPTABLES -A INPUT -p tcp --syn -j SYN_FLOOD
  83. }
  84. fw_defaults() {
  85. load_policy $1
  86. DEF_INPUT=$input
  87. DEF_OUTPUT=$output
  88. DEF_FORWARD=$forward
  89. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  90. for f in /proc/sys/net/ipv4/conf/*/accept_redirects
  91. do
  92. echo 0 > $f
  93. done
  94. for f in /proc/sys/net/ipv4/conf/*/accept_source_route
  95. do
  96. echo 0 > $f
  97. done
  98. uci_revert_state firewall core
  99. uci_set_state firewall core "" firewall_state
  100. $IPTABLES -F
  101. $IPTABLES -t nat -F
  102. $IPTABLES -t mangle -F
  103. $IPTABLES -X -t nat
  104. $IPTABLES -X
  105. $IPTABLES -P INPUT $input
  106. $IPTABLES -A INPUT -m state --state INVALID -j DROP
  107. $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  108. $IPTABLES -P OUTPUT $output
  109. $IPTABLES -A OUTPUT -m state --state INVALID -j DROP
  110. $IPTABLES -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  111. $IPTABLES -P FORWARD $forward
  112. $IPTABLES -A FORWARD -m state --state INVALID -j DROP
  113. $IPTABLES -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  114. $IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  115. $IPTABLES -A INPUT -i lo -j ACCEPT
  116. $IPTABLES -A OUTPUT -o lo -j ACCEPT
  117. config_get syn_flood $1 syn_flood
  118. config_get syn_rate $1 syn_rate
  119. config_get syn_burst $1 syn_burst
  120. [ -z "$syn_rate" ] && syn_rate=25
  121. [ -z "$syn_burst" ] && syn_burst=50
  122. [ "$syn_flood" == "1" ] && load_synflood $syn_rate $syn_burst
  123. }
  124. fw_zone() {
  125. local name
  126. local network
  127. local masq
  128. config_get name $1 name
  129. config_get network $1 network
  130. config_get masq $1 masq
  131. load_policy $1
  132. [ -z "$network" ] && network=$name
  133. create_zone "$name" "$network" "$input" "$output" "$forward" "$masq"
  134. }
  135. fw_rule() {
  136. local src
  137. local src_ip
  138. local src_mac
  139. local src_port
  140. local src_mac
  141. local dest
  142. local dest_ip
  143. local dest_port
  144. local proto
  145. local target
  146. local ruleset
  147. config_get src $1 src
  148. config_get src_ip $1 src_ip
  149. config_get src_mac $1 src_mac
  150. config_get src_port $1 src_port
  151. config_get dest $1 dest
  152. config_get dest_ip $1 dest_ip
  153. config_get dest_port $1 dest_port
  154. config_get proto $1 proto
  155. config_get target $1 target
  156. config_get ruleset $1 ruleset
  157. [ -z "$target" ] && target=DROP
  158. [ -n "$src" ] && ZONE=zone_$src || ZONE=INPUT
  159. [ -n "$dest" ] && TARGET=zone_${dest}_$target || TARGET=$target
  160. add_rule() {
  161. $IPTABLES -I $ZONE 1 \
  162. ${proto:+-p $proto} \
  163. ${src_ip:+-s $src_ip} \
  164. ${src_port:+--sport $src_port} \
  165. ${src_mac:+-m mac --mac-source $src_mac} \
  166. ${dest_ip:+-d $dest_ip} \
  167. ${dest_port:+--dport $dest_port} \
  168. -j $TARGET
  169. }
  170. [ "$proto" == "tcpudp" -o -z "$proto" ] && {
  171. proto=tcp
  172. add_rule
  173. proto=udp
  174. add_rule
  175. return
  176. }
  177. add_rule
  178. }
  179. fw_forwarding() {
  180. local src
  181. local dest
  182. local masq
  183. config_get src $1 src
  184. config_get dest $1 dest
  185. [ -n "$src" ] && z_src=zone_${src}_forward || z_src=FORWARD
  186. [ -n "$dest" ] && z_dest=zone_${dest}_ACCEPT || z_dest=ACCEPT
  187. $IPTABLES -I $z_src 1 -j $z_dest
  188. }
  189. fw_redirect() {
  190. local src
  191. local src_ip
  192. local src_port
  193. local src_dport
  194. local src_mac
  195. local dest_ip
  196. local dest_port dest_port2
  197. local proto
  198. config_get src $1 src
  199. config_get src_ip $1 src_ip
  200. config_get src_port $1 src_port
  201. config_get src_dport $1 src_dport
  202. config_get src_mac $1 src_mac
  203. config_get dest_ip $1 dest_ip
  204. config_get dest_port $1 dest_port
  205. config_get proto $1 proto
  206. [ -z "$src" -o -z "$dest_ip" ] && { \
  207. echo "redirect needs src and dest_ip"; return ; }
  208. src_port_first=${src_port%-*}
  209. src_port_last=${src_port#*-}
  210. [ "$src_port_first" -ne "$src_port_last" ] && { \
  211. src_port="$src_port_first:$src_port_last"; }
  212. src_dport_first=${src_dport%-*}
  213. src_dport_last=${src_dport#*-}
  214. [ "$src_dport_first" -ne "$src_dport_last" ] && { \
  215. src_dport="$src_dport_first:$src_dport_last"; }
  216. dest_port2=$dest_port
  217. dest_port_first=${dest_port2%-*}
  218. dest_port_last=${dest_port2#*-}
  219. [ "$dest_port_first" -ne "$dest_port_last" ] && { \
  220. dest_port2="$dest_port_first:$dest_port_last"; }
  221. add_rule() {
  222. $IPTABLES -A zone_${src}_prerouting -t nat \
  223. ${proto:+-p $proto} \
  224. ${src_ip:+-s $src_ip} \
  225. ${src_port:+--sport $src_port} \
  226. ${src_dport:+--dport $src_dport} \
  227. ${src_mac:+-m mac --mac-source $src_mac} \
  228. -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
  229. $IPTABLES -I zone_${src}_forward 1 \
  230. ${proto:+-p $proto} \
  231. -d $dest_ip \
  232. ${src_ip:+-s $src_ip} \
  233. ${src_port:+--sport $src_port} \
  234. ${dest_port2:+--dport $dest_port2} \
  235. ${src_mac:+-m mac --mac-source $src_mac} \
  236. -j ACCEPT
  237. }
  238. [ "$proto" == "tcpudp" -o -z "$proto" ] && {
  239. proto=tcp
  240. add_rule
  241. proto=udp
  242. add_rule
  243. return
  244. }
  245. add_rule
  246. }
  247. fw_include() {
  248. local path
  249. config_get path $1 path
  250. [ -e $path ] && . $path
  251. }
  252. fw_addif() {
  253. local up
  254. local ifname
  255. config_get up $1 up
  256. config_get ifname $1 ifname
  257. [ -n "$up" ] || return 0
  258. (ACTION="ifup" INTERFACE="$1" . /etc/hotplug.d/iface/20-firewall)
  259. }
  260. fw_custom_chains() {
  261. $IPTABLES -N input_rule
  262. $IPTABLES -N output_rule
  263. $IPTABLES -N forwarding_rule
  264. $IPTABLES -N prerouting_rule -t nat
  265. $IPTABLES -N postrouting_rule -t nat
  266. $IPTABLES -N input_wan
  267. $IPTABLES -N forwarding_wan
  268. $IPTABLES -N prerouting_wan -t nat
  269. $IPTABLES -A INPUT -j input_rule
  270. $IPTABLES -A OUTPUT -j output_rule
  271. $IPTABLES -A FORWARD -j forwarding_rule
  272. $IPTABLES -A PREROUTING -t nat -j prerouting_rule
  273. $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
  274. $IPTABLES -A zone_wan -j input_wan
  275. $IPTABLES -A zone_wan_forward -j forwarding_wan
  276. $IPTABLES -A zone_wan_prerouting -t nat -j prerouting_wan
  277. }
  278. fw_init() {
  279. echo "Loading defaults"
  280. config_foreach fw_defaults defaults
  281. echo "Loading zones"
  282. config_foreach fw_zone zone
  283. echo "Loading rules"
  284. config_foreach fw_rule rule
  285. echo "Loading forwarding"
  286. config_foreach fw_forwarding forwarding
  287. echo "Loading redirects"
  288. config_foreach fw_redirect redirect
  289. echo "Adding custom chains"
  290. fw_custom_chains
  291. echo "Loading includes"
  292. config_foreach fw_include include
  293. uci_set_state firewall core loaded 1
  294. unset CONFIG_APPEND
  295. config_load network
  296. config_foreach fw_addif interface
  297. }
  298. fw_stop() {
  299. $IPTABLES -F
  300. $IPTABLES -t nat -F
  301. $IPTABLES -t mangle -F
  302. $IPTABLES -X -t nat
  303. $IPTABLES -X
  304. $IPTABLES -P INPUT ACCEPT
  305. $IPTABLES -P OUTPUT ACCEPT
  306. $IPTABLES -P FORWARD ACCEPT
  307. uci_revert_state firewall core
  308. }