William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 1 | # Copyright 2021 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | [ -z "${gbmc_ncsi_br_deprecated_ips_lib-}" ] || return |
| 16 | |
William A. Kennington III | 2f5e169 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 17 | source /usr/share/network/lib.sh || exit |
| 18 | |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 19 | gbmc_ncsi_br_deprecated_ips_init= |
| 20 | gbmc_ncsi_br_deprecated_ips_confip= |
| 21 | gbmc_ncsi_br_deprecated_ips_lastip= |
| 22 | |
| 23 | gbmc_ncsi_br_deprecated_ips_update() { |
| 24 | [ -n "$gbmc_ncsi_br_deprecated_ips_init" ] || return |
| 25 | [ "$gbmc_ncsi_br_deprecated_ips_confip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ] || return |
| 26 | gbmc_ncsi_br_deprecated_ips_confip="$gbmc_ncsi_br_deprecated_ips_lastip" |
| 27 | |
| 28 | printf 'gBMC Bridge NCSI Deprecated Addrs: %s\n' \ |
| 29 | "${gbmc_ncsi_br_deprecated_ips_lastip:-(deleted)}" >&2 |
| 30 | |
| 31 | local contents= |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 32 | local nfcontents= |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 33 | if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 34 | local pfx_bytes=() |
| 35 | ip_to_bytes pfx_bytes "$gbmc_ncsi_br_deprecated_ips_lastip" |
| 36 | |
| 37 | local pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 3dbea93 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 38 | (( pfx_bytes[9] &= 0xf0 )) |
| 39 | local stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 51def64 | 2022-02-13 23:11:59 -0800 | [diff] [blame] | 40 | local gbmcbr_mac="$(ip link show gbmcbr | tail -n 1 | awk '{print $2}')" |
| 41 | local gbmcbr_eui48="$(mac_to_eui48 "$gbmcbr_mac")" |
| 42 | local stateless_ip="$(ip_pfx_concat "$stateless_pfx/80" "$gbmcbr_eui48")" |
| 43 | stateless_ip="${stateless_ip%/*}" |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 44 | pfx_bytes[8]=0 |
| 45 | pfx_bytes[9]=0 |
| 46 | local host_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 47 | read -r -d '' contents <<EOF |
| 48 | [Address] |
| 49 | Address=$pfx/128 |
| 50 | PreferredLifetime=0 |
| 51 | [Address] |
William A. Kennington III | 3dbea93 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 52 | Address=$stateless_pfx/128 |
| 53 | PreferredLifetime=0 |
| 54 | [Address] |
William A. Kennington III | 51def64 | 2022-02-13 23:11:59 -0800 | [diff] [blame] | 55 | Address=$stateless_ip/128 |
| 56 | PreferredLifetime=0 |
| 57 | [Address] |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 58 | Address=$host_pfx/128 |
| 59 | PreferredLifetime=0 |
| 60 | EOF |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 61 | read -r -d '' nfcontents <<EOF |
| 62 | table inet filter { |
| 63 | chain ncsi_input { |
| 64 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input |
| 65 | } |
| 66 | chain ncsi_forward { |
| 67 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept |
| 68 | } |
| 69 | } |
| 70 | EOF |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 71 | fi |
| 72 | |
| 73 | local file |
| 74 | for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do |
| 75 | mkdir -p -m 755 "$(dirname "$file")" |
| 76 | if [ -z "$contents" ]; then |
| 77 | rm -f "$file" |
| 78 | else |
| 79 | printf '%s' "$contents" >"$file" |
| 80 | fi |
| 81 | done |
| 82 | |
| 83 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 84 | # currently check the mtime of drop-in files. |
| 85 | touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network |
| 86 | |
| 87 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 88 | networkctl reload && networkctl reconfigure @NCSI_IF@ |
| 89 | fi |
| 90 | |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 91 | local rfile=/run/nftables/40-gbmc-ncsi-br.rules |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 92 | mkdir -p -m 755 "$(dirname "$rfile")" |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 93 | if [ -z "$nfcontents" ]; then |
| 94 | rm -f "$rfile" |
| 95 | else |
| 96 | printf '%s' "$nfcontents" >"$rfile" |
| 97 | fi |
William A. Kennington III | 7356f8e | 2021-12-15 02:21:52 -0800 | [diff] [blame] | 98 | systemctl reset-failed nftables && systemctl --no-block reload-or-restart nftables || true |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | gbmc_ncsi_br_deprecated_ips_hook() { |
| 102 | if [ "$change" = 'init' ]; then |
| 103 | gbmc_ncsi_br_deprecated_ips_init=1 |
| 104 | gbmc_ip_monitor_defer |
| 105 | elif [ "$change" = 'defer' ]; then |
| 106 | gbmc_ncsi_br_deprecated_ips_update |
| 107 | elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' ] && |
| 108 | [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then |
| 109 | local pfx_bytes=() |
| 110 | ip_to_bytes pfx_bytes "$ip" || return |
| 111 | # No ULA Addresses |
| 112 | if (( pfx_bytes[0] & 0xfe == 0xfc )); then |
| 113 | return |
| 114 | fi |
| 115 | # We only want to allow a <pfx>::fd0x address, where x>0 |
| 116 | if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then |
| 117 | return |
| 118 | fi |
| 119 | for (( i = 10; i < 16; ++i )); do |
| 120 | if (( pfx_bytes[i] != 0 )); then |
| 121 | return |
| 122 | fi |
| 123 | done |
| 124 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 125 | gbmc_ncsi_br_deprecated_ips_lastip="$ip" |
| 126 | gbmc_ip_monitor_defer |
| 127 | fi |
| 128 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 129 | gbmc_ncsi_br_deprecated_ips_lastip= |
| 130 | gbmc_ip_monitor_defer |
| 131 | fi |
| 132 | fi |
| 133 | } |
| 134 | |
| 135 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook) |
| 136 | |
| 137 | gbmc_ncsi_br_deprecated_ips_lib=1 |