| #!/bin/bash |
| source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh |
| |
| DetermineRouterMac() { |
| # Attempt to find the neighbor once, in case our configuration is already |
| # valid. Errors are silenced to make the logs more clear. The next call |
| # will print any real errors. |
| if DetermineNeighbor4 "$interface" "$router" 2>/dev/null; then |
| return 0 |
| fi |
| |
| # arping might not have a valid source address, so we need to assign |
| # the given address so arping has a source to write into the request |
| # packet. We don't want a persistent configuration yet so we modify |
| # the kernel directly. |
| if ! ip -4 addr flush dev "$interface"; then |
| echo "Failed to flush $interface" >&2 |
| return 1 |
| fi |
| if ! ip addr add "$ip/$mask" dev "$interface"; then |
| echo "Failed to assign $ip/$mask to $interface" >&2 |
| # Don't return, because we need to reset networkd |
| fi |
| |
| local rc=0 |
| DetermineNeighbor4 "$interface" "$router" || rc=$? |
| |
| # We need to ensure that our old network configuration gets |
| # restored, in case our early flushing breaks things. |
| systemctl restart systemd-networkd || return $? |
| return $rc |
| } |
| |
| HandleDHCP4() { |
| local op="$1" |
| |
| if [ "$op" = "bound" ]; then |
| echo "INTF: $interface" >&2 |
| echo "IP: $ip/$mask" >&2 |
| echo "GW: $router" >&2 |
| |
| local router_mac |
| if ! router_mac="$(DetermineRouterMac "$interface" "$router")"; then |
| echo "Failed to acquire gateway mac for $router" >&2 |
| return 1 |
| fi |
| echo "GW_MAC: $router_mac" >&2 |
| |
| SuppressTerm |
| local service='xyz.openbmc_project.Network' |
| local rc=0 |
| UpdateIP "$service" "$interface" "$ip" "$mask" && \ |
| UpdateGateway "$service" "$router" && \ |
| UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \ |
| rc=$? |
| UnsuppressTerm |
| touch /run/dhcp4.done |
| return $rc |
| fi |
| } |
| |
| Main() { |
| set -o nounset |
| set -o errexit |
| set -o pipefail |
| |
| InitTerm |
| HandleDHCP4 "$@" |
| } |
| |
| return 0 2>/dev/null |
| Main "$@" |