blob: 69897100e36ed30c52602e6a8ac777bace076a86 [file] [log] [blame]
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -07001# 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
William A. Kennington III6ca70332021-05-10 03:14:42 -070030 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
William A. Kennington IIIb08a9e62021-04-26 12:43:43 -070035 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 # We have to add the address after writing the systemd config to ensure we
44 # don't race with reconfiguration and drop the address.
45 if [ -n "$addr" ]; then
46 ip addr replace "$addr" dev gbmcbr
47 fi
48}
49
50gbmc_br_ula_hook() {
51 if [ "$change" = 'init' ]; then
52 gbmc_br_ula_init=1
53 gbmc_br_ula_update
54 elif [ "$change" = 'link' -a "$intf" = 'gbmcbr' ]; then
55 if [ "$action" = 'add' -a "$mac" != "$gbmc_br_ula_mac" ]; then
56 gbmc_br_ula_mac="$mac"
57 gbmc_br_ula_update
58 fi
59 if [ "$action" = 'del' -a "$mac" = "$gbmc_br_ula_mac" ]; then
60 gbmc_br_ula_mac=
61 gbmc_br_ula_update
62 fi
63 fi
64}
65
66GBMC_IP_MONITOR_HOOKS+=(gbmc_br_ula_hook)
67
68gbmc_br_ula_lib=1