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"
27 HOOK_SETTINGS
="${HOOK_SETTINGS} STP_PRIORITY MAC MTU"
29 HOOK_PORT_SETTINGS
="COST PRIORITY"
40 hook_check_settings
() {
43 assert isinteger STP_HELLO
44 assert isinteger STP_FORWARD_DELAY
45 assert isinteger STP_PRIORITY
49 hook_parse_cmdline
() {
50 while [ $# -gt 0 ]; do
56 STP_HELLO
=${1#--stp-hello=}
58 --stp-forward-delay=*)
59 STP_FORWARD_DELAY
=${1#--stp-forward-delay=}
62 STP_PRIORITY
=${1#--stp-priority=}
71 warning
"Ignoring unknown option '${1}'"
77 # Generate a random MAC address if the user passed no one
78 isset MAC || MAC
="$(mac_generate)"
85 zone_settings_read
"${zone}"
87 # Create the bridge if it does not already exist.
88 if ! device_exists
"${zone}"; then
89 bridge_create
"${zone}" \
98 if isset STP_FORWARD_DELAY
; then
99 stp_bridge_set_forward_delay
"${zone}" "${STP_FORWARD_DELAY}"
102 if isset STP_HELLO
; then
103 stp_bridge_set_hello_time
"${zone}" "${STP_HELLO}"
106 if isset STP_MAXAGE
; then
107 stp_bridge_set_max_age
"${zone}" "${STP_MAXAGE}"
110 if isset STP_PRIORITY
; then
111 stp_bridge_set_priority
"${zone}" "${STP_PRIORITY}"
114 stp_disable
"${zone}"
117 device_set_up
"${zone}"
119 # XXX Currently, there is a bug (in the linux kernel?) that we need to
120 # set our bridges to promisc mode.
121 device_set_promisc
"${zone}" on
124 zone_ports_create
"${zone}"
125 zone_ports_up
"${zone}"
127 # Bring up all configurations
128 zone_configs_up
"${zone}"
137 case "$(hotplug_action)" in
139 # Handle ports of this zone that have just been added
140 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
141 # Bring up the zone if it is enabled but not active, yet.
142 if zone_is_enabled
"${zone}" && ! zone_is_active
"${zone}"; then
146 hook_port_up
"${zone}" "${INTERFACE}"
150 # Handle ports of this zone that have just been removed
151 if hotplug_event_interface_is_port_of_zone
"${zone}"; then
152 hook_port_down
"${zone}" "${INTERFACE}"
156 exit ${EXIT_NOT_HANDLED}
167 if ! device_is_up
"${zone}"; then
168 warning
"Zone '${zone}' is not up"
172 # Stop all the configs.
173 zone_configs_down
"${zone}"
175 # Bring down all the ports.
176 zone_ports_down
"${zone}"
177 zone_ports_remove
"${zone}"
180 device_set_down
"${zone}"
181 bridge_delete
"${zone}"
190 # Print the default header.
191 cli_device_headline
"${zone}"
193 # Exit if zone is down
194 if ! zone_is_up
"${zone}"; then
199 cli_headline
2 "Spanning Tree Protocol information"
200 if stp_is_enabled
"${zone}"; then
201 cli_print_fmt1
2 "ID" "$(stp_bridge_get_id ${zone})"
202 cli_print_fmt1
2 "Priority" "$(stp_bridge_get_priority ${zone})"
204 if stp_bridge_is_root
${zone}; then
205 cli_print
2 "This bridge is root."
207 cli_print_fmt1
2 "Designated root" \
208 "$(stp_bridge_get_designated_root ${zone})"
209 cli_print_fmt1
2 "Root path cost" \
210 "$(stp_bridge_get_root_path_cost ${zone})"
214 # Topology information
215 cli_print_fmt1
2 "Topology changing" \
216 "$(stp_bridge_get_topology_change_detected ${zone})"
217 cli_print_fmt1
2 "Topology change time" \
218 "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))"
219 cli_print_fmt1
2 "Topology change count" \
220 "$(stp_bridge_get_topology_change_count ${zone})"
223 cli_print
2 "Disabled"
227 cli_headline
2 "Ports"
228 zone_ports_status
"${zone}"
231 cli_headline
2 "Configurations"
232 zone_configs_cmd status
"${zone}"
238 hook_check_port_settings
() {
240 assert isinteger COST
243 if isset PRIORITY
; then
244 assert isinteger PRIORITY
249 # Excepting at least two arguments here
256 if zone_has_port
"${zone}" "${port}"; then
257 zone_port_settings_read
"${zone}" "${port}"
265 COST
="$(cli_get_val "${arg}")"
268 PRIORITY
="$(cli_get_val "${arg}")"
271 done <<< "$(args "$@
")"
273 if ! zone_port_settings_write
"${zone}" "${port}"; then
286 # Shut down the port (if possible)
289 if ! zone_port_settings_remove
"${zone}" "${port}"; then
297 hook_port_attach
"$@"
306 # Try bringing up the port if it has not been
308 # We will get here as soon as the port device has
309 # been created and will then connect it with the bridge.
310 if ! device_exists
"${port}"; then
311 port_create
"${port}"
316 # Read configuration values
317 zone_port_settings_read
"${zone}" "${port}" ${HOOK_PORT_SETTINGS}
319 # Attach the port to the bridge
320 bridge_attach_device "${zone}" "${port}"
322 # Set STP configuration
324 stp_port_set_cost "${zone}" "${port}" "${COST}"
327 if isset PRIORITY; then
328 stp_port_set_priority "${zone}" "${port}" "${PRIORITY}"
331 # Make sure that the port is up
343 if device_exists "${port}"; then
344 bridge_detach_device "${zone}" "${port}"
358 # Do nothing for devices which are not up and running.
359 device_exists "${port}" || exit ${EXIT_OK}
363 # Check if the device is down.
364 if ! device_is_up "${port}"; then
365 status="${MSG_DEVICE_STATUS_DOWN}"
367 # Check if the device has no carrier.
368 elif ! device_has_carrier "${port}"; then
369 status="${MSG_DEVICE_STATUS_NOCARRIER}"
371 # Check for STP information.
372 elif stp_is_enabled "${zone}"; then
373 local state="$
(stp_port_get_state
"${zone}" "${port}")"
374 state="MSG_STP_
${state}"
377 status="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
378 status
="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
380 status="${MSG_DEVICE_STATUS_UP}"
382 cli_statusline 3 "${port}" "${status}"