#!/bin/bash
|
#!/bin/bash
|
|
|
# Copyright (C) 2010 ORSoC AB
|
# Copyright (C) 2010 ORSoC AB
|
# Copyright (C) 2010 Embecosm Limited
|
# Copyright (C) 2010 Embecosm Limited
|
|
|
# Contributor Julius Baxter <julius.baxter@orsoc.se>
|
# Contributor Julius Baxter <julius.baxter@orsoc.se>
|
# Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
# Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
|
|
# This file is a superuser script to set up an Ethernet bridge that can be
|
# This file is a superuser script to set up an Ethernet bridge that can be
|
# used with Or1ksim via the TUN/TAP interface.
|
# used with Or1ksim via the TUN/TAP interface.
|
|
|
# This program is free software; you can redistribute it and/or modify it
|
# This program is free software; you can redistribute it and/or modify it
|
# under the terms of the GNU General Public License as published by the Free
|
# under the terms of the GNU General Public License as published by the Free
|
# Software Foundation; either version 3 of the License, or (at your option)
|
# Software Foundation; either version 3 of the License, or (at your option)
|
# any later version.
|
# any later version.
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
# more details.
|
# more details.
|
|
|
# You should have received a copy of the GNU General Public License along
|
# You should have received a copy of the GNU General Public License along
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
|
|
# Pre-requisites: bridge-utils must be installed.
|
# Pre-requisites: bridge-utils must be installed.
|
|
|
# Usage: ./brstart-static.sh <username> <groupname> <bridge> <eth> <tap>
|
# Usage: ./brstart-static.sh <username> <groupname> <bridge> <eth> <tap>
|
|
|
# - <bridge> is the bridge interface to use, e.g. br0
|
# - <bridge> is the bridge interface to use, e.g. br0
|
# - <eth> is the hardware ethernet interface to use, e.g. eth0
|
# - <eth> is the hardware ethernet interface to use, e.g. eth0
|
# - <tap> is/are the persistent TAP interface(s)
|
# - <tap> is/are the persistent TAP interface(s)
|
|
|
# Check we have the right number of arguments
|
# Check we have the right number of arguments
|
if [ "x$#" != "x5" ]
|
if [ "x$#" != "x5" ]
|
then
|
then
|
echo "Usage: ./brstart-static.sh <username> <groupname> <bridge> <eth> <tap>"
|
echo "Usage: ./brstart-static.sh <username> <groupname> <bridge> <eth> <tap>"
|
exit 1
|
exit 1
|
fi
|
fi
|
|
|
# Check we are root
|
# Check we are root
|
euid=`id -un`
|
euid=`id -un`
|
if [ "x${euid}" != "xroot" ]
|
if [ "x${euid}" != "xroot" ]
|
then
|
then
|
echo "Must run as root"
|
echo "Must run as root"
|
exit 1
|
exit 1
|
fi
|
fi
|
|
|
# Break out the arguments
|
# Break out the arguments
|
username=$1
|
username=$1
|
groupname=$2
|
groupname=$2
|
br=$3
|
br=$3
|
eth=$4
|
eth=$4
|
tap=$5
|
tap=$5
|
|
|
# Determine the IP address, netmask and broadcast of the current Ethernet
|
# Determine the IP address, netmask and broadcast of the current Ethernet
|
# interface. This is used if the bridge is set up manually, rather than using
|
# interface. This is used if the bridge is set up manually, rather than using
|
# DHCP.
|
# DHCP.
|
eth_ip=`ifconfig $eth | \
|
eth_ip=`ifconfig $eth | \
|
grep "inet addr" | \
|
grep "inet addr" | \
|
head -1 | \
|
head -1 | \
|
sed -e 's/^.*inet addr:\([^ \t]*\).*$/\1/'`
|
sed -e 's/^.*inet addr:\([^ \t]*\).*$/\1/'`
|
eth_netmask=`ifconfig $eth | \
|
eth_netmask=`ifconfig $eth | \
|
grep "Mask" | \
|
grep "Mask" | \
|
head -1 | \
|
head -1 | \
|
sed -e 's/^.*Mask:\([^ \t]*\).*$/\1/'`
|
sed -e 's/^.*Mask:\([^ \t]*\).*$/\1/'`
|
eth_broadcast=`ifconfig $eth | \
|
eth_broadcast=`ifconfig $eth | \
|
grep "Bcast" | \
|
grep "Bcast" | \
|
head -1 | \
|
head -1 | \
|
sed -e 's/^.*Bcast:\([^ \t]*\).*$/\1/'`
|
sed -e 's/^.*Bcast:\([^ \t]*\).*$/\1/'`
|
|
|
# Create the TAP interface
|
# Create the TAP interface
|
openvpn --mktun --dev ${tap} --user ${username} --group ${groupname}
|
openvpn --mktun --dev ${tap} --user ${username} --group ${groupname}
|
|
|
if [ $? != 0 ]
|
if [ $? != 0 ]
|
then
|
then
|
echo "Failed to create ${tap}"
|
echo "Failed to create ${tap}"
|
exit 1
|
exit 1
|
fi
|
fi
|
|
|
# Create the bridge
|
# Create the bridge
|
brctl addbr ${br}
|
brctl addbr ${br}
|
|
|
if [ $? != 0 ]
|
if [ $? != 0 ]
|
then
|
then
|
echo "Failed to create ${br}"
|
echo "Failed to create ${br}"
|
exit 1
|
exit 1
|
fi
|
fi
|
|
|
# Add the host Ethernet and TAP interfaces, removing the IP addresses of the
|
# Add the host Ethernet and TAP interfaces, removing the IP addresses of the
|
# underlying interfaces.
|
# underlying interfaces.
|
for i in ${eth} ${tap}
|
for i in ${eth} ${tap}
|
do
|
do
|
# Add the interface
|
# Add the interface
|
brctl addif ${br} ${i}
|
brctl addif ${br} ${i}
|
|
|
if [ $? != 0 ]
|
if [ $? != 0 ]
|
then
|
then
|
echo "Failed to create ${i}"
|
echo "Failed to create ${i}"
|
exit 1
|
exit 1
|
fi
|
fi
|
|
|
# Remove the IP address
|
# Remove the IP address
|
ifconfig ${i} 0.0.0.0 promisc up
|
ifconfig ${i} 0.0.0.0 promisc up
|
|
|
if [ $? != 0 ]
|
if [ $? != 0 ]
|
then
|
then
|
echo "Failed to remove IP interface of ${i}"
|
echo "Failed to remove IP interface of ${i}"
|
exit 1
|
exit 1
|
fi
|
fi
|
done
|
done
|
|
|
# Reconfigure the bridge to have the original IP address, netmask and
|
# Reconfigure the bridge to have the original IP address, netmask and
|
# broadcast mask
|
# broadcast mask
|
ifconfig ${br} ${eth_ip} netmask ${eth_netmask} broadcast ${eth_broadcast}
|
ifconfig ${br} ${eth_ip} netmask ${eth_netmask} broadcast ${eth_broadcast}
|
|
|
# Open up firewall to the tap and bridge. We have a generic reject at the end
|
# Open up firewall to the tap and bridge. We have a generic reject at the end
|
# of the chain, so we insert these at the start.
|
# of the chain, so we insert these at the start.
|
iptables -I INPUT 1 -i ${tap} -j ACCEPT
|
iptables -I INPUT 1 -i ${tap} -j ACCEPT
|
iptables -I INPUT 1 -i ${br} -j ACCEPT
|
iptables -I INPUT 1 -i ${br} -j ACCEPT
|
iptables -I FORWARD 1 -i ${br} -j ACCEPT
|
iptables -I FORWARD 1 -i ${br} -j ACCEPT
|
|
|