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