William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 1 | #!/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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 18 | # Shellcheck does not understand how this gets referenced |
| 19 | # shellcheck disable=SC2034 |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 20 | GBMC_BR_DHCP_HOOKS=() |
| 21 | |
William A. Kennington III | c3d512a | 2022-07-20 14:50:29 -0700 | [diff] [blame] | 22 | # A dict of outstanding items that should prevent DHCP completion |
| 23 | declare -A GBMC_BR_DHCP_OUTSTANDING=() |
| 24 | |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 25 | # SC can't find this path during repotest |
| 26 | # shellcheck disable=SC1091 |
| 27 | source /usr/share/network/lib.sh || exit |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 28 | # SC can't find this path during repotest |
| 29 | # shellcheck disable=SC1091 |
| 30 | source /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. |
| 34 | gbmc_br_source_dir /usr/share/gbmc-br-dhcp || exit |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 35 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 36 | # Write out the current PID and cleanup when complete |
| 37 | trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT |
| 38 | echo "$$" >/run/gbmc-br-dhcp.pid |
| 39 | |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 40 | if [ "$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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 50 | exit 1 |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 51 | 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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 56 | exit 1 |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 57 | fi |
| 58 | done |
| 59 | |
| 60 | pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 61 | gbmc_br_set_ip "$pfx" || exit |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 62 | |
William A. Kennington III | d1a214d | 2021-12-06 15:26:46 -0800 | [diff] [blame] | 63 | if [ -n "${fqdn-}" ]; then |
| 64 | echo "Using hostname $fqdn" >&2 |
| 65 | hostnamectl set-hostname "$fqdn" || true |
| 66 | fi |
| 67 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame] | 68 | gbmc_br_run_hooks GBMC_BR_DHCP_HOOKS || exit |
William A. Kennington III | 7e2d05d | 2022-02-12 15:36:05 -0800 | [diff] [blame] | 69 | |
William A. Kennington III | c3d512a | 2022-07-20 14:50:29 -0700 | [diff] [blame] | 70 | # 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 III | 7e2d05d | 2022-02-12 15:36:05 -0800 | [diff] [blame] | 76 | # Ensure that the installer knows we have completed processing DHCP by |
| 77 | # running a service that reports completion |
William A. Kennington III | c3d512a | 2022-07-20 14:50:29 -0700 | [diff] [blame] | 78 | echo 'Start DHCP Done' >&2 |
Yuxiao Zhang | 8fe218a | 2023-03-30 15:55:30 -0700 | [diff] [blame] | 79 | systemctl start dhcp-done@DONE --no-block |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 80 | fi |