]> git.ipfire.org Git - network.git/blame - src/functions/functions.stp
Remove support for Rapid Spanning Tree Protocol
[network.git] / src / functions / 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
1c6a4e30 22stp_enable() {
e84e4e76 23 local bridge=${1}
feb76eaf 24 assert isset bridge
e84e4e76 25
e266c18e 26 # Tell the kernel to enable STP.
7d85603e 27 print 1 > ${SYS_CLASS_NET}/${bridge}/bridge/stp_state
e84e4e76
MT
28}
29
1c6a4e30 30stp_disable() {
e84e4e76 31 local bridge=${1}
feb76eaf 32 assert isset bridge
feb76eaf 33
7d85603e
MT
34 # Tell the kernel to disable STP.
35 print 0 > ${SYS_CLASS_NET}/${bridge}/bridge/stp_state
e84e4e76
MT
36}
37
1c6a4e30 38stp_is_enabled() {
3cb2fc42
MT
39 local bridge=${1}
40 assert isset bridge
41
42 local state=$(__device_get_file ${bridge} bridge/stp_state)
43
44 case "${state}" in
45 0)
46 return ${EXIT_FALSE}
47 ;;
48 *)
49 return ${EXIT_TRUE}
50 ;;
51 esac
52}
53
1c6a4e30 54stp_bridge_get_id() {
e84e4e76 55 local bridge=${1}
feb76eaf 56 assert isset bridge
e84e4e76 57
8d4f9311 58 __device_get_file ${bridge} "bridge/bridge_id"
feb76eaf 59
8d4f9311 60 return $?
e84e4e76
MT
61}
62
1c6a4e30 63stp_bridge_get_forward_delay() {
e84e4e76 64 local bridge=${1}
feb76eaf
MT
65 assert isset bridge
66
4e0bc093
MT
67 local output=$(__device_get_file ${bridge} bridge/forward_delay)
68 __stp_div_100 ${output}
48bc31eb
MT
69
70 return ${EXIT_OK}
8d4f9311 71}
feb76eaf 72
1c6a4e30 73stp_bridge_set_forward_delay() {
e2aa12b3
MT
74 local bridge=${1}
75 assert isset bridge
76
77 local fdelay=${2}
78 assert isinteger fdelay
79
e2aa12b3
MT
80 # The smallest value that may be set is 2.
81 if [ ${fdelay} -lt 2 ]; then
82 fdelay=2
83 fi
84
016cb1c3
MT
85 # Check if the setting we want is already set.
86 local current_fdelay=$(stp_bridge_get_forward_delay ${bridge})
87 [ ${fdelay} -eq ${current_fdelay} ] && return ${EXIT_OK}
88
e2aa12b3 89 # Set the new value.
016cb1c3
MT
90 log DEBUG "Setting forward delay on bridge '${bridge}' from '${current_fdelay}' to '${fdelay}'"
91 fwrite "${SYS_CLASS_NET}/${bridge}/bridge/forward_delay" "$(( ${fdelay} * 100 ))"
e2aa12b3
MT
92
93 return ${EXIT_OK}
94}
95
1c6a4e30 96stp_bridge_get_hello_time() {
e2aa12b3
MT
97 local bridge=${1}
98 assert isset bridge
99
48bc31eb
MT
100 local ht=$(__device_get_file ${bridge} bridge/hello_time)
101 __stp_div_100 ${ht}
e2aa12b3 102
48bc31eb 103 return ${EXIT_OK}
e2aa12b3
MT
104}
105
1c6a4e30 106stp_bridge_set_hello_time() {
e2aa12b3
MT
107 local bridge=${1}
108 assert isset bridge
109
110 local hello=${2}
111 assert isinteger hello
112
113 # Check if the setting we want is already set.
114 local current_hello=$(stp_bridge_get_hello_time ${bridge})
115 [ ${hello} -eq ${current_hello} ] && return ${EXIT_OK}
116
117 # Set the new value.
48bc31eb
MT
118 log INFO "Changing hello time for '${bridge}': ${current_hello} --> ${hello}"
119 print "$(( ${hello} * 100 ))" > ${SYS_CLASS_NET}/${bridge}/bridge/hellow_time
e2aa12b3
MT
120
121 return ${EXIT_OK}
122}
123
1c6a4e30 124stp_bridge_get_max_age() {
e2aa12b3
MT
125 local bridge=${1}
126 assert isset bridge
127
128 local maxage=$(__device_get_file ${bridge} "bridge/max_age")
48bc31eb 129 __stp_div_100 ${maxage}
e2aa12b3 130
48bc31eb 131 return ${EXIT_OK}
e2aa12b3
MT
132}
133
1c6a4e30 134stp_bridge_set_max_age() {
e2aa12b3
MT
135 local bridge=${1}
136 assert isset bridge
137
138 local maxage=${2}
139 assert isinteger maxage
140
141 # Check if the setting we want is already set.
142 local current_maxage=$(stp_bridge_get_max_age ${bridge})
143 [ ${maxage} -eq ${current_maxage} ] && return ${EXIT_OK}
144
145 # Set the new value.
48bc31eb
MT
146 log INFO "Changing max age for '${bridge}': ${current_maxage} --> ${maxage}"
147 print "$(( ${maxage} * 100 ))" > ${SYS_CLASS_NET}/${bridge}/bridge/max_age
e2aa12b3
MT
148
149 return ${EXIT_OK}
150}
151
1c6a4e30 152stp_bridge_get_priority() {
e2aa12b3
MT
153 local bridge=${1}
154 assert isset bridge
155
156 __device_get_file ${bridge} "bridge/priority"
48bc31eb 157 return ${EXIT_OK}
e2aa12b3
MT
158}
159
1c6a4e30 160stp_bridge_set_priority() {
e2aa12b3
MT
161 local bridge=${1}
162 assert isset bridge
163
164 local priority=${2}
165 assert isinteger priority
166
167 # Check if the setting we want is already set.
168 local current_priority=$(stp_bridge_get_priority ${bridge})
169 [ ${priority} -eq ${current_priority} ] && return ${EXIT_OK}
170
171 # Set the new value.
48bc31eb
MT
172 log INFO "Changing priority for '${bridge}': ${current_priority} --> ${priority}"
173 print "${priority}" > ${SYS_CLASS_NET}/${bridge}/bridge/priority
e2aa12b3
MT
174
175 return ${EXIT_OK}
e84e4e76
MT
176}
177
1c6a4e30 178stp_bridge_get_designated_root() {
e84e4e76 179 local bridge=${1}
48bc31eb
MT
180 assert isset bridge
181
feb76eaf
MT
182 local output
183
4e0bc093 184 output=$(__device_get_file ${bridge} bridge/root_id)
48bc31eb 185 output=${output:6}
e84e4e76 186
48bc31eb
MT
187 # Print output (lowercase).
188 print "${output,,}"
feb76eaf 189
48bc31eb
MT
190 if isset output; then
191 return ${EXIT_OK}
192 else
feb76eaf
MT
193 return ${EXIT_ERROR}
194 fi
e84e4e76
MT
195}
196
1c6a4e30 197stp_bridge_get_root_path_cost() {
e84e4e76 198 local bridge=${1}
feb76eaf
MT
199 assert isset bridge
200
4e0bc093 201 __device_get_file ${bridge} bridge/root_path_cost
e84e4e76
MT
202}
203
1c6a4e30 204stp_bridge_get_root_port_id() {
e84e4e76 205 local bridge=${1}
feb76eaf
MT
206 assert isset bridge
207
4e0bc093 208 __device_get_file ${bridge} bridge/root_port_id
e84e4e76
MT
209}
210
1c6a4e30 211stp_bridge_get_root_port() {
e84e4e76 212 local bridge=${1}
feb76eaf 213 assert isset bridge
e84e4e76 214
feb76eaf 215 local id=$(stp_bridge_get_root_port_id ${bridge})
e84e4e76 216
48bc31eb 217 local member member_id
feb76eaf
MT
218 for member in $(bridge_get_members ${bridge}); do
219 member_id=$(stp_port_get_id ${bridge} ${member})
e84e4e76 220
feb76eaf 221 if [ "${id}" = "${member_id}" ]; then
48bc31eb 222 print "${member}"
feb76eaf
MT
223 return ${EXIT_OK}
224 fi
225 done
e84e4e76 226
feb76eaf 227 return ${EXIT_ERROR}
e84e4e76
MT
228}
229
1c6a4e30 230stp_bridge_is_root() {
feb76eaf 231 local bridge=${1}
feb76eaf 232 assert isset bridge
e84e4e76 233
48bc31eb 234 local root_path_cost=$(stp_bridge_get_root_path_cost ${bridge})
e84e4e76 235
48bc31eb
MT
236 if [ "${root_path_cost}" = "0" ]; then
237 return ${EXIT_TRUE}
238 fi
239
240 return ${EXIT_FALSE}
e84e4e76
MT
241}
242
1c6a4e30 243stp_bridge_get_topology_change_count() {
feb76eaf 244 local bridge=${1}
feb76eaf 245 assert isset bridge
e84e4e76 246
4e0bc093 247 __device_get_file ${bridge} bridge/topology_change
e84e4e76
MT
248}
249
1c6a4e30 250stp_bridge_get_topology_change_timer() {
feb76eaf 251 local bridge=${1}
feb76eaf
MT
252 assert isset bridge
253
4e0bc093 254 __device_get_file ${bridge} bridge/topology_change_timer
e84e4e76
MT
255}
256
1c6a4e30 257stp_bridge_get_topology_change_detected() {
feb76eaf 258 local bridge=${1}
feb76eaf 259 assert isset bridge
e84e4e76 260
48bc31eb 261 local change
8d4f9311 262
4e0bc093 263 change=$(__device_get_file ${bridge} bridge/topology_change_detected)
48bc31eb
MT
264
265 if enabled change; then
266 print "yes"
267 return ${EXIT_TRUE}
268 else
269 print "no"
270 return ${EXIT_FALSE}
271 fi
6b3f9c85
MT
272}
273
1c6a4e30 274stp_port_get_state() {
6b3f9c85 275 local bridge=${1}
6b3f9c85 276 assert isset bridge
48bc31eb
MT
277
278 local port=${2}
feb76eaf 279 assert isset port
6b3f9c85 280
4e0bc093
MT
281 local state=$(__device_get_file ${bridge} brif/${port}/state)
282
283 case "${state}" in
284 0)
285 print "DISABLED"
286 ;;
287 1)
288 print "LISTENING"
289 ;;
290 2)
291 print "LEARNING"
292 ;;
293 3)
294 print "FORWARDING"
295 ;;
296 4)
297 print "BLOCKING"
298 ;;
299 *)
300 return ${EXIT_ERROR}
301 ;;
302 esac
feb76eaf 303
48bc31eb 304 return ${EXIT_OK}
feb76eaf
MT
305}
306
1c6a4e30 307stp_port_get_id() {
feb76eaf 308 local bridge=${1}
feb76eaf 309 assert isset bridge
feb76eaf 310
48bc31eb
MT
311 local port=${2}
312 assert isset port
8d4f9311 313
48bc31eb 314 dec $(__device_get_file ${bridge} "brif/${port}/port_no")
8d4f9311 315 return ${EXIT_OK}
feb76eaf
MT
316}
317
1c6a4e30 318stp_port_get_cost() {
feb76eaf 319 local bridge=${1}
feb76eaf 320 assert isset bridge
48bc31eb
MT
321
322 local port=${2}
feb76eaf
MT
323 assert isset port
324
4e0bc093 325 __device_get_file ${bridge} brif/${port}/path_cost
6b3f9c85
MT
326}
327
1c6a4e30 328stp_port_set_cost() {
2c083d57
MT
329 assert [ $# -eq 3 ]
330
331 local bridge="${1}"
332 local port="${2}"
333 local cost="${3}"
334
335 local old_cost="$(stp_port_get_cost "${bridge}" "${port}")"
2227b926 336 if [ "${cost}" = "${old_cost}" ]; then
2c083d57
MT
337 return ${EXIT_OK}
338 fi
339
340 log DEBUG "Setting STP path costs of port '${port}' (bridge '${bridge}') to '${cost}'"
341
4e0bc093 342 __device_set_file "${bridge}" "brif/${port}/path_cost" "${cost}"
2c083d57
MT
343}
344
1c6a4e30 345stp_port_get_designated_root() {
6b3f9c85 346 local bridge=${1}
6b3f9c85 347 assert isset bridge
48bc31eb
MT
348
349 local port=${2}
feb76eaf 350 assert isset port
6b3f9c85 351
4e0bc093 352 local output=$(__device_get_file ${bridge} brif/${port}/designated_root)
feb76eaf 353
48bc31eb 354 if isset output; then
4e0bc093 355 mac_format ${output:5}
36e3fd2f
MT
356 return ${EXIT_OK}
357 fi
feb76eaf
MT
358
359 return ${EXIT_ERROR}
6b3f9c85 360}
48bc31eb 361
1c6a4e30 362__stp_div_100() {
48bc31eb
MT
363 local val=${1}
364
365 local split=$((${#val} - 2))
366 val="${val:0:${split}}.${val:${split}:2}"
367
368 # Round the output.
369 print "%.0f" "${val}"
370}