1# 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_br_ula_lib-}" ] || return
16
17source /usr/share/network/lib.sh || exit
18
19gbmc_br_ula_init=
20gbmc_br_ula_mac=
21
22gbmc_br_ula_update() {
23  [ -n "$gbmc_br_ula_init" ] || return
24
25  echo "gBMC Bridge ULA MAC: ${gbmc_br_ula_mac:-(deleted)}" >&2
26
27  local addr=
28  contents='[Network]'$'\n'
29  if [ -n "$gbmc_br_ula_mac" ]; then
30    local sfx
31    if sfx="$(mac_to_eui64 "$gbmc_br_ula_mac")" &&
32       addr="$(ip_pfx_concat "fdb5:0481:10ce::/64" "$sfx")"; then
33      contents+="Address=$addr"$'\n'
34    fi
35  fi
36
37  local netfile
38  for netfile in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/60-ula.conf; do
39    mkdir -p -m 755 "$(dirname "$netfile")"
40    printf '%s' "$contents" >"$netfile"
41  done
42
43  # Ensure that systemd-networkd performs a reconfiguration as it doesn't
44  # currently check the mtime of drop-in files.
45  touch -c /lib/systemd/network/*-bmc-gbmcbr.network
46
47  if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
48    networkctl reload
49    networkctl reconfigure gbmcbr
50  fi
51}
52
53gbmc_br_ula_hook() {
54  if [ "$change" = 'init' ]; then
55    gbmc_br_ula_init=1
56    gbmc_br_ula_update
57  elif [ "$change" = 'link' -a "$intf" = 'gbmcbr' ]; then
58    if [ "$action" = 'add' -a "$mac" != "$gbmc_br_ula_mac" ]; then
59      gbmc_br_ula_mac="$mac"
60      gbmc_br_ula_update
61    fi
62    if [ "$action" = 'del' -a "$mac" = "$gbmc_br_ula_mac" ]; then
63      gbmc_br_ula_mac=
64      gbmc_br_ula_update
65    fi
66  fi
67}
68
69GBMC_IP_MONITOR_HOOKS+=(gbmc_br_ula_hook)
70
71gbmc_br_ula_lib=1
72