]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/zones/bridge
bridge: Set proper defaults
[people/ms/network.git] / src / hooks / zones / bridge
CommitLineData
1848564d
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
e2aa12b3 22. /usr/lib/network/header-zone
1848564d 23
de125810
MT
24HOOK_MANPAGE="network-zone-bridge"
25
b99bbd83
MT
26HOOK_SETTINGS="HOOK ADDRESS STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE"
27HOOK_SETTINGS="${HOOK_SETTINGS} STP_PRIORITY MTU"
1848564d 28
828eb94a
MT
29HOOK_PORT_SETTINGS="COST PRIORITY"
30
1848564d 31# Default values
c259c985
MT
32DEFAULT_STP_FORWARD_DELAY=0
33DEFAULT_STP_HELLO=2
34DEFAULT_STP_MAXAGE=20
35DEFAULT_STP_PRIORITY=512
1848564d 36
1c6a4e30 37hook_check_settings() {
b99bbd83 38 assert ismac ADDRESS
d95e2fdc 39 assert isset MTU && assert mtu_is_valid "ethernet" "${MTU}"
b99bbd83
MT
40
41 # Spanning Tree Protocol
1848564d
MT
42 assert isbool STP
43 assert isinteger STP_HELLO
44 assert isinteger STP_FORWARD_DELAY
d82cf370 45 assert isinteger STP_PRIORITY
1848564d
MT
46}
47
1c6a4e30 48hook_parse_cmdline() {
1848564d
MT
49 while [ $# -gt 0 ]; do
50 case "${1}" in
b99bbd83
MT
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
1848564d 69 --stp=*)
b99bbd83
MT
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
1848564d 80 ;;
b99bbd83 81
1848564d 82 --stp-hello=*)
b99bbd83
MT
83 STP_HELLO="$(cli_get_val "${1}")"
84
85 if ! isinteger STP_HELLO; then
86 error "Invalid STP hello time: ${STP_HELLO}"
87 return ${EXIT_ERROR}
88 fi
1848564d 89 ;;
b99bbd83 90
1848564d 91 --stp-forward-delay=*)
b99bbd83
MT
92 STP_FORWARD_DELAY="$(cli_get_val "${1}")"
93
94 if ! isinteger STP_FORWARD_DELAY; then
95 error "Invalid STP forwarding delay: ${STP_FORWARD_DELAY}"
96 return ${EXIT_ERROR}
97 fi
1848564d 98 ;;
b99bbd83 99
d82cf370 100 --stp-priority=*)
b99bbd83
MT
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
1848564d 107 ;;
b99bbd83 108
1848564d 109 *)
b99bbd83
MT
110 error "Unknown argument: ${1}"
111 return ${EXIT_ERROR}
1848564d
MT
112 ;;
113 esac
114 shift
115 done
0430028a
MT
116
117 # Generate a random MAC address if the user passed no one
c259c985 118 if ! isset ADDRESS; then
b99bbd83
MT
119 ADDRESS="$(mac_generate)"
120 fi
121
c259c985
MT
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
b99bbd83 130 return ${EXIT_OK}
1848564d
MT
131}
132
1c6a4e30 133hook_up() {
1848564d 134 local zone=${1}
99be6026 135 assert isset zone
1848564d 136
1e6f187e 137 zone_settings_read "${zone}"
1848564d 138
99be6026 139 # Create the bridge if it does not already exist.
2a969c27
MT
140 if ! device_exists "${zone}"; then
141 bridge_create "${zone}" \
b99bbd83 142 --address="${ADDRESS}" \
2a969c27 143 --mtu="${MTU}"
99be6026 144 fi
1848564d
MT
145
146 # Enable STP
147 if enabled STP; then
2a969c27 148 stp_enable "${zone}"
1848564d 149
2a969c27
MT
150 if isset STP_FORWARD_DELAY; then
151 stp_bridge_set_forward_delay "${zone}" "${STP_FORWARD_DELAY}"
1848564d
MT
152 fi
153
2a969c27
MT
154 if isset STP_HELLO; then
155 stp_bridge_set_hello_time "${zone}" "${STP_HELLO}"
1848564d
MT
156 fi
157
2a969c27
MT
158 if isset STP_MAXAGE; then
159 stp_bridge_set_max_age "${zone}" "${STP_MAXAGE}"
1848564d 160 fi
d82cf370 161
2a969c27
MT
162 if isset STP_PRIORITY; then
163 stp_bridge_set_priority "${zone}" "${STP_PRIORITY}"
d82cf370 164 fi
1848564d 165 else
2a969c27 166 stp_disable "${zone}"
1848564d
MT
167 fi
168
2a969c27 169 device_set_up "${zone}"
1848564d 170
cf6e4606
MT
171 # XXX Currently, there is a bug (in the linux kernel?) that we need to
172 # set our bridges to promisc mode.
2a969c27
MT
173 device_set_promisc "${zone}" on
174
2a969c27
MT
175 # Bring up all configurations
176 zone_configs_up "${zone}"
177
178 exit ${EXIT_OK}
179}
180
1c6a4e30 181hook_down() {
2a969c27 182 local zone="${1}"
99be6026 183 assert isset zone
1848564d 184
2a969c27 185 if ! device_is_up "${zone}"; then
1848564d
MT
186 warning "Zone '${zone}' is not up"
187 exit ${EXIT_OK}
188 fi
189
2a969c27
MT
190 # Stop all the configs.
191 zone_configs_down "${zone}"
1848564d 192
2a969c27
MT
193 # Bring down all the ports.
194 zone_ports_down "${zone}"
1ba6a2bb 195 zone_ports_remove "${zone}"
cf6e4606 196
2a969c27
MT
197 # Remove the bridge.
198 device_set_down "${zone}"
199 bridge_delete "${zone}"
1848564d 200
8dcb2687 201 exit ${EXIT_OK}
1848564d
MT
202}
203
1c6a4e30 204hook_status() {
2a969c27 205 local zone="${1}"
3cb2fc42 206 assert isset zone
e84e4e76 207
3cb2fc42 208 # Print the default header.
2a969c27 209 cli_device_headline "${zone}"
e84e4e76
MT
210
211 # Exit if zone is down
2a969c27 212 if ! zone_is_up "${zone}"; then
e84e4e76
MT
213 echo # Empty line
214 exit ${EXIT_ERROR}
215 fi
216
3cb2fc42 217 cli_headline 2 "Spanning Tree Protocol information"
2a969c27 218 if stp_is_enabled "${zone}"; then
3cb2fc42
MT
219 cli_print_fmt1 2 "ID" "$(stp_bridge_get_id ${zone})"
220 cli_print_fmt1 2 "Priority" "$(stp_bridge_get_priority ${zone})"
36e3fd2f
MT
221
222 if stp_bridge_is_root ${zone}; then
3cb2fc42 223 cli_print 2 "This bridge is root."
36e3fd2f 224 else
3cb2fc42
MT
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})"
36e3fd2f 229 fi
3cb2fc42 230 cli_space
feb76eaf 231
36e3fd2f 232 # Topology information
3cb2fc42
MT
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
feb76eaf 240 else
3cb2fc42
MT
241 cli_print 2 "Disabled"
242 cli_space
feb76eaf 243 fi
e84e4e76 244
50250b79 245 cli_headline 2 "Ports"
2a969c27 246 zone_ports_status "${zone}"
8e3508ac 247 cli_space
e84e4e76 248
50250b79 249 cli_headline 2 "Configurations"
2a969c27 250 zone_configs_cmd status "${zone}"
3cb2fc42 251 cli_space
8e3508ac 252
e84e4e76
MT
253 exit ${EXIT_OK}
254}
828eb94a 255
1fc4b3ca
MT
256hook_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
1c6a4e30 305hook_check_port_settings() {
ac694a6a
MT
306 if isset COST; then
307 assert isinteger COST
308 fi
828eb94a 309
ac694a6a
MT
310 if isset PRIORITY; then
311 assert isinteger PRIORITY
312 fi
828eb94a
MT
313}
314
1c6a4e30 315hook_port_attach() {
828eb94a
MT
316 # Excepting at least two arguments here
317 assert [ $# -ge 2 ]
318
319 local zone="${1}"
320 local port="${2}"
321 shift 2
322
ac694a6a
MT
323 if zone_has_port "${zone}" "${port}"; then
324 zone_port_settings_read "${zone}" "${port}"
325 fi
828eb94a 326
ac694a6a
MT
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
2212045f 338 done <<< "$(args "$@")"
828eb94a 339
ac694a6a
MT
340 if ! zone_port_settings_write "${zone}" "${port}"; then
341 exit ${EXIT_ERROR}
342 fi
828eb94a
MT
343
344 exit ${EXIT_OK}
345}
346
1c6a4e30 347hook_port_detach() {
828eb94a
MT
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
ac694a6a
MT
356 if ! zone_port_settings_remove "${zone}" "${port}"; then
357 exit ${EXIT_ERROR}
358 fi
828eb94a
MT
359
360 exit ${EXIT_OK}
361}
362
1c6a4e30 363hook_port_edit() {
2212045f 364 hook_port_attach "$@"
ac694a6a
MT
365}
366
1c6a4e30 367hook_port_up() {
828eb94a
MT
368 assert [ $# -eq 2 ]
369
370 local zone="${1}"
371 local port="${2}"
372
2a969c27
MT
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.
3ee5ccb1 377 if ! device_exists "${port}"; then
75985eb1 378 port_create "${port}"
2a969c27 379
9b47451e 380 return ${EXIT_OK}
3ee5ccb1
MT
381 fi
382
383 # Read configuration values
384 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
828eb94a 385
8707df4b
MT
386 # Make sure that the port is up
387 port_up "${port}"
388
3ee5ccb1 389 # Attach the port to the bridge
828eb94a
MT
390 bridge_attach_device "${zone}" "${port}"
391
3ee5ccb1 392 # Set STP configuration
2c083d57
MT
393 if isset COST; then
394 stp_port_set_cost "${zone}" "${port}" "${COST}"
395 fi
396
424b8280
MT
397 if isset PRIORITY; then
398 stp_port_set_priority "${zone}" "${port}" "${PRIORITY}"
399 fi
828eb94a 400
9b47451e 401 return ${EXIT_OK}
828eb94a
MT
402}
403
1c6a4e30 404hook_port_down() {
828eb94a
MT
405 assert [ $# -eq 2 ]
406
407 local zone="${1}"
408 local port="${2}"
409
2a969c27
MT
410 if device_exists "${port}"; then
411 bridge_detach_device "${zone}" "${port}"
828eb94a 412
2a969c27
MT
413 port_down "${port}"
414 fi
828eb94a 415
9b47451e 416 return ${EXIT_OK}
828eb94a
MT
417}
418
1c6a4e30 419hook_port_status() {
828eb94a
MT
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}