summaryrefslogtreecommitdiff
path: root/meta-google/recipes-google/ncsi/files/gbmc-ncsi-br-pub-addr.sh.in
blob: 793403348eff7fc26e971fa2d862d48ae4c20b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[ -z "${gbmc_ncsi_br_pub_addr_lib-}" ] || return

gbmc_ncsi_br_pub_addr_init=
gbmc_ncsi_br_pub_addr_lastip=
gbmc_ncsi_br_pub_addr_confip=

gbmc_ncsi_br_pub_addr_update() {
  [ -n "$gbmc_ncsi_br_pub_addr_init" ] || return
  [ "$gbmc_ncsi_br_pub_addr_confip" != "$gbmc_ncsi_br_pub_addr_lastip" ] || return
  gbmc_ncsi_br_pub_addr_confip="$gbmc_ncsi_br_pub_addr_lastip"

  printf 'gBMC Bridge Pub Addr from NCSI: %s\n' \
    "${gbmc_ncsi_br_pub_addr_lastip:-(deleted)}" >&2

  local pfx_bytes=()
  if [ -n "$gbmc_ncsi_br_pub_addr_lastip" ]; then
    ip_to_bytes pfx_bytes "$gbmc_ncsi_br_pub_addr_lastip"
    # Ensure we don't have more than a /64 address
    local i
    for (( i = 8; i < 16; ++i )); do
      if (( pfx_bytes[$i] != 0 )); then
        pfx_bytes=()
        break
      fi
    done
  fi

  local contents=
  if (( ${#pfx_bytes[@]} != 0 )); then
    pfx_bytes[8]=0xfd
    local stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
    pfx_bytes[9]=0x01
    local ncsi_pfx="$(ip_bytes_to_str pfx_bytes)"
    read -r -d '' contents <<EOF
[Network]
Address=$ncsi_pfx/128
IPv6PrefixDelegation=yes
[IPv6PrefixDelegation]
RouterLifetimeSec=60
[IPv6Prefix]
Prefix=$stateless_pfx/80
PreferredLifetimeSec=60
ValidLifetimeSec=60
[IPv6RoutePrefix]
Route=$ncsi_pfx/80
LifetimeSec=60
[Route]
Destination=$stateless_pfx/76
Type=unreachable
Metric=1024
EOF
    # Delete DHCP configured addresses if we have a host published address
    rm -f /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf
  fi

  local file
  for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
    mkdir -p -m 755 "$(dirname "$file")"
    if [ -z "$contents" ]; then
      rm -f "$file"
    else
      printf '%s' "$contents" >"$file"
    fi
  done

  # Ensure that systemd-networkd performs a reconfiguration as it doesn't
  # currently check the mtime of drop-in files.
  touch -c /lib/systemd/network/*-bmc-gbmcbr.network

  if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
    networkctl reload
    networkctl reconfigure gbmcbr
  fi
}

gbmc_ncsi_br_pub_addr_hook() {
  if [ "$change" = 'init' ]; then
    gbmc_ncsi_br_pub_addr_init=1
    gbmc_ip_monitor_defer
  elif [ "$change" = 'defer' ]; then
    gbmc_ncsi_br_pub_addr_update
  elif [ "$change" = 'addr' -a "$intf" = '@NCSI_IF@' ] &&
     [ "$scope" = 'global' -a "$fam" = 'inet6' ] &&
     [[ "$flags" != *deprecated* ]]; then
    if [ "$action" = 'add' -a "$ip" != "$gbmc_ncsi_br_pub_addr_lastip" ]; then
      gbmc_ncsi_br_pub_addr_lastip="$ip"
      gbmc_ip_monitor_defer
    fi
    if [ "$action" = 'del' -a "$ip" = "$gbmc_ncsi_br_pub_addr_lastip" ]; then
      gbmc_ncsi_br_pub_addr_lastip=
      gbmc_ip_monitor_defer
    fi
  fi
}

GBMC_IP_MONITOR_HOOKS+=(gbmc_ncsi_br_pub_addr_hook)

gbmc_ncsi_br_pub_addr_lib=1