William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 1 | # Copyright 2021 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 15 | [ -n "${gbmc_br_gw_src_lib-}" ] && return |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 16 | |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 17 | source /usr/share/network/lib.sh || exit |
| 18 | |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 19 | gbmc_br_gw_src_ip_stateful= |
| 20 | gbmc_br_gw_src_ip_stateless= |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 21 | declare -A gbmc_br_gw_src_routes=() |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 22 | gbmc_br_gw_defgw= |
| 23 | |
| 24 | gbmc_br_set_router() { |
| 25 | local defgw= |
| 26 | local route |
| 27 | for route in "${!gbmc_br_gw_src_routes[@]}"; do |
| 28 | if [[ "$route" != *' dev gbmcbr '* ]]; then |
| 29 | defgw=1 |
| 30 | break |
| 31 | fi |
| 32 | done |
| 33 | [ "$defgw" = "$gbmc_br_gw_defgw" ] && return |
| 34 | gbmc_br_gw_defgw="$defgw" |
| 35 | |
| 36 | local files=(/run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-defgw.conf) |
| 37 | if [ -n "$defgw" ]; then |
| 38 | local file |
| 39 | for file in "${files[@]}"; do |
| 40 | mkdir -p "$(dirname "$file")" |
| 41 | printf '[IPv6PrefixDelegation]\nRouterLifetimeSec=30\n' >"$file" |
| 42 | done |
| 43 | else |
| 44 | rm -f "${files[@]}" |
| 45 | fi |
| 46 | |
| 47 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 48 | networkctl reload && networkctl reconfigure gbmcbr |
| 49 | fi |
| 50 | } |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 51 | |
| 52 | gbmc_br_gw_src_update() { |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 53 | local gbmc_br_gw_src_ip="${gbmc_br_gw_src_ip_stateful:-$gbmc_br_gw_src_ip_stateless}" |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 54 | [ -n "$gbmc_br_gw_src_ip" ] || return |
| 55 | |
| 56 | local route |
| 57 | for route in "${!gbmc_br_gw_src_routes[@]}"; do |
| 58 | [[ "$route" != *" src $gbmc_br_gw_src_ip "* ]] || continue |
| 59 | echo "gBMC Bridge Updating GW source [$gbmc_br_gw_src_ip]: $route" >&2 |
William A. Kennington III | 06ff304 | 2022-05-20 10:40:48 -0700 | [diff] [blame] | 60 | ip route change $route src "$gbmc_br_gw_src_ip" && \ |
| 61 | unset 'gbmc_br_gw_src_routes[$route]' |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 62 | done |
| 63 | } |
| 64 | |
| 65 | gbmc_br_gw_src_hook() { |
| 66 | # We only want to match default gateway routes that are dynamic |
| 67 | # (have an expiration time). These will be updated with our preferred |
| 68 | # source. |
| 69 | if [[ "$change" == 'route' && "$route" == 'default '*':'* ]]; then |
| 70 | if [[ "$route" =~ ^(.*)( +expires +[^ ]+)(.*)$ ]]; then |
| 71 | route="${BASH_REMATCH[1]}${BASH_REMATCH[3]}" |
| 72 | fi |
| 73 | if [ "$action" = 'add' -a -z "${gbmc_br_gw_src_routes["$route"]}" ]; then |
| 74 | gbmc_br_gw_src_routes["$route"]=1 |
| 75 | gbmc_br_gw_src_update |
William A. Kennington III | e70461f | 2022-05-20 10:11:57 -0700 | [diff] [blame] | 76 | gbmc_br_set_router |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 77 | elif [ "$action" = 'del' -a -n "${gbmc_br_gw_src_routes["$route"]}" ]; then |
| 78 | unset 'gbmc_br_gw_src_routes[$route]' |
| 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 | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 81 | fi |
| 82 | # Match only global IP addresses on the bridge that match the BMC stateless |
| 83 | # prefix (<mpfx>:fd00:). So 2002:af4:3480:2248:fd00:6345:3069:9186 would be |
| 84 | # matched as the preferred source IP for outoging traffic. |
| 85 | elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] && |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 86 | [[ "$fam" == 'inet6' && "$flags" != *tentative* ]]; then |
| 87 | local ip_bytes=() |
| 88 | if ! ip_to_bytes ip_bytes "$ip"; then |
| 89 | echo "gBMC Bridge Ensure RA Invalid IP: $ip" >&2 |
| 90 | return 1 |
| 91 | fi |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 92 | # Ignore ULAs and non-gBMC addresses |
| 93 | 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] | 94 | return 0 |
| 95 | fi |
William A. Kennington III | 3b127c4 | 2022-05-20 13:30:21 -0700 | [diff] [blame] | 96 | if (( ip_bytes[9] != 0 )); then |
| 97 | local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateful |
| 98 | else |
| 99 | local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateless |
| 100 | fi |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 101 | if [ "$action" = 'add' -a "$ip" != "$gbmc_br_gw_src_ip" ]; then |
| 102 | gbmc_br_gw_src_ip="$ip" |
| 103 | gbmc_br_gw_src_update |
| 104 | fi |
| 105 | if [ "$action" = 'del' -a "$ip" = "$gbmc_br_gw_src_ip" ]; then |
| 106 | gbmc_br_gw_src_ip= |
| 107 | fi |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | GBMC_IP_MONITOR_HOOKS+=(gbmc_br_gw_src_hook) |
| 112 | |
| 113 | gbmc_br_gw_src_lib=1 |