Fix bashism/non-POSIX usage of $UID.
[grml-network.git] / sbin / grml-bridge
1 #!/bin/sh
2 # Filename:      grml-bridge
3 # Purpose:       set up your box as bridge
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 CONFIG_FILE=/etc/grml/routersetup
10 . /etc/grml/lsb-functions
11 . /etc/grml/script-functions
12
13 check4root || exit 1
14
15 if ! [ -r "$CONFIG_FILE" ] ; then
16   eerror "$CONFIG_FILE could not be read."
17   exit 1
18 fi
19
20 . "$CONFIG_FILE"
21
22 if [ -z "$BRIDGE_DEVICES" ] ; then
23    eerror "Bridge devices (\$BRIDGE_DEVICES) not set in $CONFIG_FILE"
24    exit 1
25 fi
26
27 [ -n "$BRCTL" ] || BRCTL=brctl
28 [ -n "$STP"   ] || STP=on
29 [ -n "$BRIDGE_NAME" ] || BRIDGE_NAME=br0
30
31 check4progs $BRCTL || exit 1
32
33 case "$1" in
34     start)
35         einfo "Starting bridge"
36         eindent
37             einfo "Creating bridge device"
38             brctl addbr "$BRIDGE_NAME"
39             eend $?
40             case "$BRIDGE_STP" in
41                 no|false)
42                     ;;
43                 *)
44                     einfo "Setting Spanning-Tree Protocol (STP) to status $STP"
45                     brctl stp "$BRIDGE_NAME" $STP
46                     eend $?
47                     ;;
48             esac
49
50             einfo "Bringing network device up: "
51             eindent
52                for i in $BRIDGE_DEVICES ; do
53                    einfo "$i"
54                    ifconfig "$i" 0.0.0.0 up ; eend $?
55                done
56             eoutdent
57
58             einfo "Enabling promiscuous mode on: "
59             eindent
60                for i in $BRIDGE_DEVICES ; do
61                    einfo "$i"
62                    ip link set "$i" promisc on ; eend $?
63                done
64             eoutdent
65
66             einfo "Adding network devices to $BRIDGE_NAME: "
67             eindent
68             for i in $BRIDGE_DEVICES ; do
69                 einfo "$i"
70                 brctl addif "$BRIDGE_NAME" $i  ; eend $?
71             done
72             eoutdent
73
74             einfo "Bringing bridge $BRIDGE_NAME up"
75             ip link set "$BRIDGE_NAME" up ; eend $?
76             eindent
77                case $BRIDGE_CONFIG in
78                        DHCP)
79                          einfo "starting dhclient for $BRIDGE_NAME"
80                          dhclient -pf /var/run/dhclient.$BRIDGE_NAME.pid -lf /var/run/dhclient.$BRIDGE_NAME.leases $BRIDGE_NAME
81                        ;;
82                        FIXED)
83                          einfo "Setting IP for $BRIDGE_NAME to $BRIDGE_IP"
84                          ip a a $BRIDGE_IP dev $BRIDGE_NAME
85                        ;;
86                        NONE)
87                          einfo "Leaving $BRIDGE_NAME unconfigured"
88                        ;;
89                esac
90             eoutdent
91         eoutdent
92    ;;
93
94    stop)
95         einfo "Stopping bridge"
96         eindent
97             if [ $BRIDGE_CONFIG = DHCP ]; then
98                einfo "Terminating dhclient for $BRIDGE_NAME"
99                if [ -r "/var/run/dhclient.$BRIDGE_NAME.pid" ] ; then
100                  kill "$(cat /var/run/dhclient.$BRIDGE_NAME.pid)" || /bin/true
101                fi
102             fi
103             einfo "Removing network devices from $BRIDGE_NAME: "
104
105             eindent
106                for i in $BRIDGE_DEVICES ; do
107                    einfo "$i "
108                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
109                done
110             eoutdent
111
112             einfo "Disabling promiscuous mode on: "
113             eindent
114                for i in $BRIDGE_DEVICES ; do
115                    einfo "$i "
116                    ip link set "$i" promisc off ; eend $?
117                done
118             eoutdent
119
120             einfo "Bringing bridge: $BRIDGE_NAME down"
121             ip link set "$BRIDGE_NAME" down; eend $?
122
123             einfo "Removing bridge device"
124             ifconfig "$BRIDGE_NAME" down || /bin/true
125             brctl delbr "$BRIDGE_NAME"
126             eend $?
127         eoutdent
128    ;;
129
130    restart)
131         $0 stop
132         sleep 1
133         $0 start
134    ;;
135
136    info)
137         einfo "$0 - script which turns on basic bridging capabilities"
138         einfo "Configure via $CONFIG_FILE" ; eend 0
139    ;;
140
141    status)
142         einfo "$0 - status:"
143         $BRCTL show ; eend $?
144    ;;
145
146    *)
147         echo "Usage: $0 {start|stop|restart|status|info}"
148         exit 1
149    ;;
150 esac
151
152 ## END OF FILE #################################################################
153 # vim: ft=sh expandtab ai