William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh |
| 3 | |
| 4 | DetermineRouterMac() { |
| 5 | # Attempt to find the neighbor once, in case our configuration is already |
| 6 | # valid. Errors are silenced to make the logs more clear. The next call |
| 7 | # will print any real errors. |
| 8 | if DetermineNeighbor4 "$interface" "$router" 2>/dev/null; then |
| 9 | return 0 |
| 10 | fi |
| 11 | |
| 12 | # arping might not have a valid source address, so we need to assign |
| 13 | # the given address so arping has a source to write into the request |
| 14 | # packet. We don't want a persistent configuration yet so we modify |
| 15 | # the kernel directly. |
| 16 | if ! ip -4 addr flush dev "$interface"; then |
| 17 | echo "Failed to flush $interface" >&2 |
| 18 | return 1 |
| 19 | fi |
| 20 | if ! ip addr add "$ip/$mask" dev "$interface"; then |
| 21 | echo "Failed to assign $ip/$mask to $interface" >&2 |
| 22 | # Don't return, because we need to reset networkd |
| 23 | fi |
| 24 | |
| 25 | local rc=0 |
| 26 | DetermineNeighbor4 "$interface" "$router" || rc=$? |
| 27 | |
| 28 | # We need to ensure that our old network configuration gets |
| 29 | # restored, in case our early flushing breaks things. |
| 30 | systemctl restart systemd-networkd || return $? |
| 31 | return $rc |
| 32 | } |
| 33 | |
| 34 | HandleDHCP4() { |
| 35 | local op="$1" |
| 36 | |
| 37 | if [ "$op" = "bound" ]; then |
| 38 | echo "INTF: $interface" >&2 |
| 39 | echo "IP: $ip/$mask" >&2 |
| 40 | echo "GW: $router" >&2 |
| 41 | |
| 42 | local router_mac |
| 43 | if ! router_mac="$(DetermineRouterMac "$interface" "$router")"; then |
| 44 | echo "Failed to acquire gateway mac for $router" >&2 |
| 45 | return 1 |
| 46 | fi |
| 47 | echo "GW_MAC: $router_mac" >&2 |
| 48 | |
| 49 | SuppressTerm |
| 50 | local service='xyz.openbmc_project.Network' |
| 51 | local rc=0 |
| 52 | UpdateIP "$service" "$interface" "$ip" "$mask" && \ |
| 53 | UpdateGateway "$service" "$router" && \ |
| 54 | UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \ |
| 55 | rc=$? |
| 56 | UnsuppressTerm |
| 57 | touch /run/dhcp4.done |
| 58 | return $rc |
| 59 | fi |
| 60 | } |
| 61 | |
| 62 | Main() { |
| 63 | set -o nounset |
| 64 | set -o errexit |
| 65 | set -o pipefail |
| 66 | |
| 67 | InitTerm |
| 68 | HandleDHCP4 "$@" |
| 69 | } |
| 70 | |
| 71 | return 0 2>/dev/null |
| 72 | Main "$@" |