firewall.include 1.6 KB

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