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
() {
45 # Save the HOOK variable.
48 settings_read
"$(port_file "${port}")" "$@"
54 function port_settings_write
() {
59 settings_write
"$(port_file "${port}")" "$@"
62 function ports_get_all
() {
65 for port
in $
(port_dir
)/*; do
66 [ -f "${port}" ] ||
continue
72 function port_file
() {
76 echo "$(port_dir)/${port}"
79 function port_exists
() {
82 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
85 function port_get_hook
() {
90 config_get_hook $
(port_file
${port})
93 function port_is_attached
() {
100 for zone
in $
(zones_get_all
); do
103 assert zone_exists
${zone}
105 if listmatch
${port} $
(zone_get_ports
${zone}); then
114 function port_new
() {
118 #if port_exists ${port}; then
119 # error "Port '${port}' does already exist."
120 # return ${EXIT_ERROR}
126 if ! hook_exists port
${hook}; then
127 error
"Port hook '${hook}' does not exist."
131 #port_edit ${port} ${hook} $@
133 #if [ $? -ne ${EXIT_OK} ]; then
134 # port_destroy ${port}
137 hook_exec port
${hook} new $@
140 function port_destroy
() {
145 port_exists
${port} ||
return ${EXIT_OK}
147 # Check if the port is attached to any zone and don't delete it.
150 local attached_zone
=$
(port_is_attached
${port})
151 if [ -n "${attached_zone}" ]; then
152 error_log
"Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
156 # Check if the port is linked to any other port and don't allow the user
159 for other_port
in $
(ports_get
); do
160 [ "${other_port}" = "${port}" ] && continue
162 if listmatch
${port} $
(port_get_parents
${other_port}); then
163 error_log
"Cannot destroy port '${port}' which is a parent port to '${other_port}'."
167 if listmatch
${port} $
(port_get_children
${other_port}); then
168 error_log
"Cannot destroy port '${port}' which is child of port '${other_port}'."
173 # If ok says we are not okay --> exit
174 if [ ${ok} -ne ${EXIT_OK} ]; then
180 rm -f $
(port_file
${port})
183 function port_create
() {
187 function port_remove
() {
191 # If the device is still up, we need to bring it down first.
192 if device_is_up
"${port}"; then
196 port_cmd
"remove" "${port}"
199 function port_edit
() {
203 # XXX? Compatibility function
204 function port_show
() {
212 function port_down
() {
216 function port_status
() {
220 function port_info
() {
224 function port_cmd
() {
232 local hook
=$
(port_get_hook
${port})
236 hook_exec port
${hook} ${cmd} ${port} $@
239 function ports_get
() {
241 for port
in $
(port_dir
)/*; do
242 port
=$
(basename ${port})
243 if port_exists
${port}; then
249 function port_find_free
() {
257 while [ ${i} -lt 99 ]; do
258 port
=${pattern//N/${i}}
259 if ! port_exists
${port} && ! device_exists
${port}; then
269 function port_get_info
() {
274 assert port_exists
${port}
278 eval $
(port_info
${port})
283 function port_get_parents
() {
286 port_get_info
${port} PORT_PARENTS
289 function port_get_children
() {
292 port_get_info
${port} PORT_CHILDREN
295 function port_zone
() {
296 # Get name of the zones, this port is configured in.
303 for zone
in $
(zones_get_all
); do
304 if zone_has_port
${zone} ${port}; then
313 function port_hotplug_event
() {
317 hotplug_assert_in_hotplug_event
319 port_cmd
"hotplug" "${port}"
322 function port_get_slaves
() {
325 port_settings_read
"${port}" \
326 --ignore-superfluous-settings SLAVES
330 function port_device_is_slave
() {
337 local slaves
="$(port_get_slaves "${port}")"
339 # Returns true if device is in slaves
340 list_match
"${device}" ${slaves}
343 function port_get_phy
() {
346 port_settings_read
"${port}" \
347 --ignore-superfluous-settings PHY
351 function port_uses_phy
() {
357 # Nothing to do if an empty argument is given
362 phy
="$(phy_get_address "${phy}")"
364 local port_phy
="$(port_get_phy "${port}")"
365 [ "${port_phy}" = "${phy}" ]