meta-google: gbmc-bridge: Discover ULA addresses at runtime

If the bridge interface is assigned a MAC at runtime then it needs to
add the appropriate ULA address for that MAC.

Change-Id: Ia109c36320a78bb02ba9b54038ca23b0d3e2c948
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/networking/gbmc-bridge.bb b/meta-google/recipes-google/networking/gbmc-bridge.bb
index 1358ac5..202522a 100644
--- a/meta-google/recipes-google/networking/gbmc-bridge.bb
+++ b/meta-google/recipes-google/networking/gbmc-bridge.bb
@@ -14,16 +14,20 @@
   file://+-bmc-gbmcbrusb.network \
   file://ipmi.service.in \
   file://50-gbmc-br.rules \
+  file://gbmc-br-ula.sh \
   "
 
 FILES_${PN}_append = " \
+  ${datadir}/gbmc-ip-monitor \
   ${systemd_unitdir}/network \
   ${sysconfdir}/nftables \
   ${sysconfdir}/avahi/services \
   "
 
 RDEPENDS_${PN}_append = " \
+  gbmc-ip-monitor \
   mstpd-mstpd \
+  network-sh \
   "
 
 GBMC_BR_MAC_ADDR ?= ""
@@ -72,4 +76,8 @@
   sed -i 's,@EXTRA_ATTRS@,,g' ${WORKDIR}/ipmi.service.in
   sed 's,@NAME@,bmc,g' ${WORKDIR}/ipmi.service.in >${avahi_dir}/bmc.ipmi.service
   sed 's,@NAME@,${MACHINE}-bmc,g' ${WORKDIR}/ipmi.service.in >${avahi_dir}/${MACHINE}-bmc.ipmi.service
+
+  mondir=${D}${datadir}/gbmc-ip-monitor
+  install -d -m0755 "$mondir"
+  install -m0644 ${WORKDIR}/gbmc-br-ula.sh "$mondir"/
 }
diff --git a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-ula.sh b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-ula.sh
new file mode 100644
index 0000000..ac273a3
--- /dev/null
+++ b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-ula.sh
@@ -0,0 +1,67 @@
+# Copyright 2021 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[ -z "${gbmc_br_ula_lib-}" ] || return
+
+source /usr/share/network/lib.sh || exit
+
+gbmc_br_ula_init=
+gbmc_br_ula_mac=
+
+gbmc_br_ula_update() {
+  [ -n "$gbmc_br_ula_init" ] || return
+
+  echo "gBMC Bridge ULA MAC: ${gbmc_br_ula_mac:-(deleted)}" >&2
+
+  local addr=
+  contents='[Network]'$'\n'
+  if [ -n "$gbmc_br_ula_mac" ]; then
+    local eui64
+    eui64="$(mac_to_eui64 "$mac")" || return
+    addr="fdb5:0481:10ce:0:$eui64/64"
+    contents+="Address=$addr"$'\n'
+  fi
+
+  local netfile
+  for netfile in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/60-ula.conf; do
+    mkdir -p -m 755 "$(dirname "$netfile")"
+    printf '%s' "$contents" >"$netfile"
+  done
+
+  # We have to add the address after writing the systemd config to ensure we
+  # don't race with reconfiguration and drop the address.
+  if [ -n "$addr" ]; then
+    ip addr replace "$addr" dev gbmcbr
+  fi
+}
+
+gbmc_br_ula_hook() {
+  if [ "$change" = 'init' ]; then
+    gbmc_br_ula_init=1
+    gbmc_br_ula_update
+  elif [ "$change" = 'link' -a "$intf" = 'gbmcbr' ]; then
+    if [ "$action" = 'add' -a "$mac" != "$gbmc_br_ula_mac" ]; then
+      gbmc_br_ula_mac="$mac"
+      gbmc_br_ula_update
+    fi
+    if [ "$action" = 'del' -a "$mac" = "$gbmc_br_ula_mac" ]; then
+      gbmc_br_ula_mac=
+      gbmc_br_ula_update
+    fi
+  fi
+}
+
+GBMC_IP_MONITOR_HOOKS+=(gbmc_br_ula_hook)
+
+gbmc_br_ula_lib=1