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 hook_check_settings
() {
44 assert isoneof STP_MODE stp rstp
45 assert isinteger STP_HELLO
46 assert isinteger STP_FORWARD_DELAY
47 assert isinteger STP_PRIORITY
51 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}'"
82 # Generate a random MAC address if the user passed no one
83 isset MAC || MAC
="$(mac_generate)"
90 zone_settings_read
"${zone}"
92 # Create the bridge if it does not already exist.
93 if ! device_exists
"${zone}"; then
94 bridge_create
"${zone}" \
103 if isset STP_FORWARD_DELAY
; then
104 stp_bridge_set_forward_delay
"${zone}" "${STP_FORWARD_DELAY}"
107 if isset STP_HELLO
; then
108 stp_bridge_set_hello_time
"${zone}" "${STP_HELLO}"
111 if isset STP_MAXAGE
; then
112 stp_bridge_set_max_age
"${zone}" "${STP_MAXAGE}"
115 if isset STP_PRIORITY
; then
116 stp_bridge_set_priority
"${zone}" "${STP_PRIORITY}"
119 stp_disable
"${zone}"
122 device_set_up
"${zone}"
124 # XXX Currently, there is a bug (in the linux kernel?) that we need to
125 # set our bridges to promisc mode.
126 device_set_promisc
"${zone}" on
129 zone_ports_create
"${zone}"
130 zone_ports_up
"${zone}"
132 # Bring up all configurations
133 zone_configs_up
"${zone}"
142 case "$(hotplug_action)" in
144 # Handle ports of this zone that have just been added
145 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
146 # Bring up the zone if it is enabled but not active, yet.
147 if zone_is_enabled
"${zone}" && ! zone_is_active
"${zone}"; then
151 hook_port_up
"${zone}" "${INTERFACE}"
155 # Handle ports of this zone that have just been removed
156 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
157 hook_port_down
"${zone}" "${INTERFACE}"
161 exit ${EXIT_NOT_HANDLED}
172 if ! device_is_up
"${zone}"; then
173 warning
"Zone '${zone}' is not up"
177 # Stop all the configs.
178 zone_configs_down
"${zone}"
180 # Bring down all the ports.
181 zone_ports_down
"${zone}"
182 zone_ports_remove
"${zone}"
185 device_set_down
"${zone}"
186 bridge_delete
"${zone}"
195 # Print the default header.
196 cli_device_headline
"${zone}"
198 # Exit if zone is down
199 if ! zone_is_up
"${zone}"; then
204 cli_headline
2 "Spanning Tree Protocol information"
205 if stp_is_enabled
"${zone}"; then
206 local proto
=$
(stp_bridge_get_protocol
${zone})
208 cli_print_fmt1
2 "Version" "$(stp_get_name ${proto})"
209 cli_print_fmt1
2 "ID" "$(stp_bridge_get_id ${zone})"
210 cli_print_fmt1
2 "Priority" "$(stp_bridge_get_priority ${zone})"
212 if stp_bridge_is_root
${zone}; then
213 cli_print
2 "This bridge is root."
215 cli_print_fmt1
2 "Designated root" \
216 "$(stp_bridge_get_designated_root ${zone})"
217 cli_print_fmt1
2 "Root path cost" \
218 "$(stp_bridge_get_root_path_cost ${zone})"
222 # Topology information
223 cli_print_fmt1
2 "Topology changing" \
224 "$(stp_bridge_get_topology_change_detected ${zone})"
225 cli_print_fmt1
2 "Topology change time" \
226 "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))"
227 cli_print_fmt1
2 "Topology change count" \
228 "$(stp_bridge_get_topology_change_count ${zone})"
231 cli_print
2 "Disabled"
235 cli_headline
2 "Ports"
236 zone_ports_status
"${zone}"
239 cli_headline
2 "Configurations"
240 zone_configs_cmd status
"${zone}"
246 hook_check_port_settings
() {
248 assert isinteger COST
251 if isset PRIORITY
; then
252 assert isinteger PRIORITY
257 # Excepting at least two arguments here
264 if zone_has_port
"${zone}" "${port}"; then
265 zone_port_settings_read
"${zone}" "${port}"
273 COST
="$(cli_get_val "${arg}")"
276 PRIORITY
="$(cli_get_val "${arg}")"
279 done <<< "$(args $@)"
281 if ! zone_port_settings_write
"${zone}" "${port}"; then
294 # Shut down the port (if possible)
297 if ! zone_port_settings_remove
"${zone}" "${port}"; then
314 # Try bringing up the port if it has not been
316 # We will get here as soon as the port device has
317 # been created and will then connect it with the bridge.
318 if ! device_exists
"${port}"; then
324 # Read configuration values
325 zone_port_settings_read
"${zone}" "${port}" ${HOOK_PORT_SETTINGS}
327 # Attach the port to the bridge
328 bridge_attach_device "${zone}" "${port}"
330 # Set STP configuration
332 stp_port_set_cost "${zone}" "${port}" "${COST}"
335 # TODO Apply priority (#10609)
337 # Make sure that the port is up
349 if device_exists "${port}"; then
350 bridge_detach_device "${zone}" "${port}"
364 # Do nothing for devices which are not up and running.
365 device_exists "${port}" || exit ${EXIT_OK}
369 # Check if the device is down.
370 if ! device_is_up "${port}"; then
371 status="${MSG_DEVICE_STATUS_DOWN}"
373 # Check if the device has no carrier.
374 elif ! device_has_carrier "${port}"; then
375 status="${MSG_DEVICE_STATUS_NOCARRIER}"
377 # Check for STP information.
378 elif stp_is_enabled "${zone}"; then
379 local state="$
(stp_port_get_state
"${zone}" "${port}")"
380 state="MSG_STP_
${state}"
383 status="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
384 status
="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
386 status="${MSG_DEVICE_STATUS_UP}"
388 cli_statusline 3 "${port}" "${status}"