blob: 4bd3828213cba843a2984ed46b4f0c26e9533b78 [file] [log] [blame]
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -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
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070015[ -n "${gbmc_br_gw_src_lib-}" ] && return
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070016
William A. Kennington III4f233cd2021-05-07 03:25:25 -070017source /usr/share/network/lib.sh || exit
18
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070019gbmc_br_gw_src_ip=
20declare -A gbmc_br_gw_src_routes=()
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070021gbmc_br_gw_defgw=
22
23gbmc_br_set_router() {
24 local defgw=
25 local route
26 for route in "${!gbmc_br_gw_src_routes[@]}"; do
27 if [[ "$route" != *' dev gbmcbr '* ]]; then
28 defgw=1
29 break
30 fi
31 done
32 [ "$defgw" = "$gbmc_br_gw_defgw" ] && return
33 gbmc_br_gw_defgw="$defgw"
34
35 local files=(/run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-defgw.conf)
36 if [ -n "$defgw" ]; then
37 local file
38 for file in "${files[@]}"; do
39 mkdir -p "$(dirname "$file")"
40 printf '[IPv6PrefixDelegation]\nRouterLifetimeSec=30\n' >"$file"
41 done
42 else
43 rm -f "${files[@]}"
44 fi
45
46 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
47 networkctl reload && networkctl reconfigure gbmcbr
48 fi
49}
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070050
51gbmc_br_gw_src_update() {
52 [ -n "$gbmc_br_gw_src_ip" ] || return
53
54 local route
55 for route in "${!gbmc_br_gw_src_routes[@]}"; do
56 [[ "$route" != *" src $gbmc_br_gw_src_ip "* ]] || continue
57 echo "gBMC Bridge Updating GW source [$gbmc_br_gw_src_ip]: $route" >&2
William A. Kennington III06ff3042022-05-20 10:40:48 -070058 ip route change $route src "$gbmc_br_gw_src_ip" && \
59 unset 'gbmc_br_gw_src_routes[$route]'
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070060 done
61}
62
63gbmc_br_gw_src_hook() {
64 # We only want to match default gateway routes that are dynamic
65 # (have an expiration time). These will be updated with our preferred
66 # source.
67 if [[ "$change" == 'route' && "$route" == 'default '*':'* ]]; then
68 if [[ "$route" =~ ^(.*)( +expires +[^ ]+)(.*)$ ]]; then
69 route="${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
70 fi
71 if [ "$action" = 'add' -a -z "${gbmc_br_gw_src_routes["$route"]}" ]; then
72 gbmc_br_gw_src_routes["$route"]=1
73 gbmc_br_gw_src_update
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070074 gbmc_br_set_router
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070075 elif [ "$action" = 'del' -a -n "${gbmc_br_gw_src_routes["$route"]}" ]; then
76 unset 'gbmc_br_gw_src_routes[$route]'
77 gbmc_br_gw_src_update
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070078 gbmc_br_set_router
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070079 fi
80 # Match only global IP addresses on the bridge that match the BMC stateless
81 # prefix (<mpfx>:fd00:). So 2002:af4:3480:2248:fd00:6345:3069:9186 would be
82 # matched as the preferred source IP for outoging traffic.
83 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] &&
William A. Kennington III4f233cd2021-05-07 03:25:25 -070084 [[ "$fam" == 'inet6' && "$flags" != *tentative* ]]; then
85 local ip_bytes=()
86 if ! ip_to_bytes ip_bytes "$ip"; then
87 echo "gBMC Bridge Ensure RA Invalid IP: $ip" >&2
88 return 1
89 fi
William A. Kennington III0f31b962021-05-12 01:25:03 -070090 if (( ip_bytes[8] != 0xfd || ip_bytes[9] != 0 )); then
William A. Kennington III4f233cd2021-05-07 03:25:25 -070091 return 0
92 fi
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070093 if [ "$action" = 'add' -a "$ip" != "$gbmc_br_gw_src_ip" ]; then
94 gbmc_br_gw_src_ip="$ip"
95 gbmc_br_gw_src_update
96 fi
97 if [ "$action" = 'del' -a "$ip" = "$gbmc_br_gw_src_ip" ]; then
98 gbmc_br_gw_src_ip=
99 fi
100 fi
101}
102
103GBMC_IP_MONITOR_HOOKS+=(gbmc_br_gw_src_hook)
104
105gbmc_br_gw_src_lib=1