Disable default configuration in routersetup config file, use defaults in main scripts
[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   . "$CONFIG_FILE"
17 else
18   ewarn "Could not read $CONFIG_FILE"
19 fi
20
21 # defaults if unconfigured
22 [ -n "$BRCTL" ]          || BRCTL=brctl
23 [ -n "$BRIDGE_STP"   ]   || BRIDGE_STP=on
24 [ -n "$BRIDGE_NAME" ]    || BRIDGE_NAME=br0
25 [ -n "$BRIDGE_DEVICES" ] || BRIDGE_DEVICES='eth0 eth1'
26
27 check4progs $BRCTL || exit 1
28
29 case "$1" in
30     start)
31         einfo "Starting bridge"
32         eindent
33             einfo "Creating bridge device"
34             brctl addbr "$BRIDGE_NAME"
35             eend $?
36             case "$BRIDGE_STP" in
37                 no|false)
38                     ;;
39                 *)
40                     einfo "Setting Spanning-Tree Protocol (STP) to status $BRIDGE_STP"
41                     brctl stp "$BRIDGE_NAME" "$BRIDGE_STP"
42                     eend $?
43                     ;;
44             esac
45
46             einfo "Bringing network device up: "
47             eindent
48                for i in $BRIDGE_DEVICES ; do
49                    einfo "$i"
50                    ifconfig "$i" 0.0.0.0 up ; eend $?
51                done
52             eoutdent
53
54             einfo "Enabling promiscuous 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 unconfigured"
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                if [ -r "/var/run/dhclient.$BRIDGE_NAME.pid" ] ; then
96                  kill "$(cat /var/run/dhclient.$BRIDGE_NAME.pid)" || /bin/true
97                fi
98             fi
99             einfo "Removing network devices from $BRIDGE_NAME: "
100
101             eindent
102                for i in $BRIDGE_DEVICES ; do
103                    einfo "$i "
104                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
105                done
106             eoutdent
107
108             einfo "Disabling promiscuous mode on: "
109             eindent
110                for i in $BRIDGE_DEVICES ; do
111                    einfo "$i "
112                    ip link set "$i" promisc off ; eend $?
113                done
114             eoutdent
115
116             einfo "Bringing bridge: $BRIDGE_NAME down"
117             ip link set "$BRIDGE_NAME" down; eend $?
118
119             einfo "Removing bridge device"
120             ifconfig "$BRIDGE_NAME" down || /bin/true
121             brctl delbr "$BRIDGE_NAME"
122             eend $?
123         eoutdent
124    ;;
125
126    restart)
127         $0 stop
128         sleep 1
129         $0 start
130    ;;
131
132    info)
133         einfo "$0 - script which turns on basic bridging capabilities"
134         einfo "Configure via $CONFIG_FILE" ; eend 0
135    ;;
136
137    status)
138         einfo "$0 - status:"
139         $BRCTL show ; eend $?
140    ;;
141
142    *)
143         echo "Usage: $0 {start|stop|restart|status|info}"
144         exit 1
145    ;;
146 esac
147
148 ## END OF FILE #################################################################
149 # vim: ft=sh expandtab ai