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 | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 22 | # SC can't find this path during repotest |
| 23 | # shellcheck disable=SC1091 |
| 24 | source /usr/share/network/lib.sh || exit |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame^] | 25 | # SC can't find this path during repotest |
| 26 | # shellcheck disable=SC1091 |
| 27 | source /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. |
| 31 | gbmc_br_source_dir /usr/share/gbmc-br-dhcp || exit |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 32 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame] | 33 | # Write out the current PID and cleanup when complete |
| 34 | trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT |
| 35 | echo "$$" >/run/gbmc-br-dhcp.pid |
| 36 | |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 37 | if [ "$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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame^] | 47 | exit 1 |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 48 | 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 III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame^] | 53 | exit 1 |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 54 | fi |
| 55 | done |
| 56 | |
| 57 | pfx="$(ip_bytes_to_str pfx_bytes)" |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame^] | 58 | gbmc_br_set_ip "$pfx" || exit |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 59 | |
William A. Kennington III | d1a214d | 2021-12-06 15:26:46 -0800 | [diff] [blame] | 60 | if [ -n "${fqdn-}" ]; then |
| 61 | echo "Using hostname $fqdn" >&2 |
| 62 | hostnamectl set-hostname "$fqdn" || true |
| 63 | fi |
| 64 | |
William A. Kennington III | 37e6f39 | 2022-05-16 16:19:11 -0700 | [diff] [blame^] | 65 | gbmc_br_run_hooks GBMC_BR_DHCP_HOOKS || exit |
William A. Kennington III | 7e2d05d | 2022-02-12 15:36:05 -0800 | [diff] [blame] | 66 | |
| 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 III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 70 | fi |