]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/bonding
bonding; Validate any MAC address passed
[people/stevee/network.git] / src / hooks / ports / bonding
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/network/header-port
23
24 HOOK_SETTINGS="ADDRESS MIIMON MODE OFFLOADING SLAVES"
25
26 SLAVES=""
27 MIIMON=100
28 MODE="balance-rr"
29
30 hook_check_settings() {
31 assert isset ADDRESS
32 assert ismac ADDRESS
33
34 #assert isset SLAVES
35 assert isinteger MIIMON
36 }
37
38 hook_parse_cmdline() {
39 while [ $# -gt 0 ]; do
40 case "${1}" in
41 --address=*)
42 ADDRESS="$(cli_get_val "${1}")"
43
44 if ! mac_is_valid "${ADDRESS}"; then
45 error "Invalid MAC address: ${ADDRESS}"
46 return ${EXIT_ERROR}
47 fi
48 ;;
49
50 --miimon=*)
51 MIIMON=$(cli_get_val "${1}")
52 ;;
53 --mode=*)
54 MODE=$(cli_get_val "${1}")
55 ;;
56 --offloading=*)
57 OFFLOADING="$(cli_get_val "${1}")"
58
59 if enabled OFFLOADING; then
60 OFFLOADING="on"
61 elif disabled OFFLOADING; then
62 OFFLOADING="off"
63 else
64 error "Invalid value for offloading: ${OFFLOADING}"
65 return ${EXIT_ERROR}
66 fi
67 ;;
68 +*)
69 local slave=$(cli_get_val "${1:1}")
70
71 if port_exists "${slave}"; then
72 if list_match "${slave}" ${SLAVES}; then
73 warning "Port ${slave} is already enslaved"
74 else
75 list_append SLAVES "${slave}"
76 fi
77 else
78 warning "Port ${slave} does not exist"
79 fi
80 ;;
81 -*)
82 local slave=$(cli_get_val "${1:1}")
83 if ! list_remove SLAVES "${slave}"; then
84 warning "Port ${slave} is not a slave of this bonding device"
85 fi
86 ;;
87 *)
88 warning "Unknown argument '${1}'"
89 ;;
90 esac
91 shift
92 done
93
94 if isset ADDRESS; then
95 if ! ismac ADDRESS; then
96 error "The given MAC address is invalid: ${ADDRESS}"
97 return ${EXIT_ERROR}
98 fi
99 else
100 ADDRESS=$(mac_generate)
101 fi
102 }
103
104 hook_new() {
105 if ! hook_parse_cmdline "$@"; then
106 return ${EXIT_ERROR}
107 fi
108
109 # Find a new name
110 local port=$(port_find_free ${BONDING_PORT_PATTERN})
111 assert isset port
112
113 # Save configuration
114 if port_settings_write "${port}" ${HOOK_SETTINGS}; then
115 log INFO "New port ${port} has been created"
116 else
117 error "Could not save configuration for ${port}"
118 return ${EXIT_ERROR}
119 fi
120
121 return ${EXIT_OK}
122 }
123
124 hook_edit() {
125 local port=${1}
126
127 if ! hook_default_edit "$@"; then
128 return ${EXIT_ERROR}
129 fi
130
131 # If the master device is up, make sure that all
132 # slaves are up, too
133 if device_exists "${port}"; then
134 # Setting the mode requires us to destroy the device
135 # and to recreate it again.
136 local mode=$(bonding_get_mode "${port}")
137 if [ "${mode}" != "${MODE}" ]; then
138 port_restart "${port}"
139 return ${EXIT_OK}
140 fi
141
142 # Set address
143 device_set_address "${port}" "${ADDRESS}"
144
145 # Set miimon
146 bonding_set_miimon "${port}" "${MIIMON}"
147
148 local slave
149 for slave in ${SLAVES}; do
150 if device_exists "${slave}"; then
151 bonding_enslave_device "${port}" "${slave}"
152 else
153 port_create "${slave}"
154 fi
155 done
156 fi
157 }
158
159 hook_create() {
160 local port="${1}"
161 assert isset port
162
163 # Exit silently if the device already exists
164 device_exists "${port}" && exit ${EXIT_OK}
165
166 port_settings_read "${port}" ${HOOK_SETTINGS}
167
168 # Create the bonding devices
169 bonding_create "${port}" \
170 --address="${ADDRESS}" \
171 --mode="${MODE}" || exit ${EXIT_ERROR}
172
173 bonding_set_miimon "${port}" "${MIIMON}"
174
175 exit ${EXIT_OK}
176 }
177
178 hook_remove() {
179 local port="${1}"
180 assert isset port
181
182 port_settings_read "${port}" ${HOOK_SETTINGS}
183
184 # Remove the bonding device
185 if device_exists "${port}"; then
186 bonding_remove "${port}"
187 fi
188 }
189
190 hook_up() {
191 local port="${1}"
192 assert isset port
193
194 port_settings_read "${port}" ${HOOK_SETTINGS}
195
196 # Auto-enable or disable hardware offloading
197 if ! isset OFFLOADING || enabled OFFLOADING; then
198 offloading_auto "${port}"
199 else
200 offloading_disable_all "${port}"
201 fi
202
203 # Execute the default action
204 hook_default_up "${port}"
205
206 # Bring up all slaves
207 local slave
208 for slave in $(unquote ${SLAVES}); do
209 port_up "${slave}"
210 done
211 }
212
213 hook_down() {
214 local port="${1}"
215 assert isset port
216
217 port_settings_read "${port}" ${HOOK_SETTINGS}
218
219 # Bring down all slaves
220 local slave
221 for slave in $(unquote ${SLAVES}); do
222 port_down "${slave}"
223 done
224
225 # Execute the default action
226 hook_default_down "${port}"
227 }
228
229 hook_hotplug() {
230 local port="${1}"
231 assert isset port
232
233 case "$(hotplug_action)" in
234 add)
235 # Handle events of the same interface
236 if hotplug_event_port_is_interface "${port}"; then
237 # Read configuration
238 port_settings_read "${port}" ${HOOK_SETTINGS}
239
240 # Bring up all slaves
241 # Attach those which already exist and try to create
242 # those which don't exist yet. They will be attached
243 # in their own hotplug event.
244 local slave
245 for slave in $(unquote ${SLAVES}); do
246 if device_exists "${slave}"; then
247 bonding_enslave_device "${port}" "${slave}"
248 else
249 port_create "${slave}"
250 fi
251 done
252
253 exit ${EXIT_OK}
254
255 # Handle slave devices that have just been created and
256 # attach them.
257 elif hotplug_event_interface_is_slave_of_port "${port}"; then
258 bonding_enslave_device "${port}" "${INTERFACE}"
259
260 # If the parent device has been set up, we will
261 # bring up the slave device as well.
262 if device_is_up "${port}"; then
263 port_up "${INTERFACE}"
264 fi
265 fi
266
267 exit ${EXIT_OK}
268 ;;
269
270 remove)
271 if hotplug_event_port_is_interface "${port}"; then
272 # Bring down all slaves after the parent device went away
273 local slave
274 for slave in $(port_get_slaves "${port}"); do
275 port_remove "${slave}"
276 done
277
278 exit ${EXIT_OK}
279 fi
280 ;;
281 esac
282
283 exit ${EXIT_NOT_HANDLED}
284 }