meta-google: gbmc-ncsi-config: Re-organize RA code

This make it possible for the hostname or IP information to change
independently, in case a mistake is made in a development environment
and just the hostname or just the prefix need to be updated.

Change-Id: I66169dc6cdee681f77bad4b8638dc6a2c72fca5f
Signed-off-by: William A. Kennington III <wak@google.com>
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
index 7ba159f..80bd34f 100755
--- 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
@@ -16,41 +16,36 @@
 source /usr/share/network/lib.sh || exit
 source /usr/libexec/ncsid_lib.sh || exit
 
+NCSI_IF='@NCSI_IF@'
+
 old_pfx=
+old_fqdn=
 old_rtr=
 
-w=60
-while true; do
-  start=$SECONDS
-  while read line; do
-    if [ -z "$line" ]; then
-      pfx=
-      host=
-    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 ))
-      hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
-      pfx="$(ip_bytes_to_str t_pfx_b)"
-      (( t_pfx_b[9] &= 0xf0 ))
-      stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
-    elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then
-      host="${BASH_REMATCH[1]}"
-      domain="${BASH_REMATCH[2]}"
-    elif [[ "$line" =~ ^from' '(.*)$ ]]; then
-      rtr="${BASH_REMATCH[1]}"
-      (( "${#pfx}" != 0 )) || continue
-      [[ "$pfx" != "$old_pfx" || "$old_rtr" != "$rtr" ]] || continue
+set_host() {
+  [ -n "$host" -a -n "$domain" -a -n "$hextet" ] || return
 
-      echo "Found prefix $pfx from $rtr" >&2
+  local fqdn="$host-n$hextet.$domain"
+  [ "$fqdn" != "$old_fqdn" ] || return
+  old_fqdn="$fqdn"
 
-      # 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'
+  echo "Found hostname $fqdn" >&2
+  hostnamectl set-hostname "$fqdn" || true
+}
 
-    read -r -d '' contents <<EOF
+set_net() {
+  [ -n "$pfx" -a -n "$rtr" ] || return
+  [[ "$pfx" != "$old_pfx" || "$rtr" != "$old_rtr" ]] || return
+  old_pfx="$pfx"
+  old_rtr="$rtr"
+
+  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' || true
+  UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '::' '0' || true
+
+  read -r -d '' contents <<EOF
 [Network]
 Address=$pfx/128
 IPv6PrefixDelegation=yes
@@ -68,30 +63,26 @@
 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
+  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
+  touch -c /lib/systemd/network/*-bmc-gbmcbr.network || true
 
-      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
+  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
+  touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true
 
-      # 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 "$NCSI_IF" || true
+  fi
 
-      if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
-        networkctl reload
-        networkctl reconfigure gbmcbr
-      fi
-
-    read -r -d '' contents <<EOF
+  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
@@ -101,21 +92,40 @@
   }
 }
 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
+  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 || true
+}
 
-      # Set the machine hostname if discovered
-      if [ -n "$host" ]; then
-        hostnamectl set-hostname "$host-n$hextet.$domain"
-      fi
-
-      old_pfx="$pfx"
-      old_rtr="$rtr"
+w=60
+while true; do
+  start=$SECONDS
+  while read line; do
+    if [ -z "$line" ]; then
+      hextet=
+      pfx=
+      host=
+      domain=
+    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 ))
+      hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
+      pfx="$(ip_bytes_to_str t_pfx_b)"
+      (( t_pfx_b[9] &= 0xf0 ))
+      stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
+    elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then
+      host="${BASH_REMATCH[1]}"
+      domain="${BASH_REMATCH[2]}"
+    elif [[ "$line" =~ ^from' '(.*)$ ]]; then
+      rtr="${BASH_REMATCH[1]}"
+      set_net || true
+      set_host || true
     fi
-  done < <(rdisc6 -d -m @NCSI_IF@ -w $(( w * 1000 )) 2>/dev/null)
+  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 ))