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