]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.device
network: Put bridge functions into extra file.
[people/arne_f/network.git] / 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
68 [ -d "${SYS_CLASS_NET}/${device}" ]
69}
70
71# Check if the device is up
72function device_is_up() {
73 local device=${1}
74
75 device_exists ${device} || return ${EXIT_ERROR}
76
77 ip link show ${device} 2>/dev/null | grep -qE "<.*UP.*>"
78}
79
80# Check if the device is a bonding device
81function device_is_bonding() {
82 [ -d "/sys/class/net/${1}/bonding" ]
83}
84
85# Check if the device bonded in a bonding device
86function device_is_bonded() {
711ffac1 87 local device=${1}
1848564d 88
711ffac1 89 [ -d "${SYS_CLASS_NET}/${device}/master" ]
1848564d
MT
90}
91
92# Check if the device is a bridge
93function device_is_bridge() {
94 [ -d "/sys/class/net/${1}/bridge" ]
95}
96
81ed640c
MT
97function device_is_bridge_attached() {
98 local device=${1}
99
100 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
101}
102
1848564d
MT
103# Check if the device is a virtual device
104function device_is_virtual() {
105 local device=${1}
106
107 [ -e "/proc/net/vlan/${device}" ]
108}
109
110# Check if the device has virtual devices
111function device_has_virtuals() {
fb02e543
MT
112 local device=${1}
113
114 if device_is_virtual ${device}; then
115 return 1
116 fi
117
1848564d
MT
118 if [ ! -e "/proc/net/vlan/config" ]; then
119 return 1
120 fi
121 grep -q "${1}$" /proc/net/vlan/config
122}
123
124function device_is_vlan() { # XXX Compat function
125 log DEBUG "Deprecated function device_is_vlan() was used."
126
127 device_is_virtual $@
128}
129
130# Check if the device is a ppp device
131function device_is_ppp() {
132 local device=${1}
133
134 ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>"
135}
136
137# Check if the device is a loopback device
138function device_is_loopback() {
139 local device=$(devicify ${1})
140 [ "${device}" = "lo" ]
141}
142
143# Check if the device is a physical network interface
144function device_is_real() {
145 local device=${1}
146
147 device_is_loopback ${device} && \
148 return ${EXIT_ERROR}
149
150 device_is_bonding ${device} && \
151 return ${EXIT_ERROR}
152
153 device_is_bridge ${device} && \
154 return ${EXIT_ERROR}
155
156 device_is_ppp ${device} && \
157 return ${EXIT_ERROR}
158
159 device_is_virtual ${device} && \
160 return ${EXIT_ERROR}
161
162 return ${EXIT_OK}
163}
164
165# Get the device type
166function device_get_type() {
167 local device=$(devicify ${1})
168
169 if device_is_vlan ${device}; then
170 echo "vlan"
171
172 elif device_is_bonding ${device}; then
173 echo "bonding"
174
175 elif device_is_bridge ${device}; then
176 echo "bridge"
177
178 elif device_is_ppp ${device}; then
179 echo "ppp"
180
181 elif device_is_loopback ${device}; then
182 echo "loopback"
183
184 elif device_is_real ${device}; then
185 echo "real"
186
187 else
188 echo "unknown"
189 fi
190}
191
711ffac1
MT
192function device_get_status() {
193 local device=${1}
194
195 assert isset device
196
197 local status=${STATUS_UNKNOWN}
198
199 if ! device_has_carrier ${device}; then
200 status=${STATUS_NOCARRIER}
201 elif device_is_up ${device}; then
202 status=${STATUS_UP}
203 elif device_is_down ${device}; then
204 status=${STATUS_DOWN}
205 fi
206
207 assert isset status
208
209 echo "${status}"
210}
211
1848564d
MT
212function device_get_address() {
213 local device=${1}
214
215 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
216}
217
218function device_set_address() {
1b7a1578
MT
219 local device=${1}
220 local addr=${2}
221
222 if ! device_exists ${device}; then
223 error "Device '${device}' does not exist."
224 return ${EXIT_ERROR}
225 fi
226
227 log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
228
229 local up
230 if device_is_up ${device}; then
231 device_set_down ${device}
232 up=1
233 fi
234
235 ip link set ${device} address ${addr}
236 local ret=$?
237
238 if [ "${up}" = "1" ]; then
239 device_set_up ${device}
240 fi
241
242 if [ "${ret}" != "0" ]; then
243 error_log "Could not set address '${addr}' on device '${device}'."
244 fi
245
246 return ${ret}
1848564d
MT
247}
248
711ffac1
MT
249function device_get() {
250 local from_config
251
252 while [ $# -gt 0 ]; do
253 case "${1}" in
254 --from-config)
255 from_config=1
256 ;;
257 --no-config)
258 from_config=0
259 ;;
260 esac
261 shift
262 done
263
264 local devices
265
266 if [ "${from_config}" != "0" ]; then
267 devices="${devices} $(device_config_list)"
268 fi
269
270 if [ "${from_config}" != "1" ]; then
271 local device
272 for device in ${SYS_CLASS_NET}/*; do
273 devices="${devices} $(basename ${device})"
274 done
275 fi
276
277 echo ${devices}
278 return ${EXIT_OK}
279}
280
1848564d 281function devices_get_all() {
711ffac1 282 device_get
1848564d
MT
283}
284
285# Check if a device has a cable plugged in
286function device_has_carrier() {
287 local device=$(devicify ${1})
288 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
289}
290
1e4c26a4
MT
291function device_is_promisc() {
292 local device=${1}
293
294 ip link show ${device} | grep -qE "<.*PROMISC.*>"
295}
296
1848564d
MT
297# Check if the device is free
298function device_is_free() {
81ed640c 299 ! device_is_used $@
1848564d
MT
300}
301
302# Check if the device is used
303function device_is_used() {
304 local device=$(devicify ${1})
305
fb02e543
MT
306 device_has_virtuals ${device} && \
307 return ${EXIT_OK}
1848564d 308 device_is_bonded ${device} && \
fb02e543 309 return ${EXIT_OK}
81ed640c
MT
310 device_is_bridge_attached ${device} && \
311 return ${EXIT_OK}
1848564d 312
fb02e543 313 return ${EXIT_ERROR}
1848564d
MT
314}
315
316# XXX to be removed I think
317function device_get_free() {
318 local destination=${1}
319
320 # Replace + by a valid number
321 if grep -q "+$" <<<${destination}; then
322 local number=0
323 destination=$(sed -e "s/+//" <<<$destination)
324 while [ "${number}" -le "100" ]; do
325 if ! device_exists "${destination}${number}"; then
326 destination="${destination}${number}"
327 break
328 fi
329 number=$(($number + 1))
330 done
331 fi
332 echo "${destination}"
333}
334
1848564d 335function device_rename() {
1b7a1578
MT
336 warning_log "Called deprecated function 'device_rename'"
337
338 device_set_name $@
339}
340
341function device_hash() {
342 local device=${1}
343
37e4ec8b
MT
344 # Get mac address of device and remove all colons (:)
345 # that will result in a hash.
346 device=$(macify ${device})
347
348 echo "${device//:/}"
1b7a1578
MT
349}
350
351# Give the device a new name
352function device_set_name() {
1848564d
MT
353 local source=$1
354 local destination=$(device_get_free ${2})
355
356 # Check if devices exists
357 if ! device_exists ${source} || device_exists ${destination}; then
358 return 4
359 fi
360
361 local up
362 if device_is_up ${source}; then
363 ip link set ${source} down
364 up=1
365 fi
366
367 ip link set ${source} name ${destination}
368
369 if [ "${up}" = "1" ]; then
370 ip link set ${destination} up
371 fi
372}
373
1848564d
MT
374# Set device up
375function device_set_up() {
376 local device=$(devicify ${1})
377
711ffac1
MT
378 # Silently fail if device was not found
379 [ -z "${device}" ] && return ${EXIT_ERROR}
380
1848564d
MT
381 # Do nothing if device is already up
382 device_is_up ${device} && return ${EXIT_OK}
383
81ed640c
MT
384 device_set_parent_up ${device}
385
386 log DEBUG "Setting up device '${device}'"
387
1848564d
MT
388 ip link set ${device} up
389}
390
81ed640c
MT
391function device_set_parent_up() {
392 local device=${1}
393 local parent
394
395 if device_is_virtual ${device}; then
396 parent=$(device_virtual_get_parent ${device})
397
398 device_is_up ${parent} && return ${EXIT_OK}
399
400 log DEBUG "Setting up parent device '${parent}' of '${device}'"
401
402 device_set_up ${parent}
403 return $?
404 fi
405
406 return ${EXIT_OK}
407}
408
1848564d
MT
409# Set device down
410function device_set_down() {
411 local device=$(devicify ${1})
412
81ed640c
MT
413 local ret=${EXIT_OK}
414
415 if device_is_up ${device}; then
416 log DEBUG "Tearing down device '${device}'"
417
418 ip link set ${device} down
419 ret=$?
420 fi
421
422 device_set_parent_down ${device}
1848564d 423
81ed640c
MT
424 return ${ret}
425}
426
427function device_set_parent_down() {
428 local device=${1}
429 local parent
430
431 if device_is_virtual ${device}; then
432 parent=$(device_virtual_get_parent ${device})
433
434 device_is_up ${parent} || return ${EXIT_OK}
435
436 if device_is_free ${parent}; then
437 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
438
439 device_set_down ${parent}
440 fi
441 fi
442
443 return ${EXIT_OK}
1848564d
MT
444}
445
1848564d
MT
446function device_get_mtu() {
447 local device=${1}
448
449 if ! device_exists ${device}; then
450 error "Device '${device}' does not exist."
451 return ${EXIT_ERROR}
452 fi
453
f3e6fe50 454 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
455}
456
457# Set mtu to a device
458function device_set_mtu() {
1b7a1578 459 local device=${1}
1848564d
MT
460 local mtu=${2}
461
1b7a1578
MT
462 if ! device_exists ${device}; then
463 error "Device '${device}' does not exist."
464 return ${EXIT_ERROR}
465 fi
466
467 local oldmtu=$(device_get_mtu ${device})
468
469 if [ "${oldmtu}" = "${mtu}" ]; then
470 # No need to set mtu.
471 return ${EXIT_OK}
472 fi
473
474 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
475
1848564d 476 local up
1b7a1578
MT
477 if device_is_up ${device}; then
478 device_set_down ${device}
1848564d
MT
479 up=1
480 fi
481
1b7a1578 482 ip link set ${device} mtu ${mtu}
1848564d
MT
483 local ret=$?
484
485 if [ "${up}" = "1" ]; then
1b7a1578
MT
486 device_set_up ${device}
487 fi
488
489 if [ "${ret}" != "0" ]; then
490 error_log "Could not set mtu '${mtu}' on device '${device}'."
1848564d
MT
491 fi
492
493 return ${ret}
494}
495
496function device_discover() {
497 local device=${1}
498
1b7a1578
MT
499 log INFO "Running discovery process on device '${device}'."
500
1848564d 501 local hook
d61a01d4
MT
502 for hook in $(hook_zone_get_all); do
503 hook_zone_exec ${hook} discover ${device}
1848564d
MT
504 done
505}
506
507function device_create_virtual() {
508 log WARN "Called deprecated function device_create_virtual"
509 device_virtual_create $@
510}
511
512function device_virtual_create() {
513 local port=$(devicify ${1})
514 local vid=${2}
515 local mac=${3}
516 local newport=${port}v${vid}
711ffac1 517
1848564d
MT
518 if [ -z "${mac}" ]; then
519 mac=$(mac_generate)
520 fi
521
1b7a1578
MT
522 log INFO "Creating virtual device '${newport}' with address '${mac}'."
523
fb02e543 524 local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
1848564d 525
fb02e543
MT
526 if device_exists ${oldport}; then
527 local differences
528
529 if [ "${oldport}" != "${newport}" ]; then
530 differences="${differences} name"
531 fi
532 if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
533 differences="${differences} address"
534 fi
535
536 echo "differences: $differences"
537
538 if [ -n "${differences}" ]; then
539 if device_is_used ${oldport}; then
540 error_log "There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on."
541 return ${EXIT_ERROR}
542 else
543 log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves."
544 fi
545 else
546 log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on."
547
548 device_set_up ${oldport}
549 return ${EXIT_OK}
550 fi
551
552 else
553 log DEBUG "Virtual device '${newport}' does not exist, yet."
554
555 vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
556 vconfig add ${port} ${vid} >/dev/null
557
558 if [ $? -ne ${EXIT_OK} ]; then
559 error_log "Could not create virtual device '${newport}'."
560 return ${EXIT_ERROR}
561 fi
562
563 oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
564
565 fi
566
567 assert device_exists ${oldport}
568
569 if ! device_exists ${oldport}; then
570 error "Could not determine the created virtual device '${newport}'."
1b7a1578
MT
571 return ${EXIT_ERROR}
572 fi
1848564d
MT
573
574 # The device is expected to be named like ${port}.${vid}
575 # and will be renamed to the virtual schema
fb02e543 576 device_set_name ${oldport} ${newport}
1848564d 577
1b7a1578
MT
578 if [ $? -ne ${EXIT_OK} ]; then
579 error_log "Could not set name of virtual device '${newport}'."
580 return ${EXIT_ERROR}
581 fi
582
fb02e543
MT
583 assert device_exists ${newport}
584
1848564d
MT
585 # Setting new mac address
586 device_set_address ${newport} ${mac}
1b7a1578
MT
587
588 if [ $? -ne ${EXIT_OK} ]; then
589 error_log "Could not set address '${mac}' to virtual device '${newport}'."
590 return ${EXIT_ERROR}
591 fi
1848564d
MT
592
593 # Bring up the new device
594 device_set_up ${newport}
595
1848564d
MT
596 return ${EXIT_OK}
597}
598
599function device_virtual_remove() {
600 local device=$(devicify ${1})
601
81ed640c 602 log INFO "Removing virtual device '${device}' with address '$(macify ${device})'."
1b7a1578 603
1848564d
MT
604 device_set_down ${device}
605
606 vconfig rem ${device} >/dev/null
1848564d 607
1b7a1578
MT
608 if [ $? -ne ${EXIT_OK} ]; then
609 error_log "Could not remote virtual device '${newport}'."
610 return ${EXIT_ERROR}
611 fi
612
1848564d
MT
613 return ${EXIT_OK}
614}
615
81ed640c
MT
616function device_virtual_get_parent() {
617 local device=${1}
618
619 local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }')
620
621 if device_exists ${parent}; then
622 echo "${parent}"
623 return ${EXIT_OK}
624 fi
625
626 return ${EXIT_ERROR}
627}
628
fb02e543
MT
629function device_virtual_get_by_parent_and_vid() {
630 local parent=${1}
631 local vid=${2}
632
711ffac1
MT
633 assert isset parent
634 assert isset vid
635
fb02e543
MT
636 local v_port
637 local v_id
638 local v_parent
639
943e3f7e
MT
640 assert [ -e "/proc/net/vlan/config" ]
641
fb02e543
MT
642 fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
643 while read v_port v_id v_parent; do
644 if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
645 echo "${v_port}"
646 return ${EXIT_OK}
647 fi
648 done
649
650 return ${EXIT_ERROR}
651}
652
1848564d
MT
653function device_has_ipv4() {
654 local device=${1}
655 local addr=${2}
656
657 if ! device_exists ${device}; then
658 error "Device '${device}' does not exist."
659 return ${EXIT_ERROR}
660 fi
661
662 ip addr show ${device} | grep -q -e "inet " -e "${addr}"
663}
4231f419
MT
664
665function device_has_ipv6() {
666 local device=${1}
667 local addr=${2}
668
669 if ! device_exists ${device}; then
670 error "Device '${device}' does not exist."
671 return ${EXIT_ERROR}
672 fi
673
674 local prefix=${addr##*/}
675 addr=$(ipv6_implode ${addr%%/*})
676
677 if [ -n "${prefix}" ]; then
678 addr="${addr}/${prefix}"
679 fi
680
681 ip addr show ${device} | grep -q "inet6 ${addr}"
682}
711ffac1 683
711ffac1
MT
684function __device_get_file() {
685 local device=${1}
686 local file=${2}
687
688 assert isset device
689 assert isset file
690
691 cat ${SYS_CLASS_NET}/${device}/${file}
692}
693
694function device_get_rx_bytes() {
695 local device=${1}
696
697 __device_get_file ${device} statistics/rx_bytes
698}
699
700function device_get_tx_bytes() {
701 local device=${1}
702
703 __device_get_file ${device} statistics/tx_bytes
704}
705
706function device_get_rx_packets() {
707 local device=${1}
708
709 __device_get_file ${device} statistics/rx_packets
710}
711
712function device_get_tx_packets() {
713 local device=${1}
714
715 __device_get_file ${device} statistics/tx_packets
716}
717
718function device_get_rx_errors() {
719 local device=${1}
720
721 __device_get_file ${device} statistics/rx_errors
722}
723
724function device_get_tx_errors() {
725 local device=${1}
726
727 __device_get_file ${device} statistics/tx_errors
728}