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