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