Home | History | Annotate | Download | only in test-scripts
      1 #!/bin/sh
      2 
      3 #
      4 # Copyright (C) 2012 The Android Open Source Project
      5 #
      6 # Licensed under the Apache License, Version 2.0 (the "License");
      7 # you may not use this file except in compliance with the License.
      8 # You may obtain a copy of the License at
      9 #
     10 #      http://www.apache.org/licenses/LICENSE-2.0
     11 #
     12 # Unless required by applicable law or agreed to in writing, software
     13 # distributed under the License is distributed on an "AS IS" BASIS,
     14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 # See the License for the specific language governing permissions and
     16 # limitations under the License.
     17 #
     18 
     19 # Backchannel control script - sets up and tears down backchannel network
     20 # interfaces. Backchannel interfaces are hidden from flimflam and will never be
     21 # the default route.
     22 #
     23 # A backchannel interface can also be used to simulate a cellular
     24 # modem used by fake-cromo if the new interface name is set to
     25 # pseudo-modem0
     26 #
     27 
     28 test_if=eth_test
     29 
     30 usage () {
     31   echo "Usage: $0 <command> [args...]"
     32   echo "  setup <iface> [new_iface_name]    Set <iface> as backchannel device"
     33   echo "  teardown <iface> [new_iface_name] Return backchannel device to normal"
     34   echo "  reach <ip> <gw> [new_iface_name]  Route <ip> via gateway <gw>"
     35 }
     36 
     37 macaddr() {
     38   ip addr show "$1" | awk '/link\/ether/ { print $2 }'
     39 }
     40 
     41 ipaddr_with_subnet_mask() {
     42   ip addr show "$1" | awk '/inet / { print $2 }'
     43 }
     44 
     45 # We need to down the interface (and therefore stop flimflam) across the
     46 # invocation of nameif, according to nameif(1).
     47 renameif() {
     48   old="$1" ; shift
     49   new="$1" ; shift
     50   initctl stop shill
     51   ip link set "$old" down
     52   nameif "$new" $(macaddr "$old")
     53   ip link set "$new" up
     54   initctl start shill
     55 }
     56 
     57 setup() {
     58   oldip=$(ipaddr_with_subnet_mask "$1")
     59   if [ ! -z $2 ] ; then
     60     test_if="$2"
     61   fi
     62   renameif "$1" "$test_if"
     63   ip addr add "$oldip" dev "$test_if"
     64 }
     65 
     66 teardown() {
     67   if [ ! -z $2 ] ; then
     68     test_if="$2"
     69   fi
     70   renameif "$test_if" "$1"
     71 }
     72 
     73 reach() {
     74   ip="$1" ; shift
     75   gw="$1" ; shift
     76   if [ ! -z $1 ] ; then
     77     test_if="$1"
     78   fi
     79   ip route add "$ip" via "$gw" dev "$test_if"
     80 }
     81 
     82 if [ -z "$1" ]; then
     83   usage
     84   exit 1
     85 fi
     86 
     87 command="$1" ; shift
     88 case "$command" in
     89   setup)
     90     setup "$@"
     91     ;;
     92   teardown)
     93     teardown "$@"
     94     ;;
     95   reach)
     96     reach "$@"
     97     ;;
     98   *)
     99     usage
    100     ;;
    101 esac
    102