]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
color: add colors to zone and ports
[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 listmatch ${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 listmatch ${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 listmatch ${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 the device is still up, we need to bring it down first.
223 if device_is_up "${port}"; then
224 port_down "${port}"
225 fi
226
227 port_cmd "remove" "${port}"
228 }
229
230 port_edit() {
231 port_cmd edit $@
232 }
233
234 port_up() {
235 port_cmd up $@
236 }
237
238 port_down() {
239 port_cmd down $@
240 }
241
242 port_status() {
243 port_cmd status $@
244 }
245
246 port_info() {
247 port_cmd info $@
248 }
249
250 port_cmd() {
251 local cmd=${1}
252 local port=${2}
253 shift 2
254
255 assert isset cmd
256 assert isset port
257
258 local hook=$(port_get_hook ${port})
259
260 # Abort if we could not find a hook
261 if ! isset hook; then
262 log CRITICAL "Port ${port} does not have a hook associated with it"
263 return ${EXIT_ERROR}
264 fi
265
266 hook_exec port ${hook} ${cmd} ${port} $@
267 }
268
269 ports_get() {
270 local port
271 for port in $(port_dir)/*; do
272 port=$(basename ${port})
273 if port_exists ${port}; then
274 echo "${port}"
275 fi
276 done
277 }
278
279 port_find_free() {
280 local pattern=${1}
281
282 assert isset pattern
283
284 local port
285 local i=0
286
287 while [ ${i} -lt 99 ]; do
288 port=${pattern//N/${i}}
289 if ! port_exists ${port} && ! device_exists ${port}; then
290 echo "${port}"
291 return ${EXIT_OK}
292 fi
293 i=$(( ${i} + 1 ))
294 done
295
296 return ${EXIT_ERROR}
297 }
298
299 port_get_info() {
300 local port=${1}
301 local key=${2}
302
303 assert isset port
304 assert port_exists ${port}
305 assert isset key
306
307 (
308 eval $(port_info ${port})
309 echo "${!key}"
310 )
311 }
312
313 port_get_parents() {
314 local port=${1}
315
316 port_get_info ${port} PORT_PARENTS
317 }
318
319 port_get_children() {
320 local port=${1}
321
322 port_get_info ${port} PORT_CHILDREN
323 }
324
325 port_zone() {
326 # Get name of the zones, this port is configured in.
327 local port=${1}
328 shift
329
330 assert isset port
331
332 local zone
333 for zone in $(zones_get_all); do
334 if zone_has_port ${zone} ${port}; then
335 echo "${zone}"
336 return ${EXIT_OK}
337 fi
338 done
339
340 return ${EXIT_OK}
341 }
342
343 port_hotplug_event() {
344 local port="${1}"
345 assert isset port
346
347 hotplug_assert_in_hotplug_event
348
349 port_cmd "hotplug" "${port}"
350 }
351
352 port_get_slaves() {
353 local port="${1}"
354
355 port_settings_read "${port}" \
356 --ignore-superfluous-settings SLAVES
357 print "${SLAVES}"
358 }
359
360 port_device_is_slave() {
361 assert [ $# -eq 2 ]
362
363 local port="${1}"
364 local device="${2}"
365
366 # Get slaves of port
367 local slaves="$(port_get_slaves "${port}")"
368
369 # Returns true if device is in slaves
370 list_match "${device}" ${slaves}
371 }
372
373 port_get_phy() {
374 local port="${1}"
375
376 port_settings_read "${port}" \
377 --ignore-superfluous-settings PHY
378 print "${PHY}"
379 }
380
381 port_uses_phy() {
382 assert [ $# -eq 2 ]
383
384 local port="${1}"
385 local phy="${2}"
386
387 # Nothing to do if an empty argument is given
388 if ! isset phy; then
389 return ${EXIT_FALSE}
390 fi
391
392 phy="$(phy_get_address "${phy}")"
393
394 local port_phy="$(port_get_phy "${port}")"
395 [ "${port_phy}" = "${phy}" ]
396 }
397
398 ports_lowest_address() {
399 local address
400 local addresses
401
402 local port
403 for port in $(port_list); do
404 # Skip all ports that do not exist
405 # any more or are not plugged in
406 device_exists "${port}" || continue
407
408 # Skip all ports that are not proper ethernet devices
409 device_is_wireless "${port}" && continue
410 device_is_ethernet "${port}" || continue
411
412 list_append addresses "$(device_get_address "${port}")"
413 done
414
415 # Sort the list
416 addresses="$(list_sort ${addresses})"
417
418 # Get the first element which is the lowest MAC address
419 list_head ${addresses}
420 }
421
422 port_identify() {
423 device_identify $@
424 }
425
426 port_get_color() {
427 # This function return the color of a port
428 assert [ $# -eq 1 ]
429
430 local name=${1}
431 color_read "port" ${name}
432 }