]>
Commit | Line | Data |
---|---|---|
1848564d MT |
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 | ||
1b7a1578 | 22 | function devicify() { |
1848564d MT |
23 | local device=${1} |
24 | ||
711ffac1 MT |
25 | assert isset device |
26 | ||
1848564d MT |
27 | if device_exists ${device}; then |
28 | echo "${device}" | |
29 | return ${EXIT_OK} | |
30 | fi | |
31 | ||
32 | local d | |
33 | for d in $(devices_get_all); do | |
34 | if [ "$(device_get_address ${d})" = "${device}" ]; then | |
35 | echo "${d}" | |
36 | return ${EXIT_OK} | |
37 | fi | |
38 | done | |
39 | ||
40 | return ${EXIT_ERROR} | |
41 | } | |
42 | ||
43 | function macify() { | |
44 | local device=${1} | |
45 | ||
711ffac1 MT |
46 | assert isset device |
47 | ||
1848564d MT |
48 | if mac_is_valid ${device}; then |
49 | echo "${device}" | |
50 | return ${EXIT_OK} | |
51 | fi | |
52 | ||
53 | if device_exists ${device}; then | |
54 | device_get_address ${device} | |
55 | return ${EXIT_OK} | |
56 | fi | |
57 | ||
58 | return ${EXIT_ERROR} | |
59 | } | |
60 | ||
61 | # Check if the device exists | |
62 | function device_exists() { | |
63 | local device=${1} | |
64 | ||
65 | # If device name was not found, exit. | |
66 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
67 | ||
68 | [ -d "${SYS_CLASS_NET}/${device}" ] | |
69 | } | |
70 | ||
71 | # Check if the device is up | |
72 | function device_is_up() { | |
73 | local device=${1} | |
74 | ||
75 | device_exists ${device} || return ${EXIT_ERROR} | |
76 | ||
77 | ip link show ${device} 2>/dev/null | grep -qE "<.*UP.*>" | |
78 | } | |
79 | ||
80 | # Check if the device is a bonding device | |
81 | function device_is_bonding() { | |
82 | [ -d "/sys/class/net/${1}/bonding" ] | |
83 | } | |
84 | ||
85 | # Check if the device bonded in a bonding device | |
86 | function device_is_bonded() { | |
711ffac1 | 87 | local device=${1} |
1848564d | 88 | |
711ffac1 | 89 | [ -d "${SYS_CLASS_NET}/${device}/master" ] |
1848564d MT |
90 | } |
91 | ||
92 | # Check if the device is a bridge | |
93 | function device_is_bridge() { | |
94 | [ -d "/sys/class/net/${1}/bridge" ] | |
95 | } | |
96 | ||
81ed640c MT |
97 | function device_is_bridge_attached() { |
98 | local device=${1} | |
99 | ||
100 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] | |
101 | } | |
102 | ||
1848564d MT |
103 | # Check if the device is a virtual device |
104 | function device_is_virtual() { | |
105 | local device=${1} | |
106 | ||
107 | [ -e "/proc/net/vlan/${device}" ] | |
108 | } | |
109 | ||
110 | # Check if the device has virtual devices | |
111 | function device_has_virtuals() { | |
fb02e543 MT |
112 | local device=${1} |
113 | ||
114 | if device_is_virtual ${device}; then | |
115 | return 1 | |
116 | fi | |
117 | ||
1848564d MT |
118 | if [ ! -e "/proc/net/vlan/config" ]; then |
119 | return 1 | |
120 | fi | |
121 | grep -q "${1}$" /proc/net/vlan/config | |
122 | } | |
123 | ||
124 | function device_is_vlan() { # XXX Compat function | |
125 | log DEBUG "Deprecated function device_is_vlan() was used." | |
126 | ||
127 | device_is_virtual $@ | |
128 | } | |
129 | ||
130 | # Check if the device is a ppp device | |
131 | function device_is_ppp() { | |
132 | local device=${1} | |
133 | ||
134 | ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>" | |
135 | } | |
136 | ||
137 | # Check if the device is a loopback device | |
138 | function device_is_loopback() { | |
139 | local device=$(devicify ${1}) | |
140 | [ "${device}" = "lo" ] | |
141 | } | |
142 | ||
143 | # Check if the device is a physical network interface | |
144 | function device_is_real() { | |
145 | local device=${1} | |
146 | ||
147 | device_is_loopback ${device} && \ | |
148 | return ${EXIT_ERROR} | |
149 | ||
150 | device_is_bonding ${device} && \ | |
151 | return ${EXIT_ERROR} | |
152 | ||
153 | device_is_bridge ${device} && \ | |
154 | return ${EXIT_ERROR} | |
155 | ||
156 | device_is_ppp ${device} && \ | |
157 | return ${EXIT_ERROR} | |
158 | ||
159 | device_is_virtual ${device} && \ | |
160 | return ${EXIT_ERROR} | |
161 | ||
162 | return ${EXIT_OK} | |
163 | } | |
164 | ||
165 | # Get the device type | |
166 | function device_get_type() { | |
167 | local device=$(devicify ${1}) | |
168 | ||
169 | if device_is_vlan ${device}; then | |
170 | echo "vlan" | |
171 | ||
172 | elif device_is_bonding ${device}; then | |
173 | echo "bonding" | |
174 | ||
175 | elif device_is_bridge ${device}; then | |
176 | echo "bridge" | |
177 | ||
178 | elif device_is_ppp ${device}; then | |
179 | echo "ppp" | |
180 | ||
181 | elif device_is_loopback ${device}; then | |
182 | echo "loopback" | |
183 | ||
184 | elif device_is_real ${device}; then | |
185 | echo "real" | |
186 | ||
187 | else | |
188 | echo "unknown" | |
189 | fi | |
190 | } | |
191 | ||
711ffac1 MT |
192 | function device_get_status() { |
193 | local device=${1} | |
194 | ||
195 | assert isset device | |
196 | ||
197 | local status=${STATUS_UNKNOWN} | |
198 | ||
199 | if ! device_has_carrier ${device}; then | |
200 | status=${STATUS_NOCARRIER} | |
201 | elif device_is_up ${device}; then | |
202 | status=${STATUS_UP} | |
203 | elif device_is_down ${device}; then | |
204 | status=${STATUS_DOWN} | |
205 | fi | |
206 | ||
207 | assert isset status | |
208 | ||
209 | echo "${status}" | |
210 | } | |
211 | ||
1848564d MT |
212 | function device_get_address() { |
213 | local device=${1} | |
214 | ||
215 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
216 | } | |
217 | ||
218 | function device_set_address() { | |
1b7a1578 MT |
219 | local device=${1} |
220 | local addr=${2} | |
221 | ||
222 | if ! device_exists ${device}; then | |
223 | error "Device '${device}' does not exist." | |
224 | return ${EXIT_ERROR} | |
225 | fi | |
226 | ||
227 | log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})." | |
228 | ||
229 | local up | |
230 | if device_is_up ${device}; then | |
231 | device_set_down ${device} | |
232 | up=1 | |
233 | fi | |
234 | ||
235 | ip link set ${device} address ${addr} | |
236 | local ret=$? | |
237 | ||
238 | if [ "${up}" = "1" ]; then | |
239 | device_set_up ${device} | |
240 | fi | |
241 | ||
242 | if [ "${ret}" != "0" ]; then | |
243 | error_log "Could not set address '${addr}' on device '${device}'." | |
244 | fi | |
245 | ||
246 | return ${ret} | |
1848564d MT |
247 | } |
248 | ||
711ffac1 MT |
249 | function device_get() { |
250 | local from_config | |
251 | ||
252 | while [ $# -gt 0 ]; do | |
253 | case "${1}" in | |
254 | --from-config) | |
255 | from_config=1 | |
256 | ;; | |
257 | --no-config) | |
258 | from_config=0 | |
259 | ;; | |
260 | esac | |
261 | shift | |
262 | done | |
263 | ||
264 | local devices | |
265 | ||
266 | if [ "${from_config}" != "0" ]; then | |
267 | devices="${devices} $(device_config_list)" | |
268 | fi | |
269 | ||
270 | if [ "${from_config}" != "1" ]; then | |
271 | local device | |
272 | for device in ${SYS_CLASS_NET}/*; do | |
273 | devices="${devices} $(basename ${device})" | |
274 | done | |
275 | fi | |
276 | ||
277 | echo ${devices} | |
278 | return ${EXIT_OK} | |
279 | } | |
280 | ||
1848564d | 281 | function devices_get_all() { |
711ffac1 | 282 | device_get |
1848564d MT |
283 | } |
284 | ||
285 | # Check if a device has a cable plugged in | |
286 | function device_has_carrier() { | |
287 | local device=$(devicify ${1}) | |
288 | [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ] | |
289 | } | |
290 | ||
1e4c26a4 MT |
291 | function device_is_promisc() { |
292 | local device=${1} | |
293 | ||
294 | ip link show ${device} | grep -qE "<.*PROMISC.*>" | |
295 | } | |
296 | ||
1848564d MT |
297 | # Check if the device is free |
298 | function device_is_free() { | |
81ed640c | 299 | ! device_is_used $@ |
1848564d MT |
300 | } |
301 | ||
302 | # Check if the device is used | |
303 | function device_is_used() { | |
304 | local device=$(devicify ${1}) | |
305 | ||
fb02e543 MT |
306 | device_has_virtuals ${device} && \ |
307 | return ${EXIT_OK} | |
1848564d | 308 | device_is_bonded ${device} && \ |
fb02e543 | 309 | return ${EXIT_OK} |
81ed640c MT |
310 | device_is_bridge_attached ${device} && \ |
311 | return ${EXIT_OK} | |
1848564d | 312 | |
fb02e543 | 313 | return ${EXIT_ERROR} |
1848564d MT |
314 | } |
315 | ||
316 | # XXX to be removed I think | |
317 | function device_get_free() { | |
318 | local destination=${1} | |
319 | ||
320 | # Replace + by a valid number | |
321 | if grep -q "+$" <<<${destination}; then | |
322 | local number=0 | |
323 | destination=$(sed -e "s/+//" <<<$destination) | |
324 | while [ "${number}" -le "100" ]; do | |
325 | if ! device_exists "${destination}${number}"; then | |
326 | destination="${destination}${number}" | |
327 | break | |
328 | fi | |
329 | number=$(($number + 1)) | |
330 | done | |
331 | fi | |
332 | echo "${destination}" | |
333 | } | |
334 | ||
1848564d | 335 | function device_rename() { |
1b7a1578 MT |
336 | warning_log "Called deprecated function 'device_rename'" |
337 | ||
338 | device_set_name $@ | |
339 | } | |
340 | ||
341 | function device_hash() { | |
342 | local device=${1} | |
343 | ||
37e4ec8b MT |
344 | # Get mac address of device and remove all colons (:) |
345 | # that will result in a hash. | |
346 | device=$(macify ${device}) | |
347 | ||
348 | echo "${device//:/}" | |
1b7a1578 MT |
349 | } |
350 | ||
351 | # Give the device a new name | |
352 | function device_set_name() { | |
1848564d MT |
353 | local source=$1 |
354 | local destination=$(device_get_free ${2}) | |
355 | ||
356 | # Check if devices exists | |
357 | if ! device_exists ${source} || device_exists ${destination}; then | |
358 | return 4 | |
359 | fi | |
360 | ||
361 | local up | |
362 | if device_is_up ${source}; then | |
363 | ip link set ${source} down | |
364 | up=1 | |
365 | fi | |
366 | ||
367 | ip link set ${source} name ${destination} | |
368 | ||
369 | if [ "${up}" = "1" ]; then | |
370 | ip link set ${destination} up | |
371 | fi | |
372 | } | |
373 | ||
1848564d MT |
374 | # Set device up |
375 | function device_set_up() { | |
376 | local device=$(devicify ${1}) | |
377 | ||
711ffac1 MT |
378 | # Silently fail if device was not found |
379 | [ -z "${device}" ] && return ${EXIT_ERROR} | |
380 | ||
1848564d MT |
381 | # Do nothing if device is already up |
382 | device_is_up ${device} && return ${EXIT_OK} | |
383 | ||
81ed640c MT |
384 | device_set_parent_up ${device} |
385 | ||
386 | log DEBUG "Setting up device '${device}'" | |
387 | ||
1848564d MT |
388 | ip link set ${device} up |
389 | } | |
390 | ||
81ed640c MT |
391 | function device_set_parent_up() { |
392 | local device=${1} | |
393 | local parent | |
394 | ||
395 | if device_is_virtual ${device}; then | |
396 | parent=$(device_virtual_get_parent ${device}) | |
397 | ||
398 | device_is_up ${parent} && return ${EXIT_OK} | |
399 | ||
400 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
401 | ||
402 | device_set_up ${parent} | |
403 | return $? | |
404 | fi | |
405 | ||
406 | return ${EXIT_OK} | |
407 | } | |
408 | ||
1848564d MT |
409 | # Set device down |
410 | function device_set_down() { | |
411 | local device=$(devicify ${1}) | |
412 | ||
81ed640c MT |
413 | local ret=${EXIT_OK} |
414 | ||
415 | if device_is_up ${device}; then | |
416 | log DEBUG "Tearing down device '${device}'" | |
417 | ||
418 | ip link set ${device} down | |
419 | ret=$? | |
420 | fi | |
421 | ||
422 | device_set_parent_down ${device} | |
1848564d | 423 | |
81ed640c MT |
424 | return ${ret} |
425 | } | |
426 | ||
427 | function device_set_parent_down() { | |
428 | local device=${1} | |
429 | local parent | |
430 | ||
431 | if device_is_virtual ${device}; then | |
432 | parent=$(device_virtual_get_parent ${device}) | |
433 | ||
434 | device_is_up ${parent} || return ${EXIT_OK} | |
435 | ||
436 | if device_is_free ${parent}; then | |
437 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
438 | ||
439 | device_set_down ${parent} | |
440 | fi | |
441 | fi | |
442 | ||
443 | return ${EXIT_OK} | |
1848564d MT |
444 | } |
445 | ||
1848564d MT |
446 | function device_get_mtu() { |
447 | local device=${1} | |
448 | ||
449 | if ! device_exists ${device}; then | |
450 | error "Device '${device}' does not exist." | |
451 | return ${EXIT_ERROR} | |
452 | fi | |
453 | ||
f3e6fe50 | 454 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
455 | } |
456 | ||
457 | # Set mtu to a device | |
458 | function device_set_mtu() { | |
1b7a1578 | 459 | local device=${1} |
1848564d MT |
460 | local mtu=${2} |
461 | ||
1b7a1578 MT |
462 | if ! device_exists ${device}; then |
463 | error "Device '${device}' does not exist." | |
464 | return ${EXIT_ERROR} | |
465 | fi | |
466 | ||
467 | local oldmtu=$(device_get_mtu ${device}) | |
468 | ||
469 | if [ "${oldmtu}" = "${mtu}" ]; then | |
470 | # No need to set mtu. | |
471 | return ${EXIT_OK} | |
472 | fi | |
473 | ||
474 | log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." | |
475 | ||
1848564d | 476 | local up |
1b7a1578 MT |
477 | if device_is_up ${device}; then |
478 | device_set_down ${device} | |
1848564d MT |
479 | up=1 |
480 | fi | |
481 | ||
1b7a1578 | 482 | ip link set ${device} mtu ${mtu} |
1848564d MT |
483 | local ret=$? |
484 | ||
485 | if [ "${up}" = "1" ]; then | |
1b7a1578 MT |
486 | device_set_up ${device} |
487 | fi | |
488 | ||
489 | if [ "${ret}" != "0" ]; then | |
490 | error_log "Could not set mtu '${mtu}' on device '${device}'." | |
1848564d MT |
491 | fi |
492 | ||
493 | return ${ret} | |
494 | } | |
495 | ||
496 | function device_discover() { | |
497 | local device=${1} | |
498 | ||
1b7a1578 MT |
499 | log INFO "Running discovery process on device '${device}'." |
500 | ||
1848564d | 501 | local hook |
d61a01d4 MT |
502 | for hook in $(hook_zone_get_all); do |
503 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
504 | done |
505 | } | |
506 | ||
507 | function device_create_virtual() { | |
508 | log WARN "Called deprecated function device_create_virtual" | |
509 | device_virtual_create $@ | |
510 | } | |
511 | ||
512 | function device_virtual_create() { | |
513 | local port=$(devicify ${1}) | |
514 | local vid=${2} | |
515 | local mac=${3} | |
516 | local newport=${port}v${vid} | |
711ffac1 | 517 | |
1848564d MT |
518 | if [ -z "${mac}" ]; then |
519 | mac=$(mac_generate) | |
520 | fi | |
521 | ||
1b7a1578 MT |
522 | log INFO "Creating virtual device '${newport}' with address '${mac}'." |
523 | ||
fb02e543 | 524 | local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid}) |
1848564d | 525 | |
fb02e543 MT |
526 | if device_exists ${oldport}; then |
527 | local differences | |
528 | ||
529 | if [ "${oldport}" != "${newport}" ]; then | |
530 | differences="${differences} name" | |
531 | fi | |
532 | if [ "$(device_get_address ${oldport})" != "${mac}" ]; then | |
533 | differences="${differences} address" | |
534 | fi | |
535 | ||
536 | echo "differences: $differences" | |
537 | ||
538 | if [ -n "${differences}" ]; then | |
539 | if device_is_used ${oldport}; then | |
540 | error_log "There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on." | |
541 | return ${EXIT_ERROR} | |
542 | else | |
543 | log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves." | |
544 | fi | |
545 | else | |
546 | log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on." | |
547 | ||
548 | device_set_up ${oldport} | |
549 | return ${EXIT_OK} | |
550 | fi | |
551 | ||
552 | else | |
553 | log DEBUG "Virtual device '${newport}' does not exist, yet." | |
554 | ||
555 | vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null | |
556 | vconfig add ${port} ${vid} >/dev/null | |
557 | ||
558 | if [ $? -ne ${EXIT_OK} ]; then | |
559 | error_log "Could not create virtual device '${newport}'." | |
560 | return ${EXIT_ERROR} | |
561 | fi | |
562 | ||
563 | oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid}) | |
564 | ||
565 | fi | |
566 | ||
567 | assert device_exists ${oldport} | |
568 | ||
569 | if ! device_exists ${oldport}; then | |
570 | error "Could not determine the created virtual device '${newport}'." | |
1b7a1578 MT |
571 | return ${EXIT_ERROR} |
572 | fi | |
1848564d MT |
573 | |
574 | # The device is expected to be named like ${port}.${vid} | |
575 | # and will be renamed to the virtual schema | |
fb02e543 | 576 | device_set_name ${oldport} ${newport} |
1848564d | 577 | |
1b7a1578 MT |
578 | if [ $? -ne ${EXIT_OK} ]; then |
579 | error_log "Could not set name of virtual device '${newport}'." | |
580 | return ${EXIT_ERROR} | |
581 | fi | |
582 | ||
fb02e543 MT |
583 | assert device_exists ${newport} |
584 | ||
1848564d MT |
585 | # Setting new mac address |
586 | device_set_address ${newport} ${mac} | |
1b7a1578 MT |
587 | |
588 | if [ $? -ne ${EXIT_OK} ]; then | |
589 | error_log "Could not set address '${mac}' to virtual device '${newport}'." | |
590 | return ${EXIT_ERROR} | |
591 | fi | |
1848564d MT |
592 | |
593 | # Bring up the new device | |
594 | device_set_up ${newport} | |
595 | ||
1848564d MT |
596 | return ${EXIT_OK} |
597 | } | |
598 | ||
599 | function device_virtual_remove() { | |
600 | local device=$(devicify ${1}) | |
601 | ||
81ed640c | 602 | log INFO "Removing virtual device '${device}' with address '$(macify ${device})'." |
1b7a1578 | 603 | |
1848564d MT |
604 | device_set_down ${device} |
605 | ||
606 | vconfig rem ${device} >/dev/null | |
1848564d | 607 | |
1b7a1578 MT |
608 | if [ $? -ne ${EXIT_OK} ]; then |
609 | error_log "Could not remote virtual device '${newport}'." | |
610 | return ${EXIT_ERROR} | |
611 | fi | |
612 | ||
1848564d MT |
613 | return ${EXIT_OK} |
614 | } | |
615 | ||
81ed640c MT |
616 | function device_virtual_get_parent() { |
617 | local device=${1} | |
618 | ||
619 | local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }') | |
620 | ||
621 | if device_exists ${parent}; then | |
622 | echo "${parent}" | |
623 | return ${EXIT_OK} | |
624 | fi | |
625 | ||
626 | return ${EXIT_ERROR} | |
627 | } | |
628 | ||
fb02e543 MT |
629 | function device_virtual_get_by_parent_and_vid() { |
630 | local parent=${1} | |
631 | local vid=${2} | |
632 | ||
711ffac1 MT |
633 | assert isset parent |
634 | assert isset vid | |
635 | ||
fb02e543 MT |
636 | local v_port |
637 | local v_id | |
638 | local v_parent | |
639 | ||
943e3f7e MT |
640 | assert [ -e "/proc/net/vlan/config" ] |
641 | ||
fb02e543 MT |
642 | fgrep '|' < /proc/net/vlan/config | tr -d '|' | \ |
643 | while read v_port v_id v_parent; do | |
644 | if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then | |
645 | echo "${v_port}" | |
646 | return ${EXIT_OK} | |
647 | fi | |
648 | done | |
649 | ||
650 | return ${EXIT_ERROR} | |
651 | } | |
652 | ||
1848564d MT |
653 | function bridge_attach_device() { |
654 | local bridge=${1} | |
655 | local device=${2} | |
1848564d | 656 | |
711ffac1 MT |
657 | assert isset bridge |
658 | assert isset device | |
659 | ||
660 | assert device_exists ${bridge} | |
661 | assert device_exists ${device} | |
662 | ||
663 | # If device is already attached, exit silently | |
664 | if listmatch ${device} $(bridge_get_members ${bridge}); then | |
665 | return ${EXIT_OK} | |
1848564d MT |
666 | fi |
667 | ||
1b7a1578 MT |
668 | log INFO "Attaching device '${device}' to bridge '${bridge}'." |
669 | ||
1848564d MT |
670 | brctl addif ${bridge} ${device} |
671 | } | |
672 | ||
673 | function bridge_detach_device() { | |
674 | local bridge=${1} | |
675 | local device=${2} | |
676 | ||
711ffac1 MT |
677 | assert isset bridge |
678 | assert isset device | |
679 | ||
1848564d MT |
680 | if ! device_exists ${bridge}; then |
681 | error "Bridge '${bridge}' does not exist." | |
682 | return ${EXIT_ERROR} | |
683 | fi | |
684 | ||
685 | if ! device_exists ${device}; then | |
686 | error "Device '${device}' does not exist." | |
687 | return ${EXIT_ERROR} | |
688 | fi | |
689 | ||
1b7a1578 MT |
690 | log INFO "Detaching device '${device}' from bridge '${bridge}'." |
691 | ||
692 | brctl delif ${bridge} ${device} | |
711ffac1 MT |
693 | } |
694 | ||
695 | function bridge_get_members() { | |
696 | local bridge=${1} | |
697 | ||
698 | assert isset bridge | |
699 | ||
700 | local member | |
701 | for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do | |
eb6ae623 | 702 | member=$(basename ${member}) |
711ffac1 MT |
703 | if device_exists ${member}; then |
704 | echo "${member}" | |
705 | fi | |
706 | done | |
1848564d MT |
707 | } |
708 | ||
709 | function bridge_is_forwarding() { | |
710 | local seconds=45 | |
711 | local zone=${1} | |
712 | ||
713 | bridge_has_carrier ${zone} || return ${EXIT_ERROR} | |
714 | ||
715 | local device | |
716 | while [ ${seconds} -gt 0 ]; do | |
717 | for device in ${SYS_CLASS_NET}/${zone}/brif/*; do | |
718 | [ -e "${device}/state" ] || continue | |
719 | if [ "$(<${device}/state)" = "3" ]; then | |
720 | return ${EXIT_OK} | |
721 | fi | |
722 | done | |
723 | sleep 1 | |
724 | seconds=$((${seconds} - 1)) | |
725 | done | |
726 | ||
727 | return ${EXIT_ERROR} | |
728 | } | |
729 | ||
730 | function bridge_has_carrier() { | |
731 | local zone=${1} | |
732 | ||
733 | local has_carrier=${EXIT_ERROR} | |
734 | ||
735 | local device | |
736 | for device in ${SYS_CLASS_NET}/${zone}/brif/*; do | |
737 | device=$(basename ${device}) | |
738 | device_exists ${device} || continue | |
739 | ||
740 | device_has_carrier ${device} && has_carrier=${EXIT_OK} | |
741 | done | |
742 | ||
743 | return ${has_carrier} | |
744 | } | |
745 | ||
746 | function device_has_ipv4() { | |
747 | local device=${1} | |
748 | local addr=${2} | |
749 | ||
750 | if ! device_exists ${device}; then | |
751 | error "Device '${device}' does not exist." | |
752 | return ${EXIT_ERROR} | |
753 | fi | |
754 | ||
755 | ip addr show ${device} | grep -q -e "inet " -e "${addr}" | |
756 | } | |
4231f419 MT |
757 | |
758 | function device_has_ipv6() { | |
759 | local device=${1} | |
760 | local addr=${2} | |
761 | ||
762 | if ! device_exists ${device}; then | |
763 | error "Device '${device}' does not exist." | |
764 | return ${EXIT_ERROR} | |
765 | fi | |
766 | ||
767 | local prefix=${addr##*/} | |
768 | addr=$(ipv6_implode ${addr%%/*}) | |
769 | ||
770 | if [ -n "${prefix}" ]; then | |
771 | addr="${addr}/${prefix}" | |
772 | fi | |
773 | ||
774 | ip addr show ${device} | grep -q "inet6 ${addr}" | |
775 | } | |
711ffac1 | 776 | |
711ffac1 MT |
777 | function __device_get_file() { |
778 | local device=${1} | |
779 | local file=${2} | |
780 | ||
781 | assert isset device | |
782 | assert isset file | |
783 | ||
784 | cat ${SYS_CLASS_NET}/${device}/${file} | |
785 | } | |
786 | ||
787 | function device_get_rx_bytes() { | |
788 | local device=${1} | |
789 | ||
790 | __device_get_file ${device} statistics/rx_bytes | |
791 | } | |
792 | ||
793 | function device_get_tx_bytes() { | |
794 | local device=${1} | |
795 | ||
796 | __device_get_file ${device} statistics/tx_bytes | |
797 | } | |
798 | ||
799 | function device_get_rx_packets() { | |
800 | local device=${1} | |
801 | ||
802 | __device_get_file ${device} statistics/rx_packets | |
803 | } | |
804 | ||
805 | function device_get_tx_packets() { | |
806 | local device=${1} | |
807 | ||
808 | __device_get_file ${device} statistics/tx_packets | |
809 | } | |
810 | ||
811 | function device_get_rx_errors() { | |
812 | local device=${1} | |
813 | ||
814 | __device_get_file ${device} statistics/rx_errors | |
815 | } | |
816 | ||
817 | function device_get_tx_errors() { | |
818 | local device=${1} | |
819 | ||
820 | __device_get_file ${device} statistics/tx_errors | |
821 | } |