]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/bridge
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / hooks / zones / bridge
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
22 . /usr/lib/network/header-zone
23
24 HOOK_MANPAGE="network-zone-bridge"
25
26 HOOK_SETTINGS="HOOK STP STP_FORWARD_DELAY STP_HELLO STP_MAXAGE STP_MODE"
27 HOOK_SETTINGS="${HOOK_SETTINGS} STP_PRIORITY MAC MTU"
28
29 HOOK_PORT_SETTINGS="COST PRIORITY"
30
31 # Default values
32 MAC=$(mac_generate)
33 MTU=1500
34 STP="on"
35 STP_MODE="rstp"
36 STP_FORWARD_DELAY=0
37 STP_HELLO=2
38 STP_MAXAGE=20
39 STP_PRIORITY=512
40
41 hook_check_settings() {
42 assert ismac MAC
43 assert isbool STP
44 assert isoneof STP_MODE stp rstp
45 assert isinteger STP_HELLO
46 assert isinteger STP_FORWARD_DELAY
47 assert isinteger STP_PRIORITY
48 assert isinteger MTU
49 }
50
51 hook_parse_cmdline() {
52 while [ $# -gt 0 ]; do
53 case "${1}" in
54 --stp=*)
55 STP=${1#--stp=}
56 ;;
57 --stp-mode=*)
58 STP_MODE=${1#--stp-mode=}
59 ;;
60 --stp-hello=*)
61 STP_HELLO=${1#--stp-hello=}
62 ;;
63 --stp-forward-delay=*)
64 STP_FORWARD_DELAY=${1#--stp-forward-delay=}
65 ;;
66 --stp-priority=*)
67 STP_PRIORITY=${1#--stp-priority=}
68 ;;
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
83 hook_up() {
84 local zone=${1}
85 assert isset zone
86
87 zone_settings_read "${zone}"
88
89 # Create the bridge if it does not already exist.
90 if ! device_exists "${zone}"; then
91 bridge_create "${zone}" \
92 --address="${MAC}" \
93 --mtu="${MTU}"
94 fi
95
96 # Enable STP
97 if enabled STP; then
98 stp_enable "${zone}"
99
100 if isset STP_FORWARD_DELAY; then
101 stp_bridge_set_forward_delay "${zone}" "${STP_FORWARD_DELAY}"
102 fi
103
104 if isset STP_HELLO; then
105 stp_bridge_set_hello_time "${zone}" "${STP_HELLO}"
106 fi
107
108 if isset STP_MAXAGE; then
109 stp_bridge_set_max_age "${zone}" "${STP_MAXAGE}"
110 fi
111
112 if isset STP_PRIORITY; then
113 stp_bridge_set_priority "${zone}" "${STP_PRIORITY}"
114 fi
115 else
116 stp_disable "${zone}"
117 fi
118
119 device_set_up "${zone}"
120
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
124
125 # Bring up all ports
126 zone_ports_create "${zone}"
127 zone_ports_up "${zone}"
128
129 # Bring up all configurations
130 zone_configs_up "${zone}"
131
132 exit ${EXIT_OK}
133 }
134
135 hook_hotplug() {
136 local zone="${1}"
137 assert isset zone
138
139 case "$(hotplug_action)" in
140 add)
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
145 zone_start "${zone}"
146 fi
147
148 hook_port_up "${zone}" "${INTERFACE}"
149 fi
150 ;;
151 remove)
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}"
155 fi
156 ;;
157 *)
158 exit ${EXIT_NOT_HANDLED}
159 ;;
160 esac
161
162 exit ${EXIT_OK}
163 }
164
165 hook_down() {
166 local zone="${1}"
167 assert isset zone
168
169 if ! device_is_up "${zone}"; then
170 warning "Zone '${zone}' is not up"
171 exit ${EXIT_OK}
172 fi
173
174 # Stop all the configs.
175 zone_configs_down "${zone}"
176
177 # Bring down all the ports.
178 zone_ports_down "${zone}"
179 zone_ports_remove "${zone}"
180
181 # Remove the bridge.
182 device_set_down "${zone}"
183 bridge_delete "${zone}"
184
185 exit ${EXIT_OK}
186 }
187
188 hook_status() {
189 local zone="${1}"
190 assert isset zone
191
192 # Print the default header.
193 cli_device_headline "${zone}"
194
195 # Exit if zone is down
196 if ! zone_is_up "${zone}"; then
197 echo # Empty line
198 exit ${EXIT_ERROR}
199 fi
200
201 cli_headline 2 "Spanning Tree Protocol information"
202 if stp_is_enabled "${zone}"; then
203 local proto=$(stp_bridge_get_protocol ${zone})
204
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})"
208
209 if stp_bridge_is_root ${zone}; then
210 cli_print 2 "This bridge is root."
211 else
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})"
216 fi
217 cli_space
218
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})"
226 cli_space
227 else
228 cli_print 2 "Disabled"
229 cli_space
230 fi
231
232 cli_headline 2 "Ports"
233 zone_ports_status "${zone}"
234 cli_space
235
236 cli_headline 2 "Configurations"
237 zone_configs_cmd status "${zone}"
238 cli_space
239
240 exit ${EXIT_OK}
241 }
242
243 hook_check_port_settings() {
244 if isset COST; then
245 assert isinteger COST
246 fi
247
248 if isset PRIORITY; then
249 assert isinteger PRIORITY
250 fi
251 }
252
253 hook_port_attach() {
254 # Excepting at least two arguments here
255 assert [ $# -ge 2 ]
256
257 local zone="${1}"
258 local port="${2}"
259 shift 2
260
261 if zone_has_port "${zone}" "${port}"; then
262 zone_port_settings_read "${zone}" "${port}"
263 fi
264
265 local arg
266 local val
267 while read arg; do
268 case "${arg}" in
269 --cost=*)
270 COST="$(cli_get_val "${arg}")"
271 ;;
272 --priority=*)
273 PRIORITY="$(cli_get_val "${arg}")"
274 ;;
275 esac
276 done <<< "$(args $@)"
277
278 if ! zone_port_settings_write "${zone}" "${port}"; then
279 exit ${EXIT_ERROR}
280 fi
281
282 exit ${EXIT_OK}
283 }
284
285 hook_port_detach() {
286 assert [ $# -eq 2 ]
287
288 local zone="${1}"
289 local port="${2}"
290
291 # Shut down the port (if possible)
292 port_down "${port}"
293
294 if ! zone_port_settings_remove "${zone}" "${port}"; then
295 exit ${EXIT_ERROR}
296 fi
297
298 exit ${EXIT_OK}
299 }
300
301 hook_port_edit() {
302 hook_port_attach $@
303 }
304
305 hook_port_up() {
306 assert [ $# -eq 2 ]
307
308 local zone="${1}"
309 local port="${2}"
310
311 # Try bringing up the port if it has not been
312 # brought up before.
313 # We will get here as soon as the port device has
314 # been created and will then connect it with the bridge.
315 if ! device_exists "${port}"; then
316 port_up "${port}"
317
318 exit ${EXIT_OK}
319 fi
320
321 # Read configuration values
322 zone_port_settings_read "${zone}" "${port}" ${HOOK_PORT_SETTINGS}
323
324 # Attach the port to the bridge
325 bridge_attach_device "${zone}" "${port}"
326
327 # Set STP configuration
328 if isset COST; then
329 stp_port_set_cost "${zone}" "${port}" "${COST}"
330 fi
331
332 # TODO Apply priority (#10609)
333
334 # Make sure that the port is up
335 port_up "${port}"
336
337 exit ${EXIT_OK}
338 }
339
340 hook_port_down() {
341 assert [ $# -eq 2 ]
342
343 local zone="${1}"
344 local port="${2}"
345
346 if device_exists "${port}"; then
347 bridge_detach_device "${zone}" "${port}"
348
349 port_down "${port}"
350 fi
351
352 exit ${EXIT_OK}
353 }
354
355 hook_port_status() {
356 assert [ $# -eq 2 ]
357
358 local zone="${1}"
359 local port="${2}"
360
361 # Do nothing for devices which are not up and running.
362 device_exists "${port}" || exit ${EXIT_OK}
363
364 local status
365
366 # Check if the device is down.
367 if ! device_is_up "${port}"; then
368 status="${MSG_DEVICE_STATUS_DOWN}"
369
370 # Check if the device has no carrier.
371 elif ! device_has_carrier "${port}"; then
372 status="${MSG_DEVICE_STATUS_NOCARRIER}"
373
374 # Check for STP information.
375 elif stp_is_enabled "${zone}"; then
376 local state="$(stp_port_get_state "${zone}" "${port}")"
377 state="MSG_STP_${state}"
378 status="${!state}"
379
380 status="${status} - DSR: $(stp_port_get_designated_root "${zone}" "${port}")"
381 status="${status} - Cost: $(stp_port_get_cost "${zone}" "${port}")"
382 else
383 status="${MSG_DEVICE_STATUS_UP}"
384 fi
385 cli_statusline 3 "${port}" "${status}"
386
387 exit ${EXIT_OK}
388 }