William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 1 | #!/bin/bash |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 2 | # Copyright 2021 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 | |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 16 | [[ -n ${gbmc_br_gw_src_lib-} ]] && return |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 17 | |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 18 | # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 19 | source /usr/share/network/lib.sh || exit |
| 20 | |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 21 | gbmc_br_gw_src_ip_stateful= |
| 22 | gbmc_br_gw_src_ip_stateless= |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 23 | declare -A gbmc_br_gw_src_routes=() |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 24 | gbmc_br_gw_defgw= |
| 25 | |
| 26 | gbmc_br_set_router() { |
| 27 | local defgw= |
| 28 | local route |
| 29 | for route in "${!gbmc_br_gw_src_routes[@]}"; do |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 30 | if [[ $route != *' dev gbmcbr '* ]]; then |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 31 | defgw=1 |
| 32 | break |
| 33 | fi |
| 34 | done |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 35 | [[ $defgw == "$gbmc_br_gw_defgw" ]] && return |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 36 | gbmc_br_gw_defgw="$defgw" |
| 37 | |
| 38 | local files=(/run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-defgw.conf) |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 39 | if [[ -n $defgw ]]; then |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 40 | local file |
| 41 | for file in "${files[@]}"; do |
| 42 | mkdir -p "$(dirname "$file")" |
| 43 | printf '[IPv6PrefixDelegation]\nRouterLifetimeSec=30\n' >"$file" |
| 44 | done |
| 45 | else |
| 46 | rm -f "${files[@]}" |
| 47 | fi |
| 48 | |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 49 | if [[ $(systemctl is-active systemd-networkd) != inactive ]]; then |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 50 | networkctl reload && networkctl reconfigure gbmcbr |
| 51 | fi |
| 52 | } |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 53 | |
| 54 | gbmc_br_gw_src_update() { |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 55 | local gbmc_br_gw_src_ip="${gbmc_br_gw_src_ip_stateful:-$gbmc_br_gw_src_ip_stateless}" |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 56 | [[ -n $gbmc_br_gw_src_ip ]] || return |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 57 | |
| 58 | local route |
| 59 | for route in "${!gbmc_br_gw_src_routes[@]}"; do |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 60 | [[ $route != *" src $gbmc_br_gw_src_ip "* ]] || continue |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 61 | echo "gBMC Bridge Updating GW source [$gbmc_br_gw_src_ip]: $route" >&2 |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 62 | # shellcheck disable=SC2086 |
William A. Kennington III | 06ff304 | 2022-05-20 10:40:48 -0700 | [diff] [blame] | 63 | ip route change $route src "$gbmc_br_gw_src_ip" && \ |
| 64 | unset 'gbmc_br_gw_src_routes[$route]' |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 65 | done |
| 66 | } |
| 67 | |
| 68 | gbmc_br_gw_src_hook() { |
| 69 | # We only want to match default gateway routes that are dynamic |
| 70 | # (have an expiration time). These will be updated with our preferred |
| 71 | # source. |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 72 | # shellcheck disable=SC2154 |
| 73 | if [[ $change == route && $route == 'default '*':'* ]]; then |
| 74 | if [[ $route =~ ^(.*)( +expires +[^ ]+)(.*)$ ]]; then |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 75 | route="${BASH_REMATCH[1]}${BASH_REMATCH[3]}" |
| 76 | fi |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 77 | if [[ $action == add && -z ${gbmc_br_gw_src_routes["$route"]} ]]; then |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 78 | gbmc_br_gw_src_routes["$route"]=1 |
| 79 | gbmc_br_gw_src_update |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 80 | gbmc_br_set_router |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 81 | elif [[ $action == del && -n "${gbmc_br_gw_src_routes["$route"]}" ]]; then |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 82 | unset 'gbmc_br_gw_src_routes[$route]' |
| 83 | gbmc_br_gw_src_update |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 84 | gbmc_br_set_router |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 85 | fi |
| 86 | # Match only global IP addresses on the bridge that match the BMC stateless |
| 87 | # prefix (<mpfx>:fd00:). So 2002:af4:3480:2248:fd00:6345:3069:9186 would be |
| 88 | # matched as the preferred source IP for outoging traffic. |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 89 | elif [[ $change == addr && $intf == gbmcbr && $scope == global ]] && |
| 90 | [[ $fam == inet6 && $flags != *tentative* ]]; then |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 91 | local ip_bytes=() |
| 92 | if ! ip_to_bytes ip_bytes "$ip"; then |
| 93 | echo "gBMC Bridge Ensure RA Invalid IP: $ip" >&2 |
| 94 | return 1 |
| 95 | fi |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 96 | # Ignore ULAs and non-gBMC addresses |
| 97 | if (( ip_bytes[0] & 0xfe == 0xfc || ip_bytes[8] != 0xfd )); then |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 98 | return 0 |
| 99 | fi |
Yuxiao Zhang | f540802 | 2023-07-20 10:50:14 -0700 | [diff] [blame] | 100 | if (( ip_bytes[9] & 0x0f != 0 )); then |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 101 | local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateful |
| 102 | else |
| 103 | local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateless |
| 104 | fi |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 105 | if [[ $action == add && $ip != "$gbmc_br_gw_src_ip" ]]; then |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 106 | gbmc_br_gw_src_ip="$ip" |
| 107 | gbmc_br_gw_src_update |
| 108 | fi |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 109 | if [[ $action == del && $ip == "$gbmc_br_gw_src_ip" ]]; then |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 110 | gbmc_br_gw_src_ip= |
| 111 | fi |
| 112 | fi |
| 113 | } |
| 114 | |
| 115 | GBMC_IP_MONITOR_HOOKS+=(gbmc_br_gw_src_hook) |
| 116 | |
| 117 | gbmc_br_gw_src_lib=1 |