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
5.7 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2014 OpenWrt.org
  3. START=94
  4. STOP=15
  5. SERVICE_USE_PID=1
  6. upnpd_get_port_range() {
  7. local var="$1"; shift
  8. local val
  9. config_get val "$@"
  10. case "$val" in
  11. [0-9]*[:-][0-9]*)
  12. export -n -- "${var}_start=${val%%[:-]*}"
  13. export -n -- "${var}_end=${val##*[:-]}"
  14. ;;
  15. [0-9]*)
  16. export -n -- "${var}_start=$val"
  17. export -n -- "${var}_end="
  18. ;;
  19. esac
  20. }
  21. conf_rule_add() {
  22. local cfg="$1"
  23. local tmpconf="$2"
  24. local action external_port_start external_port_end int_addr
  25. local internal_port_start internal_port_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" >>$tmpconf
  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" >> $tmpconf
  43. else
  44. echo "$alt=yes" >> $tmpconf
  45. fi
  46. }
  47. boot() {
  48. return
  49. }
  50. start() {
  51. config_load "upnpd"
  52. local extiface intiface upload download logging secure enabled natpmp
  53. local extip port usesysuptime conffile serial_number model_number
  54. local uuid notify_interval presentation_url enable_upnp
  55. local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
  56. local ipv6_listening_ip enabled
  57. config_get_bool enabled config enabled 1
  58. [ "$enabled" -eq 0 ] && return 1
  59. config_get extiface config external_iface
  60. config_get extzone config external_zone
  61. config_get intiface config internal_iface
  62. config_get extip config external_ip
  63. config_get port config port 5000
  64. config_get upload config upload
  65. config_get download config download
  66. config_get_bool logging config log_output 0
  67. config_get conffile 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 notify_interval config notify_interval
  72. config_get presentation_url config presentation_url
  73. config_get upnp_lease_file config upnp_lease_file
  74. config_get clean_ruleset_threshold config clean_ruleset_threshold
  75. config_get clean_ruleset_interval config clean_ruleset_interval
  76. config_get ipv6_listening_ip config ipv6_listening_ip
  77. local args ifname
  78. . /lib/functions/network.sh
  79. # manual external interface overrides everything
  80. if [ -z "$extiface" ] ; then
  81. # manual external zone (if dynamically find interfaces
  82. # belonging to it) overrides network_find_wan*
  83. if [ -n "$extzone" ] ; then
  84. ifname=$(fw3 -q zone $extzone | head -1)
  85. fi
  86. [ -n "$extiface" ] || network_find_wan extiface
  87. [ -n "$extiface" ] || network_find_wan6 extiface
  88. fi
  89. [ -n "$ifname" ] || network_get_device ifname $extiface
  90. if [ -n "$conffile" ]; then
  91. args="-f $conffile"
  92. else
  93. local tmpconf="/var/etc/miniupnpd.conf"
  94. args="-f $tmpconf"
  95. mkdir -p /var/etc
  96. echo "ext_ifname=$ifname" >$tmpconf
  97. [ -n "$extip" ] && \
  98. echo "ext_ip=$extip" >>$tmpconf
  99. local iface
  100. for iface in ${intiface:-lan}; do
  101. local device
  102. network_get_device device "$iface" && {
  103. echo "listening_ip=$device" >>$tmpconf
  104. }
  105. done
  106. [ "$port" != "auto" ] && \
  107. echo "port=$port" >>$tmpconf
  108. config_load "upnpd"
  109. upnpd_write_bool enable_natpmp 1
  110. upnpd_write_bool enable_upnp 1
  111. upnpd_write_bool secure_mode 1
  112. upnpd_write_bool pcp_allow_thirdparty 0
  113. upnpd_write_bool system_uptime 1
  114. upnpd_write_bool igdv1 0 force_igd_desc_v1
  115. [ -n "$upnp_lease_file" ] && \
  116. echo "lease_file=$upnp_lease_file" >>$tmpconf
  117. [ -n "$upload" -a -n "$download" ] && {
  118. echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
  119. echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
  120. }
  121. [ -n "${presentation_url}" ] && \
  122. echo "presentation_url=${presentation_url}" >>$tmpconf
  123. [ -n "${notify_interval}" ] && \
  124. echo "notify_interval=${notify_interval}" >>$tmpconf
  125. [ -n "${clean_ruleset_threshold}" ] && \
  126. echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
  127. [ -n "${clean_ruleset_interval}" ] && \
  128. echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
  129. [ -n "${ipv6_listening_ip}" ] && \
  130. echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
  131. [ -z "$uuid" ] && {
  132. uuid="$(cat /proc/sys/kernel/random/uuid)"
  133. uci set upnpd.config.uuid=$uuid
  134. uci commit upnpd
  135. }
  136. [ "$uuid" = "nocli" ] || \
  137. echo "uuid=$uuid" >>$tmpconf
  138. [ -n "${serial_number}" ] && \
  139. echo "serial=${serial_number}" >>$tmpconf
  140. [ -n "${model_number}" ] && \
  141. echo "model_number=${model_number}" >>$tmpconf
  142. config_foreach conf_rule_add perm_rule "$tmpconf"
  143. fi
  144. if [ -n "$ifname" ]; then
  145. # start firewall
  146. iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
  147. if [ "$logging" = "1" ]; then
  148. SERVICE_DAEMONIZE=1 \
  149. service_start /usr/sbin/miniupnpd $args -d
  150. else
  151. SERVICE_DAEMONIZE= \
  152. service_start /usr/sbin/miniupnpd $args
  153. fi
  154. else
  155. logger -t "upnp daemon" "external interface not found, not starting"
  156. fi
  157. }
  158. stop() {
  159. service_stop /usr/sbin/miniupnpd
  160. iptables -t nat -F MINIUPNPD 2>/dev/null
  161. iptables -t nat -F MINIUPNPD-POSTROUTING 2>/dev/null
  162. iptables -t filter -F MINIUPNPD 2>/dev/null
  163. [ -x /usr/sbin/ip6tables ] && {
  164. ip6tables -t filter -F MINIUPNPD 2>/dev/null
  165. }
  166. }