blob: 6085240ccf5fee871afc3e6b867c48767bf2bc04 [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 III3b127c42022-05-20 13:30:21 -070019gbmc_br_gw_src_ip_stateful=
20gbmc_br_gw_src_ip_stateless=
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070021declare -A gbmc_br_gw_src_routes=()
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070022gbmc_br_gw_defgw=
23
24gbmc_br_set_router() {
25 local defgw=
26 local route
27 for route in "${!gbmc_br_gw_src_routes[@]}"; do
28 if [[ "$route" != *' dev gbmcbr '* ]]; then
29 defgw=1
30 break
31 fi
32 done
33 [ "$defgw" = "$gbmc_br_gw_defgw" ] && return
34 gbmc_br_gw_defgw="$defgw"
35
36 local files=(/run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-defgw.conf)
37 if [ -n "$defgw" ]; then
38 local file
39 for file in "${files[@]}"; do
40 mkdir -p "$(dirname "$file")"
41 printf '[IPv6PrefixDelegation]\nRouterLifetimeSec=30\n' >"$file"
42 done
43 else
44 rm -f "${files[@]}"
45 fi
46
47 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
48 networkctl reload && networkctl reconfigure gbmcbr
49 fi
50}
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070051
52gbmc_br_gw_src_update() {
William A. Kennington III3b127c42022-05-20 13:30:21 -070053 local gbmc_br_gw_src_ip="${gbmc_br_gw_src_ip_stateful:-$gbmc_br_gw_src_ip_stateless}"
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070054 [ -n "$gbmc_br_gw_src_ip" ] || return
55
56 local route
57 for route in "${!gbmc_br_gw_src_routes[@]}"; do
58 [[ "$route" != *" src $gbmc_br_gw_src_ip "* ]] || continue
59 echo "gBMC Bridge Updating GW source [$gbmc_br_gw_src_ip]: $route" >&2
William A. Kennington III06ff3042022-05-20 10:40:48 -070060 ip route change $route src "$gbmc_br_gw_src_ip" && \
61 unset 'gbmc_br_gw_src_routes[$route]'
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070062 done
63}
64
65gbmc_br_gw_src_hook() {
66 # We only want to match default gateway routes that are dynamic
67 # (have an expiration time). These will be updated with our preferred
68 # source.
69 if [[ "$change" == 'route' && "$route" == 'default '*':'* ]]; then
70 if [[ "$route" =~ ^(.*)( +expires +[^ ]+)(.*)$ ]]; then
71 route="${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
72 fi
73 if [ "$action" = 'add' -a -z "${gbmc_br_gw_src_routes["$route"]}" ]; then
74 gbmc_br_gw_src_routes["$route"]=1
75 gbmc_br_gw_src_update
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070076 gbmc_br_set_router
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070077 elif [ "$action" = 'del' -a -n "${gbmc_br_gw_src_routes["$route"]}" ]; then
78 unset 'gbmc_br_gw_src_routes[$route]'
79 gbmc_br_gw_src_update
William A. Kennington IIIe70461f2022-05-20 10:11:57 -070080 gbmc_br_set_router
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -070081 fi
82 # Match only global IP addresses on the bridge that match the BMC stateless
83 # prefix (<mpfx>:fd00:). So 2002:af4:3480:2248:fd00:6345:3069:9186 would be
84 # matched as the preferred source IP for outoging traffic.
85 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] &&
William A. Kennington III4f233cd2021-05-07 03:25:25 -070086 [[ "$fam" == 'inet6' && "$flags" != *tentative* ]]; then
87 local ip_bytes=()
88 if ! ip_to_bytes ip_bytes "$ip"; then
89 echo "gBMC Bridge Ensure RA Invalid IP: $ip" >&2
90 return 1
91 fi
William A. Kennington III3b127c42022-05-20 13:30:21 -070092 # Ignore ULAs and non-gBMC addresses
93 if (( ip_bytes[0] & 0xfe == 0xfc || ip_bytes[8] != 0xfd )); then
William A. Kennington III4f233cd2021-05-07 03:25:25 -070094 return 0
95 fi
William A. Kennington III3b127c42022-05-20 13:30:21 -070096 if (( ip_bytes[9] != 0 )); then
97 local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateful
98 else
99 local -n gbmc_br_gw_src_ip=gbmc_br_gw_src_ip_stateless
100 fi
William A. Kennington IIIcd40c7e2021-05-05 14:44:41 -0700101 if [ "$action" = 'add' -a "$ip" != "$gbmc_br_gw_src_ip" ]; then
102 gbmc_br_gw_src_ip="$ip"
103 gbmc_br_gw_src_update
104 fi
105 if [ "$action" = 'del' -a "$ip" = "$gbmc_br_gw_src_ip" ]; then
106 gbmc_br_gw_src_ip=
107 fi
108 fi
109}
110
111GBMC_IP_MONITOR_HOOKS+=(gbmc_br_gw_src_hook)
112
113gbmc_br_gw_src_lib=1