uci_firewall.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. CUSTOM_CHAINS=1
  14. DEF_INPUT=DROP
  15. DEF_OUTPUT=DROP
  16. DEF_FORWARD=DROP
  17. CONNTRACK_ZONES=
  18. NOTRACK_DISABLED=
  19. find_item() {
  20. local item="$1"; shift
  21. for i in "$@"; do
  22. [ "$i" = "$item" ] && return 0
  23. done
  24. return 1
  25. }
  26. load_policy() {
  27. config_get input $1 input
  28. config_get output $1 output
  29. config_get forward $1 forward
  30. DEF_INPUT="${input:-$DEF_INPUT}"
  31. DEF_OUTPUT="${output:-$DEF_OUTPUT}"
  32. DEF_FORWARD="${forward:-$DEF_FORWARD}"
  33. }
  34. create_zone() {
  35. local exists
  36. [ "$1" == "loopback" ] && return
  37. config_get exists $ZONE_LIST $1
  38. [ -n "$exists" ] && return
  39. config_set $ZONE_LIST $1 1
  40. $IPTABLES -N zone_$1
  41. $IPTABLES -N zone_$1_MSSFIX
  42. $IPTABLES -N zone_$1_ACCEPT
  43. $IPTABLES -N zone_$1_DROP
  44. $IPTABLES -N zone_$1_REJECT
  45. $IPTABLES -N zone_$1_forward
  46. [ "$4" ] && $IPTABLES -A output -j zone_$1_$4
  47. $IPTABLES -N zone_$1_nat -t nat
  48. $IPTABLES -N zone_$1_prerouting -t nat
  49. $IPTABLES -t raw -N zone_$1_notrack
  50. [ "$6" == "1" ] && $IPTABLES -t nat -A POSTROUTING -j zone_$1_nat
  51. [ "$7" == "1" ] && $IPTABLES -I FORWARD 1 -j zone_$1_MSSFIX
  52. }
  53. addif() {
  54. local network="$1"
  55. local ifname="$2"
  56. local zone="$3"
  57. local n_if n_zone
  58. config_get n_if core "${network}_ifname"
  59. config_get n_zone core "${network}_zone"
  60. [ -n "$n_zone" ] && {
  61. if [ "$n_zone" != "$zone" ]; then
  62. delif "$network" "$n_if" "$n_zone"
  63. else
  64. return
  65. fi
  66. }
  67. logger "adding $network ($ifname) to firewall zone $zone"
  68. $IPTABLES -A input -i "$ifname" -j zone_${zone}
  69. $IPTABLES -I zone_${zone}_MSSFIX 1 -o "$ifname" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  70. $IPTABLES -I zone_${zone}_ACCEPT 1 -o "$ifname" -j ACCEPT
  71. $IPTABLES -I zone_${zone}_DROP 1 -o "$ifname" -j DROP
  72. $IPTABLES -I zone_${zone}_REJECT 1 -o "$ifname" -j reject
  73. $IPTABLES -I zone_${zone}_ACCEPT 1 -i "$ifname" -j ACCEPT
  74. $IPTABLES -I zone_${zone}_DROP 1 -i "$ifname" -j DROP
  75. $IPTABLES -I zone_${zone}_REJECT 1 -i "$ifname" -j reject
  76. $IPTABLES -I zone_${zone}_nat 1 -t nat -o "$ifname" -j MASQUERADE
  77. $IPTABLES -I PREROUTING 1 -t nat -i "$ifname" -j zone_${zone}_prerouting
  78. $IPTABLES -A forward -i "$ifname" -j zone_${zone}_forward
  79. $IPTABLES -t raw -I PREROUTING 1 -i "$ifname" -j zone_${zone}_notrack
  80. uci_set_state firewall core "${network}_ifname" "$ifname"
  81. uci_set_state firewall core "${network}_zone" "$zone"
  82. ACTION=add ZONE="$zone" INTERFACE="$network" DEVICE="$ifname" /sbin/hotplug-call firewall
  83. }
  84. delif() {
  85. local network="$1"
  86. local ifname="$2"
  87. local zone="$3"
  88. logger "removing $network ($ifname) from firewall zone $zone"
  89. $IPTABLES -D input -i "$ifname" -j zone_$zone
  90. $IPTABLES -D zone_${zone}_MSSFIX -o "$ifname" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  91. $IPTABLES -D zone_${zone}_ACCEPT -o "$ifname" -j ACCEPT
  92. $IPTABLES -D zone_${zone}_DROP -o "$ifname" -j DROP
  93. $IPTABLES -D zone_${zone}_REJECT -o "$ifname" -j reject
  94. $IPTABLES -D zone_${zone}_ACCEPT -i "$ifname" -j ACCEPT
  95. $IPTABLES -D zone_${zone}_DROP -i "$ifname" -j DROP
  96. $IPTABLES -D zone_${zone}_REJECT -i "$ifname" -j reject
  97. $IPTABLES -D zone_${zone}_nat -t nat -o "$ifname" -j MASQUERADE
  98. $IPTABLES -D PREROUTING -t nat -i "$ifname" -j zone_${zone}_prerouting
  99. $IPTABLES -D forward -i "$ifname" -j zone_${zone}_forward
  100. uci_revert_state firewall core "${network}_ifname"
  101. uci_revert_state firewall core "${network}_zone"
  102. ACTION=remove ZONE="$zone" INTERFACE="$network" DEVICE="$ifname" /sbin/hotplug-call firewall
  103. }
  104. load_synflood() {
  105. local rate=${1:-25}
  106. local burst=${2:-50}
  107. echo "Loading synflood protection"
  108. $IPTABLES -N syn_flood
  109. $IPTABLES -A syn_flood -p tcp --syn -m limit --limit $rate/second --limit-burst $burst -j RETURN
  110. $IPTABLES -A syn_flood -j DROP
  111. $IPTABLES -A INPUT -p tcp --syn -j syn_flood
  112. }
  113. fw_set_chain_policy() {
  114. local chain=$1
  115. local target=$2
  116. [ "$target" == "REJECT" ] && {
  117. $IPTABLES -A $chain -j reject
  118. target=DROP
  119. }
  120. $IPTABLES -P $chain $target
  121. }
  122. fw_clear() {
  123. $IPTABLES -F
  124. $IPTABLES -t nat -F
  125. $IPTABLES -t nat -X
  126. $IPTABLES -t raw -F
  127. $IPTABLES -t raw -X
  128. $IPTABLES -X
  129. }
  130. fw_defaults() {
  131. [ -n "$DEFAULTS_APPLIED" ] && {
  132. echo "Error: multiple defaults sections detected"
  133. return;
  134. }
  135. DEFAULTS_APPLIED=1
  136. load_policy "$1"
  137. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  138. for f in /proc/sys/net/ipv4/conf/*/accept_redirects
  139. do
  140. echo 0 > $f
  141. done
  142. for f in /proc/sys/net/ipv4/conf/*/accept_source_route
  143. do
  144. echo 0 > $f
  145. done
  146. uci_revert_state firewall core
  147. uci_set_state firewall core "" firewall_state
  148. $IPTABLES -P INPUT DROP
  149. $IPTABLES -P OUTPUT DROP
  150. $IPTABLES -P FORWARD DROP
  151. fw_clear
  152. config_get_bool drop_invalid $1 drop_invalid 0
  153. [ "$drop_invalid" -gt 0 ] && {
  154. $IPTABLES -A INPUT -m state --state INVALID -j DROP
  155. $IPTABLES -A OUTPUT -m state --state INVALID -j DROP
  156. $IPTABLES -A FORWARD -m state --state INVALID -j DROP
  157. NOTRACK_DISABLED=1
  158. }
  159. $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  160. $IPTABLES -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  161. $IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  162. $IPTABLES -A INPUT -i lo -j ACCEPT
  163. $IPTABLES -A OUTPUT -o lo -j ACCEPT
  164. config_get syn_flood $1 syn_flood
  165. config_get syn_rate $1 syn_rate
  166. config_get syn_burst $1 syn_burst
  167. [ "$syn_flood" == "1" ] && load_synflood $syn_rate $syn_burst
  168. echo "Adding custom chains"
  169. fw_custom_chains
  170. $IPTABLES -N input
  171. $IPTABLES -N output
  172. $IPTABLES -N forward
  173. $IPTABLES -A INPUT -j input
  174. $IPTABLES -A OUTPUT -j output
  175. $IPTABLES -A FORWARD -j forward
  176. $IPTABLES -N reject
  177. $IPTABLES -A reject -p tcp -j REJECT --reject-with tcp-reset
  178. $IPTABLES -A reject -j REJECT --reject-with icmp-port-unreachable
  179. fw_set_chain_policy INPUT "$DEF_INPUT"
  180. fw_set_chain_policy OUTPUT "$DEF_OUTPUT"
  181. fw_set_chain_policy FORWARD "$DEF_FORWARD"
  182. }
  183. fw_zone_defaults() {
  184. local name
  185. local network
  186. local masq
  187. config_get name $1 name
  188. config_get network $1 network
  189. config_get_bool masq $1 masq "0"
  190. config_get_bool conntrack $1 conntrack "0"
  191. config_get_bool mtu_fix $1 mtu_fix 0
  192. load_policy $1
  193. [ "$forward" ] && $IPTABLES -A zone_${name}_forward -j zone_${name}_${forward}
  194. [ "$input" ] && $IPTABLES -A zone_${name} -j zone_${name}_${input}
  195. }
  196. fw_zone() {
  197. local name
  198. local network
  199. local masq
  200. config_get name $1 name
  201. config_get network $1 network
  202. config_get_bool masq $1 masq "0"
  203. config_get_bool conntrack $1 conntrack "0"
  204. config_get_bool mtu_fix $1 mtu_fix 0
  205. load_policy $1
  206. [ "$conntrack" = "1" -o "$masq" = "1" ] && append CONNTRACK_ZONES "$name"
  207. [ -z "$network" ] && network=$name
  208. create_zone "$name" "$network" "$input" "$output" "$forward" "$masq" "$mtu_fix"
  209. fw_custom_chains_zone "$name"
  210. }
  211. fw_rule() {
  212. local src
  213. local src_ip
  214. local src_mac
  215. local src_port
  216. local src_mac
  217. local dest
  218. local dest_ip
  219. local dest_port
  220. local proto
  221. local icmp_type
  222. local target
  223. local ruleset
  224. config_get src $1 src
  225. config_get src_ip $1 src_ip
  226. config_get src_mac $1 src_mac
  227. config_get src_port $1 src_port
  228. config_get dest $1 dest
  229. config_get dest_ip $1 dest_ip
  230. config_get dest_port $1 dest_port
  231. config_get proto $1 proto
  232. config_get icmp_type $1 icmp_type
  233. config_get target $1 target
  234. config_get ruleset $1 ruleset
  235. src_port_first=${src_port%-*}
  236. src_port_last=${src_port#*-}
  237. [ "$src_port_first" -ne "$src_port_last" ] && { \
  238. src_port="$src_port_first:$src_port_last"; }
  239. dest_port_first=${dest_port%-*}
  240. dest_port_last=${dest_port#*-}
  241. [ "$dest_port_first" -ne "$dest_port_last" ] && { \
  242. dest_port="$dest_port_first:$dest_port_last"; }
  243. ZONE=input
  244. TARGET=$target
  245. [ -z "$target" ] && target=DROP
  246. [ -n "$src" -a -z "$dest" ] && ZONE=zone_$src
  247. [ -n "$src" -a -n "$dest" ] && ZONE=zone_${src}_forward
  248. [ -n "$dest" ] && TARGET=zone_${dest}_$target
  249. add_rule() {
  250. $IPTABLES -A $ZONE \
  251. ${proto:+-p $proto} \
  252. ${icmp_type:+--icmp-type $icmp_type} \
  253. ${src_ip:+-s $src_ip} \
  254. ${src_port:+--sport $src_port} \
  255. ${src_mac:+-m mac --mac-source $src_mac} \
  256. ${dest_ip:+-d $dest_ip} \
  257. ${dest_port:+--dport $dest_port} \
  258. -j $TARGET
  259. }
  260. [ "$proto" == "tcpudp" -o -z "$proto" ] && {
  261. proto=tcp
  262. add_rule
  263. proto=udp
  264. add_rule
  265. return
  266. }
  267. add_rule
  268. }
  269. fw_forwarding() {
  270. local src
  271. local dest
  272. local masq
  273. config_get src $1 src
  274. config_get dest $1 dest
  275. [ -n "$src" ] && z_src=zone_${src}_forward || z_src=forward
  276. [ -n "$dest" ] && z_dest=zone_${dest}_ACCEPT || z_dest=ACCEPT
  277. $IPTABLES -I $z_src 1 -j $z_dest
  278. # propagate masq zone flag
  279. find_item "$src" $CONNTRACK_ZONES && append CONNTRACK_ZONES $dest
  280. find_item "$dest" $CONNTRACK_ZONES && append CONNTRACK_ZONES $src
  281. }
  282. fw_redirect() {
  283. local src
  284. local src_ip
  285. local src_port
  286. local src_dport
  287. local src_mac
  288. local dest_ip
  289. local dest_port dest_port2
  290. local proto
  291. config_get src $1 src
  292. config_get src_ip $1 src_ip
  293. config_get src_port $1 src_port
  294. config_get src_dport $1 src_dport
  295. config_get src_mac $1 src_mac
  296. config_get dest_ip $1 dest_ip
  297. config_get dest_port $1 dest_port
  298. config_get proto $1 proto
  299. [ -z "$src" -o -z "$dest_ip" ] && { \
  300. echo "redirect needs src and dest_ip"; return ; }
  301. src_port_first=${src_port%-*}
  302. src_port_last=${src_port#*-}
  303. [ "$src_port_first" -ne "$src_port_last" ] && { \
  304. src_port="$src_port_first:$src_port_last"; }
  305. src_dport_first=${src_dport%-*}
  306. src_dport_last=${src_dport#*-}
  307. [ "$src_dport_first" -ne "$src_dport_last" ] && { \
  308. src_dport="$src_dport_first:$src_dport_last"; }
  309. dest_port2=${dest_port:-$src_dport}
  310. dest_port_first=${dest_port2%-*}
  311. dest_port_last=${dest_port2#*-}
  312. [ "$dest_port_first" -ne "$dest_port_last" ] && { \
  313. dest_port2="$dest_port_first:$dest_port_last"; }
  314. add_rule() {
  315. $IPTABLES -A zone_${src}_prerouting -t nat \
  316. ${proto:+-p $proto} \
  317. ${src_ip:+-s $src_ip} \
  318. ${src_port:+--sport $src_port} \
  319. ${src_dport:+--dport $src_dport} \
  320. ${src_mac:+-m mac --mac-source $src_mac} \
  321. -j DNAT --to-destination $dest_ip${dest_port:+:$dest_port}
  322. $IPTABLES -I zone_${src}_forward 1 \
  323. ${proto:+-p $proto} \
  324. -d $dest_ip \
  325. ${src_ip:+-s $src_ip} \
  326. ${src_port:+--sport $src_port} \
  327. ${dest_port2:+--dport $dest_port2} \
  328. ${src_mac:+-m mac --mac-source $src_mac} \
  329. -j ACCEPT
  330. }
  331. [ "$proto" == "tcpudp" -o -z "$proto" ] && {
  332. proto=tcp
  333. add_rule
  334. proto=udp
  335. add_rule
  336. return
  337. }
  338. add_rule
  339. }
  340. fw_include() {
  341. local path
  342. config_get path $1 path
  343. [ -e $path ] && . $path
  344. }
  345. get_interface_zones() {
  346. local interface="$2"
  347. local name
  348. local network
  349. config_get name $1 name
  350. config_get network $1 network
  351. [ -z "$network" ] && network=$name
  352. for n in $network; do
  353. [ "$n" = "$interface" ] && append add_zone "$name"
  354. done
  355. }
  356. fw_event() {
  357. local action="$1"
  358. local interface="$2"
  359. local ifname="$(sh -c ". /etc/functions.sh; include /lib/network; scan_interfaces; config_get "$interface" ifname")"
  360. add_zone=
  361. local up
  362. [ -z "$ifname" ] && return 0
  363. config_foreach get_interface_zones zone "$interface"
  364. [ -z "$add_zone" ] && return 0
  365. case "$action" in
  366. ifup)
  367. for z in $add_zone; do
  368. local loaded
  369. config_get loaded core loaded
  370. [ -n "$loaded" ] && addif "$interface" "$ifname" "$z"
  371. done
  372. ;;
  373. ifdown)
  374. config_get up "$interface" up
  375. for z in $ZONE; do
  376. [ "$up" == "1" ] && delif "$interface" "$ifname" "$z"
  377. done
  378. ;;
  379. esac
  380. }
  381. fw_addif() {
  382. local up
  383. local ifname
  384. config_get up $1 up
  385. [ -n "$up" ] || return 0
  386. fw_event ifup "$1"
  387. }
  388. fw_custom_chains() {
  389. [ -n "$CUSTOM_CHAINS" ] || return 0
  390. $IPTABLES -N input_rule
  391. $IPTABLES -N output_rule
  392. $IPTABLES -N forwarding_rule
  393. $IPTABLES -N prerouting_rule -t nat
  394. $IPTABLES -N postrouting_rule -t nat
  395. $IPTABLES -A INPUT -j input_rule
  396. $IPTABLES -A OUTPUT -j output_rule
  397. $IPTABLES -A FORWARD -j forwarding_rule
  398. $IPTABLES -A PREROUTING -t nat -j prerouting_rule
  399. $IPTABLES -A POSTROUTING -t nat -j postrouting_rule
  400. }
  401. fw_custom_chains_zone() {
  402. local zone="$1"
  403. [ -n "$CUSTOM_CHAINS" ] || return 0
  404. $IPTABLES -N input_${zone}
  405. $IPTABLES -N forwarding_${zone}
  406. $IPTABLES -N prerouting_${zone} -t nat
  407. $IPTABLES -I zone_${zone} 1 -j input_${zone}
  408. $IPTABLES -I zone_${zone}_forward 1 -j forwarding_${zone}
  409. $IPTABLES -I zone_${zone}_prerouting 1 -t nat -j prerouting_${zone}
  410. }
  411. fw_check_notrack() {
  412. local zone="$1"
  413. config_get name "$zone" name
  414. [ -n "$NOTRACK_DISABLED" ] || \
  415. find_item "$name" $CONNTRACK_ZONES || \
  416. $IPTABLES -t raw -A zone_${name}_notrack -j NOTRACK
  417. }
  418. fw_init() {
  419. DEFAULTS_APPLIED=
  420. echo "Loading defaults"
  421. config_foreach fw_defaults defaults
  422. echo "Loading zones"
  423. config_foreach fw_zone zone
  424. echo "Loading forwarding"
  425. config_foreach fw_forwarding forwarding
  426. echo "Loading redirects"
  427. config_foreach fw_redirect redirect
  428. echo "Loading rules"
  429. config_foreach fw_rule rule
  430. echo "Loading includes"
  431. config_foreach fw_include include
  432. echo "Loading zone defaults"
  433. config_foreach fw_zone_defaults zone
  434. uci_set_state firewall core loaded 1
  435. config_set core loaded 1
  436. config_foreach fw_check_notrack zone
  437. INTERFACES="$(sh -c '. /etc/functions.sh; config_load network; config_foreach echo interface')"
  438. for interface in $INTERFACES; do
  439. fw_addif "$interface"
  440. done
  441. }
  442. fw_stop() {
  443. fw_clear
  444. $IPTABLES -P INPUT ACCEPT
  445. $IPTABLES -P OUTPUT ACCEPT
  446. $IPTABLES -P FORWARD ACCEPT
  447. uci_revert_state firewall
  448. }