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