blob: 0625c10a7dc2591cd0b9d9db046f681f687b9f86 [file] [log] [blame]
William A. Kennington III5206f662023-06-05 16:31:37 -07001#!/bin/bash
William A. Kennington III24615562021-04-26 14:10:52 -07002# 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 III5206f662023-06-05 16:31:37 -070016[[ -n ${gbmc_br_from_ra_lib-} ]] && return
William A. Kennington III24615562021-04-26 14:10:52 -070017
William A. Kennington III5206f662023-06-05 16:31:37 -070018# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
William A. Kennington III24615562021-04-26 14:10:52 -070019source /usr/share/network/lib.sh || exit
20
21gbmc_br_from_ra_init=
22gbmc_br_from_ra_mac=
23declare -A gbmc_br_from_ra_pfxs=()
24declare -A gbmc_br_from_ra_prev_addrs=()
25
26gbmc_br_from_ra_update() {
William A. Kennington III5206f662023-06-05 16:31:37 -070027 [[ -n $gbmc_br_from_ra_init && -n $gbmc_br_from_ra_mac ]] || return
William A. Kennington III24615562021-04-26 14:10:52 -070028
29 local pfx
30 for pfx in "${!gbmc_br_from_ra_pfxs[@]}"; do
31 local cidr
William A. Kennington III6ca70332021-05-10 03:14:42 -070032 if ! cidr="$(ip_pfx_to_cidr "$pfx")"; then
William A. Kennington III24615562021-04-26 14:10:52 -070033 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 III6ca70332021-05-10 03:14:42 -070043 if ! addr="$(ip_pfx_concat "$pfx" "$sfx")"; then
44 unset 'gbmc_br_from_ra_pfxs[$pfx]'
45 continue
46 fi
William A. Kennington III24615562021-04-26 14:10:52 -070047 else
William A. Kennington III6ca70332021-05-10 03:14:42 -070048 unset 'gbmc_br_from_ra_pfxs[$pfx]'
William A. Kennington III24615562021-04-26 14:10:52 -070049 continue
50 fi
51 local valid="${gbmc_br_from_ra_pfxs["$pfx"]}"
52 if (( valid > 0 )); then
William A. Kennington III5206f662023-06-05 16:31:37 -070053 if [[ -z ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070054 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 III5206f662023-06-05 16:31:37 -070059 if [[ -n ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070060 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
69gbmc_br_from_ra_hook() {
William A. Kennington III5206f662023-06-05 16:31:37 -070070 # shellcheck disable=SC2154
71 if [[ $change == init ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070072 gbmc_br_from_ra_init=1
William A. Kennington III58ac4342021-11-05 04:15:36 -070073 gbmc_ip_monitor_defer
William A. Kennington III5206f662023-06-05 16:31:37 -070074 elif [[ $change == defer ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070075 gbmc_br_from_ra_update
William A. Kennington III5206f662023-06-05 16:31:37 -070076 elif [[ $change == route && $route != *' via '* ]] &&
77 [[ $route =~ ^(.* dev gbmcbr proto ra .*)( +expires +([^ ]+)sec).*$ ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070078 pfx="${route%% *}"
William A. Kennington III5206f662023-06-05 16:31:37 -070079 # shellcheck disable=SC2154
80 if [[ $action == add ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070081 gbmc_br_from_ra_pfxs["$pfx"]="${BASH_REMATCH[3]}"
William A. Kennington III58ac4342021-11-05 04:15:36 -070082 gbmc_ip_monitor_defer
William A. Kennington III5206f662023-06-05 16:31:37 -070083 elif [[ $action == del ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070084 gbmc_br_from_ra_pfxs["$pfx"]=0
William A. Kennington III58ac4342021-11-05 04:15:36 -070085 gbmc_ip_monitor_defer
William A. Kennington III24615562021-04-26 14:10:52 -070086 fi
William A. Kennington III5206f662023-06-05 16:31:37 -070087 elif [[ $change == link && $intf == gbmcbr ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070088 rdisc6 -m gbmcbr -r 1 -w 100 >/dev/null 2>&1
William A. Kennington III5206f662023-06-05 16:31:37 -070089 if [[ $action == add && $mac != "$gbmc_br_from_ra_mac" ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070090 gbmc_br_from_ra_mac="$mac"
William A. Kennington III58ac4342021-11-05 04:15:36 -070091 gbmc_ip_monitor_defer
William A. Kennington III24615562021-04-26 14:10:52 -070092 fi
William A. Kennington III5206f662023-06-05 16:31:37 -070093 if [[ $action == del && $mac == "$gbmc_br_from_ra_mac" ]]; then
William A. Kennington III24615562021-04-26 14:10:52 -070094 gbmc_br_from_ra_mac=
William A. Kennington III58ac4342021-11-05 04:15:36 -070095 gbmc_ip_monitor_defer
William A. Kennington III24615562021-04-26 14:10:52 -070096 fi
97 fi
98}
99
100GBMC_IP_MONITOR_HOOKS+=(gbmc_br_from_ra_hook)
101
102gbmc_br_from_ra_lib=1