blob: 80bd34f04ae506f761fb451766edaea7f6bd6ad3 [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'
73 contents+="Address=$pfx/128"$'\n'
74 contents+="Gateway=$rtr"$'\n'
75 for file in /run/systemd/network/{00,}-bmc-"$NCSI_IF".network.d/49-public-ra.conf; do
76 mkdir -p -m 755 "$(dirname "$file")"
77 printf '%s' "$contents" >"$file"
78 done
79 touch -c /etc/systemd/network/*-bmc-"$NCSI_IF".network || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070080
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070081 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
82 networkctl reload && networkctl reconfigure gbmcbr "$NCSI_IF" || true
83 fi
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070084
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070085 read -r -d '' contents <<EOF
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070086table inet filter {
87 chain ncsi_input {
88 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
89 }
90 chain ncsi_forward {
91 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
92 }
93}
94EOF
William A. Kennington III5b4b4f82021-10-15 12:22:08 -070095 rfile=/run/nftables/40-gbmc-ncsi-ra.rules
96 mkdir -p -m 755 "$(dirname "$rfile")"
97 printf '%s' "$contents" >"$rfile"
98 systemctl reset-failed nftables && systemctl --no-block restart nftables || true
99}
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700100
William A. Kennington III5b4b4f82021-10-15 12:22:08 -0700101w=60
102while true; do
103 start=$SECONDS
104 while read line; do
105 if [ -z "$line" ]; then
106 hextet=
107 pfx=
108 host=
109 domain=
110 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
111 t_pfx="${BASH_REMATCH[1]}"
112 t_pfx_len="${BASH_REMATCH[2]}"
113 ip_to_bytes t_pfx_b "$t_pfx" || continue
114 (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
115 (( t_pfx_b[9] |= 1 ))
116 hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
117 pfx="$(ip_bytes_to_str t_pfx_b)"
118 (( t_pfx_b[9] &= 0xf0 ))
119 stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
120 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then
121 host="${BASH_REMATCH[1]}"
122 domain="${BASH_REMATCH[2]}"
123 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
124 rtr="${BASH_REMATCH[1]}"
125 set_net || true
126 set_host || true
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700127 fi
William A. Kennington III5b4b4f82021-10-15 12:22:08 -0700128 done < <(rdisc6 -d -m "$NCSI_IF" -w $(( w * 1000 )) 2>/dev/null)
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700129 # If rdisc6 exits early we still want to wait the full `w` time before
130 # starting again.
131 (( timeout = start + w - SECONDS ))
132 sleep $(( timeout < 0 ? 0 : timeout ))
133done