blob: 7ba159fcfbc1aa5c11c56557c66f92a8be91b859 [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
19old_pfx=
20old_rtr=
21
22w=60
23while true; do
24 start=$SECONDS
25 while read line; do
26 if [ -z "$line" ]; then
27 pfx=
William A. Kennington IIIbb9fb952021-09-27 18:10:27 -070028 host=
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070029 elif [[ "$line" =~ ^Prefix' '*:' '*(.*)/([0-9]+)$ ]]; then
30 t_pfx="${BASH_REMATCH[1]}"
31 t_pfx_len="${BASH_REMATCH[2]}"
32 ip_to_bytes t_pfx_b "$t_pfx" || continue
33 (( t_pfx_len == 76 && t_pfx_b[8] & 0xfd == 0xfd )) || continue
34 (( t_pfx_b[9] |= 1 ))
William A. Kennington IIIbb9fb952021-09-27 18:10:27 -070035 hextet="fd$(printf '%02x' ${t_pfx_b[9]})"
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070036 pfx="$(ip_bytes_to_str t_pfx_b)"
37 (( t_pfx_b[9] &= 0xf0 ))
38 stateless_pfx="$(ip_bytes_to_str t_pfx_b)"
William A. Kennington IIIbb9fb952021-09-27 18:10:27 -070039 elif [[ "$line" =~ ^'DNS search list'' '*:' '*([^.-]*)[^.]*[.](.*.google.com)$ ]]; then
40 host="${BASH_REMATCH[1]}"
41 domain="${BASH_REMATCH[2]}"
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -070042 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
43 rtr="${BASH_REMATCH[1]}"
44 (( "${#pfx}" != 0 )) || continue
45 [[ "$pfx" != "$old_pfx" || "$old_rtr" != "$rtr" ]] || continue
46
47 echo "Found prefix $pfx from $rtr" >&2
48
49 # Delete any stale IP Addresses from the primary interface as we won't use them
50 UpdateIP xyz.openbmc_project.Network @NCSI_IF@ '0.0.0.0' '0'
51 UpdateIP xyz.openbmc_project.Network @NCSI_IF@ '::' '0'
52
53 read -r -d '' contents <<EOF
54[Network]
55Address=$pfx/128
56IPv6PrefixDelegation=yes
57[IPv6PrefixDelegation]
58RouterLifetimeSec=60
59[IPv6Prefix]
60Prefix=$stateless_pfx/80
61PreferredLifetimeSec=60
62ValidLifetimeSec=60
63[IPv6RoutePrefix]
64Route=$pfx/80
65LifetimeSec=60
66[Route]
67Destination=$stateless_pfx/76
68Type=unreachable
69Metric=1024
70EOF
71 for file in /run/systemd/network/{00,}-bmc-gbmcbr.network.d/49-public-ra.conf; do
72 mkdir -p -m 755 "$(dirname "$file")"
73 printf '%s' "$contents" >"$file"
74 done
75
76 contents='[Network]'$'\n'
77 contents+="Address=$pfx/128"$'\n'
78 contents+="Gateway=$rtr"$'\n'
79 for file in /run/systemd/network/{00,}-bmc-@NCSI_IF@.network.d/49-public-ra.conf; do
80 mkdir -p -m 755 "$(dirname "$file")"
81 printf '%s' "$contents" >"$file"
82 done
83
84 # Ensure that systemd-networkd performs a reconfiguration as it doesn't
85 # currently check the mtime of drop-in files.
86 touch -c /lib/systemd/network/*-bmc-gbmcbr.network
87 touch -c /etc/systemd/network/*-bmc-@NCSI_IF@.network
88
89 if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
90 networkctl reload
91 networkctl reconfigure gbmcbr
92 fi
93
94 read -r -d '' contents <<EOF
95table inet filter {
96 chain ncsi_input {
97 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 goto ncsi_gbmc_br_pub_input
98 }
99 chain ncsi_forward {
100 ip6 saddr != $pfx/76 ip6 daddr $pfx/76 accept
101 }
102}
103EOF
104 rfile=/run/nftables/40-gbmc-ncsi-ra.rules
105 mkdir -p -m 755 "$(dirname "$rfile")"
106 printf '%s' "$contents" >"$rfile"
107 systemctl reset-failed nftables
108 systemctl --no-block restart nftables
109
William A. Kennington IIIbb9fb952021-09-27 18:10:27 -0700110 # Set the machine hostname if discovered
111 if [ -n "$host" ]; then
112 hostnamectl set-hostname "$host-n$hextet.$domain"
113 fi
114
William A. Kennington IIIc7454fb2021-09-14 16:01:37 -0700115 old_pfx="$pfx"
116 old_rtr="$rtr"
117 fi
118 done < <(rdisc6 -d -m @NCSI_IF@ -w $(( w * 1000 )) 2>/dev/null)
119 # If rdisc6 exits early we still want to wait the full `w` time before
120 # starting again.
121 (( timeout = start + w - SECONDS ))
122 sleep $(( timeout < 0 ? 0 : timeout ))
123done