| #!/bin/sh |
| # Copyright 2018 The ChromiumOS Authors |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| # Backchannel control script - sets up and tears down backchannel network |
| # interfaces. Backchannel interfaces are hidden from flimflam and will never be |
| # the default route. |
| # |
| # A backchannel interface can also be used to simulate a cellular |
| # modem used by fake-cromo if the new interface name is set to |
| # pseudo-modem0 |
| # |
| |
| readonly DEFAULT_NEW_IFNAME=eth_test |
| |
| usage() { |
| cat <<EOF |
| Usage: $0 <command> [args...] |
| setup <iface> [new_iface_name] Set <iface> as backchannel device |
| teardown <iface> [new_iface_name] Return backchannel device to normal |
| reach <ip> <gw> [new_iface_name] Route <ip> via gateway <gw> |
| |
| EOF |
| } |
| |
| ipaddr_with_subnet_mask() { |
| ip addr show "$1" | awk '/inet / { print $2 }' |
| } |
| |
| # We need to down the interface (and therefore stop shill ) across the |
| # invocation of `ip link set <old_ifname> name <new_ifname>`. |
| renameif() { |
| local old_ifname="$1" |
| local new_ifname="$2" |
| |
| initctl stop shill |
| ip link set "${old_ifname}" down |
| ip link set "${old_ifname}" name "${new_ifname}" |
| ip link set "${new_ifname}" up |
| initctl start shill |
| } |
| |
| setup() { |
| local old_ifname="$1" |
| local new_ifname="${2:-${DEFAULT_NEW_IFNAME}}" |
| |
| local old_ip |
| old_ip="$(ipaddr_with_subnet_mask "${old_ifname}")" |
| renameif "${old_ifname}" "${new_ifname}" |
| ip addr add "${old_ip}" dev "${new_ifname}" |
| } |
| |
| teardown() { |
| local old_ifname="$1" |
| local new_ifname="${2:-${DEFAULT_NEW_IFNAME}}" |
| |
| renameif "${new_ifname}" "${old_ifname}" |
| } |
| |
| reach() { |
| local ip="$1" |
| local gw="$2" |
| local new_ifname="${3:-${DEFAULT_NEW_IFNAME}}" |
| ip route add "${ip}" via "${gw}" dev "${new_ifname}" |
| } |
| |
| backchannel() { |
| local command="$1" |
| |
| case "${command}" in |
| setup|teardown|reach) |
| "$@" |
| ;; |
| *) |
| usage |
| exit 1 |
| ;; |
| esac |
| } |
| |
| backchannel "$@" |