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 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 23 | function set_rtr() { |
| 24 | [ -n "$rtr" -a -n "$lifetime" ] || return |
Yuxiao Zhang | b47ca0d | 2024-05-30 16:33:51 -0700 | [diff] [blame^] | 25 | |
| 26 | # Reconfigure gateway in case of anything goes wrong |
| 27 | if ! ip -6 route show | grep -q '^default'; then |
| 28 | echo 'default route missing, reconfiguring...' >&2 |
| 29 | old_rtr= |
| 30 | old_mac= |
| 31 | fi |
| 32 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 33 | [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return |
| 34 | # Only valid default routers can be considered, 0 lifetime implies |
| 35 | # a non-default router |
| 36 | (( lifetime > 0 )) || return |
William A. Kennington III | 246bcaa | 2022-02-11 02:53:28 -0800 | [diff] [blame] | 37 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 38 | echo "Setting default router: $rtr at $mac" >&2 |
William A. Kennington III | 5034c56 | 2024-03-19 14:00:52 -0700 | [diff] [blame] | 39 | |
| 40 | # Delete and static gateways and neighbors |
| 41 | while read entry; do |
| 42 | eval "$(echo "$entry" | JSONToVars)" || return |
| 43 | echo "Deleting neighbor $object" |
| 44 | DeleteObject "$service" "$object" || true |
| 45 | done < <(GetNeighborObjects "$netdev" 2>/dev/null) |
| 46 | |
| 47 | busctl set-property xyz.openbmc_project.Network "$(EthObjRoot "$NCSI_IF")" \ |
| 48 | xyz.openbmc_project.Network.EthernetInterface DefaultGateway6 s "" || true |
| 49 | |
| 50 | # In case we don't have a base network file, make one |
| 51 | net_file=/run/systemd/network/00-bmc-$NCSI_IF.network |
| 52 | printf '[Match]\nName=%s\n[Network]\nDHCP=false\nIPv6AcceptRA=false\nLinkLocalAddressing=yes' \ |
| 53 | "$NCSI_IF" >$net_file |
| 54 | |
| 55 | # Override any existing gateway info |
| 56 | mkdir -p $net_file.d |
| 57 | printf '[Network]\nGateway=%s\n[Neighbor]\nMACAddress=%s\nAddress=%s' \ |
| 58 | "$rtr" "$mac" "$rtr" >$net_file.d/10-gateway.conf |
| 59 | |
| 60 | networkctl reload && networkctl reconfigure "$NCSI_IF" || true |
William A. Kennington III | 246bcaa | 2022-02-11 02:53:28 -0800 | [diff] [blame] | 61 | |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 62 | retries=-1 |
| 63 | old_mac="$mac" |
| 64 | old_rtr="$rtr" |
William A. Kennington III | 246bcaa | 2022-02-11 02:53:28 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | retries=1 |
| 68 | w=60 |
| 69 | while true; do |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 70 | start=$SECONDS |
| 71 | args=(-m "$NCSI_IF" -w $(( w * 1000 ))) |
| 72 | if (( retries > 0 )); then |
| 73 | args+=(-r "$retries") |
| 74 | else |
| 75 | args+=(-d) |
William A. Kennington III | 379b061 | 2021-11-04 02:42:30 -0700 | [diff] [blame] | 76 | fi |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 77 | while read line; do |
William A. Kennington III | d36d9ef | 2024-03-19 14:02:42 -0700 | [diff] [blame] | 78 | # `script` terminates all lines with a CRLF, remove it |
| 79 | line="${line:0:-1}" |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 80 | if [ -z "$line" ]; then |
| 81 | lifetime= |
| 82 | mac= |
| 83 | elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then |
| 84 | lifetime="${BASH_REMATCH[1]}" |
| 85 | elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then |
| 86 | mac="${BASH_REMATCH[1]}" |
| 87 | elif [[ "$line" =~ ^from' '(.*)$ ]]; then |
| 88 | rtr="${BASH_REMATCH[1]}" |
| 89 | set_rtr || true |
| 90 | lifetime= |
| 91 | mac= |
| 92 | rtr= |
| 93 | fi |
William A. Kennington III | d36d9ef | 2024-03-19 14:02:42 -0700 | [diff] [blame] | 94 | done < <(exec script -q -c "rdisc6 ${args[*]}" /dev/null 2>/dev/null) |
Patrick Williams | 5948667 | 2022-12-08 06:23:47 -0600 | [diff] [blame] | 95 | # If rdisc6 exits early we still want to wait the full `w` time before |
| 96 | # starting again. |
| 97 | (( timeout = start + w - SECONDS )) |
| 98 | sleep $(( timeout < 0 ? 0 : timeout )) |
William A. Kennington III | 246bcaa | 2022-02-11 02:53:28 -0800 | [diff] [blame] | 99 | done |