William A. Kennington III | e99168a | 2021-03-10 23:40:47 -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_br_pub_addr_lib-}" ] || return |
| 16 | |
| 17 | gbmc_ncsi_br_pub_addr_init= |
| 18 | gbmc_ncsi_br_pub_addr_lastip= |
| 19 | |
| 20 | gbmc_ncsi_br_pub_addr_update() { |
| 21 | [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return |
| 22 | |
| 23 | printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \ |
| 24 | "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2 |
| 25 | |
| 26 | local pfx= |
| 27 | if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 28 | # Pad the address out to a /64 and ensure that it doesn't have extra bits |
| 29 | pfx="${gbmc_ncsi_br_pub_addr_lastip%::}" |
| 30 | while true; do |
| 31 | # Count `:` in `pfx` by removing them and diffing their lengths |
| 32 | local nos="${pfx//:/}" |
| 33 | (( ${#pfx} - ${#nos} >= 3 )) && break |
| 34 | pfx+=":0" |
| 35 | done |
| 36 | # Addresses that have more than 64bits of prefix (more than 3 separators) |
| 37 | # do not work with this scheme. Ignore them. |
| 38 | (( ${#pfx} - ${#nos} == 3 )) || pfx= |
| 39 | fi |
| 40 | |
| 41 | local contents='[Network]'$'\n' |
| 42 | if [ -n "$pfx" ]; then |
| 43 | local here= |
| 44 | read -r -d '' here <<EOF |
| 45 | Address=${pfx}:fd01::/128 |
| 46 | IPv6PrefixDelegation=yes |
| 47 | [IPv6PrefixDelegation] |
| 48 | RouterLifetimeSec=60 |
| 49 | [IPv6Prefix] |
| 50 | Prefix=${pfx}:fd00::/80 |
| 51 | PreferredLifetimeSec=60 |
| 52 | ValidLifetimeSec=60 |
| 53 | [IPv6RoutePrefix] |
| 54 | Route=${pfx}:fd01::/80 |
| 55 | LifetimeSec=60 |
| 56 | EOF |
| 57 | contents+="$here"$'\n' |
| 58 | fi |
| 59 | |
| 60 | local file |
| 61 | for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do |
| 62 | mkdir -p -m 755 "$(dirname "$file")" |
| 63 | printf '%s' "$contents" >"$file" |
| 64 | done |
| 65 | |
William A. Kennington III | 8fb9258 | 2021-05-10 03:15:56 -0700 | [diff] [blame^] | 66 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 67 | # currently check the mtime of drop-in files. |
| 68 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network |
| 69 | |
| 70 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 71 | networkctl reload |
| 72 | networkctl reconfigure gbmcbr |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 73 | fi |
| 74 | } |
| 75 | |
| 76 | gbmc_ncsi_br_pub_addr_hook() { |
| 77 | if [ "$change" = 'init' ]; then |
| 78 | gbmc_ncsi_br_pub_addr_init=1 |
| 79 | gbmc_ncsi_br_pub_addr_update |
| 80 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && |
| 81 | [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then |
| 82 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 83 | gbmc_ncsi_br_pub_addr_lastip="$ip" |
| 84 | gbmc_ncsi_br_pub_addr_update |
| 85 | fi |
| 86 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 87 | gbmc_ncsi_br_pub_addr_lastip= |
| 88 | gbmc_ncsi_br_pub_addr_update |
| 89 | fi |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) |
| 94 | |
| 95 | gbmc_ncsi_br_pub_addr_lib=1 |