blob: 2099185e8d1ff28dc0fd3868118066e502f3747e [file] [log] [blame]
William A. Kennington III53527f32021-04-27 13:05:45 -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_nft_lib-}" ] || return
16
17gbmc_br_nft_init=
18gbmc_br_nft_pfx=
19
20gbmc_br_nft_update() {
21 printf 'gBMC Bridge input firewall for %s\n' \
22 "${gbmc_br_nft_pfx:-(deleted)}" >&2
23
24 local contents=
25 contents+='table inet filter {'$'\n'
26 contents+=' chain gbmc_br_int_input {'$'\n'
27 if [ -n "${gbmc_br_nft_pfx-}" ]; then
28 contents+=" ip6 saddr $gbmc_br_nft_pfx"
29 contents+=" ip6 daddr $gbmc_br_nft_pfx accept"$'\n'
30 fi
31 contents+=' }'$'\n'
32 contents+='}'$'\n'
33
34 local rfile=/run/nftables/40-gbmc-br-int.rules
35 mkdir -p -m 755 "$(dirname "$rfile")"
36 printf '%s' "$contents" >"$rfile"
37
38 echo 'Restarting nftables' >&2
39 systemctl reset-failed nftables
40 systemctl --no-block restart nftables
41}
42
43gbmc_br_nft_hook() {
44 if [ "$change" = 'init' ]; then
45 gbmc_br_nft_init=1
46 gbmc_br_nft_update
47 # Match only global IP addresses on the bridge that match the BMC prefix
48 # (<mpfx>:fdxx:). So 2002:af4:3480:2248:fd02:6345:3069:9186 would become
49 # a 2002:af4:3480:2248:fd00/72 rule.
50 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] &&
51 [[ "$fam" == 'inet6' && "$ip" =~ ^(([^:]+:){4}fd)[^:]{2}:.*$ ]] &&
52 [[ "$flags" != *tentative* ]]; then
53 pfx="${BASH_REMATCH[1]}00::/72"
54 if [ "$action" = "add" -a "$pfx" != "$gbmc_br_nft_pfx" ]; then
55 gbmc_br_nft_pfx="$pfx"
56 gbmc_br_nft_update
57 fi
58 fi
59}
60
61GBMC_IP_MONITOR_HOOKS+=(gbmc_br_nft_hook)
62
63gbmc_br_nft_lib=1