]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ports
Fix hook settings writing and checking
[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
MT
21
22function port_dir() {
84f3bd05 23 echo "${NETWORK_CONFIG_DIR}/ports"
711ffac1
MT
24}
25
49ec20d8
MT
26function port_get_hook() {
27 local port=${1}
28 assert isset port
29
30 config_get_hook $(port_file ${port})
31}
32
33function port_config_dir() {
34 local port=${1}
35
36 print "${RUN_DIR}/ports/${port}"
37 return ${EXIT_OK}
38}
39
e9df08ad
MT
40function port_settings_read() {
41 local port="${1}"
49ec20d8
MT
42 assert isset port
43
44 # Save the HOOK variable.
45 local hook="${HOOK}"
46
1e6f187e 47 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
49ec20d8
MT
48
49 # Restore hook.
50 HOOK="${hook}"
51}
52
e9df08ad
MT
53function port_settings_write() {
54 local port="${1}"
49ec20d8 55 assert isset port
e9df08ad 56 shift
49ec20d8 57
1e6f187e
MT
58 local args
59 if function_exists "hook_check_settings"; then
60 list_append args "--check=\"hook_check_settings\""
61 fi
62 list_append args ${HOOK_SETTINGS}
63
64 settings_write "$(port_file "${port}")" ${args}
49ec20d8
MT
65}
66
8895cf8f
MT
67function ports_get_all() {
68 local port
69
70 for port in $(port_dir)/*; do
71 [ -f "${port}" ] || continue
72
73 basename ${port}
74 done
75}
76
711ffac1 77function port_file() {
e9df08ad 78 local port="${1}"
711ffac1
MT
79 assert isset port
80
81 echo "$(port_dir)/${port}"
82}
83
84function port_exists() {
85 local port=${1}
86
84f3bd05 87 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
88}
89
90function port_get_hook() {
91 local port=${1}
92
93 assert isset port
94
95 config_get_hook $(port_file ${port})
96}
97
98function port_is_attached() {
99 local port=${1}
100 shift
101
102 assert isset port
103
104 local zone
105 for zone in $(zones_get_all); do
106
107 assert isset zone
108 assert zone_exists ${zone}
109
110 if listmatch ${port} $(zone_get_ports ${zone}); then
111 echo "${zone}"
112 return ${EXIT_OK}
113 fi
114 done
115
116 return ${EXIT_ERROR}
117}
118
1ba6a2bb 119function port_new() {
711ffac1
MT
120 #local port=${1}
121 #shift
122 #
123 #if port_exists ${port}; then
124 # error "Port '${port}' does already exist."
125 # return ${EXIT_ERROR}
126 #fi
127
128 local hook=${1}
129 shift
130
131 if ! hook_exists port ${hook}; then
132 error "Port hook '${hook}' does not exist."
133 return ${EXIT_ERROR}
134 fi
135
136 #port_edit ${port} ${hook} $@
137 #
138 #if [ $? -ne ${EXIT_OK} ]; then
139 # port_destroy ${port}
140 #fi
141
1ba6a2bb 142 hook_exec port ${hook} new $@
711ffac1
MT
143}
144
145function port_destroy() {
146 local port=${1}
147
148 assert isset port
149
150 port_exists ${port} || return ${EXIT_OK}
151
98f4dae6
MT
152 # Check if the port is attached to any zone and don't delete it.
153 local ok=${EXIT_OK}
711ffac1 154
98f4dae6 155 local attached_zone=$(port_is_attached ${port})
711ffac1 156 if [ -n "${attached_zone}" ]; then
98f4dae6
MT
157 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
158 ok=${EXIT_ERROR}
159 fi
160
161 # Check if the port is linked to any other port and don't allow the user
162 # to delete it.
163 local other_port
164 for other_port in $(ports_get); do
165 [ "${other_port}" = "${port}" ] && continue
166
167 if listmatch ${port} $(port_get_parents ${other_port}); then
168 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
169 ok=${EXIT_ERROR}
170 fi
171
172 if listmatch ${port} $(port_get_children ${other_port}); then
173 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
174 ok=${EXIT_ERROR}
175 fi
176 done
177
178 # If ok says we are not okay --> exit
179 if [ ${ok} -ne ${EXIT_OK} ]; then
711ffac1
MT
180 return ${EXIT_ERROR}
181 fi
182
183 port_down ${port}
184
185 rm -f $(port_file ${port})
186}
187
1ba6a2bb
MT
188function port_create() {
189 port_cmd "create" $@
190}
191
f90e550b 192function port_remove() {
1ba6a2bb
MT
193 local port="${1}"
194 assert isset port
195
196 # If the device is still up, we need to bring it down first.
197 if device_is_up "${port}"; then
198 port_down "${port}"
199 fi
200
201 port_cmd "remove" "${port}"
f90e550b
MT
202}
203
711ffac1
MT
204function port_edit() {
205 port_cmd edit $@
206}
207
208# XXX? Compatibility function
209function port_show() {
210 port_status $@
211}
212
213function port_up() {
214 port_cmd up $@
215}
216
217function port_down() {
218 port_cmd down $@
219}
220
221function port_status() {
222 port_cmd status $@
223}
224
98f4dae6
MT
225function port_info() {
226 port_cmd info $@
227}
228
711ffac1
MT
229function port_cmd() {
230 local cmd=${1}
231 local port=${2}
232 shift 2
233
234 assert isset cmd
235 assert isset port
236
237 local hook=$(port_get_hook ${port})
238
239 assert isset hook
240
241 hook_exec port ${hook} ${cmd} ${port} $@
242}
f90e550b
MT
243
244function ports_get() {
245 local port
246 for port in $(port_dir)/*; do
247 port=$(basename ${port})
248 if port_exists ${port}; then
249 echo "${port}"
250 fi
251 done
252}
2ae0fb8d 253
d76f5107
MT
254function port_find_free() {
255 local pattern=${1}
256
257 assert isset pattern
258
259 local port
260 local i=0
261
262 while [ ${i} -lt 99 ]; do
263 port=${pattern//N/${i}}
264 if ! port_exists ${port} && ! device_exists ${port}; then
265 echo "${port}"
a1a8f0f4 266 return ${EXIT_OK}
d76f5107
MT
267 fi
268 i=$(( ${i} + 1 ))
269 done
a1a8f0f4
MT
270
271 return ${EXIT_ERROR}
d76f5107 272}
98f4dae6
MT
273
274function port_get_info() {
275 local port=${1}
276 local key=${2}
277
278 assert isset port
279 assert port_exists ${port}
280 assert isset key
281
282 (
283 eval $(port_info ${port})
284 echo "${!key}"
285 )
286}
287
288function port_get_parents() {
289 local port=${1}
290
291 port_get_info ${port} PORT_PARENTS
292}
293
294function port_get_children() {
295 local port=${1}
296
297 port_get_info ${port} PORT_CHILDREN
298}
3a7fef62
MT
299
300function port_zone() {
301 # Get name of the zones, this port is configured in.
302 local port=${1}
303 shift
304
305 assert isset port
306
307 local zone
308 for zone in $(zones_get_all); do
309 if zone_has_port ${zone} ${port}; then
310 echo "${zone}"
311 return ${EXIT_OK}
312 fi
313 done
314
315 return ${EXIT_OK}
316}
b8026986
MT
317
318function port_hotplug_event() {
319 local port="${1}"
320 assert isset port
321
322 hotplug_assert_in_hotplug_event
323
324 port_cmd "hotplug" "${port}"
325}
326
327function port_get_slaves() {
328 local port="${1}"
329
330 port_settings_read "${port}" \
331 --ignore-superfluous-settings SLAVES
332 print "${SLAVES}"
333}
334
335function port_device_is_slave() {
336 assert [ $# -eq 2 ]
337
338 local port="${1}"
339 local device="${2}"
340
341 # Get slaves of port
342 local slaves="$(port_get_slaves "${port}")"
343
344 # Returns true if device is in slaves
345 list_match "${device}" ${slaves}
346}
347
348function port_get_phy() {
349 local port="${1}"
350
351 port_settings_read "${port}" \
352 --ignore-superfluous-settings PHY
353 print "${PHY}"
354}
355
356function port_uses_phy() {
357 assert [ $# -eq 2 ]
358
359 local port="${1}"
360 local phy="${2}"
361
362 # Nothing to do if an empty argument is given
363 if ! isset phy; then
364 return ${EXIT_FALSE}
365 fi
366
367 phy="$(phy_get_address "${phy}")"
368
369 local port_phy="$(port_get_phy "${port}")"
370 [ "${port_phy}" = "${phy}" ]
371}