blob: d07b9e2f026a1a6d5c780d029d1f7353291a2aeb [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"
William A. Kennington IIIb14f1062021-09-15 15:50:05 -070038 local pfx=
William A. Kennington III1ef795b2021-03-10 18:59:12 -080039 if [ -n "$ip6" ]; then
40 contents+=" ip6 daddr $ip6/128 goto ncsi_legacy_input"$'\n'
41
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070042 local ip_bytes=()
43 ip_to_bytes ip_bytes "$ip6"
William A. Kennington III1ef795b2021-03-10 18:59:12 -080044 # If our address has enough spare bits for appending the BMC suffix
45 # then we add a rule that allows the BMC subnet. That is, we need a /64
46 # as input.
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070047 local i
48 for (( i = 8; i < 16; i++ )); do
49 if (( ip_bytes[$i] != 0 )); then
50 ip_bytes=()
51 break
52 fi
53 done
54 if (( ${#ip_bytes[@]} != 0 )); then
55 ip_bytes[8]=0xfd
William A. Kennington IIIb14f1062021-09-15 15:50:05 -070056 pfx="$(ip_bytes_to_str ip_bytes)"
William A. Kennington IIIbdbe7ce2021-05-10 16:48:43 -070057 contents+=" ip6 saddr != $pfx/72 ip6 daddr"
58 contents+=" $pfx/72 goto ncsi_gbmc_br_pub_input"$'\n'
William A. Kennington III1ef795b2021-03-10 18:59:12 -080059 fi
60 fi
61
62 contents+=' }'$'\n'
William A. Kennington IIIcf1e7272021-05-12 00:57:41 -070063 contents+=' chain ncsi_forward {'$'\n'
64 if [ -n "$pfx" ]; then
65 contents+=" ip6 saddr != $pfx/72 ip6 daddr $pfx/72 accept"$'\n'
66 fi
67 contents+=' }'$'\n'
William A. Kennington III1ef795b2021-03-10 18:59:12 -080068 contents+='}'$'\n'
69
70 local rfile=/run/nftables/40-gbmc-ncsi-in.rules
71 mkdir -p -m 755 "$(dirname "$rfile")"
72 printf '%s' "$contents" >"$rfile"
73
74 echo 'Restarting nftables' >&2
75 systemctl reset-failed nftables
76 systemctl --no-block restart nftables
77}
78
79gbmc_ncsi_nft_hook() {
80 if [ "$change" = 'init' ]; then
81 gbmc_ncsi_nft_init=1
82 gbmc_ncsi_nft_update
83 elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' -a "$scope" = 'global' ]; then
84 if [ "$fam" = 'inet6' ]; then
85 local -n lastip='gbmc_ncsi_nft_lastip6'
86 else
87 local -n lastip='gbmc_ncsi_nft_lastip4'
88 fi
89 if [ "$action" = 'add' -a "$ip" != "$lastip" ]; then
90 lastip="$ip"
91 gbmc_ncsi_nft_update
92 fi
93 if [ "$action" = 'del' -a "$ip" = "$lastip" ]; then
94 lastip=
95 gbmc_ncsi_nft_update
96 fi
97 fi
98}
99
100GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_nft_hook)
101
102gbmc_ncsi_nft_lib=1