blob: 6144617d471e055bc41e419670e743df5841d852 [file] [log] [blame]
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -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
16source /usr/share/network/lib.sh || exit
17source /usr/libexec/ncsid_lib.sh || exit
18
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070019NCSI_IF='@NCSI_IF@'
20
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070021old_pfx=
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070022old_fqdn=
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070023old_rtr=
24
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070025set_host() {
26 [ -n "$host" -a -n "$domain" -a -n "$hextet" ] || return
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070027
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070028 local fqdn="$host-n$hextet.$domain"
29 [ "$fqdn" != "$old_fqdn" ] || return
30 old_fqdn="$fqdn"
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070031
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070032 echo "Found hostname $fqdn" >&2
33 hostnamectl set-hostname "$fqdn" || true
34}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070035
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070036set_net() {
37 [ -n "$pfx" -a -n "$rtr" ] || return
38 [[ "$pfx" != "$old_pfx" || "$rtr" != "$old_rtr" ]] || return
39 old_pfx="$pfx"
40 old_rtr="$rtr"
41
42 echo "Found prefix $pfx from $rtr" >&2
43
44 # Delete any stale IP Addresses from the primary interface as we won't use them
45 UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '0.0.0.0' '0' || true
46 UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '::' '0' || true
47
48 read -r -d '' contents <<EOF
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070049[Network]
50Address=$pfx/128
51IPv6PrefixDelegation=yes
52[IPv6PrefixDelegation]
53RouterLifetimeSec=60
54[IPv6Prefix]
55Prefix=$stateless_pfx/80
56PreferredLifetimeSec=60
57ValidLifetimeSec=60
58[IPv6RoutePrefix]
59Route=$pfx/80
60LifetimeSec=60
61[Route]
62Destination=$stateless_pfx/76
63Type=unreachable
64Metric=1024
65EOF
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070066 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do
67 mkdir -p -m 755 "$(dirname "$file")"
68 printf '%s' "$contents" >"$file"
69 done
70 touch -c /lib/systemd/network/*-bmc-gbmcbr.network || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070071
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070072 contents='[Network]'$'\n'
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070073 contents+="Gateway=$rtr"$'\n'
74 for file in /run/systemd/network/{00,}-bmc-"$NCSI_IF".network.d/49-public-ra.conf; do
75 mkdir -p -m 755 "$(dirname "$file")"
76 printf '%s' "$contents" >"$file"
77 done
78 touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070079
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070080 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
81 networkctl reload && networkctl reconfigure gbmcbr "$NCSI_IF" || true
82 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070083}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070084
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070085w=60
86while true; do
87 start=$SECONDS
88 while read line; do
89 if [ -z "$line" ]; then
90 hextet=
91 pfx=
92 host=
93 domain=
94 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
95 t_pfx="${BASH_REMATCH[1]}"
96 t_pfx_len="${BASH_REMATCH[2]}"
97 ip_to_bytes t_pfx_b "$t_pfx" || continue
98 (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
99 (( t_pfx_b[9] |= 1 ))
100 hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
101 pfx="$(ip_bytes_to_str t_pfx_b)"
102 (( t_pfx_b[9] &= 0xf0 ))
103 stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
104 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then
105 host="${BASH_REMATCH[1]}"
106 domain="${BASH_REMATCH[2]}"
107 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
108 rtr="${BASH_REMATCH[1]}"
109 set_net || true
110 set_host || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700111 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -0700112 done < <(rdisc6 -d -m "$NCSI_IF" -w $(( w * 1000 )) 2>/dev/null)
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700113 # If rdisc6 exits early we still want to wait the full `w` time before
114 # starting again.
115 (( timeout = start + w - SECONDS ))
116 sleep $(( timeout < 0 ? 0 : timeout ))
117done