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 | |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 26 | local pfx_bytes=() |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 27 | if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 28 | ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip" |
| 29 | # Ensure we don't have more than a /64 address |
| 30 | local i |
| 31 | for (( i = 8; i < 16; ++i )); do |
| 32 | if (( pfx_bytes[$i] != 0 )); then |
| 33 | pfx_bytes=() |
| 34 | break |
| 35 | fi |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 36 | done |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 37 | fi |
| 38 | |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame^] | 39 | local contents= |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 40 | if (( ${#pfx_bytes[@]} != 0 )); then |
| 41 | pfx_bytes[8]=0xfd |
| 42 | local stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 43 | pfx_bytes[9]=0x01 |
| 44 | local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame^] | 45 | read -r -d '' contents <<EOF |
| 46 | [Network] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 47 | Address=$ncsi_pfx/128 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 48 | IPv6PrefixDelegation=yes |
| 49 | [IPv6PrefixDelegation] |
| 50 | RouterLifetimeSec=60 |
| 51 | [IPv6Prefix] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 52 | Prefix=$stateless_pfx/80 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 53 | PreferredLifetimeSec=60 |
| 54 | ValidLifetimeSec=60 |
| 55 | [IPv6RoutePrefix] |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 56 | Route=$ncsi_pfx/80 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 57 | LifetimeSec=60 |
William A. Kennington III | c1a3cc2 | 2021-05-12 13:32:38 -0700 | [diff] [blame] | 58 | [Route] |
William A. Kennington III | a22b445 | 2021-11-04 22:57:43 -0700 | [diff] [blame] | 59 | Destination=$stateless_pfx/76 |
William A. Kennington III | c1a3cc2 | 2021-05-12 13:32:38 -0700 | [diff] [blame] | 60 | Type=unreachable |
| 61 | Metric=1024 |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 62 | EOF |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 63 | fi |
| 64 | |
| 65 | local file |
| 66 | for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do |
| 67 | mkdir -p -m 755 "$(dirname "$file")" |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame^] | 68 | if [ -z "$contents" ]; then |
| 69 | rm -f "$file" |
| 70 | else |
| 71 | printf '%s' "$contents" >"$file" |
| 72 | fi |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 73 | done |
| 74 | |
William A. Kennington III | 8fb9258 | 2021-05-10 03:15:56 -0700 | [diff] [blame] | 75 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 76 | # currently check the mtime of drop-in files. |
| 77 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network |
| 78 | |
| 79 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 80 | networkctl reload |
| 81 | networkctl reconfigure gbmcbr |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 82 | fi |
| 83 | } |
| 84 | |
| 85 | gbmc_ncsi_br_pub_addr_hook() { |
| 86 | if [ "$change" = 'init' ]; then |
| 87 | gbmc_ncsi_br_pub_addr_init=1 |
| 88 | gbmc_ncsi_br_pub_addr_update |
| 89 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && |
| 90 | [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then |
| 91 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 92 | gbmc_ncsi_br_pub_addr_lastip="$ip" |
| 93 | gbmc_ncsi_br_pub_addr_update |
| 94 | fi |
| 95 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 96 | gbmc_ncsi_br_pub_addr_lastip= |
| 97 | gbmc_ncsi_br_pub_addr_update |
| 98 | fi |
| 99 | fi |
| 100 | } |
| 101 | |
| 102 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) |
| 103 | |
| 104 | gbmc_ncsi_br_pub_addr_lib=1 |