]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ports
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / functions / functions.ports
CommitLineData
711ffac1 1#!/bin/bash
1578dae9
MT
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###############################################################################
711ffac1 21
8707df4b
MT
22ports_get_all() {
23 local port
24 for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
25 if port_exists "${port}"; then
26 print "${port}"
27 fi
28 done
29}
30
31# XXX TO BE REMOVED
1c6a4e30 32port_list() {
8707df4b 33 ports_get_all "$@"
bae37360
MT
34}
35
1c6a4e30 36port_list_in_use() {
bae37360
MT
37 local ports_in_use
38
39 # Collect all ports that are attached to a zone
40 local zone
41 for zone in $(zones_get_all); do
42 list_append ports_in_use $(zone_get_ports "${zone}")
43 done
44
45 # Collect all ports that are enslaved by an other port
46 local port
47 for port in $(port_list); do
48 list_append ports_in_use $(port_get_slaves "${port}")
49 done
50
51 list_sort ${ports_in_use}
52}
53
1c6a4e30 54port_list_free() {
bae37360
MT
55 local ports_in_use="$(port_list_in_use)"
56
57 local port
58 for port in $(port_list); do
59 if ! list_match "${port}" ${ports_in_use}; then
60 print "${port}"
61 fi
62 done
63
64 return ${EXIT_OK}
65}
66
1c6a4e30 67port_get_hook() {
49ec20d8
MT
68 local port=${1}
69 assert isset port
70
71 config_get_hook $(port_file ${port})
72}
73
1c6a4e30 74port_config_dir() {
49ec20d8
MT
75 local port=${1}
76
77 print "${RUN_DIR}/ports/${port}"
78 return ${EXIT_OK}
79}
80
1c6a4e30 81port_settings_read() {
e9df08ad 82 local port="${1}"
49ec20d8
MT
83 assert isset port
84
85 # Save the HOOK variable.
86 local hook="${HOOK}"
87
d389e96b 88 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS[*]}
49ec20d8
MT
89
90 # Restore hook.
91 HOOK="${hook}"
92}
93
1c6a4e30 94port_settings_write() {
e9df08ad 95 local port="${1}"
49ec20d8 96 assert isset port
e9df08ad 97 shift
49ec20d8 98
1e6f187e
MT
99 local args
100 if function_exists "hook_check_settings"; then
101 list_append args "--check=\"hook_check_settings\""
102 fi
d389e96b 103 list_append args HOOK ${HOOK_SETTINGS[*]}
1e6f187e
MT
104
105 settings_write "$(port_file "${port}")" ${args}
49ec20d8
MT
106}
107
1c6a4e30 108port_file() {
e9df08ad 109 local port="${1}"
711ffac1
MT
110 assert isset port
111
0d994c19 112 echo "${NETWORK_PORTS_DIR}/${port}/settings"
711ffac1
MT
113}
114
1c6a4e30 115port_exists() {
711ffac1
MT
116 local port=${1}
117
af51d3a9 118 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
119}
120
1c6a4e30 121port_get_hook() {
711ffac1
MT
122 local port=${1}
123
124 assert isset port
125
126 config_get_hook $(port_file ${port})
127}
128
1c6a4e30 129port_is_attached() {
711ffac1
MT
130 local port=${1}
131 shift
132
133 assert isset port
134
135 local zone
136 for zone in $(zones_get_all); do
8c9205b1 137 if list_match ${port} $(zone_get_ports ${zone}); then
711ffac1
MT
138 echo "${zone}"
139 return ${EXIT_OK}
140 fi
141 done
142
143 return ${EXIT_ERROR}
144}
145
1c6a4e30 146port_is_up() {
2212045f 147 device_is_up "$@"
abba34c1
MT
148}
149
1c6a4e30 150port_new() {
ed75c16d 151 local hook="${1}"
711ffac1
MT
152 shift
153
ed75c16d 154 if ! hook_exists port "${hook}"; then
711ffac1
MT
155 error "Port hook '${hook}' does not exist."
156 return ${EXIT_ERROR}
157 fi
158
2212045f 159 hook_exec port "${hook}" new "$@"
711ffac1
MT
160}
161
1c6a4e30 162port_destroy() {
711ffac1 163 local port=${1}
711ffac1
MT
164 assert isset port
165
ab5edf53
MT
166 # Cannot delete a port that does not exist
167 if ! port_exists ${port}; then
168 error "No such port: ${port}"
169 return ${EXIT_ERROR}
170 fi
711ffac1 171
98f4dae6 172 local attached_zone=$(port_is_attached ${port})
711ffac1 173 if [ -n "${attached_zone}" ]; then
27fff860
MT
174 if ! zone_port_detach "${attached_zone}" "${port}"; then
175 error "Could not remove port ${port} from zone ${zone}"
176 return ${EXIT_ERROR}
177 fi
98f4dae6
MT
178 fi
179
180 # Check if the port is linked to any other port and don't allow the user
181 # to delete it.
182 local other_port
183 for other_port in $(ports_get); do
184 [ "${other_port}" = "${port}" ] && continue
185
8c9205b1 186 if list_match ${port} $(port_get_children ${other_port}); then
7e2b4356
MT
187 log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
188 return ${EXIT_ERROR}
98f4dae6
MT
189 fi
190 done
191
54c790c2
MT
192 # Shut down the port before destroying it
193 if ! port_remove "${port}"; then
194 return ${EXIT_ERROR}
195 fi
711ffac1 196
8707df4b
MT
197 if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then
198 log ERROR "Could not destroy port ${port}"
199 return ${EXIT_ERROR}
200 fi
201
202 log INFO "Destroyed port ${port}"
203 return ${EXIT_OK}
711ffac1
MT
204}
205
1c6a4e30 206port_create() {
2212045f 207 port_cmd "create" "$@"
1ba6a2bb
MT
208}
209
1c6a4e30 210port_remove() {
1ba6a2bb
MT
211 local port="${1}"
212 assert isset port
213
270aab39
MT
214 if ! port_exists "${port}"; then
215 log ERROR "Port ${port} does not exist"
216 return ${EXIT_ERROR}
217 fi
218
1ba6a2bb
MT
219 # If the device is still up, we need to bring it down first.
220 if device_is_up "${port}"; then
221 port_down "${port}"
222 fi
223
224 port_cmd "remove" "${port}"
f90e550b
MT
225}
226
270aab39
MT
227# Restarts the port by removing it and then re-creating it
228port_restart() {
229 local port="${1}"
230 assert isset port
231
232 port_remove "${port}"
233
234 port_create "${port}"
235}
236
1c6a4e30 237port_edit() {
2212045f 238 port_cmd edit "$@"
711ffac1
MT
239}
240
1c6a4e30 241port_up() {
f28e991c
MT
242 assert [ $# -eq 1 ]
243
244 local port="${1}"
245
246 # Check if the port exists
247 if ! device_exists "${port}"; then
248 log ERROR "Could not bring up port ${port} which has not been created"
249 return ${EXIT_ERROR}
250 fi
251
252 port_cmd up "${port}"
711ffac1
MT
253}
254
1c6a4e30 255port_down() {
2212045f 256 port_cmd down "$@"
711ffac1
MT
257}
258
1c6a4e30 259port_status() {
2212045f 260 port_cmd status "$@"
711ffac1
MT
261}
262
1c6a4e30 263port_cmd() {
711ffac1
MT
264 local cmd=${1}
265 local port=${2}
266 shift 2
267
268 assert isset cmd
269 assert isset port
270
271 local hook=$(port_get_hook ${port})
272
0c949dd5
MT
273 # Abort if we could not find a hook
274 if ! isset hook; then
275 log CRITICAL "Port ${port} does not have a hook associated with it"
276 return ${EXIT_ERROR}
277 fi
711ffac1 278
2212045f 279 hook_exec port ${hook} ${cmd} ${port} "$@"
711ffac1 280}
f90e550b 281
1c6a4e30 282ports_get() {
f90e550b 283 local port
0d994c19 284 for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
f90e550b
MT
285 if port_exists ${port}; then
286 echo "${port}"
287 fi
288 done
289}
2ae0fb8d 290
1c6a4e30 291port_find_free() {
d76f5107
MT
292 local pattern=${1}
293
294 assert isset pattern
295
296 local port
297 local i=0
298
299 while [ ${i} -lt 99 ]; do
300 port=${pattern//N/${i}}
301 if ! port_exists ${port} && ! device_exists ${port}; then
302 echo "${port}"
a1a8f0f4 303 return ${EXIT_OK}
d76f5107
MT
304 fi
305 i=$(( ${i} + 1 ))
306 done
a1a8f0f4
MT
307
308 return ${EXIT_ERROR}
d76f5107 309}
98f4dae6 310
1c6a4e30 311port_get_children() {
98f4dae6
MT
312 local port=${1}
313
7da8cf02
MT
314 assert port_exists "${port}"
315
316 port_cmd "children" "${port}"
98f4dae6 317}
3a7fef62 318
1c6a4e30 319port_zone() {
3a7fef62
MT
320 # Get name of the zones, this port is configured in.
321 local port=${1}
322 shift
323
324 assert isset port
325
326 local zone
327 for zone in $(zones_get_all); do
328 if zone_has_port ${zone} ${port}; then
329 echo "${zone}"
330 return ${EXIT_OK}
331 fi
332 done
333
334 return ${EXIT_OK}
335}
b8026986 336
1c6a4e30 337port_hotplug_event() {
b8026986
MT
338 local port="${1}"
339 assert isset port
340
341 hotplug_assert_in_hotplug_event
342
343 port_cmd "hotplug" "${port}"
344}
345
1c6a4e30 346port_get_slaves() {
b8026986
MT
347 local port="${1}"
348
349 port_settings_read "${port}" \
350 --ignore-superfluous-settings SLAVES
351 print "${SLAVES}"
352}
353
1c6a4e30 354port_device_is_slave() {
b8026986
MT
355 assert [ $# -eq 2 ]
356
357 local port="${1}"
358 local device="${2}"
359
360 # Get slaves of port
361 local slaves="$(port_get_slaves "${port}")"
362
363 # Returns true if device is in slaves
364 list_match "${device}" ${slaves}
365}
366
1c6a4e30 367port_get_phy() {
b8026986
MT
368 local port="${1}"
369
370 port_settings_read "${port}" \
371 --ignore-superfluous-settings PHY
372 print "${PHY}"
373}
374
1c6a4e30 375port_uses_phy() {
b8026986
MT
376 assert [ $# -eq 2 ]
377
378 local port="${1}"
379 local phy="${2}"
380
381 # Nothing to do if an empty argument is given
382 if ! isset phy; then
383 return ${EXIT_FALSE}
384 fi
385
386 phy="$(phy_get_address "${phy}")"
387
388 local port_phy="$(port_get_phy "${port}")"
389 [ "${port_phy}" = "${phy}" ]
390}
7ad5252c 391
1c6a4e30 392ports_lowest_address() {
7ad5252c
MT
393 local address
394 local addresses
395
396 local port
397 for port in $(port_list); do
398 # Skip all ports that do not exist
399 # any more or are not plugged in
400 device_exists "${port}" || continue
401
402 # Skip all ports that are not proper ethernet devices
403 device_is_wireless "${port}" && continue
404 device_is_ethernet "${port}" || continue
405
406 list_append addresses "$(device_get_address "${port}")"
407 done
408
409 # Sort the list
410 addresses="$(list_sort ${addresses})"
411
412 # Get the first element which is the lowest MAC address
413 list_head ${addresses}
414}
f5ee091e
MT
415
416port_identify() {
2212045f 417 device_identify "$@"
f5ee091e 418}
410d2e85
JS
419
420port_get_color() {
421 # This function return the color of a port
422 assert [ $# -eq 1 ]
423
424 local name=${1}
425 color_read "port" ${name}
426}
5de34b4c
JS
427
428port_get_description_title() {
429 assert [ $# -eq 1 ]
430
431 local name=${1}
432 description_title_read $(description_format_filename "port" "${name}")
433}