William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 1 | #!/bin/bash |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -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_from_ra_lib-} ]] && return |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -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 | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 19 | source /usr/share/network/lib.sh || exit |
| 20 | |
| 21 | gbmc_br_from_ra_init= |
| 22 | gbmc_br_from_ra_mac= |
| 23 | declare -A gbmc_br_from_ra_pfxs=() |
| 24 | declare -A gbmc_br_from_ra_prev_addrs=() |
| 25 | |
| 26 | gbmc_br_from_ra_update() { |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 27 | [[ -n $gbmc_br_from_ra_init && -n $gbmc_br_from_ra_mac ]] || return |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 28 | |
| 29 | local pfx |
| 30 | for pfx in "${!gbmc_br_from_ra_pfxs[@]}"; do |
| 31 | local cidr |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 32 | if ! cidr="$(ip_pfx_to_cidr "$pfx")"; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 33 | unset 'gbmc_br_from_ra_pfxs[$pfx]' |
| 34 | continue |
| 35 | fi |
| 36 | if (( cidr == 80 )); then |
| 37 | local sfx |
| 38 | if ! sfx="$(mac_to_eui48 "$gbmc_br_from_ra_mac")"; then |
| 39 | unset 'gbmc_br_from_ra_pfxs[$pfx]' |
| 40 | continue |
| 41 | fi |
| 42 | local addr |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 43 | if ! addr="$(ip_pfx_concat "$pfx" "$sfx")"; then |
| 44 | unset 'gbmc_br_from_ra_pfxs[$pfx]' |
| 45 | continue |
| 46 | fi |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 47 | else |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 48 | unset 'gbmc_br_from_ra_pfxs[$pfx]' |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 49 | continue |
| 50 | fi |
| 51 | local valid="${gbmc_br_from_ra_pfxs["$pfx"]}" |
| 52 | if (( valid > 0 )); then |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 53 | if [[ -z ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 54 | echo "gBMC Bridge RA Addr Add: $addr" >&2 |
| 55 | gbmc_br_from_ra_prev_addrs["$addr"]=1 |
| 56 | fi |
| 57 | ip addr replace "$addr" dev gbmcbr noprefixroute |
| 58 | else |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 59 | if [[ -n ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 60 | echo "gBMC Bridge RA Addr Del: $addr" >&2 |
| 61 | unset 'gbmc_br_from_ra_prev_addrs[$addr]' |
| 62 | fi |
| 63 | ip addr del "$addr" dev gbmcbr |
| 64 | unset 'gbmc_br_from_ra_pfxs[$pfx]' |
| 65 | fi |
| 66 | done |
| 67 | } |
| 68 | |
| 69 | gbmc_br_from_ra_hook() { |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 70 | # shellcheck disable=SC2154 |
| 71 | if [[ $change == init ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 72 | gbmc_br_from_ra_init=1 |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 73 | gbmc_ip_monitor_defer |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 74 | elif [[ $change == defer ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 75 | gbmc_br_from_ra_update |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 76 | elif [[ $change == route && $route != *' via '* ]] && |
| 77 | [[ $route =~ ^(.* dev gbmcbr proto ra .*)( +expires +([^ ]+)sec).*$ ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 78 | pfx="${route%% *}" |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 79 | # shellcheck disable=SC2154 |
| 80 | if [[ $action == add ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 81 | gbmc_br_from_ra_pfxs["$pfx"]="${BASH_REMATCH[3]}" |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 82 | gbmc_ip_monitor_defer |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 83 | elif [[ $action == del ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 84 | gbmc_br_from_ra_pfxs["$pfx"]=0 |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 85 | gbmc_ip_monitor_defer |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 86 | fi |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 87 | elif [[ $change == link && $intf == gbmcbr ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 88 | rdisc6 -m gbmcbr -r 1 -w 100 >/dev/null 2>&1 |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 89 | if [[ $action == add && $mac != "$gbmc_br_from_ra_mac" ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 90 | gbmc_br_from_ra_mac="$mac" |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 91 | gbmc_ip_monitor_defer |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 92 | fi |
William A. Kennington III | 5206f66 | 2023-06-05 16:31:37 -0700 | [diff] [blame] | 93 | if [[ $action == del && $mac == "$gbmc_br_from_ra_mac" ]]; then |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 94 | gbmc_br_from_ra_mac= |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 95 | gbmc_ip_monitor_defer |
William A. Kennington III | 2461556 | 2021-04-26 14:10:52 -0700 | [diff] [blame] | 96 | fi |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | GBMC_IP_MONITOR_HOOKS+=(gbmc_br_from_ra_hook) |
| 101 | |
| 102 | gbmc_br_from_ra_lib=1 |