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.

266 lines
5.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=19
  3. USE_PROCD=1
  4. PROG=/usr/sbin/dhcpd
  5. lease_file=/tmp/dhcpd.leases
  6. config_file=/tmp/run/dhcpd.conf
  7. time2seconds() {
  8. local timestring=$1
  9. local multiplier number suffix
  10. suffix="${timestring//[0-9 ]}"
  11. number="${timestring%%$suffix}"
  12. [ "$number$suffix" != "$timestring" ] && return 1
  13. case "$suffix" in
  14. "" | s)
  15. multiplier=1
  16. ;;
  17. m)
  18. multiplier=60
  19. ;;
  20. h)
  21. multiplier=3600
  22. ;;
  23. d)
  24. multiplier=86400
  25. ;;
  26. w)
  27. multiplier=604800
  28. ;;
  29. *)
  30. return 1
  31. ;;
  32. esac
  33. echo $(( number * multiplier ))
  34. }
  35. # duplicated from dnsmasq init script
  36. hex_to_hostid() {
  37. local var="$1"
  38. local hex="${2#0x}" # strip optional "0x" prefix
  39. if [ -n "${hex//[0-9a-fA-F]/}" ]; then
  40. # is invalid hex literal
  41. return 1
  42. fi
  43. # convert into host id
  44. export "$var=$(
  45. printf "%0x:%0x" \
  46. $(((0x$hex >> 16) % 65536)) \
  47. $(( 0x$hex % 65536))
  48. )"
  49. return 0
  50. }
  51. static_host_add() {
  52. local cfg="$1"
  53. local broadcast hostid macn macs mac name ip leasetime
  54. config_get macs "$cfg" "mac"
  55. [ -n "$macs" ] || return 0
  56. config_get name "$cfg" "name"
  57. [ -n "$name" ] || return 0
  58. config_get ip "$cfg" "ip"
  59. [ -n "$ip" ] || return 0
  60. config_get_bool broadcast "$cfg" "broadcast" 0
  61. config_get dns "$cfg" "dns"
  62. config_get gateway "$cfg" "gateway"
  63. config_get leasetime "$cfg" "leasetime"
  64. if [ -n "$leasetime" ] ; then
  65. leasetime="$(time2seconds "$leasetime")"
  66. [ "$?" -ne 0 ] && return 1
  67. fi
  68. config_get hostid "$cfg" "hostid"
  69. if [ -n "$hostid" ] ; then
  70. hex_to_hostid hostid "$hostid" || return 1
  71. fi
  72. macn=0
  73. for mac in $macs; do
  74. macn=$(( macn + 1 ))
  75. done
  76. for mac in $macs; do
  77. local secname="$name"
  78. if [ $macn -gt 1 ] ; then
  79. secname="${name}-${mac//:}"
  80. fi
  81. echo "host $secname {"
  82. echo " hardware ethernet $mac;"
  83. echo " fixed-address $ip;"
  84. echo " option host-name \"$name\";"
  85. if [ "$broadcast" -eq 1 ] ; then
  86. echo " always-broadcast true;"
  87. fi
  88. if [ -n "$leasetime" ] ; then
  89. echo " default-lease-time $leasetime;"
  90. echo " max-lease-time $leasetime;"
  91. fi
  92. if [ -n "$hostid" ] ; then
  93. echo " option dhcp-client-identifier $hostid;"
  94. fi
  95. if [ -n "$dns" ] ; then
  96. echo " option domain-name-servers $dns;"
  97. fi
  98. if [ -n "$gateway" ] ; then
  99. echo " option routers $gateway;"
  100. fi
  101. echo "}"
  102. done
  103. }
  104. static_hosts() {
  105. config_foreach static_host_add host "$@"
  106. }
  107. gen_dhcp_subnet() {
  108. echo "subnet $NETWORK netmask $NETMASK {"
  109. echo " range $START $END;"
  110. echo " option subnet-mask $netmask;"
  111. if [ "$BROADCAST" != "0.0.0.0" ] ; then
  112. echo " option broadcast-address $BROADCAST;"
  113. fi
  114. if [ "$dynamicdhcp" -eq 0 ] ; then
  115. if [ "$authoritative" -eq 1 ] ; then
  116. echo " deny unknown-clients;"
  117. else
  118. echo " ignore unknown-clients;"
  119. fi
  120. fi
  121. if [ -n "$leasetime" ] ; then
  122. echo " default-lease-time $leasetime;"
  123. echo " max-lease-time $leasetime;"
  124. fi
  125. echo " option routers $gateway;"
  126. echo " option domain-name-servers $DNS;"
  127. echo "}"
  128. }
  129. dhcpd_add() {
  130. local cfg="$1"
  131. local dhcp6range="::"
  132. local dynamicdhcp end gateway ifname ignore leasetime limit net netmask
  133. local proto networkid start subnet
  134. local IP NETMASK BROADCAST NETWORK PREFIX DNS START END
  135. config_get_bool ignore "$cfg" "ignore" 0
  136. [ "$ignore" = "0" ] || return 0
  137. config_get net "$cfg" "interface"
  138. [ -n "$net" ] || return 0
  139. config_get start "$cfg" "start"
  140. [ -n "$start" ] || return 0
  141. config_get limit "$cfg" "limit"
  142. [ -n "$limit" ] || return 0
  143. network_get_subnet subnet "$net" || return 0
  144. network_get_device ifname "$net" || return 0
  145. network_get_protocol proto "$net" || return 0
  146. [ static = "$proto" ] || return 0
  147. config_get_bool dynamicdhcp "$cfg" "dynamicdhcp" 1
  148. dhcp_ifs="$dhcp_ifs $ifname"
  149. eval "$(ipcalc.sh $subnet $start $limit)"
  150. config_get netmask "$cfg" "netmask" "$NETMASK"
  151. config_get leasetime "$cfg" "leasetime"
  152. if [ -n "$leasetime" ] ; then
  153. leasetime="$(time2seconds "$leasetime")"
  154. [ "$?" -ne 0 ] && return 1
  155. fi
  156. if network_get_dnsserver dnsserver "$net" ; then
  157. for dnsserv in $dnsserver ; do
  158. DNS="$DNS${DNS:+, }$dnsserv"
  159. done
  160. else
  161. DNS="$IP"
  162. fi
  163. if ! network_get_gateway gateway "$net" ; then
  164. gateway="$IP"
  165. fi
  166. gen_dhcp_subnet >> $config_file
  167. }
  168. general_config() {
  169. local always_broadcast boot_unknown_clients log_facility
  170. local default_lease_time max_lease_time
  171. config_get_bool always_broadcast "isc_dhcpd" "always_broadcast" 0
  172. config_get_bool authoritative "isc_dhcpd" "authoritative" 1
  173. config_get_bool boot_unknown_clients "isc_dhcpd" "boot_unknown_clients" 1
  174. config_get default_lease_time "isc_dhcpd" "default_lease_time" 3600
  175. config_get max_lease_time "isc_dhcpd" "max_lease_time" 86400
  176. config_get log_facility "isc_dhcpd" "log_facility"
  177. [ $always_broadcast -eq 1 ] && echo "always-broadcast true;"
  178. [ $authoritative -eq 1 ] && echo "authoritative;"
  179. [ $boot_unknown_clients -eq 0 ] && echo "boot-unknown-clients false;"
  180. default_lease_time="$(time2seconds "$default_lease_time")"
  181. [ "$?" -ne 0 ] && return 1
  182. max_lease_time="$(time2seconds "$max_lease_time")"
  183. [ "$?" -ne 0 ] && return 1
  184. if [ -n "$log_facility" ] ; then
  185. echo "log-facility $log_facility;"
  186. fi
  187. echo "default-lease-time $default_lease_time;"
  188. echo "max-lease-time $max_lease_time;"
  189. }
  190. start_service() {
  191. if [ -n "$DHCPD_BOOT" ] ; then
  192. return 0
  193. fi
  194. if [ ! -e $lease_file ] ; then
  195. touch $lease_file
  196. fi
  197. dhcp_ifs=""
  198. if [ -e "/etc/dhcpd.conf" ] ; then
  199. config_file="/etc/dhcpd.conf"
  200. else
  201. . /lib/functions/network.sh
  202. config_load dhcp
  203. local authoritative
  204. general_config > $config_file
  205. config_foreach dhcpd_add dhcp
  206. static_hosts >> $config_file
  207. [ -z "$dhcp_ifs" ] && return 0
  208. fi
  209. procd_open_instance
  210. procd_set_param command $PROG -q -f -cf $config_file -lf $lease_file $dhcp_ifs
  211. procd_close_instance
  212. }
  213. boot() {
  214. DHCPD_BOOT=1
  215. start "$@"
  216. }
  217. service_triggers()
  218. {
  219. procd_add_reload_trigger "dhcp"
  220. procd_add_raw_trigger "interface.*" 3000 /etc/init.d/dhcpd reload
  221. }