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 | |
| 44 | # Delete any stale IP Addresses from the primary interface as we won't use them |
| 45 | UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '0.0.0.0' '0' || true |
| 46 | UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '::' '0' || true |
| 47 | |
| 48 | read -r -d '' contents <<EOF |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 49 | [Network] |
| 50 | Address=$pfx/128 |
| 51 | IPv6PrefixDelegation=yes |
| 52 | [IPv6PrefixDelegation] |
| 53 | RouterLifetimeSec=60 |
| 54 | [IPv6Prefix] |
| 55 | Prefix=$stateless_pfx/80 |
| 56 | PreferredLifetimeSec=60 |
| 57 | ValidLifetimeSec=60 |
| 58 | [IPv6RoutePrefix] |
| 59 | Route=$pfx/80 |
| 60 | LifetimeSec=60 |
| 61 | [Route] |
| 62 | Destination=$stateless_pfx/76 |
| 63 | Type=unreachable |
| 64 | Metric=1024 |
| 65 | EOF |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 66 | for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do |
| 67 | mkdir -p -m 755 "$(dirname "$file")" |
| 68 | printf '%s' "$contents" >"$file" |
| 69 | done |
| 70 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 71 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 72 | contents='[Network]'$'\n' |
| 73 | contents+="Address=$pfx/128"$'\n' |
| 74 | contents+="Gateway=$rtr"$'\n' |
| 75 | for file in /run/systemd/network/{00,}-bmc-"$NCSI_IF".network.d/49-public-ra.conf; do |
| 76 | mkdir -p -m 755 "$(dirname "$file")" |
| 77 | printf '%s' "$contents" >"$file" |
| 78 | done |
| 79 | touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 80 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 81 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 82 | networkctl reload && networkctl reconfigure gbmcbr "$NCSI_IF" || true |
| 83 | fi |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 84 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 85 | read -r -d '' contents <<EOF |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 86 | table inet filter { |
| 87 | chain ncsi_input { |
| 88 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input |
| 89 | } |
| 90 | chain ncsi_forward { |
| 91 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept |
| 92 | } |
| 93 | } |
| 94 | EOF |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 95 | rfile=/run/nftables/40-gbmc-ncsi-ra.rules |
| 96 | mkdir -p -m 755 "$(dirname "$rfile")" |
| 97 | printf '%s' "$contents" >"$rfile" |
| 98 | systemctl reset-failed nftables && systemctl --no-block restart nftables || true |
| 99 | } |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 100 | |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 101 | w=60 |
| 102 | while true; do |
| 103 | start=$SECONDS |
| 104 | while read line; do |
| 105 | if [ -z "$line" ]; then |
| 106 | hextet= |
| 107 | pfx= |
| 108 | host= |
| 109 | domain= |
| 110 | elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then |
| 111 | t_pfx="${BASH_REMATCH[1]}" |
| 112 | t_pfx_len="${BASH_REMATCH[2]}" |
| 113 | ip_to_bytes t_pfx_b "$t_pfx" || continue |
| 114 | (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue |
| 115 | (( t_pfx_b[9] |= 1 )) |
| 116 | hextet="fd$(printf '%02x' ${t_pfx_b[9]})" |
| 117 | pfx="$(ip_bytes_to_str t_pfx_b)" |
| 118 | (( t_pfx_b[9] &= 0xf0 )) |
| 119 | stateless_pfx="$(ip_bytes_to_str t_pfx_b)" |
| 120 | elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then |
| 121 | host="${BASH_REMATCH[1]}" |
| 122 | domain="${BASH_REMATCH[2]}" |
| 123 | elif [[ "$line" =~ ^from' '(.*)$ ]]; then |
| 124 | rtr="${BASH_REMATCH[1]}" |
| 125 | set_net || true |
| 126 | set_host || true |
William A. Kennington III | c7454fb | 2021-09-14 16:01:37 -0700 | [diff] [blame] | 127 | fi |
William A. Kennington III | 5b4b4f8 | 2021-10-15 12:22:08 -0700 | [diff] [blame] | 128 | 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] | 129 | # If rdisc6 exits early we still want to wait the full `w` time before |
| 130 | # starting again. |
| 131 | (( timeout = start + w - SECONDS )) |
| 132 | sleep $(( timeout < 0 ? 0 : timeout )) |
| 133 | done |