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
55 elif disabled OFFLOADING
; then
58 error
"Invalid value for offloading: ${OFFLOADING}"
63 local slave
=$
(cli_get_val
"${1:1}")
65 if port_exists
"${slave}"; then
66 if list_match
"${slave}" ${SLAVES}; then
67 warning
"Port ${slave} is already enslaved"
69 list_append SLAVES
"${slave}"
72 warning
"Port ${slave} does not exist"
76 local slave
=$
(cli_get_val
"${1:1}")
77 if ! list_remove SLAVES
"${slave}"; then
78 warning
"Port ${slave} is not a slave of this bonding device"
82 warning
"Unknown argument '${1}'"
88 if isset ADDRESS
; then
89 if ! ismac ADDRESS
; then
90 error
"The given MAC address is invalid: ${ADDRESS}"
94 ADDRESS
=$
(mac_generate
)
99 if ! hook_parse_cmdline
"$@"; then
104 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
108 if port_settings_write
"${port}" ${HOOK_SETTINGS}; then
109 log INFO
"New port ${port} has been created"
111 error
"Could not save configuration for ${port}"
121 if ! hook_default_edit
"$@"; then
125 # If the master device is up, make sure that all
127 if device_exists
"${port}"; then
128 # Setting the mode requires us to destroy the device
129 # and to recreate it again.
130 local mode
=$
(bonding_get_mode
"${port}")
131 if [ "${mode}" != "${MODE}" ]; then
132 port_restart
"${port}"
137 device_set_address
"${port}" "${ADDRESS}"
140 bonding_set_miimon
"${port}" "${MIIMON}"
143 for slave
in ${SLAVES}; do
144 if device_exists
"${slave}"; then
145 bonding_enslave_device
"${port}" "${slave}"
147 port_create
"${slave}"
157 # Exit silently if the device already exists
158 device_exists
"${port}" && exit ${EXIT_OK}
160 port_settings_read
"${port}" ${HOOK_SETTINGS}
162 # Create the bonding devices
163 bonding_create
"${port}" \
164 --address="${ADDRESS}" \
165 --mode="${MODE}" ||
exit ${EXIT_ERROR}
167 bonding_set_miimon
"${port}" "${MIIMON}"
176 port_settings_read
"${port}" ${HOOK_SETTINGS}
178 # Remove the bonding device
179 if device_exists
"${port}"; then
180 bonding_remove
"${port}"
188 port_settings_read
"${port}" ${HOOK_SETTINGS}
190 # Auto-enable or disable hardware offloading
191 if ! isset OFFLOADING || enabled OFFLOADING
; then
192 offloading_auto
"${port}"
194 offloading_disable_all
"${port}"
197 # Execute the default action
198 hook_default_up
"${port}"
200 # Bring up all slaves
202 for slave
in $
(unquote
${SLAVES}); do
211 port_settings_read
"${port}" ${HOOK_SETTINGS}
213 # Bring down all slaves
215 for slave
in $
(unquote
${SLAVES}); do
219 # Execute the default action
220 hook_default_down
"${port}"
227 case "$(hotplug_action)" in
229 # Handle events of the same interface
230 if hotplug_event_port_is_interface
"${port}"; then
232 port_settings_read
"${port}" ${HOOK_SETTINGS}
234 # Bring up all slaves
235 # Attach those which already exist and try to create
236 # those which don't exist yet. They will be attached
237 # in their own hotplug event.
239 for slave
in $
(unquote
${SLAVES}); do
240 if device_exists
"${slave}"; then
241 bonding_enslave_device
"${port}" "${slave}"
243 port_create
"${slave}"
249 # Handle slave devices that have just been created and
251 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
252 bonding_enslave_device
"${port}" "${INTERFACE}"
254 # If the parent device has been set up, we will
255 # bring up the slave device as well.
256 if device_is_up
"${port}"; then
257 port_up
"${INTERFACE}"
265 if hotplug_event_port_is_interface
"${port}"; then
266 # Bring down all slaves after the parent device went away
268 for slave
in $
(port_get_slaves
"${port}"); do
269 port_remove
"${slave}"
277 exit ${EXIT_NOT_HANDLED}