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 | |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 15 | [ -n "${gbmc_ncsi_br_deprecated_ips_lib-}" ] && return |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 16 | |
William A. Kennington III | 2f5e169 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 17 | source /usr/share/network/lib.sh || exit |
William A. Kennington III | a108fcd | 2024-09-12 14:39:38 -0700 | [diff] [blame] | 18 | source /usr/share/gbmc-net-lib.sh || exit |
William A. Kennington III | 2f5e169 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 19 | |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 20 | gbmc_ncsi_br_deprecated_ips_init= |
| 21 | gbmc_ncsi_br_deprecated_ips_confip= |
| 22 | gbmc_ncsi_br_deprecated_ips_lastip= |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 23 | gbmc_ncsi_br_deprecated_ips_lastncsi= |
| 24 | gbmc_ncsi_br_deprecated_ips_confncsi= |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 25 | |
William A. Kennington III | 8bd3a6f | 2024-09-10 16:44:23 -0700 | [diff] [blame] | 26 | GBMC_NCSI_ROUTE_TABLE=900 |
| 27 | |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 28 | gbmc_ncsi_br_deprecated_ips_update() { |
| 29 | [ -n "$gbmc_ncsi_br_deprecated_ips_init" ] || return |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 30 | [ "$gbmc_ncsi_br_deprecated_ips_confip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ] || \ |
| 31 | [ "$gbmc_ncsi_br_deprecated_ips_confncsi" != "$gbmc_ncsi_br_deprecated_ips_lastncsi" ] || return |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 32 | gbmc_ncsi_br_deprecated_ips_confip="$gbmc_ncsi_br_deprecated_ips_lastip" |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 33 | gbmc_ncsi_br_deprecated_ips_confncsi="$gbmc_ncsi_br_deprecated_ips_lastncsi" |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 34 | |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 35 | printf 'gBMC NCSI Deprecated Addrs: %s\n' \ |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 36 | "${gbmc_ncsi_br_deprecated_ips_lastip:-(deleted)}" >&2 |
| 37 | |
| 38 | local contents= |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 39 | local nfcontents= |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 40 | if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 41 | local pfx_bytes=() |
| 42 | ip_to_bytes pfx_bytes "$gbmc_ncsi_br_deprecated_ips_lastip" |
| 43 | |
| 44 | local pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 3dbea93 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 45 | (( pfx_bytes[9] &= 0xf0 )) |
| 46 | local stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 47 | local stateless_ip= |
| 48 | if [ -e /sys/class/net/gbmcbr ]; then |
| 49 | local gbmcbr_mac="$(ip link show gbmcbr | tail -n 1 | awk '{print $2}')" |
| 50 | local gbmcbr_eui48="$(mac_to_eui48 "$gbmcbr_mac")" |
| 51 | stateless_ip="$(ip_pfx_concat "$stateless_pfx/80" "$gbmcbr_eui48")" |
| 52 | stateless_ip="${stateless_ip%/*}" |
| 53 | fi |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 54 | pfx_bytes[8]=0 |
| 55 | pfx_bytes[9]=0 |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 56 | local host_pfx= |
| 57 | if [ -n "${gbmc_ncsi_br_deprecated_ips_confncsi}" ]; then |
| 58 | # Only impersonate the host if we have an NCSI state machine |
| 59 | host_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 60 | fi |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 61 | read -r -d '' contents <<EOF |
| 62 | [Network] |
| 63 | IPv6ProxyNDP=yes |
| 64 | IPv6ProxyNDPAddress=$pfx |
| 65 | IPv6ProxyNDPAddress=$stateless_pfx |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 66 | ${host_pfx:+IPv6ProxyNDPAddress=}$host_pfx |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 67 | ${stateless_ip:+IPv6ProxyNDPAddress=}$stateless_ip |
William A. Kennington III | 8bd3a6f | 2024-09-10 16:44:23 -0700 | [diff] [blame] | 68 | [RoutingPolicyRule] |
| 69 | From=$pfx/76 |
| 70 | Table=$GBMC_NCSI_ROUTE_TABLE |
| 71 | Priority=$GBMC_NCSI_ROUTE_TABLE |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 72 | EOF |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 73 | read -r -d '' nfcontents <<EOF |
| 74 | table inet filter { |
| 75 | chain ncsi_input { |
| 76 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 77 | ${host_pfx:+ip6 daddr $host_pfx/64 goto ncsi_legacy_input} |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 78 | } |
| 79 | chain ncsi_forward { |
| 80 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept |
| 81 | } |
| 82 | } |
| 83 | EOF |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 84 | fi |
| 85 | |
| 86 | local file |
| 87 | for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do |
| 88 | mkdir -p -m 755 "$(dirname "$file")" |
| 89 | if [ -z "$contents" ]; then |
| 90 | rm -f "$file" |
| 91 | else |
| 92 | printf '%s' "$contents" >"$file" |
| 93 | fi |
| 94 | done |
| 95 | |
William A. Kennington III | 770b25a | 2024-09-05 14:26:14 -0700 | [diff] [blame] | 96 | # Remove all old ncsi neighbor proxied entries, since we solely |
| 97 | # manage all of these entries and we don't want any that are stale |
| 98 | local entry |
| 99 | while read -r entry; do |
| 100 | ip -6 neigh del proxy ${entry%% *} dev @NCSI_IF@ || true |
| 101 | done < <(ip -6 neigh show proxy dev @NCSI_IF@) |
William A. Kennington III | 8bd3a6f | 2024-09-10 16:44:23 -0700 | [diff] [blame] | 102 | ip -6 rule del pref $GBMC_NCSI_ROUTE_TABLE 2>/dev/null || true |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 103 | |
William A. Kennington III | 770b25a | 2024-09-05 14:26:14 -0700 | [diff] [blame] | 104 | local st=0 |
| 105 | if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 106 | sysctl net.ipv6.conf.@NCSI_IF@.proxy_ndp=1 >/dev/null || st=$? |
| 107 | ip -6 neigh add proxy $pfx dev @NCSI_IF@ || st=$? |
| 108 | ip -6 neigh add proxy $stateless_pfx dev @NCSI_IF@ || st=$? |
| 109 | if [ -n "$host_pfx" ]; then |
| 110 | ip -6 neigh add proxy $host_pfx dev @NCSI_IF@ || st=$? |
| 111 | fi |
| 112 | if [ -n "$stateless_ip" ]; then |
| 113 | ip -6 neigh add proxy $stateless_ip dev @NCSI_IF@ || st=$? |
| 114 | fi |
William A. Kennington III | 8bd3a6f | 2024-09-10 16:44:23 -0700 | [diff] [blame] | 115 | ip -6 rule add pref $GBMC_NCSI_ROUTE_TABLE from $pfx/76 \ |
| 116 | lookup $GBMC_NCSI_ROUTE_TABLE || st=$? |
William A. Kennington III | 770b25a | 2024-09-05 14:26:14 -0700 | [diff] [blame] | 117 | fi |
| 118 | |
William A. Kennington III | a108fcd | 2024-09-12 14:39:38 -0700 | [diff] [blame] | 119 | (( st == 0 )) || gbmc_net_networkd_reload @NCSI_IF@ |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 120 | |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 121 | local rfile=/run/nftables/40-gbmc-ncsi-br.rules |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 122 | mkdir -p -m 755 "$(dirname "$rfile")" |
William A. Kennington III | b823f89 | 2021-11-23 20:54:56 -0800 | [diff] [blame] | 123 | if [ -z "$nfcontents" ]; then |
| 124 | rm -f "$rfile" |
| 125 | else |
| 126 | printf '%s' "$nfcontents" >"$rfile" |
| 127 | fi |
William A. Kennington III | 7356f8e | 2021-12-15 02:21:52 -0800 | [diff] [blame] | 128 | 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] | 129 | } |
| 130 | |
| 131 | gbmc_ncsi_br_deprecated_ips_hook() { |
| 132 | if [ "$change" = 'init' ]; then |
| 133 | gbmc_ncsi_br_deprecated_ips_init=1 |
| 134 | gbmc_ip_monitor_defer |
| 135 | elif [ "$change" = 'defer' ]; then |
| 136 | gbmc_ncsi_br_deprecated_ips_update |
William A. Kennington III | 80a7528 | 2022-05-20 09:47:33 -0700 | [diff] [blame] | 137 | elif [ "$change" = 'addr' -a "$scope" = 'global' -a "$fam" = 'inet6' ] && |
| 138 | [ "$intf" = 'gbmcbr' -o "$intf" = '@NCSI_IF@' ] && |
| 139 | [[ "$flags" != *deprecated* ]]; then |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 140 | local pfx_bytes=() |
| 141 | ip_to_bytes pfx_bytes "$ip" || return |
| 142 | # No ULA Addresses |
William A. Kennington III | 36a6f08 | 2024-02-27 15:42:56 -0800 | [diff] [blame] | 143 | if (( (pfx_bytes[0] & 0xfe) == 0xfc )); then |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 144 | return |
| 145 | fi |
| 146 | # We only want to allow a <pfx>::fd0x address, where x>0 |
William A. Kennington III | 36a6f08 | 2024-02-27 15:42:56 -0800 | [diff] [blame] | 147 | if (( pfx_bytes[8] != 0xfd || (pfx_bytes[9] & 0xf) == 0 )); then |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 148 | return |
| 149 | fi |
| 150 | for (( i = 10; i < 16; ++i )); do |
| 151 | if (( pfx_bytes[i] != 0 )); then |
| 152 | return |
| 153 | fi |
| 154 | done |
| 155 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 156 | gbmc_ncsi_br_deprecated_ips_lastip="$ip" |
| 157 | gbmc_ip_monitor_defer |
| 158 | fi |
| 159 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 160 | gbmc_ncsi_br_deprecated_ips_lastip= |
| 161 | gbmc_ip_monitor_defer |
| 162 | fi |
William A. Kennington III | 9f57cee | 2022-11-03 14:17:35 -0700 | [diff] [blame] | 163 | elif [ "$change" = 'link' -a "$action" = 'add' -a "$intf" = '@NCSI_IF@' ]; then |
| 164 | if ip link show '@NCSI_IF@' | grep -q '^ *alias ncsi$'; then |
| 165 | gbmc_ncsi_br_deprecated_ips_lastncsi=1 |
| 166 | gbmc_ip_monitor_defer |
| 167 | else |
| 168 | gbmc_ncsi_br_deprecated_ips_lastncsi= |
| 169 | gbmc_ip_monitor_defer |
| 170 | fi |
William A. Kennington III | 21e7e45 | 2021-11-05 01:31:59 -0700 | [diff] [blame] | 171 | fi |
| 172 | } |
| 173 | |
| 174 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook) |
| 175 | |
| 176 | gbmc_ncsi_br_deprecated_ips_lib=1 |