blob: 793403348eff7fc26e971fa2d862d48ae4c20b06 [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=
William A. Kennington III58ac4342021-11-05 04:15:36 -070019gbmc_ncsi_br_pub_addr_confip=
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080020
21gbmc_ncsi_br_pub_addr_update() {
22 [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return
William A. Kennington III58ac4342021-11-05 04:15:36 -070023 [ "$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"
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080025
26 printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \
27 "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2
28
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070029 local pfx_bytes=()
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080030 if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070031 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
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080039 done
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080040 fi
41
William A. Kennington III03178532021-11-04 22:49:39 -070042 local contents=
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070043 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)"
William A. Kennington III03178532021-11-04 22:49:39 -070048 read -r -d '' contents <<EOF
49[Network]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070050Address=$ncsi_pfx/128
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080051IPv6PrefixDelegation=yes
52[IPv6PrefixDelegation]
53RouterLifetimeSec=60
54[IPv6Prefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070055Prefix=$stateless_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080056PreferredLifetimeSec=60
57ValidLifetimeSec=60
58[IPv6RoutePrefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070059Route=$ncsi_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080060LifetimeSec=60
William A. Kennington IIIc1a3cc22021-05-12 13:32:38 -070061[Route]
William A. Kennington IIIa22b4452021-11-04 22:57:43 -070062Destination=$stateless_pfx/76
William A. Kennington IIIc1a3cc22021-05-12 13:32:38 -070063Type=unreachable
64Metric=1024
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080065EOF
William A. Kennington IIIb174c182021-11-03 14:54:51 -070066 # 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
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080068 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")"
William A. Kennington III03178532021-11-04 22:49:39 -070073 if [ -z "$contents" ]; then
74 rm -f "$file"
75 else
76 printf '%s' "$contents" >"$file"
77 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080078 done
79
William A. Kennington III8fb92582021-05-10 03:15:56 -070080 # 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
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080087 fi
88}
89
90gbmc_ncsi_br_pub_addr_hook() {
91 if [ "$change" = 'init' ]; then
92 gbmc_ncsi_br_pub_addr_init=1
William A. Kennington III58ac4342021-11-05 04:15:36 -070093 gbmc_ip_monitor_defer
94 elif [ "$change" = 'defer' ]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080095 gbmc_ncsi_br_pub_addr_update
96 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
William A. Kennington IIIcbd9ef02021-11-04 20:52:17 -070097 [ "$scope" = 'global' -a "$fam" = 'inet6' ] &&
98 [[ "$flags" != *deprecated* ]]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080099 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
100 gbmc_ncsi_br_pub_addr_lastip="$ip"
William A. Kennington III58ac4342021-11-05 04:15:36 -0700101 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800102 fi
103 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
104 gbmc_ncsi_br_pub_addr_lastip=
William A. Kennington III58ac4342021-11-05 04:15:36 -0700105 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800106 fi
107 fi
108}
109
110GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
111
112gbmc_ncsi_br_pub_addr_lib=1