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 | |
| 17 | gbmc_ncsi_br_deprecated_ips_init= |
| 18 | gbmc_ncsi_br_deprecated_ips_confip= |
| 19 | gbmc_ncsi_br_deprecated_ips_lastip= |
| 20 | |
| 21 | gbmc_ncsi_br_deprecated_ips_update() { |
| 22 | [ -n "$gbmc_ncsi_br_deprecated_ips_init" ] || return |
| 23 | [ "$gbmc_ncsi_br_deprecated_ips_confip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ] || return |
| 24 | gbmc_ncsi_br_deprecated_ips_confip="$gbmc_ncsi_br_deprecated_ips_lastip" |
| 25 | |
| 26 | printf 'gBMC Bridge NCSI Deprecated Addrs: %s\n' \ |
| 27 | "${gbmc_ncsi_br_deprecated_ips_lastip:-(deleted)}" >&2 |
| 28 | |
| 29 | local contents= |
| 30 | if [ -n "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 31 | local pfx_bytes=() |
| 32 | ip_to_bytes pfx_bytes "$gbmc_ncsi_br_deprecated_ips_lastip" |
| 33 | |
| 34 | local pfx="$(ip_bytes_to_str pfx_bytes)" |
| 35 | pfx_bytes[8]=0 |
| 36 | pfx_bytes[9]=0 |
| 37 | local host_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 38 | read -r -d '' contents <<EOF |
| 39 | [Address] |
| 40 | Address=$pfx/128 |
| 41 | PreferredLifetime=0 |
| 42 | [Address] |
| 43 | Address=$host_pfx/128 |
| 44 | PreferredLifetime=0 |
| 45 | EOF |
| 46 | fi |
| 47 | |
| 48 | local file |
| 49 | for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/50-deprecated.conf; do |
| 50 | mkdir -p -m 755 "$(dirname "$file")" |
| 51 | if [ -z "$contents" ]; then |
| 52 | rm -f "$file" |
| 53 | else |
| 54 | printf '%s' "$contents" >"$file" |
| 55 | fi |
| 56 | done |
| 57 | |
| 58 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 59 | # currently check the mtime of drop-in files. |
| 60 | touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network |
| 61 | |
| 62 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 63 | networkctl reload && networkctl reconfigure @NCSI_IF@ |
| 64 | fi |
| 65 | |
| 66 | read -r -d '' contents <<EOF |
| 67 | table inet filter { |
| 68 | chain ncsi_input { |
| 69 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input |
| 70 | } |
| 71 | chain ncsi_forward { |
| 72 | ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept |
| 73 | } |
| 74 | } |
| 75 | EOF |
| 76 | rfile=/run/nftables/40-gbmc-ncsi-br.rules |
| 77 | mkdir -p -m 755 "$(dirname "$rfile")" |
| 78 | printf '%s' "$contents" >"$rfile" |
| 79 | systemctl reset-failed nftables && systemctl --no-block restart nftables || true |
| 80 | } |
| 81 | |
| 82 | gbmc_ncsi_br_deprecated_ips_hook() { |
| 83 | if [ "$change" = 'init' ]; then |
| 84 | gbmc_ncsi_br_deprecated_ips_init=1 |
| 85 | gbmc_ip_monitor_defer |
| 86 | elif [ "$change" = 'defer' ]; then |
| 87 | gbmc_ncsi_br_deprecated_ips_update |
| 88 | elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' ] && |
| 89 | [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then |
| 90 | local pfx_bytes=() |
| 91 | ip_to_bytes pfx_bytes "$ip" || return |
| 92 | # No ULA Addresses |
| 93 | if (( pfx_bytes[0] & 0xfe == 0xfc )); then |
| 94 | return |
| 95 | fi |
| 96 | # We only want to allow a <pfx>::fd0x address, where x>0 |
| 97 | if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then |
| 98 | return |
| 99 | fi |
| 100 | for (( i = 10; i < 16; ++i )); do |
| 101 | if (( pfx_bytes[i] != 0 )); then |
| 102 | return |
| 103 | fi |
| 104 | done |
| 105 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 106 | gbmc_ncsi_br_deprecated_ips_lastip="$ip" |
| 107 | gbmc_ip_monitor_defer |
| 108 | fi |
| 109 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_deprecated_ips_lastip" ]; then |
| 110 | gbmc_ncsi_br_deprecated_ips_lastip= |
| 111 | gbmc_ip_monitor_defer |
| 112 | fi |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_deprecated_ips_hook) |
| 117 | |
| 118 | gbmc_ncsi_br_deprecated_ips_lib=1 |