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