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 | |
| 15 | [ -z "${gbmc_br_gw_src_lib-}" ] || return |
| 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=() |
| 21 | |
| 22 | gbmc_br_gw_src_update() { |
| 23 | [ -n "$gbmc_br_gw_src_ip" ] || return |
| 24 | |
| 25 | local route |
| 26 | for route in "${!gbmc_br_gw_src_routes[@]}"; do |
| 27 | [[ "$route" != *" src $gbmc_br_gw_src_ip "* ]] || continue |
| 28 | echo "gBMC Bridge Updating GW source [$gbmc_br_gw_src_ip]: $route" >&2 |
| 29 | ip route change $route src "$gbmc_br_gw_src_ip" |
| 30 | unset 'gbmc_br_gw_src_routes[$route]' |
| 31 | done |
| 32 | } |
| 33 | |
| 34 | gbmc_br_gw_src_hook() { |
| 35 | # We only want to match default gateway routes that are dynamic |
| 36 | # (have an expiration time). These will be updated with our preferred |
| 37 | # source. |
| 38 | if [[ "$change" == 'route' && "$route" == 'default '*':'* ]]; then |
| 39 | if [[ "$route" =~ ^(.*)( +expires +[^ ]+)(.*)$ ]]; then |
| 40 | route="${BASH_REMATCH[1]}${BASH_REMATCH[3]}" |
| 41 | fi |
| 42 | if [ "$action" = 'add' -a -z "${gbmc_br_gw_src_routes["$route"]}" ]; then |
| 43 | gbmc_br_gw_src_routes["$route"]=1 |
| 44 | gbmc_br_gw_src_update |
| 45 | elif [ "$action" = 'del' -a -n "${gbmc_br_gw_src_routes["$route"]}" ]; then |
| 46 | unset 'gbmc_br_gw_src_routes[$route]' |
| 47 | gbmc_br_gw_src_update |
| 48 | fi |
| 49 | # Match only global IP addresses on the bridge that match the BMC stateless |
| 50 | # prefix (<mpfx>:fd00:). So 2002:af4:3480:2248:fd00:6345:3069:9186 would be |
| 51 | # matched as the preferred source IP for outoging traffic. |
| 52 | elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] && |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 53 | [[ "$fam" == 'inet6' && "$flags" != *tentative* ]]; then |
| 54 | local ip_bytes=() |
| 55 | if ! ip_to_bytes ip_bytes "$ip"; then |
| 56 | echo "gBMC Bridge Ensure RA Invalid IP: $ip" >&2 |
| 57 | return 1 |
| 58 | fi |
William A. Kennington III | 0f31b96 | 2021-05-12 01:25:03 -0700 | [diff] [blame] | 59 | if (( ip_bytes[8] != 0xfd || ip_bytes[9] != 0 )); then |
William A. Kennington III | 4f233cd | 2021-05-07 03:25:25 -0700 | [diff] [blame] | 60 | return 0 |
| 61 | fi |
William A. Kennington III | cd40c7e | 2021-05-05 14:44:41 -0700 | [diff] [blame] | 62 | if [ "$action" = 'add' -a "$ip" != "$gbmc_br_gw_src_ip" ]; then |
| 63 | gbmc_br_gw_src_ip="$ip" |
| 64 | gbmc_br_gw_src_update |
| 65 | fi |
| 66 | if [ "$action" = 'del' -a "$ip" = "$gbmc_br_gw_src_ip" ]; then |
| 67 | gbmc_br_gw_src_ip= |
| 68 | fi |
| 69 | fi |
| 70 | } |
| 71 | |
| 72 | GBMC_IP_MONITOR_HOOKS+=(gbmc_br_gw_src_hook) |
| 73 | |
| 74 | gbmc_br_gw_src_lib=1 |