blob: 7aa2158989d9a7fca28e942b91594e76aca0ae76 [file] [log] [blame]
William A. Kennington III5206f662023-06-05 16:31:37 -07001#!/bin/bash
William A. Kennington III53527f32021-04-27 13:05:45 -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_nft_lib-} ]] && return
William A. Kennington III53527f32021-04-27 13:05:45 -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 III4f233cd2021-05-07 03:25:25 -070019source /usr/share/network/lib.sh || exit
20
William A. Kennington III53527f32021-04-27 13:05:45 -070021gbmc_br_nft_pfx=
22
23gbmc_br_nft_update() {
24 printf 'gBMC Bridge input firewall for %s\n' \
25 "${gbmc_br_nft_pfx:-(deleted)}" >&2
26
27 local contents=
28 contents+='table inet filter {'$'\n'
29 contents+=' chain gbmc_br_int_input {'$'\n'
William A. Kennington III5206f662023-06-05 16:31:37 -070030 if [[ -n ${gbmc_br_nft_pfx-} ]]; then
William A. Kennington III53527f32021-04-27 13:05:45 -070031 contents+=" ip6 saddr $gbmc_br_nft_pfx"
32 contents+=" ip6 daddr $gbmc_br_nft_pfx accept"$'\n'
33 fi
34 contents+=' }'$'\n'
35 contents+='}'$'\n'
36
37 local rfile=/run/nftables/40-gbmc-br-int.rules
William A. Kennington III5206f662023-06-05 16:31:37 -070038 mkdir -p "$(dirname "$rfile")"
William A. Kennington III53527f32021-04-27 13:05:45 -070039 printf '%s' "$contents" >"$rfile"
40
William A. Kennington III5206f662023-06-05 16:31:37 -070041 # shellcheck disable=SC2015
William A. Kennington III7356f8e2021-12-15 02:21:52 -080042 systemctl reset-failed nftables && systemctl --no-block reload-or-restart nftables || true
William A. Kennington III53527f32021-04-27 13:05:45 -070043}
44
45gbmc_br_nft_hook() {
William A. Kennington III53527f32021-04-27 13:05:45 -070046 # Match only global IP addresses on the bridge that match the BMC prefix
47 # (<mpfx>:fdxx:). So 2002:af4:3480:2248:fd02:6345:3069:9186 would become
William A. Kennington IIIa22b4452021-11-04 22:57:43 -070048 # a 2002:af4:3480:2248:fd00/76 rule.
William A. Kennington III5206f662023-06-05 16:31:37 -070049 # shellcheck disable=SC2154
50 if [[ $change == addr && $intf == gbmcbr && $scope == global ]] &&
51 [[ $fam == inet6 && $flags != *tentative* ]]; then
William A. Kennington III4f233cd2021-05-07 03:25:25 -070052 local ip_bytes=()
53 if ! ip_to_bytes ip_bytes "$ip"; then
54 echo "gBMC Bridge NFT Invalid IP: $ip" >&2
55 return 1
56 fi
William A. Kennington III6ca70332021-05-10 03:14:42 -070057 if (( ip_bytes[8] != 0xfd )); then
William A. Kennington III4f233cd2021-05-07 03:25:25 -070058 return 0
59 fi
William A. Kennington III6ca70332021-05-10 03:14:42 -070060 local i
61 for (( i=9; i<16; i++ )); do
William A. Kennington III5206f662023-06-05 16:31:37 -070062 ip_bytes["$i"]=0
William A. Kennington III6ca70332021-05-10 03:14:42 -070063 done
William A. Kennington IIIa22b4452021-11-04 22:57:43 -070064 pfx="$(ip_bytes_to_str ip_bytes)/76"
William A. Kennington III5206f662023-06-05 16:31:37 -070065 if [[ $action == add && $pfx != "$gbmc_br_nft_pfx" ]]; then
William A. Kennington III53527f32021-04-27 13:05:45 -070066 gbmc_br_nft_pfx="$pfx"
67 gbmc_br_nft_update
68 fi
69 fi
70}
71
72GBMC_IP_MONITOR_HOOKS+=(gbmc_br_nft_hook)
73
74gbmc_br_nft_lib=1