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 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 15 | [ -n "${gbmc_ncsi_br_pub_addr_lib-}" ] && return |
| 16 | |
| 17 | [ ! -e /usr/share/gbmc-br-lib.sh ] && return |
| 18 | |
| 19 | source /usr/share/network/lib.sh || exit |
| 20 | source /usr/share/gbmc-br-lib.sh || exit |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 21 | |
| 22 | gbmc_ncsi_br_pub_addr_init= |
| 23 | gbmc_ncsi_br_pub_addr_lastip= |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 24 | gbmc_ncsi_br_pub_addr_confip= |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 25 | |
| 26 | gbmc_ncsi_br_pub_addr_update() { |
| 27 | [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 28 | [ "$gbmc_ncsi_br_pub_addr_confip" != "$gbmc_ncsi_br_pub_addr_lastip" ] || return |
| 29 | 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] | 30 | |
| 31 | printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \ |
| 32 | "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2 |
| 33 | |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 34 | local pfx_bytes=() |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 35 | if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 36 | 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] | 37 | # Ensure we have a /64 or an fdxx address |
| 38 | if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] == 0 )); then |
| 39 | local i |
| 40 | for (( i = 8; i < 16; ++i )); do |
| 41 | if (( pfx_bytes[$i] != 0 )); then |
| 42 | pfx_bytes=() |
| 43 | break |
| 44 | fi |
| 45 | done |
| 46 | fi |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 47 | fi |
| 48 | |
William A. Kennington III | 0317853 | 2021-11-04 22:49:39 -0700 | [diff] [blame] | 49 | local contents= |
William A. Kennington III | bdbe7ce | 2021-05-10 16:48:43 -0700 | [diff] [blame] | 50 | if (( ${#pfx_bytes[@]} != 0 )); then |
| 51 | pfx_bytes[8]=0xfd |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 52 | # We never want to use the stateless pfx |
| 53 | if (( pfx_bytes[9] == 0 )); then |
| 54 | pfx_bytes[9]=0x01 |
William A. Kennington III | 10dc43a | 2022-05-10 10:30:25 -0700 | [diff] [blame] | 55 | fi |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 56 | # Remove any existing persisted IP |
| 57 | gbmc_br_set_ip |
| 58 | # Load the IP to the bridge non-persistently |
| 59 | gbmc_br_reload_ip "$(ip_bytes_to_str pfx_bytes)" |
| 60 | else |
| 61 | gbmc_br_reload_ip |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 62 | fi |
| 63 | } |
| 64 | |
| 65 | gbmc_ncsi_br_pub_addr_hook() { |
| 66 | if [ "$change" = 'init' ]; then |
| 67 | gbmc_ncsi_br_pub_addr_init=1 |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 68 | gbmc_ip_monitor_defer |
| 69 | elif [ "$change" = 'defer' ]; then |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 70 | gbmc_ncsi_br_pub_addr_update |
| 71 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && |
William A. Kennington III | cbd9ef0 | 2021-11-04 20:52:17 -0700 | [diff] [blame] | 72 | [ "$scope" = 'global' -a "$fam" = 'inet6' ] && |
| 73 | [[ "$flags" != *deprecated* ]]; then |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 74 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 75 | gbmc_ncsi_br_pub_addr_lastip="$ip" |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 76 | gbmc_ip_monitor_defer |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 77 | fi |
| 78 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 79 | gbmc_ncsi_br_pub_addr_lastip= |
William A. Kennington III | 58ac434 | 2021-11-05 04:15:36 -0700 | [diff] [blame] | 80 | gbmc_ip_monitor_defer |
William A. Kennington III | e99168a | 2021-03-10 23:40:47 -0800 | [diff] [blame] | 81 | fi |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) |
| 86 | |
| 87 | gbmc_ncsi_br_pub_addr_lib=1 |