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