William A. Kennington III | 53527f3 | 2021-04-27 13:05:45 -0700 | [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_br_nft_lib-}" ] || return |
| 16 | |
| 17 | gbmc_br_nft_init= |
| 18 | gbmc_br_nft_pfx= |
| 19 | |
| 20 | gbmc_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 | |
| 43 | gbmc_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 | |
| 61 | GBMC_IP_MONITOR_HOOKS+=(gbmc_br_nft_hook) |
| 62 | |
| 63 | gbmc_br_nft_lib=1 |