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
78 eval "mod=\$FW_${fam#G}_${tab}"
84 *4) mod=iptable_${tab} ;;
85 *6) mod=ip6table_${tab} ;;
88 grep -q "^${mod} " /proc/modules
90 export FW_${fam}_${tab}=$mod
96 eval "err=\$FW_${fam}_ERROR"
103 *4) [ $FW_DISABLE_IPV4 == 0 ] && app=iptables || return ;;
104 *6) [ $FW_DISABLE_IPV6 == 0 ] && app=ip6tables || return ;;
105 i) fw__dualip "$@"; return ;;
106 I) fw__autoip "$@"; return ;;
109 -) fw $cmd i $tab $chn $tgt $pos "$@"; return ;;
119 case "$cmd:$chn:$tgt:$pos" in
120 add:*:-:*) cmd=new-chain ;;
121 add:*:*:-) cmd=append ;;
122 add:*:*:$) cmd=append ;;
123 add:*:*:*) cmd=insert ;;
124 del:-:*:*) cmd=delete-chain; fw flush $fam $tab ;;
125 del:*:-:*) cmd=delete-chain; fw flush $fam $tab $chn ;;
126 del:*:*:*) cmd=delete ;;
128 policy:*) pol=$tgt; tgt=- ;;
129 has:*) fw__has; return ;;
130 err:*) fw__err; return ;;
131 list:*) cmd="numeric --verbose --$cmd" ;;
146 if ! fw__has - family || ! fw__has $tab ; then
147 export FW_${fam}_ERROR=0
152 G*) shift; while [ $# -gt 0 ] && [ "$1" != "{" ]; do shift; done ;;
155 if [ $# -gt 0 ]; then
157 if [ $cmd == delete ]; then
162 local cmdline="$app --table ${tab} --${cmd} ${chn} ${pol} ${pos} ${tgt:+--jump "$tgt"}"
163 while [ $# -gt 1 ]; do
165 ip6tables:--icmp-type) cmdline="$cmdline --icmpv6-type" ;;
166 ip6tables:icmp|ip6tables:ICMP) cmdline="$cmdline icmpv6" ;;
167 iptables:--icmpv6-type) cmdline="$cmdline --icmp-type" ;;
168 iptables:icmpv6) cmdline="$cmdline icmp" ;;
169 *) cmdline="$cmdline $1" ;;
174 [ -n "$FW_TRACE" ] && echo $cmdline >&2
181 fw_get_port_range() {
186 fw_get_port_range $_var "${_ports}-${4}" $_delim
190 local _first=${_ports%-*}
191 local _last=${_ports#*-}
192 if [ "$_first" != "$_last" ]; then
193 export -- "$_var=$_first$_delim$_last"
195 export -- "$_var=$_first"
199 fw_get_family_mode() {
206 [ -n "$FW_ZONES4$FW_ZONES6" ] && {
207 list_contains FW_ZONES4 $_zone && _ipv4=1 || _ipv4=0
208 list_contains FW_ZONES6 $_zone && _ipv6=1 || _ipv6=0
210 _ipv4=$(uci_get_state firewall core ${_zone}_ipv4 0)
211 _ipv6=$(uci_get_state firewall core ${_zone}_ipv6 0)
214 case "$_hint:$_ipv4:$_ipv6" in
215 *4:1:*|*:1:0) export -n -- "$_var=G4" ;;
216 *6:*:1|*:0:1) export -n -- "$_var=G6" ;;
217 *) export -n -- "$_var=$_mode" ;;
226 [ "${_ipaddr#!}" != "$_ipaddr" ] && \
227 export -n -- "$_var=! $_flag ${_ipaddr#!}" || \
228 export -n -- "$_var=${_ipaddr:+$_flag $_ipaddr}"