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.

226 lines
6.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2014 OpenWrt.org
  3. START=94
  4. STOP=15
  5. USE_PROCD=1
  6. PROG=/usr/sbin/miniupnpd
  7. [ -x "$(command -v nft)" ] && FW="fw4" || FW="fw3"
  8. upnpd_get_port_range() {
  9. local var="$1"; shift
  10. local val
  11. config_get val "$@"
  12. case "$val" in
  13. [0-9]*[:-][0-9]*)
  14. export -n -- "${var}_start=${val%%[:-]*}"
  15. export -n -- "${var}_end=${val##*[:-]}"
  16. ;;
  17. [0-9]*)
  18. export -n -- "${var}_start=$val"
  19. export -n -- "${var}_end="
  20. ;;
  21. esac
  22. }
  23. conf_rule_add() {
  24. local cfg="$1"
  25. local action int_addr
  26. local ext_start ext_end int_start int_end comment
  27. config_get action "$cfg" action "deny" # allow or deny
  28. upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
  29. config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
  30. upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
  31. config_get comment "$cfg" comment "ACL" # comment
  32. # Make a single IP IP/32 so that miniupnpd.conf can use it.
  33. [ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
  34. echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment"
  35. }
  36. upnpd_write_bool() {
  37. local opt="$1"
  38. local def="${2:-0}"
  39. local alt="${3:-$opt}"
  40. local val
  41. config_get_bool val config "$opt" "$def"
  42. if [ "$val" -eq 0 ]; then
  43. echo "$alt=no"
  44. else
  45. echo "$alt=yes"
  46. fi
  47. }
  48. upnpd() {
  49. config_load "upnpd"
  50. local external_iface external_iface6 external_zone external_ip internal_iface
  51. local upload download log_output port config_file serial_number model_number
  52. local use_stun stun_host stun_port uuid notify_interval presentation_url
  53. local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
  54. local ipv6_disable
  55. local enabled
  56. config_get_bool enabled config enabled 1
  57. [ "$enabled" -eq 0 ] && return 1
  58. config_get external_iface config external_iface
  59. config_get external_iface6 config external_iface6
  60. config_get external_zone config external_zone
  61. config_get external_ip config external_ip
  62. config_get internal_iface config internal_iface
  63. config_get port config port 5000
  64. config_get upload config upload
  65. config_get download config download
  66. config_get_bool log_output config log_output 0
  67. config_get config_file config config_file
  68. config_get serial_number config serial_number
  69. config_get model_number config model_number
  70. config_get uuid config uuid
  71. config_get use_stun config use_stun 0
  72. config_get stun_host config stun_host
  73. config_get stun_port config stun_port
  74. config_get notify_interval config notify_interval
  75. config_get presentation_url config presentation_url
  76. config_get upnp_lease_file config upnp_lease_file
  77. config_get clean_ruleset_threshold config clean_ruleset_threshold
  78. config_get clean_ruleset_interval config clean_ruleset_interval
  79. config_get ipv6_disable config ipv6_disable 0
  80. local conf ifname ifname6
  81. . /lib/functions/network.sh
  82. if [ -n "$external_iface" ] ; then
  83. network_get_device ifname "$external_iface"
  84. else
  85. if [ -n "$external_zone" ] ; then
  86. ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
  87. else
  88. network_find_wan external_iface && \
  89. network_get_device ifname "$external_iface"
  90. fi
  91. fi
  92. if [ -n "$external_iface6" ] ; then
  93. network_get_device ifname6 "$external_iface6"
  94. else
  95. if [ -n "$external_zone" ] ; then
  96. ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
  97. else
  98. network_find_wan6 external_iface6 && \
  99. network_get_device ifname6 "$external_iface6"
  100. fi
  101. fi
  102. if [ -n "$config_file" ]; then
  103. conf="$config_file"
  104. else
  105. local tmpconf="/var/etc/miniupnpd.conf"
  106. conf="$tmpconf"
  107. mkdir -p /var/etc
  108. {
  109. echo "ext_ifname=$ifname"
  110. echo "ext_ifname6=$ifname6"
  111. [ -n "$external_ip" ] && echo "ext_ip=$external_ip"
  112. local iface
  113. for iface in ${internal_iface:-lan}; do
  114. local device
  115. network_get_device device "$iface" && echo "listening_ip=$device"
  116. done
  117. config_load "upnpd"
  118. upnpd_write_bool enable_natpmp 1
  119. upnpd_write_bool enable_upnp 1
  120. upnpd_write_bool secure_mode 1
  121. upnpd_write_bool system_uptime 1
  122. upnpd_write_bool igdv1 0 force_igd_desc_v1
  123. upnpd_write_bool use_stun 0 ext_perform_stun
  124. upnpd_write_bool ipv6_disable $ipv6_disable
  125. [ "$use_stun" -eq 0 ] || {
  126. [ -n "$stun_host" ] && echo "ext_stun_host=$stun_host"
  127. [ -n "$stun_port" ] && echo "ext_stun_port=$stun_port"
  128. }
  129. [ -n "$upload" ] && [ -n "$download" ] && {
  130. echo "bitrate_down=$((download * 1024 * 8))"
  131. echo "bitrate_up=$((upload * 1024 * 8))"
  132. }
  133. [ -n "$upnp_lease_file" ] && touch "$upnp_lease_file" && echo "lease_file=$upnp_lease_file"
  134. [ -n "$presentation_url" ] && echo "presentation_url=$presentation_url"
  135. [ -n "$notify_interval" ] && echo "notify_interval=$notify_interval"
  136. [ -n "$clean_ruleset_threshold" ] && echo "clean_ruleset_threshold=$clean_ruleset_threshold"
  137. [ -n "$clean_ruleset_interval" ] && echo "clean_ruleset_interval=$clean_ruleset_interval"
  138. [ -n "$serial_number" ] && echo "serial=$serial_number"
  139. [ -n "$model_number" ] && echo "model_number=$model_number"
  140. [ -n "$port" ] && echo "port=$port"
  141. [ -z "$uuid" ] && {
  142. uuid="$(cat /proc/sys/kernel/random/uuid)"
  143. uci set upnpd.config.uuid="$uuid"
  144. uci commit upnpd
  145. }
  146. [ "$uuid" = "nocli" ] || echo "uuid=$uuid"
  147. config_foreach conf_rule_add perm_rule
  148. if [ "$FW" = "fw4" ]; then
  149. #When using nftables configure miniupnpd to use its own table and chains
  150. echo "upnp_table_name=fw4"
  151. echo "upnp_nat_table_name=fw4"
  152. echo "upnp_forward_chain=upnp_forward"
  153. echo "upnp_nat_chain=upnp_prerouting"
  154. echo "upnp_nat_postrouting_chain=upnp_postrouting"
  155. fi
  156. } > "$tmpconf"
  157. fi
  158. if [ -n "$ifname" ]; then
  159. # start firewall
  160. if [ "$FW" = "fw4" ]; then
  161. nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
  162. else
  163. iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
  164. fi
  165. else
  166. logger -t "upnp daemon" "external interface not found, not starting"
  167. fi
  168. procd_open_instance
  169. procd_set_param file "$conf" "/etc/config/firewall"
  170. procd_set_param command "$PROG"
  171. procd_append_param command -f "$conf"
  172. [ "$log_output" = "1" ] && procd_append_param command -d
  173. procd_close_instance
  174. }
  175. stop_service() {
  176. if [ "$FW" = "fw3" ]; then
  177. iptables -t nat -F MINIUPNPD 2>/dev/null
  178. iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
  179. iptables -t filter -F MINIUPNPD 2>/dev/null
  180. [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
  181. else
  182. nft flush chain inet fw4 upnp_forward 2>/dev/null
  183. nft flush chain inet fw4 upnp_prerouting 2>/dev/null
  184. nft flush chain inet fw4 upnp_postrouting 2>/dev/null
  185. fi
  186. }
  187. start_service() {
  188. config_load "upnpd"
  189. config_foreach upnpd "upnpd"
  190. }
  191. service_triggers() {
  192. procd_add_reload_trigger "upnpd"
  193. }