c7decc7037b6455c78e5141d7f1bbd767adcfc01
[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"
49                     brctl stp "$BRIDGE_NAME" $STP
50                     eend $?
51                     ;;
52             esac
53
54             einfo "Enabling promiscous mode on: "
55             eindent
56                for i in $BRIDGE_DEVICES ; do
57                    einfo "$i"
58                    ip link set "$i" promisc on ; eend $?
59                done
60             eoutdent
61
62             einfo "Adding network devices to $BRIDGE_NAME: "
63             eindent
64             for i in $BRIDGE_DEVICES ; do
65                 einfo "$i"
66                 brctl addif "$BRIDGE_NAME" $i  ; eend $?
67             done
68             eoutdent
69
70             einfo "Bringing bridge $BRIDGE_NAME up"
71             ip link set "$BRIDGE_NAME" up ; eend $?
72             eindent
73                case $BRIDGE_CONFIG in
74                        DHCP)
75                          einfo "starting dhclient for $BRIDGE_NAME"
76                          dhclient -pf /var/run/dhclient.$BRIDGE_NAME.pid -lf /var/run/dhclient.$BRIDGE_NAME.leases $BRIDGE_NAME
77                        ;;
78                        FIXED)
79                          einfo "Setting IP for $BRIDGE_NAME to $BRIDGE_IP"
80                          ip a a $BRIDGE_IP dev $BRIDGE_NAME
81                        ;;
82                        NONE)
83                          einfo "Leaving $BRIDGE_NAME uconfigured"
84                        ;;
85                esac
86             eoutdent
87         eoutdent
88    ;;
89
90    stop)
91         einfo "Stopping bridge"
92         eindent
93             if [ $BRIDGE_CONFIG = DHCP ]; then
94                einfo "Terminating dhclient for $BRIDGE_NAME"
95                kill $(cat /var/run/dhclient.$BRIDGE_NAME.pid);
96             fi
97             einfo "Removing network devices from $BRIDGE_NAME: "
98
99             eindent
100                for i in $BRIDGE_DEVICES ; do
101                    einfo "$i "
102                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
103                done
104             eoutdent
105
106             einfo "Disabling promiscous mode on: "
107             eindent
108                for i in $BRIDGE_DEVICES ; do
109                    einfo "$i "
110                    ip link set "$i" promisc off ; eend $?
111                done
112             eoutdent
113
114             einfo "Bringing bridge: $BRIDGE_NAME down"
115             ip link set "$BRIDGE_NAME" down; eend $?
116
117             einfo "Removing bridge device"
118             brctl delbr "$BRIDGE_NAME"
119             eend $?
120         eoutdent
121    ;;
122
123    restart)
124         $0 stop
125         sleep 1
126         $0 start
127    ;;
128
129    info)
130         einfo "$0 - script which turns on basic bridging capabilities"
131         einfo "Configure via $CONFIG_FILE" ; eend 0
132    ;;
133
134    status)
135         einfo "$0 - status:"
136         $BRCTL show ; eend $?
137    ;;
138
139    *)
140         echo "Usage: $0 {start|stop|restart|status|info}"
141         exit 1
142    ;;
143 esac
144
145 ## END OF FILE #################################################################
146 # vim: ft=sh expandtab ai