1379b061fSWilliam A. Kennington III#!/bin/bash 2379b061fSWilliam A. Kennington III# Copyright 2021 Google LLC 3379b061fSWilliam A. Kennington III# 4379b061fSWilliam A. Kennington III# Licensed under the Apache License, Version 2.0 (the "License"); 5379b061fSWilliam A. Kennington III# you may not use this file except in compliance with the License. 6379b061fSWilliam A. Kennington III# You may obtain a copy of the License at 7379b061fSWilliam A. Kennington III# 8379b061fSWilliam A. Kennington III# http://www.apache.org/licenses/LICENSE-2.0 9379b061fSWilliam A. Kennington III# 10379b061fSWilliam A. Kennington III# Unless required by applicable law or agreed to in writing, software 11379b061fSWilliam A. Kennington III# distributed under the License is distributed on an "AS IS" BASIS, 12379b061fSWilliam A. Kennington III# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13379b061fSWilliam A. Kennington III# See the License for the specific language governing permissions and 14379b061fSWilliam A. Kennington III# limitations under the License. 15379b061fSWilliam A. Kennington III 16379b061fSWilliam A. Kennington IIIsource "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh 17379b061fSWilliam A. Kennington III 18246bcaa4SWilliam A. Kennington IIINCSI_IF="$1" 19379b061fSWilliam A. Kennington III 20246bcaa4SWilliam A. Kennington IIIold_rtr= 21246bcaa4SWilliam A. Kennington IIIold_mac= 22246bcaa4SWilliam A. Kennington III 2359486679SPatrick Williamsfunction set_rtr() { 24246bcaa4SWilliam A. Kennington III [ -n "$rtr" -a -n "$lifetime" ] || return 25246bcaa4SWilliam A. Kennington III [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return 26246bcaa4SWilliam A. Kennington III # Only valid default routers can be considered, 0 lifetime implies 27246bcaa4SWilliam A. Kennington III # a non-default router 28246bcaa4SWilliam A. Kennington III (( lifetime > 0 )) || return 29246bcaa4SWilliam A. Kennington III 30246bcaa4SWilliam A. Kennington III echo "Setting default router: $rtr at $mac" >&2 315034c566SWilliam A. Kennington III 325034c566SWilliam A. Kennington III # Delete and static gateways and neighbors 335034c566SWilliam A. Kennington III while read entry; do 345034c566SWilliam A. Kennington III eval "$(echo "$entry" | JSONToVars)" || return 355034c566SWilliam A. Kennington III echo "Deleting neighbor $object" 365034c566SWilliam A. Kennington III DeleteObject "$service" "$object" || true 375034c566SWilliam A. Kennington III done < <(GetNeighborObjects "$netdev" 2>/dev/null) 385034c566SWilliam A. Kennington III 395034c566SWilliam A. Kennington III busctl set-property xyz.openbmc_project.Network "$(EthObjRoot "$NCSI_IF")" \ 405034c566SWilliam A. Kennington III xyz.openbmc_project.Network.EthernetInterface DefaultGateway6 s "" || true 415034c566SWilliam A. Kennington III 425034c566SWilliam A. Kennington III # In case we don't have a base network file, make one 435034c566SWilliam A. Kennington III net_file=/run/systemd/network/00-bmc-$NCSI_IF.network 445034c566SWilliam A. Kennington III printf '[Match]\nName=%s\n[Network]\nDHCP=false\nIPv6AcceptRA=false\nLinkLocalAddressing=yes' \ 455034c566SWilliam A. Kennington III "$NCSI_IF" >$net_file 465034c566SWilliam A. Kennington III 475034c566SWilliam A. Kennington III # Override any existing gateway info 485034c566SWilliam A. Kennington III mkdir -p $net_file.d 495034c566SWilliam A. Kennington III printf '[Network]\nGateway=%s\n[Neighbor]\nMACAddress=%s\nAddress=%s' \ 505034c566SWilliam A. Kennington III "$rtr" "$mac" "$rtr" >$net_file.d/10-gateway.conf 515034c566SWilliam A. Kennington III 525034c566SWilliam A. Kennington III networkctl reload && networkctl reconfigure "$NCSI_IF" || true 53246bcaa4SWilliam A. Kennington III 54246bcaa4SWilliam A. Kennington III retries=-1 55246bcaa4SWilliam A. Kennington III old_mac="$mac" 56246bcaa4SWilliam A. Kennington III old_rtr="$rtr" 57246bcaa4SWilliam A. Kennington III} 58246bcaa4SWilliam A. Kennington III 59246bcaa4SWilliam A. Kennington IIIretries=1 60246bcaa4SWilliam A. Kennington IIIw=60 61246bcaa4SWilliam A. Kennington IIIwhile true; do 62246bcaa4SWilliam A. Kennington III start=$SECONDS 63246bcaa4SWilliam A. Kennington III args=(-m "$NCSI_IF" -w $(( w * 1000 ))) 64246bcaa4SWilliam A. Kennington III if (( retries > 0 )); then 65246bcaa4SWilliam A. Kennington III args+=(-r "$retries") 66246bcaa4SWilliam A. Kennington III else 67246bcaa4SWilliam A. Kennington III args+=(-d) 68379b061fSWilliam A. Kennington III fi 69246bcaa4SWilliam A. Kennington III while read line; do 70*d36d9ef6SWilliam A. Kennington III # `script` terminates all lines with a CRLF, remove it 71*d36d9ef6SWilliam A. Kennington III line="${line:0:-1}" 72246bcaa4SWilliam A. Kennington III if [ -z "$line" ]; then 73246bcaa4SWilliam A. Kennington III lifetime= 74246bcaa4SWilliam A. Kennington III mac= 75246bcaa4SWilliam A. Kennington III elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then 76246bcaa4SWilliam A. Kennington III lifetime="${BASH_REMATCH[1]}" 77246bcaa4SWilliam A. Kennington III elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then 78246bcaa4SWilliam A. Kennington III mac="${BASH_REMATCH[1]}" 79246bcaa4SWilliam A. Kennington III elif [[ "$line" =~ ^from' '(.*)$ ]]; then 80246bcaa4SWilliam A. Kennington III rtr="${BASH_REMATCH[1]}" 81246bcaa4SWilliam A. Kennington III set_rtr || true 82246bcaa4SWilliam A. Kennington III lifetime= 83246bcaa4SWilliam A. Kennington III mac= 84246bcaa4SWilliam A. Kennington III rtr= 85246bcaa4SWilliam A. Kennington III fi 86*d36d9ef6SWilliam A. Kennington III done < <(exec script -q -c "rdisc6 ${args[*]}" /dev/null 2>/dev/null) 87246bcaa4SWilliam A. Kennington III # If rdisc6 exits early we still want to wait the full `w` time before 88246bcaa4SWilliam A. Kennington III # starting again. 89246bcaa4SWilliam A. Kennington III (( timeout = start + w - SECONDS )) 90246bcaa4SWilliam A. Kennington III sleep $(( timeout < 0 ? 0 : timeout )) 91379b061fSWilliam A. Kennington IIIdone 92