#!/bin/sh
# Filename:      grml-bridge
# Purpose:       set up your box as bridge
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

CONFIG_FILE=/etc/grml/routersetup
. /etc/grml/lsb-functions
. /etc/grml/script-functions

check4root || exit 1

if ! [ -r "$CONFIG_FILE" ] ; then
  eerror "$CONFIG_FILE could not be read."
  exit 1
fi

. "$CONFIG_FILE"

if [ -z "$BRIDGE_DEVICES" ] ; then
   eerror "Bridge devices (\$BRIDGE_DEVICES) not set in $CONFIG_FILE"
   exit 1
fi

[ -n "$BRCTL" ] || BRCTL=brctl
[ -n "$STP"   ] || STP=on
[ -n "$BRIDGE_NAME" ] || BRIDGE_NAME=br0

check4progs $BRCTL || exit 1

case "$1" in
    start)
        einfo "Starting bridge"
        eindent
            einfo "Creating bridge device"
            brctl addbr "$BRIDGE_NAME"
            eend $?
            case "$BRIDGE_STP" in
                no|false)
                    ;;
                *)
                    einfo "Setting Spanning-Tree Protocol (STP) to status $STP"
                    brctl stp "$BRIDGE_NAME" $STP
                    eend $?
                    ;;
            esac

            einfo "Bringing network device up: "
            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i"
                   ifconfig "$i" 0.0.0.0 up ; eend $?
               done
            eoutdent

            einfo "Enabling promiscuous mode on: "
            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i"
                   ip link set "$i" promisc on ; eend $?
               done
            eoutdent

            einfo "Adding network devices to $BRIDGE_NAME: "
            eindent
            for i in $BRIDGE_DEVICES ; do
                einfo "$i"
                brctl addif "$BRIDGE_NAME" $i  ; eend $?
            done
            eoutdent

            einfo "Bringing bridge $BRIDGE_NAME up"
            ip link set "$BRIDGE_NAME" up ; eend $?
            eindent
               case $BRIDGE_CONFIG in
                       DHCP)
                         einfo "starting dhclient for $BRIDGE_NAME"
                         dhclient -pf /var/run/dhclient.$BRIDGE_NAME.pid -lf /var/run/dhclient.$BRIDGE_NAME.leases $BRIDGE_NAME
                       ;;
                       FIXED)
                         einfo "Setting IP for $BRIDGE_NAME to $BRIDGE_IP"
                         ip a a $BRIDGE_IP dev $BRIDGE_NAME
                       ;;
                       NONE)
                         einfo "Leaving $BRIDGE_NAME unconfigured"
                       ;;
               esac
            eoutdent
        eoutdent
   ;;

   stop)
        einfo "Stopping bridge"
        eindent
            if [ $BRIDGE_CONFIG = DHCP ]; then
               einfo "Terminating dhclient for $BRIDGE_NAME"
               if [ -r "/var/run/dhclient.$BRIDGE_NAME.pid" ] ; then
                 kill "$(cat /var/run/dhclient.$BRIDGE_NAME.pid)" || /bin/true
               fi
            fi
            einfo "Removing network devices from $BRIDGE_NAME: "

            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i "
                   brctl delif "$BRIDGE_NAME" $i  ; eend $?
               done
            eoutdent

            einfo "Disabling promiscuous mode on: "
            eindent
               for i in $BRIDGE_DEVICES ; do
                   einfo "$i "
                   ip link set "$i" promisc off ; eend $?
               done
            eoutdent

            einfo "Bringing bridge: $BRIDGE_NAME down"
            ip link set "$BRIDGE_NAME" down; eend $?

            einfo "Removing bridge device"
            ifconfig "$BRIDGE_NAME" down || /bin/true
            brctl delbr "$BRIDGE_NAME"
            eend $?
        eoutdent
   ;;

   restart)
        $0 stop
        sleep 1
        $0 start
   ;;

   info)
        einfo "$0 - script which turns on basic bridging capabilities"
        einfo "Configure via $CONFIG_FILE" ; eend 0
   ;;

   status)
        einfo "$0 - status:"
        $BRCTL show ; eend $?
   ;;

   *)
        echo "Usage: $0 {start|stop|restart|status|info}"
        exit 1
   ;;
esac

## END OF FILE #################################################################
# vim: ft=sh expandtab ai
