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