| 1 | #!/bin/sh |
| 2 | # Copyright (C) 2008 John Crispin <blogic@openwrt.org> |
| 3 | |
| 4 | . /etc/functions.sh |
| 5 | |
| 6 | IPTABLES="echo iptables" |
| 7 | IPTABLES=iptables |
| 8 | |
| 9 | config_clear |
| 10 | include /lib/network |
| 11 | scan_interfaces |
| 12 | |
| 13 | CONFIG_APPEND=1 |
| 14 | config_load firewall |
| 15 | |
| 16 | config fw_zones |
| 17 | ZONE_LIST=$CONFIG_SECTION |
| 18 | ZONE_NAMES= |
| 19 | |
| 20 | CUSTOM_CHAINS=1 |
| 21 | DEF_INPUT=DROP |
| 22 | DEF_OUTPUT=DROP |
| 23 | DEF_FORWARD=DROP |
| 24 | CONNTRACK_ZONES= |
| 25 | NOTRACK_DISABLED= |
| 26 | |
| 27 | add_state() { |
| 28 | local var="$1" |
| 29 | local item="$2" |
| 30 | |
| 31 | local val="$(uci_get_state firewall core $var)" |
| 32 | uci_set_state firewall core $var "${val:+$val }$item" |
| 33 | } |
| 34 | |
| 35 | del_state() { |
| 36 | local var="$1" |
| 37 | local item="$2" |
| 38 | |
| 39 | local val=" $(uci_get_state firewall core $var) " |
| 40 | val="${val// $item / }" |
| 41 | val="${val# }" |
| 42 | val="${val% }" |
| 43 | uci_set_state firewall core $var "$val" |
| 44 | } |
| 45 | |
| 46 | find_item() { |
| 47 | local item="$1"; shift |
| 48 | for i in "$@"; do |
| 49 | [ "$i" = "$item" ] && return 0 |
| 50 | done |
| 51 | return 1 |
| 52 | } |
| 53 | |
| 54 | get_portrange() { |
| 55 | local _var="$1" |
| 56 | local _range="$2" |
| 57 | local _delim="${3:-:}" |
| 58 | |
| 59 | local _min="${_range%%[:-]*}" |
| 60 | local _max="${_range##*[:-]}" |
| 61 | |
| 62 | [ -n "$_min" ] && [ -n "$_max" ] && [ "$_min" != "$_max" ] && \ |
| 63 | export -n -- "$_var=$_min$_delim$_max" || \ |
| 64 | export -n -- "$_var=${_min:-$_max}" |
| 65 | } |
| 66 | |
| 67 | get_negation() { |
| 68 | local _var="$1" |
| 69 | local _flag="$2" |
| 70 | local _ipaddr="$3" |
| 71 | |
| 72 | [ "${_ipaddr#!}" != "$_ipaddr" ] && \ |
| 73 | export -n -- "$_var=! $_flag ${_ipaddr#!}" || \ |
| 74 | export -n -- "$_var=${_ipaddr:+$_flag $_ipaddr}" |
| 75 | } |
| 76 | |
| 77 | load_policy() { |
| 78 | config_get input $1 input |
| 79 | config_get output $1 output |
| 80 | config_get forward $1 forward |
| 81 | |
| 82 | DEF_INPUT="${input:-$DEF_INPUT}" |
| 83 | DEF_OUTPUT="${output:-$DEF_OUTPUT}" |
| 84 | DEF_FORWARD="${forward:-$DEF_FORWARD}" |
| 85 | } |
| 86 | |
| 87 | create_zone() { |
| 88 | local name="$1" |
| 89 | local network="$2" |
| 90 | local input="$3" |
| 91 | local output="$4" |
| 92 | local forward="$5" |
| 93 | local mtu_fix="$6" |
| 94 | local masq="$7" |
| 95 | local masq_src="$8" |
| 96 | local masq_dest="$9" |
| 97 | |
| 98 | local exists |
| 99 | |
| 100 | [ "$name" == "loopback" ] && return |
| 101 | |
| 102 | config_get exists $ZONE_LIST $name |
| 103 | [ -n "$exists" ] && return |
| 104 | config_set $ZONE_LIST $name 1 |
| 105 | |
| 106 | $IPTABLES -N zone_${name} |
| 107 | $IPTABLES -N zone_${name}_MSSFIX |
| 108 | $IPTABLES -N zone_${name}_ACCEPT |
| 109 | $IPTABLES -N zone_${name}_DROP |
| 110 | $IPTABLES -N zone_${name}_REJECT |
| 111 | $IPTABLES -N zone_${name}_forward |
| 112 | [ "$output" ] && $IPTABLES -A output -j zone_${name}_${output} |
| 113 | $IPTABLES -N zone_${name}_nat -t nat |
| 114 | $IPTABLES -N zone_${name}_prerouting -t nat |
| 115 | $IPTABLES -t raw -N zone_${name}_notrack |
| 116 | [ "$mtu_fix" == "1" ] && $IPTABLES -I FORWARD 1 -j zone_${name}_MSSFIX |
| 117 | |
| 118 | if [ "$masq" == "1" ]; then |
| 119 | local msrc mdst |
| 120 | for msrc in ${masq_src:-0.0.0.0/0}; do |
| 121 | get_negation msrc '-s' "$msrc" |
| 122 | for mdst in ${masq_dest:-0.0.0.0/0}; do |
| 123 | get_negation mdst '-d' "$mdst" |
| 124 | $IPTABLES -A zone_${name}_nat -t nat $msrc $mdst -j MASQUERADE |
| 125 | done |
| 126 | done |
| 127 | fi |
| 128 | |
| 129 | append ZONE_NAMES "$name" |
| 130 | } |
| 131 | |
| 132 | |
| 133 | addif() { |
| 134 | local network="$1" |
| 135 | local ifname="$2" |
| 136 | local zone="$3" |
| 137 | |
| 138 | local n_if n_zone |
| 139 | config_get n_if core "${network}_ifname" |
| 140 | config_get n_zone core "${network}_zone" |
| 141 | [ -n "$n_zone" ] && { |
| 142 | if [ "$n_zone" != "$zone" ]; then |
| 143 | delif "$network" "$n_if" "$n_zone" |
| 144 | else |
| 145 | return |
| 146 | fi |
| 147 | } |
| 148 | |
| 149 | logger "adding $network ($ifname) to firewall zone $zone" |
| 150 | $IPTABLES -A input -i "$ifname" -j zone_${zone} |
| 151 | $IPTABLES -I zone_${zone}_MSSFIX 1 -o "$ifname" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
| 152 | $IPTABLES -I zone_${zone}_ACCEPT 1 -o "$ifname" -j ACCEPT |
| 153 | $IPTABLES -I zone_${zone}_DROP 1 -o "$ifname" -j DROP |
| 154 | $IPTABLES -I zone_${zone}_REJECT 1 -o "$ifname" -j reject |
| 155 | $IPTABLES -I zone_${zone}_ACCEPT 1 -i "$ifname" -j ACCEPT |
| 156 | $IPTABLES -I zone_${zone}_DROP 1 -i "$ifname" -j DROP |
| 157 | $IPTABLES -I zone_${zone}_REJECT 1 -i "$ifname" -j reject |
| 158 | |
| 159 | $IPTABLES -I PREROUTING 1 -t nat -i "$ifname" -j zone_${zone}_prerouting |
| 160 | $IPTABLES -I POSTROUTING 1 -t nat -o "$ifname" -j zone_${zone}_nat |
| 161 | $IPTABLES -A forward -i "$ifname" -j zone_${zone}_forward |
| 162 | $IPTABLES -I PREROUTING 1 -t raw -i "$ifname" -j zone_${zone}_notrack |
| 163 | |
| 164 | uci_set_state firewall core "${network}_ifname" "$ifname" |
| 165 | uci_set_state firewall core "${network}_zone" "$zone" |
| 166 | |
| 167 | add_state "${zone}_networks" "$network" |
| 168 | |
| 169 | ACTION=add ZONE="$zone" INTERFACE="$network" DEVICE="$ifname" /sbin/hotplug-call firewall |
| 170 | } |
| 171 | |
| 172 | delif() { |
| 173 | local network="$1" |
| 174 | local ifname="$2" |
| 175 | local zone="$3" |
| 176 | |
| 177 | logger "removing $network ($ifname) from firewall zone $zone" |
| 178 | $IPTABLES -D input -i "$ifname" -j zone_$zone |
| 179 | $IPTABLES -D zone_${zone}_MSSFIX -o "$ifname" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
| 180 | $IPTABLES -D zone_${zone}_ACCEPT -o "$ifname" -j ACCEPT |
| 181 | $IPTABLES -D zone_${zone}_DROP -o "$ifname" -j DROP |
| 182 | $IPTABLES -D zone_${zone}_REJECT -o "$ifname" -j reject |
| 183 | $IPTABLES -D zone_${zone}_ACCEPT -i "$ifname" -j ACCEPT |
| 184 | $IPTABLES -D zone_${zone}_DROP -i "$ifname" -j DROP |
| 185 | $IPTABLES -D zone_${zone}_REJECT -i "$ifname" -j reject |
| 186 | |
| 187 | $IPTABLES -D PREROUTING -t nat -i "$ifname" -j zone_${zone}_prerouting |
| 188 | $IPTABLES -D POSTROUTING -t nat -o "$ifname" -j zone_${zone}_nat |
| 189 | $IPTABLES -D forward -i "$ifname" -j zone_${zone}_forward |
| 190 | $IPTABLES -D PREROUTING -t raw -i "$ifname" -j zone_${zone}_notrack |
| 191 | |
| 192 | uci_revert_state firewall core "${network}_ifname" |
| 193 | uci_revert_state firewall core "${network}_zone" |
| 194 | |
| 195 | del_state "${zone}_networks" "$network" |
| 196 | |
| 197 | ACTION=remove ZONE="$zone" INTERFACE="$network" DEVICE="$ifname" /sbin/hotplug-call firewall |
| 198 | } |
| 199 | |
| 200 | load_synflood() { |
| 201 | local rate=${1:-25} |
| 202 | local burst=${2:-50} |
| 203 | echo "Loading synflood protection" |
| 204 | $IPTABLES -N syn_flood |
| 205 | $IPTABLES -A syn_flood -p tcp --syn -m limit --limit $rate/second --limit-burst $burst -j RETURN |
| 206 | $IPTABLES -A syn_flood -j DROP |
| 207 | $IPTABLES -A INPUT -p tcp --syn -j syn_flood |
| 208 | } |
| 209 | |
| 210 | fw_set_chain_policy() { |
| 211 | local chain=$1 |
| 212 | local target=$2 |
| 213 | [ "$target" == "REJECT" ] && { |
| 214 | $IPTABLES -A $chain -j reject |
| 215 | target=DROP |
| 216 | } |
| 217 | $IPTABLES -P $chain $target |
| 218 | } |
| 219 | |
| 220 | fw_clear() { |
| 221 | $IPTABLES -F |
| 222 | $IPTABLES -t nat -F |
| 223 | $IPTABLES -t nat -X |
| 224 | $IPTABLES -t raw -F |
| 225 | $IPTABLES -t raw -X |
| 226 | $IPTABLES -X |
| 227 | } |
| 228 | |
| 229 | fw_defaults() { |
| 230 | [ -n "$DEFAULTS_APPLIED" ] && { |
| 231 | echo "Error: multiple defaults sections detected" |
| 232 | return; |
| 233 | } |
| 234 | DEFAULTS_APPLIED=1 |
| 235 | |
| 236 | load_policy "$1" |
| 237 | |
| 238 | echo 1 > /proc/sys/net/ipv4/tcp_syncookies |
| 239 | for f in /proc/sys/net/ipv4/conf/*/accept_redirects |
| 240 | do |
| 241 | echo 0 > $f |
| 242 | done |
| 243 | for f in /proc/sys/net/ipv4/conf/*/accept_source_route |
| 244 | do |
| 245 | echo 0 > $f |
| 246 | done |
| 247 | |
| 248 | uci_revert_state firewall core |
| 249 | uci_set_state firewall core "" firewall_state |
| 250 | |
| 251 | $IPTABLES -P INPUT DROP |
| 252 | $IPTABLES -P OUTPUT DROP |
| 253 | $IPTABLES -P FORWARD DROP |
| 254 | |
| 255 | fw_clear |
| 256 | config_get_bool drop_invalid $1 drop_invalid 0 |
| 257 | |
| 258 | [ "$drop_invalid" -gt 0 ] && { |
| 259 | $IPTABLES -A INPUT -m state --state INVALID -j DROP |
| 260 | $IPTABLES -A OUTPUT -m state --state INVALID -j DROP |
| 261 | $IPTABLES -A FORWARD -m state --state INVALID -j DROP |
| 262 | NOTRACK_DISABLED=1 |
| 263 | } |
| 264 | |
| 265 | $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT |
| 266 | $IPTABLES -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT |
| 267 | $IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT |
| 268 | |
| 269 | $IPTABLES -A INPUT -i lo -j ACCEPT |
| 270 | $IPTABLES -A OUTPUT -o lo -j ACCEPT |
| 271 | |
| 272 | config_get syn_flood $1 syn_flood |
| 273 | config_get syn_rate $1 syn_rate |
| 274 | config_get syn_burst $1 syn_burst |
| 275 | [ "$syn_flood" == "1" ] && load_synflood $syn_rate $syn_burst |
| 276 | |
| 277 | echo "Adding custom chains" |
| 278 | fw_custom_chains |
| 279 | |
| 280 | $IPTABLES -N input |
| 281 | $IPTABLES -N output |
| 282 | $IPTABLES -N forward |
| 283 | |
| 284 | $IPTABLES -A INPUT -j input |
| 285 | $IPTABLES -A OUTPUT -j output |
| 286 | $IPTABLES -A FORWARD -j forward |
| 287 | |
| 288 | $IPTABLES -N reject |
| 289 | $IPTABLES -A reject -p tcp -j REJECT --reject-with tcp-reset |
| 290 | $IPTABLES -A reject -j REJECT --reject-with icmp-port-unreachable |
| 291 | |
| 292 | fw_set_chain_policy INPUT "$DEF_INPUT" |
| 293 | fw_set_chain_policy OUTPUT "$DEF_OUTPUT" |
| 294 | fw_set_chain_policy FORWARD "$DEF_FORWARD" |
| 295 | } |
| 296 | |
| 297 | fw_zone_defaults() { |
| 298 | local name |
| 299 | local network |
| 300 | local masq |
| 301 | |
| 302 | config_get name $1 name |
| 303 | config_get network $1 network |
| 304 | config_get_bool masq $1 masq "0" |
| 305 | config_get_bool conntrack $1 conntrack "0" |
| 306 | config_get_bool mtu_fix $1 mtu_fix 0 |
| 307 | |
| 308 | load_policy $1 |
| 309 | [ "$forward" ] && $IPTABLES -A zone_${name}_forward -j zone_${name}_${forward} |
| 310 | [ "$input" ] && $IPTABLES -A zone_${name} -j zone_${name}_${input} |
| 311 | } |
| 312 | |
| 313 | fw_zone() { |
| 314 | local name |
| 315 | local network |
| 316 | local mtu_fix |
| 317 | local conntrack |
| 318 | local masq |
| 319 | local masq_src |
| 320 | local masq_dest |
| 321 | |
| 322 | config_get name $1 name |
| 323 | config_get network $1 network |
| 324 | config_get_bool masq $1 masq "0" |
| 325 | config_get_bool conntrack $1 conntrack "0" |
| 326 | config_get_bool mtu_fix $1 mtu_fix 0 |
| 327 | config_get masq_src $1 masq_src |
| 328 | config_get masq_dest $1 masq_dest |
| 329 | |
| 330 | load_policy $1 |
| 331 | [ "$conntrack" = "1" -o "$masq" = "1" ] && append CONNTRACK_ZONES "$name" |
| 332 | [ -z "$network" ] && network=$name |
| 333 | |
| 334 | create_zone "$name" "$network" "$input" "$output" "$forward" "$mtu_fix" \ |
| 335 | "$masq" "$masq_src" "$masq_dest" |
| 336 | |
| 337 | fw_custom_chains_zone "$name" |
| 338 | } |
| 339 | |
| 340 | fw_rule() { |
| 341 | local src |
| 342 | local src_ip |
| 343 | local src_mac |
| 344 | local src_port |
| 345 | local src_mac |
| 346 | local dest |
| 347 | local dest_ip |
| 348 | local dest_port |
| 349 | local proto |
| 350 | local icmp_type |
| 351 | local target |
| 352 | local ruleset |
| 353 | |
| 354 | config_get src $1 src |
| 355 | config_get src_ip $1 src_ip |
| 356 | config_get src_mac $1 src_mac |
| 357 | config_get src_port $1 src_port |
| 358 | config_get dest $1 dest |
| 359 | config_get dest_ip $1 dest_ip |
| 360 | config_get dest_port $1 dest_port |
| 361 | config_get proto $1 proto |
| 362 | config_get icmp_type $1 icmp_type |
| 363 | config_get target $1 target |
| 364 | config_get ruleset $1 ruleset |
| 365 | |
| 366 | [ "$target" != "NOTRACK" ] || [ -n "$src" ] || { |
| 367 | echo "NOTRACK rule needs src" |
| 368 | return |
| 369 | } |
| 370 | |
| 371 | local srcaddr destaddr |
| 372 | get_negation srcaddr '-s' "$src_ip" |
| 373 | get_negation destaddr '-d' "$dest_ip" |
| 374 | |
| 375 | local srcports destports |
| 376 | get_portrange srcports "$src_port" ":" |
| 377 | get_portrange destports "$dest_port" ":" |
| 378 | |
| 379 | ZONE=input |
| 380 | TABLE=filter |
| 381 | TARGET="${target:-DROP}" |
| 382 | |
| 383 | if [ "$TARGET" = "NOTRACK" ]; then |
| 384 | TABLE=raw |
| 385 | ZONE="zone_${src}_notrack" |
| 386 | else |
| 387 | [ -n "$src" ] && ZONE="zone_${src}${dest:+_forward}" |
| 388 | [ -n "$dest" ] && TARGET="zone_${dest}_${TARGET}" |
| 389 | fi |
| 390 | |
| 391 | local pos |
| 392 | eval 'pos=$((++FW__RULE_COUNT_'$ZONE'))' |
| 393 | |
| 394 | add_rule() { |
| 395 | $IPTABLES -t $TABLE -I $ZONE $pos \ |
| 396 | $srcaddr $destaddr \ |
| 397 | ${proto:+-p $proto} \ |
| 398 | ${icmp_type:+--icmp-type $icmp_type} \ |
| 399 | ${srcports:+--sport $srcports} \ |
| 400 | ${src_mac:+-m mac --mac-source $src_mac} \ |
| 401 | ${destports:+--dport $destports} \ |
| 402 | -j $TARGET |
| 403 | } |
| 404 | |
| 405 | [ "$proto" == "tcpudp" ] && proto="tcp udp" |
| 406 | for proto in ${proto:-tcp udp}; do |
| 407 | add_rule |
| 408 | done |
| 409 | } |
| 410 | |
| 411 | fw_forwarding() { |
| 412 | local src |
| 413 | local dest |
| 414 | local masq |
| 415 | |
| 416 | config_get src $1 src |
| 417 | config_get dest $1 dest |
| 418 | [ -n "$src" ] && z_src=zone_${src}_forward || z_src=forward |
| 419 | [ -n "$dest" ] && z_dest=zone_${dest}_ACCEPT || z_dest=ACCEPT |
| 420 | $IPTABLES -I $z_src 1 -j $z_dest |
| 421 | |
| 422 | # propagate masq zone flag |
| 423 | find_item "$src" $CONNTRACK_ZONES && append CONNTRACK_ZONES $dest |
| 424 | find_item "$dest" $CONNTRACK_ZONES && append CONNTRACK_ZONES $src |
| 425 | } |
| 426 | |
| 427 | fw_redirect() { |
| 428 | local src |
| 429 | local src_ip |
| 430 | local src_dip |
| 431 | local src_port |
| 432 | local src_dport |
| 433 | local src_mac |
| 434 | local dest |
| 435 | local dest_ip |
| 436 | local dest_port |
| 437 | local proto |
| 438 | local target |
| 439 | |
| 440 | config_get src $1 src |
| 441 | config_get src_ip $1 src_ip |
| 442 | config_get src_dip $1 src_dip |
| 443 | config_get src_port $1 src_port |
| 444 | config_get src_dport $1 src_dport |
| 445 | config_get src_mac $1 src_mac |
| 446 | config_get dest $1 dest |
| 447 | config_get dest_ip $1 dest_ip |
| 448 | config_get dest_port $1 dest_port |
| 449 | config_get proto $1 proto |
| 450 | config_get target $1 target |
| 451 | |
| 452 | local fwdchain natchain natopt nataddr natports srcdaddr srcdports |
| 453 | if [ "${target:-DNAT}" == "DNAT" ]; then |
| 454 | [ -n "$src" -a -n "$dest_ip$dest_port" ] || { |
| 455 | echo "DNAT redirect needs src and dest_ip or dest_port" |
| 456 | return |
| 457 | } |
| 458 | |
| 459 | fwdchain="zone_${src}_forward" |
| 460 | |
| 461 | natopt="--to-destination" |
| 462 | natchain="zone_${src}_prerouting" |
| 463 | nataddr="$dest_ip" |
| 464 | get_portrange natports "$dest_port" "-" |
| 465 | |
| 466 | get_negation srcdaddr '-d' "$src_dip" |
| 467 | get_portrange srcdports "$src_dport" ":" |
| 468 | |
| 469 | find_item "$src" $CONNTRACK_ZONES || \ |
| 470 | append CONNTRACK_ZONES "$src" |
| 471 | |
| 472 | elif [ "$target" == "SNAT" ]; then |
| 473 | [ -n "$dest" -a -n "$src_dip" ] || { |
| 474 | echo "SNAT redirect needs dest and src_dip" |
| 475 | return |
| 476 | } |
| 477 | |
| 478 | fwdchain="${src:+zone_${src}_forward}" |
| 479 | |
| 480 | natopt="--to-source" |
| 481 | natchain="zone_${dest}_nat" |
| 482 | nataddr="$src_dip" |
| 483 | get_portrange natports "$src_dport" "-" |
| 484 | |
| 485 | get_negation srcdaddr '-d' "$dest_ip" |
| 486 | get_portrange srcdports "$dest_port" ":" |
| 487 | |
| 488 | find_item "$dest" $CONNTRACK_ZONES || \ |
| 489 | append CONNTRACK_ZONES "$dest" |
| 490 | |
| 491 | else |
| 492 | echo "redirect target must be either DNAT or SNAT" |
| 493 | return |
| 494 | fi |
| 495 | |
| 496 | local srcaddr destaddr |
| 497 | get_negation srcaddr '-s' "$src_ip" |
| 498 | get_negation destaddr '-d' "$dest_ip" |
| 499 | |
| 500 | local srcports destports |
| 501 | get_portrange srcports "$src_port" ":" |
| 502 | get_portrange destports "${dest_port-$src_dport}" ":" |
| 503 | |
| 504 | add_rule() { |
| 505 | local pos |
| 506 | eval 'pos=$((++FW__REDIR_COUNT_'$natchain'))' |
| 507 | |
| 508 | $IPTABLES -I $natchain $pos -t nat \ |
| 509 | $srcaddr $srcdaddr \ |
| 510 | ${proto:+-p $proto} \ |
| 511 | ${srcports:+--sport $srcports} \ |
| 512 | ${srcdports:+--dport $srcdports} \ |
| 513 | ${src_mac:+-m mac --mac-source $src_mac} \ |
| 514 | -j ${target:-DNAT} $natopt $nataddr${natports:+:$natports} |
| 515 | |
| 516 | [ -n "$dest_ip" ] && \ |
| 517 | $IPTABLES -I ${fwdchain:-forward} 1 \ |
| 518 | $srcaddr $destaddr \ |
| 519 | ${proto:+-p $proto} \ |
| 520 | ${srcports:+--sport $srcports} \ |
| 521 | ${destports:+--dport $destports} \ |
| 522 | ${src_mac:+-m mac --mac-source $src_mac} \ |
| 523 | -j ACCEPT |
| 524 | } |
| 525 | |
| 526 | [ "$proto" == "tcpudp" ] && proto="tcp udp" |
| 527 | for proto in ${proto:-tcp udp}; do |
| 528 | add_rule |
| 529 | done |
| 530 | } |
| 531 | |
| 532 | fw_include() { |
| 533 | local path |
| 534 | config_get path $1 path |
| 535 | [ -e $path ] && . $path |
| 536 | } |
| 537 | |
| 538 | get_interface_zones() { |
| 539 | local interface="$2" |
| 540 | local name |
| 541 | local network |
| 542 | local masq_src |
| 543 | local masq_dest |
| 544 | config_get name $1 name |
| 545 | config_get network $1 network |
| 546 | config_get masq_src $1 masq_src |
| 547 | config_get masq_dest $1 masq_dest |
| 548 | [ -z "$network" ] && network=$name |
| 549 | for n in $network; do |
| 550 | [ "$n" = "$interface" ] && { |
| 551 | append add_zone "$name" |
| 552 | append add_masq_src "$masq_src" |
| 553 | append add_masq_dest "$masq_dest" |
| 554 | } |
| 555 | done |
| 556 | } |
| 557 | |
| 558 | fw_event() { |
| 559 | local action="$1" |
| 560 | local interface="$2" |
| 561 | local ifname="$(sh -c ". /etc/functions.sh; include /lib/network; scan_interfaces; config_get "$interface" ifname")" |
| 562 | local add_zone= |
| 563 | local add_masq_src= |
| 564 | local add_masq_dest= |
| 565 | local up |
| 566 | |
| 567 | [ -z "$ifname" ] && return 0 |
| 568 | config_foreach get_interface_zones zone "$interface" |
| 569 | [ -z "$add_zone" ] && return 0 |
| 570 | |
| 571 | case "$action" in |
| 572 | ifup) |
| 573 | for z in $add_zone; do |
| 574 | local loaded masq_src masq_dest |
| 575 | config_get loaded core loaded |
| 576 | [ -n "$loaded" ] && addif "$interface" "$ifname" "$z" "$add_masq_src" "$add_masq_dest" |
| 577 | done |
| 578 | ;; |
| 579 | ifdown) |
| 580 | config_get up "$interface" up |
| 581 | |
| 582 | for z in $ZONE; do |
| 583 | local masq_src masq_dest |
| 584 | config_get masq_src core "${z}_masq_src" |
| 585 | config_get masq_dest core "${z}_masq_dest" |
| 586 | [ "$up" == "1" ] && delif "$interface" "$ifname" "$z" "$masq_src" "$masq_dest" |
| 587 | done |
| 588 | ;; |
| 589 | esac |
| 590 | } |
| 591 | |
| 592 | fw_addif() { |
| 593 | local up |
| 594 | local ifname |
| 595 | config_get up $1 up |
| 596 | [ -n "$up" ] || return 0 |
| 597 | fw_event ifup "$1" |
| 598 | } |
| 599 | |
| 600 | fw_custom_chains() { |
| 601 | [ -n "$CUSTOM_CHAINS" ] || return 0 |
| 602 | $IPTABLES -N input_rule |
| 603 | $IPTABLES -N output_rule |
| 604 | $IPTABLES -N forwarding_rule |
| 605 | $IPTABLES -N prerouting_rule -t nat |
| 606 | $IPTABLES -N postrouting_rule -t nat |
| 607 | |
| 608 | $IPTABLES -A INPUT -j input_rule |
| 609 | $IPTABLES -A OUTPUT -j output_rule |
| 610 | $IPTABLES -A FORWARD -j forwarding_rule |
| 611 | $IPTABLES -A PREROUTING -t nat -j prerouting_rule |
| 612 | $IPTABLES -A POSTROUTING -t nat -j postrouting_rule |
| 613 | } |
| 614 | |
| 615 | fw_custom_chains_zone() { |
| 616 | local zone="$1" |
| 617 | |
| 618 | [ -n "$CUSTOM_CHAINS" ] || return 0 |
| 619 | $IPTABLES -N input_${zone} |
| 620 | $IPTABLES -N forwarding_${zone} |
| 621 | $IPTABLES -N prerouting_${zone} -t nat |
| 622 | $IPTABLES -I zone_${zone} 1 -j input_${zone} |
| 623 | $IPTABLES -I zone_${zone}_forward 1 -j forwarding_${zone} |
| 624 | $IPTABLES -I zone_${zone}_prerouting 1 -t nat -j prerouting_${zone} |
| 625 | } |
| 626 | |
| 627 | fw_check_notrack() { |
| 628 | local zone="$1" |
| 629 | config_get name "$zone" name |
| 630 | [ -n "$NOTRACK_DISABLED" ] || \ |
| 631 | find_item "$name" $CONNTRACK_ZONES || \ |
| 632 | $IPTABLES -t raw -A zone_${name}_notrack -j NOTRACK |
| 633 | } |
| 634 | |
| 635 | fw_init() { |
| 636 | DEFAULTS_APPLIED= |
| 637 | |
| 638 | echo "Loading defaults" |
| 639 | config_foreach fw_defaults defaults |
| 640 | echo "Loading zones" |
| 641 | config_foreach fw_zone zone |
| 642 | echo "Loading forwarding" |
| 643 | config_foreach fw_forwarding forwarding |
| 644 | echo "Loading redirects" |
| 645 | config_foreach fw_redirect redirect |
| 646 | echo "Loading rules" |
| 647 | config_foreach fw_rule rule |
| 648 | echo "Loading includes" |
| 649 | config_foreach fw_include include |
| 650 | echo "Loading zone defaults" |
| 651 | config_foreach fw_zone_defaults zone |
| 652 | uci_set_state firewall core loaded 1 |
| 653 | config_set core loaded 1 |
| 654 | config_foreach fw_check_notrack zone |
| 655 | INTERFACES="$(sh -c ' |
| 656 | . /etc/functions.sh; config_load network |
| 657 | echo_up() { local up; config_get_bool up "$1" up 0; [ $up = 1 ] && echo "$1"; } |
| 658 | config_foreach echo_up interface |
| 659 | ')" |
| 660 | for interface in $INTERFACES; do |
| 661 | fw_event ifup "$interface" |
| 662 | done |
| 663 | |
| 664 | uci_set_state firewall core zones "$ZONE_NAMES" |
| 665 | } |
| 666 | |
| 667 | fw_stop() { |
| 668 | local z n i |
| 669 | config_get z core zones |
| 670 | for z in $z; do |
| 671 | config_get n core "${z}_networks" |
| 672 | for n in $n; do |
| 673 | config_get i core "${n}_ifname" |
| 674 | [ -n "$i" ] && env -i ACTION=remove ZONE="$z" INTERFACE="$n" DEVICE="$i" \ |
| 675 | /sbin/hotplug-call firewall |
| 676 | done |
| 677 | done |
| 678 | |
| 679 | fw_clear |
| 680 | $IPTABLES -P INPUT ACCEPT |
| 681 | $IPTABLES -P OUTPUT ACCEPT |
| 682 | $IPTABLES -P FORWARD ACCEPT |
| 683 | uci_revert_state firewall |
| 684 | } |
| 685 | |