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