[package] firewall:
[openwrt/svn-archive/archive.git] / package / firewall / files / lib / core_interface.sh
1 # Copyright (C) 2009-2010 OpenWrt.org
2
3 fw_configure_interface() {
4 local iface=$1
5 local action=$2
6 local ifname=$3
7 local aliasnet=$4
8
9 [ "$action" == "add" ] && {
10 local status=$(uci_get_state network "$iface" up 0)
11 [ "$status" == 1 ] || [ -n "$aliasnet" ] || return 0
12 }
13
14 [ -n "$ifname" ] || {
15 ifname=$(uci_get_state network "$iface" ifname "$iface")
16 ifname="${ifname%%:*}"
17 }
18
19 [ "$ifname" == "lo" ] && return 0
20
21 fw_callback pre interface
22
23 fw__do_rules() {
24 local action=$1
25 local zone=$2
26 local chain=zone_${zone}
27 local ifname=$3
28 local subnet=$4
29
30 local inet onet
31 local mode=$(fw_get_family_mode x $zone i)
32
33 case "$mode/$subnet" in
34 # Zone supports v6 only or dual, need v6
35 6/*:*|i/*:*)
36 inet="{ -s $subnet -d ::/0 }"
37 onet="{ -s ::/0 -d $subnet }"
38 mode=6
39 ;;
40
41 # Zone supports v4 only or dual, need v4
42 4/*.*.*.*|i/*.*.*.*)
43 inet="{ -s $subnet -d 0.0.0.0/0 }"
44 onet="{ -s 0.0.0.0/0 -d $subnet }"
45 mode=4
46 ;;
47
48 # Need v6 while zone is v4
49 */*:*) fw_log info "zone $zone does not support IPv6 address family, skipping"; return ;;
50
51 # Need v4 while zone is v6
52 */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;;
53 esac
54
55 fw $action $mode f ${chain}_ACCEPT ACCEPT ^ $onet { -o "$ifname" }
56 fw $action $mode f ${chain}_ACCEPT ACCEPT ^ $inet { -i "$ifname" }
57 fw $action $mode f ${chain}_DROP DROP ^ $onet { -o "$ifname" }
58 fw $action $mode f ${chain}_DROP DROP ^ $inet { -i "$ifname" }
59 fw $action $mode f ${chain}_REJECT reject ^ $onet { -o "$ifname" }
60 fw $action $mode f ${chain}_REJECT reject ^ $inet { -i "$ifname" }
61
62 fw $action $mode n ${chain}_nat MASQUERADE ^ $onet { -o "$ifname" }
63 fw $action $mode f ${chain}_MSSFIX TCPMSS ^ $onet { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu }
64
65 fw $action $mode f input ${chain} $ $inet { -i "$ifname" }
66 fw $action $mode f forward ${chain}_forward $ $inet { -i "$ifname" }
67 fw $action $mode n PREROUTING ${chain}_prerouting ^ $inet { -i "$ifname" }
68 fw $action $mode r PREROUTING ${chain}_notrack ^ $inet { -i "$ifname" }
69 }
70
71 local old_zones old_ifname old_subnets
72 config_get old_zones core "${iface}_zone"
73 [ -n "$old_zones" ] && {
74 config_get old_ifname core "${iface}_ifname"
75 config_get old_subnets core "${iface}_subnets"
76
77 local z
78 for z in $old_zones; do
79 local n
80 for n in ${old_subnets:-""}; do
81 fw_log info "removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
82 fw__do_rules del $z $old_ifname $n
83 done
84
85 [ -n "$old_subnets" ] || ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
86 done
87
88 local old_aliases
89 config_get old_aliases core "${iface}_aliases"
90
91 local a
92 for a in $old_aliases; do
93 fw_configure_interface "$a" del "$old_ifname"
94 done
95
96 uci_revert_state firewall core "${iface}_zone"
97 uci_revert_state firewall core "${iface}_ifname"
98 uci_revert_state firewall core "${iface}_subnets"
99 uci_revert_state firewall core "${iface}_aliases"
100 }
101
102 [ "$action" == del ] && return
103
104 local new_zones=
105 load_zone() {
106 fw_config_get_zone "$1"
107 list_contains zone_network "$iface" || return
108
109 fw_log info "adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
110 fw__do_rules add ${zone_name} "$ifname" $aliasnet
111 append new_zones $zone_name
112
113 [ -n "$aliasnet" ] || ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
114 }
115 config_foreach load_zone zone
116
117 [ -z "$aliasnet" ] && {
118 local aliases
119 config_get aliases "$iface" aliases
120
121 local a
122 for a in $aliases; do
123 local ipaddr netmask ip6addr
124 config_get ipaddr "$a" ipaddr
125 config_get netmask "$a" netmask
126 config_get ip6addr "$a" ip6addr
127
128 [ -n "$ipaddr" ] && fw_configure_interface "$a" add "" "$ipaddr${netmask:+/$netmask}"
129 [ -n "$ip6addr" ] && fw_configure_interface "$a" add "" "$ip6addr"
130 done
131
132 fw_sysctl_interface $ifname
133 fw_callback post interface
134
135 uci_set_state firewall core "${iface}_aliases" "$aliases"
136 } || {
137 local subnets=
138 config_get subnets core "${iface}_subnets"
139 append subnets "$aliasnet"
140
141 config_set core "${iface}_subnets" "$subnets"
142 uci_set_state firewall core "${iface}_subnets" "$subnets"
143 }
144
145 uci_set_state firewall core "${iface}_zone" "$new_zones"
146 uci_set_state firewall core "${iface}_ifname" "$ifname"
147 }
148
149 fw_sysctl_interface() {
150 local ifname=$1
151 {
152 sysctl -w net.ipv4.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
153 sysctl -w net.ipv6.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
154 sysctl -w net.ipv4.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
155 sysctl -w net.ipv6.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
156 } >/dev/null 2>/dev/null
157 }
158