blob: 42bb566c8afa9e4c7b8a5b93d8477682a2e78b09 [file] [log] [blame]
William A. Kennington III5206f662023-06-05 16:31:37 -07001#!/bin/bash
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -07002# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
William A. Kennington III5206f662023-06-05 16:31:37 -070016[[ -n ${gbmc_br_ula_lib-} ]] && return
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070017
William A. Kennington III5206f662023-06-05 16:31:37 -070018# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070019source /usr/share/network/lib.sh || exit
20
21gbmc_br_ula_init=
22gbmc_br_ula_mac=
23
24gbmc_br_ula_update() {
William A. Kennington III5206f662023-06-05 16:31:37 -070025 [[ -n $gbmc_br_ula_init ]] || return
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070026
27 echo "gBMC Bridge ULA MAC: ${gbmc_br_ula_mac:-(deleted)}" >&2
28
29 local addr=
30 contents='[Network]'$'\n'
William A. Kennington III5206f662023-06-05 16:31:37 -070031 if [[ -n $gbmc_br_ula_mac ]]; then
William A. Kennington III6ca70332021-05-10 03:14:42 -070032 local sfx
33 if sfx="$(mac_to_eui64 "$gbmc_br_ula_mac")" &&
34 addr="$(ip_pfx_concat "fdb5:0481:10ce::/64" "$sfx")"; then
35 contents+="Address=$addr"$'\n'
36 fi
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070037 fi
38
39 local netfile
40 for netfile in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/60-ula.conf; do
William A. Kennington III5206f662023-06-05 16:31:37 -070041 mkdir -p "$(dirname "$netfile")"
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070042 printf '%s' "$contents" >"$netfile"
43 done
44
William A. Kennington III8fb92582021-05-10 03:15:56 -070045 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
46 # currently check the mtime of drop-in files.
47 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
48
William A. Kennington III5206f662023-06-05 16:31:37 -070049 if [[ $(systemctl is-active systemd-networkd) != inactive ]]; then
William A. Kennington III8fb92582021-05-10 03:15:56 -070050 networkctl reload
51 networkctl reconfigure gbmcbr
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070052 fi
53}
54
55gbmc_br_ula_hook() {
William A. Kennington III5206f662023-06-05 16:31:37 -070056 # shellcheck disable=SC2154
57 if [[ $change == init ]]; then
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070058 gbmc_br_ula_init=1
59 gbmc_br_ula_update
William A. Kennington III5206f662023-06-05 16:31:37 -070060 elif [[ $change == link && $intf == gbmcbr ]]; then
61 if [[ $action == add && $mac != "$gbmc_br_ula_mac" ]]; then
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070062 gbmc_br_ula_mac="$mac"
63 gbmc_br_ula_update
64 fi
William A. Kennington III5206f662023-06-05 16:31:37 -070065 if [[ $action == del && $mac == "$gbmc_br_ula_mac" ]]; then
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070066 gbmc_br_ula_mac=
67 gbmc_br_ula_update
68 fi
69 fi
70}
71
72GBMC_IP_MONITOR_HOOKS+=(gbmc_br_ula_hook)
73
74gbmc_br_ula_lib=1