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_pub_addr_lib-}" ] || return 16 17gbmc_ncsi_br_pub_addr_init= 18gbmc_ncsi_br_pub_addr_lastip= 19gbmc_ncsi_br_pub_addr_confip= 20 21gbmc_ncsi_br_pub_addr_update() { 22 [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return 23 [ "$gbmc_ncsi_br_pub_addr_confip" != "$gbmc_ncsi_br_pub_addr_lastip" ] || return 24 gbmc_ncsi_br_pub_addr_confip="$gbmc_ncsi_br_pub_addr_lastip" 25 26 printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \ 27 "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2 28 29 local pfx_bytes=() 30 if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then 31 ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip" 32 # Ensure we don't have more than a /64 address 33 local i 34 for (( i = 8; i < 16; ++i )); do 35 if (( pfx_bytes[$i] != 0 )); then 36 pfx_bytes=() 37 break 38 fi 39 done 40 fi 41 42 local contents= 43 if (( ${#pfx_bytes[@]} != 0 )); then 44 pfx_bytes[8]=0xfd 45 local stateless_pfx="$(ip_bytes_to_str pfx_bytes)" 46 pfx_bytes[9]=0x01 47 local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)" 48 read -r -d '' contents <<EOF 49[Network] 50Address=$ncsi_pfx/128 51IPv6PrefixDelegation=yes 52[IPv6PrefixDelegation] 53RouterLifetimeSec=60 54[IPv6Prefix] 55Prefix=$stateless_pfx/80 56PreferredLifetimeSec=60 57ValidLifetimeSec=60 58[IPv6RoutePrefix] 59Route=$ncsi_pfx/80 60LifetimeSec=60 61[Route] 62Destination=$stateless_pfx/76 63Type=unreachable 64Metric=1024 65EOF 66 # Delete DHCP configured addresses if we have a host published address 67 rm -f /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf 68 fi 69 70 local file 71 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do 72 mkdir -p -m 755 "$(dirname "$file")" 73 if [ -z "$contents" ]; then 74 rm -f "$file" 75 else 76 printf '%s' "$contents" >"$file" 77 fi 78 done 79 80 # Ensure that systemd-networkd performs a reconfiguration as it doesn't 81 # currently check the mtime of drop-in files. 82 touch -c /lib/systemd/network/*-bmc-gbmcbr.network 83 84 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then 85 networkctl reload 86 networkctl reconfigure gbmcbr 87 fi 88} 89 90gbmc_ncsi_br_pub_addr_hook() { 91 if [ "$change" = 'init' ]; then 92 gbmc_ncsi_br_pub_addr_init=1 93 gbmc_ip_monitor_defer 94 elif [ "$change" = 'defer' ]; then 95 gbmc_ncsi_br_pub_addr_update 96 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && 97 [ "$scope" = 'global' -a "$fam" = 'inet6' ] && 98 [[ "$flags" != *deprecated* ]]; then 99 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then 100 gbmc_ncsi_br_pub_addr_lastip="$ip" 101 gbmc_ip_monitor_defer 102 fi 103 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then 104 gbmc_ncsi_br_pub_addr_lastip= 105 gbmc_ip_monitor_defer 106 fi 107 fi 108} 109 110GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) 111 112gbmc_ncsi_br_pub_addr_lib=1 113