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 ###############################################################################
22 .
/usr
/lib
/network
/header-port
24 HOOK_SETTINGS
="HOOK ADDRESS MIIMON MODE SLAVES"
30 hook_check_settings
() {
35 assert isinteger MIIMON
38 hook_parse_cmdline
() {
39 while [ $# -gt 0 ]; do
42 ADDRESS
=$
(cli_get_val
"${1}")
45 MIIMON
=$
(cli_get_val
"${1}")
48 MODE
=$
(cli_get_val
"${1}")
51 local slave
=$
(cli_get_val
"${1:1}")
53 if port_exists
"${slave}"; then
54 if list_match
"${slave}" ${SLAVES}; then
55 warning
"Port ${slave} is already enslaved"
57 list_append SLAVES
"${slave}"
60 warning
"Port ${slave} does not exist"
64 local slave
=$
(cli_get_val
"${1:1}")
65 if ! list_remove SLAVES
"${slave}"; then
66 warning
"Port ${slave} is not a slave of this bonding device"
70 warning
"Unknown argument '${1}'"
76 if isset ADDRESS
; then
77 if ! ismac ADDRESS
; then
78 error
"The given MAC address is invalid: ${ADDRESS}"
82 ADDRESS
=$
(mac_generate
)
87 if ! hook_parse_cmdline
"$@"; then
92 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
96 if port_settings_write
"${port}" ${HOOK_SETTINGS}; then
97 log INFO
"New port ${port} has been created"
99 error
"Could not save configuration for ${port}"
109 if ! hook_default_edit
"$@"; then
113 # If the master device is up, make sure that all
115 if device_exists
"${port}"; then
116 # Setting the mode requires us to destroy the device
117 # and to recreate it again.
118 local mode
=$
(bonding_get_mode
"${port}")
119 if [ "${mode}" != "${MODE}" ]; then
120 port_restart
"${port}"
125 device_set_address
"${port}" "${ADDRESS}"
128 bonding_set_miimon
"${port}" "${MIIMON}"
131 for slave
in ${SLAVES}; do
132 if device_exists
"${slave}"; then
133 bonding_enslave_device
"${port}" "${slave}"
135 port_create
"${slave}"
145 # Exit silently if the device already exists
146 device_exists
"${port}" && exit ${EXIT_OK}
148 port_settings_read
"${port}" ${HOOK_SETTINGS}
150 # Create the bonding devices
151 bonding_create
"${port}" \
152 --address="${ADDRESS}" \
153 --mode="${MODE}" ||
exit ${EXIT_ERROR}
155 bonding_set_miimon
"${port}" "${MIIMON}"
164 port_settings_read
"${port}" ${HOOK_SETTINGS}
166 # Remove the bonding device
167 if device_exists
"${port}"; then
168 bonding_remove
"${port}"
176 port_settings_read
"${port}" ${HOOK_SETTINGS}
178 # Execute the default action
179 hook_default_up
"${port}"
181 # Bring up all slaves
183 for slave
in $
(unquote
${SLAVES}); do
192 port_settings_read
"${port}" ${HOOK_SETTINGS}
194 # Bring down all slaves
196 for slave
in $
(unquote
${SLAVES}); do
200 # Execute the default action
201 hook_default_down
"${port}"
208 case "$(hotplug_action)" in
210 # Handle events of the same interface
211 if hotplug_event_port_is_interface
"${port}"; then
213 port_settings_read
"${port}" ${HOOK_SETTINGS}
215 # Bring up all slaves
216 # Attach those which already exist and try to create
217 # those which don't exist yet. They will be attached
218 # in their own hotplug event.
220 for slave
in $
(unquote
${SLAVES}); do
221 if device_exists
"${slave}"; then
222 bonding_enslave_device
"${port}" "${slave}"
224 port_create
"${slave}"
230 # Handle slave devices that have just been created and
232 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
233 bonding_enslave_device
"${port}" "${INTERFACE}"
235 # If the parent device has been set up, we will
236 # bring up the slave device as well.
237 if device_is_up
"${port}"; then
238 port_up
"${INTERFACE}"
246 if hotplug_event_port_is_interface
"${port}"; then
247 # Bring down all slaves after the parent device went away
249 for slave
in $
(port_get_slaves
"${port}"); do
250 port_remove
"${slave}"
258 exit ${EXIT_NOT_HANDLED}