]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.device
device: Rewrite device_set_address
[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 assert [ $# -eq 2 ]
383
384 local device="${1}"
385 local addr="${2}"
386
387 if ! device_exists "${device}"; then
388 error "Device '${device}' does not exist."
389 return ${EXIT_ERROR}
390 fi
391
392 # Do nothing if the address has not changed
393 local old_addr="$(device_get_address "${device}")"
394 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
395 return ${EXIT_OK}
396 fi
397
398 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
399
400 local up
401 if device_is_up "${device}"; then
402 device_set_down "${device}"
403 up=1
404 fi
405
406 ip link set "${device}" address "${addr}"
407 local ret=$?
408
409 if [ "${up}" = "1" ]; then
410 device_set_up "${device}"
411 fi
412
413 if [ "${ret}" != "0" ]; then
414 error_log "Could not set address '${addr}' on device '${device}'"
415 fi
416
417 return ${ret}
418 }
419
420 function device_get() {
421 local device
422 local devices
423
424 for device in ${SYS_CLASS_NET}/*; do
425 device=$(basename ${device})
426
427 # bonding_masters is no device
428 [ "${device}" = "bonding_masters" ] && continue
429
430 devices="${devices} ${device}"
431 done
432
433 echo ${devices}
434 return ${EXIT_OK}
435 }
436
437 function devices_get_all() {
438 device_get
439 }
440
441 # Check if a device has a cable plugged in
442 function device_has_carrier() {
443 local device=${1}
444 assert isset device
445
446 local carrier=$(__device_get_file ${device} carrier)
447 [ "${carrier}" = "1" ]
448 }
449
450 function device_is_promisc() {
451 local device=${1}
452
453 device_has_flag ${device} 0x200
454 }
455
456 function device_set_promisc() {
457 local device=${1}
458 local state=${2}
459
460 assert device_exists ${device}
461 assert isset state
462 assert isoneof state on off
463
464 ip link set ${device} promisc ${state}
465 }
466
467 # Check if the device is free
468 function device_is_free() {
469 ! device_is_used $@
470 }
471
472 # Check if the device is used
473 function device_is_used() {
474 local device=${1}
475
476 device_has_vlans ${device} && \
477 return ${EXIT_OK}
478 device_is_bonded ${device} && \
479 return ${EXIT_OK}
480 device_is_bridge_attached ${device} && \
481 return ${EXIT_OK}
482
483 return ${EXIT_ERROR}
484 }
485
486 function device_hash() {
487 local device=${1}
488
489 # Get mac address of device and remove all colons (:)
490 # that will result in a hash.
491 device=$(macify ${device})
492
493 echo "${device//:/}"
494 }
495
496 # Give the device a new name
497 function device_set_name() {
498 local source=$1
499 local destination=${2}
500
501 # Check if devices exists
502 if ! device_exists ${source} || device_exists ${destination}; then
503 return 4
504 fi
505
506 local up
507 if device_is_up ${source}; then
508 ip link set ${source} down
509 up=1
510 fi
511
512 ip link set ${source} name ${destination}
513
514 if [ "${up}" = "1" ]; then
515 ip link set ${destination} up
516 fi
517 }
518
519 # Set device up
520 function device_set_up() {
521 local device=${1}
522
523 # Silently fail if device was not found
524 [ -z "${device}" ] && return ${EXIT_ERROR}
525
526 # Do nothing if device is already up
527 device_is_up ${device} && return ${EXIT_OK}
528
529 device_set_parent_up ${device}
530
531 log DEBUG "Setting up device '${device}'"
532
533 ip link set ${device} up
534 }
535
536 function device_set_parent_up() {
537 local device=${1}
538 local parent
539
540 if device_is_vlan ${device}; then
541 parent=$(vlan_get_parent ${device})
542
543 device_is_up ${parent} && return ${EXIT_OK}
544
545 log DEBUG "Setting up parent device '${parent}' of '${device}'"
546
547 device_set_up ${parent}
548 return $?
549 fi
550
551 return ${EXIT_OK}
552 }
553
554 # Set device down
555 function device_set_down() {
556 local device=${1}
557 assert isset device
558
559 local ret=${EXIT_OK}
560
561 if device_is_up ${device}; then
562 log DEBUG "Tearing down device '${device}'"
563
564 ip link set ${device} down
565 ret=$?
566 fi
567
568 device_set_parent_down ${device}
569
570 return ${ret}
571 }
572
573 function device_set_parent_down() {
574 local device=${1}
575 local parent
576
577 if device_is_vlan ${device}; then
578 parent=$(vlan_get_parent ${device})
579
580 device_is_up ${parent} || return ${EXIT_OK}
581
582 if device_is_free ${parent}; then
583 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
584
585 device_set_down ${parent}
586 fi
587 fi
588
589 return ${EXIT_OK}
590 }
591
592 function device_get_mtu() {
593 local device=${1}
594
595 if ! device_exists ${device}; then
596 error "Device '${device}' does not exist."
597 return ${EXIT_ERROR}
598 fi
599
600 echo $(<${SYS_CLASS_NET}/${device}/mtu)
601 }
602
603 # Set mtu to a device
604 function device_set_mtu() {
605 local device=${1}
606 local mtu=${2}
607
608 if ! device_exists ${device}; then
609 error "Device '${device}' does not exist."
610 return ${EXIT_ERROR}
611 fi
612
613 local oldmtu=$(device_get_mtu ${device})
614
615 if [ "${oldmtu}" = "${mtu}" ]; then
616 # No need to set mtu.
617 return ${EXIT_OK}
618 fi
619
620 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
621
622 local up
623 if device_is_up ${device}; then
624 device_set_down ${device}
625 up=1
626 fi
627
628 ip link set ${device} mtu ${mtu}
629 local ret=$?
630
631 if [ "${up}" = "1" ]; then
632 device_set_up ${device}
633 fi
634
635 if [ "${ret}" != "0" ]; then
636 error_log "Could not set mtu '${mtu}' on device '${device}'."
637 fi
638
639 return ${ret}
640 }
641
642 function device_adjust_mtu() {
643 assert [ $# -eq 2 ]
644
645 local device="${1}"
646 local other_device="${2}"
647
648 local mtu="$(device_get_mtu "${other_device}")"
649 device_set_mtu "${device}" "${mtu}"
650 }
651
652 function device_discover() {
653 local device=${1}
654
655 log INFO "Running discovery process on device '${device}'."
656
657 local hook
658 for hook in $(hook_zone_get_all); do
659 hook_zone_exec ${hook} discover ${device}
660 done
661 }
662
663 function device_has_ip() {
664 local device=${1}
665 local addr=${2}
666
667 assert isset addr
668 assert device_exists ${device}
669
670 # IPv6 addresses must be fully imploded
671 local protocol=$(ip_detect_protocol ${addr})
672 case "${protocol}" in
673 ipv6)
674 addr=$(ipv6_implode ${addr})
675 ;;
676 esac
677
678 listmatch ${addr} $(device_get_addresses ${device})
679 }
680
681 function device_get_addresses() {
682 local device=${1}
683
684 assert device_exists ${device}
685
686 local prot
687 local addr
688 local line
689 ip addr show ${device} | \
690 while read prot addr line; do
691 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
692 done
693 }
694
695 function __device_get_file() {
696 local device=${1}
697 local file=${2}
698
699 assert isset device
700 assert isset file
701
702 local path="${SYS_CLASS_NET}/${device}/${file}"
703 [ -r "${path}" ] || return ${EXIT_ERROR}
704
705 echo "$(<${path})"
706 }
707
708 function __device_set_file() {
709 assert [ $# -eq 3 ]
710
711 local device="${1}"
712 local file="${2}"
713 local value="${3}"
714
715 local path="${SYS_CLASS_NET}/${device}/${file}"
716 if [ ! -w "${path}" ]; then
717 log DEBUG "Cannot write to file '${file}' (${value})"
718 return ${EXIT_ERROR}
719 fi
720
721 echo "${value}" > "${path}"
722 }
723
724 function device_get_rx_bytes() {
725 local device=${1}
726
727 __device_get_file ${device} statistics/rx_bytes
728 }
729
730 function device_get_tx_bytes() {
731 local device=${1}
732
733 __device_get_file ${device} statistics/tx_bytes
734 }
735
736 function device_get_rx_packets() {
737 local device=${1}
738
739 __device_get_file ${device} statistics/rx_packets
740 }
741
742 function device_get_tx_packets() {
743 local device=${1}
744
745 __device_get_file ${device} statistics/tx_packets
746 }
747
748 function device_get_rx_errors() {
749 local device=${1}
750
751 __device_get_file ${device} statistics/rx_errors
752 }
753
754 function device_get_tx_errors() {
755 local device=${1}
756
757 __device_get_file ${device} statistics/tx_errors
758 }
759
760 function device_get_speed() {
761 local device=${1}
762
763 __device_get_file ${device} speed
764 }
765
766 function device_get_duplex() {
767 local device=${1}
768
769 __device_get_file ${device} duplex
770 }