Blame SOURCES/dhclient-script

df4638
#!/bin/bash
df4638
#
df4638
# dhclient-script: Network interface configuration script run by
df4638
#                  dhclient based on DHCP client communication
df4638
#
df4638
# Copyright (C) 2008-2014  Red Hat, Inc.
df4638
#
df4638
# This program is free software; you can redistribute it and/or modify
df4638
# it under the terms of the GNU General Public License as published by
df4638
# the Free Software Foundation; either version 2 of the License, or
df4638
# (at your option) any later version.
df4638
#
df4638
# This program is distributed in the hope that it will be useful,
df4638
# but WITHOUT ANY WARRANTY; without even the implied warranty of
df4638
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
df4638
# GNU General Public License for more details.
df4638
#
df4638
# You should have received a copy of the GNU General Public License
df4638
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
df4638
#
df4638
# Author(s): David Cantrell <dcantrell@redhat.com>
df4638
#            Jiri Popelka <jpopelka@redhat.com>
df4638
#
df4638
# ----------
df4638
# This script is a rewrite/reworking on dhclient-script originally
df4638
# included as part of dhcp-970306:
df4638
# dhclient-script for Linux. Dan Halbert, March, 1997.
df4638
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
df4638
# Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
df4638
# ----------
df4638
#
df4638
df4638
PATH=/bin:/usr/bin:/sbin
df4638
# scripts in dhclient.d/ use $SAVEDIR (#833054)
df4638
export SAVEDIR=/var/lib/dhclient
df4638
df4638
LOGFACILITY="local7"
df4638
LOGLEVEL="notice"
df4638
df4638
ETCDIR="/etc/dhcp"
df4638
df4638
RESOLVCONF="/etc/resolv.conf"
df4638
df4638
logmessage() {
df4638
    msg="${1}"
df4638
    logger -p "${LOGFACILITY}.${LOGLEVEL}" -t "NET" "dhclient: ${msg}"
df4638
}
df4638
df4638
eventually_add_hostnames_domain_to_search() {
df4638
# For the case when hostname for this machine has a domain that is not in domain_search list
df4638
# 1) get a hostname with `ipcalc --hostname` or `hostnamectl --transient`
df4638
# 2) get the domain from this hostname
df4638
# 3) add this domain to search line in resolv.conf if it's not already
df4638
#    there (domain list that we have recently added there is a parameter of this function)
df4638
# We can't do this directly when generating resolv.conf in make_resolv_conf(), because
df4638
# we need to first save the resolv.conf with obtained values before we can call `ipcalc --hostname`.
df4638
# See bug 637763
df4638
    search="${1}"
df4638
    if need_hostname; then
df4638
        status=1
df4638
        OLD_HOSTNAME=$HOSTNAME
df4638
        if [ -n "${new_ip_address}" ]; then
df4638
            eval $(/usr/bin/ipcalc --silent --hostname "${new_ip_address}" ; echo "status=$?")
df4638
        elif [ -n "${new_ip6_address}" ]; then
df4638
            eval $(/usr/bin/ipcalc --silent --hostname "${new_ip6_address}" ; echo "status=$?")
df4638
        fi
df4638
df4638
        if [ ${status} -eq 0 ]; then
df4638
            domain=$(echo "${HOSTNAME}" | cut -s -d "." -f 2-)
df4638
        fi
df4638
        HOSTNAME=$OLD_HOSTNAME
df4638
    else
df4638
          domain=$(hostnamectl --transient 2>/dev/null | cut -s -d "." -f 2-)
df4638
    fi
df4638
df4638
    if [ -n "${domain}" ] &&
df4638
       [ ! "${domain}" = "localdomain" ] &&
df4638
       [ ! "${domain}" = "localdomain6" ] &&
df4638
       [ ! "${domain}" = "(none)" ] &&
df4638
       [[ ! "${domain}" = *\ * ]]; then
df4638
       is_in="false"
df4638
       for s in ${search}; do
df4638
           if [ "${s}" = "${domain}" ] ||
df4638
              [ "${s}" = "${domain}." ]; then
df4638
              is_in="true"
df4638
           fi
df4638
       done
df4638
df4638
       if [ "${is_in}" = "false" ]; then
df4638
           # Add domain name to search list (#637763)
df4638
          sed -i -e "s/${search}/${search} ${domain}/" "${RESOLVCONF}"
df4638
       fi
df4638
    fi
df4638
}
df4638
df4638
make_resolv_conf() {
df4638
    [ "${PEERDNS}" = "no" ] && return
df4638
df4638
    if [ "${reason}" = "RENEW" ] &&
df4638
       [ "${new_domain_name}" = "${old_domain_name}" ] &&
df4638
       [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
df4638
        return
df4638
    fi
df4638
df4638
    if [ -n "${new_domain_name}" ] ||
df4638
       [ -n "${new_domain_name_servers}" ] ||
df4638
       [ -n "${new_domain_search}" ]; then
df4638
        rscf="$(mktemp "${TMPDIR:-/tmp}/XXXXXX")"
df4638
        [[ -z "${rscf}" ]] && return
df4638
        echo "; generated by /usr/sbin/dhclient-script" > "${rscf}"
df4638
df4638
        if [ -n "${SEARCH}" ]; then
df4638
            search="${SEARCH}"
df4638
        else
df4638
            if [ -n "${new_domain_search}" ]; then
df4638
                # Remove instaces of \032 (#450042)
df4638
                search="${new_domain_search//\\032/ }"
df4638
            elif [ -n "${new_domain_name}" ]; then
df4638
                # Note that the DHCP 'Domain Name Option' is really just a domain
df4638
                # name, and that this practice of using the domain name option as
df4638
                # a search path is both nonstandard and deprecated.
df4638
                search="${new_domain_name}"
df4638
            fi
df4638
        fi
df4638
df4638
        if [ -n "${search}" ]; then
df4638
            echo "search ${search}" >> "${rscf}"
df4638
        fi
df4638
df4638
        if [ -n "${RES_OPTIONS}" ]; then
df4638
            echo "options ${RES_OPTIONS}" >> "${rscf}"
df4638
        fi
df4638
df4638
        if [ -n "${new_domain_name_servers}" ]; then
df4638
            for nameserver in ${new_domain_name_servers} ; do
df4638
                echo "nameserver ${nameserver}" >> "${rscf}"
df4638
            done
df4638
        else # keep 'old' nameservers
df4638
            sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p "${RESOLVCONF}" >> "${rscf}"
df4638
        fi
df4638
df4638
        change_resolv_conf "${rscf}"
df4638
        rm -f "${rscf}"
df4638
df4638
        if [ -n "${search}" ]; then
df4638
            eventually_add_hostnames_domain_to_search "${search}"
df4638
        fi
df4638
    elif [ -n "${new_dhcp6_name_servers}" ] ||
df4638
         [ -n "${new_dhcp6_domain_search}" ]; then
df4638
        rscf="$(mktemp "${TMPDIR:-/tmp}/XXXXXX")"
df4638
        [[ -z "${rscf}" ]] && return
df4638
        echo "; generated by /usr/sbin/dhclient-script" > "${rscf}"
df4638
df4638
        if [ -n "${SEARCH}" ]; then
df4638
            search="${SEARCH}"
df4638
        else
df4638
            if [ -n "${new_dhcp6_domain_search}" ]; then
df4638
                search="${new_dhcp6_domain_search//\\032/ }"
df4638
            fi
df4638
        fi
df4638
df4638
        if [ -n "${search}" ]; then
df4638
            echo "search ${search}" >> "${rscf}"
df4638
        fi
df4638
df4638
        if [ -n "${RES_OPTIONS}" ]; then
df4638
            echo "options ${RES_OPTIONS}" >> "${rscf}"
df4638
        fi
df4638
df4638
        shopt -s nocasematch
df4638
        if [ -n "${new_dhcp6_name_servers}" ]; then
df4638
            for nameserver in ${new_dhcp6_name_servers} ; do
df4638
                # If the nameserver has a link-local address
df4638
                # add a <zone_id> (interface name) to it.
df4638
                if  [[ "$nameserver" =~ ^fe80:: ]]
df4638
                then
df4638
                    zone_id="%${interface}"
df4638
                else
df4638
                    zone_id=
df4638
                fi
df4638
                echo "nameserver ${nameserver}$zone_id" >> "${rscf}"
df4638
            done
df4638
        else # keep 'old' nameservers
df4638
            sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p "${RESOLVCONF}" >> "${rscf}"
df4638
        fi
df4638
        shopt -u nocasematch
df4638
df4638
        change_resolv_conf "${rscf}"
df4638
        rm -f "${rscf}"
df4638
df4638
        if [ -n "${search}" ]; then
df4638
            eventually_add_hostnames_domain_to_search "${search}"
df4638
        fi
df4638
    fi
df4638
}
df4638
df4638
# run given script
df4638
run_hook() {
df4638
    local script
df4638
    local exit_status
df4638
    script="${1}"
df4638
df4638
    if [ -f ${script} ]; then
df4638
        . ${script}
df4638
    fi
df4638
df4638
    if [ -n "${exit_status}" ] && [ "${exit_status}" -ne 0 ]; then
df4638
        logmessage "${script} returned non-zero exit status ${exit_status}"
df4638
    fi
df4638
df4638
    return ${exit_status}
df4638
}
df4638
df4638
# run scripts in given directory
df4638
run_hookdir() {
df4638
    local dir
df4638
    dir="${1}"
df4638
df4638
    if [ -d "${dir}" ]; then
df4638
        for script in $(find $dir -executable ! -empty); do
df4638
            run_hook ${script} || return $?
df4638
        done
df4638
    fi
df4638
df4638
    return 0
df4638
}
df4638
df4638
exit_with_hooks() {
df4638
    # Source the documented exit-hook script, if it exists
df4638
    run_hook "${ETCDIR}/dhclient-exit-hooks" || exit $?
df4638
    # Now run scripts in the hooks directory.
df4638
    run_hookdir "${ETCDIR}/dhclient-exit-hooks.d" || exit $?
df4638
df4638
    exit ${1}
df4638
}
df4638
df4638
quad2num() {
df4638
    if [ $# -eq 4 ]; then
df4638
        let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
df4638
        echo "${n}"
df4638
        return 0
df4638
    else
df4638
        echo "0"
df4638
        return 1
df4638
    fi
df4638
}
df4638
df4638
ip2num() {
df4638
    IFS='.' quad2num ${1}
df4638
}
df4638
df4638
num2ip() {
df4638
    let n="${1}"
df4638
    let o1="(${n} >> 24) & 0xff"
df4638
    let o2="(${n} >> 16) & 0xff"
df4638
    let o3="(${n} >> 8) & 0xff"
df4638
    let o4="${n} & 0xff"
df4638
    echo "${o1}.${o2}.${o3}.${o4}"
df4638
}
df4638
df4638
get_network_address() {
df4638
# get network address for the given IP address and (netmask or prefix)
df4638
    ip="${1}"
df4638
    nm="${2}"
df4638
df4638
    if [ -n "${ip}" -a -n "${nm}" ]; then
df4638
        if [[ "${nm}" = *.* ]]; then
df4638
            ipcalc -s -n "${ip}" "${nm}" | cut -d '=' -f 2
df4638
        else
df4638
            ipcalc -s -n "${ip}/${nm}" | cut -d '=' -f 2
df4638
        fi
df4638
    fi
df4638
}
df4638
df4638
get_prefix() {
df4638
# get prefix for the given IP address and mask
df4638
    ip="${1}"
df4638
    nm="${2}"
df4638
df4638
    if [ -n "${ip}" -a -n "${nm}" ]; then
df4638
        ipcalc -s -p "${ip}" "${nm}" | cut -d '=' -f 2
df4638
    fi
df4638
}
df4638
df4638
class_bits() {
df4638
    let ip=$(IFS='.' ip2num "${1}")
df4638
    let bits=32
df4638
    let mask='255'
df4638
    for ((i=0; i <= 3; i++, 'mask<<=8')); do
df4638
        let v='ip&mask'
df4638
        if [ "$v" -eq 0 ] ; then
df4638
             let bits-=8
df4638
        else
df4638
             break
df4638
        fi
df4638
    done
df4638
    echo $bits
df4638
}
df4638
df4638
is_router_reachable() {
df4638
    # handle DHCP servers that give us a router not on our subnet
df4638
    router="${1}"
df4638
    routersubnet="$(get_network_address "${router}" "${new_subnet_mask}")"
df4638
    mysubnet="$(get_network_address "${new_ip_address}" "${new_subnet_mask}")"
df4638
df4638
    if [ ! "${routersubnet}" = "${mysubnet}" ]; then
df4638
        # TODO: This function should not have side effects such as adding or
df4638
        # removing routes. Can this be done with "ip route get" or similar
df4638
        # instead? Are there cases that rely on this route being created here?
df4638
        ip -4 route replace "${router}/32" dev "${interface}"
df4638
        if [ "$?" -ne 0 ]; then
df4638
            logmessage "failed to create host route for ${router}"
df4638
            return 1
df4638
        fi
df4638
    fi
df4638
df4638
    return 0
df4638
}
df4638
df4638
add_default_gateway() {
df4638
    router="${1}"
df4638
df4638
    if is_router_reachable "${router}" ; then
df4638
        if [ $# -gt 1 ] && [ -n "${2}" ] && [[ "${2}" -gt 0 ]]; then
df4638
            ip -4 route replace default via "${router}" dev "${interface}" metric "${2}"
df4638
        else
df4638
            ip -4 route replace default via "${router}" dev "${interface}"
df4638
        fi
df4638
        if [ $? -ne 0 ]; then
df4638
            logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
df4638
            return 1
df4638
        else
df4638
            return 0
df4638
        fi
df4638
    fi
df4638
df4638
    return 1
df4638
}
df4638
df4638
execute_client_side_configuration_scripts() {
df4638
# execute any additional client side configuration scripts we have
df4638
    if [ "${1}" == "config" ] || [ "${1}" == "restore" ]; then
df4638
        for f in ${ETCDIR}/dhclient.d/*.sh ; do
df4638
            if [ -x "${f}" ]; then
df4638
                subsystem="${f%.sh}"
df4638
                subsystem="${subsystem##*/}"
df4638
                . "${f}"
df4638
                "${subsystem}_${1}"
df4638
            fi
df4638
        done
df4638
    fi
df4638
}
df4638
df4638
flush_dev() {
df4638
# Instead of bringing the interface down (#574568)
df4638
# explicitly clear ARP cache and flush all addresses & routes.
df4638
    ip -4 addr flush dev "${1}" >/dev/null 2>&1
df4638
    ip -4 route flush dev "${1}" >/dev/null 2>&1
df4638
    ip -4 neigh flush dev "${1}" >/dev/null 2>&1
df4638
}
df4638
df4638
remove_old_addr() {
df4638
    if [ -n "${old_ip_address}" ]; then
df4638
        if [ -n "${old_prefix}" ]; then
df4638
            ip -4 addr del "${old_ip_address}/${old_prefix}" dev "${interface}" >/dev/null 2>&1
df4638
        else
df4638
            ip -4 addr del "${old_ip_address}" dev "${interface}" >/dev/null 2>&1
df4638
        fi
df4638
    fi
df4638
}
df4638
df4638
dhconfig() {
df4638
    if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
df4638
       [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
df4638
        # possible new alias, remove old alias first
df4638
        ip -4 addr del "${old_ip_address}" dev "${interface}" label "${interface}:0"
df4638
    fi
df4638
df4638
    if [ -n "${old_ip_address}" ] &&
df4638
       [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
df4638
        # IP address changed. Delete all routes, and clear the ARP cache.
df4638
        flush_dev "${interface}"
df4638
    fi
df4638
df4638
    # make sure the interface is up
df4638
    ip link set dev "${interface}" up
df4638
df4638
    # replace = add if it doesn't exist or override (update lifetimes) if it's there
df4638
    ip -4 addr replace "${new_ip_address}/${new_prefix}" broadcast "${new_broadcast_address}" dev "${interface}" \
df4638
       valid_lft "${new_dhcp_lease_time}" preferred_lft "${new_dhcp_lease_time}" >/dev/null 2>&1
df4638
df4638
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
df4638
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
df4638
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
df4638
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
df4638
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
df4638
       [ ! "${old_routers}" = "${new_routers}" ] ||
df4638
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
df4638
df4638
        # The 576 MTU is only used for X.25 and dialup connections
df4638
        # where the admin wants low latency.  Such a low MTU can cause
df4638
        # problems with UDP traffic, among other things.  As such,
df4638
        # disallow MTUs from 576 and below by default, so that broken
df4638
        # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
df4638
        if [ -n "${new_interface_mtu}" ] && [ "${new_interface_mtu}" -gt 576 ]; then
df4638
            ip link set dev "${interface}" mtu "${new_interface_mtu}"
df4638
        fi
df4638
df4638
        # static routes
df4638
        if [ -n "${new_classless_static_routes}" ] ||
df4638
           [ -n "${new_static_routes}" ]; then
df4638
            if [ -n "${new_classless_static_routes}" ]; then
df4638
                IFS=', |' static_routes=(${new_classless_static_routes})
df4638
                # If the DHCP server returns both a Classless Static Routes option and
df4638
                # a Router option, the DHCP client MUST ignore the Router option. (RFC3442)
df4638
                new_routers=""
df4638
            else
df4638
                IFS=', |' static_routes=(${new_static_routes})
df4638
            fi
df4638
            route_targets=()
df4638
df4638
            for((i=0; i<${#static_routes[@]}; i+=2)); do
df4638
                target=${static_routes[$i]}
df4638
                if [ -n "${new_classless_static_routes}" ]; then
df4638
                    if [ "${target}" = "0" ]; then
df4638
                        new_routers="${static_routes[$i+1]}"
df4638
                        continue
df4638
                    else
df4638
                        prefix=${target%%.*}
df4638
                        target=${target#*.}
df4638
                        IFS="." target_arr=(${target})
df4638
                        unset IFS
df4638
                        ((pads=4-${#target_arr[@]}))
df4638
                        for j in $(seq $pads); do
df4638
                            target="${target}.0"
df4638
                        done
df4638
df4638
                        # Client MUST zero any bits in the subnet number where the corresponding bit in the mask is zero.
df4638
                        # In other words, the subnet number installed in the routing table is the logical AND of
df4638
                        # the subnet number and subnet mask given in the Classless Static Routes option. (RFC3442)
df4638
                        target="$(get_network_address "${target}" "${prefix}")"
df4638
                    fi
df4638
                else
df4638
                    prefix=$(class_bits "${target}")
df4638
                fi
df4638
                gateway=${static_routes[$i+1]}
df4638
df4638
                # special case 0.0.0.0 to allow static routing for link-local addresses
df4638
                # (including IPv4 multicast) which will not have a next-hop (#769463, #787318)
df4638
                if [ "${gateway}" = "0.0.0.0" ]; then
df4638
                    valid_gateway=0
df4638
                    scope='scope link'
df4638
                else
df4638
                    is_router_reachable "${gateway}"
df4638
                    valid_gateway=$?
df4638
                    scope=''
df4638
                fi
df4638
                if [ "${valid_gateway}" -eq 0 ]; then
df4638
                    metric=''
df4638
                    for t in "${route_targets[@]}"; do
df4638
                        if [ "${t}" = "${target}" ]; then
df4638
                            if [ -z "${metric}" ]; then
df4638
                                metric=1
df4638
                            else
df4638
                                ((metric=metric+1))
df4638
                            fi
df4638
                        fi
df4638
                    done
df4638
df4638
                    if [ -n "${metric}" ]; then
df4638
                        metric="metric ${metric}"
df4638
                    fi
df4638
df4638
                    ip -4 route replace "${target}/${prefix}" proto static via "${gateway}" dev "${interface}" ${metric} ${scope}
df4638
df4638
                    if [ $? -ne 0 ]; then
df4638
                        logmessage "failed to create static route: ${target}/${prefix} via ${gateway} dev ${interface} ${metric}"
df4638
                    else
df4638
                        route_targets=(${route_targets[@]} ${target})
df4638
                    fi
df4638
                fi
df4638
            done
df4638
        fi
df4638
df4638
        # gateways
df4638
        if [[ ( "${DEFROUTE}" != "no" ) &&
df4638
              (( -z "${GATEWAYDEV}" ) || ( "${GATEWAYDEV}" = "${interface}" )) ]]; then
df4638
            if [[ ( -z "${GATEWAY}" ) ||
df4638
                  (( -n "${DHCLIENT_IGNORE_GATEWAY}" ) && ( "${DHCLIENT_IGNORE_GATEWAY}" = [Yy]* )) ]]; then
df4638
                metric="${METRIC:-}"
df4638
                let i="${METRIC:-0}"
df4638
                default_routers=()
df4638
df4638
                for router in ${new_routers} ; do
df4638
                    added_router=-
df4638
df4638
                    for r in "${default_routers[@]}" ; do
df4638
                        if [ "${r}" = "${router}" ]; then
df4638
                            added_router=1
df4638
                        fi
df4638
                    done
df4638
df4638
                    if [ -z "${router}" ] ||
df4638
                       [ "${added_router}" = "1" ] ||
df4638
                       [ "$(IFS='.' ip2num ${router})" -le 0 ] ||
df4638
                       [[ ( "${router}" = "${new_broadcast_address}" ) &&
df4638
                          ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
df4638
                        continue
df4638
                    fi
df4638
df4638
                    default_routers=(${default_routers[@]} ${router})
df4638
                    add_default_gateway "${router}" "${metric}"
df4638
                    let i=i+1
df4638
                    metric=${i}
df4638
                done
df4638
            elif [ -n "${GATEWAY}" ]; then
df4638
                routersubnet=$(get_network_address "${GATEWAY}" "${new_subnet_mask}")
df4638
                mysubnet=$(get_network_address "${new_ip_address}" "${new_subnet_mask}")
df4638
df4638
                if [ "${routersubnet}" = "${mysubnet}" ]; then
df4638
                    ip -4 route replace default via "${GATEWAY}" dev "${interface}"
df4638
                fi
df4638
            fi
df4638
        fi
df4638
    fi
df4638
df4638
    if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
df4638
       [ -n "${alias_ip_address}" ]; then
df4638
        # Reset the alias address (fix: this should really only do this on changes)
df4638
        ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
df4638
        ip -4 addr replace "${alias_ip_address}/${alias_prefix}" broadcast "${alias_broadcast_address}" dev "${interface}" label "${interface}:0"
df4638
        ip -4 route replace "${alias_ip_address}/32" dev "${interface}"
df4638
    fi
df4638
df4638
    # After dhclient brings an interface UP with a new IP address, subnet mask, 
df4638
    # and routes, in the REBOOT/BOUND states -> search for "dhclient-up-hooks".
df4638
    if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
df4638
       [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
df4638
       [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
df4638
       [ ! "${old_network_number}" = "${new_network_number}" ] ||
df4638
       [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
df4638
       [ ! "${old_routers}" = "${new_routers}" ] ||
df4638
       [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
df4638
df4638
        if [ -x "${ETCDIR}/dhclient-${interface}-up-hooks" ]; then
df4638
            . "${ETCDIR}/dhclient-${interface}-up-hooks"
df4638
        elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
df4638
            . ${ETCDIR}/dhclient-up-hooks
df4638
        fi
df4638
    fi
df4638
df4638
    make_resolv_conf
df4638
df4638
    if [ -n "${new_host_name}" ] && need_hostname; then
df4638
        hostnamectl set-hostname --transient --no-ask-password "${new_host_name}"
df4638
    fi
df4638
df4638
    if [[ ( "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ) &&
df4638
          ( -n "${new_time_offset}" ) ]]; then
df4638
        # DHCP option "time-offset" is requested by default and should be
df4638
        # handled.  The geographical zone abbreviation cannot be determined
df4638
        # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
df4638
        # used - note: this disables DST.
df4638
        ((z=new_time_offset/3600))
df4638
        ((hoursWest=$(printf '%+d' $z)))
df4638
df4638
        if (( $hoursWest < 0 )); then
df4638
            # tzdata treats negative 'hours west' as positive 'gmtoff'!
df4638
            ((hoursWest*=-1))
df4638
        fi
df4638
df4638
        tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
df4638
        if [ -e "${tzfile}" ]; then
df4638
            cp -fp "${tzfile}" /etc/localtime
df4638
            touch /etc/localtime
df4638
        fi
df4638
    fi
df4638
df4638
    execute_client_side_configuration_scripts "config"
df4638
}
df4638
df4638
wait_for_link_local() {
df4638
    # we need a link-local address to be ready (not tentative)
df4638
    for i in $(seq 50); do
df4638
        linklocal=$(ip -6 addr show dev "${interface}" scope link)
df4638
        # tentative flag means DAD is still not complete
df4638
        tentative=$(echo "${linklocal}" | grep tentative)
df4638
        [[ -n "${linklocal}" && -z "${tentative}" ]] && exit_with_hooks 0
df4638
        sleep 0.1
df4638
    done
df4638
}
df4638
df4638
# Section 18.1.8. (Receipt of Reply Messages) of RFC 3315 says:
df4638
# The client SHOULD perform duplicate address detection on each of
df4638
# the addresses in any IAs it receives in the Reply message before
df4638
# using that address for traffic.
df4638
add_ipv6_addr_with_DAD() {
df4638
            ip -6 addr replace "${new_ip6_address}/${new_ip6_prefixlen}" \
df4638
                dev "${interface}" scope global valid_lft "${new_max_life}" \
df4638
                                          preferred_lft "${new_preferred_life}"
df4638
df4638
            # repeatedly test whether newly added address passed
df4638
            # duplicate address detection (DAD)
df4638
            for i in $(seq 5); do
df4638
                sleep 1 # give the DAD some time
df4638
df4638
                addr=$(ip -6 addr show dev "${interface}" \
df4638
                       | grep "${new_ip6_address}/${new_ip6_prefixlen}")
df4638
df4638
                # tentative flag == DAD is still not complete
df4638
                tentative=$(echo "${addr}" | grep tentative)
df4638
                # dadfailed flag == address is already in use somewhere else
df4638
                dadfailed=$(echo "${addr}" | grep dadfailed)
df4638
df4638
                if [ -n "${dadfailed}" ] ; then
df4638
                    # address was added with valid_lft/preferred_lft 'forever', remove it
df4638
                    ip -6 addr del "${new_ip6_address}/${new_ip6_prefixlen}" dev "${interface}"
df4638
                    exit_with_hooks 3
df4638
                fi
df4638
                if [ -z "${tentative}" ] ; then
df4638
                    if [ -n "${addr}" ]; then
df4638
                        # DAD is over
df4638
                        return 0
df4638
                    else
df4638
                        # address was auto-removed (or not added at all)
df4638
                        exit_with_hooks 3
df4638
                    fi
df4638
                fi
df4638
            done
df4638
            return 0
df4638
}
df4638
df4638
dh6config() {
df4638
    if [ -n "${old_ip6_prefix}" ] ||
df4638
       [ -n "${new_ip6_prefix}" ]; then
df4638
        echo "Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}"
df4638
        exit_with_hooks 0
df4638
    fi
df4638
df4638
    case "${reason}" in
df4638
        BOUND6)
df4638
            if [ -z "${new_ip6_address}" ] ||
df4638
               [ -z "${new_ip6_prefixlen}" ]; then
df4638
                exit_with_hooks 2
df4638
            fi
df4638
df4638
            add_ipv6_addr_with_DAD
df4638
df4638
            make_resolv_conf
df4638
            ;;
df4638
df4638
        RENEW6|REBIND6)
df4638
            if [[ -n "${new_ip6_address}" ]] &&
df4638
               [[ -n "${new_ip6_prefixlen}" ]]; then
df4638
               if [[  ! "${new_ip6_address}" = "${old_ip6_address}" ]]; then
df4638
                   [[ -n "${old_ip6_address}" ]] && ip -6 addr del "${old_ip6_address}" dev "${interface}"
df4638
               fi
df4638
               # call it even if new_ip6_address = old_ip6_address to update lifetimes
df4638
               add_ipv6_addr_with_DAD
df4638
            fi
df4638
df4638
            if [ ! "${new_dhcp6_name_servers}" = "${old_dhcp6_name_servers}" ] ||
df4638
               [ ! "${new_dhcp6_domain_search}" = "${old_dhcp6_domain_search}" ]; then
df4638
                make_resolv_conf
df4638
            fi
df4638
            ;;
df4638
df4638
        DEPREF6)
df4638
            if [ -z "${new_ip6_prefixlen}" ]; then
df4638
                exit_with_hooks 2
df4638
            fi
df4638
df4638
            ip -6 addr change "${new_ip6_address}/${new_ip6_prefixlen}" \
df4638
                dev "${interface}" scope global preferred_lft 0
df4638
            ;;
df4638
    esac
df4638
df4638
    execute_client_side_configuration_scripts "config"
df4638
}
df4638
df4638
# Functions from /etc/sysconfig/network-scripts/network-functions
df4638
df4638
need_hostname ()
df4638
{
df4638
    CHECK_HOSTNAME=$(hostnamectl --transient)
df4638
    if [[ "${CHECK_HOSTNAME}" = "(none)" ]] ||
df4638
       [[ "${CHECK_HOSTNAME}" = "localhost" ]] ||
df4638
       [[ "${CHECK_HOSTNAME}" = "localhost.localdomain" ]]; then
df4638
        return 0
df4638
    else
df4638
        return 1
df4638
    fi
df4638
}
df4638
df4638
# Takes one argument - temporary resolv.conf file
df4638
change_resolv_conf ()
df4638
{
df4638
    options=$(grep '^[\ \	]*option' "${RESOLVCONF}" 2>/dev/null);
df4638
    if [[ -n "${options}" ]]; then
df4638
       # merge options from existing resolv.conf with specified resolv.conf content
df4638
       newres="${options}"$'\n'$(grep -vF "${options}" "${1}");
df4638
    else
df4638
       newres=$(cat "${1}");
df4638
    fi;
df4638
df4638
    eval $(echo "${newres}" > "${RESOLVCONF}"; echo "status=$?")
df4638
    if [[ $status -eq 0 ]]; then
df4638
        logger -p local7.notice -t "NET" -i "${0} : updated ${RESOLVCONF}";
df4638
        [[ -e /var/run/nscd/socket ]] && /usr/sbin/nscd -i hosts; # invalidate cache
df4638
    fi;
df4638
    return $status;
df4638
}
df4638
df4638
get_config_by_name ()
df4638
{
df4638
    LANG=C grep -E -i -l \
df4638
        "^[[:space:]]*NAME=\"(Auto |System )?${1}\"" \
df4638
        /etc/sysconfig/network-scripts/ifcfg-* \
df4638
        | LC_ALL=C sed -e "$__sed_discard_ignored_files"
df4638
}
df4638
df4638
get_hwaddr ()
df4638
{
df4638
    if [ -f /sys/class/net/${1}/address ]; then
df4638
        awk '{ print toupper($0) }' < /sys/class/net/${1}/address
df4638
    elif [ -d "/sys/class/net/${1}" ]; then
df4638
        LC_ALL= LANG= ip -o link show ${1} 2>/dev/null | \
df4638
            awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
df4638
                                        "\\1", 1)); }'
df4638
    fi
df4638
}
df4638
df4638
validate_resolv_conf()
df4638
{
df4638
    # It's possible to have broken symbolic link $RESOLVCONF -> <some_nm_dir>
df4638
    # https://bugzilla.redhat.com/1475279
df4638
    # Remove broken link and hope NM will survive
df4638
    if [ -h "${RESOLVCONF}" -a ! -e "${RESOLVCONF}" ];
df4638
    then
df4638
        logmessage "${RESOLVCONF} is broken symlink. Recreating..."
df4638
        unlink "${RESOLVCONF}"
df4638
        touch "${RESOLVCONF}"
df4638
    fi;
df4638
}
df4638
df4638
df4638
get_config_by_hwaddr ()
df4638
{
df4638
    LANG=C grep -il "^[[:space:]]*HWADDR=\"\?${1}\"\?\([[:space:]#]\|$\)" /etc/sysconfig/network-scripts/ifcfg-* \
df4638
      | LC_ALL=C sed -e "$__sed_discard_ignored_files"
df4638
}
df4638
df4638
get_config_by_device ()
df4638
{
df4638
    LANG=C grep -l "^[[:space:]]*DEVICE=\"\?${1}\"\?\([[:space:]#]\|$\)" \
df4638
        /etc/sysconfig/network-scripts/ifcfg-* \
df4638
        | LC_ALL=C sed -e "$__sed_discard_ignored_files"
df4638
}
df4638
df4638
need_config ()
df4638
{
df4638
    # A sed expression to filter out the files that is_ignored_file recognizes
df4638
    __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
df4638
df4638
    local nconfig
df4638
df4638
    CONFIG="ifcfg-${1}"
df4638
    [ -f "${CONFIG}" ] && return
df4638
    CONFIG="${1##*/}"
df4638
    [ -f "${CONFIG}" ] && return
df4638
    nconfig=$(get_config_by_name "${1}")
df4638
    if [ -n "$nconfig" ] && [ -f "$nconfig" ]; then
df4638
      CONFIG=${nconfig##*/}
df4638
      return
df4638
    fi
df4638
    local addr=$(get_hwaddr ${1})
df4638
    if [ -n "$addr" ]; then
df4638
      nconfig=$(get_config_by_hwaddr ${addr})
df4638
      if [ -n "$nconfig" ] ; then
df4638
        CONFIG=${nconfig##*/}
df4638
        [ -f "${CONFIG}" ] && return
df4638
      fi
df4638
    fi
df4638
    nconfig=$(get_config_by_device ${1})
df4638
    if [ -n "$nconfig" ] && [ -f "$nconfig" ]; then
df4638
      CONFIG=${nconfig##*/}
df4638
      return
df4638
    fi
df4638
}
df4638
df4638
# We need this because of PEERDNS
df4638
source_config ()
df4638
{
df4638
    CONFIG=${CONFIG##*/}
df4638
    . /etc/sysconfig/network-scripts/$CONFIG
df4638
}
df4638
df4638
#
df4638
# ### MAIN
df4638
#
df4638
df4638
# Invoke the local dhcp client enter hooks, if they exist.
df4638
run_hook "${ETCDIR}/dhclient-enter-hooks" || exit $?
df4638
run_hookdir "${ETCDIR}/dhclient-enter-hooks.d" || exit $?
df4638
df4638
[ "${PEERDNS}" = "no" ] || validate_resolv_conf
df4638
df4638
if [ -f /etc/sysconfig/network ]; then
df4638
    . /etc/sysconfig/network
df4638
fi
df4638
df4638
if [ -f /etc/sysconfig/networking/network ]; then
df4638
    . /etc/sysconfig/networking/network
df4638
fi
df4638
df4638
## it's possible initscripts package is not installed
df4638
## for example in container. Don't flood stderr then
df4638
if [ -d /etc/sysconfig/network-scripts ]; then
df4638
        cd /etc/sysconfig/network-scripts
df4638
        CONFIG="${interface}"
df4638
        need_config "${CONFIG}"
df4638
        source_config >/dev/null 2>&1
df4638
fi;
df4638
df4638
# In case there's some delay in rebinding, it might happen, that the valid_lft drops to 0,
df4638
# address is removed by kernel and then re-added few seconds later by dhclient-script.
df4638
# With this work-around the address lives a minute longer.
df4638
# "4294967235" = infinite (forever) - 60
df4638
[[ "${new_dhcp_lease_time}" -lt "4294967235" ]] && new_dhcp_lease_time=$((new_dhcp_lease_time + 60))
df4638
[[ "${new_max_life}" -lt "4294967235" ]] && new_max_life=$((new_max_life + 60))
df4638
df4638
new_prefix="$(get_prefix "${new_ip_address}" "${new_subnet_mask}")"
df4638
old_prefix="$(get_prefix "${old_ip_address}" "${old_subnet_mask}")"
df4638
alias_prefix="$(get_prefix "${alias_ip_address}" "${alias_subnet_mask}")"
df4638
df4638
case "${reason}" in
df4638
    MEDIUM|ARPCHECK|ARPSEND)
df4638
        # Do nothing
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    PREINIT)
df4638
        if [ -n "${alias_ip_address}" ]; then
df4638
            # Flush alias, its routes will disappear too.
df4638
            ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
df4638
        fi
df4638
df4638
        # upstream dhclient-script removes (ifconfig $interface 0 up) old adresses in PREINIT,
df4638
        # but we sometimes (#125298) need (for iSCSI/nfs root to have a dhcp interface) to keep the existing ip
df4638
        # flush_dev ${interface}
df4638
        ip link set dev "${interface}" up
df4638
        if [ -n "${DHCLIENT_DELAY}" ] && [ "${DHCLIENT_DELAY}" -gt 0 ]; then
df4638
            # We need to give the kernel some time to get the interface up.
df4638
            sleep "${DHCLIENT_DELAY}"
df4638
        fi
df4638
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    PREINIT6)
df4638
        # ensure interface is up
df4638
        ip link set dev "${interface}" up
df4638
df4638
        # Removing stale addresses from aborted clients shouldn't be needed
df4638
        # since we've been adding addresses with lifetimes.
df4638
        # Which means that kernel eventually removes them automatically.
df4638
        # ip -6 addr flush dev "${interface}" scope global permanent
df4638
df4638
        wait_for_link_local
df4638
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    BOUND|RENEW|REBIND|REBOOT)
df4638
        if [ -z "${interface}" ] || [ -z "${new_ip_address}" ]; then
df4638
            exit_with_hooks 2
df4638
        fi
df4638
        if arping -D -q -c2 -I "${interface}" "${new_ip_address}"; then
df4638
            dhconfig
df4638
            exit_with_hooks 0
df4638
        else  # DAD failed, i.e. address is already in use
df4638
            ARP_REPLY=$(arping -D -c2 -I "${interface}" "${new_ip_address}" | grep reply | awk '{print toupper($5)}' | cut -d "[" -f2 | cut -d "]" -f1)
df4638
            OUR_MACS=$(ip link show | grep link | awk '{print toupper($2)}' | uniq)
df4638
            if [[ "${OUR_MACS}" = *"${ARP_REPLY}"* ]]; then
df4638
                # the reply can come from our system, that's OK (#1116004#c33)
df4638
                dhconfig
df4638
                exit_with_hooks 0
df4638
            else
df4638
                exit_with_hooks 1
df4638
            fi
df4638
        fi
df4638
        ;;
df4638
df4638
    BOUND6|RENEW6|REBIND6|DEPREF6)
df4638
        dh6config
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    EXPIRE6|RELEASE6|STOP6)
df4638
        if [ -z "${old_ip6_address}" ] || [ -z "${old_ip6_prefixlen}" ]; then
df4638
            exit_with_hooks 2
df4638
        fi
df4638
df4638
        ip -6 addr del "${old_ip6_address}/${old_ip6_prefixlen}" \
df4638
            dev "${interface}"
df4638
df4638
        execute_client_side_configuration_scripts "restore"
df4638
df4638
        if [ -x "${ETCDIR}/dhclient-${interface}-down-hooks" ]; then
df4638
            . "${ETCDIR}/dhclient-${interface}-down-hooks"
df4638
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
df4638
            . ${ETCDIR}/dhclient-down-hooks
df4638
        fi
df4638
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    EXPIRE|FAIL|RELEASE|STOP)
df4638
        execute_client_side_configuration_scripts "restore"
df4638
df4638
        if [ -x "${ETCDIR}/dhclient-${interface}-down-hooks" ]; then
df4638
            . "${ETCDIR}/dhclient-${interface}-down-hooks"
df4638
        elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
df4638
            . ${ETCDIR}/dhclient-down-hooks
df4638
        fi
df4638
df4638
        if [ -n "${alias_ip_address}" ]; then
df4638
            # Flush alias
df4638
            ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
df4638
        fi
df4638
df4638
        # upstream script sets interface down here,
df4638
        # we only remove old ip address
df4638
        #flush_dev ${interface}
df4638
        remove_old_addr
df4638
df4638
        if [ -n "${alias_ip_address}" ]; then
df4638
            ip -4 addr replace "${alias_ip_address}/${alias_prefix}" broadcast "${alias_broadcast_address}" dev "${interface}" label "${interface}:0"
df4638
            ip -4 route replace "${alias_ip_address}/32" dev "${interface}"
df4638
        fi
df4638
df4638
        exit_with_hooks 0
df4638
        ;;
df4638
df4638
    TIMEOUT)
df4638
        if [ -n "${new_routers}" ]; then
df4638
            if [ -n "${alias_ip_address}" ]; then
df4638
                ip -4 addr flush dev "${interface}" label "${interface}:0" >/dev/null 2>&1
df4638
            fi
df4638
df4638
            ip -4 addr replace "${new_ip_address}/${new_prefix}" \
df4638
                broadcast "${new_broadcast_address}" dev "${interface}" \
df4638
                valid_lft "${new_dhcp_lease_time}" preferred_lft "${new_dhcp_lease_time}"
df4638
            set ${new_routers}
df4638
df4638
            if ping -q -c 1 -w 10 -I "${interface}" "${1}"; then
df4638
                dhconfig
df4638
                exit_with_hooks 0
df4638
            fi
df4638
df4638
            #flush_dev ${interface}
df4638
            remove_old_addr
df4638
            exit_with_hooks 1
df4638
        else
df4638
            exit_with_hooks 1
df4638
        fi
df4638
        ;;
df4638
df4638
    *)
df4638
        logmessage "unhandled state: ${reason}"
df4638
        exit_with_hooks 1
df4638
        ;;
df4638
esac
df4638
df4638
exit_with_hooks 0