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