]> git.ipfire.org Git - people/arne_f/network.git/blob - hooks/bridge
network: Don't wait that zone gets in forwarding state.
[people/arne_f/network.git] / hooks / bridge
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /lib/network/header-zone
23
24 HOOK_SETTINGS="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_PRIORITY"
25 HOOK_SETTINGS="${HOOK_SETTINGS} MAC MTU"
26
27 # Default values
28 MAC=$(mac_generate)
29 MTU=1500
30 STP="on"
31 STP_FORWARD_DELAY=0
32 STP_HELLO=2
33 STP_MAXAGE=20
34 STP_PRIORITY=512 # XXX check out better value
35
36 function _check() {
37 assert ismac MAC
38 assert isbool STP
39 assert isinteger STP_HELLO
40 assert isinteger STP_FORWARD_DELAY
41 assert isinteger STP_PRIORITY
42 assert isinteger MTU
43 }
44
45 function _parse_cmdline() {
46 while [ $# -gt 0 ]; do
47 case "${1}" in
48 --stp=*)
49 STP=${1#--stp=}
50 ;;
51 --stp-hello=*)
52 STP_HELLO=${1#--stp-hello=}
53 ;;
54 --stp-forward-delay=*)
55 STP_FORWARD_DELAY=${1#--stp-forward-delay=}
56 ;;
57 --stp-priority=*)
58 STP_PRIORITY=${1#--stp-priority=}
59 ;;
60 --mtu=*)
61 MTU=${1#--mtu=}
62 ;;
63 --mac=*)
64 MAC=${1#--mac=}
65 ;;
66 *)
67 warning "Ignoring unknown option '${1}'"
68 ;;
69 esac
70 shift
71 done
72 }
73
74 function _up() {
75 local zone=${1}
76 shift
77
78 config_read ${ZONE_DIR}/${zone}/settings
79
80 if ! device_exists ${zone}; then
81 brctl addbr ${zone}
82 fi
83
84 [ -n "${MAC}" ] && device_set_address ${zone} ${MAC}
85 [ -n "${MTU}" ] && device_set_mtu ${zone} ${MTU}
86
87 # Enable STP
88 if enabled STP; then
89 brctl stp ${zone} on
90
91 if [ -n "${STP_FORWARD_DELAY}" ]; then
92 brctl setfd ${zone} ${STP_FORWARD_DELAY}
93 fi
94
95 if [ -n "${STP_HELLO}" ]; then
96 brctl sethello ${zone} ${STP_HELLO}
97 fi
98
99 if [ -n "${STP_MAXAGE}" ]; then
100 brctl setmaxage ${zone} ${STP_MAXAGE}
101 fi
102
103 if [ -n "${STP_PRIORITY}" ]; then
104 brctl setbridgeprio ${zone} ${STP_PRIORITY}
105 fi
106 else
107 brctl stp ${zone} off
108 fi
109
110 device_set_up ${zone}
111
112 # Bring all ports up
113 zone_ports_up ${zone}
114 zone_configs_up ${zone}
115
116 event_interface_up ${zone}
117
118 exit $?
119 }
120
121 function _down() {
122 local zone=${1}
123 shift
124
125 if ! device_is_up ${zone}; then
126 warning "Zone '${zone}' is not up"
127 exit ${EXIT_OK}
128 fi
129
130 event_interface_down ${zone}
131
132 zone_configs_down ${zone}
133 zone_ports_down ${zone}
134
135 device_set_down ${zone}
136 brctl delbr ${zone}
137
138 exit $?
139 }
140
141 function _status() {
142 local zone=${1}
143
144 cli_status_headline ${zone}
145
146 # Exit if zone is down
147 if ! zone_is_up ${zone}; then
148 echo # Empty line
149 exit ${EXIT_ERROR}
150 fi
151
152 # XXX Add bridge stp priority here
153 # brctl does not give any information about that
154
155 cli_headline " Spanning Tree Protocol information:"
156 echo " Bridge ID : $(stp_bridge_id ${zone})"
157 echo " Designated root : $(stp_designated_root ${zone})"
158 echo " Path cost : $(stp_pathcost ${zone})"
159 echo # Empty line
160
161 # Topology information
162 printf " Topology changing : %6s\n" $(stp_topology_change ${zone})
163 printf " Topology change time : %6s\n" $(beautify_time $(stp_topology_change_time ${zone}))
164 printf " Topology change count : %6s\n" $(stp_topology_change_count ${zone})
165
166 cli_headline " Ports:"
167 zone_ports_cmd status ${zone}
168
169 cli_headline " Configurations:"
170 zone_configs_cmd status ${zone}
171
172 echo # Empty line
173 exit ${EXIT_OK}
174 }
175
176 function _addport() {
177 local zone=${1}
178 local hook=${2}
179 shift 2
180
181 if ! hook_exists port ${hook}; then
182 error "Hook does not exist '${hook}'"
183 exit ${EXIT_ERROR}
184 fi
185
186 port_hook ${hook} add ${zone}
187 }
188
189 run $@