]> git.ipfire.org Git - people/ms/network.git/blame - functions.stp
Remove all hardcoded path now use switches for it - also perform UsrMove.
[people/ms/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
e266c18e
MT
22# The default mode.
23# We default to RSTP, because it has the better user experience and
24# faster convergence times. Despite of that, it completely downgradeable
25# to plain STP.
26STP_DEFAULT_MODE="rstp"
6b3f9c85 27
e266c18e
MT
28# Allowed modes of the spanning tree protocol.
29STP_ALLOWED_MODES="rstp stp"
6b3f9c85 30
feb76eaf 31function stp_enable() {
e84e4e76 32 local bridge=${1}
e266c18e 33 local mode=${2}
e84e4e76 34
feb76eaf
MT
35 assert isset bridge
36 assert zone_exists ${bridge}
e84e4e76 37
e266c18e 38 # Tell the kernel to enable STP.
feb76eaf 39 brctl stp ${bridge} on
e84e4e76 40
e266c18e
MT
41 # Set the correct protocol version.
42 if [ -z "${mode}" ]; then
43 mode="${STP_DEFAULT_MODE}"
44 fi
45 stp_bridge_set_protocol ${bridge} ${mode}
e84e4e76
MT
46}
47
feb76eaf 48function stp_disable() {
e84e4e76 49 local bridge=${1}
e84e4e76 50
feb76eaf
MT
51 assert isset bridge
52 assert zone_exists ${bridge}
53
54 brctl stp ${bridge} off
e84e4e76
MT
55}
56
8d4f9311
MT
57function stp_get_name() {
58 local proto=${1}
59
60 case "${proto}" in
61 stp)
62 echo "Spanning Tree Protocol"
63 ;;
64 rstp)
65 echo "Rapid Spanning Tree Protocol"
66 ;;
67 mstp)
68 echo "Multiple Spanning Tree Protocol"
69 ;;
70 esac
71
72 return ${EXIT_OK}
73}
74
e266c18e
MT
75function stp_bridge_set_protocol() {
76 local bridge=${1}
77 local mode=${2}
78
79 assert isset bridge
80 assert isset mode
81
82 if ! listmatch ${mode} ${STP_ALLOWED_MODES}; then
83 log WARNING "Unknown protocol version: ${mode}."
84 log WARNING "Using default mode."
85
86 mode="${STP_DEFAULT_MODE}"
87 fi
88
89 mstpctl setforcevers ${bridge} ${mode}
90 assert [ $? -eq 0 ]
91}
92
feb76eaf 93function stp_bridge_get_protocol() {
e84e4e76 94 local bridge=${1}
e84e4e76 95
feb76eaf
MT
96 assert isset bridge
97
8d4f9311
MT
98 # Let's check what the kernel is telling us about it's STP state.
99 local state=$(__device_get_file ${bridge} "bridge/stp_state")
feb76eaf 100
8d4f9311 101 case "${state}" in
feb76eaf 102 0)
8d4f9311
MT
103 # STP is disabled.
104 return ${EXIT_OK}
105 ;;
106 1)
107 # Kernel mode STP is running.
feb76eaf 108 echo "stp"
8d4f9311 109 return ${EXIT_OK}
feb76eaf
MT
110 ;;
111 2)
8d4f9311 112 # User-space STP is running.
36e3fd2f 113 ;;
8d4f9311
MT
114 *)
115 log ERROR "Kernel is running in an unknown STP state."
116 return ${EXIT_ERROR}
36e3fd2f 117 ;;
feb76eaf 118 esac
e84e4e76 119
8d4f9311 120 # We get here, when STP is running in user-space mode.
e84e4e76 121
8d4f9311 122 # Get the current protocol version.
6cd354eb 123 mstpctl showbridge ${bridge} force-protocol-version
feb76eaf 124
8d4f9311 125 return ${EXIT_OK}
e84e4e76
MT
126}
127
8d4f9311 128function stp_bridge_get_id() {
e84e4e76
MT
129 local bridge=${1}
130
feb76eaf 131 assert isset bridge
e84e4e76 132
8d4f9311 133 __device_get_file ${bridge} "bridge/bridge_id"
feb76eaf 134
8d4f9311 135 return $?
e84e4e76
MT
136}
137
8d4f9311 138function stp_bridge_get_forward_delay() {
e84e4e76
MT
139 local bridge=${1}
140
feb76eaf
MT
141 assert isset bridge
142
6cd354eb 143 mstpctl showbridge ${bridge} forward-delay
8d4f9311 144}
feb76eaf 145
8d4f9311
MT
146function stp_bridge_set_forward_delay() {
147 : # XXX WANTED
e84e4e76
MT
148}
149
feb76eaf 150function stp_bridge_get_designated_root() {
e84e4e76 151 local bridge=${1}
feb76eaf
MT
152 local output
153
154 assert isset bridge
e84e4e76 155
6cd354eb 156 local output=$(mstpctl showbridge ${bridge} designated-root)
feb76eaf
MT
157
158 if ! isset output; then
159 return ${EXIT_ERROR}
160 fi
161
8d4f9311 162 echo "${output,,}"
feb76eaf 163 return ${EXIT_OK}
e84e4e76
MT
164}
165
feb76eaf 166function stp_bridge_get_root_path_cost() {
e84e4e76
MT
167 local bridge=${1}
168
feb76eaf
MT
169 assert isset bridge
170
6cd354eb 171 mstpctl showbridge ${bridge} path-cost
e84e4e76
MT
172}
173
feb76eaf 174function stp_bridge_get_root_port_id() {
e84e4e76
MT
175 local bridge=${1}
176
feb76eaf
MT
177 assert isset bridge
178
6cd354eb 179 local root_port=$(mstpctl showbridge ${bridge} root-port)
e84e4e76 180
8d4f9311
MT
181 # Return error, when there is no root port.
182 if [ "${root_port}" = "none" ]; then
183 return ${EXIT_ERROR}
184 fi
185
186 echo "${root_port}"
187 return ${EXIT_OK}
e84e4e76
MT
188}
189
feb76eaf 190function stp_bridge_get_root_port() {
e84e4e76
MT
191 local bridge=${1}
192
feb76eaf 193 assert isset bridge
e84e4e76 194
feb76eaf 195 local id=$(stp_bridge_get_root_port_id ${bridge})
e84e4e76 196
feb76eaf
MT
197 local member
198 local member_id
199 for member in $(bridge_get_members ${bridge}); do
200 member_id=$(stp_port_get_id ${bridge} ${member})
e84e4e76 201
feb76eaf
MT
202 if [ "${id}" = "${member_id}" ]; then
203 echo "${member}"
204 return ${EXIT_OK}
205 fi
206 done
e84e4e76 207
feb76eaf 208 return ${EXIT_ERROR}
e84e4e76
MT
209}
210
feb76eaf 211function stp_bridge_is_root() {
8d4f9311
MT
212 stp_bridge_get_root_port_id $@ >/dev/null
213 local ret=$?
e84e4e76 214
8d4f9311
MT
215 if [ ${ret} -eq ${EXIT_ERROR} ]; then
216 return ${EXIT_OK}
217 fi
e84e4e76 218
8d4f9311 219 return ${EXIT_ERROR}
e84e4e76
MT
220}
221
feb76eaf
MT
222function stp_bridge_get_priority() {
223 local bridge=${1}
e84e4e76 224
feb76eaf 225 assert isset bridge
e84e4e76 226
8d4f9311 227 local id=$(stp_bridge_get_id ${bridge})
e84e4e76 228
8d4f9311 229 dec "${id:0:4}"
e84e4e76
MT
230}
231
feb76eaf
MT
232function stp_bridge_get_topology_change_count() {
233 local bridge=${1}
e84e4e76 234
feb76eaf 235 assert isset bridge
e84e4e76 236
6cd354eb 237 mstpctl showbridge ${bridge} topology-change-count
e84e4e76
MT
238}
239
feb76eaf
MT
240function stp_bridge_get_topology_change_timer() {
241 local bridge=${1}
242
243 assert isset bridge
244
6cd354eb 245 mstpctl showbridge ${bridge} time-since-topology-change
e84e4e76
MT
246}
247
feb76eaf
MT
248function stp_bridge_get_topology_change_detected() {
249 local bridge=${1}
e84e4e76 250
feb76eaf 251 assert isset bridge
e84e4e76 252
6cd354eb 253 local change=$(mstpctl showbridge ${bridge} topology-change)
8d4f9311
MT
254
255 echo "${change}"
256 case "${change}" in
257 yes)
feb76eaf
MT
258 return ${EXIT_OK}
259 ;;
8d4f9311
MT
260 *)
261 return ${EXIT_ERROR}
feb76eaf
MT
262 ;;
263 esac
6b3f9c85
MT
264}
265
feb76eaf 266function stp_port_get_state() {
6b3f9c85 267 local bridge=${1}
feb76eaf 268 local port=${2}
6b3f9c85
MT
269
270 assert isset bridge
feb76eaf 271 assert isset port
6b3f9c85 272
6cd354eb 273 local state=$(mstpctl showportdetail ${bridge} ${port} state)
feb76eaf 274
8d4f9311 275 echo "${state^^}"
feb76eaf
MT
276}
277
278function stp_port_get_id() {
279 local bridge=${1}
280 local port=${2}
281
282 assert isset bridge
283 assert isset port
284
8d4f9311 285 local id=$(__device_get_file ${bridge} "brif/${port}/port_no")
feb76eaf 286
8d4f9311
MT
287 dec ${id}
288
289 return ${EXIT_OK}
feb76eaf
MT
290}
291
292function stp_port_get_cost() {
293 local bridge=${1}
294 local port=${2}
295
296 assert isset bridge
297 assert isset port
298
6cd354eb 299 mstpctl showportdetail ${bridge} ${port} external-port-cost
feb76eaf
MT
300
301 return ${EXIT_ERROR}
6b3f9c85
MT
302}
303
feb76eaf 304function stp_port_get_designated_root() {
6b3f9c85 305 local bridge=${1}
feb76eaf
MT
306 local port=${2}
307 local output
6b3f9c85
MT
308
309 assert isset bridge
feb76eaf 310 assert isset port
6b3f9c85 311
6cd354eb 312 output=$(mstpctl showportdetail ${bridge} ${port} designated-root)
feb76eaf 313
36e3fd2f 314 if [ -n "${output}" ]; then
8d4f9311 315 echo "${output,,}"
36e3fd2f
MT
316 return ${EXIT_OK}
317 fi
feb76eaf
MT
318
319 return ${EXIT_ERROR}
6b3f9c85 320}