]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.device
Split port hooks in to (create|remove|up|down) actions
[people/ms/network.git] / src / functions / functions.device
CommitLineData
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 22function 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
43function 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
62function device_exists() {
63 local device=${1}
64
65 # If device name was not found, exit.
66 [ -n "${device}" ] || return ${EXIT_ERROR}
67
6c74a64c
MT
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}
1848564d
MT
74}
75
a23fdc0e
MT
76function 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
99be6026
MT
89function 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
e369be1a
MT
108function 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
1848564d
MT
121# Check if the device is up
122function device_is_up() {
123 local device=${1}
124
125 device_exists ${device} || return ${EXIT_ERROR}
126
e369be1a 127 device_has_flag ${device} 0x1
1848564d
MT
128}
129
99be6026
MT
130function 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
150function 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
e6993835
MT
162# Check if the device is a batman-adv bridge
163function device_is_batman_adv() {
164 [ -d "${SYS_CLASS_NET}/${1}/mesh" ]
165}
166
b8026986
MT
167# Check if the device is a batman-adv slave port
168function device_is_batman_adv_slave() {
7b192cf4
MT
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}
e6993835
MT
185}
186
1848564d
MT
187# Check if the device is a bonding device
188function device_is_bonding() {
189 [ -d "/sys/class/net/${1}/bonding" ]
190}
191
192# Check if the device bonded in a bonding device
193function device_is_bonded() {
711ffac1 194 local device=${1}
1848564d 195
0959482b 196 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
1848564d
MT
197}
198
199# Check if the device is a bridge
200function device_is_bridge() {
201 [ -d "/sys/class/net/${1}/bridge" ]
202}
203
81ed640c
MT
204function device_is_bridge_attached() {
205 local device=${1}
81ed640c
MT
206 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
207}
208
a23fdc0e
MT
209function 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
b8026986
MT
217function 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
99be6026
MT
225function 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
7951525a
MT
241# Check if the device is a vlan device
242function device_is_vlan() {
1848564d 243 local device=${1}
7951525a 244 assert isset device
1848564d 245
7951525a 246 [ -e "${PROC_NET_VLAN}/${device}" ]
1848564d
MT
247}
248
7951525a
MT
249# Check if the device has vlan devices
250function device_has_vlans() {
fb02e543 251 local device=${1}
7951525a 252 assert isset device
fb02e543 253
7951525a 254 if device_is_vlan ${device}; then
ec63256a 255 return ${EXIT_FALSE}
fb02e543
MT
256 fi
257
7951525a
MT
258 local vlans=$(device_get_vlans ${device})
259 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
ec63256a
MT
260}
261
7951525a 262function device_get_vlans() {
ec63256a 263 local device=${1}
7951525a 264 assert isset device
ec63256a 265
8357a7ff
MT
266 # If no 8021q module has been loaded into the kernel,
267 # we cannot do anything.
7951525a 268 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
8357a7ff 269
ec63256a
MT
270 local dev spacer1 id spacer2 parent
271 while read dev spacer1 id spacer2 parent; do
7951525a
MT
272 [ "${parent}" = "${device}" ] || continue
273
274 print "${dev}"
275 done < ${PROC_NET_VLAN_CONFIG}
1848564d
MT
276}
277
1848564d
MT
278# Check if the device is a ppp device
279function device_is_ppp() {
280 local device=${1}
281
55b802cc 282 local type=$(__device_get_file ${device} type)
28f0b4ab 283
e369be1a
MT
284 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
285}
55b802cc 286
e369be1a
MT
287# Check if the device is a pointopoint device.
288function device_is_ptp() {
289 local device=${1}
290
291 device_has_flag ${device} 0x10
1848564d
MT
292}
293
294# Check if the device is a loopback device
295function device_is_loopback() {
5bb2429a
MT
296 local device=${1}
297
1848564d
MT
298 [ "${device}" = "lo" ]
299}
300
a508c27e
MT
301# Check if the device is a wireless device
302function device_is_wireless() {
303 local device=${1}
304
305 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
306}
307
4733a336
MT
308function 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
6c74a64c
MT
319function device_is_serial() {
320 serial_exists $@
321}
322
1848564d 323# Check if the device is a physical network interface
ec63256a 324function device_is_ethernet() {
1848564d
MT
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
7951525a 339 device_is_vlan ${device} && \
1848564d
MT
340 return ${EXIT_ERROR}
341
419b4cd0
MT
342 [ "$(__device_get_file ${device} type)" != "1" ] && \
343 return ${EXIT_ERROR}
344
1848564d
MT
345 return ${EXIT_OK}
346}
347
348# Get the device type
349function device_get_type() {
5bb2429a 350 local device=${1}
1848564d 351
7951525a 352 if device_is_vlan ${device}; then
1848564d
MT
353 echo "vlan"
354
355 elif device_is_bonding ${device}; then
356 echo "bonding"
357
358 elif device_is_bridge ${device}; then
359 echo "bridge"
360
361 elif device_is_ppp ${device}; then
362 echo "ppp"
363
e6993835
MT
364 elif device_is_batman_adv ${device}; then
365 echo "batman-adv"
366
1848564d
MT
367 elif device_is_loopback ${device}; then
368 echo "loopback"
369
b8026986
MT
370 elif device_is_wireless_adhoc ${device}; then
371 echo "wireless-adhoc"
372
a508c27e
MT
373 elif device_is_wireless ${device}; then
374 echo "wireless"
375
ec63256a
MT
376 elif device_is_ethernet ${device}; then
377 echo "ethernet"
1848564d 378
6c74a64c
MT
379 elif device_is_serial ${device}; then
380 echo "serial"
381
1848564d
MT
382 else
383 echo "unknown"
384 fi
385}
386
711ffac1
MT
387function device_get_status() {
388 local device=${1}
711ffac1
MT
389 assert isset device
390
3cb2fc42 391 local status=${STATUS_DOWN}
711ffac1 392
3cb2fc42 393 if device_is_up ${device}; then
711ffac1 394 status=${STATUS_UP}
711ffac1 395
3cb2fc42
MT
396 if ! device_has_carrier ${device}; then
397 status=${STATUS_NOCARRIER}
398 fi
399 fi
711ffac1
MT
400
401 echo "${status}"
402}
403
1848564d
MT
404function device_get_address() {
405 local device=${1}
406
407 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
408}
409
410function device_set_address() {
08c5b789
MT
411 assert [ $# -eq 2 ]
412
413 local device="${1}"
414 local addr="${2}"
1b7a1578 415
08c5b789 416 if ! device_exists "${device}"; then
1b7a1578
MT
417 error "Device '${device}' does not exist."
418 return ${EXIT_ERROR}
419 fi
420
08c5b789
MT
421 # Do nothing if the address has not changed
422 local old_addr="$(device_get_address "${device}")"
423 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
424 return ${EXIT_OK}
425 fi
426
427 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
1b7a1578
MT
428
429 local up
08c5b789
MT
430 if device_is_up "${device}"; then
431 device_set_down "${device}"
1b7a1578
MT
432 up=1
433 fi
434
08c5b789 435 ip link set "${device}" address "${addr}"
1b7a1578
MT
436 local ret=$?
437
438 if [ "${up}" = "1" ]; then
08c5b789 439 device_set_up "${device}"
1b7a1578
MT
440 fi
441
442 if [ "${ret}" != "0" ]; then
08c5b789 443 error_log "Could not set address '${addr}' on device '${device}'"
1b7a1578
MT
444 fi
445
446 return ${ret}
1848564d
MT
447}
448
711ffac1 449function device_get() {
2ae0fb8d 450 local device
711ffac1
MT
451 local devices
452
2ae0fb8d
MT
453 for device in ${SYS_CLASS_NET}/*; do
454 device=$(basename ${device})
711ffac1 455
2ae0fb8d
MT
456 # bonding_masters is no device
457 [ "${device}" = "bonding_masters" ] && continue
458
459 devices="${devices} ${device}"
460 done
711ffac1
MT
461
462 echo ${devices}
463 return ${EXIT_OK}
464}
465
1848564d 466function devices_get_all() {
711ffac1 467 device_get
1848564d
MT
468}
469
470# Check if a device has a cable plugged in
471function device_has_carrier() {
5bb2429a
MT
472 local device=${1}
473 assert isset device
474
ec63256a
MT
475 local carrier=$(__device_get_file ${device} carrier)
476 [ "${carrier}" = "1" ]
1848564d
MT
477}
478
1e4c26a4
MT
479function device_is_promisc() {
480 local device=${1}
481
e369be1a 482 device_has_flag ${device} 0x200
1e4c26a4
MT
483}
484
cf6e4606
MT
485function device_set_promisc() {
486 local device=${1}
487 local state=${2}
488
489 assert device_exists ${device}
490 assert isset state
491 assert isoneof state on off
492
493 ip link set ${device} promisc ${state}
494}
495
1848564d
MT
496# Check if the device is free
497function device_is_free() {
81ed640c 498 ! device_is_used $@
1848564d
MT
499}
500
501# Check if the device is used
502function device_is_used() {
5bb2429a 503 local device=${1}
1848564d 504
7951525a 505 device_has_vlans ${device} && \
fb02e543 506 return ${EXIT_OK}
1848564d 507 device_is_bonded ${device} && \
fb02e543 508 return ${EXIT_OK}
81ed640c
MT
509 device_is_bridge_attached ${device} && \
510 return ${EXIT_OK}
1848564d 511
fb02e543 512 return ${EXIT_ERROR}
1848564d
MT
513}
514
1b7a1578
MT
515function device_hash() {
516 local device=${1}
517
37e4ec8b
MT
518 # Get mac address of device and remove all colons (:)
519 # that will result in a hash.
520 device=$(macify ${device})
521
522 echo "${device//:/}"
1b7a1578
MT
523}
524
525# Give the device a new name
526function device_set_name() {
1848564d 527 local source=$1
1578dae9 528 local destination=${2}
1848564d
MT
529
530 # Check if devices exists
531 if ! device_exists ${source} || device_exists ${destination}; then
532 return 4
533 fi
534
535 local up
536 if device_is_up ${source}; then
537 ip link set ${source} down
538 up=1
539 fi
540
541 ip link set ${source} name ${destination}
542
543 if [ "${up}" = "1" ]; then
544 ip link set ${destination} up
545 fi
546}
547
1848564d
MT
548# Set device up
549function device_set_up() {
5bb2429a 550 local device=${1}
1848564d 551
711ffac1
MT
552 # Silently fail if device was not found
553 [ -z "${device}" ] && return ${EXIT_ERROR}
554
1848564d
MT
555 # Do nothing if device is already up
556 device_is_up ${device} && return ${EXIT_OK}
557
81ed640c
MT
558 device_set_parent_up ${device}
559
560 log DEBUG "Setting up device '${device}'"
561
1848564d
MT
562 ip link set ${device} up
563}
564
81ed640c
MT
565function device_set_parent_up() {
566 local device=${1}
567 local parent
568
7951525a
MT
569 if device_is_vlan ${device}; then
570 parent=$(vlan_get_parent ${device})
81ed640c
MT
571
572 device_is_up ${parent} && return ${EXIT_OK}
573
574 log DEBUG "Setting up parent device '${parent}' of '${device}'"
575
576 device_set_up ${parent}
577 return $?
578 fi
579
580 return ${EXIT_OK}
581}
582
1848564d
MT
583# Set device down
584function device_set_down() {
5bb2429a
MT
585 local device=${1}
586 assert isset device
1848564d 587
81ed640c
MT
588 local ret=${EXIT_OK}
589
590 if device_is_up ${device}; then
591 log DEBUG "Tearing down device '${device}'"
592
593 ip link set ${device} down
594 ret=$?
595 fi
596
597 device_set_parent_down ${device}
1848564d 598
81ed640c
MT
599 return ${ret}
600}
601
602function device_set_parent_down() {
603 local device=${1}
604 local parent
605
7951525a
MT
606 if device_is_vlan ${device}; then
607 parent=$(vlan_get_parent ${device})
81ed640c
MT
608
609 device_is_up ${parent} || return ${EXIT_OK}
610
611 if device_is_free ${parent}; then
612 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
613
614 device_set_down ${parent}
615 fi
616 fi
617
618 return ${EXIT_OK}
1848564d
MT
619}
620
1848564d
MT
621function device_get_mtu() {
622 local device=${1}
623
624 if ! device_exists ${device}; then
625 error "Device '${device}' does not exist."
626 return ${EXIT_ERROR}
627 fi
628
f3e6fe50 629 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
630}
631
632# Set mtu to a device
633function device_set_mtu() {
1b7a1578 634 local device=${1}
1848564d
MT
635 local mtu=${2}
636
1b7a1578
MT
637 if ! device_exists ${device}; then
638 error "Device '${device}' does not exist."
639 return ${EXIT_ERROR}
640 fi
641
642 local oldmtu=$(device_get_mtu ${device})
643
644 if [ "${oldmtu}" = "${mtu}" ]; then
645 # No need to set mtu.
646 return ${EXIT_OK}
647 fi
648
649 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
650
1848564d 651 local up
1b7a1578
MT
652 if device_is_up ${device}; then
653 device_set_down ${device}
1848564d
MT
654 up=1
655 fi
656
1b7a1578 657 ip link set ${device} mtu ${mtu}
1848564d
MT
658 local ret=$?
659
660 if [ "${up}" = "1" ]; then
1b7a1578
MT
661 device_set_up ${device}
662 fi
663
664 if [ "${ret}" != "0" ]; then
665 error_log "Could not set mtu '${mtu}' on device '${device}'."
1848564d
MT
666 fi
667
668 return ${ret}
669}
670
3ee5ccb1
MT
671function device_adjust_mtu() {
672 assert [ $# -eq 2 ]
673
674 local device="${1}"
675 local other_device="${2}"
676
677 local mtu="$(device_get_mtu "${other_device}")"
678 device_set_mtu "${device}" "${mtu}"
679}
680
1848564d
MT
681function device_discover() {
682 local device=${1}
683
1b7a1578
MT
684 log INFO "Running discovery process on device '${device}'."
685
1848564d 686 local hook
d61a01d4
MT
687 for hook in $(hook_zone_get_all); do
688 hook_zone_exec ${hook} discover ${device}
1848564d
MT
689 done
690}
691
38f61548 692function device_has_ip() {
1848564d
MT
693 local device=${1}
694 local addr=${2}
695
38f61548
MT
696 assert isset addr
697 assert device_exists ${device}
698
699 # IPv6 addresses must be fully imploded
700 local protocol=$(ip_detect_protocol ${addr})
701 case "${protocol}" in
702 ipv6)
703 addr=$(ipv6_implode ${addr})
704 ;;
705 esac
1848564d 706
38f61548 707 listmatch ${addr} $(device_get_addresses ${device})
1848564d 708}
4231f419 709
38f61548 710function device_get_addresses() {
4231f419 711 local device=${1}
4231f419 712
38f61548 713 assert device_exists ${device}
4231f419 714
38f61548
MT
715 local prot
716 local addr
717 local line
718 ip addr show ${device} | \
719 while read prot addr line; do
720 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
721 done
4231f419 722}
711ffac1 723
711ffac1
MT
724function __device_get_file() {
725 local device=${1}
726 local file=${2}
727
728 assert isset device
729 assert isset file
730
e369be1a
MT
731 local path="${SYS_CLASS_NET}/${device}/${file}"
732 [ -r "${path}" ] || return ${EXIT_ERROR}
733
734 echo "$(<${path})"
711ffac1
MT
735}
736
2c083d57
MT
737function __device_set_file() {
738 assert [ $# -eq 3 ]
739
740 local device="${1}"
741 local file="${2}"
742 local value="${3}"
743
744 local path="${SYS_CLASS_NET}/${device}/${file}"
745 if [ ! -w "${path}" ]; then
746 log DEBUG "Cannot write to file '${file}' (${value})"
747 return ${EXIT_ERROR}
748 fi
749
750 echo "${value}" > "${path}"
751}
752
711ffac1
MT
753function device_get_rx_bytes() {
754 local device=${1}
755
756 __device_get_file ${device} statistics/rx_bytes
757}
758
759function device_get_tx_bytes() {
760 local device=${1}
761
762 __device_get_file ${device} statistics/tx_bytes
763}
764
765function device_get_rx_packets() {
766 local device=${1}
767
768 __device_get_file ${device} statistics/rx_packets
769}
770
771function device_get_tx_packets() {
772 local device=${1}
773
774 __device_get_file ${device} statistics/tx_packets
775}
776
777function device_get_rx_errors() {
778 local device=${1}
779
780 __device_get_file ${device} statistics/rx_errors
781}
782
783function device_get_tx_errors() {
784 local device=${1}
785
786 __device_get_file ${device} statistics/tx_errors
787}
ec63256a
MT
788
789function device_get_speed() {
790 local device=${1}
791
792 __device_get_file ${device} speed
793}
794
795function device_get_duplex() {
796 local device=${1}
797
798 __device_get_file ${device} duplex
799}
657540d8
MT
800
801function device_get_link_string() {
802 local device="${1}"
803 assert isset device
804
805 local s
806
807 local speed="$(device_get_speed "${device}")"
808 if isset speed; then
809 list_append s "${speed} MBit/s"
810 fi
811
812 local duplex="$(device_get_duplex "${device}")"
813 if isset duplex; then
814 list_append s "${duplex} duplex"
815 fi
816
817 print "${s}"
818}