2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
25 assert binary_exists brctl
26 assert binary_exists rstpctl
29 init_register stp_init
31 function __rstpctl_bridge_get
() {
40 rstpctl dumpbridge
${bridge} | \
41 while read bridge key val
; do
42 if [ "${key}" = "${param}" ]; then
51 function __rstpctl_port_get
() {
62 rstpctl dumpports
${bridge} | \
63 while read por key val
; do
64 if [ "${port}" = "${por}" -a "${key}" = "${param}" ]; then
73 function stp_enable
() {
77 assert zone_exists
${bridge}
79 brctl stp
${bridge} on
81 local mode
=$
(zone_config_get
${bridge} STP_MODE
)
85 rstpctl setforcevers
${bridge} slow
88 rstpctl setforcevers
${bridge} normal
91 log WARNING
"Unknown protocol version: ${mode}."
92 log WARNING
"Using default mode."
97 function stp_disable
() {
101 assert zone_exists
${bridge}
103 brctl stp
${bridge} off
106 function stp_bridge_get_protocol
() {
111 local enabled
=$
(__device_get_file
${bridge} "bridge/stp_state")
112 if ! enabled
${enabled}; then
116 local mode
=$
(__rstpctl_bridge_get
${bridge} protocol_version
)
125 # When rstpctl has an error, we assume that rstpd is not running and
126 # return the slow mode.
133 function stp_bridge_set_protocol
() {
137 function stp_bridge_get_id
() {
142 case "$(stp_bridge_get_protocol ${bridge})" in
144 __rstpctl_bridge_get
${bridge} "id"
148 __device_get_file
${bridge} "bridge/bridge_id"
156 function stp_bridge_get_forward_delay
() {
161 case "$(stp_bridge_get_protocol ${bridge})" in
163 __rstpctl_bridge_get
${bridge} "bridge_forward_delay"
167 __device_get_file
${bridge} "bridge/forward_delay"
175 function stp_bridge_get_hello_time
() {
180 case "$(stp_bridge_get_protocol ${bridge})" in
182 __rstpctl_bridge_get
${bridge} "bridge_hello_time"
186 __device_get_file
${bridge} "bridge/hello_time"
194 function stp_bridge_get_max_age
() {
199 case "$(stp_bridge_get_protocol ${bridge})" in
201 __rstpctl_bridge_get
${bridge} "bridge_max_age"
205 __device_get_file
${bridge} "bridge/max_age"
213 function stp_bridge_get_designated_root
() {
219 case "$(stp_bridge_get_protocol ${bridge})" in
221 output
=$
(__rstpctl_bridge_get
${bridge} "designated_root")
224 output
=$
(__device_get_file
${bridge} "bridge/root_id")
228 if ! isset output
; then
232 mac_format
"${output:5:12}"
237 function stp_bridge_get_root_path_cost
() {
242 case "$(stp_bridge_get_protocol ${bridge})" in
244 __rstpctl_bridge_get
${bridge} "root_path_cost"
248 __device_get_file
${bridge} "bridge/root_path_cost"
256 function stp_bridge_get_root_port_id
() {
261 case "$(stp_bridge_get_protocol ${bridge})" in
263 __rstpctl_bridge_get
${bridge} "root_port"
267 __device_get_file
${bridge} "bridge/root_port"
275 function stp_bridge_get_root_port
() {
280 local id
=$
(stp_bridge_get_root_port_id
${bridge})
284 for member
in $
(bridge_get_members
${bridge}); do
285 member_id
=$
(stp_port_get_id
${bridge} ${member})
287 if [ "${id}" = "${member_id}" ]; then
296 function stp_bridge_is_root
() {
301 [ -n "$(stp_bridge_get_root_port ${bridge})" ]
304 function stp_bridge_get_priority
() {
309 case "$(stp_bridge_get_protocol ${bridge})" in
311 local output
=$
(__rstpctl_bridge_get
${bridge} "root_path_cost")
316 __device_get_file
${bridge} "bridge/priority"
324 function stp_bridge_get_topology_change_count
() {
329 case "$(stp_bridge_get_protocol ${bridge})" in
331 __rstpctl_bridge_get
${bridge} "topology_change_count"
335 __device_get_file
${bridge} "bridge/topology_change"
343 function stp_bridge_get_topology_change_timer
() {
348 case "$(stp_bridge_get_protocol ${bridge})" in
350 __rstpctl_bridge_get
${bridge} "time_since_topology_change"
354 __device_get_file
${bridge} "bridge/topology_change_timer"
362 function stp_bridge_get_topology_change_detected
() {
367 case "$(stp_bridge_get_protocol ${bridge})" in
369 __rstpctl_bridge_get
${bridge} "topology_change"
373 __device_get_file
${bridge} "bridge/topology_change_detected"
382 STP_STATE
[0]="disabled"
383 STP_STATE
[1]="listening"
384 STP_STATE
[2]="learning"
385 STP_STATE
[3]="forwarding"
386 STP_STATE
[4]="blocking"
388 function stp_port_get_state
() {
396 case "$(stp_bridge_get_protocol ${bridge})" in
398 output
=$
(__rstpctl_port_get
${bridge} ${port} "state")
401 output
=$
(__device_get_file
${bridge} "brif/${port}/state")
403 # Translate int to name
404 output
="${STP_STATE[${output}]}"
408 if ! isset output
; then
417 function stp_port_get_id
() {
424 case "$(stp_bridge_get_protocol ${bridge})" in
426 __rstpctl_port_get
${bridge} ${port} "id"
430 dec $
(__device_get_file
${bridge} "brif/${port}/port_no")
438 function stp_port_get_cost
() {
445 case "$(stp_bridge_get_protocol ${bridge})" in
447 __rstpctl_port_get
${bridge} ${port} "path_cost"
451 __device_get_file
${bridge} "brif/${port}/path_cost"
459 function stp_port_get_designated_root
() {
467 case "$(stp_bridge_get_protocol ${bridge})" in
469 output
=$
(__rstpctl_port_get
${bridge} ${port} "designated_root")
472 output
=$
(__device_get_file
${bridge} "brif/${port}/designated_root")
476 if [ -n "${output}" ]; then
477 mac_format
${output:5:12}