blob: 1a6c71af1b924e6534c24baf96b88e8e26f540ee [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.
18GBMC_BR_DHCP_HOOKS=()
19
20# Load configurations from a known location in the filesystem to populate
21# hooks that are executed after each event.
22shopt -s nullglob
23for conf in /usr/share/gbmc-br-dhcp/*.sh; do
24 # SC doesn't like dynamic source loading
25 # shellcheck disable=SC1090
26 source "$conf"
27done
28
29gbmc_br_dhcp_run_hooks() {
30 local hook
31 for hook in "${GBMC_BR_DHCP_HOOKS[@]}"; do
William A. Kennington IIIfe08f022022-02-11 10:04:03 -080032 "$hook" || return
William A. Kennington IIIb174c182021-11-03 14:54:51 -070033 done
34}
35
36# SC can't find this path during repotest
37# shellcheck disable=SC1091
38source /usr/share/network/lib.sh || exit
39
40if [ "$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]
65Address=$pfx/128
66IPv6PrefixDelegation=yes
67[IPv6PrefixDelegation]
68RouterLifetimeSec=60
69[IPv6Prefix]
70Prefix=$stateless_pfx/80
71PreferredLifetimeSec=60
72ValidLifetimeSec=60
73[IPv6RoutePrefix]
74Route=$pfx/80
75LifetimeSec=60
76[Route]
77Destination=$stateless_pfx/76
78Type=unreachable
79Metric=1024
80EOF
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 IIId1a214d2021-12-06 15:26:46 -080095 if [ -n "${fqdn-}" ]; then
96 echo "Using hostname $fqdn" >&2
97 hostnamectl set-hostname "$fqdn" || true
98 fi
99
William A. Kennington IIIfe08f022022-02-11 10:04:03 -0800100 gbmc_br_dhcp_run_hooks || exit
William A. Kennington III7e2d05d2022-02-12 15:36:05 -0800101
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 IIIb174c182021-11-03 14:54:51 -0700105fi