[package] firewall:
[openwrt.git] / package / firewall / files / lib / core_interface.sh
1 # Copyright (C) 2009-2011 OpenWrt.org
2
3 fw__uci_state_add() {
4 local var="$1"
5 local item="$2"
6
7 local val="$(uci_get_state firewall core $var)"
8 local e1; for e1 in $item; do
9 local e2; for e2 in $val; do
10 [ "$e1" = "$e2" ] && e1=""
11 done
12 val="${val:+$val${e1:+ }}$e1"
13 done
14
15 uci_toggle_state firewall core $var "$val"
16 }
17
18 fw__uci_state_del() {
19 local var="$1"
20 local item="$2"
21
22 local rest=""
23 local val="$(uci_get_state firewall core $var)"
24 local e1; for e1 in $val; do
25 local e2; for e2 in $item; do
26 [ "$e1" = "$e2" ] && e1=""
27 done
28 rest="${rest:+$rest${e1:+ }}$e1"
29 done
30
31 uci_toggle_state firewall core $var "$val"
32 }
33
34 fw_configure_interface() {
35 local iface=$1
36 local action=$2
37 local ifname=$3
38 local aliasnet=$4
39
40 [ "$action" == "add" ] && {
41 local status=$(uci_get_state network "$iface" up 0)
42 [ "$status" == 1 ] || [ -n "$aliasnet" ] || return 0
43 }
44
45 [ -n "$ifname" ] || {
46 ifname=$(uci_get_state network "$iface" ifname)
47 ifname="${ifname%%:*}"
48 [ -z "$ifname" ] && return 0
49 }
50
51 [ "$ifname" == "lo" ] && return 0
52
53 fw_callback pre interface
54
55 fw__do_rules() {
56 local action=$1
57 local zone=$2
58 local chain=zone_${zone}
59 local ifname=$3
60 local subnet=$4
61
62 local inet onet mode
63 fw_get_family_mode mode x $zone i
64
65 case "$mode/$subnet" in
66 # Zone supports v6 only or dual, need v6
67 G6/*:*|i/*:*)
68 inet="-s $subnet -d ::/0"
69 onet="-s ::/0 -d $subnet"
70 mode=6
71 ;;
72
73 # Zone supports v4 only or dual, need v4
74 G4/*.*.*.*|i/*.*.*.*)
75 inet="-s $subnet -d 0.0.0.0/0"
76 onet="-s 0.0.0.0/0 -d $subnet"
77 mode=4
78 ;;
79
80 # Need v6 while zone is v4
81 */*:*) fw_log info "zone $zone does not support IPv6 address family, skipping"; return ;;
82
83 # Need v4 while zone is v6
84 */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;;
85
86 # Strip prefix
87 *) mode="${mode#G}" ;;
88 esac
89
90 lock /var/run/firewall-interface.lock
91
92 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -o "$ifname" $onet }
93 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -i "$ifname" $inet }
94 fw $action $mode f ${chain}_DROP DROP $ { -o "$ifname" $onet }
95 fw $action $mode f ${chain}_DROP DROP $ { -i "$ifname" $inet }
96 fw $action $mode f ${chain}_REJECT reject $ { -o "$ifname" $onet }
97 fw $action $mode f ${chain}_REJECT reject $ { -i "$ifname" $inet }
98
99 [ "$(uci_get_state firewall core "${zone}_tcpmss")" == 1 ] && \
100 fw $action $mode m ${chain}_MSSFIX TCPMSS $ \
101 { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet }
102
103 fw $action $mode f input ${chain} $ { -i "$ifname" $inet }
104 fw $action $mode f forward ${chain}_forward $ { -i "$ifname" $inet }
105 fw $action $mode n PREROUTING ${chain}_prerouting $ { -i "$ifname" $inet }
106 fw $action $mode r PREROUTING ${chain}_notrack $ { -i "$ifname" $inet }
107 fw $action $mode n POSTROUTING ${chain}_nat $ { -o "$ifname" $onet }
108
109 lock -u /var/run/firewall-interface.lock
110 }
111
112 local old_zones old_ifname old_subnets
113 config_get old_zones core "${iface}_zone"
114 [ -n "$old_zones" ] && {
115 config_get old_ifname core "${iface}_ifname"
116 config_get old_subnets core "${iface}_subnets"
117
118 local z
119 for z in $old_zones; do
120 local n
121 for n in ${old_subnets:-""}; do
122 fw_log info "removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
123 fw__do_rules del $z $old_ifname $n
124 done
125
126 [ -n "$old_subnets" ] || {
127 fw__uci_state_del "${z}_networks" "$iface"
128 env -i ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
129 }
130 done
131
132 local old_aliases
133 config_get old_aliases core "${iface}_aliases"
134
135 local a
136 for a in $old_aliases; do
137 fw_configure_interface "$a" del "$old_ifname"
138 done
139
140 uci_revert_state firewall core "${iface}_zone"
141 uci_revert_state firewall core "${iface}_ifname"
142 uci_revert_state firewall core "${iface}_subnets"
143 uci_revert_state firewall core "${iface}_aliases"
144 }
145
146 [ "$action" == del ] && return
147
148 [ -z "$aliasnet" ] && {
149 local aliases
150 config_get aliases "$iface" aliases
151
152 local a
153 for a in $aliases; do
154 local ipaddr netmask ip6addr
155 config_get ipaddr "$a" ipaddr
156 config_get netmask "$a" netmask
157 config_get ip6addr "$a" ip6addr
158
159 [ -n "$ipaddr" ] && fw_configure_interface "$a" add "" "$ipaddr${netmask:+/$netmask}"
160 [ -n "$ip6addr" ] && fw_configure_interface "$a" add "" "$ip6addr"
161 done
162
163 fw_sysctl_interface $ifname
164 fw_callback post interface
165
166 uci_toggle_state firewall core "${iface}_aliases" "$aliases"
167 } || {
168 local subnets=
169 config_get subnets core "${iface}_subnets"
170 append subnets "$aliasnet"
171
172 config_set core "${iface}_subnets" "$subnets"
173 uci_toggle_state firewall core "${iface}_subnets" "$subnets"
174 }
175
176 local new_zones=
177 load_zone() {
178 fw_config_get_zone "$1"
179 list_contains zone_network "$iface" || return
180
181 fw_log info "adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
182 fw__do_rules add ${zone_name} "$ifname" "$aliasnet"
183 append new_zones $zone_name
184
185 [ -n "$aliasnet" ] || {
186 fw__uci_state_add "${zone_name}_networks" "${zone_network}"
187 env -i ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
188 }
189 }
190 config_foreach load_zone zone
191
192 uci_toggle_state firewall core "${iface}_zone" "$new_zones"
193 uci_toggle_state firewall core "${iface}_ifname" "$ifname"
194 }
195
196 fw_sysctl_interface() {
197 local ifname=$1
198 {
199 sysctl -w net.ipv4.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
200 sysctl -w net.ipv6.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
201 sysctl -w net.ipv4.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
202 sysctl -w net.ipv6.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
203 } >/dev/null 2>/dev/null
204 }
205
This page took 0.061533 seconds and 5 git commands to generate.