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_get_hook
() {
30 config_get_hook $
(port_file
${port})
33 function port_config_dir
() {
36 print
"${RUN_DIR}/ports/${port}"
40 function port_settings_read
() {
44 # Save the HOOK variable.
47 settings_read
"$(port_file "${port}")" ${HOOK_SETTINGS}
53 function port_settings_write
() {
59 if function_exists
"hook_check_settings"; then
60 list_append args
"--check=\"hook_check_settings\""
62 list_append args
${HOOK_SETTINGS}
64 settings_write
"$(port_file "${port}")" ${args}
67 function ports_get_all
() {
70 for port
in $
(port_dir
)/*; do
71 [ -f "${port}" ] ||
continue
77 function port_file
() {
81 echo "$(port_dir)/${port}"
84 function port_exists
() {
87 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
90 function port_get_hook
() {
95 config_get_hook $
(port_file
${port})
98 function port_is_attached
() {
105 for zone
in $
(zones_get_all
); do
108 assert zone_exists
${zone}
110 if listmatch
${port} $
(zone_get_ports
${zone}); then
119 function port_new
() {
123 #if port_exists ${port}; then
124 # error "Port '${port}' does already exist."
125 # return ${EXIT_ERROR}
131 if ! hook_exists port
${hook}; then
132 error
"Port hook '${hook}' does not exist."
136 #port_edit ${port} ${hook} $@
138 #if [ $? -ne ${EXIT_OK} ]; then
139 # port_destroy ${port}
142 hook_exec port
${hook} new $@
145 function port_destroy
() {
150 port_exists
${port} ||
return ${EXIT_OK}
152 # Check if the port is attached to any zone and don't delete it.
155 local attached_zone
=$
(port_is_attached
${port})
156 if [ -n "${attached_zone}" ]; then
157 error_log
"Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
161 # Check if the port is linked to any other port and don't allow the user
164 for other_port
in $
(ports_get
); do
165 [ "${other_port}" = "${port}" ] && continue
167 if listmatch
${port} $
(port_get_parents
${other_port}); then
168 error_log
"Cannot destroy port '${port}' which is a parent port to '${other_port}'."
172 if listmatch
${port} $
(port_get_children
${other_port}); then
173 error_log
"Cannot destroy port '${port}' which is child of port '${other_port}'."
178 # If ok says we are not okay --> exit
179 if [ ${ok} -ne ${EXIT_OK} ]; then
185 rm -f $
(port_file
${port})
188 function port_create
() {
192 function port_remove
() {
196 # If the device is still up, we need to bring it down first.
197 if device_is_up
"${port}"; then
201 port_cmd
"remove" "${port}"
204 function port_edit
() {
208 # XXX? Compatibility function
209 function port_show
() {
217 function port_down
() {
221 function port_status
() {
225 function port_info
() {
229 function port_cmd
() {
237 local hook
=$
(port_get_hook
${port})
241 hook_exec port
${hook} ${cmd} ${port} $@
244 function ports_get
() {
246 for port
in $
(port_dir
)/*; do
247 port
=$
(basename ${port})
248 if port_exists
${port}; then
254 function port_find_free
() {
262 while [ ${i} -lt 99 ]; do
263 port
=${pattern//N/${i}}
264 if ! port_exists
${port} && ! device_exists
${port}; then
274 function port_get_info
() {
279 assert port_exists
${port}
283 eval $
(port_info
${port})
288 function port_get_parents
() {
291 port_get_info
${port} PORT_PARENTS
294 function port_get_children
() {
297 port_get_info
${port} PORT_CHILDREN
300 function port_zone
() {
301 # Get name of the zones, this port is configured in.
308 for zone
in $
(zones_get_all
); do
309 if zone_has_port
${zone} ${port}; then
318 function port_hotplug_event
() {
322 hotplug_assert_in_hotplug_event
324 port_cmd
"hotplug" "${port}"
327 function port_get_slaves
() {
330 port_settings_read
"${port}" \
331 --ignore-superfluous-settings SLAVES
335 function port_device_is_slave
() {
342 local slaves
="$(port_get_slaves "${port}")"
344 # Returns true if device is in slaves
345 list_match
"${device}" ${slaves}
348 function port_get_phy
() {
351 port_settings_read
"${port}" \
352 --ignore-superfluous-settings PHY
356 function port_uses_phy
() {
362 # Nothing to do if an empty argument is given
367 phy
="$(phy_get_address "${phy}")"
369 local port_phy
="$(port_get_phy "${port}")"
370 [ "${port_phy}" = "${phy}" ]