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.

210 lines
6.1 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. upnpd_get_port_range() {
  8. local var="$1"; shift
  9. local val
  10. config_get val "$@"
  11. case "$val" in
  12. [0-9]*[:-][0-9]*)
  13. export -n -- "${var}_start=${val%%[:-]*}"
  14. export -n -- "${var}_end=${val##*[:-]}"
  15. ;;
  16. [0-9]*)
  17. export -n -- "${var}_start=$val"
  18. export -n -- "${var}_end="
  19. ;;
  20. esac
  21. }
  22. conf_rule_add() {
  23. local cfg="$1"
  24. local action int_addr
  25. local ext_start ext_end int_start int_end comment
  26. config_get action "$cfg" action "deny" # allow or deny
  27. upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
  28. config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
  29. upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
  30. config_get comment "$cfg" comment "ACL" # comment
  31. # Make a single IP IP/32 so that miniupnpd.conf can use it.
  32. [ "${int_addr%/*}" = "$int_addr" ] && int_addr="$int_addr/32"
  33. echo "$action $ext_start${ext_end:+-}$ext_end $int_addr $int_start${int_end:+-}$int_end #$comment"
  34. }
  35. upnpd_write_bool() {
  36. local opt="$1"
  37. local def="${2:-0}"
  38. local alt="${3:-$opt}"
  39. local val
  40. config_get_bool val config "$opt" "$def"
  41. if [ "$val" -eq 0 ]; then
  42. echo "$alt=no"
  43. else
  44. echo "$alt=yes"
  45. fi
  46. }
  47. upnpd() {
  48. config_load "upnpd"
  49. local external_iface external_iface6 external_zone external_ip internal_iface
  50. local upload download log_output port config_file serial_number model_number
  51. local use_stun stun_host stun_port uuid notify_interval presentation_url
  52. local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
  53. local ipv6_disable
  54. local ext_ip_reserved_ignore
  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. config_get ext_ip_reserved_ignore config ext_ip_reserved_ignore 0
  81. local conf ifname ifname6
  82. . /lib/functions/network.sh
  83. if [ -n "$external_iface" ] ; then
  84. network_get_device ifname "$external_iface"
  85. else
  86. if [ -n "$external_zone" ] ; then
  87. ifname=$(fw3 -q zone "$external_zone" 2>/dev/null | head -1)
  88. else
  89. network_find_wan external_iface && \
  90. network_get_device ifname "$external_iface"
  91. fi
  92. fi
  93. if [ -n "$external_iface6" ] ; then
  94. network_get_device ifname6 "$external_iface6"
  95. else
  96. if [ -n "$external_zone" ] ; then
  97. ifname6=$(fw3 -q zone "$external_zone" 2>/dev/null | head -1)
  98. else
  99. network_find_wan6 external_iface6 && \
  100. network_get_device ifname6 "$external_iface6"
  101. fi
  102. fi
  103. if [ -n "$config_file" ]; then
  104. conf="$config_file"
  105. else
  106. local tmpconf="/var/etc/miniupnpd.conf"
  107. conf="$tmpconf"
  108. mkdir -p /var/etc
  109. {
  110. echo "ext_ifname=$ifname"
  111. echo "ext_ifname6=$ifname6"
  112. [ -n "$external_ip" ] && echo "ext_ip=$external_ip"
  113. local iface
  114. for iface in ${internal_iface:-lan}; do
  115. local device
  116. network_get_device device "$iface" && echo "listening_ip=$device"
  117. network_get_device device "$iface" && echo "ipv6_listening_ip=$device"
  118. done
  119. config_load "upnpd"
  120. upnpd_write_bool enable_natpmp 1
  121. upnpd_write_bool enable_upnp 1
  122. upnpd_write_bool secure_mode 1
  123. upnpd_write_bool system_uptime 1
  124. upnpd_write_bool igdv1 0 force_igd_desc_v1
  125. upnpd_write_bool use_stun 0 ext_perform_stun
  126. upnpd_write_bool ipv6_disable $ipv6_disable
  127. upnpd_write_bool ext_ip_reserved_ignore $ext_ip_reserved_ignore
  128. [ "$use_stun" -eq 0 ] || {
  129. [ -n "$stun_host" ] && echo "ext_stun_host=$stun_host"
  130. [ -n "$stun_port" ] && echo "ext_stun_port=$stun_port"
  131. }
  132. [ -n "$upload" ] && [ -n "$download" ] && {
  133. echo "bitrate_down=$((download * 1024 * 8))"
  134. echo "bitrate_up=$((upload * 1024 * 8))"
  135. }
  136. [ -n "$upnp_lease_file" ] && touch "$upnp_lease_file" && echo "lease_file=$upnp_lease_file"
  137. [ -n "$presentation_url" ] && echo "presentation_url=$presentation_url"
  138. [ -n "$notify_interval" ] && echo "notify_interval=$notify_interval"
  139. [ -n "$clean_ruleset_threshold" ] && echo "clean_ruleset_threshold=$clean_ruleset_threshold"
  140. [ -n "$clean_ruleset_interval" ] && echo "clean_ruleset_interval=$clean_ruleset_interval"
  141. [ -n "$serial_number" ] && echo "serial=$serial_number"
  142. [ -n "$model_number" ] && echo "model_number=$model_number"
  143. [ -n "$port" ] && echo "port=$port"
  144. [ -z "$uuid" ] && {
  145. uuid="$(cat /proc/sys/kernel/random/uuid)"
  146. uci set upnpd.config.uuid="$uuid"
  147. uci commit upnpd
  148. }
  149. [ "$uuid" = "nocli" ] || echo "uuid=$uuid"
  150. config_foreach conf_rule_add perm_rule
  151. } > "$tmpconf"
  152. fi
  153. if [ -n "$ifname" ]; then
  154. # start firewall
  155. iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
  156. else
  157. logger -t "upnp daemon" "external interface not found, not starting"
  158. fi
  159. procd_open_instance
  160. procd_set_param command "$PROG"
  161. procd_append_param command -f "$conf"
  162. [ "$log_output" = "1" ] && procd_append_param command -d
  163. procd_close_instance
  164. }
  165. stop_service() {
  166. iptables -t nat -F MINIUPNPD 2>/dev/null
  167. iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
  168. iptables -t filter -F MINIUPNPD 2>/dev/null
  169. [ -x /usr/sbin/ip6tables ] && ip6tables -t filter -F MINIUPNPD 2>/dev/null
  170. }
  171. start_service() {
  172. config_load "upnpd"
  173. config_foreach upnpd "upnpd"
  174. }
  175. service_triggers() {
  176. procd_add_reload_trigger "upnpd"
  177. }