blob: 1b31ef9f6ffee40fb58fe0cf5f6b161d64ab2f5d [file] [log] [blame]
William A. Kennington III37e6f392022-05-16 16:19:11 -07001#!/bin/bash
2# Copyright 2022 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
16[ -n "${gbmc_br_lib_init-}" ] && return
17
18# SC can't find this path during repotest
19# shellcheck disable=SC1091
20source /usr/share/network/lib.sh || exit
21
22# A list of functions which get executed for each configured IP.
23# These are configured by the files included below.
24# Shellcheck does not understand how this gets referenced
25# shellcheck disable=SC2034
26GBMC_BR_LIB_SET_IP_HOOKS=()
27
28gbmc_br_source_dir() {
29 local dir="$1"
30
31 local file
32 while read -r -d $'\0' file; do
33 # SC doesn't like dynamic source loading
34 # shellcheck disable=SC1090
35 source "$file" || return
36 done < <(shopt -s nullglob; for f in "$dir"/*.sh; do printf '%s\0' "$f"; done)
37}
38
39# Load configurations from a known location in the filesystem to populate
40# hooks that are executed after each event.
41gbmc_br_source_dir /usr/share/gbmc-br-lib || exit
42
43gbmc_br_run_hooks() {
44 local -n hookvar="$1"
45 shift
46 local hook
47 for hook in "${hookvar[@]}"; do
48 "$hook" "$@" || return
49 done
50}
51
52gbmc_br_reload() {
53 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
54 networkctl reload && networkctl reconfigure gbmcbr
55 fi
56}
57
58gbmc_br_no_ip() {
59 echo "Runtime removing gbmcbr IP" >&2
60 rm -f /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf
61 gbmc_br_reload
62}
63
64gbmc_br_reload_ip() {
65 local ip="${1-}"
66
67 if [ -z "$ip" ] && ! ip="$(cat /var/google/gbmc-br-ip 2>/dev/null)"; then
68 echo "Ignoring unconfigured IP" >&2
69 gbmc_br_no_ip
70 return 0
71 fi
72
Yuxiao Zhang4382c5e2023-05-05 16:10:16 -070073 # Remove legacy network configuration
74 rm -rf /etc/systemd/network/{00,}-bmc-gbmcbr.network.d
75
William A. Kennington III37e6f392022-05-16 16:19:11 -070076 local pfx_bytes=()
77 if ! ip_to_bytes pfx_bytes "$ip"; then
78 echo "Ignoring Invalid IPv6: $ip" >&2
79 gbmc_br_no_ip
80 return 0
81 fi
82
83 local pfx
84 pfx="$(ip_bytes_to_str pfx_bytes)"
85 (( pfx_bytes[9] &= 0xf0 ))
86 local stateless_pfx
87 stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
88 local contents
89 read -r -d '' contents <<EOF
90[Network]
91Address=$pfx/128
92[IPv6Prefix]
93Prefix=$stateless_pfx/80
94PreferredLifetimeSec=60
95ValidLifetimeSec=60
96[IPv6RoutePrefix]
97Route=$pfx/80
98LifetimeSec=60
99[Route]
100Destination=$stateless_pfx/76
101Type=unreachable
102Metric=1024
103EOF
104 echo "Runtime setting gbmcbr IP: $pfx" >&2
105
106 local file
107 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
108 mkdir -p "$(dirname "$file")"
109 printf '%s' "$contents" >"$file"
110 done
111
112 gbmc_br_reload
113}
114
115gbmc_br_set_ip() {
116 local ip="${1-}"
Yuxiao Zhang4382c5e2023-05-05 16:10:16 -0700117 local old_ip=
William A. Kennington III37e6f392022-05-16 16:19:11 -0700118 if [ -n "$ip" ]; then
Yuxiao Zhang4382c5e2023-05-05 16:10:16 -0700119 old_ip="$(cat /var/google/gbmc-br-ip 2>/dev/null)"
Yuxiao Zhangb9ccc8b2023-05-11 14:04:29 -0700120 [ "$old_ip" == "$ip" ] && return
William A. Kennington III37e6f392022-05-16 16:19:11 -0700121 mkdir -p /var/google || return
122 echo "$ip" >/var/google/gbmc-br-ip || return
123 else
Yuxiao Zhangb9ccc8b2023-05-11 14:04:29 -0700124 [ ! -f "/var/google/gbmc-br-ip" ] && return
William A. Kennington III37e6f392022-05-16 16:19:11 -0700125 rm -rf /var/google/gbmc-br-ip
126 fi
127
William A. Kennington III37e6f392022-05-16 16:19:11 -0700128 gbmc_br_run_hooks GBMC_BR_LIB_SET_IP_HOOKS "$ip" || return
129
130 gbmc_br_reload_ip "$ip"
131}
132
133gbmc_br_lib_init=1