ncsid: Add service for discovering routers

This will be required for relaying egress traffic from the BMC network
once the static network configurations go away. Currently this will just
be redundant behavior with existing gateway discovery.

Change-Id: I26b3f040f0752b948b9c1d9a9110a975f8168f8a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/subprojects/ncsid/src/meson.build b/subprojects/ncsid/src/meson.build
index f3e708c..9d3137f 100644
--- a/subprojects/ncsid/src/meson.build
+++ b/subprojects/ncsid/src/meson.build
@@ -77,6 +77,9 @@
 install_data(
   'ncsid_udhcpc4.script',
   'ncsid_udhcpc6.script',
+  'update_ra_gw.sh',
+  'update_ra_neighbor.sh',
+  'update_static_neighbors.sh',
   install_mode: 'rwxr-xr-x',
   install_dir: get_option('libexecdir'))
 
@@ -85,16 +88,6 @@
   install_mode: 'rw-r--r--',
   install_dir: get_option('libexecdir'))
 
-install_data(
-  'update_ra_neighbor.sh',
-  install_mode: 'rwxr-xr-x',
-  install_dir: get_option('libexecdir'))
-
-install_data(
-  'update_static_neighbors.sh',
-  install_mode: 'rwxr-xr-x',
-  install_dir: get_option('libexecdir'))
-
 systemd = dependency('systemd')
 systemunitdir = systemd.get_pkgconfig_variable('systemdsystemunitdir')
 
@@ -108,6 +101,13 @@
   install_dir: systemunitdir)
 
 configure_file(
+  configuration: {'BIN': libexecdir / 'update_ra_gw.sh'},
+  input: 'update-ra-gw@.service.in',
+  output: 'update-ra-gw@.service',
+  install_mode: 'rw-r--r--',
+  install_dir: systemunitdir)
+
+configure_file(
   configuration: {'BIN': libexecdir / 'update_ra_neighbor.sh'},
   input: 'update-ra-neighbor@.service.in',
   output: 'update-ra-neighbor@.service',
diff --git a/subprojects/ncsid/src/ncsid@.service.in b/subprojects/ncsid/src/ncsid@.service.in
index 97e041e..f592461 100644
--- a/subprojects/ncsid/src/ncsid@.service.in
+++ b/subprojects/ncsid/src/ncsid@.service.in
@@ -2,6 +2,7 @@
 Description=Run Ncsid daemon
 Wants=mapper-wait@-xyz-openbmc_project-network-%i.service
 After=mapper-wait@-xyz-openbmc_project-network-%i.service
+Wants=update-ra-gw@%i.service
 
 [Service]
 Restart=always
diff --git a/subprojects/ncsid/src/ncsid_lib.sh b/subprojects/ncsid/src/ncsid_lib.sh
index 2318595..a4899c7 100644
--- a/subprojects/ncsid/src/ncsid_lib.sh
+++ b/subprojects/ncsid/src/ncsid_lib.sh
@@ -449,5 +449,7 @@
 
   local ip="$(echo "$output" | grep 'from' | awk '{print $2}')"
   local mac="$(echo "$output" | grep 'Source link-layer' | ParseMACFromLine)"
-  printf '{"router_ip":"%s","router_mac":"%s"}\n' "$ip" "$mac"
+  local staddr="$(echo "$output" | grep 'Stateful address conf.*Yes')"
+  printf '{"router_ip":"%s","router_mac":"%s","stateful_address":"%s"}\n' \
+    "$ip" "$mac" "$staddr"
 }
diff --git a/subprojects/ncsid/src/nic-hostless@.target b/subprojects/ncsid/src/nic-hostless@.target
index 1290316..7d6fdfe 100644
--- a/subprojects/ncsid/src/nic-hostless@.target
+++ b/subprojects/ncsid/src/nic-hostless@.target
@@ -5,7 +5,7 @@
 After=ncsid@%i.service
 Wants=dhcp4@%i.service
 Wants=dhcp6@%i.service
-Wants=update-static-neighbors@%i.service
-Wants=update-static-neighbors@%i.timer
 Wants=update-ra-neighbor@%i.service
 Wants=update-ra-neighbor@%i.timer
+Wants=update-static-neighbors@%i.service
+Wants=update-static-neighbors@%i.timer
diff --git a/subprojects/ncsid/src/update-ra-gw@.service.in b/subprojects/ncsid/src/update-ra-gw@.service.in
new file mode 100644
index 0000000..d0ed003
--- /dev/null
+++ b/subprojects/ncsid/src/update-ra-gw@.service.in
@@ -0,0 +1,15 @@
+[Unit]
+Description=RA Gateway Updater
+Wants=mapper-wait@-xyz-openbmc_project-network-%i.service
+After=mapper-wait@-xyz-openbmc_project-network-%i.service
+BindsTo=ncsid@%i.service
+After=ncsid@%i.service
+StartLimitIntervalSec=1min
+StartLimitBurst=5
+
+[Service]
+KillMode=mixed
+Restart=on-failure
+ExecStart=@@BIN@ update-ra-gw %I
+SyslogIdentifier=update-ra-gw@%I
+SuccessExitStatus=10
diff --git a/subprojects/ncsid/src/update_ra_gw.sh b/subprojects/ncsid/src/update_ra_gw.sh
new file mode 100644
index 0000000..5ebb15a
--- /dev/null
+++ b/subprojects/ncsid/src/update_ra_gw.sh
@@ -0,0 +1,57 @@
+#!/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 "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
+
+UpdateRAGW() {
+  local netdev="$1"
+
+  local reqs=5
+  while true; do
+    local disc
+    if ! disc="$(RunInterruptible DiscoverRouter6 "$1" "$reqs" 360000)"; then
+      echo "Failed to discover router" >&2
+      continue
+    fi
+    # We don't want to send any requests after the initial finding
+    # Just passively listen now
+    reqs=-1
+
+    local vars
+    vars="$(echo "$disc" | JSONToVars)" || return
+    eval "$vars" || return
+    [ -n "$stateful_address" ] || continue
+    echo "GW($netdev) $router_ip MAC: $router_mac" >&2
+
+    SuppressTerm
+    local service='xyz.openbmc_project.Network'
+    local rc=0
+    UpdateGateway "$service" "$router_ip" && \
+      UpdateNeighbor "$service" "$netdev" "$router_ip" "$router_mac" || rc=$?
+    UnsuppressTerm
+  done
+}
+
+Main() {
+  set -o nounset
+  set -o errexit
+  set -o pipefail
+
+  InitTerm
+  UpdateRAGW "$@"
+}
+
+return 0 2>/dev/null
+Main "$@"