]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/zones/bridge
Remove support for macvtap devices
[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
6b3f9c85
MT
26HOOK_SETTINGS="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_MODE"
27HOOK_SETTINGS="${HOOK_SETTINGS} STP_PRIORITY MAC MTU"
1848564d 28
828eb94a
MT
29HOOK_PORT_SETTINGS="COST PRIORITY"
30
1848564d
MT
31# Default values
32MAC=$(mac_generate)
33MTU=1500
34STP="on"
6b3f9c85 35STP_MODE="rstp"
1848564d
MT
36STP_FORWARD_DELAY=0
37STP_HELLO=2
38STP_MAXAGE=20
e266c18e 39STP_PRIORITY=512
1848564d 40
2181765d 41function hook_check() {
1848564d
MT
42 assert ismac MAC
43 assert isbool STP
6b3f9c85 44 assert isoneof STP_MODE stp rstp
1848564d
MT
45 assert isinteger STP_HELLO
46 assert isinteger STP_FORWARD_DELAY
d82cf370 47 assert isinteger STP_PRIORITY
1848564d
MT
48 assert isinteger MTU
49}
50
2181765d 51function hook_parse_cmdline() {
1848564d
MT
52 while [ $# -gt 0 ]; do
53 case "${1}" in
54 --stp=*)
55 STP=${1#--stp=}
56 ;;
6b3f9c85
MT
57 --stp-mode=*)
58 STP_MODE=${1#--stp-mode=}
59 ;;
1848564d
MT
60 --stp-hello=*)
61 STP_HELLO=${1#--stp-hello=}
62 ;;
63 --stp-forward-delay=*)
64 STP_FORWARD_DELAY=${1#--stp-forward-delay=}
65 ;;
d82cf370
MT
66 --stp-priority=*)
67 STP_PRIORITY=${1#--stp-priority=}
68 ;;
1848564d
MT
69 --mtu=*)
70 MTU=${1#--mtu=}
71 ;;
72 --mac=*)
73 MAC=${1#--mac=}
74 ;;
75 *)
76 warning "Ignoring unknown option '${1}'"
77 ;;
78 esac
79 shift
80 done
81}
82
2181765d 83function hook_up() {
1848564d 84 local zone=${1}
99be6026 85 assert isset zone
1848564d 86
e9df08ad 87 zone_settings_read "${zone}" ${HOOK_SETTINGS}
1848564d 88
99be6026 89 # Create the bridge if it does not already exist.
2a969c27
MT
90 if ! device_exists "${zone}"; then
91 bridge_create "${zone}" \
92 --address="${MAC}" \
93 --mtu="${MTU}"
99be6026 94 fi
1848564d
MT
95
96 # Enable STP
97 if enabled STP; then
2a969c27 98 stp_enable "${zone}"
1848564d 99
2a969c27
MT
100 if isset STP_FORWARD_DELAY; then
101 stp_bridge_set_forward_delay "${zone}" "${STP_FORWARD_DELAY}"
1848564d
MT
102 fi
103
2a969c27
MT
104 if isset STP_HELLO; then
105 stp_bridge_set_hello_time "${zone}" "${STP_HELLO}"
1848564d
MT
106 fi
107
2a969c27
MT
108 if isset STP_MAXAGE; then
109 stp_bridge_set_max_age "${zone}" "${STP_MAXAGE}"
1848564d 110 fi
d82cf370 111
2a969c27
MT
112 if isset STP_PRIORITY; then
113 stp_bridge_set_priority "${zone}" "${STP_PRIORITY}"
d82cf370 114 fi
1848564d 115 else
2a969c27 116 stp_disable "${zone}"
1848564d
MT
117 fi
118
2a969c27 119 device_set_up "${zone}"
1848564d 120
cf6e4606
MT
121 # XXX Currently, there is a bug (in the linux kernel?) that we need to
122 # set our bridges to promisc mode.
2a969c27
MT
123 device_set_promisc "${zone}" on
124
125 # Bring up all ports
126 zone_ports_up "${zone}"
127
128 # Bring up all configurations
129 zone_configs_up "${zone}"
130
131 exit ${EXIT_OK}
132}
133
134function hook_hotplug() {
135 local zone="${1}"
136 assert isset zone
cf6e4606 137
2a969c27
MT
138 case "$(hotplug_action)" in
139 add)
140 # Handle ports of this zone that have just been added
141 if hotplug_event_interface_is_port_of_zone "${zone}"; then
142 # Bring up the zone if it is enabled but not active, yet.
143 if zone_is_enabled "${zone}" && ! zone_is_active "${zone}"; then
144 zone_start "${zone}"
145 fi
146
147 hook_port_up "${zone}" "${INTERFACE}"
148 fi
149 ;;
150 remove)
151 # Handle ports of this zone that have just been removed
152 if hotplug_event_interface_is_port_of_zone "${zone}"; then
153 hook_port_down "${zone}" "${INTERFACE}"
154 fi
155 ;;
156 *)
157 exit ${EXIT_NOT_HANDLED}
158 ;;
159 esac
1848564d 160
8dcb2687 161 exit ${EXIT_OK}
1848564d
MT
162}
163
2181765d 164function hook_down() {
2a969c27 165 local zone="${1}"
99be6026 166 assert isset zone
1848564d 167
2a969c27 168 if ! device_is_up "${zone}"; then
1848564d
MT
169 warning "Zone '${zone}' is not up"
170 exit ${EXIT_OK}
171 fi
172
2a969c27
MT
173 # Stop all the configs.
174 zone_configs_down "${zone}"
1848564d 175
2a969c27
MT
176 # Bring down all the ports.
177 zone_ports_down "${zone}"
cf6e4606 178
2a969c27
MT
179 # Remove the bridge.
180 device_set_down "${zone}"
181 bridge_delete "${zone}"
1848564d 182
8dcb2687 183 exit ${EXIT_OK}
1848564d
MT
184}
185
2181765d 186function hook_status() {
2a969c27 187 local zone="${1}"
3cb2fc42 188 assert isset zone
e84e4e76 189
3cb2fc42 190 # Print the default header.
2a969c27 191 cli_device_headline "${zone}"
e84e4e76
MT
192
193 # Exit if zone is down
2a969c27 194 if ! zone_is_up "${zone}"; then
e84e4e76
MT
195 echo # Empty line
196 exit ${EXIT_ERROR}
197 fi
198
3cb2fc42 199 cli_headline 2 "Spanning Tree Protocol information"
2a969c27 200 if stp_is_enabled "${zone}"; then
3cb2fc42 201 local proto=$(stp_bridge_get_protocol ${zone})
8d4f9311 202
3cb2fc42
MT
203 cli_print_fmt1 2 "Version" "$(stp_get_name ${proto})"
204 cli_print_fmt1 2 "ID" "$(stp_bridge_get_id ${zone})"
205 cli_print_fmt1 2 "Priority" "$(stp_bridge_get_priority ${zone})"
36e3fd2f
MT
206
207 if stp_bridge_is_root ${zone}; then
3cb2fc42 208 cli_print 2 "This bridge is root."
36e3fd2f 209 else
3cb2fc42
MT
210 cli_print_fmt1 2 "Designated root" \
211 "$(stp_bridge_get_designated_root ${zone})"
212 cli_print_fmt1 2 "Root path cost" \
213 "$(stp_bridge_get_root_path_cost ${zone})"
36e3fd2f 214 fi
3cb2fc42 215 cli_space
feb76eaf 216
36e3fd2f 217 # Topology information
3cb2fc42
MT
218 cli_print_fmt1 2 "Topology changing" \
219 "$(stp_bridge_get_topology_change_detected ${zone})"
220 cli_print_fmt1 2 "Topology change time" \
221 "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))"
222 cli_print_fmt1 2 "Topology change count" \
223 "$(stp_bridge_get_topology_change_count ${zone})"
224 cli_space
feb76eaf 225 else
3cb2fc42
MT
226 cli_print 2 "Disabled"
227 cli_space
feb76eaf 228 fi
e84e4e76 229
50250b79 230 cli_headline 2 "Ports"
2a969c27 231 zone_ports_status "${zone}"
8e3508ac 232 cli_space
e84e4e76 233
50250b79 234 cli_headline 2 "Configurations"
2a969c27 235 zone_configs_cmd status "${zone}"
3cb2fc42 236 cli_space
8e3508ac 237
e84e4e76
MT
238 exit ${EXIT_OK}
239}
828eb94a
MT
240
241function __parse_cmdline_args() {
242 while [ $# -gt 0 ]; do
243 case "${1}" in
244 --priority=*)
245 PRIORITY="$(cli_get_val ${1})"
246 ;;
247 --cost=*)
248 COST="$(cli_get_val ${1})"
249 ;;
250 esac
251 shift
252 done
253
254 return ${EXIT_OK}
255}
256
257function hook_port_add() {
258 # Excepting at least two arguments here
259 assert [ $# -ge 2 ]
260
261 local zone="${1}"
262 local port="${2}"
263 shift 2
264
265 __parse_cmdline_args "$@"
266 [ $? -eq ${EXIT_OK} ] || return ${EXIT_ERROR}
267
e9df08ad 268 zone_port_settings_write "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
828eb94a
MT
269
270 log INFO "Port '${port}' has been added to zone '${zone}'"
271
272 exit ${EXIT_OK}
273}
274
275function hook_port_edit() {
276 assert [ $# -ge 2 ]
277
278 local zone="${1}"
279 local port="${2}"
280 shift 2
281
e9df08ad 282 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
828eb94a
MT
283
284 __parse_cmdline_args "$@"
285 [ $? -eq ${EXIT_OK} ] || return ${EXIT_ERROR}
286
e9df08ad 287 zone_port_settings_write "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
828eb94a
MT
288
289 log INFO "Port '${port}' (member of zone '${zone}') has been edited"
290
291 exit ${EXIT_OK}
292}
293
294function hook_port_remove() {
295 assert [ $# -eq 2 ]
296
297 local zone="${1}"
298 local port="${2}"
299
300 # Shut down the port (if possible)
301 port_down "${port}"
302
303 log INFO "Port '${port}' has been removed from zone '${zone}'"
e9df08ad 304 zone_port_settings_remove "${zone}" "${port}"
828eb94a
MT
305
306 exit ${EXIT_OK}
307}
308
309function hook_port_up() {
310 assert [ $# -eq 2 ]
311
312 local zone="${1}"
313 local port="${2}"
314
2a969c27
MT
315 # Try bringing up the port if it has not been
316 # brought up before.
317 # We will get here as soon as the port device has
318 # been created and will then connect it with the bridge.
3ee5ccb1 319 if ! device_exists "${port}"; then
2a969c27
MT
320 port_up "${port}"
321
3ee5ccb1
MT
322 exit ${EXIT_OK}
323 fi
324
325 # Read configuration values
326 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
828eb94a 327
3ee5ccb1 328 # Attach the port to the bridge
828eb94a
MT
329 bridge_attach_device "${zone}" "${port}"
330
3ee5ccb1 331 # Set STP configuration
2c083d57
MT
332 if isset COST; then
333 stp_port_set_cost "${zone}" "${port}" "${COST}"
334 fi
335
336 # TODO Apply priority (#10609)
828eb94a 337
2a969c27
MT
338 # Make sure that the port is up
339 device_set_up "${port}"
340
828eb94a
MT
341 exit ${EXIT_OK}
342}
343
344function hook_port_down() {
345 assert [ $# -eq 2 ]
346
347 local zone="${1}"
348 local port="${2}"
349
2a969c27
MT
350 if device_exists "${port}"; then
351 bridge_detach_device "${zone}" "${port}"
828eb94a 352
2a969c27
MT
353 port_down "${port}"
354 fi
828eb94a
MT
355
356 exit ${EXIT_OK}
357}
358
359function hook_port_status() {
360 assert [ $# -eq 2 ]
361
362 local zone="${1}"
363 local port="${2}"
364
365 # Do nothing for devices which are not up and running.
366 device_exists "${port}" || exit ${EXIT_OK}
367
368 local status
369
370 # Check if the device is down.
371 if ! device_is_up "${port}"; then
372 status="${MSG_DEVICE_STATUS_DOWN}"
373
374 # Check if the device has no carrier.
375 elif ! device_has_carrier "${port}"; then
376 status="${MSG_DEVICE_STATUS_NOCARRIER}"
377
378 # Check for STP information.
379 elif stp_is_enabled "${zone}"; then
380 local state="$(stp_port_get_state "${zone}" "${port}")"
381 state="MSG_STP_${state}"
382 status="${!state}"
383
384 status="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
385 status="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
386 else
387 status="${MSG_DEVICE_STATUS_UP}"
388 fi
389 cli_statusline 3 "${port}" "${status}"
390
391 exit ${EXIT_OK}
392}