blob: 7ccee8e611b09d69ed591cb5505feda62725e40e [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
73 local pfx_bytes=()
74 if ! ip_to_bytes pfx_bytes "$ip"; then
75 echo "Ignoring Invalid IPv6: $ip" >&2
76 gbmc_br_no_ip
77 return 0
78 fi
79
80 local pfx
81 pfx="$(ip_bytes_to_str pfx_bytes)"
82 (( pfx_bytes[9] &= 0xf0 ))
83 local stateless_pfx
84 stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
85 local contents
86 read -r -d '' contents <<EOF
87[Network]
88Address=$pfx/128
89[IPv6Prefix]
90Prefix=$stateless_pfx/80
91PreferredLifetimeSec=60
92ValidLifetimeSec=60
93[IPv6RoutePrefix]
94Route=$pfx/80
95LifetimeSec=60
96[Route]
97Destination=$stateless_pfx/76
98Type=unreachable
99Metric=1024
100EOF
101 echo "Runtime setting gbmcbr IP: $pfx" >&2
102
103 local file
104 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
105 mkdir -p "$(dirname "$file")"
106 printf '%s' "$contents" >"$file"
107 done
108
109 gbmc_br_reload
110}
111
112gbmc_br_set_ip() {
113 local ip="${1-}"
114
115 if [ -n "$ip" ]; then
116 mkdir -p /var/google || return
117 echo "$ip" >/var/google/gbmc-br-ip || return
118 else
119 rm -rf /var/google/gbmc-br-ip
120 fi
121
122 # Remove legacy network configuration
123 rm -rf /etc/systemd/network/{00,}-bmc-gbmcbr.network.d
124
125 gbmc_br_run_hooks GBMC_BR_LIB_SET_IP_HOOKS "$ip" || return
126
127 gbmc_br_reload_ip "$ip"
128}
129
130gbmc_br_lib_init=1
131return 0 2>/dev/null
132echo "gbmc-br-lib is a library, not executed directly" >&2
133exit 1