2018-04-30 10:09:05 +00:00
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2014 OpenWrt.org
START=94
STOP=15
2020-02-07 05:47:58 +00:00
USE_PROCD=1
PROG=/usr/sbin/miniupnpd
2022-08-16 21:35:11 +00:00
[ -x "$(command -v nft)" ] && FW="fw4" || FW="fw3"
2018-04-30 10:09:05 +00:00
upnpd_get_port_range() {
local var="$1"; shift
local val
config_get val "$@"
case "$val" in
[0-9]*[:-][0-9]*)
export -n -- "${var}_start=${val%%[:-]*}"
export -n -- "${var}_end=${val##*[:-]}"
;;
[0-9]*)
export -n -- "${var}_start=$val"
export -n -- "${var}_end="
;;
esac
}
conf_rule_add() {
local cfg="$1"
2020-02-07 05:47:58 +00:00
local action int_addr
local ext_start ext_end int_start int_end comment
2018-04-30 10:09:05 +00:00
config_get action "$cfg" action "deny" # allow or deny
upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
config_get comment "$cfg" comment "ACL" # comment
# Make a single IP IP/32 so that miniupnpd.conf can use it.
[ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
2020-02-07 05:47:58 +00:00
echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment"
2018-04-30 10:09:05 +00:00
}
upnpd_write_bool() {
local opt="$1"
local def="${2:-0}"
local alt="${3:-$opt}"
local val
config_get_bool val config "$opt" "$def"
if [ "$val" -eq 0 ]; then
2020-02-07 05:47:58 +00:00
echo "$alt=no"
2018-04-30 10:09:05 +00:00
else
2020-02-07 05:47:58 +00:00
echo "$alt=yes"
2018-04-30 10:09:05 +00:00
fi
}
2020-02-07 05:47:58 +00:00
upnpd() {
2018-04-30 10:09:05 +00:00
config_load "upnpd"
2020-02-07 05:47:58 +00:00
local external_iface external_iface6 external_zone external_ip internal_iface
local upload download log_output port config_file serial_number model_number
local use_stun stun_host stun_port uuid notify_interval presentation_url
2018-04-30 10:09:05 +00:00
local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
2020-07-02 03:18:45 +00:00
local ipv6_disable
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
local enabled
2018-04-30 10:09:05 +00:00
config_get_bool enabled config enabled 1
[ "$enabled" -eq 0 ] && return 1
2020-02-07 05:47:58 +00:00
config_get external_iface config external_iface
2020-05-14 10:30:12 +00:00
config_get external_iface6 config external_iface6
2020-02-07 05:47:58 +00:00
config_get external_zone config external_zone
config_get external_ip config external_ip
config_get internal_iface config internal_iface
2018-04-30 10:09:05 +00:00
config_get port config port 5000
config_get upload config upload
config_get download config download
2020-02-07 05:47:58 +00:00
config_get_bool log_output config log_output 0
config_get config_file config config_file
2018-04-30 10:09:05 +00:00
config_get serial_number config serial_number
config_get model_number config model_number
config_get uuid config uuid
2020-02-14 19:23:06 +00:00
config_get use_stun config use_stun 0
2020-02-07 05:47:58 +00:00
config_get stun_host config stun_host
config_get stun_port config stun_port
2018-04-30 10:09:05 +00:00
config_get notify_interval config notify_interval
config_get presentation_url config presentation_url
config_get upnp_lease_file config upnp_lease_file
config_get clean_ruleset_threshold config clean_ruleset_threshold
config_get clean_ruleset_interval config clean_ruleset_interval
2020-07-02 03:18:45 +00:00
config_get ipv6_disable config ipv6_disable 0
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
local conf ifname ifname6
2018-04-30 10:09:05 +00:00
. /lib/functions/network.sh
2020-05-14 10:30:12 +00:00
if [ -n "$external_iface" ] ; then
network_get_device ifname "$external_iface"
else
if [ -n "$external_zone" ] ; then
2022-08-16 21:35:11 +00:00
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
2020-05-14 10:30:12 +00:00
else
network_find_wan external_iface && \
network_get_device ifname "$external_iface"
fi
fi
if [ -n "$external_iface6" ] ; then
network_get_device ifname6 "$external_iface6"
else
if [ -n "$external_zone" ] ; then
2022-08-16 21:35:11 +00:00
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
2020-05-14 10:30:12 +00:00
else
network_find_wan6 external_iface6 && \
network_get_device ifname6 "$external_iface6"
fi
fi
2020-02-07 05:47:58 +00:00
if [ -n "$config_file" ]; then
conf="$config_file"
2018-04-30 10:09:05 +00:00
else
local tmpconf="/var/etc/miniupnpd.conf"
2020-02-07 05:47:58 +00:00
conf="$tmpconf"
2018-04-30 10:09:05 +00:00
mkdir -p /var/etc
2020-02-07 05:47:58 +00:00
{
echo "ext_ifname=$ifname"
echo "ext_ifname6=$ifname6"
[ -n "$external_ip" ] && echo "ext_ip=$external_ip"
2018-04-30 10:09:05 +00:00
local iface
2020-02-07 05:47:58 +00:00
for iface in ${internal_iface:-lan}; do
2020-12-07 03:52:17 +00:00
local device
2020-02-07 05:47:58 +00:00
network_get_device device "$iface" && echo "listening_ip=$device"
2018-04-30 10:09:05 +00:00
done
config_load "upnpd"
upnpd_write_bool enable_natpmp 1
upnpd_write_bool enable_upnp 1
upnpd_write_bool secure_mode 1
upnpd_write_bool system_uptime 1
upnpd_write_bool igdv1 0 force_igd_desc_v1
2020-02-07 05:47:58 +00:00
upnpd_write_bool use_stun 0 ext_perform_stun
2020-07-02 03:18:45 +00:00
upnpd_write_bool ipv6_disable $ipv6_disable
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
[ "$use_stun" -eq 0 ] || {
[ -n "$stun_host" ] && echo "ext_stun_host=$stun_host"
[ -n "$stun_port" ] && echo "ext_stun_port=$stun_port"
2018-04-30 10:09:05 +00:00
}
2020-02-07 05:47:58 +00:00
[ -n "$upload" ] && [ -n "$download" ] && {
echo "bitrate_down=$((download * 1024 * 8))"
echo "bitrate_up=$((upload * 1024 * 8))"
}
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
[ -n "$upnp_lease_file" ] && touch "$upnp_lease_file" && echo "lease_file=$upnp_lease_file"
[ -n "$presentation_url" ] && echo "presentation_url=$presentation_url"
[ -n "$notify_interval" ] && echo "notify_interval=$notify_interval"
[ -n "$clean_ruleset_threshold" ] && echo "clean_ruleset_threshold=$clean_ruleset_threshold"
[ -n "$clean_ruleset_interval" ] && echo "clean_ruleset_interval=$clean_ruleset_interval"
[ -n "$serial_number" ] && echo "serial=$serial_number"
[ -n "$model_number" ] && echo "model_number=$model_number"
[ -n "$port" ] && echo "port=$port"
2018-04-30 10:09:05 +00:00
[ -z "$uuid" ] && {
uuid="$(cat /proc/sys/kernel/random/uuid)"
2020-02-07 05:47:58 +00:00
uci set upnpd.config.uuid="$uuid"
2018-04-30 10:09:05 +00:00
uci commit upnpd
}
2020-02-07 05:47:58 +00:00
[ "$uuid" = "nocli" ] || echo "uuid=$uuid"
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
config_foreach conf_rule_add perm_rule
2018-04-30 10:09:05 +00:00
2022-08-16 21:35:11 +00:00
if [ "Z$FW" = "Zfw4" ]; then
#When using nftables configure miniupnpd to use its own table and chains
echo "upnp_table_name=miniupnpd"
echo "upnp_nat_table_name=miniupnpd"
echo "upnp_forward_chain=forward"
echo "upnp_nat_chain=prerouting"
echo "upnp_nat_postrouting_chain=postrouting"
fi
2020-02-07 05:47:58 +00:00
} > "$tmpconf"
2018-04-30 10:09:05 +00:00
fi
if [ -n "$ifname" ]; then
# start firewall
2022-08-16 21:35:11 +00:00
if [ "Z$FW" = "Zfw4" ]; then
#Add a miniupnpd table so that when fw4 reloads port-forwadings aren't lost, also give it priority so that port-forwards are considered before standard firewall rules
nft add table inet miniupnpd
nft add chain inet miniupnpd forward { type filter hook forward priority -20 \; policy accept \; comment \"Miniupnpd forwarding table\" \; }
nft add chain inet miniupnpd prerouting { type nat hook prerouting priority dstnat -20 \; policy accept \; comment \"Miniupnpd prerouting table\" \; }
nft add chain inet miniupnpd postrouting { type nat hook postrouting priority srcnat -20 \; policy accept \; comment \"Miniupnpd postrouting table\" \; }
else
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
fi
2018-04-30 10:09:05 +00:00
else
logger -t "upnp daemon" "external interface not found, not starting"
fi
2020-02-07 05:47:58 +00:00
procd_open_instance
procd_set_param command "$PROG"
procd_append_param command -f "$conf"
[ "$log_output" = "1" ] && procd_append_param command -d
procd_close_instance
}
2018-04-30 10:09:05 +00:00
2020-02-07 05:47:58 +00:00
stop_service() {
2022-08-16 21:35:11 +00:00
if [ "Z$FW" = "Zfw3" ]; then
iptables -t nat -F MINIUPNPD 2>/dev/null
iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
iptables -t filter -F MINIUPNPD 2>/dev/null
[ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
else
#delete the table removing port-forwardings when exiting
nft delete table inet miniupnpd
fi
2020-02-07 05:47:58 +00:00
}
start_service() {
config_load "upnpd"
config_foreach upnpd "upnpd"
}
service_triggers() {
procd_add_reload_trigger "upnpd"
2022-08-16 21:35:11 +00:00
}