]> git.ipfire.org Git - people/ms/network.git/blob - functions.stp
Add support to set the hostname.
[people/ms/network.git] / functions.stp
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
22 function stp_init() {
23 module_load stp
24
25 assert binary_exists brctl
26 assert binary_exists rstpctl
27 }
28
29 init_register stp_init
30
31 function __rstpctl_bridge_get() {
32 local bridge=${1}
33 local param=${2}
34
35 assert isset bridge
36 assert isset param
37
38 local key
39 local val
40 rstpctl dumpbridge ${bridge} | \
41 while read bridge key val; do
42 if [ "${key}" = "${param}" ]; then
43 echo "${val}"
44 return ${EXIT_OK}
45 fi
46 done
47
48 return ${EXIT_ERROR}
49 }
50
51 function __rstpctl_port_get() {
52 local bridge=${1}
53 local port=${2}
54 local param=${3}
55
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}"
66 return ${EXIT_OK}
67 fi
68 done
69
70 return ${EXIT_ERROR}
71 }
72
73 function stp_enable() {
74 local bridge=${1}
75
76 assert isset bridge
77 assert zone_exists ${bridge}
78
79 brctl stp ${bridge} on
80
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
94 }
95
96 function stp_disable() {
97 local bridge=${1}
98
99 assert isset bridge
100 assert zone_exists ${bridge}
101
102 brctl stp ${bridge} off
103 }
104
105 function stp_bridge_get_protocol() {
106 local bridge=${1}
107
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
120 }
121
122 function stp_bridge_set_protocol() {
123 : XXX WANTED
124 }
125
126 function stp_bridge_get_id() {
127 local bridge=${1}
128
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}
143 }
144
145 function stp_bridge_get_forward_delay() {
146 local bridge=${1}
147
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}
162 }
163
164 function stp_bridge_get_hello_time() {
165 local bridge=${1}
166
167 assert isset bridge
168
169 case "$(stp_bridge_get_protocol ${bridge})" in
170 rstp)
171 __rstpctl_bridge_get ${bridge} "bridge_hello_time"
172 return ${EXIT_OK}
173 ;;
174 stp)
175 __device_get_file ${bridge} "bridge/hello_time"
176 return ${EXIT_OK}
177 ;;
178 esac
179
180 return ${EXIT_ERROR}
181 }
182
183 function stp_bridge_get_max_age() {
184 local bridge=${1}
185
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}
200 }
201
202 function stp_bridge_get_designated_root() {
203 local bridge=${1}
204 local output
205
206 assert isset bridge
207
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}
224 }
225
226 function stp_bridge_get_root_path_cost() {
227 local bridge=${1}
228
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
241
242 return ${EXIT_ERROR}
243 }
244
245 function stp_bridge_get_root_port_id() {
246 local bridge=${1}
247
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
260
261 return ${EXIT_ERROR}
262 }
263
264 function stp_bridge_get_root_port() {
265 local bridge=${1}
266
267 assert isset bridge
268
269 local id=$(stp_bridge_get_root_port_id ${bridge})
270
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})
275
276 if [ "${id}" = "${member_id}" ]; then
277 echo "${member}"
278 return ${EXIT_OK}
279 fi
280 done
281
282 return ${EXIT_ERROR}
283 }
284
285 function stp_bridge_is_root() {
286 local bridge=${1}
287
288 assert isset bridge
289
290 [ -n "$(stp_bridge_get_root_port ${bridge})" ]
291 }
292
293 function stp_bridge_get_priority() {
294 local bridge=${1}
295
296 assert isset bridge
297
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
309
310 return ${EXIT_ERROR}
311 }
312
313 function stp_bridge_get_topology_change_count() {
314 local bridge=${1}
315
316 assert isset bridge
317
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
328
329 return ${EXIT_ERROR}
330 }
331
332 function 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
347
348 return ${EXIT_ERROR}
349 }
350
351 function stp_bridge_get_topology_change_detected() {
352 local bridge=${1}
353
354 assert isset bridge
355
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
366
367 return ${EXIT_ERROR}
368 }
369
370 # STP states
371 STP_STATE[0]="disabled"
372 STP_STATE[1]="listening"
373 STP_STATE[2]="learning"
374 STP_STATE[3]="forwarding"
375 STP_STATE[4]="blocking"
376
377 function stp_port_get_state() {
378 local bridge=${1}
379 local port=${2}
380 local output
381
382 assert isset bridge
383 assert isset port
384
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")
391
392 # Translate int to name
393 output="${STP_STATE[${output}]}"
394 ;;
395 esac
396
397 if ! isset output; then
398 return ${EXIT_ERROR}
399 fi
400
401 echo "${output^^}"
402
403 return ${EXIT_OK}
404 }
405
406 function 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 ;;
418 stp)
419 dec $(__device_get_file ${bridge} "brif/${port}/port_no")
420 return ${EXIT_OK}
421 ;;
422 esac
423
424 return ${EXIT_ERROR}
425 }
426
427 function 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
435 rstp)
436 __rstpctl_port_get ${bridge} ${port} "path_cost"
437 return ${EXIT_OK}
438 ;;
439 stp)
440 __device_get_file ${bridge} "brif/${port}/path_cost"
441 return ${EXIT_OK}
442 ;;
443 esac
444
445 return ${EXIT_ERROR}
446 }
447
448 function stp_port_get_designated_root() {
449 local bridge=${1}
450 local port=${2}
451 local output
452
453 assert isset bridge
454 assert isset port
455
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}
468 }