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 | |
| 16 | source /usr/share/network/lib.sh || exit |
| 17 | source /usr/libexec/ncsid_lib.sh || exit |
| 18 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 19 | NCSI_IF='@NCSI_IF@' |
| 20 | |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 21 | old_pfx= |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 22 | old_fqdn= |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 23 | old_rtr= |
| 24 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 25 | set_host() { |
| 26 | [ -n "$host" -a -n "$domain" -a -n "$hextet" ] || return |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 27 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 28 | local fqdn="$host-n$hextet.$domain" |
| 29 | [ "$fqdn" != "$old_fqdn" ] || return |
| 30 | old_fqdn="$fqdn" |
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 | echo "Found hostname $fqdn" >&2 |
| 33 | hostnamectl set-hostname "$fqdn" || true |
| 34 | } |
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 | set_net() { |
| 37 | [ -n "$pfx" -a -n "$rtr" ] || return |
| 38 | [[ "$pfx" != "$old_pfx" || "$rtr" != "$old_rtr" ]] || return |
| 39 | old_pfx="$pfx" |
| 40 | old_rtr="$rtr" |
| 41 | |
| 42 | echo "Found prefix $pfx from $rtr" >&2 |
| 43 | |
William A. Kennington III | 7843a81 | 2021-12-09 14:17:10 -0800 | [diff] [blame] | 44 | # We no longer need NCSId if we are in this configuration |
| 45 | systemctl stop --no-block ncsid@"$NCSI_IF" || true |
| 46 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 47 | # Delete any stale IP Addresses from the primary interface as we won't use them |
| 48 | UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '0.0.0.0' '0' || true |
| 49 | UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '::' '0' || true |
| 50 | |
| 51 | read -r -d '' contents <<EOF |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 52 | [Network] |
| 53 | Address=$pfx/128 |
| 54 | IPv6PrefixDelegation=yes |
| 55 | [IPv6PrefixDelegation] |
| 56 | RouterLifetimeSec=60 |
| 57 | [IPv6Prefix] |
| 58 | Prefix=$stateless_pfx/80 |
| 59 | PreferredLifetimeSec=60 |
| 60 | ValidLifetimeSec=60 |
| 61 | [IPv6RoutePrefix] |
| 62 | Route=$pfx/80 |
| 63 | LifetimeSec=60 |
| 64 | [Route] |
| 65 | Destination=$stateless_pfx/76 |
| 66 | Type=unreachable |
| 67 | Metric=1024 |
| 68 | EOF |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 69 | for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do |
| 70 | mkdir -p -m 755 "$(dirname "$file")" |
| 71 | printf '%s' "$contents" >"$file" |
| 72 | done |
| 73 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 74 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 75 | contents='[Network]'$'\n' |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 76 | contents+="Gateway=$rtr"$'\n' |
| 77 | for file in /run/systemd/network/{00,}-bmc-"$NCSI_IF".network.d/49-public-ra.conf; do |
| 78 | mkdir -p -m 755 "$(dirname "$file")" |
| 79 | printf '%s' "$contents" >"$file" |
| 80 | done |
| 81 | touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 82 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 83 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 84 | networkctl reload && networkctl reconfigure gbmcbr "$NCSI_IF" || true |
| 85 | fi |
William A. Kennington III | 71fc189 | 2021-12-13 14:32:20 -0800 | [diff] [blame] | 86 | |
| 87 | # DHCP Relay workaround until alternate source port is supported |
| 88 | # TODO: Remove this once internal relaying cleanups land |
| 89 | gbmc-ncsi-smartnic-wa.sh || true |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 90 | } |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 91 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 92 | w=60 |
| 93 | while true; do |
| 94 | start=$SECONDS |
| 95 | while read line; do |
| 96 | if [ -z "$line" ]; then |
| 97 | hextet= |
| 98 | pfx= |
| 99 | host= |
| 100 | domain= |
| 101 | elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then |
| 102 | t_pfx="${BASH_REMATCH[1]}" |
| 103 | t_pfx_len="${BASH_REMATCH[2]}" |
| 104 | ip_to_bytes t_pfx_b "$t_pfx" || continue |
| 105 | (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue |
| 106 | (( t_pfx_b[9] |= 1 )) |
| 107 | hextet="fd$(printf '%02x' ${t_pfx_b[9]})" |
| 108 | pfx="$(ip_bytes_to_str t_pfx_b)" |
| 109 | (( t_pfx_b[9] &= 0xf0 )) |
| 110 | stateless_pfx="$(ip_bytes_to_str t_pfx_b)" |
| 111 | elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then |
| 112 | host="${BASH_REMATCH[1]}" |
| 113 | domain="${BASH_REMATCH[2]}" |
| 114 | elif [[ "$line" =~ ^from' '(.*)$ ]]; then |
| 115 | rtr="${BASH_REMATCH[1]}" |
| 116 | set_net || true |
| 117 | set_host || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 118 | fi |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 119 | 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] | 120 | # If rdisc6 exits early we still want to wait the full `w` time before |
| 121 | # starting again. |
| 122 | (( timeout = start + w - SECONDS )) |
| 123 | sleep $(( timeout < 0 ? 0 : timeout )) |
| 124 | done |