]> git.ipfire.org Git - people/ms/network.git/blob - src/hooks/zones/bridge
bridge: Order arguments in alphabetical order
[people/ms/network.git] / src / hooks / zones / bridge
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 . /usr/lib/network/header-zone
23
24 HOOK_MANPAGE="network-zone-bridge"
25
26 HOOK_SETTINGS="HOOK ADDRESS STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE"
27 HOOK_SETTINGS="${HOOK_SETTINGS} STP_PRIORITY MTU"
28
29 HOOK_PORT_SETTINGS="COST PRIORITY"
30
31 # Default values
32 DEFAULT_STP_FORWARD_DELAY=0
33 DEFAULT_STP_HELLO=2
34 DEFAULT_STP_MAXAGE=20
35 DEFAULT_STP_PRIORITY=512
36
37 hook_check_settings() {
38 assert ismac ADDRESS
39 assert isset MTU && assert mtu_is_valid "ethernet" "${MTU}"
40
41 # Spanning Tree Protocol
42 assert isbool STP
43 assert isinteger STP_HELLO
44 assert isinteger STP_FORWARD_DELAY
45 assert isinteger STP_PRIORITY
46 }
47
48 hook_parse_cmdline() {
49 while [ $# -gt 0 ]; do
50 case "${1}" in
51 --address=*)
52 ADDRESS="$(cli_get_val "${1}")"
53
54 if ! mac_is_valid "${ADDRESS}"; then
55 error "Invalid MAC address: ${ADDRESS}"
56 return ${EXIT_ERROR}
57 fi
58 ;;
59
60 --mtu=*)
61 MTU="$(cli_get_val "${1}")"
62
63 if ! mtu_is_valid "ethernet" "${MTU}"; then
64 error "Invalid MTU: ${MTU}"
65 return ${EXIT_ERROR}
66 fi
67 ;;
68
69 --stp=*)
70 STP="$(cli_get_val "${1}")"
71
72 if enabled STP; then
73 STP="on"
74 elif disabled STP; then
75 STP="off"
76 else
77 error "Invalid value for STP: ${STP}"
78 return ${EXIT_ERROR}
79 fi
80 ;;
81
82 --stp-forward-delay=*)
83 STP_FORWARD_DELAY="$(cli_get_val "${1}")"
84
85 if ! isinteger STP_FORWARD_DELAY; then
86 error "Invalid STP forwarding delay: ${STP_FORWARD_DELAY}"
87 return ${EXIT_ERROR}
88 fi
89 ;;
90
91 --stp-hello=*)
92 STP_HELLO="$(cli_get_val "${1}")"
93
94 if ! isinteger STP_HELLO; then
95 error "Invalid STP hello time: ${STP_HELLO}"
96 return ${EXIT_ERROR}
97 fi
98 ;;
99
100 --stp-priority=*)
101 STP_PRIORITY="$(cli_get_val "${1}")"
102
103 if ! isinteger STP_PRIORITY; then
104 error "Invalid STP priority: ${STP_PRIORITY}"
105 return ${EXIT_ERROR}
106 fi
107 ;;
108
109 *)
110 error "Unknown argument: ${1}"
111 return ${EXIT_ERROR}
112 ;;
113 esac
114 shift
115 done
116
117 # Generate a random MAC address if the user passed no one
118 if ! isset ADDRESS; then
119 ADDRESS="$(mac_generate)"
120 fi
121
122 # Enable Spanning Tree Protocol by default
123 if ! isset STP; then
124 STP="on"
125 fi
126
127 # Set all other defaults
128 hook_set_defaults
129
130 return ${EXIT_OK}
131 }
132
133 hook_up() {
134 local zone=${1}
135 assert isset zone
136
137 zone_settings_read "${zone}"
138
139 # Create the bridge if it does not already exist.
140 if ! device_exists "${zone}"; then
141 bridge_create "${zone}" \
142 --address="${ADDRESS}" \
143 --mtu="${MTU}"
144 fi
145
146 # Enable STP
147 if enabled STP; then
148 stp_enable "${zone}"
149
150 if isset STP_FORWARD_DELAY; then
151 stp_bridge_set_forward_delay "${zone}" "${STP_FORWARD_DELAY}"
152 fi
153
154 if isset STP_HELLO; then
155 stp_bridge_set_hello_time "${zone}" "${STP_HELLO}"
156 fi
157
158 if isset STP_MAXAGE; then
159 stp_bridge_set_max_age "${zone}" "${STP_MAXAGE}"
160 fi
161
162 if isset STP_PRIORITY; then
163 stp_bridge_set_priority "${zone}" "${STP_PRIORITY}"
164 fi
165 else
166 stp_disable "${zone}"
167 fi
168
169 device_set_up "${zone}"
170
171 # XXX Currently, there is a bug (in the linux kernel?) that we need to
172 # set our bridges to promisc mode.
173 device_set_promisc "${zone}" on
174
175 # Bring up all configurations
176 zone_configs_up "${zone}"
177
178 exit ${EXIT_OK}
179 }
180
181 hook_down() {
182 local zone="${1}"
183 assert isset zone
184
185 if ! device_is_up "${zone}"; then
186 warning "Zone '${zone}' is not up"
187 exit ${EXIT_OK}
188 fi
189
190 # Stop all the configs.
191 zone_configs_down "${zone}"
192
193 # Bring down all the ports.
194 zone_ports_down "${zone}"
195 zone_ports_remove "${zone}"
196
197 # Remove the bridge.
198 device_set_down "${zone}"
199 bridge_delete "${zone}"
200
201 exit ${EXIT_OK}
202 }
203
204 hook_status() {
205 local zone="${1}"
206 assert isset zone
207
208 # Print the default header.
209 cli_device_headline "${zone}"
210
211 # Exit if zone is down
212 if ! zone_is_up "${zone}"; then
213 echo # Empty line
214 exit ${EXIT_ERROR}
215 fi
216
217 cli_headline 2 "Spanning Tree Protocol information"
218 if stp_is_enabled "${zone}"; then
219 cli_print_fmt1 2 "ID" "$(stp_bridge_get_id ${zone})"
220 cli_print_fmt1 2 "Priority" "$(stp_bridge_get_priority ${zone})"
221
222 if stp_bridge_is_root ${zone}; then
223 cli_print 2 "This bridge is root."
224 else
225 cli_print_fmt1 2 "Designated root" \
226 "$(stp_bridge_get_designated_root ${zone})"
227 cli_print_fmt1 2 "Root path cost" \
228 "$(stp_bridge_get_root_path_cost ${zone})"
229 fi
230 cli_space
231
232 # Topology information
233 cli_print_fmt1 2 "Topology changing" \
234 "$(stp_bridge_get_topology_change_detected ${zone})"
235 cli_print_fmt1 2 "Topology change time" \
236 "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))"
237 cli_print_fmt1 2 "Topology change count" \
238 "$(stp_bridge_get_topology_change_count ${zone})"
239 cli_space
240 else
241 cli_print 2 "Disabled"
242 cli_space
243 fi
244
245 cli_headline 2 "Ports"
246 zone_ports_status "${zone}"
247 cli_space
248
249 cli_headline 2 "Configurations"
250 zone_configs_cmd status "${zone}"
251 cli_space
252
253 exit ${EXIT_OK}
254 }
255
256 hook_hotplug() {
257 local zone="${1}"
258 assert isset zone
259
260 case "$(hotplug_action)" in
261 add)
262 # Attach all ports when zone is coming up
263 if hotplug_event_interface_is_zone "${zone}"; then
264 # Bring up all ports
265 local port
266 for port in $(zone_get_ports "${zone}"); do
267 log DEBUG "Trying to attach port ${port} to ${zone}"
268
269 hook_port_up "${zone}" "${port}"
270 done
271
272 # Handle ports of this zone that have just been added
273 elif hotplug_event_interface_is_port_of_zone "${zone}"; then
274 # Attach the device if the parent bridge is up
275 if zone_is_active "${zone}"; then
276 hook_port_up "${zone}" "${INTERFACE}"
277 fi
278 fi
279 ;;
280
281 remove)
282 if hotplug_event_interface_is_zone "${zone}"; then
283 # Bring down/destroy all ports
284 local port
285 for port in $(zone_get_ports "${zone}"); do
286 log DEBUG "Trying to detach port ${port} from ${zone}"
287
288 hook_port_down "${zone}" "${port}"
289 done
290
291 # Handle ports of this zone that have just been removed
292 elif hotplug_event_interface_is_port_of_zone "${zone}"; then
293 hook_port_down "${zone}" "${INTERFACE}"
294 fi
295 ;;
296
297 *)
298 exit ${EXIT_NOT_HANDLED}
299 ;;
300 esac
301
302 exit ${EXIT_OK}
303 }
304
305 hook_check_port_settings() {
306 if isset COST; then
307 assert isinteger COST
308 fi
309
310 if isset PRIORITY; then
311 assert isinteger PRIORITY
312 fi
313 }
314
315 hook_port_attach() {
316 # Excepting at least two arguments here
317 assert [ $# -ge 2 ]
318
319 local zone="${1}"
320 local port="${2}"
321 shift 2
322
323 if zone_has_port "${zone}" "${port}"; then
324 zone_port_settings_read "${zone}" "${port}"
325 fi
326
327 local arg
328 local val
329 while read arg; do
330 case "${arg}" in
331 --cost=*)
332 COST="$(cli_get_val "${arg}")"
333 ;;
334 --priority=*)
335 PRIORITY="$(cli_get_val "${arg}")"
336 ;;
337 esac
338 done <<< "$(args "$@")"
339
340 if ! zone_port_settings_write "${zone}" "${port}"; then
341 exit ${EXIT_ERROR}
342 fi
343
344 exit ${EXIT_OK}
345 }
346
347 hook_port_detach() {
348 assert [ $# -eq 2 ]
349
350 local zone="${1}"
351 local port="${2}"
352
353 # Shut down the port (if possible)
354 port_down "${port}"
355
356 if ! zone_port_settings_remove "${zone}" "${port}"; then
357 exit ${EXIT_ERROR}
358 fi
359
360 exit ${EXIT_OK}
361 }
362
363 hook_port_edit() {
364 hook_port_attach "$@"
365 }
366
367 hook_port_up() {
368 assert [ $# -eq 2 ]
369
370 local zone="${1}"
371 local port="${2}"
372
373 # Try bringing up the port if it has not been
374 # brought up before.
375 # We will get here as soon as the port device has
376 # been created and will then connect it with the bridge.
377 if ! device_exists "${port}"; then
378 port_create "${port}"
379
380 return ${EXIT_OK}
381 fi
382
383 # Read configuration values
384 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
385
386 # Make sure that the port is up
387 port_up "${port}"
388
389 # Attach the port to the bridge
390 bridge_attach_device "${zone}" "${port}"
391
392 # Set STP configuration
393 if isset COST; then
394 stp_port_set_cost "${zone}" "${port}" "${COST}"
395 fi
396
397 if isset PRIORITY; then
398 stp_port_set_priority "${zone}" "${port}" "${PRIORITY}"
399 fi
400
401 return ${EXIT_OK}
402 }
403
404 hook_port_down() {
405 assert [ $# -eq 2 ]
406
407 local zone="${1}"
408 local port="${2}"
409
410 if device_exists "${port}"; then
411 bridge_detach_device "${zone}" "${port}"
412
413 port_down "${port}"
414 fi
415
416 return ${EXIT_OK}
417 }
418
419 hook_port_status() {
420 assert [ $# -eq 2 ]
421
422 local zone="${1}"
423 local port="${2}"
424
425 # Do nothing for devices which are not up and running.
426 device_exists "${port}" || exit ${EXIT_OK}
427
428 local status
429
430 # Check if the device is down.
431 if ! device_is_up "${port}"; then
432 status="${MSG_DEVICE_STATUS_DOWN}"
433
434 # Check if the device has no carrier.
435 elif ! device_has_carrier "${port}"; then
436 status="${MSG_DEVICE_STATUS_NOCARRIER}"
437
438 # Check for STP information.
439 elif stp_is_enabled "${zone}"; then
440 local state="$(stp_port_get_state "${zone}" "${port}")"
441 state="MSG_STP_${state}"
442 status="${!state}"
443
444 status="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
445 status="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
446 else
447 status="${MSG_DEVICE_STATUS_UP}"
448 fi
449 cli_statusline 3 "${port}" "${status}"
450
451 exit ${EXIT_OK}
452 }