]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.device
network: Automatically create all ethernet ports.
[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 249function device_get() {
2ae0fb8d 250 local device
711ffac1
MT
251 local devices
252
2ae0fb8d
MT
253 for device in ${SYS_CLASS_NET}/*; do
254 device=$(basename ${device})
711ffac1 255
2ae0fb8d
MT
256 # bonding_masters is no device
257 [ "${device}" = "bonding_masters" ] && continue
258
259 devices="${devices} ${device}"
260 done
711ffac1
MT
261
262 echo ${devices}
263 return ${EXIT_OK}
264}
265
1848564d 266function devices_get_all() {
711ffac1 267 device_get
1848564d
MT
268}
269
270# Check if a device has a cable plugged in
271function device_has_carrier() {
272 local device=$(devicify ${1})
273 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
274}
275
1e4c26a4
MT
276function device_is_promisc() {
277 local device=${1}
278
279 ip link show ${device} | grep -qE "<.*PROMISC.*>"
280}
281
1848564d
MT
282# Check if the device is free
283function device_is_free() {
81ed640c 284 ! device_is_used $@
1848564d
MT
285}
286
287# Check if the device is used
288function device_is_used() {
289 local device=$(devicify ${1})
290
fb02e543
MT
291 device_has_virtuals ${device} && \
292 return ${EXIT_OK}
1848564d 293 device_is_bonded ${device} && \
fb02e543 294 return ${EXIT_OK}
81ed640c
MT
295 device_is_bridge_attached ${device} && \
296 return ${EXIT_OK}
1848564d 297
fb02e543 298 return ${EXIT_ERROR}
1848564d
MT
299}
300
301# XXX to be removed I think
302function device_get_free() {
303 local destination=${1}
304
305 # Replace + by a valid number
306 if grep -q "+$" <<<${destination}; then
307 local number=0
308 destination=$(sed -e "s/+//" <<<$destination)
309 while [ "${number}" -le "100" ]; do
310 if ! device_exists "${destination}${number}"; then
311 destination="${destination}${number}"
312 break
313 fi
314 number=$(($number + 1))
315 done
316 fi
317 echo "${destination}"
318}
319
1848564d 320function device_rename() {
1b7a1578
MT
321 warning_log "Called deprecated function 'device_rename'"
322
323 device_set_name $@
324}
325
326function device_hash() {
327 local device=${1}
328
37e4ec8b
MT
329 # Get mac address of device and remove all colons (:)
330 # that will result in a hash.
331 device=$(macify ${device})
332
333 echo "${device//:/}"
1b7a1578
MT
334}
335
336# Give the device a new name
337function device_set_name() {
1848564d
MT
338 local source=$1
339 local destination=$(device_get_free ${2})
340
341 # Check if devices exists
342 if ! device_exists ${source} || device_exists ${destination}; then
343 return 4
344 fi
345
346 local up
347 if device_is_up ${source}; then
348 ip link set ${source} down
349 up=1
350 fi
351
352 ip link set ${source} name ${destination}
353
354 if [ "${up}" = "1" ]; then
355 ip link set ${destination} up
356 fi
357}
358
1848564d
MT
359# Set device up
360function device_set_up() {
361 local device=$(devicify ${1})
362
711ffac1
MT
363 # Silently fail if device was not found
364 [ -z "${device}" ] && return ${EXIT_ERROR}
365
1848564d
MT
366 # Do nothing if device is already up
367 device_is_up ${device} && return ${EXIT_OK}
368
81ed640c
MT
369 device_set_parent_up ${device}
370
371 log DEBUG "Setting up device '${device}'"
372
1848564d
MT
373 ip link set ${device} up
374}
375
81ed640c
MT
376function device_set_parent_up() {
377 local device=${1}
378 local parent
379
380 if device_is_virtual ${device}; then
381 parent=$(device_virtual_get_parent ${device})
382
383 device_is_up ${parent} && return ${EXIT_OK}
384
385 log DEBUG "Setting up parent device '${parent}' of '${device}'"
386
387 device_set_up ${parent}
388 return $?
389 fi
390
391 return ${EXIT_OK}
392}
393
1848564d
MT
394# Set device down
395function device_set_down() {
396 local device=$(devicify ${1})
397
81ed640c
MT
398 local ret=${EXIT_OK}
399
400 if device_is_up ${device}; then
401 log DEBUG "Tearing down device '${device}'"
402
403 ip link set ${device} down
404 ret=$?
405 fi
406
407 device_set_parent_down ${device}
1848564d 408
81ed640c
MT
409 return ${ret}
410}
411
412function device_set_parent_down() {
413 local device=${1}
414 local parent
415
416 if device_is_virtual ${device}; then
417 parent=$(device_virtual_get_parent ${device})
418
419 device_is_up ${parent} || return ${EXIT_OK}
420
421 if device_is_free ${parent}; then
422 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
423
424 device_set_down ${parent}
425 fi
426 fi
427
428 return ${EXIT_OK}
1848564d
MT
429}
430
1848564d
MT
431function device_get_mtu() {
432 local device=${1}
433
434 if ! device_exists ${device}; then
435 error "Device '${device}' does not exist."
436 return ${EXIT_ERROR}
437 fi
438
f3e6fe50 439 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
440}
441
442# Set mtu to a device
443function device_set_mtu() {
1b7a1578 444 local device=${1}
1848564d
MT
445 local mtu=${2}
446
1b7a1578
MT
447 if ! device_exists ${device}; then
448 error "Device '${device}' does not exist."
449 return ${EXIT_ERROR}
450 fi
451
452 local oldmtu=$(device_get_mtu ${device})
453
454 if [ "${oldmtu}" = "${mtu}" ]; then
455 # No need to set mtu.
456 return ${EXIT_OK}
457 fi
458
459 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
460
1848564d 461 local up
1b7a1578
MT
462 if device_is_up ${device}; then
463 device_set_down ${device}
1848564d
MT
464 up=1
465 fi
466
1b7a1578 467 ip link set ${device} mtu ${mtu}
1848564d
MT
468 local ret=$?
469
470 if [ "${up}" = "1" ]; then
1b7a1578
MT
471 device_set_up ${device}
472 fi
473
474 if [ "${ret}" != "0" ]; then
475 error_log "Could not set mtu '${mtu}' on device '${device}'."
1848564d
MT
476 fi
477
478 return ${ret}
479}
480
481function device_discover() {
482 local device=${1}
483
1b7a1578
MT
484 log INFO "Running discovery process on device '${device}'."
485
1848564d 486 local hook
d61a01d4
MT
487 for hook in $(hook_zone_get_all); do
488 hook_zone_exec ${hook} discover ${device}
1848564d
MT
489 done
490}
491
1848564d
MT
492function device_has_ipv4() {
493 local device=${1}
494 local addr=${2}
495
496 if ! device_exists ${device}; then
497 error "Device '${device}' does not exist."
498 return ${EXIT_ERROR}
499 fi
500
501 ip addr show ${device} | grep -q -e "inet " -e "${addr}"
502}
4231f419
MT
503
504function device_has_ipv6() {
505 local device=${1}
506 local addr=${2}
507
508 if ! device_exists ${device}; then
509 error "Device '${device}' does not exist."
510 return ${EXIT_ERROR}
511 fi
512
513 local prefix=${addr##*/}
514 addr=$(ipv6_implode ${addr%%/*})
515
516 if [ -n "${prefix}" ]; then
517 addr="${addr}/${prefix}"
518 fi
519
520 ip addr show ${device} | grep -q "inet6 ${addr}"
521}
711ffac1 522
711ffac1
MT
523function __device_get_file() {
524 local device=${1}
525 local file=${2}
526
527 assert isset device
528 assert isset file
529
530 cat ${SYS_CLASS_NET}/${device}/${file}
531}
532
533function device_get_rx_bytes() {
534 local device=${1}
535
536 __device_get_file ${device} statistics/rx_bytes
537}
538
539function device_get_tx_bytes() {
540 local device=${1}
541
542 __device_get_file ${device} statistics/tx_bytes
543}
544
545function device_get_rx_packets() {
546 local device=${1}
547
548 __device_get_file ${device} statistics/rx_packets
549}
550
551function device_get_tx_packets() {
552 local device=${1}
553
554 __device_get_file ${device} statistics/tx_packets
555}
556
557function device_get_rx_errors() {
558 local device=${1}
559
560 __device_get_file ${device} statistics/rx_errors
561}
562
563function device_get_tx_errors() {
564 local device=${1}
565
566 __device_get_file ${device} statistics/tx_errors
567}