summaryrefslogtreecommitdiff
path: root/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-from-ra.sh
blob: 0625c10a7dc2591cd0b9d9db046f681f687b9f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[[ -n ${gbmc_br_from_ra_lib-} ]] && return

# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
source /usr/share/network/lib.sh || exit

gbmc_br_from_ra_init=
gbmc_br_from_ra_mac=
declare -A gbmc_br_from_ra_pfxs=()
declare -A gbmc_br_from_ra_prev_addrs=()

gbmc_br_from_ra_update() {
  [[ -n $gbmc_br_from_ra_init && -n $gbmc_br_from_ra_mac ]] || return

  local pfx
  for pfx in "${!gbmc_br_from_ra_pfxs[@]}"; do
    local cidr
    if ! cidr="$(ip_pfx_to_cidr "$pfx")"; then
      unset 'gbmc_br_from_ra_pfxs[$pfx]'
      continue
    fi
    if (( cidr == 80 )); then
      local sfx
      if ! sfx="$(mac_to_eui48 "$gbmc_br_from_ra_mac")"; then
        unset 'gbmc_br_from_ra_pfxs[$pfx]'
        continue
      fi
      local addr
      if ! addr="$(ip_pfx_concat "$pfx" "$sfx")"; then
        unset 'gbmc_br_from_ra_pfxs[$pfx]'
        continue
      fi
    else
      unset 'gbmc_br_from_ra_pfxs[$pfx]'
      continue
    fi
    local valid="${gbmc_br_from_ra_pfxs["$pfx"]}"
    if (( valid > 0 )); then
      if [[ -z ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then
        echo "gBMC Bridge RA Addr Add: $addr" >&2
        gbmc_br_from_ra_prev_addrs["$addr"]=1
      fi
      ip addr replace "$addr" dev gbmcbr noprefixroute
    else
      if [[ -n ${gbmc_br_from_ra_prev_addrs["$addr"]-} ]]; then
        echo "gBMC Bridge RA Addr Del: $addr" >&2
        unset 'gbmc_br_from_ra_prev_addrs[$addr]'
      fi
      ip addr del "$addr" dev gbmcbr
      unset 'gbmc_br_from_ra_pfxs[$pfx]'
    fi
  done
}

gbmc_br_from_ra_hook() {
  # shellcheck disable=SC2154
  if [[ $change == init ]]; then
    gbmc_br_from_ra_init=1
    gbmc_ip_monitor_defer
  elif [[ $change == defer ]]; then
    gbmc_br_from_ra_update
  elif [[ $change == route && $route != *' via '* ]] &&
       [[ $route =~ ^(.* dev gbmcbr proto ra .*)( +expires +([^ ]+)sec).*$ ]]; then
    pfx="${route%% *}"
    # shellcheck disable=SC2154
    if [[ $action == add ]]; then
      gbmc_br_from_ra_pfxs["$pfx"]="${BASH_REMATCH[3]}"
      gbmc_ip_monitor_defer
    elif [[ $action == del ]]; then
      gbmc_br_from_ra_pfxs["$pfx"]=0
      gbmc_ip_monitor_defer
    fi
  elif [[ $change == link && $intf == gbmcbr ]]; then
    rdisc6 -m gbmcbr -r 1 -w 100 >/dev/null 2>&1
    if [[ $action == add && $mac != "$gbmc_br_from_ra_mac" ]]; then
      gbmc_br_from_ra_mac="$mac"
      gbmc_ip_monitor_defer
    fi
    if [[ $action == del && $mac == "$gbmc_br_from_ra_mac" ]]; then
      gbmc_br_from_ra_mac=
      gbmc_ip_monitor_defer
    fi
  fi
}

GBMC_IP_MONITOR_HOOKS+=(gbmc_br_from_ra_hook)

gbmc_br_from_ra_lib=1