blob: fbf90f1597410d131be85f1689fa82b7ad4d7224 [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001#!/bin/bash
2source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
3
Patrick Williams59486672022-12-08 06:23:47 -06004function 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
William A. Kennington III7d6fa422021-02-08 17:04:02 -080010 fi
William A. Kennington III7d6fa422021-02-08 17:04:02 -080011
Patrick Williams59486672022-12-08 06:23:47 -060012 # 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
William A. Kennington III7d6fa422021-02-08 17:04:02 -080025 local rc=0
Patrick Williams59486672022-12-08 06:23:47 -060026 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 $?
William A. Kennington III7d6fa422021-02-08 17:04:02 -080031 return $rc
William A. Kennington III7d6fa422021-02-08 17:04:02 -080032}
33
Patrick Williams59486672022-12-08 06:23:47 -060034function HandleDHCP4() {
35 local op="$1"
William A. Kennington III7d6fa422021-02-08 17:04:02 -080036
Patrick Williams59486672022-12-08 06:23:47 -060037 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 SetStatic "$service" "$interface" && \
53 UpdateIP "$service" "$interface" "$ip" "$mask" && \
54 UpdateGateway "$service" "$interface" "$router" && \
55 UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \
56 rc=$?
57 UnsuppressTerm
58 touch /run/dhcp4.done
59 return $rc
60 fi
61}
62
63function Main() {
64 set -o nounset
65 set -o errexit
66 set -o pipefail
67
68 InitTerm
69 HandleDHCP4 "$@"
William A. Kennington III7d6fa422021-02-08 17:04:02 -080070}
71
72return 0 2>/dev/null
73Main "$@"