blob: 9f008a9a44783928e114013b11355905faa3e14a [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"
William A. Kennington III10dc43a2022-05-10 10:30:25 -070032 # Ensure we have a /64 or an fdxx address
33 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] == 0 )); then
34 local i
35 for (( i = 8; i < 16; ++i )); do
36 if (( pfx_bytes[$i] != 0 )); then
37 pfx_bytes=()
38 break
39 fi
40 done
41 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080042 fi
43
William A. Kennington III03178532021-11-04 22:49:39 -070044 local contents=
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070045 if (( ${#pfx_bytes[@]} != 0 )); then
46 pfx_bytes[8]=0xfd
William A. Kennington III10dc43a2022-05-10 10:30:25 -070047 # Save our old prefix assuming we have one
48 local old_offset="${pfx_bytes[9]}"
49 if (( old_offset == 0 )); then
50 old_offset=0x01
51 else
52 pfx_bytes[9]=0x00
53 fi
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070054 local stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III10dc43a2022-05-10 10:30:25 -070055 pfx_bytes[9]="$old_offset"
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070056 local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III03178532021-11-04 22:49:39 -070057 read -r -d '' contents <<EOF
58[Network]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070059Address=$ncsi_pfx/128
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080060IPv6PrefixDelegation=yes
61[IPv6PrefixDelegation]
62RouterLifetimeSec=60
63[IPv6Prefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070064Prefix=$stateless_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080065PreferredLifetimeSec=60
66ValidLifetimeSec=60
67[IPv6RoutePrefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070068Route=$ncsi_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080069LifetimeSec=60
William A. Kennington IIIc1a3cc22021-05-12 13:32:38 -070070[Route]
William A. Kennington IIIa22b4452021-11-04 22:57:43 -070071Destination=$stateless_pfx/76
William A. Kennington IIIc1a3cc22021-05-12 13:32:38 -070072Type=unreachable
73Metric=1024
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080074EOF
William A. Kennington IIIb174c182021-11-03 14:54:51 -070075 # Delete DHCP configured addresses if we have a host published address
76 rm -f /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080077 fi
78
79 local file
80 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
81 mkdir -p -m 755 "$(dirname "$file")"
William A. Kennington III03178532021-11-04 22:49:39 -070082 if [ -z "$contents" ]; then
83 rm -f "$file"
84 else
85 printf '%s' "$contents" >"$file"
86 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080087 done
88
William A. Kennington III8fb92582021-05-10 03:15:56 -070089 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
90 # currently check the mtime of drop-in files.
91 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
92
93 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
94 networkctl reload
95 networkctl reconfigure gbmcbr
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080096 fi
97}
98
99gbmc_ncsi_br_pub_addr_hook() {
100 if [ "$change" = 'init' ]; then
101 gbmc_ncsi_br_pub_addr_init=1
William A. Kennington III58ac4342021-11-05 04:15:36 -0700102 gbmc_ip_monitor_defer
103 elif [ "$change" = 'defer' ]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800104 gbmc_ncsi_br_pub_addr_update
105 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
William A. Kennington IIIcbd9ef02021-11-04 20:52:17 -0700106 [ "$scope" = 'global' -a "$fam" = 'inet6' ] &&
107 [[ "$flags" != *deprecated* ]]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800108 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
109 gbmc_ncsi_br_pub_addr_lastip="$ip"
William A. Kennington III58ac4342021-11-05 04:15:36 -0700110 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800111 fi
112 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
113 gbmc_ncsi_br_pub_addr_lastip=
William A. Kennington III58ac4342021-11-05 04:15:36 -0700114 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -0800115 fi
116 fi
117}
118
119GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
120
121gbmc_ncsi_br_pub_addr_lib=1