]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.stp
Fix weird device CLI command.
[people/arne_f/network.git] / functions.stp
CommitLineData
e84e4e76
MT
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
6b3f9c85
MT
22function stp_init() {
23 module_load stp
24
25 assert binary_exists brctl
26 assert binary_exists rstpctl
27}
28
29init_register stp_init
30
feb76eaf
MT
31function __rstpctl_bridge_get() {
32 local bridge=${1}
33 local param=${2}
34
35 assert isset bridge
36 assert isset param
e84e4e76 37
e84e4e76
MT
38 local key
39 local val
feb76eaf
MT
40 rstpctl dumpbridge ${bridge} | \
41 while read bridge key val; do
42 if [ "${key}" = "${param}" ]; then
43 echo "${val}"
e84e4e76
MT
44 return ${EXIT_OK}
45 fi
46 done
47
48 return ${EXIT_ERROR}
49}
50
feb76eaf 51function __rstpctl_port_get() {
e84e4e76
MT
52 local bridge=${1}
53 local port=${2}
feb76eaf 54 local param=${3}
e84e4e76 55
feb76eaf
MT
56 assert isset bridge
57 assert isset port
58 assert isset param
59
60 local key
61 local val
62 rstpctl dumpports ${bridge} | \
63 while read por key val; do
64 if [ "${port}" = "${por}" -a "${key}" = "${param}" ]; then
65 echo "${val}"
e84e4e76
MT
66 return ${EXIT_OK}
67 fi
68 done
69
70 return ${EXIT_ERROR}
71}
72
feb76eaf 73function stp_enable() {
e84e4e76 74 local bridge=${1}
e84e4e76 75
feb76eaf
MT
76 assert isset bridge
77 assert zone_exists ${bridge}
e84e4e76 78
feb76eaf 79 brctl stp ${bridge} on
e84e4e76 80
feb76eaf
MT
81 local mode=$(zone_config_get ${bridge} STP_MODE)
82
83 case "${mode}" in
84 stp)
85 rstpctl setforcevers ${bridge} slow
86 ;;
87 rstp)
88 rstpctl setforcevers ${bridge} normal
89 ;;
90 *)
91 error_log "Unknown protocol version: ${mode}."
92 ;;
93 esac
e84e4e76
MT
94}
95
feb76eaf 96function stp_disable() {
e84e4e76 97 local bridge=${1}
e84e4e76 98
feb76eaf
MT
99 assert isset bridge
100 assert zone_exists ${bridge}
101
102 brctl stp ${bridge} off
e84e4e76
MT
103}
104
feb76eaf 105function stp_bridge_get_protocol() {
e84e4e76 106 local bridge=${1}
e84e4e76 107
feb76eaf
MT
108 assert isset bridge
109
110 local mode=$(__rstpctl_bridge_get ${bridge} protocol_version)
111
112 case "${mode}" in
113 0)
114 echo "stp"
115 ;;
116 2)
117 echo "rstp"
118 ;;
119 esac
e84e4e76
MT
120}
121
feb76eaf
MT
122function stp_bridge_set_protocol() {
123 : XXX WANTED
124}
125
126function stp_bridge_get_id() {
e84e4e76 127 local bridge=${1}
e84e4e76 128
feb76eaf
MT
129 assert isset bridge
130
131 case "$(stp_bridge_get_protocol ${bridge})" in
132 rstp)
133 __rstpctl_bridge_get ${bridge} "id"
134 return ${EXIT_OK}
135 ;;
136 stp)
137 __device_get_file ${bridge} "bridge/bridge_id"
138 return ${EXIT_OK}
139 ;;
140 esac
141
142 return ${EXIT_ERROR}
e84e4e76
MT
143}
144
feb76eaf 145function stp_bridge_get_forward_delay() {
e84e4e76 146 local bridge=${1}
e84e4e76 147
feb76eaf
MT
148 assert isset bridge
149
150 case "$(stp_bridge_get_protocol ${bridge})" in
151 rstp)
152 __rstpctl_bridge_get ${bridge} "bridge_forward_delay"
153 return ${EXIT_OK}
154 ;;
155 stp)
156 __device_get_file ${bridge} "bridge/forward_delay"
157 return ${EXIT_OK}
158 ;;
159 esac
160
161 return ${EXIT_ERROR}
e84e4e76
MT
162}
163
feb76eaf 164function stp_bridge_get_hello_time() {
e84e4e76
MT
165 local bridge=${1}
166
feb76eaf 167 assert isset bridge
e84e4e76 168
feb76eaf
MT
169 case "$(stp_bridge_get_protocol ${bridge})" in
170 rstp)
171 __rstpctl_bridge_get ${bridge} "bridge_hello_time"
e84e4e76
MT
172 return ${EXIT_OK}
173 ;;
feb76eaf
MT
174 stp)
175 __device_get_file ${bridge} "bridge/hello_time"
176 return ${EXIT_OK}
e84e4e76
MT
177 ;;
178 esac
feb76eaf
MT
179
180 return ${EXIT_ERROR}
e84e4e76
MT
181}
182
feb76eaf 183function stp_bridge_get_max_age() {
e84e4e76
MT
184 local bridge=${1}
185
feb76eaf
MT
186 assert isset bridge
187
188 case "$(stp_bridge_get_protocol ${bridge})" in
189 rstp)
190 __rstpctl_bridge_get ${bridge} "bridge_max_age"
191 return ${EXIT_OK}
192 ;;
193 stp)
194 __device_get_file ${bridge} "bridge/max_age"
195 return ${EXIT_OK}
196 ;;
197 esac
198
199 return ${EXIT_ERROR}
e84e4e76
MT
200}
201
feb76eaf 202function stp_bridge_get_designated_root() {
e84e4e76 203 local bridge=${1}
feb76eaf
MT
204 local output
205
206 assert isset bridge
e84e4e76 207
feb76eaf
MT
208 case "$(stp_bridge_get_protocol ${bridge})" in
209 rstp)
210 output=$(__rstpctl_bridge_get ${bridge} "designated_root")
211 ;;
212 stp)
213 output=$(__device_get_file ${bridge} "bridge/root_id")
214 ;;
215 esac
216
217 if ! isset output; then
218 return ${EXIT_ERROR}
219 fi
220
221 mac_format "${output:5:12}"
222
223 return ${EXIT_OK}
e84e4e76
MT
224}
225
feb76eaf 226function stp_bridge_get_root_path_cost() {
e84e4e76
MT
227 local bridge=${1}
228
feb76eaf
MT
229 assert isset bridge
230
231 case "$(stp_bridge_get_protocol ${bridge})" in
232 rstp)
233 __rstpctl_bridge_get ${bridge} "root_path_cost"
234 return ${EXIT_OK}
235 ;;
236 stp)
237 __device_get_file ${bridge} "bridge/root_path_cost"
238 return ${EXIT_OK}
239 ;;
240 esac
e84e4e76 241
feb76eaf 242 return ${EXIT_ERROR}
e84e4e76
MT
243}
244
feb76eaf 245function stp_bridge_get_root_port_id() {
e84e4e76
MT
246 local bridge=${1}
247
feb76eaf
MT
248 assert isset bridge
249
250 case "$(stp_bridge_get_protocol ${bridge})" in
251 rstp)
252 __rstpctl_bridge_get ${bridge} "root_port"
253 return ${EXIT_OK}
254 ;;
255 stp)
256 __device_get_file ${bridge} "bridge/root_port"
257 return ${EXIT_OK}
258 ;;
259 esac
e84e4e76 260
feb76eaf 261 return ${EXIT_ERROR}
e84e4e76
MT
262}
263
feb76eaf 264function stp_bridge_get_root_port() {
e84e4e76
MT
265 local bridge=${1}
266
feb76eaf 267 assert isset bridge
e84e4e76 268
feb76eaf 269 local id=$(stp_bridge_get_root_port_id ${bridge})
e84e4e76 270
feb76eaf
MT
271 local member
272 local member_id
273 for member in $(bridge_get_members ${bridge}); do
274 member_id=$(stp_port_get_id ${bridge} ${member})
e84e4e76 275
feb76eaf
MT
276 if [ "${id}" = "${member_id}" ]; then
277 echo "${member}"
278 return ${EXIT_OK}
279 fi
280 done
e84e4e76 281
feb76eaf 282 return ${EXIT_ERROR}
e84e4e76
MT
283}
284
feb76eaf
MT
285function stp_bridge_is_root() {
286 local bridge=${1}
e84e4e76 287
feb76eaf 288 assert isset bridge
e84e4e76 289
feb76eaf 290 [ -n "$(stp_bridge_get_root_port ${bridge})" ]
e84e4e76
MT
291}
292
feb76eaf
MT
293function stp_bridge_get_priority() {
294 local bridge=${1}
e84e4e76 295
feb76eaf 296 assert isset bridge
e84e4e76 297
feb76eaf
MT
298 case "$(stp_bridge_get_protocol ${bridge})" in
299 rstp)
300 local output=$(__rstpctl_bridge_get ${bridge} "root_path_cost")
301 dec "${output:0:4}"
302 return ${EXIT_OK}
303 ;;
304 stp)
305 __device_get_file ${bridge} "bridge/priority"
306 return ${EXIT_OK}
307 ;;
308 esac
e84e4e76 309
feb76eaf 310 return ${EXIT_ERROR}
e84e4e76
MT
311}
312
feb76eaf
MT
313function stp_bridge_get_topology_change_count() {
314 local bridge=${1}
e84e4e76 315
feb76eaf 316 assert isset bridge
e84e4e76 317
feb76eaf
MT
318 case "$(stp_bridge_get_protocol ${bridge})" in
319 rstp)
320 __rstpctl_bridge_get ${bridge} "topology_change_count"
321 return ${EXIT_OK}
322 ;;
323 stp)
324 __device_get_file ${bridge} "bridge/topology_change"
325 return ${EXIT_OK}
326 ;;
327 esac
e84e4e76 328
feb76eaf 329 return ${EXIT_ERROR}
e84e4e76
MT
330}
331
feb76eaf
MT
332function stp_bridge_get_topology_change_timer() {
333 local bridge=${1}
334
335 assert isset bridge
336
337 case "$(stp_bridge_get_protocol ${bridge})" in
338 rstp)
339 __rstpctl_bridge_get ${bridge} "time_since_topology_change"
340 return ${EXIT_OK}
341 ;;
342 stp)
343 __device_get_file ${bridge} "bridge/topology_change_timer"
344 return ${EXIT_OK}
345 ;;
346 esac
e84e4e76 347
feb76eaf 348 return ${EXIT_ERROR}
e84e4e76
MT
349}
350
feb76eaf
MT
351function stp_bridge_get_topology_change_detected() {
352 local bridge=${1}
e84e4e76 353
feb76eaf 354 assert isset bridge
e84e4e76 355
feb76eaf
MT
356 case "$(stp_bridge_get_protocol ${bridge})" in
357 rstp)
358 __rstpctl_bridge_get ${bridge} "topology_change"
359 return ${EXIT_OK}
360 ;;
361 stp)
362 __device_get_file ${bridge} "bridge/topology_change_detected"
363 return ${EXIT_OK}
364 ;;
365 esac
6b3f9c85 366
feb76eaf 367 return ${EXIT_ERROR}
6b3f9c85
MT
368}
369
feb76eaf
MT
370# STP states
371STP_STATE[0]="disabled"
372STP_STATE[1]="listening"
373STP_STATE[2]="learning"
374STP_STATE[3]="forwarding"
375STP_STATE[4]="blocking"
376
377function stp_port_get_state() {
6b3f9c85 378 local bridge=${1}
feb76eaf
MT
379 local port=${2}
380 local output
6b3f9c85
MT
381
382 assert isset bridge
feb76eaf 383 assert isset port
6b3f9c85 384
feb76eaf
MT
385 case "$(stp_bridge_get_protocol ${bridge})" in
386 rstp)
387 output=$(__rstpctl_port_get ${bridge} ${port} "state")
388 ;;
389 stp)
390 output=$(__device_get_file ${bridge} "brif/${port}/state")
6b3f9c85 391
feb76eaf
MT
392 # Translate int to name
393 output="${STP_STATE[${output}]}"
394 ;;
395 esac
6b3f9c85 396
feb76eaf
MT
397 if ! isset output; then
398 return ${EXIT_ERROR}
399 fi
400
401 echo "${output^^}"
402
403 return ${EXIT_OK}
404}
405
406function stp_port_get_id() {
407 local bridge=${1}
408 local port=${2}
409
410 assert isset bridge
411 assert isset port
412
413 case "$(stp_bridge_get_protocol ${bridge})" in
414 rstp)
415 __rstpctl_port_get ${bridge} ${port} "id"
416 return ${EXIT_OK}
417 ;;
6b3f9c85 418 stp)
feb76eaf
MT
419 dec $(__device_get_file ${bridge} "brif/${port}/port_no")
420 return ${EXIT_OK}
6b3f9c85 421 ;;
feb76eaf
MT
422 esac
423
424 return ${EXIT_ERROR}
425}
426
427function stp_port_get_cost() {
428 local bridge=${1}
429 local port=${2}
430
431 assert isset bridge
432 assert isset port
433
434 case "$(stp_bridge_get_protocol ${bridge})" in
6b3f9c85 435 rstp)
feb76eaf
MT
436 __rstpctl_port_get ${bridge} ${port} "path_cost"
437 return ${EXIT_OK}
6b3f9c85 438 ;;
feb76eaf
MT
439 stp)
440 __device_get_file ${bridge} "brif/${port}/path_cost"
441 return ${EXIT_OK}
6b3f9c85
MT
442 ;;
443 esac
feb76eaf
MT
444
445 return ${EXIT_ERROR}
6b3f9c85
MT
446}
447
feb76eaf 448function stp_port_get_designated_root() {
6b3f9c85 449 local bridge=${1}
feb76eaf
MT
450 local port=${2}
451 local output
6b3f9c85
MT
452
453 assert isset bridge
feb76eaf 454 assert isset port
6b3f9c85 455
feb76eaf
MT
456 case "$(stp_bridge_get_protocol ${bridge})" in
457 rstp)
458 output=$(__rstpctl_port_get ${bridge} ${port} "designated_root")
459 ;;
460 stp)
461 output=$(__device_get_file ${bridge} "brif/${port}/designated_root")
462 ;;
463 esac
464
465 mac_format ${output:5:12}
466
467 return ${EXIT_ERROR}
6b3f9c85 468}