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 ###############################################################################
23 echo "${NETWORK_CONFIG_DIR}/ports"
26 function port_list
() {
28 for port
in $
(port_dir
)/*; do
29 port
="$(basename "${port}")"
30 if port_exists
"${port}"; then
36 function port_list_in_use
() {
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}
54 function port_list_free
() {
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
67 function port_get_hook
() {
71 config_get_hook $
(port_file
${port})
74 function port_config_dir
() {
77 print
"${RUN_DIR}/ports/${port}"
81 function port_settings_read
() {
85 # Save the HOOK variable.
88 settings_read
"$(port_file "${port}")" ${HOOK_SETTINGS}
94 function port_settings_write
() {
100 if function_exists
"hook_check_settings"; then
101 list_append args
"--check=\"hook_check_settings\""
103 list_append args
${HOOK_SETTINGS}
105 settings_write
"$(port_file "${port}")" ${args}
108 function ports_get_all
() {
112 function port_file
() {
116 echo "$(port_dir)/${port}"
119 function port_exists
() {
122 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
125 function port_get_hook
() {
130 config_get_hook $
(port_file
${port})
133 function port_is_attached
() {
140 for zone
in $
(zones_get_all
); do
143 assert zone_exists
${zone}
145 if listmatch
${port} $
(zone_get_ports
${zone}); then
154 function port_new
() {
158 #if port_exists ${port}; then
159 # error "Port '${port}' does already exist."
160 # return ${EXIT_ERROR}
166 if ! hook_exists port
${hook}; then
167 error
"Port hook '${hook}' does not exist."
171 #port_edit ${port} ${hook} $@
173 #if [ $? -ne ${EXIT_OK} ]; then
174 # port_destroy ${port}
177 hook_exec port
${hook} new $@
180 function port_destroy
() {
185 port_exists
${port} ||
return ${EXIT_OK}
187 # Check if the port is attached to any zone and don't delete it.
190 local attached_zone
=$
(port_is_attached
${port})
191 if [ -n "${attached_zone}" ]; then
192 error_log
"Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
196 # Check if the port is linked to any other port and don't allow the user
199 for other_port
in $
(ports_get
); do
200 [ "${other_port}" = "${port}" ] && continue
202 if listmatch
${port} $
(port_get_parents
${other_port}); then
203 error_log
"Cannot destroy port '${port}' which is a parent port to '${other_port}'."
207 if listmatch
${port} $
(port_get_children
${other_port}); then
208 error_log
"Cannot destroy port '${port}' which is child of port '${other_port}'."
213 # If ok says we are not okay --> exit
214 if [ ${ok} -ne ${EXIT_OK} ]; then
220 rm -f $
(port_file
${port})
223 function port_create
() {
227 function port_remove
() {
231 # If the device is still up, we need to bring it down first.
232 if device_is_up
"${port}"; then
236 port_cmd
"remove" "${port}"
239 function port_edit
() {
247 function port_down
() {
251 function port_status
() {
255 function port_info
() {
259 function port_cmd
() {
267 local hook
=$
(port_get_hook
${port})
271 hook_exec port
${hook} ${cmd} ${port} $@
274 function ports_get
() {
276 for port
in $
(port_dir
)/*; do
277 port
=$
(basename ${port})
278 if port_exists
${port}; then
284 function port_find_free
() {
292 while [ ${i} -lt 99 ]; do
293 port
=${pattern//N/${i}}
294 if ! port_exists
${port} && ! device_exists
${port}; then
304 function port_get_info
() {
309 assert port_exists
${port}
313 eval $
(port_info
${port})
318 function port_get_parents
() {
321 port_get_info
${port} PORT_PARENTS
324 function port_get_children
() {
327 port_get_info
${port} PORT_CHILDREN
330 function port_zone
() {
331 # Get name of the zones, this port is configured in.
338 for zone
in $
(zones_get_all
); do
339 if zone_has_port
${zone} ${port}; then
348 function port_hotplug_event
() {
352 hotplug_assert_in_hotplug_event
354 port_cmd
"hotplug" "${port}"
357 function port_get_slaves
() {
360 port_settings_read
"${port}" \
361 --ignore-superfluous-settings SLAVES
365 function port_device_is_slave
() {
372 local slaves
="$(port_get_slaves "${port}")"
374 # Returns true if device is in slaves
375 list_match
"${device}" ${slaves}
378 function port_get_phy
() {
381 port_settings_read
"${port}" \
382 --ignore-superfluous-settings PHY
386 function port_uses_phy
() {
392 # Nothing to do if an empty argument is given
397 phy
="$(phy_get_address "${phy}")"
399 local port_phy
="$(port_get_phy "${port}")"
400 [ "${port_phy}" = "${phy}" ]