| #!/bin/sh |
| |
| # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| # 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 |
| # |
| |
| test_if=eth_test |
| |
| usage () { |
| echo "Usage: $0 <command> [args...]" |
| echo " setup <iface> [new_iface_name] Sets <iface> as the backchannel device" |
| echo " teardown <iface> [new_iface_name] Returns the backchannel device to normal" |
| echo " reach <ip> <gw> [new_iface_name] Inserts routes to <ip> via gateway <gw>" |
| } |
| |
| macaddr () { |
| ifconfig "$1" | awk '/HWaddr/ { print $5 }' |
| } |
| |
| ipaddr () { |
| ifconfig "$1" | awk '/inet addr:/ { print $2 }' \ |
| | awk -F ':' '{ print $2 }' |
| } |
| |
| netmask () { |
| ifconfig "$1" | awk '/Mask:/ { print $4 }' \ |
| | awk -F ':' '{ print $2 }' |
| } |
| |
| # We need to down the interface (and therefore stop flimflam) across the |
| # invocation of nameif, according to nameif(1). |
| renameif () { |
| old="$1" ; shift |
| new="$1" ; shift |
| initctl stop shill |
| ifconfig "$old" down |
| nameif "$new" $(macaddr "$old") |
| ifconfig "$new" up |
| initctl start shill |
| } |
| |
| setup () { |
| oldip=$(ipaddr "$1") |
| oldnetmask=$(netmask "$1") |
| if [ ! -z $2 ] ; then |
| test_if="$2" |
| fi |
| renameif "$1" "$test_if" |
| ifconfig "$test_if" "$oldip" netmask "$oldnetmask" |
| } |
| |
| teardown () { |
| if [ ! -z $2 ] ; then |
| test_if="$2" |
| fi |
| renameif "$test_if" "$1" |
| } |
| |
| reach () { |
| ip="$1" ; shift |
| gw="$1" ; shift |
| if [ ! -z $1 ] ; then |
| test_if="$1" |
| fi |
| route add -host "$ip" gw "$gw" "$test_if" |
| } |
| |
| if [ -z "$1" ]; then |
| usage |
| exit 1 |
| fi |
| |
| command="$1" ; shift |
| case "$command" in |
| setup) |
| setup "$@" |
| ;; |
| teardown) |
| teardown "$@" |
| ;; |
| reach) |
| reach "$@" |
| ;; |
| *) |
| usage |
| ;; |
| esac |