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