]> git.ipfire.org Git - people/stevee/network.git/blob - functions.device
network: Again very much changes that are hard to break down.
[people/stevee/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 assert [ -e "/proc/net/vlan/config" ]
641
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
653 function bridge_attach_device() {
654 local bridge=${1}
655 local device=${2}
656
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}
666 fi
667
668 log INFO "Attaching device '${device}' to bridge '${bridge}'."
669
670 # XXX device_set_up ${device} # Do we need this here?
671
672 brctl addif ${bridge} ${device}
673 }
674
675 function bridge_detach_device() {
676 local bridge=${1}
677 local device=${2}
678
679 assert isset bridge
680 assert isset device
681
682 if ! device_exists ${bridge}; then
683 error "Bridge '${bridge}' does not exist."
684 return ${EXIT_ERROR}
685 fi
686
687 if ! device_exists ${device}; then
688 error "Device '${device}' does not exist."
689 return ${EXIT_ERROR}
690 fi
691
692 log INFO "Detaching device '${device}' from bridge '${bridge}'."
693
694 brctl delif ${bridge} ${device}
695
696 #device_set_down ${device}
697 }
698
699 function bridge_get_members() {
700 local bridge=${1}
701
702 assert isset bridge
703
704 local member
705 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
706 member=$(basename ${master})
707 if device_exists ${member}; then
708 echo "${member}"
709 fi
710 done
711 }
712
713 function bridge_is_forwarding() {
714 local seconds=45
715 local zone=${1}
716
717 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
718
719 local device
720 while [ ${seconds} -gt 0 ]; do
721 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
722 [ -e "${device}/state" ] || continue
723 if [ "$(<${device}/state)" = "3" ]; then
724 return ${EXIT_OK}
725 fi
726 done
727 sleep 1
728 seconds=$((${seconds} - 1))
729 done
730
731 return ${EXIT_ERROR}
732 }
733
734 function bridge_has_carrier() {
735 local zone=${1}
736
737 local has_carrier=${EXIT_ERROR}
738
739 local device
740 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
741 device=$(basename ${device})
742 device_exists ${device} || continue
743
744 device_has_carrier ${device} && has_carrier=${EXIT_OK}
745 done
746
747 return ${has_carrier}
748 }
749
750 function device_has_ipv4() {
751 local device=${1}
752 local addr=${2}
753
754 if ! device_exists ${device}; then
755 error "Device '${device}' does not exist."
756 return ${EXIT_ERROR}
757 fi
758
759 ip addr show ${device} | grep -q -e "inet " -e "${addr}"
760 }
761
762 function device_has_ipv6() {
763 local device=${1}
764 local addr=${2}
765
766 if ! device_exists ${device}; then
767 error "Device '${device}' does not exist."
768 return ${EXIT_ERROR}
769 fi
770
771 local prefix=${addr##*/}
772 addr=$(ipv6_implode ${addr%%/*})
773
774 if [ -n "${prefix}" ]; then
775 addr="${addr}/${prefix}"
776 fi
777
778 ip addr show ${device} | grep -q "inet6 ${addr}"
779 }
780
781 function __device_get_file() {
782 local device=${1}
783 local file=${2}
784
785 assert isset device
786 assert isset file
787
788 cat ${SYS_CLASS_NET}/${device}/${file}
789 }
790
791 function device_get_rx_bytes() {
792 local device=${1}
793
794 __device_get_file ${device} statistics/rx_bytes
795 }
796
797 function device_get_tx_bytes() {
798 local device=${1}
799
800 __device_get_file ${device} statistics/tx_bytes
801 }
802
803 function device_get_rx_packets() {
804 local device=${1}
805
806 __device_get_file ${device} statistics/rx_packets
807 }
808
809 function device_get_tx_packets() {
810 local device=${1}
811
812 __device_get_file ${device} statistics/tx_packets
813 }
814
815 function device_get_rx_errors() {
816 local device=${1}
817
818 __device_get_file ${device} statistics/rx_errors
819 }
820
821 function device_get_tx_errors() {
822 local device=${1}
823
824 __device_get_file ${device} statistics/tx_errors
825 }