Add support for miniupnpd and cjdns
[librecmc/package-feed.git] / net / miniupnpd / files / firewall.include
1 #!/bin/sh
2 # miniupnpd integration for firewall3
3
4 IP6TABLES=/usr/sbin/ip6tables
5
6 iptables -t filter -N MINIUPNPD 2>/dev/null
7 iptables -t nat -N MINIUPNPD 2>/dev/null
8 iptables -t nat -N MINIUPNPD-POSTROUTING 2>/dev/null
9
10 [ -x $IP6TABLES ] && $IP6TABLES -t filter -N MINIUPNPD 2>/dev/null
11
12 . /lib/functions/network.sh
13
14 ADDED=0
15
16 add_extzone_rules() {
17     local ext_zone=$1
18
19     [ -z "$ext_zone" ] && return
20
21     # IPv4 - due to NAT, need to add both to nat and filter table
22     iptables -t filter -I zone_${ext_zone}_forward -j MINIUPNPD
23     iptables -t nat -I zone_${ext_zone}_prerouting -j MINIUPNPD
24     iptables -t nat -I zone_${ext_zone}_postrouting -j MINIUPNPD-POSTROUTING
25
26     # IPv6 if available - filter only
27     [ -x $IP6TABLES ] && {
28         $IP6TABLES -t filter -I zone_${ext_zone}_forward -j MINIUPNPD
29     }
30     ADDED=$(($ADDED + 1))
31 }
32
33 # By default, user configuration is king.
34
35 for ext_iface in $(uci -q get upnpd.config.external_iface); do
36     add_extzone_rules $(fw3 -q network "$ext_iface")
37 done
38
39 add_extzone_rules $(uci -q get upnpd.config.external_zone)
40
41 [ ! $ADDED = 0 ] && exit 0
42
43
44 # If really nothing is available, resort to network_find_wan{,6} and
45 # assume external interfaces all have same firewall zone.
46
47 # (This heuristic may fail horribly, in case of e.g. multihoming, so
48 # please set external_zone in that case!)
49
50 network_find_wan wan_iface
51 network_find_wan6 wan6_iface
52
53 for ext_iface in $wan_iface $wan6_iface; do
54     # fw3 -q network fails on sub-interfaces => map to device first
55     network_get_device ext_device $ext_iface
56     add_extzone_rules $(fw3 -q device "$ext_device")
57 done