blob: 1f3ba25607de340bee1ebc834efbd3a4a61b82c3 [file] [log] [blame]
William A. Kennington III1ef795b2021-03-10 18:59:12 -08001# 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_ncsi_nft_lib-}" ] || return
16
17gbmc_ncsi_nft_init=
18gbmc_ncsi_nft_lastip4=
19gbmc_ncsi_nft_lastip6=
20
21gbmc_ncsi_nft_update() {
22 [ -n "$gbmc_ncsi_nft_init" ] || return
23
24 printf 'NCSI firewall for IPv4(%s) IPv6(%s)\n' \
25 "${gbmc_ncsi_nft_lastip4:-(deleted)}" \
26 "${gbmc_ncsi_nft_lastip6:-(deleted)}" >&2
27
28 local contents=
29 contents+='table inet filter {'$'\n'
30 contents+=' chain ncsi_input {'$'\n'
31
32 local ip4="$gbmc_ncsi_nft_lastip4"
33 if [ -n "$ip4" ]; then
34 contents+=" ip daddr $ip4 goto ncsi_legacy_input"$'\n'
35 fi
36
37 local ip6="$gbmc_ncsi_nft_lastip6"
38 if [ -n "$ip6" ]; then
William A. Kennington III10dc43a2022-05-10 10:30:25 -070039 # Normalize the provided ipv6 address to the impersonated ipv6 address
40 local ip6_bytes=()
41 ip_to_bytes ip6_bytes "$ip6"
42 for (( i=8; i<16; ++i )); do
43 ip6_bytes[$i]=0
44 done
45 ip6="$(ip_bytes_to_str ip6_bytes)"
William A. Kennington III1ef795b2021-03-10 18:59:12 -080046 contents+=" ip6 daddr $ip6/128 goto ncsi_legacy_input"$'\n'
William A. Kennington III1ef795b2021-03-10 18:59:12 -080047 fi
48
49 contents+=' }'$'\n'
William A. Kennington III1ef795b2021-03-10 18:59:12 -080050 contents+='}'$'\n'
51
52 local rfile=/run/nftables/40-gbmc-ncsi-in.rules
53 mkdir -p -m 755 "$(dirname "$rfile")"
54 printf '%s' "$contents" >"$rfile"
55
William A. Kennington III7356f8e2021-12-15 02:21:52 -080056 systemctl reset-failed nftables && systemctl --no-block reload-or-restart nftables || true
William A. Kennington III1ef795b2021-03-10 18:59:12 -080057}
58
59gbmc_ncsi_nft_hook() {
60 if [ "$change" = 'init' ]; then
61 gbmc_ncsi_nft_init=1
62 gbmc_ncsi_nft_update
William A. Kennington IIIcbd9ef02021-11-04 20:52:17 -070063 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' -a "$scope" = 'global' ] &&
64 [[ "$flags" != *deprecated* ]]; then
William A. Kennington III1ef795b2021-03-10 18:59:12 -080065 if [ "$fam" = 'inet6' ]; then
66 local -n lastip='gbmc_ncsi_nft_lastip6'
67 else
68 local -n lastip='gbmc_ncsi_nft_lastip4'
69 fi
70 if [ "$action" = 'add' -a "$ip" != "$lastip" ]; then
71 lastip="$ip"
72 gbmc_ncsi_nft_update
73 fi
74 if [ "$action" = 'del' -a "$ip" = "$lastip" ]; then
75 lastip=
76 gbmc_ncsi_nft_update
77 fi
78 fi
79}
80
81GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_nft_hook)
82
83gbmc_ncsi_nft_lib=1