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 OFFLOADING 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 OFFLOADING
="$(cli_get_val "${1}")"
53 if enabled OFFLOADING
; then
60 local slave
=$
(cli_get_val
"${1:1}")
62 if port_exists
"${slave}"; then
63 if list_match
"${slave}" ${SLAVES}; then
64 warning
"Port ${slave} is already enslaved"
66 list_append SLAVES
"${slave}"
69 warning
"Port ${slave} does not exist"
73 local slave
=$
(cli_get_val
"${1:1}")
74 if ! list_remove SLAVES
"${slave}"; then
75 warning
"Port ${slave} is not a slave of this bonding device"
79 warning
"Unknown argument '${1}'"
85 if isset ADDRESS
; then
86 if ! ismac ADDRESS
; then
87 error
"The given MAC address is invalid: ${ADDRESS}"
91 ADDRESS
=$
(mac_generate
)
96 if ! hook_parse_cmdline
"$@"; then
101 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
105 if port_settings_write
"${port}" ${HOOK_SETTINGS}; then
106 log INFO
"New port ${port} has been created"
108 error
"Could not save configuration for ${port}"
118 if ! hook_default_edit
"$@"; then
122 # If the master device is up, make sure that all
124 if device_exists
"${port}"; then
125 # Setting the mode requires us to destroy the device
126 # and to recreate it again.
127 local mode
=$
(bonding_get_mode
"${port}")
128 if [ "${mode}" != "${MODE}" ]; then
129 port_restart
"${port}"
134 device_set_address
"${port}" "${ADDRESS}"
137 bonding_set_miimon
"${port}" "${MIIMON}"
140 for slave
in ${SLAVES}; do
141 if device_exists
"${slave}"; then
142 bonding_enslave_device
"${port}" "${slave}"
144 port_create
"${slave}"
154 # Exit silently if the device already exists
155 device_exists
"${port}" && exit ${EXIT_OK}
157 port_settings_read
"${port}" ${HOOK_SETTINGS}
159 # Create the bonding devices
160 bonding_create
"${port}" \
161 --address="${ADDRESS}" \
162 --mode="${MODE}" ||
exit ${EXIT_ERROR}
164 bonding_set_miimon
"${port}" "${MIIMON}"
173 port_settings_read
"${port}" ${HOOK_SETTINGS}
175 # Remove the bonding device
176 if device_exists
"${port}"; then
177 bonding_remove
"${port}"
185 port_settings_read
"${port}" ${HOOK_SETTINGS}
187 # Auto-enable or disable hardware offloading
188 if ! isset OFFLOADING || enabled OFFLOADING
; then
189 offloading_auto
"${port}"
191 offloading_disable_all
"${port}"
194 # Execute the default action
195 hook_default_up
"${port}"
197 # Bring up all slaves
199 for slave
in $
(unquote
${SLAVES}); do
208 port_settings_read
"${port}" ${HOOK_SETTINGS}
210 # Bring down all slaves
212 for slave
in $
(unquote
${SLAVES}); do
216 # Execute the default action
217 hook_default_down
"${port}"
224 case "$(hotplug_action)" in
226 # Handle events of the same interface
227 if hotplug_event_port_is_interface
"${port}"; then
229 port_settings_read
"${port}" ${HOOK_SETTINGS}
231 # Bring up all slaves
232 # Attach those which already exist and try to create
233 # those which don't exist yet. They will be attached
234 # in their own hotplug event.
236 for slave
in $
(unquote
${SLAVES}); do
237 if device_exists
"${slave}"; then
238 bonding_enslave_device
"${port}" "${slave}"
240 port_create
"${slave}"
246 # Handle slave devices that have just been created and
248 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
249 bonding_enslave_device
"${port}" "${INTERFACE}"
251 # If the parent device has been set up, we will
252 # bring up the slave device as well.
253 if device_is_up
"${port}"; then
254 port_up
"${INTERFACE}"
262 if hotplug_event_port_is_interface
"${port}"; then
263 # Bring down all slaves after the parent device went away
265 for slave
in $
(port_get_slaves
"${port}"); do
266 port_remove
"${slave}"
274 exit ${EXIT_NOT_HANDLED}