meta-google: gbmc-ncsi-config: Derive IP from RA

Our end2end DHCP solution is not yet working, but we need a way to
derive addresses for a BMC from the smart NIC in front of it. This
provides a mechanism for detecting the address from RA beacons the NIC
is sending.

Change-Id: I0cdc8c192974c0b00257ebe58e911e62636e4c81
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/ncsi/files/50-gbmc-ncsi.rules.in b/meta-google/recipes-google/ncsi/files/50-gbmc-ncsi.rules.in
index f712720..e2ade6e 100644
--- a/meta-google/recipes-google/ncsi/files/50-gbmc-ncsi.rules.in
+++ b/meta-google/recipes-google/ncsi/files/50-gbmc-ncsi.rules.in
@@ -8,6 +8,7 @@
     }
     chain ncsi_gbmc_br_pub_input {
         jump gbmc_br_pub_input
+        jump ncsi_legacy_input
         reject
     }
     chain gbmc_br_pub_input {
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.service.in b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.service.in
new file mode 100644
index 0000000..82972ff
--- /dev/null
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.service.in
@@ -0,0 +1,15 @@
+[Unit]
+Description=gBMC NCSI RA Discovery
+After=network.target
+StartLimitIntervalSec=10
+StartLimitBurst=3
+Conflicts=nic-hostless@@NCSI_IF@.target
+Conflicts=nic-hostful@@NCSI_IF@.target
+
+[Service]
+Restart=always
+RestartSec=5
+ExecStart=/usr/libexec/gbmc-ncsi-ip-from-ra.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.sh.in b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.sh.in
new file mode 100755
index 0000000..e4ee10d
--- /dev/null
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ip-from-ra.sh.in
@@ -0,0 +1,113 @@
+#!/bin/bash
+# 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.
+
+source /usr/share/network/lib.sh || exit
+source /usr/libexec/ncsid_lib.sh || exit
+
+old_pfx=
+old_rtr=
+
+w=60
+while true; do
+  start=$SECONDS
+  while read line; do
+    if [ -z "$line" ]; then
+      pfx=
+    elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
+      t_pfx="${BASH_REMATCH[1]}"
+      t_pfx_len="${BASH_REMATCH[2]}"
+      ip_to_bytes t_pfx_b "$t_pfx" || continue
+      (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
+      (( t_pfx_b[9] |= 1 ))
+      pfx="$(ip_bytes_to_str t_pfx_b)"
+      (( t_pfx_b[9] &= 0xf0 ))
+      stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
+    elif [[ "$line" =~ ^from' '(.*)$ ]]; then
+      rtr="${BASH_REMATCH[1]}"
+      (( "${#pfx}" != 0 )) || continue
+      [[ "$pfx" != "$old_pfx" || "$old_rtr" != "$rtr" ]] || continue
+
+      echo "Found prefix $pfx from $rtr" >&2
+
+      # Delete any stale IP Addresses from the primary interface as we won't use them
+      UpdateIP xyz.openbmc_project.Network @NCSI_IF@ '0.0.0.0' '0'
+      UpdateIP xyz.openbmc_project.Network @NCSI_IF@ '::' '0'
+
+    read -r -d '' contents <<EOF
+[Network]
+Address=$pfx/128
+IPv6PrefixDelegation=yes
+[IPv6PrefixDelegation]
+RouterLifetimeSec=60
+[IPv6Prefix]
+Prefix=$stateless_pfx/80
+PreferredLifetimeSec=60
+ValidLifetimeSec=60
+[IPv6RoutePrefix]
+Route=$pfx/80
+LifetimeSec=60
+[Route]
+Destination=$stateless_pfx/76
+Type=unreachable
+Metric=1024
+EOF
+      for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do
+        mkdir -p -m 755 "$(dirname "$file")"
+        printf '%s' "$contents" >"$file"
+      done
+
+      contents='[Network]'$'\n'
+      contents+="Address=$pfx/128"$'\n'
+      contents+="Gateway=$rtr"$'\n'
+      for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/49-public-ra.conf; do
+        mkdir -p -m 755 "$(dirname "$file")"
+        printf '%s' "$contents" >"$file"
+      done
+
+      # Ensure that systemd-networkd performs a reconfiguration as it doesn't
+      # currently check the mtime of drop-in files.
+      touch -c /lib/systemd/network/*-bmc-gbmcbr.network
+      touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network
+
+      if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
+        networkctl reload
+        networkctl reconfigure gbmcbr
+      fi
+
+    read -r -d '' contents <<EOF
+table inet filter {
+  chain ncsi_input {
+    ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
+  }
+  chain ncsi_forward {
+    ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
+  }
+}
+EOF
+      rfile=/run/nftables/40-gbmc-ncsi-ra.rules
+      mkdir -p -m 755 "$(dirname "$rfile")"
+      printf '%s' "$contents" >"$rfile"
+      systemctl reset-failed nftables
+      systemctl --no-block restart nftables
+
+      old_pfx="$pfx"
+      old_rtr="$rtr"
+    fi
+  done < <(rdisc6 -d -m @NCSI_IF@ -w $(( w * 1000 )) 2>/dev/null)
+  # If rdisc6 exits early we still want to wait the full `w` time before
+  # starting again.
+  (( timeout = start + w - SECONDS ))
+  sleep $(( timeout < 0 ? 0 : timeout ))
+done
diff --git a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
index 0302ed8..29cf9c3 100644
--- a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
+++ b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
@@ -12,6 +12,8 @@
   file://-bmc-gbmcncsidhcp.network \
   file://50-gbmc-ncsi.rules.in \
   file://gbmc-ncsi-dhcrelay.service.in \
+  file://gbmc-ncsi-ip-from-ra.service.in \
+  file://gbmc-ncsi-ip-from-ra.sh.in \
   file://gbmc-ncsi-sslh.socket.in \
   file://gbmc-ncsi-sslh.service \
   file://gbmc-ncsi-nft.sh.in \
@@ -39,6 +41,7 @@
   gbmc-ncsi-sslh.service \
   gbmc-ncsi-sslh.socket \
   gbmc-ncsi-set-nicenabled.service \
+  gbmc-ncsi-ip-from-ra.service \
   "
 
 do_install:append() {
@@ -100,6 +103,14 @@
 
   sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-dhcrelay.service.in \
     >${D}${systemd_system_unitdir}/gbmc-ncsi-dhcrelay.service
+
+  sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-ip-from-ra.service.in \
+    >${WORKDIR}/gbmc-ncsi-ip-from-ra.service
+  install -m0644 ${WORKDIR}/gbmc-ncsi-ip-from-ra.service ${D}${systemd_system_unitdir}
+  sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-ip-from-ra.sh.in \
+    >${WORKDIR}/gbmc-ncsi-ip-from-ra.sh
+  install -d -m0755 ${D}${libexecdir}
+  install -m0755 ${WORKDIR}/gbmc-ncsi-ip-from-ra.sh ${D}${libexecdir}/
 }
 
 do_rm_work:prepend() {