blob: 1992dd1a69fb83a07c5fb04d3515b3f0f96bb0e1 [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
William A. Kennington III37e6f392022-05-16 16:19:11 -070015[ -n "${gbmc_ncsi_br_pub_addr_lib-}" ] && return
16
17[ ! -e /usr/share/gbmc-br-lib.sh ] && return
18
19source /usr/share/network/lib.sh || exit
20source /usr/share/gbmc-br-lib.sh || exit
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080021
22gbmc_ncsi_br_pub_addr_init=
23gbmc_ncsi_br_pub_addr_lastip=
William A. Kennington III58ac4342021-11-05 04:15:36 -070024gbmc_ncsi_br_pub_addr_confip=
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080025
26gbmc_ncsi_br_pub_addr_update() {
27 [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return
William A. Kennington III58ac4342021-11-05 04:15:36 -070028 [ "$gbmc_ncsi_br_pub_addr_confip" != "$gbmc_ncsi_br_pub_addr_lastip" ] || return
29 gbmc_ncsi_br_pub_addr_confip="$gbmc_ncsi_br_pub_addr_lastip"
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080030
31 printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \
32 "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2
33
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070034 local pfx_bytes=()
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080035 if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070036 ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip"
William A. Kennington III10dc43a2022-05-10 10:30:25 -070037 # Ensure we have a /64 or an fdxx address
38 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] == 0 )); then
39 local i
40 for (( i = 8; i < 16; ++i )); do
41 if (( pfx_bytes[$i] != 0 )); then
42 pfx_bytes=()
43 break
44 fi
45 done
46 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080047 fi
48
William A. Kennington III03178532021-11-04 22:49:39 -070049 local contents=
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070050 if (( ${#pfx_bytes[@]} != 0 )); then
51 pfx_bytes[8]=0xfd
William A. Kennington III37e6f392022-05-16 16:19:11 -070052 # We never want to use the stateless pfx
53 if (( pfx_bytes[9] == 0 )); then
54 pfx_bytes[9]=0x01
William A. Kennington III10dc43a2022-05-10 10:30:25 -070055 fi
William A. Kennington III37e6f392022-05-16 16:19:11 -070056 # Remove any existing persisted IP
57 gbmc_br_set_ip
58 # Load the IP to the bridge non-persistently
59 gbmc_br_reload_ip "$(ip_bytes_to_str pfx_bytes)"
60 else
61 gbmc_br_reload_ip
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080062 fi
63}
64
65gbmc_ncsi_br_pub_addr_hook() {
66 if [ "$change" = 'init' ]; then
67 gbmc_ncsi_br_pub_addr_init=1
William A. Kennington III58ac4342021-11-05 04:15:36 -070068 gbmc_ip_monitor_defer
69 elif [ "$change" = 'defer' ]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080070 gbmc_ncsi_br_pub_addr_update
71 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
William A. Kennington IIIcbd9ef02021-11-04 20:52:17 -070072 [ "$scope" = 'global' -a "$fam" = 'inet6' ] &&
73 [[ "$flags" != *deprecated* ]]; then
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080074 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
75 gbmc_ncsi_br_pub_addr_lastip="$ip"
William A. Kennington III58ac4342021-11-05 04:15:36 -070076 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080077 fi
78 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
79 gbmc_ncsi_br_pub_addr_lastip=
William A. Kennington III58ac4342021-11-05 04:15:36 -070080 gbmc_ip_monitor_defer
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080081 fi
82 fi
83}
84
85GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
86
87gbmc_ncsi_br_pub_addr_lib=1