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 | |
William A. Kennington III | bef990f | 2022-02-08 16:50:30 -0800 | [diff] [blame^] | 40 | # Write out the current PID and cleanup when complete |
| 41 | trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT |
| 42 | echo "$$" >/run/gbmc-br-dhcp.pid |
| 43 | |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 44 | if [ "$1" = bound ]; then |
| 45 | # Variable is from the environment via udhcpc6 |
| 46 | # shellcheck disable=SC2154 |
| 47 | echo "DHCPv6(gbmcbr): $ipv6/128" >&2 |
| 48 | |
| 49 | pfx_bytes=() |
| 50 | ip_to_bytes pfx_bytes "$ipv6" |
| 51 | # Ensure we are a BMC and have a suffix nibble, the 0th index is reserved |
| 52 | if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then |
| 53 | echo "Invalid address" >&2 |
| 54 | exit |
| 55 | fi |
| 56 | # Ensure we don't have more than a /80 address |
| 57 | for (( i = 10; i < 16; ++i )); do |
| 58 | if (( pfx_bytes[i] != 0 )); then |
| 59 | echo "Invalid address" >&2 |
| 60 | exit |
| 61 | fi |
| 62 | done |
| 63 | |
| 64 | pfx="$(ip_bytes_to_str pfx_bytes)" |
| 65 | (( pfx_bytes[9] &= 0xf0 )) |
| 66 | stateless_pfx="$(ip_bytes_to_str pfx_bytes)" |
| 67 | read -r -d '' contents <<EOF |
| 68 | [Network] |
| 69 | Address=$pfx/128 |
| 70 | IPv6PrefixDelegation=yes |
| 71 | [IPv6PrefixDelegation] |
| 72 | RouterLifetimeSec=60 |
| 73 | [IPv6Prefix] |
| 74 | Prefix=$stateless_pfx/80 |
| 75 | PreferredLifetimeSec=60 |
| 76 | ValidLifetimeSec=60 |
| 77 | [IPv6RoutePrefix] |
| 78 | Route=$pfx/80 |
| 79 | LifetimeSec=60 |
| 80 | [Route] |
| 81 | Destination=$stateless_pfx/76 |
| 82 | Type=unreachable |
| 83 | Metric=1024 |
| 84 | EOF |
| 85 | |
| 86 | for file in /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do |
| 87 | mkdir -p "$(dirname "$file")" |
| 88 | printf '%s' "$contents" >"$file" |
| 89 | done |
| 90 | |
| 91 | # Ensure that systemd-networkd performs a reconfiguration as it doesn't |
| 92 | # currently check the mtime of drop-in files. |
| 93 | touch -c /lib/systemd/network/*-bmc-gbmcbr.network |
| 94 | |
| 95 | if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then |
| 96 | networkctl reload && networkctl reconfigure gbmcbr |
| 97 | fi |
| 98 | |
William A. Kennington III | d1a214d | 2021-12-06 15:26:46 -0800 | [diff] [blame] | 99 | if [ -n "${fqdn-}" ]; then |
| 100 | echo "Using hostname $fqdn" >&2 |
| 101 | hostnamectl set-hostname "$fqdn" || true |
| 102 | fi |
| 103 | |
William A. Kennington III | fe08f02 | 2022-02-11 10:04:03 -0800 | [diff] [blame] | 104 | gbmc_br_dhcp_run_hooks || exit |
William A. Kennington III | 7e2d05d | 2022-02-12 15:36:05 -0800 | [diff] [blame] | 105 | |
| 106 | # Ensure that the installer knows we have completed processing DHCP by |
| 107 | # running a service that reports completion |
| 108 | systemctl start dhcp-done --no-block |
William A. Kennington III | b174c18 | 2021-11-03 14:54:51 -0700 | [diff] [blame] | 109 | fi |