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