Project:
IPFire
Code Location:
git://git.ipfire.org/network.gitmaster
/
functions.stp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2010 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################### # The default mode. # We default to RSTP, because it has the better user experience and # faster convergence times. Despite of that, it completely downgradeable # to plain STP. STP_DEFAULT_MODE="rstp" # Allowed modes of the spanning tree protocol. STP_ALLOWED_MODES="rstp stp" function stp_enable() { local bridge=${1} assert isset bridge # Tell the kernel to enable STP. print 1 > ${SYS_CLASS_NET}/${bridge}/bridge/stp_state } function stp_disable() { local bridge=${1} assert isset bridge # Tell the kernel to disable STP. print 0 > ${SYS_CLASS_NET}/${bridge}/bridge/stp_state } function stp_is_enabled() { local bridge=${1} assert isset bridge local state=$(__device_get_file ${bridge} bridge/stp_state) case "${state}" in 0) return ${EXIT_FALSE} ;; *) return ${EXIT_TRUE} ;; esac } function stp_is_userspace() { local bridge=${1} assert isset bridge local state=$(__device_get_file ${bridge} bridge/stp_state) case "${state}" in 2) return ${EXIT_TRUE} ;; *) return ${EXIT_FALSE} ;; esac } function stp_get_name() { local proto=${1} case "${proto}" in stp) echo "Spanning Tree Protocol" ;; rstp) echo "Rapid Spanning Tree Protocol" ;; mstp) echo "Multiple Spanning Tree Protocol" ;; esac return ${EXIT_OK} } function stp_bridge_set_protocol() { local bridge=${1} assert isset bridge local mode=${2} assert isset mode if ! listmatch ${mode} ${STP_ALLOWED_MODES}; then log WARNING "Unknown protocol version: ${mode}." log WARNING "Using default mode." mode="${STP_DEFAULT_MODE}" fi cmd mstpctl setforcevers ${bridge} ${mode} assert [ $? -eq 0 ] } function stp_bridge_get_protocol() { local bridge=${1} assert isset bridge # Let's check what the kernel is telling us about it's STP state. local state=$(__device_get_file ${bridge} "bridge/stp_state") case "${state}" in 0) # STP is disabled. return ${EXIT_OK} ;; 1) # Kernel mode STP is running. echo "stp" return ${EXIT_OK} ;; 2) # User-space STP is running. ;; *) log ERROR "Kernel is running in an unknown STP state." return ${EXIT_ERROR} ;; esac # We get here, when STP is running in user-space mode. # Get the current protocol version. mstpctl showbridge ${bridge} force-protocol-version 2>/dev/null return ${EXIT_OK} } function stp_bridge_get_id() { local bridge=${1} assert isset bridge __device_get_file ${bridge} "bridge/bridge_id" return $? } function stp_bridge_get_forward_delay() { local bridge=${1} assert isset bridge if stp_is_userspace ${bridge}; then cmd mstpctl showbridge ${bridge} forward-delay else local output=$(__device_get_file ${bridge} bridge/forward_delay) __stp_div_100 ${output} fi return ${EXIT_OK} } function stp_bridge_set_forward_delay() { local bridge=${1} assert isset bridge local fdelay=${2} assert isinteger fdelay # Check if the setting we want is already set. local current_fdelay=$(stp_bridge_get_forward_delay ${bridge}) [ ${fdelay} -eq ${current_fdelay} ] && return ${EXIT_OK} # The smallest value that may be set is 2. if [ ${fdelay} -lt 2 ]; then fdelay=2 fi # Set the new value. log INFO "Changing forward delay for '${bridge}': ${current_fdelay} --> ${fdelay}" print "$(( ${fdelay} * 100 ))" > ${SYS_CLASS_NET}/${bridge}/bridge/forward_delay return ${EXIT_OK} } function stp_bridge_get_hello_time() { local bridge=${1} assert isset bridge local ht=$(__device_get_file ${bridge} bridge/hello_time) __stp_div_100 ${ht} return ${EXIT_OK} } function stp_bridge_set_hello_time() { local bridge=${1} assert isset bridge local hello=${2} assert isinteger hello # Check if the setting we want is already set. local current_hello=$(stp_bridge_get_hello_time ${bridge}) [ ${hello} -eq ${current_hello} ] && return ${EXIT_OK} # Set the new value. log INFO "Changing hello time for '${bridge}': ${current_hello} --> ${hello}" print "$(( ${hello} * 100 ))" > ${SYS_CLASS_NET}/${bridge}/bridge/hellow_time return ${EXIT_OK} } function stp_bridge_get_max_age() { local bridge=${1} assert isset bridge local maxage=$(__device_get_file ${bridge} "bridge/max_age") __stp_div_100 ${maxage} return ${EXIT_OK} } function stp_bridge_set_max_age() { local bridge=${1} assert isset bridge local maxage=${2} assert isinteger maxage # Check if the setting we want is already set. local current_maxage=$(stp_bridge_get_max_age ${bridge}) [ ${maxage} -eq ${current_maxage} ] && return ${EXIT_OK} # Set the new value. log INFO "Changing max age for '${bridge}': ${current_maxage} --> ${maxage}" print "$(( ${maxage} * 100 ))" > ${SYS_CLASS_NET}/${bridge}/bridge/max_age return ${EXIT_OK} } function stp_bridge_get_priority() { local bridge=${1} assert isset bridge __device_get_file ${bridge} "bridge/priority" return ${EXIT_OK} } function stp_bridge_set_priority() { local bridge=${1} assert isset bridge local priority=${2} assert isinteger priority # Check if the setting we want is already set. local current_priority=$(stp_bridge_get_priority ${bridge}) [ ${priority} -eq ${current_priority} ] && return ${EXIT_OK} # Set the new value. log INFO "Changing priority for '${bridge}': ${current_priority} --> ${priority}" print "${priority}" > ${SYS_CLASS_NET}/${bridge}/bridge/priority return ${EXIT_OK} } function stp_bridge_get_designated_root() { local bridge=${1} assert isset bridge local output if stp_is_userspace ${bridge}; then output=$(cmd mstpctl showbridge ${bridge} designated-root) else output=$(__device_get_file ${bridge} bridge/root_id) fi output=${output:6} # Print output (lowercase). print "${output,,}" if isset output; then return ${EXIT_OK} else return ${EXIT_ERROR} fi } function stp_bridge_get_root_path_cost() { local bridge=${1} assert isset bridge if stp_is_userspace ${bridge}; then cmd mstpctl showbridge ${bridge} path-cost else __device_get_file ${bridge} bridge/root_path_cost fi return ${EXIT_OK} } function stp_bridge_get_root_port_id() { local bridge=${1} assert isset bridge if stp_is_userspace ${bridge}; then local root_port=$(cmd mstpctl showbridge ${bridge} root-port) # Return error, when there is no root port. if [ "${root_port}" = "none" ]; then return ${EXIT_ERROR} fi print "${root_port}" else __device_get_file ${bridge} bridge/root_port_id fi return ${EXIT_OK} } function stp_bridge_get_root_port() { local bridge=${1} assert isset bridge local id=$(stp_bridge_get_root_port_id ${bridge}) local member member_id for member in $(bridge_get_members ${bridge}); do member_id=$(stp_port_get_id ${bridge} ${member}) if [ "${id}" = "${member_id}" ]; then print "${member}" return ${EXIT_OK} fi done return ${EXIT_ERROR} } function stp_bridge_is_root() { local bridge=${1} assert isset bridge local root_path_cost=$(stp_bridge_get_root_path_cost ${bridge}) if [ "${root_path_cost}" = "0" ]; then return ${EXIT_TRUE} fi return ${EXIT_FALSE} } function stp_bridge_get_topology_change_count() { local bridge=${1} assert isset bridge if stp_is_userspace ${bridge}; then cmd mstpctl showbridge ${bridge} topology-change-count else __device_get_file ${bridge} bridge/topology_change fi return ${EXIT_OK} } function stp_bridge_get_topology_change_timer() { local bridge=${1} assert isset bridge if stp_is_userspace ${bridge}; then cmd mstpctl showbridge ${bridge} time-since-topology-change else __device_get_file ${bridge} bridge/topology_change_timer fi return ${EXIT_OK} } function stp_bridge_get_topology_change_detected() { local bridge=${1} assert isset bridge local change if stp_is_userspace ${bridge}; then change=$(mstpctl showbridge ${bridge} topology-change) else change=$(__device_get_file ${bridge} bridge/topology_change_detected) fi if enabled change; then print "yes" return ${EXIT_TRUE} else print "no" return ${EXIT_FALSE} fi } function stp_port_get_state() { local bridge=${1} assert isset bridge local port=${2} assert isset port local space if stp_is_userspace ${bridge}; then state=$(mstpctl showportdetail ${bridge} ${port} state) print "${state^^}" else state=$(__device_get_file ${bridge} brif/${port}/state) case "${state}" in 0) print "DISABLED" ;; 1) print "LISTENING" ;; 2) print "LEARNING" ;; 3) print "FORWARDING" ;; 4) print "BLOCKING" ;; *) return ${EXIT_ERROR} ;; esac fi return ${EXIT_OK} } function stp_port_get_id() { local bridge=${1} assert isset bridge local port=${2} assert isset port dec $(__device_get_file ${bridge} "brif/${port}/port_no") return ${EXIT_OK} } function stp_port_get_cost() { local bridge=${1} assert isset bridge local port=${2} assert isset port if stp_is_userspace ${bridge}; then cmd mstpctl showportdetail ${bridge} ${port} external-port-cost else __device_get_file ${bridge} brif/${port}/path_cost fi return ${EXIT_ERROR} } function stp_port_get_designated_root() { local bridge=${1} assert isset bridge local port=${2} assert isset port local output if stp_is_userspace ${bridge}; then output=$(cmd mstpctl showportdetail ${bridge} ${port} designated-root) output=${output:6} else output=$(__device_get_file ${bridge} brif/${port}/designated_root) output=${output:5} fi if isset output; then mac_format ${output} return ${EXIT_OK} fi return ${EXIT_ERROR} } function __stp_div_100() { local val=${1} local split=$((${#val} - 2)) val="${val:0:${split}}.${val:${split}:2}" # Round the output. print "%.0f" "${val}" }
