blob: 185d78b816d713d2eaccc36e87c0009ec22e9c35 [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
William A. Kennington III4f233cd2021-05-07 03:25:25 -070017source /usr/share/network/lib.sh || exit
18
William A. Kennington III53527f32021-04-27 13:05:45 -070019gbmc_br_nft_init=
20gbmc_br_nft_pfx=
21
22gbmc_br_nft_update() {
23 printf 'gBMC Bridge input firewall for %s\n' \
24 "${gbmc_br_nft_pfx:-(deleted)}" >&2
25
26 local contents=
27 contents+='table inet filter {'$'\n'
28 contents+=' chain gbmc_br_int_input {'$'\n'
29 if [ -n "${gbmc_br_nft_pfx-}" ]; then
30 contents+=" ip6 saddr $gbmc_br_nft_pfx"
31 contents+=" ip6 daddr $gbmc_br_nft_pfx accept"$'\n'
32 fi
33 contents+=' }'$'\n'
34 contents+='}'$'\n'
35
36 local rfile=/run/nftables/40-gbmc-br-int.rules
37 mkdir -p -m 755 "$(dirname "$rfile")"
38 printf '%s' "$contents" >"$rfile"
39
40 echo 'Restarting nftables' >&2
41 systemctl reset-failed nftables
42 systemctl --no-block restart nftables
43}
44
45gbmc_br_nft_hook() {
46 if [ "$change" = 'init' ]; then
47 gbmc_br_nft_init=1
48 gbmc_br_nft_update
49 # Match only global IP addresses on the bridge that match the BMC prefix
50 # (<mpfx>:fdxx:). So 2002:af4:3480:2248:fd02:6345:3069:9186 would become
51 # a 2002:af4:3480:2248:fd00/72 rule.
52 elif [ "$change" = 'addr' -a "$intf" = 'gbmcbr' -a "$scope" = 'global' ] &&
William A. Kennington III4f233cd2021-05-07 03:25:25 -070053 [[ "$fam" == 'inet6' && "$flags" != *tentative* ]]; then
54 local ip_bytes=()
55 if ! ip_to_bytes ip_bytes "$ip"; then
56 echo "gBMC Bridge NFT Invalid IP: $ip" >&2
57 return 1
58 fi
59 if (( ip_bytes[9] != 0xfd )); then
60 return 0
61 fi
62 pfx="$(printf '%02x%02x:%02x%02x:%02x%02x:%02x%02x:fd00::/72' "${ip_bytes[@]}")"
William A. Kennington III53527f32021-04-27 13:05:45 -070063 if [ "$action" = "add" -a "$pfx" != "$gbmc_br_nft_pfx" ]; then
64 gbmc_br_nft_pfx="$pfx"
65 gbmc_br_nft_update
66 fi
67 fi
68}
69
70GBMC_IP_MONITOR_HOOKS+=(gbmc_br_nft_hook)
71
72gbmc_br_nft_lib=1