blob: e033fd2a5a4870a030c728e40353f768a1408fa9 [file] [log] [blame]
William A. Kennington IIIe99168a2021-03-10 23:40:47 -08001# 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=
19
20gbmc_ncsi_br_pub_addr_update() {
21 [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return
22
23 printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \
24 "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2
25
26 local pfx=
27 if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
28 # Pad the address out to a /64 and ensure that it doesn't have extra bits
29 pfx="${gbmc_ncsi_br_pub_addr_lastip%::}"
30 while true; do
31 # Count `:` in `pfx` by removing them and diffing their lengths
32 local nos="${pfx//:/}"
33 (( ${#pfx} - ${#nos} >= 3 )) && break
34 pfx+=":0"
35 done
36 # Addresses that have more than 64bits of prefix (more than 3 separators)
37 # do not work with this scheme. Ignore them.
38 (( ${#pfx} - ${#nos} == 3 )) || pfx=
39 fi
40
41 local contents='[Network]'$'\n'
42 if [ -n "$pfx" ]; then
43 local here=
44 read -r -d '' here <<EOF
45Address=${pfx}:fd01::/128
46IPv6PrefixDelegation=yes
47[IPv6PrefixDelegation]
48RouterLifetimeSec=60
49[IPv6Prefix]
50Prefix=${pfx}:fd00::/80
51PreferredLifetimeSec=60
52ValidLifetimeSec=60
53[IPv6RoutePrefix]
54Route=${pfx}:fd01::/80
55LifetimeSec=60
56EOF
57 contents+="$here"$'\n'
58 fi
59
60 local file
61 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
62 mkdir -p -m 755 "$(dirname "$file")"
63 printf '%s' "$contents" >"$file"
64 done
65
William A. Kennington III8fb92582021-05-10 03:15:56 -070066 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
67 # currently check the mtime of drop-in files.
68 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
69
70 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
71 networkctl reload
72 networkctl reconfigure gbmcbr
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080073 fi
74}
75
76gbmc_ncsi_br_pub_addr_hook() {
77 if [ "$change" = 'init' ]; then
78 gbmc_ncsi_br_pub_addr_init=1
79 gbmc_ncsi_br_pub_addr_update
80 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
81 [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
82 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
83 gbmc_ncsi_br_pub_addr_lastip="$ip"
84 gbmc_ncsi_br_pub_addr_update
85 fi
86 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
87 gbmc_ncsi_br_pub_addr_lastip=
88 gbmc_ncsi_br_pub_addr_update
89 fi
90 fi
91}
92
93GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
94
95gbmc_ncsi_br_pub_addr_lib=1