]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.ports
settings: Some code refactoring
[people/ms/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 ports_get_all() {
23 local port
24 for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
25 if port_exists "${port}"; then
26 print "${port}"
27 fi
28 done
29 }
30
31 # XXX TO BE REMOVED
32 port_list() {
33 ports_get_all "$@"
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
98 settings_write "$(port_file "${port}")" \
99 --check="hook_check_settings" HOOK ${HOOK_SETTINGS[*]}
100 }
101
102 port_file() {
103 local port="${1}"
104 assert isset port
105
106 echo "${NETWORK_PORTS_DIR}/${port}/settings"
107 }
108
109 port_exists() {
110 local port=${1}
111
112 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
113 }
114
115 port_get_hook() {
116 local port=${1}
117
118 assert isset port
119
120 config_get_hook $(port_file ${port})
121 }
122
123 port_is_attached() {
124 local port=${1}
125 shift
126
127 assert isset port
128
129 local zone
130 for zone in $(zones_get_all); do
131 if list_match ${port} $(zone_get_ports ${zone}); then
132 echo "${zone}"
133 return ${EXIT_OK}
134 fi
135 done
136
137 return ${EXIT_ERROR}
138 }
139
140 port_is_up() {
141 device_is_up "$@"
142 }
143
144 port_new() {
145 local hook="${1}"
146 shift
147
148 if ! hook_exists port "${hook}"; then
149 error "Port hook '${hook}' does not exist."
150 return ${EXIT_ERROR}
151 fi
152
153 hook_exec port "${hook}" new "$@"
154 }
155
156 port_destroy() {
157 local port=${1}
158 assert isset port
159
160 # Cannot delete a port that does not exist
161 if ! port_exists ${port}; then
162 error "No such port: ${port}"
163 return ${EXIT_ERROR}
164 fi
165
166 local attached_zone=$(port_is_attached ${port})
167 if [ -n "${attached_zone}" ]; then
168 if ! zone_port_detach "${attached_zone}" "${port}"; then
169 error "Could not remove port ${port} from zone ${zone}"
170 return ${EXIT_ERROR}
171 fi
172 fi
173
174 # Check if the port is linked to any other port and don't allow the user
175 # to delete it.
176 local other_port
177 for other_port in $(ports_get); do
178 [ "${other_port}" = "${port}" ] && continue
179
180 if list_match ${port} $(port_get_children ${other_port}); then
181 log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
182 return ${EXIT_ERROR}
183 fi
184 done
185
186 # Shut down the port before destroying it
187 if ! port_remove "${port}"; then
188 return ${EXIT_ERROR}
189 fi
190
191 if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then
192 log ERROR "Could not destroy port ${port}"
193 return ${EXIT_ERROR}
194 fi
195
196 log INFO "Destroyed port ${port}"
197 return ${EXIT_OK}
198 }
199
200 port_create() {
201 port_cmd "create" "$@"
202 }
203
204 port_remove() {
205 local port="${1}"
206 assert isset port
207
208 if ! port_exists "${port}"; then
209 log ERROR "Port ${port} does not exist"
210 return ${EXIT_ERROR}
211 fi
212
213 # If the device is still up, we need to bring it down first.
214 if device_is_up "${port}"; then
215 port_down "${port}"
216 fi
217
218 port_cmd "remove" "${port}"
219 }
220
221 # Restarts the port by removing it and then re-creating it
222 port_restart() {
223 local port="${1}"
224 assert isset port
225
226 port_remove "${port}"
227
228 port_create "${port}"
229 }
230
231 port_edit() {
232 port_cmd edit "$@"
233 }
234
235 port_up() {
236 assert [ $# -eq 1 ]
237
238 local port="${1}"
239
240 # Check if the port exists
241 if ! device_exists "${port}"; then
242 log ERROR "Could not bring up port ${port} which has not been created"
243 return ${EXIT_ERROR}
244 fi
245
246 port_cmd up "${port}"
247 }
248
249 port_down() {
250 port_cmd down "$@"
251 }
252
253 port_status() {
254 port_cmd status "$@"
255 }
256
257 port_cmd() {
258 local cmd=${1}
259 local port=${2}
260 shift 2
261
262 assert isset cmd
263 assert isset port
264
265 local hook=$(port_get_hook ${port})
266
267 # Abort if we could not find a hook
268 if ! isset hook; then
269 log CRITICAL "Port ${port} does not have a hook associated with it"
270 return ${EXIT_ERROR}
271 fi
272
273 hook_exec port ${hook} ${cmd} ${port} "$@"
274 }
275
276 ports_get() {
277 local port
278 for port in $(list_directory "${NETWORK_PORTS_DIR}"); do
279 if port_exists ${port}; then
280 echo "${port}"
281 fi
282 done
283 }
284
285 port_find_free() {
286 local pattern=${1}
287
288 assert isset pattern
289
290 local port
291 local i=0
292
293 while [ ${i} -lt 99 ]; do
294 port=${pattern//N/${i}}
295 if ! port_exists ${port} && ! device_exists ${port}; then
296 echo "${port}"
297 return ${EXIT_OK}
298 fi
299 i=$(( ${i} + 1 ))
300 done
301
302 return ${EXIT_ERROR}
303 }
304
305 port_get_children() {
306 local port=${1}
307
308 assert port_exists "${port}"
309
310 port_cmd "children" "${port}"
311 }
312
313 port_zone() {
314 # Get name of the zones, this port is configured in.
315 local port=${1}
316 shift
317
318 assert isset port
319
320 local zone
321 for zone in $(zones_get_all); do
322 if zone_has_port ${zone} ${port}; then
323 echo "${zone}"
324 return ${EXIT_OK}
325 fi
326 done
327
328 return ${EXIT_OK}
329 }
330
331 port_hotplug_event() {
332 local port="${1}"
333 assert isset port
334
335 hotplug_assert_in_hotplug_event
336
337 port_cmd "hotplug" "${port}"
338 }
339
340 port_get_slaves() {
341 local port="${1}"
342
343 port_settings_read "${port}" \
344 --ignore-superfluous-settings SLAVES
345 print "${SLAVES}"
346 }
347
348 port_device_is_slave() {
349 assert [ $# -eq 2 ]
350
351 local port="${1}"
352 local device="${2}"
353
354 # Get slaves of port
355 local slaves="$(port_get_slaves "${port}")"
356
357 # Returns true if device is in slaves
358 list_match "${device}" ${slaves}
359 }
360
361 port_get_phy() {
362 local port="${1}"
363
364 port_settings_read "${port}" \
365 --ignore-superfluous-settings PHY
366 print "${PHY}"
367 }
368
369 port_uses_phy() {
370 assert [ $# -eq 2 ]
371
372 local port="${1}"
373 local phy="${2}"
374
375 # Nothing to do if an empty argument is given
376 if ! isset phy; then
377 return ${EXIT_FALSE}
378 fi
379
380 phy="$(phy_get_address "${phy}")"
381
382 local port_phy="$(port_get_phy "${port}")"
383 [ "${port_phy}" = "${phy}" ]
384 }
385
386 ports_lowest_address() {
387 local address
388 local addresses
389
390 local port
391 for port in $(port_list); do
392 # Skip all ports that do not exist
393 # any more or are not plugged in
394 device_exists "${port}" || continue
395
396 # Skip all ports that are not proper ethernet devices
397 device_is_wireless "${port}" && continue
398 device_is_ethernet "${port}" || continue
399
400 list_append addresses "$(device_get_address "${port}")"
401 done
402
403 # Sort the list
404 addresses="$(list_sort ${addresses})"
405
406 # Get the first element which is the lowest MAC address
407 list_head ${addresses}
408 }
409
410 port_identify() {
411 device_identify "$@"
412 }
413
414 port_get_color() {
415 # This function return the color of a port
416 assert [ $# -eq 1 ]
417
418 local name=${1}
419 color_read "port" ${name}
420 }
421
422 port_get_description_title() {
423 assert [ $# -eq 1 ]
424
425 local name=${1}
426 description_title_read $(description_format_filename "port" "${name}")
427 }