blob: 6255f708583a1cb3ec688e30b27a48a859421572 [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
William A. Kennington III7843a812021-12-09 14:17:10 -080044 # We no longer need NCSId if we are in this configuration
45 systemctl stop --no-block ncsid@"$NCSI_IF" || true
46
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070047 # Delete any stale IP Addresses from the primary interface as we won't use them
48 UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '0.0.0.0' '0' || true
49 UpdateIP xyz.openbmc_project.Network "$NCSI_IF" '::' '0' || true
50
51 read -r -d '' contents <<EOF
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070052[Network]
53Address=$pfx/128
54IPv6PrefixDelegation=yes
55[IPv6PrefixDelegation]
56RouterLifetimeSec=60
57[IPv6Prefix]
58Prefix=$stateless_pfx/80
59PreferredLifetimeSec=60
60ValidLifetimeSec=60
61[IPv6RoutePrefix]
62Route=$pfx/80
63LifetimeSec=60
64[Route]
65Destination=$stateless_pfx/76
66Type=unreachable
67Metric=1024
68EOF
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070069 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do
70 mkdir -p -m 755 "$(dirname "$file")"
71 printf '%s' "$contents" >"$file"
72 done
73 touch -c /lib/systemd/network/*-bmc-gbmcbr.network || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070074
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070075 contents='[Network]'$'\n'
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070076 contents+="Gateway=$rtr"$'\n'
77 for file in /run/systemd/network/{00,}-bmc-"$NCSI_IF".network.d/49-public-ra.conf; do
78 mkdir -p -m 755 "$(dirname "$file")"
79 printf '%s' "$contents" >"$file"
80 done
81 touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070082
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070083 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
84 networkctl reload && networkctl reconfigure gbmcbr "$NCSI_IF" || true
85 fi
William A. Kennington III71fc1892021-12-13 14:32:20 -080086
87 # DHCP Relay workaround until alternate source port is supported
88 # TODO: Remove this once internal relaying cleanups land
89 gbmc-ncsi-smartnic-wa.sh || true
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070090}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070091
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070092w=60
93while true; do
94 start=$SECONDS
95 while read line; do
96 if [ -z "$line" ]; then
97 hextet=
98 pfx=
99 host=
100 domain=
101 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
102 t_pfx="${BASH_REMATCH[1]}"
103 t_pfx_len="${BASH_REMATCH[2]}"
104 ip_to_bytes t_pfx_b "$t_pfx" || continue
105 (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
106 (( t_pfx_b[9] |= 1 ))
107 hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
108 pfx="$(ip_bytes_to_str t_pfx_b)"
109 (( t_pfx_b[9] &= 0xf0 ))
110 stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
William A. Kennington III882b78b2022-03-01 16:27:20 -0800111 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([a-z]+[0-9]+)[^.]*[.](.*.google.com)$ ]]; then
William A. Kennington III5b4b4f82021-10-15 12:22:08 -0700112 host="${BASH_REMATCH[1]}"
113 domain="${BASH_REMATCH[2]}"
114 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
115 rtr="${BASH_REMATCH[1]}"
116 set_net || true
117 set_host || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700118 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -0700119 done < <(rdisc6 -d -m "$NCSI_IF" -w $(( w * 1000 )) 2>/dev/null)
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700120 # If rdisc6 exits early we still want to wait the full `w` time before
121 # starting again.
122 (( timeout = start + w - SECONDS ))
123 sleep $(( timeout < 0 ? 0 : timeout ))
124done