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= |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 19 | gbmc_ncsi_br_pub_addr_confip= |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 20 | |
| 21 | gbmc_ncsi_br_pub_addr_update() { |
| 22 | [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 23 | [ "$gbmc_ncsi_br_pub_addr_confip" != "$gbmc_ncsi_br_pub_addr_lastip" ] || return |
| 24 | gbmc_ncsi_br_pub_addr_confip="$gbmc_ncsi_br_pub_addr_lastip" |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 25 | |
| 26 | printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \ |
| 27 | "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2 |
| 28 | |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 29 | local pfx_bytes=() |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 30 | if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 31 | ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip" |
William A. Kennington III | 10dc43a | 2022-05-10 10:30:25 -0700 | [diff] [blame^] | 32 | # Ensure we have a /64 or an fdxx address |
| 33 | if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] == 0 )); then |
| 34 | local i |
| 35 | for (( i = 8; i < 16; ++i )); do |
| 36 | if (( pfx_bytes[$i] != 0 )); then |
| 37 | pfx_bytes=() |
| 38 | break |
| 39 | fi |
| 40 | done |
| 41 | fi |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 42 | fi |
| 43 | |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame] | 44 | local contents= |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 45 | if (( ${#pfx_bytes[@]} != 0 )); then |
| 46 | pfx_bytes[8]=0xfd |
William A. Kennington III | 10dc43a | 2022-05-10 10:30:25 -0700 | [diff] [blame^] | 47 | # Save our old prefix assuming we have one |
| 48 | local old_offset="${pfx_bytes[9]}" |
| 49 | if (( old_offset == 0 )); then |
| 50 | old_offset=0x01 |
| 51 | else |
| 52 | pfx_bytes[9]=0x00 |
| 53 | fi |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 54 | local stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 10dc43a | 2022-05-10 10:30:25 -0700 | [diff] [blame^] | 55 | pfx_bytes[9]="$old_offset" |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 56 | local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame] | 57 | read -r -d '' contents <<EOF |
| 58 | [Network] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 59 | Address=$ncsi_pfx/128 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 60 | IPv6PrefixDelegation=yes |
| 61 | [IPv6PrefixDelegation] |
| 62 | RouterLifetimeSec=60 |
| 63 | [IPv6Prefix] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 64 | Prefix=$stateless_pfx/80 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 65 | PreferredLifetimeSec=60 |
| 66 | ValidLifetimeSec=60 |
| 67 | [IPv6RoutePrefix] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 68 | Route=$ncsi_pfx/80 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 69 | LifetimeSec=60 |
William A. Kennington III | c1a3cc2 | 2021-05-12 13:32:38 -0700 | [diff] [blame] | 70 | [Route] |
William A. Kennington III | a22b445 | 2021-11-04 22:57:43 -0700 | [diff] [blame] | 71 | Destination=$stateless_pfx/76 |
William A. Kennington III | c1a3cc2 | 2021-05-12 13:32:38 -0700 | [diff] [blame] | 72 | Type=unreachable |
| 73 | Metric=1024 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 74 | EOF |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 75 | # Delete DHCP configured addresses if we have a host published address |
| 76 | rm -f /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 77 | fi |
| 78 | |
| 79 | local file |
| 80 | for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do |
| 81 | mkdir -p -m 755 "$(dirname "$file")" |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame] | 82 | if [ -z "$contents" ]; then |
| 83 | rm -f "$file" |
| 84 | else |
| 85 | printf '%s' "$contents" >"$file" |
| 86 | fi |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 87 | done |
| 88 | |
William A. Kennington III | 8fb9258 | 2021-05-10 03:15:56 -0700 | [diff] [blame] | 89 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 90 | # currently check the mtime of drop-in files. |
| 91 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network |
| 92 | |
| 93 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 94 | networkctl reload |
| 95 | networkctl reconfigure gbmcbr |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 96 | fi |
| 97 | } |
| 98 | |
| 99 | gbmc_ncsi_br_pub_addr_hook() { |
| 100 | if [ "$change" = 'init' ]; then |
| 101 | gbmc_ncsi_br_pub_addr_init=1 |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 102 | gbmc_ip_monitor_defer |
| 103 | elif [ "$change" = 'defer' ]; then |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 104 | gbmc_ncsi_br_pub_addr_update |
| 105 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && |
William A. Kennington III | cbd9ef0 | 2021-11-04 20:52:17 -0700 | [diff] [blame] | 106 | [ "$scope" = 'global' -a "$fam" = 'inet6' ] && |
| 107 | [[ "$flags" != *deprecated* ]]; then |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 108 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 109 | gbmc_ncsi_br_pub_addr_lastip="$ip" |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 110 | gbmc_ip_monitor_defer |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 111 | fi |
| 112 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 113 | gbmc_ncsi_br_pub_addr_lastip= |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 114 | gbmc_ip_monitor_defer |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 115 | fi |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) |
| 120 | |
| 121 | gbmc_ncsi_br_pub_addr_lib=1 |