]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ports
Remove the function keyword which is a bashism
[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
1c6a4e30 22port_dir() {
84f3bd05 23 echo "${NETWORK_CONFIG_DIR}/ports"
711ffac1
MT
24}
25
1c6a4e30 26port_list() {
bae37360
MT
27 local port
28 for port in $(port_dir)/*; do
29 port="$(basename "${port}")"
30 if port_exists "${port}"; then
31 print "${port}"
32 fi
33 done
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
1e6f187e 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
103 list_append args ${HOOK_SETTINGS}
104
105 settings_write "$(port_file "${port}")" ${args}
49ec20d8
MT
106}
107
1c6a4e30 108ports_get_all() {
bae37360 109 port_list
8895cf8f
MT
110}
111
1c6a4e30 112port_file() {
e9df08ad 113 local port="${1}"
711ffac1
MT
114 assert isset port
115
116 echo "$(port_dir)/${port}"
117}
118
1c6a4e30 119port_exists() {
711ffac1
MT
120 local port=${1}
121
84f3bd05 122 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
123}
124
1c6a4e30 125port_get_hook() {
711ffac1
MT
126 local port=${1}
127
128 assert isset port
129
130 config_get_hook $(port_file ${port})
131}
132
1c6a4e30 133port_is_attached() {
711ffac1
MT
134 local port=${1}
135 shift
136
137 assert isset port
138
139 local zone
140 for zone in $(zones_get_all); do
141
142 assert isset zone
143 assert zone_exists ${zone}
144
145 if listmatch ${port} $(zone_get_ports ${zone}); then
146 echo "${zone}"
147 return ${EXIT_OK}
148 fi
149 done
150
151 return ${EXIT_ERROR}
152}
153
1c6a4e30 154port_is_up() {
abba34c1
MT
155 device_is_up $@
156}
157
1c6a4e30 158port_new() {
711ffac1
MT
159 #local port=${1}
160 #shift
161 #
162 #if port_exists ${port}; then
163 # error "Port '${port}' does already exist."
164 # return ${EXIT_ERROR}
165 #fi
166
167 local hook=${1}
168 shift
169
170 if ! hook_exists port ${hook}; then
171 error "Port hook '${hook}' does not exist."
172 return ${EXIT_ERROR}
173 fi
174
175 #port_edit ${port} ${hook} $@
176 #
177 #if [ $? -ne ${EXIT_OK} ]; then
178 # port_destroy ${port}
179 #fi
180
1ba6a2bb 181 hook_exec port ${hook} new $@
711ffac1
MT
182}
183
1c6a4e30 184port_destroy() {
711ffac1
MT
185 local port=${1}
186
187 assert isset port
188
189 port_exists ${port} || return ${EXIT_OK}
190
98f4dae6
MT
191 # Check if the port is attached to any zone and don't delete it.
192 local ok=${EXIT_OK}
711ffac1 193
98f4dae6 194 local attached_zone=$(port_is_attached ${port})
711ffac1 195 if [ -n "${attached_zone}" ]; then
98f4dae6
MT
196 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
197 ok=${EXIT_ERROR}
198 fi
199
200 # Check if the port is linked to any other port and don't allow the user
201 # to delete it.
202 local other_port
203 for other_port in $(ports_get); do
204 [ "${other_port}" = "${port}" ] && continue
205
206 if listmatch ${port} $(port_get_parents ${other_port}); then
207 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
208 ok=${EXIT_ERROR}
209 fi
210
211 if listmatch ${port} $(port_get_children ${other_port}); then
212 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
213 ok=${EXIT_ERROR}
214 fi
215 done
216
217 # If ok says we are not okay --> exit
218 if [ ${ok} -ne ${EXIT_OK} ]; then
711ffac1
MT
219 return ${EXIT_ERROR}
220 fi
221
510d8184 222 port_remove "${port}"
711ffac1
MT
223
224 rm -f $(port_file ${port})
225}
226
1c6a4e30 227port_create() {
1ba6a2bb
MT
228 port_cmd "create" $@
229}
230
1c6a4e30 231port_remove() {
1ba6a2bb
MT
232 local port="${1}"
233 assert isset port
234
235 # If the device is still up, we need to bring it down first.
236 if device_is_up "${port}"; then
237 port_down "${port}"
238 fi
239
240 port_cmd "remove" "${port}"
f90e550b
MT
241}
242
1c6a4e30 243port_edit() {
711ffac1
MT
244 port_cmd edit $@
245}
246
1c6a4e30 247port_up() {
711ffac1
MT
248 port_cmd up $@
249}
250
1c6a4e30 251port_down() {
711ffac1
MT
252 port_cmd down $@
253}
254
1c6a4e30 255port_status() {
711ffac1
MT
256 port_cmd status $@
257}
258
1c6a4e30 259port_info() {
98f4dae6
MT
260 port_cmd info $@
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
273 assert isset hook
274
275 hook_exec port ${hook} ${cmd} ${port} $@
276}
f90e550b 277
1c6a4e30 278ports_get() {
f90e550b
MT
279 local port
280 for port in $(port_dir)/*; do
281 port=$(basename ${port})
282 if port_exists ${port}; then
283 echo "${port}"
284 fi
285 done
286}
2ae0fb8d 287
1c6a4e30 288port_find_free() {
d76f5107
MT
289 local pattern=${1}
290
291 assert isset pattern
292
293 local port
294 local i=0
295
296 while [ ${i} -lt 99 ]; do
297 port=${pattern//N/${i}}
298 if ! port_exists ${port} && ! device_exists ${port}; then
299 echo "${port}"
a1a8f0f4 300 return ${EXIT_OK}
d76f5107
MT
301 fi
302 i=$(( ${i} + 1 ))
303 done
a1a8f0f4
MT
304
305 return ${EXIT_ERROR}
d76f5107 306}
98f4dae6 307
1c6a4e30 308port_get_info() {
98f4dae6
MT
309 local port=${1}
310 local key=${2}
311
312 assert isset port
313 assert port_exists ${port}
314 assert isset key
315
316 (
317 eval $(port_info ${port})
318 echo "${!key}"
319 )
320}
321
1c6a4e30 322port_get_parents() {
98f4dae6
MT
323 local port=${1}
324
325 port_get_info ${port} PORT_PARENTS
326}
327
1c6a4e30 328port_get_children() {
98f4dae6
MT
329 local port=${1}
330
331 port_get_info ${port} PORT_CHILDREN
332}
3a7fef62 333
1c6a4e30 334port_zone() {
3a7fef62
MT
335 # Get name of the zones, this port is configured in.
336 local port=${1}
337 shift
338
339 assert isset port
340
341 local zone
342 for zone in $(zones_get_all); do
343 if zone_has_port ${zone} ${port}; then
344 echo "${zone}"
345 return ${EXIT_OK}
346 fi
347 done
348
349 return ${EXIT_OK}
350}
b8026986 351
1c6a4e30 352port_hotplug_event() {
b8026986
MT
353 local port="${1}"
354 assert isset port
355
356 hotplug_assert_in_hotplug_event
357
358 port_cmd "hotplug" "${port}"
359}
360
1c6a4e30 361port_get_slaves() {
b8026986
MT
362 local port="${1}"
363
364 port_settings_read "${port}" \
365 --ignore-superfluous-settings SLAVES
366 print "${SLAVES}"
367}
368
1c6a4e30 369port_device_is_slave() {
b8026986
MT
370 assert [ $# -eq 2 ]
371
372 local port="${1}"
373 local device="${2}"
374
375 # Get slaves of port
376 local slaves="$(port_get_slaves "${port}")"
377
378 # Returns true if device is in slaves
379 list_match "${device}" ${slaves}
380}
381
1c6a4e30 382port_get_phy() {
b8026986
MT
383 local port="${1}"
384
385 port_settings_read "${port}" \
386 --ignore-superfluous-settings PHY
387 print "${PHY}"
388}
389
1c6a4e30 390port_uses_phy() {
b8026986
MT
391 assert [ $# -eq 2 ]
392
393 local port="${1}"
394 local phy="${2}"
395
396 # Nothing to do if an empty argument is given
397 if ! isset phy; then
398 return ${EXIT_FALSE}
399 fi
400
401 phy="$(phy_get_address "${phy}")"
402
403 local port_phy="$(port_get_phy "${port}")"
404 [ "${port_phy}" = "${phy}" ]
405}
7ad5252c 406
1c6a4e30 407ports_lowest_address() {
7ad5252c
MT
408 local address
409 local addresses
410
411 local port
412 for port in $(port_list); do
413 # Skip all ports that do not exist
414 # any more or are not plugged in
415 device_exists "${port}" || continue
416
417 # Skip all ports that are not proper ethernet devices
418 device_is_wireless "${port}" && continue
419 device_is_ethernet "${port}" || continue
420
421 list_append addresses "$(device_get_address "${port}")"
422 done
423
424 # Sort the list
425 addresses="$(list_sort ${addresses})"
426
427 # Get the first element which is the lowest MAC address
428 list_head ${addresses}
429}