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