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
="ADDRESS MIIMON MODE OFFLOADING SLAVES"
27 DEFAULT_MODE
="balance-rr"
29 hook_check_settings
() {
34 assert isinteger MIIMON
37 hook_parse_cmdline
() {
38 while [ $# -gt 0 ]; do
41 ADDRESS
="$(cli_get_val "${1}")"
43 if ! mac_is_valid
"${ADDRESS}"; then
44 error
"Invalid MAC address: ${ADDRESS}"
50 MIIMON
=$
(cli_get_val
"${1}")
53 MODE
=$
(cli_get_val
"${1}")
56 OFFLOADING
="$(cli_get_val "${1}")"
58 if enabled OFFLOADING
; then
60 elif disabled OFFLOADING
; then
63 error
"Invalid value for offloading: ${OFFLOADING}"
68 local slave
=$
(cli_get_val
"${1:1}")
70 if port_exists
"${slave}"; then
71 if list_match
"${slave}" ${SLAVES}; then
72 warning
"Port ${slave} is already enslaved"
74 list_append SLAVES
"${slave}"
77 warning
"Port ${slave} does not exist"
81 local slave
=$
(cli_get_val
"${1:1}")
82 if ! list_remove SLAVES
"${slave}"; then
83 warning
"Port ${slave} is not a slave of this bonding device"
87 warning
"Unknown argument '${1}'"
93 if isset ADDRESS
; then
94 if ! ismac ADDRESS
; then
95 error
"The given MAC address is invalid: ${ADDRESS}"
99 ADDRESS
=$
(mac_generate
)
104 if ! hook_parse_cmdline
"$@"; then
109 local port
=$
(port_find_free
${BONDING_PORT_PATTERN})
113 if port_settings_write
"${port}" ${HOOK_SETTINGS}; then
114 log INFO
"New port ${port} has been created"
116 error
"Could not save configuration for ${port}"
126 if ! hook_default_edit
"$@"; then
130 # If the master device is up, make sure that all
132 if device_exists
"${port}"; then
133 # Setting the mode requires us to destroy the device
134 # and to recreate it again.
135 local mode
=$
(bonding_get_mode
"${port}")
136 if [ "${mode}" != "${MODE}" ]; then
137 port_restart
"${port}"
142 device_set_address
"${port}" "${ADDRESS}"
145 bonding_set_miimon
"${port}" "${MIIMON}"
148 for slave
in ${SLAVES}; do
149 if device_exists
"${slave}"; then
150 bonding_enslave_device
"${port}" "${slave}"
152 port_create
"${slave}"
162 # Exit silently if the device already exists
163 device_exists
"${port}" && exit ${EXIT_OK}
165 port_settings_read
"${port}" ${HOOK_SETTINGS}
167 # Create the bonding devices
168 bonding_create
"${port}" \
169 --address="${ADDRESS}" \
170 --mode="${MODE}" ||
exit ${EXIT_ERROR}
172 bonding_set_miimon
"${port}" "${MIIMON}"
181 port_settings_read
"${port}" ${HOOK_SETTINGS}
183 # Remove the bonding device
184 if device_exists
"${port}"; then
185 bonding_remove
"${port}"
193 port_settings_read
"${port}" ${HOOK_SETTINGS}
195 # Auto-enable or disable hardware offloading
196 if ! isset OFFLOADING || enabled OFFLOADING
; then
197 offloading_auto
"${port}"
199 offloading_disable_all
"${port}"
202 # Execute the default action
203 hook_default_up
"${port}"
205 # Bring up all slaves
207 for slave
in $
(unquote
${SLAVES}); do
216 port_settings_read
"${port}" ${HOOK_SETTINGS}
218 # Bring down all slaves
220 for slave
in $
(unquote
${SLAVES}); do
224 # Execute the default action
225 hook_default_down
"${port}"
232 case "$(hotplug_action)" in
234 # Handle events of the same interface
235 if hotplug_event_port_is_interface
"${port}"; then
237 port_settings_read
"${port}" ${HOOK_SETTINGS}
239 # Bring up all slaves
240 # Attach those which already exist and try to create
241 # those which don't exist yet. They will be attached
242 # in their own hotplug event.
244 for slave
in $
(unquote
${SLAVES}); do
245 if device_exists
"${slave}"; then
246 bonding_enslave_device
"${port}" "${slave}"
248 port_create
"${slave}"
254 # Handle slave devices that have just been created and
256 elif hotplug_event_interface_is_slave_of_port
"${port}"; then
257 bonding_enslave_device
"${port}" "${INTERFACE}"
259 # If the parent device has been set up, we will
260 # bring up the slave device as well.
261 if device_is_up
"${port}"; then
262 port_up
"${INTERFACE}"
270 if hotplug_event_port_is_interface
"${port}"; then
271 # Bring down all slaves after the parent device went away
273 for slave
in $
(port_get_slaves
"${port}"); do
274 port_remove
"${slave}"
282 exit ${EXIT_NOT_HANDLED}