]>
Commit | Line | Data |
---|---|---|
711ffac1 | 1 | #!/bin/bash |
1578dae9 MT |
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 | ############################################################################### | |
711ffac1 | 21 | |
1c6a4e30 | 22 | port_dir() { |
af51d3a9 JS |
23 | local port="${1}" |
24 | echo "${NETWORK_CONFIG_DIR}/ports/${port}" | |
711ffac1 MT |
25 | } |
26 | ||
1c6a4e30 | 27 | port_list() { |
bae37360 MT |
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 | ||
1c6a4e30 | 37 | port_list_in_use() { |
bae37360 MT |
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 | ||
1c6a4e30 | 55 | port_list_free() { |
bae37360 MT |
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 | ||
1c6a4e30 | 68 | port_get_hook() { |
49ec20d8 MT |
69 | local port=${1} |
70 | assert isset port | |
71 | ||
72 | config_get_hook $(port_file ${port}) | |
73 | } | |
74 | ||
1c6a4e30 | 75 | port_config_dir() { |
49ec20d8 MT |
76 | local port=${1} |
77 | ||
78 | print "${RUN_DIR}/ports/${port}" | |
79 | return ${EXIT_OK} | |
80 | } | |
81 | ||
1c6a4e30 | 82 | port_settings_read() { |
e9df08ad | 83 | local port="${1}" |
49ec20d8 MT |
84 | assert isset port |
85 | ||
86 | # Save the HOOK variable. | |
87 | local hook="${HOOK}" | |
88 | ||
1e6f187e | 89 | settings_read "$(port_file "${port}")" ${HOOK_SETTINGS} |
49ec20d8 MT |
90 | |
91 | # Restore hook. | |
92 | HOOK="${hook}" | |
93 | } | |
94 | ||
1c6a4e30 | 95 | port_settings_write() { |
e9df08ad | 96 | local port="${1}" |
49ec20d8 | 97 | assert isset port |
e9df08ad | 98 | shift |
49ec20d8 | 99 | |
1e6f187e MT |
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} | |
49ec20d8 MT |
107 | } |
108 | ||
1c6a4e30 | 109 | ports_get_all() { |
bae37360 | 110 | port_list |
8895cf8f MT |
111 | } |
112 | ||
1c6a4e30 | 113 | port_file() { |
e9df08ad | 114 | local port="${1}" |
711ffac1 MT |
115 | assert isset port |
116 | ||
af51d3a9 | 117 | echo "$(port_dir ${port})/settings" |
711ffac1 MT |
118 | } |
119 | ||
1c6a4e30 | 120 | port_exists() { |
711ffac1 MT |
121 | local port=${1} |
122 | ||
af51d3a9 | 123 | [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ] |
711ffac1 MT |
124 | } |
125 | ||
1c6a4e30 | 126 | port_get_hook() { |
711ffac1 MT |
127 | local port=${1} |
128 | ||
129 | assert isset port | |
130 | ||
131 | config_get_hook $(port_file ${port}) | |
132 | } | |
133 | ||
1c6a4e30 | 134 | port_is_attached() { |
711ffac1 MT |
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 | ||
8c9205b1 | 146 | if list_match ${port} $(zone_get_ports ${zone}); then |
711ffac1 MT |
147 | echo "${zone}" |
148 | return ${EXIT_OK} | |
149 | fi | |
150 | done | |
151 | ||
152 | return ${EXIT_ERROR} | |
153 | } | |
154 | ||
1c6a4e30 | 155 | port_is_up() { |
abba34c1 MT |
156 | device_is_up $@ |
157 | } | |
158 | ||
1c6a4e30 | 159 | port_new() { |
ed75c16d | 160 | local hook="${1}" |
711ffac1 MT |
161 | shift |
162 | ||
ed75c16d | 163 | if ! hook_exists port "${hook}"; then |
711ffac1 MT |
164 | error "Port hook '${hook}' does not exist." |
165 | return ${EXIT_ERROR} | |
166 | fi | |
167 | ||
ed75c16d | 168 | hook_exec port "${hook}" new $@ |
711ffac1 MT |
169 | } |
170 | ||
1c6a4e30 | 171 | port_destroy() { |
711ffac1 MT |
172 | local port=${1} |
173 | ||
174 | assert isset port | |
175 | ||
176 | port_exists ${port} || return ${EXIT_OK} | |
177 | ||
98f4dae6 MT |
178 | # Check if the port is attached to any zone and don't delete it. |
179 | local ok=${EXIT_OK} | |
711ffac1 | 180 | |
98f4dae6 | 181 | local attached_zone=$(port_is_attached ${port}) |
711ffac1 | 182 | if [ -n "${attached_zone}" ]; then |
98f4dae6 MT |
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 | ||
8c9205b1 | 193 | if list_match ${port} $(port_get_parents ${other_port}); then |
98f4dae6 MT |
194 | error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'." |
195 | ok=${EXIT_ERROR} | |
196 | fi | |
197 | ||
8c9205b1 | 198 | if list_match ${port} $(port_get_children ${other_port}); then |
98f4dae6 MT |
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 | |
711ffac1 MT |
206 | return ${EXIT_ERROR} |
207 | fi | |
208 | ||
510d8184 | 209 | port_remove "${port}" |
711ffac1 | 210 | |
af51d3a9 | 211 | rm -rf $(port_dir ${port}) |
711ffac1 MT |
212 | } |
213 | ||
1c6a4e30 | 214 | port_create() { |
1ba6a2bb MT |
215 | port_cmd "create" $@ |
216 | } | |
217 | ||
1c6a4e30 | 218 | port_remove() { |
1ba6a2bb MT |
219 | local port="${1}" |
220 | assert isset port | |
221 | ||
270aab39 MT |
222 | if ! port_exists "${port}"; then |
223 | log ERROR "Port ${port} does not exist" | |
224 | return ${EXIT_ERROR} | |
225 | fi | |
226 | ||
1ba6a2bb MT |
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}" | |
f90e550b MT |
233 | } |
234 | ||
270aab39 MT |
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 | ||
1c6a4e30 | 245 | port_edit() { |
711ffac1 MT |
246 | port_cmd edit $@ |
247 | } | |
248 | ||
1c6a4e30 | 249 | port_up() { |
711ffac1 MT |
250 | port_cmd up $@ |
251 | } | |
252 | ||
1c6a4e30 | 253 | port_down() { |
711ffac1 MT |
254 | port_cmd down $@ |
255 | } | |
256 | ||
1c6a4e30 | 257 | port_status() { |
711ffac1 MT |
258 | port_cmd status $@ |
259 | } | |
260 | ||
1c6a4e30 | 261 | port_info() { |
98f4dae6 MT |
262 | port_cmd info $@ |
263 | } | |
264 | ||
1c6a4e30 | 265 | port_cmd() { |
711ffac1 MT |
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 | ||
0c949dd5 MT |
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 | |
711ffac1 MT |
280 | |
281 | hook_exec port ${hook} ${cmd} ${port} $@ | |
282 | } | |
f90e550b | 283 | |
1c6a4e30 | 284 | ports_get() { |
f90e550b MT |
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 | } | |
2ae0fb8d | 293 | |
1c6a4e30 | 294 | port_find_free() { |
d76f5107 MT |
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}" | |
a1a8f0f4 | 306 | return ${EXIT_OK} |
d76f5107 MT |
307 | fi |
308 | i=$(( ${i} + 1 )) | |
309 | done | |
a1a8f0f4 MT |
310 | |
311 | return ${EXIT_ERROR} | |
d76f5107 | 312 | } |
98f4dae6 | 313 | |
1c6a4e30 | 314 | port_get_info() { |
98f4dae6 MT |
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 | ||
1c6a4e30 | 328 | port_get_parents() { |
98f4dae6 MT |
329 | local port=${1} |
330 | ||
331 | port_get_info ${port} PORT_PARENTS | |
332 | } | |
333 | ||
1c6a4e30 | 334 | port_get_children() { |
98f4dae6 MT |
335 | local port=${1} |
336 | ||
337 | port_get_info ${port} PORT_CHILDREN | |
338 | } | |
3a7fef62 | 339 | |
1c6a4e30 | 340 | port_zone() { |
3a7fef62 MT |
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 | } | |
b8026986 | 357 | |
1c6a4e30 | 358 | port_hotplug_event() { |
b8026986 MT |
359 | local port="${1}" |
360 | assert isset port | |
361 | ||
362 | hotplug_assert_in_hotplug_event | |
363 | ||
364 | port_cmd "hotplug" "${port}" | |
365 | } | |
366 | ||
1c6a4e30 | 367 | port_get_slaves() { |
b8026986 MT |
368 | local port="${1}" |
369 | ||
370 | port_settings_read "${port}" \ | |
371 | --ignore-superfluous-settings SLAVES | |
372 | print "${SLAVES}" | |
373 | } | |
374 | ||
1c6a4e30 | 375 | port_device_is_slave() { |
b8026986 MT |
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 | ||
1c6a4e30 | 388 | port_get_phy() { |
b8026986 MT |
389 | local port="${1}" |
390 | ||
391 | port_settings_read "${port}" \ | |
392 | --ignore-superfluous-settings PHY | |
393 | print "${PHY}" | |
394 | } | |
395 | ||
1c6a4e30 | 396 | port_uses_phy() { |
b8026986 MT |
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 | } | |
7ad5252c | 412 | |
1c6a4e30 | 413 | ports_lowest_address() { |
7ad5252c MT |
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 | } | |
f5ee091e MT |
436 | |
437 | port_identify() { | |
438 | device_identify $@ | |
439 | } | |
410d2e85 JS |
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 | } | |
5de34b4c JS |
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 | } |