blob: 961da50959b09343ce7996eab539ea25376e527b [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
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070026 local pfx_bytes=()
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080027 if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070028 ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip"
29 # Ensure we don't have more than a /64 address
30 local i
31 for (( i = 8; i < 16; ++i )); do
32 if (( pfx_bytes[$i] != 0 )); then
33 pfx_bytes=()
34 break
35 fi
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080036 done
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080037 fi
38
39 local contents='[Network]'$'\n'
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070040 if (( ${#pfx_bytes[@]} != 0 )); then
41 pfx_bytes[8]=0xfd
42 local stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
43 pfx_bytes[9]=0x01
44 local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080045 local here=
46 read -r -d '' here <<EOF
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070047Address=$ncsi_pfx/128
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080048IPv6PrefixDelegation=yes
49[IPv6PrefixDelegation]
50RouterLifetimeSec=60
51[IPv6Prefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070052Prefix=$stateless_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080053PreferredLifetimeSec=60
54ValidLifetimeSec=60
55[IPv6RoutePrefix]
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070056Route=$ncsi_pfx/80
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080057LifetimeSec=60
William A. Kennington IIIc1a3cc22021-05-12 13:32:38 -070058[Route]
59Destination=$stateless_pfx/72
60Type=unreachable
61Metric=1024
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080062EOF
63 contents+="$here"$'\n'
64 fi
65
66 local file
67 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
68 mkdir -p -m 755 "$(dirname "$file")"
69 printf '%s' "$contents" >"$file"
70 done
71
William A. Kennington III8fb92582021-05-10 03:15:56 -070072 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
73 # currently check the mtime of drop-in files.
74 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
75
76 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
77 networkctl reload
78 networkctl reconfigure gbmcbr
William A. Kennington IIIe99168a2021-03-10 23:40:47 -080079 fi
80}
81
82gbmc_ncsi_br_pub_addr_hook() {
83 if [ "$change" = 'init' ]; then
84 gbmc_ncsi_br_pub_addr_init=1
85 gbmc_ncsi_br_pub_addr_update
86 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
87 [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then
88 if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
89 gbmc_ncsi_br_pub_addr_lastip="$ip"
90 gbmc_ncsi_br_pub_addr_update
91 fi
92 if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
93 gbmc_ncsi_br_pub_addr_lastip=
94 gbmc_ncsi_br_pub_addr_update
95 fi
96 fi
97}
98
99GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)
100
101gbmc_ncsi_br_pub_addr_lib=1