William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 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 | # SC can't find this path during repotest |
| 19 | # shellcheck disable=SC1091 |
| 20 | source /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 |
| 26 | GBMC_BR_LIB_SET_IP_HOOKS=() |
| 27 | |
| 28 | gbmc_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. |
| 41 | gbmc_br_source_dir /usr/share/gbmc-br-lib || exit |
| 42 | |
| 43 | gbmc_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 | |
| 52 | gbmc_br_reload() { |
| 53 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 54 | networkctl reload && networkctl reconfigure gbmcbr |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | gbmc_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 | |
| 64 | gbmc_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 Zhang | 4382c5e | 2023-05-05 16:10:16 -0700 | [diff] [blame] | 73 | # Remove legacy network configuration |
| 74 | rm -rf /etc/systemd/network/{00,}-bmc-gbmcbr.network.d |
| 75 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 76 | 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] |
| 91 | Address=$pfx/128 |
| 92 | [IPv6Prefix] |
| 93 | Prefix=$stateless_pfx/80 |
| 94 | PreferredLifetimeSec=60 |
| 95 | ValidLifetimeSec=60 |
| 96 | [IPv6RoutePrefix] |
| 97 | Route=$pfx/80 |
| 98 | LifetimeSec=60 |
| 99 | [Route] |
| 100 | Destination=$stateless_pfx/76 |
| 101 | Type=unreachable |
| 102 | Metric=1024 |
| 103 | EOF |
| 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 | |
| 115 | gbmc_br_set_ip() { |
| 116 | local ip="${1-}" |
Yuxiao Zhang | 4382c5e | 2023-05-05 16:10:16 -0700 | [diff] [blame] | 117 | local old_ip= |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 118 | if [ -n "$ip" ]; then |
Yuxiao Zhang | 4382c5e | 2023-05-05 16:10:16 -0700 | [diff] [blame] | 119 | old_ip="$(cat /var/google/gbmc-br-ip 2>/dev/null)" |
Yuxiao Zhang | b9ccc8b | 2023-05-11 14:04:29 -0700 | [diff] [blame] | 120 | [ "$old_ip" == "$ip" ] && return |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 121 | mkdir -p /var/google || return |
| 122 | echo "$ip" >/var/google/gbmc-br-ip || return |
| 123 | else |
Yuxiao Zhang | b9ccc8b | 2023-05-11 14:04:29 -0700 | [diff] [blame] | 124 | [ ! -f "/var/google/gbmc-br-ip" ] && return |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 125 | rm -rf /var/google/gbmc-br-ip |
| 126 | fi |
| 127 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 128 | gbmc_br_run_hooks GBMC_BR_LIB_SET_IP_HOOKS "$ip" || return |
| 129 | |
| 130 | gbmc_br_reload_ip "$ip" |
| 131 | } |
| 132 | |
| 133 | gbmc_br_lib_init=1 |