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