blob: 5adc41328a0170b49c21971950c47d4052c211c8 [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 IIIe99168a2021-03-10 23:40:47 -080066 fi
67
68 local file
69 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
70 mkdir -p -m 755 "$(dirname "$file")"
William A. Kennington III03178532021-11-04 22:49:39 -070071 if [ -z "$contents" ]; then
72 rm -f "$file"
73 else
74 printf '%s' "$contents" >"$file"
75 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080076 done
77
William A. Kennington III8fb92582021-05-10 03:15:56 -070078 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
79 # currently check the mtime of drop-in files.
80 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
81
82 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
83 networkctl reload
84 networkctl reconfigure gbmcbr
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080085 fi
86}
87
88gbmc_ncsi_br_pub_addr_hook() {
89 if [ "$change" = 'init' ]; then
90 gbmc_ncsi_br_pub_addr_init=1
William A. Kennington III58ac4342021-11-05 04:15:36 -070091 gbmc_ip_monitor_defer
92 elif [ "$change" = 'defer' ]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080093 gbmc_ncsi_br_pub_addr_update
94 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
William A. Kennington IIIcbd9ef02021-11-04 20:52:17 -070095 [ "$scope" = 'global' -a "$fam" = 'inet6' ] &&
96 [[ "$flags" != *deprecated* ]]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080097 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
98 gbmc_ncsi_br_pub_addr_lastip="$ip"
William A. Kennington III58ac4342021-11-05 04:15:36 -070099 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800100 fi
101 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
102 gbmc_ncsi_br_pub_addr_lastip=
William A. Kennington III58ac4342021-11-05 04:15:36 -0700103 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800104 fi
105 fi
106}
107
108GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
109
110gbmc_ncsi_br_pub_addr_lib=1