You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

  1. #!/bin/sh
  2. # miniupnpd integration for firewall3
  3. IPTABLES=/usr/sbin/iptables
  4. IP6TABLES=/usr/sbin/ip6tables
  5. $IPTABLES -t filter -N MINIUPNPD 2>/dev/null
  6. $IPTABLES -t nat -N MINIUPNPD 2>/dev/null
  7. $IPTABLES -t nat -N MINIUPNPD-POSTROUTING 2>/dev/null
  8. [ -x $IP6TABLES ] && $IP6TABLES -t filter -N MINIUPNPD 2>/dev/null
  9. . /lib/functions/network.sh
  10. # helper to insert in chain as penultimate
  11. iptables_prepend_rule() {
  12. local iptables="$1"
  13. local table="$2"
  14. local chain="$3"
  15. local target="$4"
  16. $iptables -t "$table" -I "$chain" $($iptables -t "$table" --line-numbers -nL "$chain" | \
  17. sed -ne '$s/[^0-9].*//p') -j "$target"
  18. }
  19. ADDED=0
  20. add_extzone_rules() {
  21. local ext_zone="$1"
  22. [ -z "$ext_zone" ] && return
  23. # IPv4 - due to NAT, need to add both to nat and filter table
  24. # need to insert as penultimate rule for forward & postrouting since final rule might be a fw3 REJECT
  25. iptables_prepend_rule "$IPTABLES" filter "zone_${ext_zone}_forward" MINIUPNPD
  26. $IPTABLES -t nat -A "zone_${ext_zone}_prerouting" -j MINIUPNPD
  27. iptables_prepend_rule "$IPTABLES" nat "zone_${ext_zone}_postrouting" MINIUPNPD-POSTROUTING
  28. # IPv6 if available - filter only
  29. [ -x $IP6TABLES ] && {
  30. iptables_prepend_rule "$IP6TABLES" filter "zone_${ext_zone}_forward" MINIUPNPD
  31. }
  32. ADDED=$(($ADDED + 1))
  33. }
  34. # By default, user configuration is king.
  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. add_extzone_rules $(uci -q get upnpd.config.external_zone)
  39. [ "$ADDED" -ne 0 ] && exit 0
  40. # If really nothing is available, resort to network_find_wan{,6} and
  41. # assume external interfaces all have same firewall zone.
  42. # (This heuristic may fail horribly, in case of e.g. multihoming, so
  43. # please set external_zone in that case!)
  44. network_find_wan wan_iface
  45. network_find_wan6 wan6_iface
  46. for ext_iface in $wan_iface $wan6_iface; do
  47. # fw3 -q network fails on sub-interfaces => map to device first
  48. network_get_device ext_device $ext_iface
  49. add_extzone_rules $(fw3 -q device "$ext_device")
  50. done