1#!/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 18NCSI_IF="$1" 19 20old_rtr= 21old_mac= 22 23set_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 29 30 echo "Setting default router: $rtr at $mac" >&2 31 UpdateGateway xyz.openbmc_project.Network "$rtr" || return 32 UpdateNeighbor xyz.openbmc_project.Network "$NCSI_IF" "$rtr" "$mac" || return 33 34 retries=-1 35 old_mac="$mac" 36 old_rtr="$rtr" 37} 38 39retries=1 40w=60 41while true; do 42 start=$SECONDS 43 args=(-m "$NCSI_IF" -w $(( w * 1000 ))) 44 if (( retries > 0 )); then 45 args+=(-r "$retries") 46 else 47 args+=(-d) 48 fi 49 while read line; do 50 if [ -z "$line" ]; then 51 lifetime= 52 mac= 53 elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then 54 lifetime="${BASH_REMATCH[1]}" 55 elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then 56 mac="${BASH_REMATCH[1]}" 57 elif [[ "$line" =~ ^from' '(.*)$ ]]; then 58 rtr="${BASH_REMATCH[1]}" 59 set_rtr || true 60 lifetime= 61 mac= 62 rtr= 63 fi 64 done < <(exec rdisc6 "${args[@]}" 2>/dev/null) 65 # If rdisc6 exits early we still want to wait the full `w` time before 66 # starting again. 67 (( timeout = start + w - SECONDS )) 68 sleep $(( timeout < 0 ? 0 : timeout )) 69done 70