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