blob: bb6fa6ade171137b878bf34c1861713471cbd682 [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001#!/bin/bash
2source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
3
4DetermineRouterMac() {
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
34HandleDHCP4() {
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 IIIa7af2e02022-02-13 22:47:39 -080052 SetStatic "$service" "$interface" && \
53 UpdateIP "$service" "$interface" "$ip" "$mask" && \
William A. Kennington III2d6858d2022-02-11 03:08:25 -080054 UpdateGateway "$service" "$interface" "$router" && \
William A. Kennington III7d6fa422021-02-08 17:04:02 -080055 UpdateNeighbor "$service" "$interface" "$router" "$router_mac" || \
56 rc=$?
57 UnsuppressTerm
58 touch /run/dhcp4.done
59 return $rc
60 fi
61}
62
63Main() {
64 set -o nounset
65 set -o errexit
66 set -o pipefail
67
68 InitTerm
69 HandleDHCP4 "$@"
70}
71
72return 0 2>/dev/null
73Main "$@"