blob: 9c610366517ef7c215a72183bb0cb40668fb1d7c [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 IIIb174c182021-11-03 14:54:51 -070022# SC can't find this path during repotest
23# shellcheck disable=SC1091
24source /usr/share/network/lib.sh || exit
William A. Kennington III37e6f392022-05-16 16:19:11 -070025# SC can't find this path during repotest
26# shellcheck disable=SC1091
27source /usr/share/gbmc-br-lib.sh || exit
28
29# Load configurations from a known location in the filesystem to populate
30# hooks that are executed after each event.
31gbmc_br_source_dir /usr/share/gbmc-br-dhcp || exit
William A. Kennington IIIb174c182021-11-03 14:54:51 -070032
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080033# Write out the current PID and cleanup when complete
34trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT
35echo "$$" >/run/gbmc-br-dhcp.pid
36
William A. Kennington IIIb174c182021-11-03 14:54:51 -070037if [ "$1" = bound ]; then
38 # Variable is from the environment via udhcpc6
39 # shellcheck disable=SC2154
40 echo "DHCPv6(gbmcbr): $ipv6/128" >&2
41
42 pfx_bytes=()
43 ip_to_bytes pfx_bytes "$ipv6"
44 # Ensure we are a BMC and have a suffix nibble, the 0th index is reserved
45 if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
46 echo "Invalid address" >&2
William A. Kennington III37e6f392022-05-16 16:19:11 -070047 exit 1
William A. Kennington IIIb174c182021-11-03 14:54:51 -070048 fi
49 # Ensure we don't have more than a /80 address
50 for (( i = 10; i < 16; ++i )); do
51 if (( pfx_bytes[i] != 0 )); then
52 echo "Invalid address" >&2
William A. Kennington III37e6f392022-05-16 16:19:11 -070053 exit 1
William A. Kennington IIIb174c182021-11-03 14:54:51 -070054 fi
55 done
56
57 pfx="$(ip_bytes_to_str pfx_bytes)"
William A. Kennington III37e6f392022-05-16 16:19:11 -070058 gbmc_br_set_ip "$pfx" || exit
William A. Kennington IIIb174c182021-11-03 14:54:51 -070059
William A. Kennington IIId1a214d2021-12-06 15:26:46 -080060 if [ -n "${fqdn-}" ]; then
61 echo "Using hostname $fqdn" >&2
62 hostnamectl set-hostname "$fqdn" || true
63 fi
64
William A. Kennington III37e6f392022-05-16 16:19:11 -070065 gbmc_br_run_hooks GBMC_BR_DHCP_HOOKS || exit
William A. Kennington III7e2d05d2022-02-12 15:36:05 -080066
67 # Ensure that the installer knows we have completed processing DHCP by
68 # running a service that reports completion
69 systemctl start dhcp-done --no-block
William A. Kennington IIIb174c182021-11-03 14:54:51 -070070fi