2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 .
/usr
/lib
/network
/header-zone
24 HOOK_MANPAGE
="network-zone-bridge"
26 HOOK_SETTINGS
="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_MODE"
27 HOOK_SETTINGS
="${HOOK_SETTINGS} STP_PRIORITY MAC MTU"
29 HOOK_PORT_SETTINGS
="COST PRIORITY"
41 function hook_check
() {
44 assert isoneof STP_MODE stp rstp
45 assert isinteger STP_HELLO
46 assert isinteger STP_FORWARD_DELAY
47 assert isinteger STP_PRIORITY
51 function hook_parse_cmdline
() {
52 while [ $# -gt 0 ]; do
58 STP_MODE
=${1#--stp-mode=}
61 STP_HELLO
=${1#--stp-hello=}
63 --stp-forward-delay=*)
64 STP_FORWARD_DELAY
=${1#--stp-forward-delay=}
67 STP_PRIORITY
=${1#--stp-priority=}
76 warning
"Ignoring unknown option '${1}'"
87 zone_settings_read
"${zone}" ${HOOK_SETTINGS}
89 # Create the bridge if it does not already exist.
90 if ! device_exists
"${zone}"; then
91 bridge_create
"${zone}" \
100 if isset STP_FORWARD_DELAY
; then
101 stp_bridge_set_forward_delay
"${zone}" "${STP_FORWARD_DELAY}"
104 if isset STP_HELLO
; then
105 stp_bridge_set_hello_time
"${zone}" "${STP_HELLO}"
108 if isset STP_MAXAGE
; then
109 stp_bridge_set_max_age
"${zone}" "${STP_MAXAGE}"
112 if isset STP_PRIORITY
; then
113 stp_bridge_set_priority
"${zone}" "${STP_PRIORITY}"
116 stp_disable
"${zone}"
119 device_set_up
"${zone}"
121 # XXX Currently, there is a bug (in the linux kernel?) that we need to
122 # set our bridges to promisc mode.
123 device_set_promisc
"${zone}" on
126 zone_ports_create
"${zone}"
127 zone_ports_up
"${zone}"
129 # Bring up all configurations
130 zone_configs_up
"${zone}"
135 function hook_hotplug
() {
139 case "$(hotplug_action)" in
141 # Handle ports of this zone that have just been added
142 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
143 # Bring up the zone if it is enabled but not active, yet.
144 if zone_is_enabled
"${zone}" && ! zone_is_active
"${zone}"; then
148 hook_port_up
"${zone}" "${INTERFACE}"
152 # Handle ports of this zone that have just been removed
153 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
154 hook_port_down
"${zone}" "${INTERFACE}"
158 exit ${EXIT_NOT_HANDLED}
165 function hook_down
() {
169 if ! device_is_up
"${zone}"; then
170 warning
"Zone '${zone}' is not up"
174 # Stop all the configs.
175 zone_configs_down
"${zone}"
177 # Bring down all the ports.
178 zone_ports_down
"${zone}"
179 zone_ports_remove
"${zone}"
182 device_set_down
"${zone}"
183 bridge_delete
"${zone}"
188 function hook_status
() {
192 # Print the default header.
193 cli_device_headline
"${zone}"
195 # Exit if zone is down
196 if ! zone_is_up
"${zone}"; then
201 cli_headline
2 "Spanning Tree Protocol information"
202 if stp_is_enabled
"${zone}"; then
203 local proto
=$
(stp_bridge_get_protocol
${zone})
205 cli_print_fmt1
2 "Version" "$(stp_get_name ${proto})"
206 cli_print_fmt1
2 "ID" "$(stp_bridge_get_id ${zone})"
207 cli_print_fmt1
2 "Priority" "$(stp_bridge_get_priority ${zone})"
209 if stp_bridge_is_root
${zone}; then
210 cli_print
2 "This bridge is root."
212 cli_print_fmt1
2 "Designated root" \
213 "$(stp_bridge_get_designated_root ${zone})"
214 cli_print_fmt1
2 "Root path cost" \
215 "$(stp_bridge_get_root_path_cost ${zone})"
219 # Topology information
220 cli_print_fmt1
2 "Topology changing" \
221 "$(stp_bridge_get_topology_change_detected ${zone})"
222 cli_print_fmt1
2 "Topology change time" \
223 "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))"
224 cli_print_fmt1
2 "Topology change count" \
225 "$(stp_bridge_get_topology_change_count ${zone})"
228 cli_print
2 "Disabled"
232 cli_headline
2 "Ports"
233 zone_ports_status
"${zone}"
236 cli_headline
2 "Configurations"
237 zone_configs_cmd status
"${zone}"
243 function __parse_cmdline_args
() {
244 while [ $# -gt 0 ]; do
247 PRIORITY
="$(cli_get_val ${1})"
250 COST
="$(cli_get_val ${1})"
259 function hook_port_add
() {
260 # Excepting at least two arguments here
267 __parse_cmdline_args
"$@"
268 [ $?
-eq ${EXIT_OK} ] ||
return ${EXIT_ERROR}
270 zone_port_settings_write
"${zone}" "${port}" ${HOOK_PORT_SETTINGS}
272 log INFO "Port
'${port}' has been added to zone
'${zone}'"
277 function hook_port_edit() {
284 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
286 __parse_cmdline_args
"$@"
287 [ $?
-eq ${EXIT_OK} ] ||
return ${EXIT_ERROR}
289 zone_port_settings_write
"${zone}" "${port}" ${HOOK_PORT_SETTINGS}
291 log INFO "Port
'${port}' (member of zone
'${zone}') has been edited
"
296 function hook_port_destroy() {
302 # Shut down the port (if possible)
305 log INFO "Port
'${port}' has been removed from zone
'${zone}'"
306 zone_port_settings_remove "${zone}" "${port}"
311 function hook_port_up() {
317 # Try bringing up the port if it has not been
319 # We will get here as soon as the port device has
320 # been created and will then connect it with the bridge.
321 if ! device_exists "${port}"; then
327 # Read configuration values
328 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
330 # Attach the port to the bridge
331 bridge_attach_device
"${zone}" "${port}"
333 # Set STP configuration
335 stp_port_set_cost
"${zone}" "${port}" "${COST}"
338 # TODO Apply priority (#10609)
340 # Make sure that the port is up
346 function hook_port_down
() {
352 if device_exists
"${port}"; then
353 bridge_detach_device
"${zone}" "${port}"
361 function hook_port_status
() {
367 # Do nothing for devices which are not up and running.
368 device_exists
"${port}" ||
exit ${EXIT_OK}
372 # Check if the device is down.
373 if ! device_is_up
"${port}"; then
374 status
="${MSG_DEVICE_STATUS_DOWN}"
376 # Check if the device has no carrier.
377 elif ! device_has_carrier
"${port}"; then
378 status
="${MSG_DEVICE_STATUS_NOCARRIER}"
380 # Check for STP information.
381 elif stp_is_enabled
"${zone}"; then
382 local state
="$(stp_port_get_state "${zone}" "${port}")"
383 state
="MSG_STP_${state}"
386 status
="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
387 status="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
389 status
="${MSG_DEVICE_STATUS_UP}"
391 cli_statusline
3 "${port}" "${status}"