1 # Copyright (C) 2009-2010 OpenWrt.org
2 # Copyright (C) 2009 Malte S. Stretz
11 [ "${-#*x}" == "$-" ] && {
26 fw__exec() { # <action> <family> <table> <chain> <target> <position> { <rules> }
27 local cmd fam tab chn tgt pos
29 for i in cmd fam tab chn tgt pos; do
30 if [ "$1" -a "$1" != '{' ]; then
39 export FW_${fam#G}_ERROR=$1
44 fw $cmd 4 $tab $chn $tgt $pos "$@"
45 fw $cmd 6 $tab $chn $tgt $pos "$@"
46 fw__rc $((FW_4_ERROR | FW_6_ERROR))
52 while [ "$1" != '}' ]; do
60 if [ "${ip4:-4}" == "${ip6:-6}" ]; then
61 echo "fw: can't mix ip4 and ip6" >&2
64 local ver=${ip4:+4}${ip6:+6}
66 fw $cmd ${ver:-i} $tab $chn $tgt $pos "$@"
72 if [ $tab == '-' ]; then
73 type $app > /dev/null 2> /dev/null
82 eval "err=\$FW_${fam}_ERROR"
89 *4) [ $FW_DISABLE_IPV4 == 0 ] && app=iptables || return ;;
90 *6) [ $FW_DISABLE_IPV6 == 0 ] && app=ip6tables || return ;;
91 i) fw__dualip "$@"; return ;;
92 I) fw__autoip "$@"; return ;;
95 -) fw $cmd i $tab $chn $tgt $pos "$@"; return ;;
105 case "$cmd:$chn:$tgt:$pos" in
106 add:*:-:*) cmd=new-chain ;;
107 add:*:*:-) cmd=append ;;
108 add:*:*:$) cmd=append ;;
109 add:*:*:*) cmd=insert ;;
110 del:-:*:*) cmd=delete-chain; fw flush $fam $tab ;;
111 del:*:-:*) cmd=delete-chain; fw flush $fam $tab $chn ;;
112 del:*:*:*) cmd=delete ;;
114 policy:*) pol=$tgt; tgt=- ;;
115 has:*) fw__has; return ;;
116 err:*) fw__err; return ;;
117 list:*) cmd="numeric --verbose --$cmd" ;;
132 +) eval "rule_offset=\${FW__RULE_OFS_${app}_${tab}_${chn}:-1}" ;;
135 if ! fw__has - family || ! fw__has $tab ; then
136 export FW_${fam}_ERROR=0
141 G*) shift; while [ $# -gt 0 ] && [ "$1" != "{" ]; do shift; done ;;
144 if [ $# -gt 0 ]; then
146 if [ $cmd == delete ]; then
151 local cmdline="$app --table ${tab} --${cmd} ${chn} ${pol} ${rule_offset:-${pos}} ${tgt:+--jump "$tgt"}"
152 while [ $# -gt 1 ]; do
153 # special parameter handling
155 -p:icmp*|-p:1|-p:58|--protocol:icmp*|--protocol:1|--protocol:58)
156 [ "$app" = ip6tables ] && \
157 cmdline="$cmdline -p icmpv6" || \
158 cmdline="$cmdline -p icmp"
161 --icmp-type:*|--icmpv6-type:*)
163 if [ "$app" = ip6tables ] && fw_check_icmptype6 icmp_type "$2"; then
164 cmdline="$cmdline $icmp_type"
165 elif [ "$app" = iptables ] && fw_check_icmptype4 icmp_type "$2"; then
166 cmdline="$cmdline $icmp_type"
168 local fam=IPv4; [ "$app" = ip6tables ] && fam=IPv6
169 fw_log info "ICMP type '$2' is not valid for $fam address family, skipping rule"
174 *) cmdline="$cmdline $1" ;;
179 [ -n "$FW_TRACE" ] && echo $cmdline >&2
184 [ $rv -eq 0 ] && [ -n "$rule_offset" ] && \
185 export -- "FW__RULE_OFS_${app}_${tab}_${chn}=$(($rule_offset + 1))"
189 fw_get_port_range() {
194 fw_get_port_range $_var "${_ports}-${4}" $_delim
198 local _first=${_ports%-*}
199 local _last=${_ports#*-}
200 if [ "${_first#!}" != "${_last#!}" ]; then
201 export -- "$_var=$_first$_delim${_last#!}"
203 export -- "$_var=$_first"
207 fw_get_family_mode() {
214 [ "$_zone" != "*" ] && {
215 [ -n "$FW_ZONES4$FW_ZONES6" ] && {
216 list_contains FW_ZONES4 "$_zone" && _ipv4=1 || _ipv4=0
217 list_contains FW_ZONES6 "$_zone" && _ipv6=1 || _ipv6=0
219 _ipv4=$(uci_get_state firewall core "${_zone}_ipv4" 0)
220 _ipv6=$(uci_get_state firewall core "${_zone}_ipv6" 0)
227 case "$_hint:$_ipv4:$_ipv6" in
228 *4:1:*|*:1:0) export -n -- "$_var=G4" ;;
229 *6:*:1|*:0:1) export -n -- "$_var=G6" ;;
230 *) export -n -- "$_var=$_mode" ;;
239 [ "${_value#!}" != "$_value" ] && \
240 export -n -- "$_var=! $_flag ${_value#!}" || \
241 export -n -- "$_var=${_value:+$_flag $_value}"
249 local _ipaddr="$(uci_get_state network "${_name#!}" ipaddr)"
250 local _netmask="$(uci_get_state network "${_name#!}" netmask)"
254 [ "${_name#!}" != "$_name" ] && \
255 export -n -- "$_var=! $_flag $_ipaddr/${_netmask:-255.255.255.255}" || \
256 export -n -- "$_var=$_flag $_ipaddr/${_netmask:-255.255.255.255}"
258 *) export -n -- "$_var=" ;;
262 fw_check_icmptype4() {
266 ![0-9]*) export -n -- "$_var=! --icmp-type ${_type#!}"; return 0 ;;
267 [0-9]*) export -n -- "$_var=--icmp-type $_type"; return 0 ;;
270 [ -z "$FW_ICMP4_TYPES" ] && \
271 export FW_ICMP4_TYPES=$(
272 iptables -p icmp -h 2>/dev/null | \
273 sed -n -e '/^Valid ICMP Types:/ {
275 /router-advertisement/d;
276 /router-solicitation/d;
277 s/[()]/ /g; s/[[:space:]]\+/\n/g; p; n; b r
282 for _check in $FW_ICMP4_TYPES; do
283 if [ "$_check" = "${_type#!}" ]; then
284 [ "${_type#!}" != "$_type" ] && \
285 export -n -- "$_var=! --icmp-type ${_type#!}" || \
286 export -n -- "$_var=--icmp-type $_type"
291 export -n -- "$_var="
295 fw_check_icmptype6() {
299 ![0-9]*) export -n -- "$_var=! --icmpv6-type ${_type#!}"; return 0 ;;
300 [0-9]*) export -n -- "$_var=--icmpv6-type $_type"; return 0 ;;
303 [ -z "$FW_ICMP6_TYPES" ] && \
304 export FW_ICMP6_TYPES=$(
305 ip6tables -p icmpv6 -h 2>/dev/null | \
306 sed -n -e '/^Valid ICMPv6 Types:/ {
307 n; :r; s/[()]/ /g; s/[[:space:]]\+/\n/g; p; n; b r
312 for _check in $FW_ICMP6_TYPES; do
313 if [ "$_check" = "${_type#!}" ]; then
314 [ "${_type#!}" != "$_type" ] && \
315 export -n -- "$_var=! --icmpv6-type ${_type#!}" || \
316 export -n -- "$_var=--icmpv6-type $_type"
321 export -n -- "$_var="