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. |
| 18 | GBMC_BR_DHCP_HOOKS=() |
| 19 | |
| 20 | # Load configurations from a known location in the filesystem to populate |
| 21 | # hooks that are executed after each event. |
| 22 | shopt -s nullglob |
| 23 | for conf in /usr/share/gbmc-br-dhcp/*.sh; do |
| 24 | # SC doesn't like dynamic source loading |
| 25 | # shellcheck disable=SC1090 |
| 26 | source "$conf" |
| 27 | done |
| 28 | |
| 29 | gbmc_br_dhcp_run_hooks() { |
| 30 | local hook |
| 31 | for hook in "${GBMC_BR_DHCP_HOOKS[@]}"; do |
William A. Kennington III | fe08f02 | 2022-02-11 10:04:03 -0800 | [diff] [blame] | 32 | "$hook" || return |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 33 | done |
| 34 | } |
| 35 | |
| 36 | # SC can't find this path during repotest |
| 37 | # shellcheck disable=SC1091 |
| 38 | source /usr/share/network/lib.sh || exit |
| 39 | |
| 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 |
| 50 | exit |
| 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 |
| 56 | exit |
| 57 | fi |
| 58 | done |
| 59 | |
| 60 | pfx="$(ip_bytes_to_str pfx_bytes)" |
| 61 | (( pfx_bytes[9] &= 0xf0 )) |
| 62 | stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 63 | read -r -d '' contents <<EOF |
| 64 | [Network] |
| 65 | Address=$pfx/128 |
| 66 | IPv6PrefixDelegation=yes |
| 67 | [IPv6PrefixDelegation] |
| 68 | RouterLifetimeSec=60 |
| 69 | [IPv6Prefix] |
| 70 | Prefix=$stateless_pfx/80 |
| 71 | PreferredLifetimeSec=60 |
| 72 | ValidLifetimeSec=60 |
| 73 | [IPv6RoutePrefix] |
| 74 | Route=$pfx/80 |
| 75 | LifetimeSec=60 |
| 76 | [Route] |
| 77 | Destination=$stateless_pfx/76 |
| 78 | Type=unreachable |
| 79 | Metric=1024 |
| 80 | EOF |
| 81 | |
| 82 | for file in /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do |
| 83 | mkdir -p "$(dirname "$file")" |
| 84 | printf '%s' "$contents" >"$file" |
| 85 | done |
| 86 | |
| 87 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 88 | # currently check the mtime of drop-in files. |
| 89 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network |
| 90 | |
| 91 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 92 | networkctl reload && networkctl reconfigure gbmcbr |
| 93 | fi |
| 94 | |
William A. Kennington III | d1a214d | 2021-12-06 15:26:46 -0800 | [diff] [blame] | 95 | if [ -n "${fqdn-}" ]; then |
| 96 | echo "Using hostname $fqdn" >&2 |
| 97 | hostnamectl set-hostname "$fqdn" || true |
| 98 | fi |
| 99 | |
William A. Kennington III | fe08f02 | 2022-02-11 10:04:03 -0800 | [diff] [blame] | 100 | gbmc_br_dhcp_run_hooks || exit |
William A. Kennington III | 7e2d05d | 2022-02-12 15:36:05 -0800 | [diff] [blame^] | 101 | |
| 102 | # Ensure that the installer knows we have completed processing DHCP by |
| 103 | # running a service that reports completion |
| 104 | systemctl start dhcp-done --no-block |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 105 | fi |