William A. Kennington III | 1ef795b | 2021-03-10 18:59:12 -0800 | [diff] [blame] | 1 | # 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 | |
| 17 | gbmc_ncsi_nft_init= |
| 18 | gbmc_ncsi_nft_lastip4= |
| 19 | gbmc_ncsi_nft_lastip6= |
| 20 | |
| 21 | gbmc_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 |
| 39 | contents+=" ip6 daddr $ip6/128 goto ncsi_legacy_input"$'\n' |
| 40 | |
| 41 | # Pad out to 4 hextets with no trailing semicolons |
| 42 | local pfx= |
| 43 | pfx="${ip6%::}" |
| 44 | while true; do |
| 45 | # Count `:` in `pfx` by removing them and diffing their lengths |
| 46 | local nos="${pfx//:/}" |
| 47 | (( ${#pfx} - ${#nos} >= 3 )) && break |
| 48 | pfx+=":0" |
| 49 | done |
| 50 | |
| 51 | # If our address has enough spare bits for appending the BMC suffix |
| 52 | # then we add a rule that allows the BMC subnet. That is, we need a /64 |
| 53 | # as input. |
| 54 | if (( ${#pfx} - ${#nos} == 3 )); then |
| 55 | contents+=" ip6 saddr != ${pfx}:fd00::/72 ip6 daddr" |
| 56 | contents+=" ${pfx}:fd00::/72 goto ncsi_gbmc_br_pub_input"$'\n' |
| 57 | fi |
| 58 | fi |
| 59 | |
| 60 | contents+=' }'$'\n' |
| 61 | contents+='}'$'\n' |
| 62 | |
| 63 | local rfile=/run/nftables/40-gbmc-ncsi-in.rules |
| 64 | mkdir -p -m 755 "$(dirname "$rfile")" |
| 65 | printf '%s' "$contents" >"$rfile" |
| 66 | |
| 67 | echo 'Restarting nftables' >&2 |
| 68 | systemctl reset-failed nftables |
| 69 | systemctl --no-block restart nftables |
| 70 | } |
| 71 | |
| 72 | gbmc_ncsi_nft_hook() { |
| 73 | if [ "$change" = 'init' ]; then |
| 74 | gbmc_ncsi_nft_init=1 |
| 75 | gbmc_ncsi_nft_update |
| 76 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' -a "$scope" = 'global' ]; then |
| 77 | if [ "$fam" = 'inet6' ]; then |
| 78 | local -n lastip='gbmc_ncsi_nft_lastip6' |
| 79 | else |
| 80 | local -n lastip='gbmc_ncsi_nft_lastip4' |
| 81 | fi |
| 82 | if [ "$action" = 'add' -a "$ip" != "$lastip" ]; then |
| 83 | lastip="$ip" |
| 84 | gbmc_ncsi_nft_update |
| 85 | fi |
| 86 | if [ "$action" = 'del' -a "$ip" = "$lastip" ]; then |
| 87 | lastip= |
| 88 | gbmc_ncsi_nft_update |
| 89 | fi |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_nft_hook) |
| 94 | |
| 95 | gbmc_ncsi_nft_lib=1 |