blob: 19fa7b10100370be7c44951776fbaefdd0e2f5a2 [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
William A. Kennington IIIbef990f2022-02-08 16:50:30 -080040# Write out the current PID and cleanup when complete
41trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT
42echo "$$" >/run/gbmc-br-dhcp.pid
43
William A. Kennington IIIb174c182021-11-03 14:54:51 -070044if [ "$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]
69Address=$pfx/128
70IPv6PrefixDelegation=yes
71[IPv6PrefixDelegation]
72RouterLifetimeSec=60
73[IPv6Prefix]
74Prefix=$stateless_pfx/80
75PreferredLifetimeSec=60
76ValidLifetimeSec=60
77[IPv6RoutePrefix]
78Route=$pfx/80
79LifetimeSec=60
80[Route]
81Destination=$stateless_pfx/76
82Type=unreachable
83Metric=1024
84EOF
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 IIId1a214d2021-12-06 15:26:46 -080099 if [ -n "${fqdn-}" ]; then
100 echo "Using hostname $fqdn" >&2
101 hostnamectl set-hostname "$fqdn" || true
102 fi
103
William A. Kennington IIIfe08f022022-02-11 10:04:03 -0800104 gbmc_br_dhcp_run_hooks || exit
William A. Kennington III7e2d05d2022-02-12 15:36:05 -0800105
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 IIIb174c182021-11-03 14:54:51 -0700109fi