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