1#!/bin/bash
2#
3# Copyright (c) 2018 Intel Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#    http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17usage() {
18    cat <<EOF
19$0 [--config|-c <file>] [--iface|-i <interface>] [start|up] [stop|down]
20               [any optional parameters for ip command]
21
22If no parameters are given, then "zeth" network interface and "zeth.conf"
23configuration file are used. The script waits until user presses CTRL-c
24and then removes the network interface.
25
26Examples:
27
28$ net-setup.sh
29$ net-setup.sh --config zeth-vlan.conf
30$ net-setup.sh --config my-own-config.conf --iface foobar
31
32It is also possible to let the script return and then stop the network
33interface later. Is can be done by first creating the interface with
34"start" or "up" command, and then later remove the interface with
35"stop" or "down" command.
36
37$ net-setup.sh start
38do your things here
39$ net-setup.sh stop
40
41$ net-setup.sh --config my-own-config.conf up
42do your things here
43$ net-setup.sh --config my-own-config.conf down
44
45Any extra parameters that the script does not know, are passed directly
46to "ip" command.
47
48$ net-setup.sh --config my-own-config.conf --iface foo user bar
49
50EOF
51    exit
52}
53
54if [ "$1" = "-h" -o "$1" = "--help" ]; then
55    usage
56fi
57
58if [ `id -u` != 0 ]; then
59    echo "Run this script as a root user!"
60    sudo $0 $@
61    exit
62fi
63
64IFACE=zeth
65
66# Default config file setups default connectivity IP addresses
67CONF_FILE=./zeth.conf
68
69while [ $# -gt 0 ]
70do
71    case $1 in
72	--config|-c)
73	    CONF_FILE="$2"
74	    shift 2
75	    ;;
76	--iface|-i)
77	    IFACE="$2"
78	    shift 2
79	    ;;
80	--help|-h)
81	    usage
82	    ;;
83	up|start)
84	    ACTION=start
85	    shift
86	    ;;
87	down|stop)
88	    ACTION=stop
89	    shift
90	    ;;
91	*)
92	    break
93	    ;;
94    esac
95done
96
97if [ ! -f "$CONF_FILE" ]; then
98    if [ ! -f "${0%/*}/$CONF_FILE" ];then
99	echo "No such file '$CONF_FILE'"
100	exit
101    fi
102
103    CONF_FILE="${0%/*}/$CONF_FILE"
104fi
105
106echo "Using $CONF_FILE configuration file."
107
108STOPPED=0
109trap ctrl_c INT TERM
110
111ctrl_c() {
112    STOPPED=1
113}
114
115if [ "$ACTION" != stop ]; then
116    echo "Creating $IFACE"
117    ip tuntap add $IFACE mode tap $@
118
119    # The idea is that the configuration file will setup
120    # the IP addresses etc. for the created interface.
121    . "$CONF_FILE" $IFACE
122fi
123
124if [ "$ACTION" = "" ]; then
125    while [ $STOPPED -eq 0 ]; do
126	sleep 1d
127    done
128fi
129
130if [ "$ACTION" != start ]; then
131    ip link set $IFACE down
132
133    if [ -f "$CONF_FILE".stop ]; then
134	. "$CONF_FILE".stop $IFACE
135    fi
136
137    echo "Removing $IFACE"
138    ip tuntap del $IFACE mode tap
139fi
140