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