2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 NETWORK_DHCP_DUID_FILE
="${NETWORK_CONFIG_DIR}/dhcp-duid"
28 assert isset interface
29 assert device_exists
${interface}
31 local service
=$
(dhclient_proto2service
${proto} ${interface})
32 service_start
${service}
39 local service
=$
(dhclient_proto2service
${proto} ${interface})
40 service_stop
${service}
47 local service
=$
(dhclient_proto2service
${proto} ${interface})
48 service_status
${service}
51 dhclient_proto2service
() {
56 assert isset interface
62 service
="dhclient4@${interface}.service"
65 service
="dhclient6@${interface}.service"
78 dhclient_write_config
() {
79 local interface
="${1}"
82 assert isset interface
90 while [ $# -gt 0 ]; do
93 config_file
="$(cli_get_val "${1}")"
96 hostname
="$(cli_get_val "${1}")"
99 leases_file
="$(cli_get_val "${1}")"
102 vendor
="$(cli_get_val "${1}")"
105 log WARNING $
"Unknown configuration option passed: ${1}."
111 assert isset config_file
114 if ! isset hostname
; then
115 hostname
="$(config_hostname)"
118 if ! isset vendor
; then
119 vendor
="$(distro_get_pretty_name)"
122 # Clear configuration file (if any).
123 mkdir
-p $
(dirname ${config_file}) 2>/dev
/null
126 config_header
"dhclient daemon configuration file for ${interface}" \
130 fappend
"${config_file}" "send vendor-class-identifier \"${vendor}\";"
131 fappend
"${config_file}" # empty line
135 echo "interface \"${interface}\" {"
137 if isset hostname
; then
138 echo " send host-name \"${hostname}\";"
143 ) >> "${config_file}"
146 if isset leases_file
; then
147 __dhclient_update_leases_file
"${leases_file}" ||
return $?
153 dhclient_get_duid
() {
154 # If the DUID already exists, we do not do anything at all.
155 if [ -s "${NETWORK_DHCP_DUID_FILE}" ]; then
156 print
"$(<${NETWORK_DHCP_DUID_FILE})"
160 # If no DUID exists, we will need to create a new one
161 local duid
="$(dhclient_generate_duid)"
162 log DEBUG
"Created new DHCP DUID: ${duid}"
164 # Save the new DUID to file and return it
165 print
"${duid}" > "${NETWORK_DHCP_DUID_FILE}"
172 dhclient_generate_duid
() {
173 # Find lowest MAC/link-local address
174 local address
="$(ports_lowest_address)"
176 # Use a random MAC address if no physical address could
178 if ! isset address
; then
179 log WARNING
"Could not determine the lowest MAC/link-local address"
180 address
="$(mac_generate)"
183 log DEBUG
"Using '${address}' to generate the DHCP DUID"
185 print
"00030001${address//\:/}"
189 __dhclient_update_leases_file
() {
192 local duid
="$(dhclient_get_duid)"
194 if [ -e "${leases_file}" ]; then
195 local old_duid
="$(__dhclient_get_duid_from_leases_file "${leases_file}")"
197 if [ "${duid}" = "${old_duid}" ]; then
198 log DEBUG
"DUID in leases file matches. Nothing to do"
203 # If the leases file does not exist, yet, or the
204 # DUID in there is different, we will create/overwrite
205 # the leases file with the correct DUID.
208 printf "default-duid \""
211 while [ ${i} -lt ${#duid} ]; do
213 printf "x${duid:${i}:2}"
223 __dhclient_get_duid_from_leases_file
() {
226 # Do nothing if the leases file cannot be read
227 [ -r "${file}" ] ||
return ${EXIT_OK}
231 if [[ ${line} =~ ^default-duid
]]; then
232 line
="${line/default-duid/}"
233 line
="${line//\\\\x/}"
236 line
="$(strip "${line}")"