William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 1 | #!/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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 16 | [ ! -e /usr/share/gbmc-br-lib.sh ] && exit |
| 17 | |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 18 | # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 19 | source /usr/share/network/lib.sh || exit |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 20 | # shellcheck source=meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 21 | source /usr/share/gbmc-br-lib.sh || exit |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 22 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 23 | NCSI_IF='@NCSI_IF@' |
| 24 | |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 25 | old_pfx= |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 26 | old_fqdn= |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 27 | old_rtr= |
| 28 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 29 | set_host() { |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 30 | [[ -n "$host" && -n "$domain" && -n "$hextet" ]] || return |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 31 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 32 | local fqdn="$host-n$hextet.$domain" |
| 33 | [ "$fqdn" != "$old_fqdn" ] || return |
| 34 | old_fqdn="$fqdn" |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 35 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 36 | echo "Found hostname $fqdn" >&2 |
| 37 | hostnamectl set-hostname "$fqdn" || true |
| 38 | } |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 39 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 40 | set_net() { |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 41 | [[ -n "$pfx" && -n "$rtr" ]] || return |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 42 | [[ "$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 III | 7843a81 | 2021-12-09 14:17:10 -0800 | [diff] [blame] | 48 | # We no longer need NCSId if we are in this configuration |
| 49 | systemctl stop --no-block ncsid@"$NCSI_IF" || true |
| 50 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 51 | # Save the IP address for the interface |
| 52 | gbmc_br_set_ip "$pfx" || true |
William A. Kennington III | 71fc189 | 2021-12-13 14:32:20 -0800 | [diff] [blame] | 53 | |
| 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 III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 57 | } |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 58 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 59 | w=60 |
| 60 | while true; do |
| 61 | start=$SECONDS |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 62 | while read -r line; do |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 63 | if [ -z "$line" ]; then |
| 64 | hextet= |
| 65 | pfx= |
| 66 | host= |
| 67 | domain= |
| 68 | elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then |
| 69 | t_pfx="${BASH_REMATCH[1]}" |
| 70 | t_pfx_len="${BASH_REMATCH[2]}" |
| 71 | ip_to_bytes t_pfx_b "$t_pfx" || continue |
| 72 | (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue |
| 73 | (( t_pfx_b[9] |= 1 )) |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 74 | hextet="fd$(printf '%02x' "${t_pfx_b[9]}")" |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 75 | pfx="$(ip_bytes_to_str t_pfx_b)" |
William A. Kennington III | ba60019 | 2023-02-15 16:42:29 -0800 | [diff] [blame] | 76 | elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.]+)(.*[.]google[.]com)$ ]]; then |
| 77 | # Ideally, we use PCRE and with lookahead and can do this in a single regex |
| 78 | # ^([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)$ |
| 79 | # Instead we do multiple steps to extract the needed info |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 80 | host="${BASH_REMATCH[1]}" |
William A. Kennington III | ba60019 | 2023-02-15 16:42:29 -0800 | [diff] [blame] | 81 | domain="${BASH_REMATCH[2]#.}" |
| 82 | if [[ "$host" =~ (-n[a-fA-F0-9]{1,4})$ ]]; then |
William A. Kennington III | 4372aab | 2023-06-05 14:23:49 -0700 | [diff] [blame] | 83 | host="${host%"${BASH_REMATCH[1]}"}" |
William A. Kennington III | ba60019 | 2023-02-15 16:42:29 -0800 | [diff] [blame] | 84 | fi |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 85 | elif [[ "$line" =~ ^from' '(.*)$ ]]; then |
| 86 | rtr="${BASH_REMATCH[1]}" |
| 87 | set_net || true |
| 88 | set_host || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 89 | fi |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 90 | done < <(rdisc6 -d -m "$NCSI_IF" -w $(( w * 1000 )) 2>/dev/null) |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 91 | # If rdisc6 exits early we still want to wait the full `w` time before |
| 92 | # starting again. |
| 93 | (( timeout = start + w - SECONDS )) |
| 94 | sleep $(( timeout < 0 ? 0 : timeout )) |
| 95 | done |