blob: f51b033d54446e9e251e4ede819486987c63fd70 [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
66 # We only restart networkd if we know we have a management network available
67 # on the machine and networkd is already running.
68 if [ -e /lib/systemd/network/-bmc-gbmcbrdummy.network ] && \
69 ! systemctl status systemd-networkd | grep -q inactive; then
70 echo "Restarting networkd" >&2
71 # HACK: We can't restart systemd-networkd without coordinating with
72 # phosphor-networkd, otherwise it will sometimes detect interfaces as
73 # unmanaged because it reads administrative state to determine enabled
74 # status. Adding an IP to phosphor-networkd is guaranteed to trigger the
75 # restart we want, and systemd-network will never actually accept the
76 # new value.
77 local start=$SECONDS
78 while (( SECONDS - start < 30 )); do
79 busctl call xyz.openbmc_project.Network \
80 /xyz/openbmc_project/network/gbmcbrdummy \
81 xyz.openbmc_project.Network.IP.Create IP ssys \
82 xyz.openbmc_project.Network.IP.Protocol.IPv6 ff02::1 128 '' && break
83 sleep 1
84 done
85 fi
86}
87
88gbmc_ncsi_br_pub_addr_hook() {
89 if [ "$change" = 'init' ]; then
90 gbmc_ncsi_br_pub_addr_init=1
91 gbmc_ncsi_br_pub_addr_update
92 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
93 [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
94 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
95 gbmc_ncsi_br_pub_addr_lastip="$ip"
96 gbmc_ncsi_br_pub_addr_update
97 fi
98 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
99 gbmc_ncsi_br_pub_addr_lastip=
100 gbmc_ncsi_br_pub_addr_update
101 fi
102 fi
103}
104
105GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
106
107gbmc_ncsi_br_pub_addr_lib=1