blob: ac273a3951f0f02bc7adac51e99451b6101401ff [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
30 local eui64
31 eui64="$(mac_to_eui64 "$mac")" || return
32 addr="fdb5:0481:10ce:0:$eui64/64"
33 contents+="Address=$addr"$'\n'
34 fi
35
36 local netfile
37 for netfile in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/60-ula.conf; do
38 mkdir -p -m 755 "$(dirname "$netfile")"
39 printf '%s' "$contents" >"$netfile"
40 done
41
42 # We have to add the address after writing the systemd config to ensure we
43 # don't race with reconfiguration and drop the address.
44 if [ -n "$addr" ]; then
45 ip addr replace "$addr" dev gbmcbr
46 fi
47}
48
49gbmc_br_ula_hook() {
50 if [ "$change" = 'init' ]; then
51 gbmc_br_ula_init=1
52 gbmc_br_ula_update
53 elif [ "$change" = 'link' -a "$intf" = 'gbmcbr' ]; then
54 if [ "$action" = 'add' -a "$mac" != "$gbmc_br_ula_mac" ]; then
55 gbmc_br_ula_mac="$mac"
56 gbmc_br_ula_update
57 fi
58 if [ "$action" = 'del' -a "$mac" = "$gbmc_br_ula_mac" ]; then
59 gbmc_br_ula_mac=
60 gbmc_br_ula_update
61 fi
62 fi
63}
64
65GBMC_IP_MONITOR_HOOKS+=(gbmc_br_ula_hook)
66
67gbmc_br_ula_lib=1