]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ports
Remove some unused code
[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() {
ed75c16d 159 local hook="${1}"
711ffac1
MT
160 shift
161
ed75c16d 162 if ! hook_exists port "${hook}"; then
711ffac1
MT
163 error "Port hook '${hook}' does not exist."
164 return ${EXIT_ERROR}
165 fi
166
ed75c16d 167 hook_exec port "${hook}" new $@
711ffac1
MT
168}
169
1c6a4e30 170port_destroy() {
711ffac1
MT
171 local port=${1}
172
173 assert isset port
174
175 port_exists ${port} || return ${EXIT_OK}
176
98f4dae6
MT
177 # Check if the port is attached to any zone and don't delete it.
178 local ok=${EXIT_OK}
711ffac1 179
98f4dae6 180 local attached_zone=$(port_is_attached ${port})
711ffac1 181 if [ -n "${attached_zone}" ]; then
98f4dae6
MT
182 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
183 ok=${EXIT_ERROR}
184 fi
185
186 # Check if the port is linked to any other port and don't allow the user
187 # to delete it.
188 local other_port
189 for other_port in $(ports_get); do
190 [ "${other_port}" = "${port}" ] && continue
191
192 if listmatch ${port} $(port_get_parents ${other_port}); then
193 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
194 ok=${EXIT_ERROR}
195 fi
196
197 if listmatch ${port} $(port_get_children ${other_port}); then
198 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
199 ok=${EXIT_ERROR}
200 fi
201 done
202
203 # If ok says we are not okay --> exit
204 if [ ${ok} -ne ${EXIT_OK} ]; then
711ffac1
MT
205 return ${EXIT_ERROR}
206 fi
207
510d8184 208 port_remove "${port}"
711ffac1
MT
209
210 rm -f $(port_file ${port})
211}
212
1c6a4e30 213port_create() {
1ba6a2bb
MT
214 port_cmd "create" $@
215}
216
1c6a4e30 217port_remove() {
1ba6a2bb
MT
218 local port="${1}"
219 assert isset port
220
221 # If the device is still up, we need to bring it down first.
222 if device_is_up "${port}"; then
223 port_down "${port}"
224 fi
225
226 port_cmd "remove" "${port}"
f90e550b
MT
227}
228
1c6a4e30 229port_edit() {
711ffac1
MT
230 port_cmd edit $@
231}
232
1c6a4e30 233port_up() {
711ffac1
MT
234 port_cmd up $@
235}
236
1c6a4e30 237port_down() {
711ffac1
MT
238 port_cmd down $@
239}
240
1c6a4e30 241port_status() {
711ffac1
MT
242 port_cmd status $@
243}
244
1c6a4e30 245port_info() {
98f4dae6
MT
246 port_cmd info $@
247}
248
1c6a4e30 249port_cmd() {
711ffac1
MT
250 local cmd=${1}
251 local port=${2}
252 shift 2
253
254 assert isset cmd
255 assert isset port
256
257 local hook=$(port_get_hook ${port})
258
259 assert isset hook
260
261 hook_exec port ${hook} ${cmd} ${port} $@
262}
f90e550b 263
1c6a4e30 264ports_get() {
f90e550b
MT
265 local port
266 for port in $(port_dir)/*; do
267 port=$(basename ${port})
268 if port_exists ${port}; then
269 echo "${port}"
270 fi
271 done
272}
2ae0fb8d 273
1c6a4e30 274port_find_free() {
d76f5107
MT
275 local pattern=${1}
276
277 assert isset pattern
278
279 local port
280 local i=0
281
282 while [ ${i} -lt 99 ]; do
283 port=${pattern//N/${i}}
284 if ! port_exists ${port} && ! device_exists ${port}; then
285 echo "${port}"
a1a8f0f4 286 return ${EXIT_OK}
d76f5107
MT
287 fi
288 i=$(( ${i} + 1 ))
289 done
a1a8f0f4
MT
290
291 return ${EXIT_ERROR}
d76f5107 292}
98f4dae6 293
1c6a4e30 294port_get_info() {
98f4dae6
MT
295 local port=${1}
296 local key=${2}
297
298 assert isset port
299 assert port_exists ${port}
300 assert isset key
301
302 (
303 eval $(port_info ${port})
304 echo "${!key}"
305 )
306}
307
1c6a4e30 308port_get_parents() {
98f4dae6
MT
309 local port=${1}
310
311 port_get_info ${port} PORT_PARENTS
312}
313
1c6a4e30 314port_get_children() {
98f4dae6
MT
315 local port=${1}
316
317 port_get_info ${port} PORT_CHILDREN
318}
3a7fef62 319
1c6a4e30 320port_zone() {
3a7fef62
MT
321 # Get name of the zones, this port is configured in.
322 local port=${1}
323 shift
324
325 assert isset port
326
327 local zone
328 for zone in $(zones_get_all); do
329 if zone_has_port ${zone} ${port}; then
330 echo "${zone}"
331 return ${EXIT_OK}
332 fi
333 done
334
335 return ${EXIT_OK}
336}
b8026986 337
1c6a4e30 338port_hotplug_event() {
b8026986
MT
339 local port="${1}"
340 assert isset port
341
342 hotplug_assert_in_hotplug_event
343
344 port_cmd "hotplug" "${port}"
345}
346
1c6a4e30 347port_get_slaves() {
b8026986
MT
348 local port="${1}"
349
350 port_settings_read "${port}" \
351 --ignore-superfluous-settings SLAVES
352 print "${SLAVES}"
353}
354
1c6a4e30 355port_device_is_slave() {
b8026986
MT
356 assert [ $# -eq 2 ]
357
358 local port="${1}"
359 local device="${2}"
360
361 # Get slaves of port
362 local slaves="$(port_get_slaves "${port}")"
363
364 # Returns true if device is in slaves
365 list_match "${device}" ${slaves}
366}
367
1c6a4e30 368port_get_phy() {
b8026986
MT
369 local port="${1}"
370
371 port_settings_read "${port}" \
372 --ignore-superfluous-settings PHY
373 print "${PHY}"
374}
375
1c6a4e30 376port_uses_phy() {
b8026986
MT
377 assert [ $# -eq 2 ]
378
379 local port="${1}"
380 local phy="${2}"
381
382 # Nothing to do if an empty argument is given
383 if ! isset phy; then
384 return ${EXIT_FALSE}
385 fi
386
387 phy="$(phy_get_address "${phy}")"
388
389 local port_phy="$(port_get_phy "${port}")"
390 [ "${port_phy}" = "${phy}" ]
391}
7ad5252c 392
1c6a4e30 393ports_lowest_address() {
7ad5252c
MT
394 local address
395 local addresses
396
397 local port
398 for port in $(port_list); do
399 # Skip all ports that do not exist
400 # any more or are not plugged in
401 device_exists "${port}" || continue
402
403 # Skip all ports that are not proper ethernet devices
404 device_is_wireless "${port}" && continue
405 device_is_ethernet "${port}" || continue
406
407 list_append addresses "$(device_get_address "${port}")"
408 done
409
410 # Sort the list
411 addresses="$(list_sort ${addresses})"
412
413 # Get the first element which is the lowest MAC address
414 list_head ${addresses}
415}