]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
ipsec: remove whitespace
[people/stevee/network.git] / src / functions / functions.ports
1 #!/bin/bash
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 ###############################################################################
21
22 port_dir() {
23 local port="${1}"
24 echo "${NETWORK_CONFIG_DIR}/ports/${port}"
25 }
26
27 port_list() {
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
37 port_list_in_use() {
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
55 port_list_free() {
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
68 port_get_hook() {
69 local port=${1}
70 assert isset port
71
72 config_get_hook $(port_file ${port})
73 }
74
75 port_config_dir() {
76 local port=${1}
77
78 print "${RUN_DIR}/ports/${port}"
79 return ${EXIT_OK}
80 }
81
82 port_settings_read() {
83 local port="${1}"
84 assert isset port
85
86 # Save the HOOK variable.
87 local hook="${HOOK}"
88
89 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
90
91 # Restore hook.
92 HOOK="${hook}"
93 }
94
95 port_settings_write() {
96 local port="${1}"
97 assert isset port
98 shift
99
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}
107 }
108
109 ports_get_all() {
110 port_list
111 }
112
113 port_file() {
114 local port="${1}"
115 assert isset port
116
117 echo "$(port_dir ${port})/settings"
118 }
119
120 port_exists() {
121 local port=${1}
122
123 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
124 }
125
126 port_get_hook() {
127 local port=${1}
128
129 assert isset port
130
131 config_get_hook $(port_file ${port})
132 }
133
134 port_is_attached() {
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
146 if list_match ${port} $(zone_get_ports ${zone}); then
147 echo "${zone}"
148 return ${EXIT_OK}
149 fi
150 done
151
152 return ${EXIT_ERROR}
153 }
154
155 port_is_up() {
156 device_is_up $@
157 }
158
159 port_new() {
160 local hook="${1}"
161 shift
162
163 if ! hook_exists port "${hook}"; then
164 error "Port hook '${hook}' does not exist."
165 return ${EXIT_ERROR}
166 fi
167
168 hook_exec port "${hook}" new $@
169 }
170
171 port_destroy() {
172 local port=${1}
173
174 assert isset port
175
176 port_exists ${port} || return ${EXIT_OK}
177
178 # Check if the port is attached to any zone and don't delete it.
179 local ok=${EXIT_OK}
180
181 local attached_zone=$(port_is_attached ${port})
182 if [ -n "${attached_zone}" ]; then
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
193 if list_match ${port} $(port_get_parents ${other_port}); then
194 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
195 ok=${EXIT_ERROR}
196 fi
197
198 if list_match ${port} $(port_get_children ${other_port}); then
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
206 return ${EXIT_ERROR}
207 fi
208
209 port_remove "${port}"
210
211 rm -rf $(port_dir ${port})
212 }
213
214 port_create() {
215 port_cmd "create" $@
216 }
217
218 port_remove() {
219 local port="${1}"
220 assert isset port
221
222 if ! port_exists "${port}"; then
223 log ERROR "Port ${port} does not exist"
224 return ${EXIT_ERROR}
225 fi
226
227 # If the device is still up, we need to bring it down first.
228 if device_is_up "${port}"; then
229 port_down "${port}"
230 fi
231
232 port_cmd "remove" "${port}"
233 }
234
235 # Restarts the port by removing it and then re-creating it
236 port_restart() {
237 local port="${1}"
238 assert isset port
239
240 port_remove "${port}"
241
242 port_create "${port}"
243 }
244
245 port_edit() {
246 port_cmd edit $@
247 }
248
249 port_up() {
250 port_cmd up $@
251 }
252
253 port_down() {
254 port_cmd down $@
255 }
256
257 port_status() {
258 port_cmd status $@
259 }
260
261 port_info() {
262 port_cmd info $@
263 }
264
265 port_cmd() {
266 local cmd=${1}
267 local port=${2}
268 shift 2
269
270 assert isset cmd
271 assert isset port
272
273 local hook=$(port_get_hook ${port})
274
275 # Abort if we could not find a hook
276 if ! isset hook; then
277 log CRITICAL "Port ${port} does not have a hook associated with it"
278 return ${EXIT_ERROR}
279 fi
280
281 hook_exec port ${hook} ${cmd} ${port} $@
282 }
283
284 ports_get() {
285 local port
286 for port in $(port_dir)/*; do
287 port=$(basename ${port})
288 if port_exists ${port}; then
289 echo "${port}"
290 fi
291 done
292 }
293
294 port_find_free() {
295 local pattern=${1}
296
297 assert isset pattern
298
299 local port
300 local i=0
301
302 while [ ${i} -lt 99 ]; do
303 port=${pattern//N/${i}}
304 if ! port_exists ${port} && ! device_exists ${port}; then
305 echo "${port}"
306 return ${EXIT_OK}
307 fi
308 i=$(( ${i} + 1 ))
309 done
310
311 return ${EXIT_ERROR}
312 }
313
314 port_get_info() {
315 local port=${1}
316 local key=${2}
317
318 assert isset port
319 assert port_exists ${port}
320 assert isset key
321
322 (
323 eval $(port_info ${port})
324 echo "${!key}"
325 )
326 }
327
328 port_get_parents() {
329 local port=${1}
330
331 port_get_info ${port} PORT_PARENTS
332 }
333
334 port_get_children() {
335 local port=${1}
336
337 port_get_info ${port} PORT_CHILDREN
338 }
339
340 port_zone() {
341 # Get name of the zones, this port is configured in.
342 local port=${1}
343 shift
344
345 assert isset port
346
347 local zone
348 for zone in $(zones_get_all); do
349 if zone_has_port ${zone} ${port}; then
350 echo "${zone}"
351 return ${EXIT_OK}
352 fi
353 done
354
355 return ${EXIT_OK}
356 }
357
358 port_hotplug_event() {
359 local port="${1}"
360 assert isset port
361
362 hotplug_assert_in_hotplug_event
363
364 port_cmd "hotplug" "${port}"
365 }
366
367 port_get_slaves() {
368 local port="${1}"
369
370 port_settings_read "${port}" \
371 --ignore-superfluous-settings SLAVES
372 print "${SLAVES}"
373 }
374
375 port_device_is_slave() {
376 assert [ $# -eq 2 ]
377
378 local port="${1}"
379 local device="${2}"
380
381 # Get slaves of port
382 local slaves="$(port_get_slaves "${port}")"
383
384 # Returns true if device is in slaves
385 list_match "${device}" ${slaves}
386 }
387
388 port_get_phy() {
389 local port="${1}"
390
391 port_settings_read "${port}" \
392 --ignore-superfluous-settings PHY
393 print "${PHY}"
394 }
395
396 port_uses_phy() {
397 assert [ $# -eq 2 ]
398
399 local port="${1}"
400 local phy="${2}"
401
402 # Nothing to do if an empty argument is given
403 if ! isset phy; then
404 return ${EXIT_FALSE}
405 fi
406
407 phy="$(phy_get_address "${phy}")"
408
409 local port_phy="$(port_get_phy "${port}")"
410 [ "${port_phy}" = "${phy}" ]
411 }
412
413 ports_lowest_address() {
414 local address
415 local addresses
416
417 local port
418 for port in $(port_list); do
419 # Skip all ports that do not exist
420 # any more or are not plugged in
421 device_exists "${port}" || continue
422
423 # Skip all ports that are not proper ethernet devices
424 device_is_wireless "${port}" && continue
425 device_is_ethernet "${port}" || continue
426
427 list_append addresses "$(device_get_address "${port}")"
428 done
429
430 # Sort the list
431 addresses="$(list_sort ${addresses})"
432
433 # Get the first element which is the lowest MAC address
434 list_head ${addresses}
435 }
436
437 port_identify() {
438 device_identify $@
439 }
440
441 port_get_color() {
442 # This function return the color of a port
443 assert [ $# -eq 1 ]
444
445 local name=${1}
446 color_read "port" ${name}
447 }
448
449 port_get_description_title() {
450 assert [ $# -eq 1 ]
451
452 local name=${1}
453 description_title_read $(description_format_filename "port" "${name}")
454 }