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