]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.device
d4318f843a20f360b7490d9f0bfa70e7b385dcfa
[people/ms/network.git] / src / functions / 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 # Check for a normal network device.
69 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
70
71 # If the check above, did not find a result,
72 # we check for serial devices.
73 serial_exists ${device}
74 }
75
76 function device_matches_pattern() {
77 local device="${1}"
78 assert isset device
79
80 local pattern="${2}"
81 assert isset pattern
82
83 pattern="^${pattern//N/[[:digit:]]+}$"
84
85 [[ ${device} =~ ${pattern} ]] \
86 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
87 }
88
89 function device_delete() {
90 local device=${1}
91 assert isset device
92
93 # Nothing to do, it device does not exist.
94 device_exists ${device} || return ${EXIT_OK}
95
96 # Delete the device.
97 cmd_quiet ip link delete ${device}
98 local ret=$?
99
100 if [ ${ret} -ne ${EXIT_OK} ]; then
101 log ERROR "device: Could not delete device '${device}': ${ret}"
102 return ${EXIT_ERROR}
103 fi
104
105 return ${ret}
106 }
107
108 function device_has_flag() {
109 local device=${1}
110 local flag=${2}
111
112 local flags=$(__device_get_file ${device} flags)
113
114 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
115 return ${EXIT_FALSE}
116 else
117 return ${EXIT_TRUE}
118 fi
119 }
120
121 # Check if the device is up
122 function device_is_up() {
123 local device=${1}
124
125 device_exists ${device} || return ${EXIT_ERROR}
126
127 device_has_flag ${device} 0x1
128 }
129
130 function device_ifindex_to_name() {
131 local idx=${1}
132 assert isset idx
133
134 local device device_idx
135 for device in ${SYS_CLASS_NET}/*; do
136 device=$(basename ${device})
137 device_exists ${device} || continue
138
139 device_idx=$(device_get_ifindex ${device})
140
141 if [ "${device_idx}" = "${idx}" ]; then
142 print "${device}"
143 return ${EXIT_OK}
144 fi
145 done
146
147 return ${EXIT_ERROR}
148 }
149
150 function device_get_ifindex() {
151 local device=${1}
152 assert isset device
153
154 local path="${SYS_CLASS_NET}/${1}/ifindex"
155
156 # Check if file can be read.
157 [ -r "${path}" ] || return ${EXIT_ERROR}
158
159 print "$(<${path})"
160 }
161
162 # Check if the device is a batman-adv bridge
163 function device_is_batman_adv() {
164 [ -d "${SYS_CLASS_NET}/${1}/mesh" ]
165 }
166
167 # Check if the device is a batman-adv slave port
168 function device_is_batman_adv_slave() {
169 local device="${1}"
170
171 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then
172 local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)"
173
174 case "${status}" in
175 "active")
176 return ${EXIT_TRUE}
177 ;;
178 *)
179 return ${EXIT_FALSE}
180 ;;
181 esac
182 fi
183
184 return ${EXIT_FALSE}
185 }
186
187 # Check if the device is a bonding device
188 function device_is_bonding() {
189 [ -d "/sys/class/net/${1}/bonding" ]
190 }
191
192 # Check if the device bonded in a bonding device
193 function device_is_bonded() {
194 local device=${1}
195
196 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
197 }
198
199 # Check if the device is a bridge
200 function device_is_bridge() {
201 [ -d "/sys/class/net/${1}/bridge" ]
202 }
203
204 function device_is_bridge_attached() {
205 local device=${1}
206 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
207 }
208
209 function device_is_wireless_monitor() {
210 local device="${1}"
211 assert isset device
212
213 device_is_wireless "${device}" && \
214 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
215 }
216
217 function device_is_wireless_adhoc() {
218 local device="${1}"
219 assert isset device
220
221 device_is_wireless "${device}" && \
222 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
223 }
224
225 function device_get_bridge() {
226 local device=${1}
227 assert isset device
228
229 # Check if device is attached to a bridge.
230 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
231
232 local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
233 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
234
235 local ifindex=$(<${ifindex_path})
236 assert isset ifindex
237
238 device_ifindex_to_name ${ifindex}
239 }
240
241 # Check if the device is a vlan device
242 function device_is_vlan() {
243 local device=${1}
244 assert isset device
245
246 [ -e "${PROC_NET_VLAN}/${device}" ]
247 }
248
249 # Check if the device has vlan devices
250 function device_has_vlans() {
251 local device=${1}
252 assert isset device
253
254 if device_is_vlan ${device}; then
255 return ${EXIT_FALSE}
256 fi
257
258 local vlans=$(device_get_vlans ${device})
259 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
260 }
261
262 function device_get_vlans() {
263 local device=${1}
264 assert isset device
265
266 # If no 8021q module has been loaded into the kernel,
267 # we cannot do anything.
268 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
269
270 local dev spacer1 id spacer2 parent
271 while read dev spacer1 id spacer2 parent; do
272 [ "${parent}" = "${device}" ] || continue
273
274 print "${dev}"
275 done < ${PROC_NET_VLAN_CONFIG}
276 }
277
278 # Check if the device is a ppp device
279 function device_is_ppp() {
280 local device=${1}
281
282 local type=$(__device_get_file ${device} type)
283
284 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
285 }
286
287 # Check if the device is a pointopoint device.
288 function device_is_ptp() {
289 local device=${1}
290
291 device_has_flag ${device} 0x10
292 }
293
294 # Check if the device is a loopback device
295 function device_is_loopback() {
296 local device=${1}
297
298 [ "${device}" = "lo" ]
299 }
300
301 # Check if the device is a wireless device
302 function device_is_wireless() {
303 local device=${1}
304
305 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
306 }
307
308 function device_get_phy() {
309 local device="${1}"
310
311 if device_is_wireless "${device}"; then
312 print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
313 return ${EXIT_OK}
314 fi
315
316 return ${EXIT_ERROR}
317 }
318
319 function device_is_serial() {
320 serial_exists $@
321 }
322
323 # Check if the device is a physical network interface
324 function device_is_ethernet() {
325 local device=${1}
326
327 device_is_loopback ${device} && \
328 return ${EXIT_ERROR}
329
330 device_is_bonding ${device} && \
331 return ${EXIT_ERROR}
332
333 device_is_bridge ${device} && \
334 return ${EXIT_ERROR}
335
336 device_is_ppp ${device} && \
337 return ${EXIT_ERROR}
338
339 device_is_vlan ${device} && \
340 return ${EXIT_ERROR}
341
342 [ "$(__device_get_file ${device} type)" != "1" ] && \
343 return ${EXIT_ERROR}
344
345 return ${EXIT_OK}
346 }
347
348 # Get the device type
349 function device_get_type() {
350 local device=${1}
351
352 # If the device does not exist (happens on udev remove events),
353 # we do not bother to run all checks.
354 if ! device_exists "${device}"; then
355 echo "unknown"
356
357 elif device_is_vlan ${device}; then
358 echo "vlan"
359
360 elif device_is_bonding ${device}; then
361 echo "bonding"
362
363 elif device_is_bridge ${device}; then
364 echo "bridge"
365
366 elif device_is_ppp ${device}; then
367 echo "ppp"
368
369 elif device_is_batman_adv ${device}; then
370 echo "batman-adv"
371
372 elif device_is_loopback ${device}; then
373 echo "loopback"
374
375 elif device_is_wireless_adhoc ${device}; then
376 echo "wireless-adhoc"
377
378 elif device_is_wireless ${device}; then
379 echo "wireless"
380
381 elif device_is_ethernet ${device}; then
382 echo "ethernet"
383
384 elif device_is_serial ${device}; then
385 echo "serial"
386
387 else
388 echo "unknown"
389 fi
390 }
391
392 function device_is_ethernet_compatible() {
393 local device="${1}"
394
395 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
396 local type="$(__device_get_file "${device}" "type")"
397 [[ "${type}" = "1" ]]
398 }
399
400 function device_get_status() {
401 local device=${1}
402 assert isset device
403
404 local status=${STATUS_DOWN}
405
406 if device_is_up ${device}; then
407 status=${STATUS_UP}
408
409 if ! device_has_carrier ${device}; then
410 status=${STATUS_NOCARRIER}
411 fi
412 fi
413
414 echo "${status}"
415 }
416
417 function device_get_address() {
418 local device=${1}
419
420 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
421 }
422
423 function device_set_address() {
424 assert [ $# -eq 2 ]
425
426 local device="${1}"
427 local addr="${2}"
428
429 if ! device_exists "${device}"; then
430 error "Device '${device}' does not exist."
431 return ${EXIT_ERROR}
432 fi
433
434 # Do nothing if the address has not changed
435 local old_addr="$(device_get_address "${device}")"
436 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
437 return ${EXIT_OK}
438 fi
439
440 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
441
442 local up
443 if device_is_up "${device}"; then
444 device_set_down "${device}"
445 up=1
446 fi
447
448 ip link set "${device}" address "${addr}"
449 local ret=$?
450
451 if [ "${up}" = "1" ]; then
452 device_set_up "${device}"
453 fi
454
455 if [ "${ret}" != "0" ]; then
456 error_log "Could not set address '${addr}' on device '${device}'"
457 fi
458
459 return ${ret}
460 }
461
462 function device_get() {
463 local device
464 local devices
465
466 for device in ${SYS_CLASS_NET}/*; do
467 device=$(basename ${device})
468
469 # bonding_masters is no device
470 [ "${device}" = "bonding_masters" ] && continue
471
472 devices="${devices} ${device}"
473 done
474
475 echo ${devices}
476 return ${EXIT_OK}
477 }
478
479 function devices_get_all() {
480 device_get
481 }
482
483 # Check if a device has a cable plugged in
484 function device_has_carrier() {
485 local device=${1}
486 assert isset device
487
488 local carrier=$(__device_get_file ${device} carrier)
489 [ "${carrier}" = "1" ]
490 }
491
492 function device_is_promisc() {
493 local device=${1}
494
495 device_has_flag ${device} 0x200
496 }
497
498 function device_set_promisc() {
499 local device=${1}
500 local state=${2}
501
502 assert device_exists ${device}
503 assert isset state
504 assert isoneof state on off
505
506 ip link set ${device} promisc ${state}
507 }
508
509 # Check if the device is free
510 function device_is_free() {
511 ! device_is_used $@
512 }
513
514 # Check if the device is used
515 function device_is_used() {
516 local device=${1}
517
518 device_has_vlans ${device} && \
519 return ${EXIT_OK}
520 device_is_bonded ${device} && \
521 return ${EXIT_OK}
522 device_is_bridge_attached ${device} && \
523 return ${EXIT_OK}
524
525 return ${EXIT_ERROR}
526 }
527
528 function device_hash() {
529 local device=${1}
530
531 # Get mac address of device and remove all colons (:)
532 # that will result in a hash.
533 device=$(macify ${device})
534
535 echo "${device//:/}"
536 }
537
538 # Give the device a new name
539 function device_set_name() {
540 local source=$1
541 local destination=${2}
542
543 # Check if devices exists
544 if ! device_exists ${source} || device_exists ${destination}; then
545 return 4
546 fi
547
548 local up
549 if device_is_up ${source}; then
550 ip link set ${source} down
551 up=1
552 fi
553
554 ip link set ${source} name ${destination}
555
556 if [ "${up}" = "1" ]; then
557 ip link set ${destination} up
558 fi
559 }
560
561 # Set device up
562 function device_set_up() {
563 local device=${1}
564
565 # Silently fail if device was not found
566 [ -z "${device}" ] && return ${EXIT_ERROR}
567
568 # Do nothing if device is already up
569 device_is_up ${device} && return ${EXIT_OK}
570
571 device_set_parent_up ${device}
572
573 log DEBUG "Setting up device '${device}'"
574
575 ip link set ${device} up
576 }
577
578 function device_set_parent_up() {
579 local device=${1}
580 local parent
581
582 if device_is_vlan ${device}; then
583 parent=$(vlan_get_parent ${device})
584
585 device_is_up ${parent} && return ${EXIT_OK}
586
587 log DEBUG "Setting up parent device '${parent}' of '${device}'"
588
589 device_set_up ${parent}
590 return $?
591 fi
592
593 return ${EXIT_OK}
594 }
595
596 # Set device down
597 function device_set_down() {
598 local device=${1}
599 assert isset device
600
601 local ret=${EXIT_OK}
602
603 if device_is_up ${device}; then
604 log DEBUG "Tearing down device '${device}'"
605
606 ip link set ${device} down
607 ret=$?
608 fi
609
610 device_set_parent_down ${device}
611
612 return ${ret}
613 }
614
615 function device_set_parent_down() {
616 local device=${1}
617 local parent
618
619 if device_is_vlan ${device}; then
620 parent=$(vlan_get_parent ${device})
621
622 device_is_up ${parent} || return ${EXIT_OK}
623
624 if device_is_free ${parent}; then
625 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
626
627 device_set_down ${parent}
628 fi
629 fi
630
631 return ${EXIT_OK}
632 }
633
634 function device_get_mtu() {
635 local device=${1}
636
637 if ! device_exists ${device}; then
638 error "Device '${device}' does not exist."
639 return ${EXIT_ERROR}
640 fi
641
642 echo $(<${SYS_CLASS_NET}/${device}/mtu)
643 }
644
645 # Set mtu to a device
646 function device_set_mtu() {
647 local device=${1}
648 local mtu=${2}
649
650 if ! device_exists ${device}; then
651 error "Device '${device}' does not exist."
652 return ${EXIT_ERROR}
653 fi
654
655 local oldmtu=$(device_get_mtu ${device})
656
657 if [ "${oldmtu}" = "${mtu}" ]; then
658 # No need to set mtu.
659 return ${EXIT_OK}
660 fi
661
662 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
663
664 local up
665 if device_is_up ${device}; then
666 device_set_down ${device}
667 up=1
668 fi
669
670 ip link set ${device} mtu ${mtu}
671 local ret=$?
672
673 if [ "${up}" = "1" ]; then
674 device_set_up ${device}
675 fi
676
677 if [ "${ret}" != "0" ]; then
678 error_log "Could not set mtu '${mtu}' on device '${device}'."
679 fi
680
681 return ${ret}
682 }
683
684 function device_adjust_mtu() {
685 assert [ $# -eq 2 ]
686
687 local device="${1}"
688 local other_device="${2}"
689
690 local mtu="$(device_get_mtu "${other_device}")"
691 device_set_mtu "${device}" "${mtu}"
692 }
693
694 function device_discover() {
695 local device=${1}
696
697 log INFO "Running discovery process on device '${device}'."
698
699 local hook
700 for hook in $(hook_zone_get_all); do
701 hook_zone_exec ${hook} discover ${device}
702 done
703 }
704
705 function device_has_ip() {
706 local device=${1}
707 local addr=${2}
708
709 assert isset addr
710 assert device_exists ${device}
711
712 # IPv6 addresses must be fully imploded
713 local protocol=$(ip_detect_protocol ${addr})
714 case "${protocol}" in
715 ipv6)
716 addr=$(ipv6_implode ${addr})
717 ;;
718 esac
719
720 listmatch ${addr} $(device_get_addresses ${device})
721 }
722
723 function device_get_addresses() {
724 local device=${1}
725
726 assert device_exists ${device}
727
728 local prot
729 local addr
730 local line
731 ip addr show ${device} | \
732 while read prot addr line; do
733 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
734 done
735 }
736
737 function __device_get_file() {
738 local device=${1}
739 local file=${2}
740
741 assert isset device
742 assert isset file
743
744 local path="${SYS_CLASS_NET}/${device}/${file}"
745 [ -r "${path}" ] || return ${EXIT_ERROR}
746
747 echo "$(<${path})"
748 }
749
750 function __device_set_file() {
751 assert [ $# -eq 3 ]
752
753 local device="${1}"
754 local file="${2}"
755 local value="${3}"
756
757 local path="${SYS_CLASS_NET}/${device}/${file}"
758 if [ ! -w "${path}" ]; then
759 log DEBUG "Cannot write to file '${file}' (${value})"
760 return ${EXIT_ERROR}
761 fi
762
763 echo "${value}" > "${path}"
764 }
765
766 function device_get_rx_bytes() {
767 local device=${1}
768
769 __device_get_file ${device} statistics/rx_bytes
770 }
771
772 function device_get_tx_bytes() {
773 local device=${1}
774
775 __device_get_file ${device} statistics/tx_bytes
776 }
777
778 function device_get_rx_packets() {
779 local device=${1}
780
781 __device_get_file ${device} statistics/rx_packets
782 }
783
784 function device_get_tx_packets() {
785 local device=${1}
786
787 __device_get_file ${device} statistics/tx_packets
788 }
789
790 function device_get_rx_errors() {
791 local device=${1}
792
793 __device_get_file ${device} statistics/rx_errors
794 }
795
796 function device_get_tx_errors() {
797 local device=${1}
798
799 __device_get_file ${device} statistics/tx_errors
800 }
801
802 function device_get_speed() {
803 local device=${1}
804
805 __device_get_file ${device} speed
806 }
807
808 function device_get_duplex() {
809 local device=${1}
810
811 __device_get_file ${device} duplex
812 }
813
814 function device_get_link_string() {
815 local device="${1}"
816 assert isset device
817
818 local s
819
820 local speed="$(device_get_speed "${device}")"
821 if isset speed; then
822 list_append s "${speed} MBit/s"
823 fi
824
825 local duplex="$(device_get_duplex "${device}")"
826 if isset duplex; then
827 list_append s "${duplex} duplex"
828 fi
829
830 print "${s}"
831 }