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