blob: 6557125a9eb32c2ad0b96843e6695f9c3f1736b8 [file] [log] [blame]
William A. Kennington IIIb174c182021-11-03 14:54:51 -07001#!/bin/bash
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# A list of functions which get executed for each bound DHCP lease.
17# These are configured by the files included below.
William A. Kennington III37e6f392022-05-16 16:19:11 -070018# Shellcheck does not understand how this gets referenced
19# shellcheck disable=SC2034
William A. Kennington IIIb174c182021-11-03 14:54:51 -070020GBMC_BR_DHCP_HOOKS=()
21
William A. Kennington IIIc3d512a2022-07-20 14:50:29 -070022# A dict of outstanding items that should prevent DHCP completion
23declare -A GBMC_BR_DHCP_OUTSTANDING=()
24
William A. Kennington IIIb174c182021-11-03 14:54:51 -070025# SC can't find this path during repotest
26# shellcheck disable=SC1091
27source /usr/share/network/lib.sh || exit
William A. Kennington III37e6f392022-05-16 16:19:11 -070028# SC can't find this path during repotest
29# shellcheck disable=SC1091
30source /usr/share/gbmc-br-lib.sh || exit
31
32# Load configurations from a known location in the filesystem to populate
33# hooks that are executed after each event.
34gbmc_br_source_dir /usr/share/gbmc-br-dhcp || exit
William A. Kennington IIIb174c182021-11-03 14:54:51 -070035
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080036# Write out the current PID and cleanup when complete
37trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT
38echo "$$" >/run/gbmc-br-dhcp.pid
39
William A. Kennington IIIb174c182021-11-03 14:54:51 -070040if [ "$1" = bound ]; then
41 # Variable is from the environment via udhcpc6
42 # shellcheck disable=SC2154
43 echo "DHCPv6(gbmcbr): $ipv6/128" >&2
44
45 pfx_bytes=()
46 ip_to_bytes pfx_bytes "$ipv6"
47 # Ensure we are a BMC and have a suffix nibble, the 0th index is reserved
48 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
49 echo "Invalid address" >&2
William A. Kennington III37e6f392022-05-16 16:19:11 -070050 exit 1
William A. Kennington IIIb174c182021-11-03 14:54:51 -070051 fi
52 # Ensure we don't have more than a /80 address
53 for (( i = 10; i < 16; ++i )); do
54 if (( pfx_bytes[i] != 0 )); then
55 echo "Invalid address" >&2
William A. Kennington III37e6f392022-05-16 16:19:11 -070056 exit 1
William A. Kennington IIIb174c182021-11-03 14:54:51 -070057 fi
58 done
59
60 pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III37e6f392022-05-16 16:19:11 -070061 gbmc_br_set_ip "$pfx" || exit
William A. Kennington IIIb174c182021-11-03 14:54:51 -070062
William A. Kennington IIId1a214d2021-12-06 15:26:46 -080063 if [ -n "${fqdn-}" ]; then
64 echo "Using hostname $fqdn" >&2
65 hostnamectl set-hostname "$fqdn" || true
66 fi
67
William A. Kennington III37e6f392022-05-16 16:19:11 -070068 gbmc_br_run_hooks GBMC_BR_DHCP_HOOKS || exit
William A. Kennington III7e2d05d2022-02-12 15:36:05 -080069
William A. Kennington IIIc3d512a2022-07-20 14:50:29 -070070 # If any of our hooks had expectations we should fail here
71 if [ "${#GBMC_BR_DHCP_OUTSTANDING[@]}" -gt 0 ]; then
72 echo "Not done with DHCP process: ${!GBMC_BR_DHCP_OUTSTANDING[*]}" >&2
73 exit 1
74 fi
75
William A. Kennington III7e2d05d2022-02-12 15:36:05 -080076 # Ensure that the installer knows we have completed processing DHCP by
77 # running a service that reports completion
William A. Kennington IIIc3d512a2022-07-20 14:50:29 -070078 echo 'Start DHCP Done' >&2
Yuxiao Zhang8fe218a2023-03-30 15:55:30 -070079 systemctl start dhcp-done@DONE --no-block
William A. Kennington IIIb174c182021-11-03 14:54:51 -070080fi