blob: 5daa1527a338bdf55b4e578ec16e07aee1e69423 [file] [log] [blame]
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -07001#!/bin/bash
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
William A. Kennington III37e6f392022-05-16 16:19:11 -070016[ ! -e /usr/share/gbmc-br-lib.sh ] && exit
17
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070018source /usr/share/network/lib.sh || exit
William A. Kennington III37e6f392022-05-16 16:19:11 -070019source /usr/share/gbmc-br-lib.sh || exit
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070020
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070021NCSI_IF='@NCSI_IF@'
22
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070023old_pfx=
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070024old_fqdn=
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070025old_rtr=
26
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070027set_host() {
28 [ -n "$host" -a -n "$domain" -a -n "$hextet" ] || return
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070029
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070030 local fqdn="$host-n$hextet.$domain"
31 [ "$fqdn" != "$old_fqdn" ] || return
32 old_fqdn="$fqdn"
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070033
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070034 echo "Found hostname $fqdn" >&2
35 hostnamectl set-hostname "$fqdn" || true
36}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070037
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070038set_net() {
39 [ -n "$pfx" -a -n "$rtr" ] || return
40 [[ "$pfx" != "$old_pfx" || "$rtr" != "$old_rtr" ]] || return
41 old_pfx="$pfx"
42 old_rtr="$rtr"
43
44 echo "Found prefix $pfx from $rtr" >&2
45
William A. Kennington III7843a812021-12-09 14:17:10 -080046 # We no longer need NCSId if we are in this configuration
47 systemctl stop --no-block ncsid@"$NCSI_IF" || true
48
William A. Kennington III37e6f392022-05-16 16:19:11 -070049 # Save the IP address for the interface
50 gbmc_br_set_ip "$pfx" || true
William A. Kennington III71fc1892021-12-13 14:32:20 -080051
52 # DHCP Relay workaround until alternate source port is supported
53 # TODO: Remove this once internal relaying cleanups land
54 gbmc-ncsi-smartnic-wa.sh || true
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070055}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070056
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070057w=60
58while true; do
59 start=$SECONDS
60 while read line; do
61 if [ -z "$line" ]; then
62 hextet=
63 pfx=
64 host=
65 domain=
66 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
67 t_pfx="${BASH_REMATCH[1]}"
68 t_pfx_len="${BASH_REMATCH[2]}"
69 ip_to_bytes t_pfx_b "$t_pfx" || continue
70 (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
71 (( t_pfx_b[9] |= 1 ))
72 hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
73 pfx="$(ip_bytes_to_str t_pfx_b)"
William A. Kennington III882b78b2022-03-01 16:27:20 -080074 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([a-z]+[0-9]+)[^.]*[.](.*.google.com)$ ]]; then
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070075 host="${BASH_REMATCH[1]}"
76 domain="${BASH_REMATCH[2]}"
77 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
78 rtr="${BASH_REMATCH[1]}"
79 set_net || true
80 set_host || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070081 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070082 done < <(rdisc6 -d -m "$NCSI_IF" -w $(( w * 1000 )) 2>/dev/null)
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070083 # If rdisc6 exits early we still want to wait the full `w` time before
84 # starting again.
85 (( timeout = start + w - SECONDS ))
86 sleep $(( timeout < 0 ? 0 : timeout ))
87done