]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.device
network: Magnificent changes on code.
[people/arne_f/network.git] / functions.device
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 function devicify() {
23 local device=${1}
24
25 assert isset device
26
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
46 assert isset device
47
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() {
87 local device=${1}
88
89 [ -d "${SYS_CLASS_NET}/${device}/master" ]
90 }
91
92 # Check if the device is a bridge
93 function device_is_bridge() {
94 [ -d "/sys/class/net/${1}/bridge" ]
95 }
96
97 function device_is_bridge_attached() {
98 local device=${1}
99
100 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
101 }
102
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() {
112 local device=${1}
113
114 if device_is_virtual ${device}; then
115 return 1
116 fi
117
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
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
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() {
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}
247 }
248
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
281 function devices_get_all() {
282 device_get
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
291 function device_is_promisc() {
292 local device=${1}
293
294 ip link show ${device} | grep -qE "<.*PROMISC.*>"
295 }
296
297 # Check if the device is free
298 function device_is_free() {
299 ! device_is_used $@
300 }
301
302 # Check if the device is used
303 function device_is_used() {
304 local device=$(devicify ${1})
305
306 device_has_virtuals ${device} && \
307 return ${EXIT_OK}
308 device_is_bonded ${device} && \
309 return ${EXIT_OK}
310 device_is_bridge_attached ${device} && \
311 return ${EXIT_OK}
312
313 return ${EXIT_ERROR}
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
335 function device_rename() {
336 warning_log "Called deprecated function 'device_rename'"
337
338 device_set_name $@
339 }
340
341 function device_hash() {
342 local device=${1}
343
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//:/}"
349 }
350
351 # Give the device a new name
352 function device_set_name() {
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
374 # Set device up
375 function device_set_up() {
376 local device=$(devicify ${1})
377
378 # Silently fail if device was not found
379 [ -z "${device}" ] && return ${EXIT_ERROR}
380
381 # Do nothing if device is already up
382 device_is_up ${device} && return ${EXIT_OK}
383
384 device_set_parent_up ${device}
385
386 log DEBUG "Setting up device '${device}'"
387
388 ip link set ${device} up
389 }
390
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
409 # Set device down
410 function device_set_down() {
411 local device=$(devicify ${1})
412
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}
423
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}
444 }
445
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
454 echo $(<${SYS_CLASS_NET}/${device}/mtu)
455 }
456
457 # Set mtu to a device
458 function device_set_mtu() {
459 local device=${1}
460 local mtu=${2}
461
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
476 local up
477 if device_is_up ${device}; then
478 device_set_down ${device}
479 up=1
480 fi
481
482 ip link set ${device} mtu ${mtu}
483 local ret=$?
484
485 if [ "${up}" = "1" ]; then
486 device_set_up ${device}
487 fi
488
489 if [ "${ret}" != "0" ]; then
490 error_log "Could not set mtu '${mtu}' on device '${device}'."
491 fi
492
493 return ${ret}
494 }
495
496 function device_discover() {
497 local device=${1}
498
499 log INFO "Running discovery process on device '${device}'."
500
501 local hook
502 for hook in $(hook_zone_get_all); do
503 hook_zone_exec ${hook} discover ${device}
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}
517
518 if [ -z "${mac}" ]; then
519 mac=$(mac_generate)
520 fi
521
522 log INFO "Creating virtual device '${newport}' with address '${mac}'."
523
524 local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
525
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}'."
571 return ${EXIT_ERROR}
572 fi
573
574 # The device is expected to be named like ${port}.${vid}
575 # and will be renamed to the virtual schema
576 device_set_name ${oldport} ${newport}
577
578 if [ $? -ne ${EXIT_OK} ]; then
579 error_log "Could not set name of virtual device '${newport}'."
580 return ${EXIT_ERROR}
581 fi
582
583 assert device_exists ${newport}
584
585 # Setting new mac address
586 device_set_address ${newport} ${mac}
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
592
593 # Bring up the new device
594 device_set_up ${newport}
595
596 return ${EXIT_OK}
597 }
598
599 function device_virtual_remove() {
600 local device=$(devicify ${1})
601
602 log INFO "Removing virtual device '${device}' with address '$(macify ${device})'."
603
604 device_set_down ${device}
605
606 vconfig rem ${device} >/dev/null
607
608 if [ $? -ne ${EXIT_OK} ]; then
609 error_log "Could not remote virtual device '${newport}'."
610 return ${EXIT_ERROR}
611 fi
612
613 return ${EXIT_OK}
614 }
615
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
629 function device_virtual_get_by_parent_and_vid() {
630 local parent=${1}
631 local vid=${2}
632
633 assert isset parent
634 assert isset vid
635
636 local v_port
637 local v_id
638 local v_parent
639
640 fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
641 while read v_port v_id v_parent; do
642 if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
643 echo "${v_port}"
644 return ${EXIT_OK}
645 fi
646 done
647
648 return ${EXIT_ERROR}
649 }
650
651 function bridge_attach_device() {
652 local bridge=${1}
653 local device=${2}
654
655 assert isset bridge
656 assert isset device
657
658 assert device_exists ${bridge}
659 assert device_exists ${device}
660
661 # If device is already attached, exit silently
662 if listmatch ${device} $(bridge_get_members ${bridge}); then
663 return ${EXIT_OK}
664 fi
665
666 log INFO "Attaching device '${device}' to bridge '${bridge}'."
667
668 # XXX device_set_up ${device} # Do we need this here?
669
670 brctl addif ${bridge} ${device}
671 }
672
673 function bridge_detach_device() {
674 local bridge=${1}
675 local device=${2}
676
677 assert isset bridge
678 assert isset device
679
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
690 log INFO "Detaching device '${device}' from bridge '${bridge}'."
691
692 brctl delif ${bridge} ${device}
693
694 #device_set_down ${device}
695 }
696
697 function bridge_get_members() {
698 local bridge=${1}
699
700 assert isset bridge
701
702 local member
703 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
704 member=$(basename ${master})
705 if device_exists ${member}; then
706 echo "${member}"
707 fi
708 done
709 }
710
711 function bridge_is_forwarding() {
712 local seconds=45
713 local zone=${1}
714
715 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
716
717 local device
718 while [ ${seconds} -gt 0 ]; do
719 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
720 [ -e "${device}/state" ] || continue
721 if [ "$(<${device}/state)" = "3" ]; then
722 return ${EXIT_OK}
723 fi
724 done
725 sleep 1
726 seconds=$((${seconds} - 1))
727 done
728
729 return ${EXIT_ERROR}
730 }
731
732 function bridge_has_carrier() {
733 local zone=${1}
734
735 local has_carrier=${EXIT_ERROR}
736
737 local device
738 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
739 device=$(basename ${device})
740 device_exists ${device} || continue
741
742 device_has_carrier ${device} && has_carrier=${EXIT_OK}
743 done
744
745 return ${has_carrier}
746 }
747
748 function device_has_ipv4() {
749 local device=${1}
750 local addr=${2}
751
752 if ! device_exists ${device}; then
753 error "Device '${device}' does not exist."
754 return ${EXIT_ERROR}
755 fi
756
757 ip addr show ${device} | grep -q -e "inet " -e "${addr}"
758 }
759
760 function device_has_ipv6() {
761 local device=${1}
762 local addr=${2}
763
764 if ! device_exists ${device}; then
765 error "Device '${device}' does not exist."
766 return ${EXIT_ERROR}
767 fi
768
769 local prefix=${addr##*/}
770 addr=$(ipv6_implode ${addr%%/*})
771
772 if [ -n "${prefix}" ]; then
773 addr="${addr}/${prefix}"
774 fi
775
776 ip addr show ${device} | grep -q "inet6 ${addr}"
777 }
778
779 function device_config_list() {
780 local device
781 for device in ${CONFIG_DIR}/devices/*; do
782 device=$(basename ${device})
783 if device_config_exists ${device}; then
784 echo "${device}"
785 fi
786 done
787 }
788
789 function device_config_exists() {
790 local device=${1}
791
792 [ -e "${CONFIG_DIR}/devices/${device}" ]
793 }
794
795 function device_config_write() {
796 local device=${1}
797 shift
798
799 config_write ${CONFIG_DIR}/devices/${device} $@
800 }
801
802 function device_config_read() {
803 local device=${1}
804
805 config_read ${CONFIG_DIR}/devices/${device}
806 }
807
808 function device_create() {
809 local hook=${1}
810 shift
811
812 if ! hook_exists device ${hook}; then
813 error "Hook '${hook}' does not exist."
814 return ${EXIT_ERROR}
815 fi
816
817 hook_exec device ${hook} create $@
818 }
819
820 # XXX to be fixed
821 function device_up() {
822 local device=${1}
823 shift
824
825 if ! device_config_exists ${device}; then
826 error "Device '${device}' does not exist."
827 return ${EXIT_ERROR}
828 fi
829
830 local hook=$(config_get_hook ${CONFIG_DIR}/devices/${device})
831
832 if ! hook_exists device ${hook}; then
833 error "Hook '${hook}' does not exist."
834 return ${EXIT_ERROR}
835 fi
836
837 hook_exec device ${hook} up ${device} $@
838 }
839
840 # XXX to be fixed
841 function device_down() {
842 local device=${1}
843 shift
844
845 if ! device_config_exists ${device}; then
846 error "Device '${device}' does not exist."
847 return ${EXIT_ERROR}
848 fi
849
850 local hook=$(config_get_hook ${CONFIG_DIR}/devices/${device})
851
852 if ! hook_exists device ${hook}; then
853 error "Hook '${hook}' does not exist."
854 return ${EXIT_ERROR}
855 fi
856
857 hook_exec device ${hook} down ${device} $@
858 }
859
860 function device_print() {
861 local device=${1}
862 local type=$(device_get_type ${device})
863
864 echo "${device}"
865 printf "${DEVICE_PRINT_LINE1}" "Type:" "${type}"
866
867 #device_config_exists ${device}
868 #local has_config=$?
869
870 # XXX need something is_function() method here
871 if [ -n "$(type -t ${type}_device_print)" ]; then
872 ${type}_device_print ${device}
873 fi
874
875 echo # Empty line
876 }
877
878 function __device_get_file() {
879 local device=${1}
880 local file=${2}
881
882 assert isset device
883 assert isset file
884
885 cat ${SYS_CLASS_NET}/${device}/${file}
886 }
887
888 function device_get_rx_bytes() {
889 local device=${1}
890
891 __device_get_file ${device} statistics/rx_bytes
892 }
893
894 function device_get_tx_bytes() {
895 local device=${1}
896
897 __device_get_file ${device} statistics/tx_bytes
898 }
899
900 function device_get_rx_packets() {
901 local device=${1}
902
903 __device_get_file ${device} statistics/rx_packets
904 }
905
906 function device_get_tx_packets() {
907 local device=${1}
908
909 __device_get_file ${device} statistics/tx_packets
910 }
911
912 function device_get_rx_errors() {
913 local device=${1}
914
915 __device_get_file ${device} statistics/rx_errors
916 }
917
918 function device_get_tx_errors() {
919 local device=${1}
920
921 __device_get_file ${device} statistics/tx_errors
922 }