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.

71 lines
2.2 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 input & forward & postrouting since final rule might be a fw3 REJECT
  25. iptables_prepend_rule "$IPTABLES" filter "zone_${ext_zone}_input" MINIUPNPD
  26. iptables_prepend_rule "$IPTABLES" filter "zone_${ext_zone}_forward" MINIUPNPD
  27. $IPTABLES -t nat -A "zone_${ext_zone}_prerouting" -j MINIUPNPD
  28. iptables_prepend_rule "$IPTABLES" nat "zone_${ext_zone}_postrouting" MINIUPNPD-POSTROUTING
  29. # IPv6 if available - filter only
  30. [ -x $IP6TABLES ] && {
  31. iptables_prepend_rule "$IP6TABLES" filter "zone_${ext_zone}_input" MINIUPNPD
  32. iptables_prepend_rule "$IP6TABLES" filter "zone_${ext_zone}_forward" MINIUPNPD
  33. }
  34. ADDED=$(($ADDED + 1))
  35. }
  36. # By default, user configuration is king.
  37. for ext_iface in $(uci -q get upnpd.config.external_iface); do
  38. add_extzone_rules $(fw3 -q network "$ext_iface")
  39. done
  40. add_extzone_rules $(uci -q get upnpd.config.external_zone)
  41. [ "$ADDED" -ne 0 ] && exit 0
  42. # If really nothing is available, resort to network_find_wan{,6} and
  43. # assume external interfaces all have same firewall zone.
  44. # (This heuristic may fail horribly, in case of e.g. multihoming, so
  45. # please set external_zone in that case!)
  46. network_find_wan wan_iface
  47. network_find_wan6 wan6_iface
  48. for ext_iface in $wan_iface $wan6_iface; do
  49. # fw3 -q network fails on sub-interfaces => map to device first
  50. network_get_device ext_device $ext_iface
  51. add_extzone_rules $(fw3 -q device "$ext_device")
  52. done