blob: 9403b28debc68b8833f0f1feb5cc5f37deefdac8 [file] [log] [blame]
William A. Kennington III379b0612021-11-04 02:42:30 -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 "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
17
William A. Kennington III246bcaa2022-02-11 02:53:28 -080018NCSI_IF="$1"
William A. Kennington III379b0612021-11-04 02:42:30 -070019
William A. Kennington III246bcaa2022-02-11 02:53:28 -080020old_rtr=
21old_mac=
22
Patrick Williams59486672022-12-08 06:23:47 -060023function set_rtr() {
24 [ -n "$rtr" -a -n "$lifetime" ] || return
25 [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return
26 # Only valid default routers can be considered, 0 lifetime implies
27 # a non-default router
28 (( lifetime > 0 )) || return
William A. Kennington III246bcaa2022-02-11 02:53:28 -080029
Patrick Williams59486672022-12-08 06:23:47 -060030 echo "Setting default router: $rtr at $mac" >&2
William A. Kennington III5034c562024-03-19 14:00:52 -070031
32 # Delete and static gateways and neighbors
33 while read entry; do
34 eval "$(echo "$entry" | JSONToVars)" || return
35 echo "Deleting neighbor $object"
36 DeleteObject "$service" "$object" || true
37 done < <(GetNeighborObjects "$netdev" 2>/dev/null)
38
39 busctl set-property xyz.openbmc_project.Network "$(EthObjRoot "$NCSI_IF")" \
40 xyz.openbmc_project.Network.EthernetInterface DefaultGateway6 s "" || true
41
42 # In case we don't have a base network file, make one
43 net_file=/run/systemd/network/00-bmc-$NCSI_IF.network
44 printf '[Match]\nName=%s\n[Network]\nDHCP=false\nIPv6AcceptRA=false\nLinkLocalAddressing=yes' \
45 "$NCSI_IF" >$net_file
46
47 # Override any existing gateway info
48 mkdir -p $net_file.d
49 printf '[Network]\nGateway=%s\n[Neighbor]\nMACAddress=%s\nAddress=%s' \
50 "$rtr" "$mac" "$rtr" >$net_file.d/10-gateway.conf
51
52 networkctl reload && networkctl reconfigure "$NCSI_IF" || true
William A. Kennington III246bcaa2022-02-11 02:53:28 -080053
Patrick Williams59486672022-12-08 06:23:47 -060054 retries=-1
55 old_mac="$mac"
56 old_rtr="$rtr"
William A. Kennington III246bcaa2022-02-11 02:53:28 -080057}
58
59retries=1
60w=60
61while true; do
Patrick Williams59486672022-12-08 06:23:47 -060062 start=$SECONDS
63 args=(-m "$NCSI_IF" -w $(( w * 1000 )))
64 if (( retries > 0 )); then
65 args+=(-r "$retries")
66 else
67 args+=(-d)
William A. Kennington III379b0612021-11-04 02:42:30 -070068 fi
Patrick Williams59486672022-12-08 06:23:47 -060069 while read line; do
William A. Kennington IIId36d9ef2024-03-19 14:02:42 -070070 # `script` terminates all lines with a CRLF, remove it
71 line="${line:0:-1}"
Patrick Williams59486672022-12-08 06:23:47 -060072 if [ -z "$line" ]; then
73 lifetime=
74 mac=
75 elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then
76 lifetime="${BASH_REMATCH[1]}"
77 elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then
78 mac="${BASH_REMATCH[1]}"
79 elif [[ "$line" =~ ^from' '(.*)$ ]]; then
80 rtr="${BASH_REMATCH[1]}"
81 set_rtr || true
82 lifetime=
83 mac=
84 rtr=
85 fi
William A. Kennington IIId36d9ef2024-03-19 14:02:42 -070086 done < <(exec script -q -c "rdisc6 ${args[*]}" /dev/null 2>/dev/null)
Patrick Williams59486672022-12-08 06:23:47 -060087 # If rdisc6 exits early we still want to wait the full `w` time before
88 # starting again.
89 (( timeout = start + w - SECONDS ))
90 sleep $(( timeout < 0 ? 0 : timeout ))
William A. Kennington III246bcaa2022-02-11 02:53:28 -080091done