]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.ports
reset: Remove all ports instead of just bringing them down
[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
MT
21
22function port_dir() {
84f3bd05 23 echo "${NETWORK_CONFIG_DIR}/ports"
711ffac1
MT
24}
25
bae37360
MT
26function port_list() {
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
36function port_list_in_use() {
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
54function port_list_free() {
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
49ec20d8
MT
67function port_get_hook() {
68 local port=${1}
69 assert isset port
70
71 config_get_hook $(port_file ${port})
72}
73
74function port_config_dir() {
75 local port=${1}
76
77 print "${RUN_DIR}/ports/${port}"
78 return ${EXIT_OK}
79}
80
e9df08ad
MT
81function port_settings_read() {
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
e9df08ad
MT
94function port_settings_write() {
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
8895cf8f 108function ports_get_all() {
bae37360 109 port_list
8895cf8f
MT
110}
111
711ffac1 112function port_file() {
e9df08ad 113 local port="${1}"
711ffac1
MT
114 assert isset port
115
116 echo "$(port_dir)/${port}"
117}
118
119function port_exists() {
120 local port=${1}
121
84f3bd05 122 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
123}
124
125function port_get_hook() {
126 local port=${1}
127
128 assert isset port
129
130 config_get_hook $(port_file ${port})
131}
132
133function port_is_attached() {
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
1ba6a2bb 154function port_new() {
711ffac1
MT
155 #local port=${1}
156 #shift
157 #
158 #if port_exists ${port}; then
159 # error "Port '${port}' does already exist."
160 # return ${EXIT_ERROR}
161 #fi
162
163 local hook=${1}
164 shift
165
166 if ! hook_exists port ${hook}; then
167 error "Port hook '${hook}' does not exist."
168 return ${EXIT_ERROR}
169 fi
170
171 #port_edit ${port} ${hook} $@
172 #
173 #if [ $? -ne ${EXIT_OK} ]; then
174 # port_destroy ${port}
175 #fi
176
1ba6a2bb 177 hook_exec port ${hook} new $@
711ffac1
MT
178}
179
180function port_destroy() {
181 local port=${1}
182
183 assert isset port
184
185 port_exists ${port} || return ${EXIT_OK}
186
98f4dae6
MT
187 # Check if the port is attached to any zone and don't delete it.
188 local ok=${EXIT_OK}
711ffac1 189
98f4dae6 190 local attached_zone=$(port_is_attached ${port})
711ffac1 191 if [ -n "${attached_zone}" ]; then
98f4dae6
MT
192 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
193 ok=${EXIT_ERROR}
194 fi
195
196 # Check if the port is linked to any other port and don't allow the user
197 # to delete it.
198 local other_port
199 for other_port in $(ports_get); do
200 [ "${other_port}" = "${port}" ] && continue
201
202 if listmatch ${port} $(port_get_parents ${other_port}); then
203 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
204 ok=${EXIT_ERROR}
205 fi
206
207 if listmatch ${port} $(port_get_children ${other_port}); then
208 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
209 ok=${EXIT_ERROR}
210 fi
211 done
212
213 # If ok says we are not okay --> exit
214 if [ ${ok} -ne ${EXIT_OK} ]; then
711ffac1
MT
215 return ${EXIT_ERROR}
216 fi
217
510d8184 218 port_remove "${port}"
711ffac1
MT
219
220 rm -f $(port_file ${port})
221}
222
1ba6a2bb
MT
223function port_create() {
224 port_cmd "create" $@
225}
226
f90e550b 227function port_remove() {
1ba6a2bb
MT
228 local port="${1}"
229 assert isset port
230
231 # If the device is still up, we need to bring it down first.
232 if device_is_up "${port}"; then
233 port_down "${port}"
234 fi
235
236 port_cmd "remove" "${port}"
f90e550b
MT
237}
238
711ffac1
MT
239function port_edit() {
240 port_cmd edit $@
241}
242
711ffac1
MT
243function port_up() {
244 port_cmd up $@
245}
246
247function port_down() {
248 port_cmd down $@
249}
250
251function port_status() {
252 port_cmd status $@
253}
254
98f4dae6
MT
255function port_info() {
256 port_cmd info $@
257}
258
711ffac1
MT
259function port_cmd() {
260 local cmd=${1}
261 local port=${2}
262 shift 2
263
264 assert isset cmd
265 assert isset port
266
267 local hook=$(port_get_hook ${port})
268
269 assert isset hook
270
271 hook_exec port ${hook} ${cmd} ${port} $@
272}
f90e550b
MT
273
274function ports_get() {
275 local port
276 for port in $(port_dir)/*; do
277 port=$(basename ${port})
278 if port_exists ${port}; then
279 echo "${port}"
280 fi
281 done
282}
2ae0fb8d 283
d76f5107
MT
284function port_find_free() {
285 local pattern=${1}
286
287 assert isset pattern
288
289 local port
290 local i=0
291
292 while [ ${i} -lt 99 ]; do
293 port=${pattern//N/${i}}
294 if ! port_exists ${port} && ! device_exists ${port}; then
295 echo "${port}"
a1a8f0f4 296 return ${EXIT_OK}
d76f5107
MT
297 fi
298 i=$(( ${i} + 1 ))
299 done
a1a8f0f4
MT
300
301 return ${EXIT_ERROR}
d76f5107 302}
98f4dae6
MT
303
304function port_get_info() {
305 local port=${1}
306 local key=${2}
307
308 assert isset port
309 assert port_exists ${port}
310 assert isset key
311
312 (
313 eval $(port_info ${port})
314 echo "${!key}"
315 )
316}
317
318function port_get_parents() {
319 local port=${1}
320
321 port_get_info ${port} PORT_PARENTS
322}
323
324function port_get_children() {
325 local port=${1}
326
327 port_get_info ${port} PORT_CHILDREN
328}
3a7fef62
MT
329
330function port_zone() {
331 # Get name of the zones, this port is configured in.
332 local port=${1}
333 shift
334
335 assert isset port
336
337 local zone
338 for zone in $(zones_get_all); do
339 if zone_has_port ${zone} ${port}; then
340 echo "${zone}"
341 return ${EXIT_OK}
342 fi
343 done
344
345 return ${EXIT_OK}
346}
b8026986
MT
347
348function port_hotplug_event() {
349 local port="${1}"
350 assert isset port
351
352 hotplug_assert_in_hotplug_event
353
354 port_cmd "hotplug" "${port}"
355}
356
357function port_get_slaves() {
358 local port="${1}"
359
360 port_settings_read "${port}" \
361 --ignore-superfluous-settings SLAVES
362 print "${SLAVES}"
363}
364
365function port_device_is_slave() {
366 assert [ $# -eq 2 ]
367
368 local port="${1}"
369 local device="${2}"
370
371 # Get slaves of port
372 local slaves="$(port_get_slaves "${port}")"
373
374 # Returns true if device is in slaves
375 list_match "${device}" ${slaves}
376}
377
378function port_get_phy() {
379 local port="${1}"
380
381 port_settings_read "${port}" \
382 --ignore-superfluous-settings PHY
383 print "${PHY}"
384}
385
386function port_uses_phy() {
387 assert [ $# -eq 2 ]
388
389 local port="${1}"
390 local phy="${2}"
391
392 # Nothing to do if an empty argument is given
393 if ! isset phy; then
394 return ${EXIT_FALSE}
395 fi
396
397 phy="$(phy_get_address "${phy}")"
398
399 local port_phy="$(port_get_phy "${port}")"
400 [ "${port_phy}" = "${phy}" ]
401}