]>
Commit | Line | Data |
---|---|---|
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 | ||
6b3f9c85 MT |
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 | ||
e84e4e76 MT |
31 | # XXX Very slow thing, caching? |
32 | function __rstpctl_cmd() { | |
33 | local command=$@ | |
34 | ||
35 | local line | |
36 | local key | |
37 | local val | |
38 | ||
39 | rstpctl ${command} | \ | |
40 | sed -e "s/\t\t\t/\n/g" \ | |
41 | -e "s/^ //g" \ | |
42 | -e "s/\t\s*/___/g" | \ | |
43 | while read line; do | |
44 | [ "${line}" = "${line/___/_}" ] && continue | |
45 | ||
46 | key=${line%%___*} | |
47 | key=${key// /_} | |
48 | key=${key^^} | |
49 | ||
50 | val=${line#*___} | |
51 | ||
52 | echo "${key}=\"${val}\"" | |
53 | done | |
54 | } | |
55 | ||
56 | function __rstpctl_showbridge_get() { | |
57 | local bridge=${1} | |
58 | local param=${2^^} | |
59 | ||
60 | local line | |
61 | for line in $(__rstpctl_cmd showbridge ${bridge}); do | |
62 | if [ "${line%%=*}" = "${param}" ]; then | |
63 | line="${line##*=}" | |
64 | echo "${line//\"/}" | |
65 | return ${EXIT_OK} | |
66 | fi | |
67 | done | |
68 | ||
69 | return ${EXIT_ERROR} | |
70 | } | |
71 | ||
72 | function __rstpctl_showportdetail_get() { | |
73 | local bridge=${1} | |
74 | local port=${2} | |
75 | local param=${3^^} | |
76 | ||
77 | local line | |
78 | for line in $(__rstpctl_cmd showportdetail ${bridge} ${port}); do | |
79 | if [ "${line%%=*}" = "${param}" ]; then | |
80 | line="${line##*=}" | |
81 | echo "${line//\"/}" | |
82 | return ${EXIT_OK} | |
83 | fi | |
84 | done | |
85 | ||
86 | return ${EXIT_ERROR} | |
87 | } | |
88 | ||
89 | function __rstp_port_enabled() { | |
90 | local bridge=${1} | |
91 | local port=${2} | |
92 | ||
93 | local status=$(__rstpctl_showportdetail_get ${bridge} ${port} enabled) | |
94 | ||
95 | if [ "${status}" = "yes" ]; then | |
96 | return ${EXIT_OK} | |
97 | fi | |
98 | ||
99 | return ${EXIT_ERROR} | |
100 | } | |
101 | ||
102 | function __rstp_port_state() { | |
103 | local bridge=${1} | |
104 | local port=${2} | |
105 | ||
106 | local output=$(__rstpctl_showportdetail_get ${bridge} ${port} state) | |
107 | echo "${output^^}" | |
108 | } | |
109 | ||
110 | function __rstp_port_pathcost() { | |
111 | local bridge=${1} | |
112 | local port=${2} | |
113 | ||
114 | __rstpctl_showportdetail_get ${bridge} ${port} path_cost | |
115 | } | |
116 | ||
117 | function __rstp_port_designated_root() { | |
118 | local bridge=${1} | |
119 | local port=${2} | |
120 | ||
121 | __rstpctl_showportdetail_get ${bridge} ${port} designated_root | |
122 | } | |
123 | ||
124 | function __rstp_port_designated_bridge() { | |
125 | local bridge=${1} | |
126 | local port=${2} | |
127 | ||
128 | __rstpctl_showportdetail_get ${bridge} ${port} designated_bridge | |
129 | } | |
130 | ||
131 | function __rstp_topology_change() { | |
132 | local bridge=${1} | |
133 | ||
134 | local state=$(__rstpctl_showbridge_get ${bridge} topology_change) | |
135 | ||
136 | case "${state}" in | |
137 | yes) | |
138 | echo "${state}" | |
139 | return ${EXIT_OK} | |
140 | ;; | |
141 | no) | |
142 | echo "${state}" | |
143 | return ${EXIT_ERROR} | |
144 | ;; | |
145 | esac | |
146 | } | |
147 | ||
148 | function __rstp_topology_change_count() { | |
149 | local bridge=${1} | |
150 | ||
151 | # XXX typo in rstpctl -> toplogy | |
152 | __rstpctl_showbridge_get ${bridge} toplogy_change_count | |
153 | } | |
154 | ||
155 | function __rstp_topology_change_time() { | |
156 | local bridge=${1} | |
157 | ||
158 | __rstpctl_showbridge_get ${bridge} time_since_topology_change | |
159 | } | |
160 | ||
161 | function __rstp_bridge_id() { | |
162 | local bridge=${1} | |
163 | ||
164 | local id=$(__rstpctl_showbridge_get ${bridge} bridge_id) | |
165 | id=${id:5:12} | |
166 | ||
167 | mac_format "${id}" | |
168 | } | |
169 | ||
170 | function __rstp_designated_root() { | |
171 | local bridge=${1} | |
172 | ||
173 | local root=$(__rstpctl_showbridge_get ${bridge} designated_root) | |
174 | root=${root:5:12} | |
175 | ||
176 | mac_format "${root}" | |
177 | } | |
178 | ||
179 | function __rstp_pathcost() { | |
180 | local bridge=${1} | |
181 | ||
182 | __rstpctl_showbridge_get ${bridge} path_cost | |
183 | } | |
184 | ||
185 | function __stp_port_enabled() { | |
186 | : # XXX TBD | |
187 | } | |
188 | ||
189 | function __stp_port_state() { | |
190 | : # XXX TBD | |
191 | } | |
192 | ||
193 | function __stp_port_pathcost() { | |
194 | : # XXX TBD | |
195 | } | |
196 | ||
197 | function __stp_port_designated_root() { | |
198 | : # XXX TBD | |
199 | } | |
200 | ||
201 | function __stp_port_designated_bridge() { | |
202 | : # XXX TBD | |
203 | } | |
204 | ||
205 | function stp_port_enabled() { | |
206 | __stp_wrapper port_enabled $@ | |
207 | } | |
208 | ||
209 | function stp_port_state() { | |
210 | __stp_wrapper port_state $@ | |
211 | } | |
212 | ||
213 | function stp_port_pathcost() { | |
214 | __stp_wrapper port_pathcost $@ | |
215 | } | |
216 | ||
217 | function stp_port_designated_root() { | |
218 | local root=$(__stp_wrapper port_designated_root $@) | |
219 | ||
220 | # Cut prefix 8000. and format mac | |
221 | root="${root:5:12}" | |
222 | mac_format "${root}" | |
223 | } | |
224 | ||
225 | function stp_port_designated_bridge() { | |
226 | __stp_wrapper port_designated_bridge $@ | |
227 | } | |
228 | ||
229 | function stp_topology_change() { | |
230 | __stp_wrapper topology_change $@ | |
231 | } | |
232 | ||
233 | function stp_topology_change_count() { | |
234 | __stp_wrapper topology_change_count $@ | |
235 | } | |
236 | ||
237 | function stp_topology_change_time() { | |
238 | __stp_wrapper topology_change_time $@ | |
239 | } | |
240 | ||
241 | function stp_bridge_id() { | |
242 | __stp_wrapper bridge_id $@ | |
243 | } | |
244 | ||
245 | function stp_designated_root() { | |
246 | __stp_wrapper designated_root $@ | |
247 | } | |
248 | ||
249 | function stp_pathcost() { | |
250 | __stp_wrapper pathcost $@ | |
251 | } | |
252 | ||
253 | function __stp_wrapper() { | |
254 | local func=${1} | |
255 | shift | |
256 | ||
257 | # XXX we will detect what kind of protocol the | |
258 | # bridge is running and process the correct funtions | |
259 | local proto_version="rstp" | |
260 | ||
261 | __${proto_version}_${func} $@ | |
262 | } | |
6b3f9c85 MT |
263 | |
264 | function stp_mode() { | |
265 | : # XXX wanted | |
266 | } | |
267 | ||
268 | function stp_enable() { | |
269 | local bridge=${1} | |
270 | ||
271 | assert isset bridge | |
272 | assert zone_exists ${bridge} | |
273 | ||
274 | brctl stp ${bridge} on | |
275 | ||
276 | local mode=$(zone_config_get ${bridge} STP_MODE) | |
277 | ||
278 | case "${mode}" in | |
279 | stp) | |
280 | rstpctl setforcevers ${bridge} slow | |
281 | ;; | |
282 | rstp) | |
283 | rstpctl setforcevers ${bridge} normal | |
284 | ;; | |
285 | *) | |
286 | error_log "Unknown protocol version: ${mode}." | |
287 | ;; | |
288 | esac | |
289 | } | |
290 | ||
291 | function stp_disable() { | |
292 | local bridge=${1} | |
293 | ||
294 | assert isset bridge | |
295 | assert zone_exists ${bridge} | |
296 | ||
297 | brctl stp ${bridge} off | |
298 | } |