blob: 4aae5c86b3f18f3043f206b53e81deb529e8a9bb [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 III4372aab2023-06-05 14:23:49 -070018# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070019source /usr/share/network/lib.sh || exit
William A. Kennington III4372aab2023-06-05 14:23:49 -070020# shellcheck source=meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh
William A. Kennington III37e6f392022-05-16 16:19:11 -070021source /usr/share/gbmc-br-lib.sh || exit
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070022
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070023NCSI_IF='@NCSI_IF@'
24
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070025old_pfx=
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070026old_fqdn=
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070027old_rtr=
28
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070029set_host() {
William A. Kennington III4372aab2023-06-05 14:23:49 -070030 [[ -n "$host" && -n "$domain" && -n "$hextet" ]] || return
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070031
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070032 local fqdn="$host-n$hextet.$domain"
33 [ "$fqdn" != "$old_fqdn" ] || return
34 old_fqdn="$fqdn"
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070035
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070036 echo "Found hostname $fqdn" >&2
37 hostnamectl set-hostname "$fqdn" || true
38}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070039
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070040set_net() {
William A. Kennington III4372aab2023-06-05 14:23:49 -070041 [[ -n "$pfx" && -n "$rtr" ]] || return
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070042 [[ "$pfx" != "$old_pfx" || "$rtr" != "$old_rtr" ]] || return
43 old_pfx="$pfx"
44 old_rtr="$rtr"
45
46 echo "Found prefix $pfx from $rtr" >&2
47
William A. Kennington III7843a812021-12-09 14:17:10 -080048 # We no longer need NCSId if we are in this configuration
49 systemctl stop --no-block ncsid@"$NCSI_IF" || true
50
William A. Kennington III37e6f392022-05-16 16:19:11 -070051 # Save the IP address for the interface
52 gbmc_br_set_ip "$pfx" || true
William A. Kennington III71fc1892021-12-13 14:32:20 -080053
54 # DHCP Relay workaround until alternate source port is supported
55 # TODO: Remove this once internal relaying cleanups land
56 gbmc-ncsi-smartnic-wa.sh || true
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070057}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070058
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070059w=60
60while true; do
61 start=$SECONDS
William A. Kennington III4372aab2023-06-05 14:23:49 -070062 while read -r line; do
William A. Kennington IIIaa21bd62024-03-19 17:32:17 -070063 # `script` terminates all lines with a CRLF, remove it
64 line="${line:0:-1}"
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070065 if [ -z "$line" ]; then
66 hextet=
67 pfx=
68 host=
69 domain=
70 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
71 t_pfx="${BASH_REMATCH[1]}"
72 t_pfx_len="${BASH_REMATCH[2]}"
73 ip_to_bytes t_pfx_b "$t_pfx" || continue
William A. Kennington III36a6f082024-02-27 15:42:56 -080074 (( t_pfx_len == 76 && (t_pfx_b[8] & 0xfd) == 0xfd )) || continue
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070075 (( t_pfx_b[9] |= 1 ))
William A. Kennington III4372aab2023-06-05 14:23:49 -070076 hextet="fd$(printf '%02x' "${t_pfx_b[9]}")"
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070077 pfx="$(ip_bytes_to_str t_pfx_b)"
William A. Kennington III98da9ef2024-03-22 02:37:48 -070078 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.]+)(.*[.]google[.]com)' '*$ ]]; then
William A. Kennington IIIba600192023-02-15 16:42:29 -080079 # Ideally, we use PCRE and with lookahead and can do this in a single regex
80 # ^([a-zA-Z0-9-]+(?=-n[a-fA-F0-9]{1,4})|[a-zA-Z0-9-]+(?!-n[a-fA-F0-9]{1,4}))[^.]*[.]((?:[a-zA-Z0-9]*[.])*google[.]com)$
81 # Instead we do multiple steps to extract the needed info
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070082 host="${BASH_REMATCH[1]}"
William A. Kennington IIIba600192023-02-15 16:42:29 -080083 domain="${BASH_REMATCH[2]#.}"
84 if [[ "$host" =~ (-n[a-fA-F0-9]{1,4})$ ]]; then
William A. Kennington III4372aab2023-06-05 14:23:49 -070085 host="${host%"${BASH_REMATCH[1]}"}"
William A. Kennington IIIba600192023-02-15 16:42:29 -080086 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070087 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
88 rtr="${BASH_REMATCH[1]}"
89 set_net || true
90 set_host || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070091 fi
William A. Kennington IIIaa21bd62024-03-19 17:32:17 -070092 done < <(exec script -q -c "rdisc6 -d -m $NCSI_IF -w $(( w * 1000 ))" /dev/null 2>/dev/null)
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070093 # If rdisc6 exits early we still want to wait the full `w` time before
94 # starting again.
95 (( timeout = start + w - SECONDS ))
96 sleep $(( timeout < 0 ? 0 : timeout ))
97done