2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 for port
in $
(list_directory
"${NETWORK_PORTS_DIR}"); do
25 if port_exists
"${port}"; then
39 # Collect all ports that are attached to a zone
41 for zone
in $
(zones_get_all
); do
42 list_append ports_in_use $
(zone_get_ports
"${zone}")
45 # Collect all ports that are enslaved by an other port
47 for port
in $
(port_list
); do
48 list_append ports_in_use $
(port_get_slaves
"${port}")
51 list_sort
${ports_in_use}
55 local ports_in_use
="$(port_list_in_use)"
58 for port
in $
(port_list
); do
59 if ! list_match
"${port}" ${ports_in_use}; then
71 config_get_hook $
(port_file
${port})
77 print
"${RUN_DIR}/ports/${port}"
81 port_settings_read
() {
85 # Save the HOOK variable.
88 settings_read
"$(port_file "${port}")" ${HOOK_SETTINGS[*]}
94 port_settings_write
() {
98 settings_write
"$(port_file "${port}")" \
99 --check="hook_check_settings" HOOK
${HOOK_SETTINGS[*]}
106 echo "${NETWORK_PORTS_DIR}/${port}/settings"
112 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
120 config_get_hook $
(port_file
${port})
130 for zone
in $
(zones_get_all
); do
131 if list_match
${port} $
(zone_get_ports
${zone}); then
148 if ! hook_exists port
"${hook}"; then
149 error
"Port hook '${hook}' does not exist."
153 hook_exec port
"${hook}" new
"$@"
160 # Cannot delete a port that does not exist
161 if ! port_exists
${port}; then
162 error
"No such port: ${port}"
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}"
174 # Check if the port is linked to any other port and don't allow the user
177 for other_port
in $
(ports_get
); do
178 [ "${other_port}" = "${port}" ] && continue
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}'."
186 # Shut down the port before destroying it
187 if ! port_remove
"${port}"; then
191 if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then
192 log ERROR
"Could not destroy port ${port}"
196 log INFO
"Destroyed port ${port}"
201 port_cmd
"create" "$@"
208 if ! port_exists
"${port}"; then
209 log ERROR
"Port ${port} does not exist"
213 # If the device is still up, we need to bring it down first.
214 if device_is_up
"${port}"; then
218 port_cmd
"remove" "${port}"
221 # Restarts the port by removing it and then re-creating it
226 port_remove
"${port}"
228 port_create
"${port}"
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"
246 port_cmd up
"${port}"
265 local hook
=$
(port_get_hook
${port})
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"
273 hook_exec port
${hook} ${cmd} ${port} "$@"
278 for port
in $
(list_directory
"${NETWORK_PORTS_DIR}"); do
279 if port_exists
${port}; then
293 while [ ${i} -lt 99 ]; do
294 port
=${pattern//N/${i}}
295 if ! port_exists
${port} && ! device_exists
${port}; then
305 port_get_children
() {
308 assert port_exists
"${port}"
310 port_cmd
"children" "${port}"
314 # Get name of the zones, this port is configured in.
321 for zone
in $
(zones_get_all
); do
322 if zone_has_port
${zone} ${port}; then
331 port_hotplug_event
() {
335 hotplug_assert_in_hotplug_event
337 port_cmd
"hotplug" "${port}"
343 port_settings_read
"${port}" \
344 --ignore-superfluous-settings SLAVES
348 port_device_is_slave
() {
355 local slaves
="$(port_get_slaves "${port}")"
357 # Returns true if device is in slaves
358 list_match
"${device}" ${slaves}
364 port_settings_read
"${port}" \
365 --ignore-superfluous-settings PHY
375 # Nothing to do if an empty argument is given
380 phy
="$(phy_get_address "${phy}")"
382 local port_phy
="$(port_get_phy "${port}")"
383 [ "${port_phy}" = "${phy}" ]
386 ports_lowest_address
() {
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
396 # Skip all ports that are not proper ethernet devices
397 device_is_wireless
"${port}" && continue
398 device_is_ethernet
"${port}" ||
continue
400 list_append addresses
"$(device_get_address "${port}")"
404 addresses
="$(list_sort ${addresses})"
406 # Get the first element which is the lowest MAC address
407 list_head
${addresses}
415 # This function return the color of a port
419 color_read
"port" ${name}
422 port_get_description_title
() {
426 description_title_read $
(description_format_filename
"port" "${name}")