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