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 | |
| 66 | # We only restart networkd if we know we have a management network available |
| 67 | # on the machine and networkd is already running. |
| 68 | if [ -e /lib/systemd/network/-bmc-gbmcbrdummy.network ] && \ |
| 69 | ! systemctl status systemd-networkd | grep -q inactive; then |
| 70 | echo "Restarting networkd" >&2 |
| 71 | # HACK: We can't restart systemd-networkd without coordinating with |
| 72 | # phosphor-networkd, otherwise it will sometimes detect interfaces as |
| 73 | # unmanaged because it reads administrative state to determine enabled |
| 74 | # status. Adding an IP to phosphor-networkd is guaranteed to trigger the |
| 75 | # restart we want, and systemd-network will never actually accept the |
| 76 | # new value. |
| 77 | local start=$SECONDS |
| 78 | while (( SECONDS - start < 30 )); do |
| 79 | busctl call xyz.openbmc_project.Network \ |
| 80 | /xyz/openbmc_project/network/gbmcbrdummy \ |
| 81 | xyz.openbmc_project.Network.IP.Create IP ssys \ |
| 82 | xyz.openbmc_project.Network.IP.Protocol.IPv6 ff02::1 128 '' && break |
| 83 | sleep 1 |
| 84 | done |
| 85 | fi |
| 86 | } |
| 87 | |
| 88 | gbmc_ncsi_br_pub_addr_hook() { |
| 89 | if [ "$change" = 'init' ]; then |
| 90 | gbmc_ncsi_br_pub_addr_init=1 |
| 91 | gbmc_ncsi_br_pub_addr_update |
| 92 | elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] && |
| 93 | [ "$scope" = 'global' -a "$fam" = 'inet6' ]; then |
| 94 | if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 95 | gbmc_ncsi_br_pub_addr_lastip="$ip" |
| 96 | gbmc_ncsi_br_pub_addr_update |
| 97 | fi |
| 98 | if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then |
| 99 | gbmc_ncsi_br_pub_addr_lastip= |
| 100 | gbmc_ncsi_br_pub_addr_update |
| 101 | fi |
| 102 | fi |
| 103 | } |
| 104 | |
| 105 | GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook) |
| 106 | |
| 107 | gbmc_ncsi_br_pub_addr_lib=1 |