blob: e033fd2a5a4870a030c728e40353f768a1408fa9 [file] [log] [blame]
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[ -z "${gbmc_ncsi_br_pub_addr_lib-}" ] || return
gbmc_ncsi_br_pub_addr_init=
gbmc_ncsi_br_pub_addr_lastip=
gbmc_ncsi_br_pub_addr_update() {
[ -n "$gbmc_ncsi_br_pub_addr_init" ] || return
printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \
"${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2
local pfx=
if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
# Pad the address out to a /64 and ensure that it doesn't have extra bits
pfx="${gbmc_ncsi_br_pub_addr_lastip%::}"
while true; do
# Count `:` in `pfx` by removing them and diffing their lengths
local nos="${pfx//:/}"
(( ${#pfx} - ${#nos} >= 3 )) && break
pfx+=":0"
done
# Addresses that have more than 64bits of prefix (more than 3 separators)
# do not work with this scheme. Ignore them.
(( ${#pfx} - ${#nos} == 3 )) || pfx=
fi
local contents='[Network]'$'\n'
if [ -n "$pfx" ]; then
local here=
read -r -d '' here <<EOF
Address=${pfx}:fd01::/128
IPv6PrefixDelegation=yes
[IPv6PrefixDelegation]
RouterLifetimeSec=60
[IPv6Prefix]
Prefix=${pfx}:fd00::/80
PreferredLifetimeSec=60
ValidLifetimeSec=60
[IPv6RoutePrefix]
Route=${pfx}:fd01::/80
LifetimeSec=60
EOF
contents+="$here"$'\n'
fi
local file
for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
mkdir -p -m 755 "$(dirname "$file")"
printf '%s' "$contents" >"$file"
done
# Ensure that systemd-networkd performs a reconfiguration as it doesn't
# currently check the mtime of drop-in files.
touch -c /lib/systemd/network/*-bmc-gbmcbr.network
if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
networkctl reload
networkctl reconfigure gbmcbr
fi
}
gbmc_ncsi_br_pub_addr_hook() {
if [ "$change" = 'init' ]; then
gbmc_ncsi_br_pub_addr_init=1
gbmc_ncsi_br_pub_addr_update
elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
[ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
gbmc_ncsi_br_pub_addr_lastip="$ip"
gbmc_ncsi_br_pub_addr_update
fi
if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
gbmc_ncsi_br_pub_addr_lastip=
gbmc_ncsi_br_pub_addr_update
fi
fi
}
GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
gbmc_ncsi_br_pub_addr_lib=1