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 |
William A. Kennington III | a7af2e0 | 2022-02-13 22:47:39 -0800 | [diff] [blame] | 52 | SetStatic "$service" "$interface" && \ |
| 53 | UpdateIP "$service" "$interface" "$ip" "$mask" && \ |
William A. Kennington III | 2d6858d | 2022-02-11 03:08:25 -0800 | [diff] [blame] | 54 | UpdateGateway "$service" "$interface" "$router" && \ |
William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 55 | UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \ |
| 56 | rc=$? |
| 57 | UnsuppressTerm |
| 58 | touch /run/dhcp4.done |
| 59 | return $rc |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | Main() { |
| 64 | set -o nounset |
| 65 | set -o errexit |
| 66 | set -o pipefail |
| 67 | |
| 68 | InitTerm |
| 69 | HandleDHCP4 "$@" |
| 70 | } |
| 71 | |
| 72 | return 0 2>/dev/null |
| 73 | Main "$@" |