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
33 DEFAULT_MODE
="balance-rr"
35 hook_check_settings
() {
40 assert isinteger MIIMON
43 hook_parse_cmdline
() {
44 while [ $# -gt 0 ]; do
47 ADDRESS
="$(cli_get_val "${1}")"
49 if ! mac_is_valid
"${ADDRESS}"; then
50 error
"Invalid MAC address: ${ADDRESS}"
56 MIIMON
=$
(cli_get_val
"${1}")
59 MODE
=$
(cli_get_val
"${1}")
62 OFFLOADING
="$(cli_get_val "${1}")"
64 if enabled OFFLOADING
; then
66 elif disabled OFFLOADING
; then
69 error
"Invalid value for offloading: ${OFFLOADING}"
74 local slave
=$
(cli_get_val
"${1:1}")
76 if port_exists
"${slave}"; then
77 if list_match
"${slave}" ${SLAVES}; then
78 warning
"Port ${slave} is already enslaved"
80 list_append SLAVES
"${slave}"
83 warning
"Port ${slave} does not exist"
87 local slave
=$
(cli_get_val
"${1:1}")
88 if ! list_remove SLAVES
"${slave}"; then
89 warning
"Port ${slave} is not a slave of this bonding device"
93 warning
"Unknown argument '${1}'"
99 if isset ADDRESS
; then
100 if ! ismac ADDRESS
; then
101 error
"The given MAC address is invalid: ${ADDRESS}"
105 ADDRESS
=$
(mac_generate
)
110 if ! hook_parse_cmdline
"$@"; then
115 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
119 if port_settings_write
"${port}"; then
120 log INFO
"New port ${port} has been created"
122 error
"Could not save configuration for ${port}"
132 if ! hook_default_edit
"$@"; then
136 # If the master device is up, make sure that all
138 if device_exists
"${port}"; then
139 # Setting the mode requires us to destroy the device
140 # and to recreate it again.
141 local mode
=$
(bonding_get_mode
"${port}")
142 if [ "${mode}" != "${MODE}" ]; then
143 port_restart
"${port}"
148 device_set_address
"${port}" "${ADDRESS}"
151 bonding_set_miimon
"${port}" "${MIIMON}"
154 for slave
in ${SLAVES}; do
155 if device_exists
"${slave}"; then
156 bonding_enslave_device
"${port}" "${slave}"
158 port_create
"${slave}"
168 # Exit silently if the device already exists
169 device_exists
"${port}" && exit ${EXIT_OK}
171 port_settings_read
"${port}"
173 # Create the bonding devices
174 bonding_create
"${port}" \
175 --address="${ADDRESS}" \
176 --mode="${MODE}" ||
exit ${EXIT_ERROR}
178 bonding_set_miimon
"${port}" "${MIIMON}"
187 port_settings_read
"${port}"
189 # Remove the bonding device
190 if device_exists
"${port}"; then
191 bonding_remove
"${port}"
199 port_settings_read
"${port}"
201 # Auto-enable or disable hardware offloading
202 if ! isset OFFLOADING || enabled OFFLOADING
; then
203 offloading_auto
"${port}"
205 offloading_disable_all
"${port}"
208 # Execute the default action
209 hook_default_up
"${port}"
211 # Bring up all slaves
213 for slave
in $
(unquote
${SLAVES}); do
222 port_settings_read
"${port}"
224 # Bring down all slaves
226 for slave
in $
(unquote
${SLAVES}); do
230 # Execute the default action
231 hook_default_down
"${port}"
238 case "$(hotplug_action)" in
240 # Handle events of the same interface
241 if hotplug_event_port_is_interface
"${port}"; then
243 port_settings_read
"${port}"
245 # Bring up all slaves
246 # Attach those which already exist and try to create
247 # those which don't exist yet. They will be attached
248 # in their own hotplug event.
250 for slave
in $
(unquote
${SLAVES}); do
251 if device_exists
"${slave}"; then
252 bonding_enslave_device
"${port}" "${slave}"
254 port_create
"${slave}"
260 # Handle slave devices that have just been created and
262 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
263 bonding_enslave_device
"${port}" "${INTERFACE}"
265 # If the parent device has been set up, we will
266 # bring up the slave device as well.
267 if device_is_up
"${port}"; then
268 port_up
"${INTERFACE}"
276 if hotplug_event_port_is_interface
"${port}"; then
277 # Bring down all slaves after the parent device went away
279 for slave
in $
(port_get_slaves
"${port}"); do
280 port_remove
"${slave}"
288 exit ${EXIT_NOT_HANDLED}