]> git.ipfire.org Git - network.git/blame - functions.device
Add "network zone list-hooks" command.
[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
1848564d
MT
124# Check if the device is a ppp device
125function device_is_ppp() {
126 local device=${1}
127
55b802cc 128 local type=$(__device_get_file ${device} type)
28f0b4ab
MT
129
130 case "${type}" in
131 512|65534)
132 return ${EXIT_OK}
133 ;;
134 esac
55b802cc
MT
135
136 return ${EXIT_ERROR}
1848564d
MT
137}
138
139# Check if the device is a loopback device
140function device_is_loopback() {
5bb2429a
MT
141 local device=${1}
142
1848564d
MT
143 [ "${device}" = "lo" ]
144}
145
a508c27e
MT
146# Check if the device is a wireless device
147function device_is_wireless() {
148 local device=${1}
149
150 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
151}
152
1848564d
MT
153# Check if the device is a physical network interface
154function device_is_real() {
155 local device=${1}
156
157 device_is_loopback ${device} && \
158 return ${EXIT_ERROR}
159
160 device_is_bonding ${device} && \
161 return ${EXIT_ERROR}
162
163 device_is_bridge ${device} && \
164 return ${EXIT_ERROR}
165
166 device_is_ppp ${device} && \
167 return ${EXIT_ERROR}
168
169 device_is_virtual ${device} && \
170 return ${EXIT_ERROR}
171
419b4cd0
MT
172 [ "$(__device_get_file ${device} type)" != "1" ] && \
173 return ${EXIT_ERROR}
174
1848564d
MT
175 return ${EXIT_OK}
176}
177
178# Get the device type
179function device_get_type() {
5bb2429a 180 local device=${1}
1848564d 181
8c6a8966 182 if device_is_virtual ${device}; then
1848564d
MT
183 echo "vlan"
184
185 elif device_is_bonding ${device}; then
186 echo "bonding"
187
188 elif device_is_bridge ${device}; then
189 echo "bridge"
190
191 elif device_is_ppp ${device}; then
192 echo "ppp"
193
194 elif device_is_loopback ${device}; then
195 echo "loopback"
196
a508c27e
MT
197 elif device_is_wireless ${device}; then
198 echo "wireless"
199
1848564d
MT
200 elif device_is_real ${device}; then
201 echo "real"
202
203 else
204 echo "unknown"
205 fi
206}
207
711ffac1
MT
208function device_get_status() {
209 local device=${1}
210
211 assert isset device
212
213 local status=${STATUS_UNKNOWN}
214
215 if ! device_has_carrier ${device}; then
216 status=${STATUS_NOCARRIER}
217 elif device_is_up ${device}; then
218 status=${STATUS_UP}
219 elif device_is_down ${device}; then
220 status=${STATUS_DOWN}
221 fi
222
223 assert isset status
224
225 echo "${status}"
226}
227
1848564d
MT
228function device_get_address() {
229 local device=${1}
230
231 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
232}
233
234function device_set_address() {
1b7a1578
MT
235 local device=${1}
236 local addr=${2}
237
238 if ! device_exists ${device}; then
239 error "Device '${device}' does not exist."
240 return ${EXIT_ERROR}
241 fi
242
243 log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
244
245 local up
246 if device_is_up ${device}; then
247 device_set_down ${device}
248 up=1
249 fi
250
251 ip link set ${device} address ${addr}
252 local ret=$?
253
254 if [ "${up}" = "1" ]; then
255 device_set_up ${device}
256 fi
257
258 if [ "${ret}" != "0" ]; then
259 error_log "Could not set address '${addr}' on device '${device}'."
260 fi
261
262 return ${ret}
1848564d
MT
263}
264
711ffac1 265function device_get() {
2ae0fb8d 266 local device
711ffac1
MT
267 local devices
268
2ae0fb8d
MT
269 for device in ${SYS_CLASS_NET}/*; do
270 device=$(basename ${device})
711ffac1 271
2ae0fb8d
MT
272 # bonding_masters is no device
273 [ "${device}" = "bonding_masters" ] && continue
274
275 devices="${devices} ${device}"
276 done
711ffac1
MT
277
278 echo ${devices}
279 return ${EXIT_OK}
280}
281
1848564d 282function devices_get_all() {
711ffac1 283 device_get
1848564d
MT
284}
285
286# Check if a device has a cable plugged in
287function device_has_carrier() {
5bb2429a
MT
288 local device=${1}
289 assert isset device
290
1848564d
MT
291 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
292}
293
1e4c26a4
MT
294function device_is_promisc() {
295 local device=${1}
296
297 ip link show ${device} | grep -qE "<.*PROMISC.*>"
298}
299
cf6e4606
MT
300function device_set_promisc() {
301 local device=${1}
302 local state=${2}
303
304 assert device_exists ${device}
305 assert isset state
306 assert isoneof state on off
307
308 ip link set ${device} promisc ${state}
309}
310
1848564d
MT
311# Check if the device is free
312function device_is_free() {
81ed640c 313 ! device_is_used $@
1848564d
MT
314}
315
316# Check if the device is used
317function device_is_used() {
5bb2429a 318 local device=${1}
1848564d 319
fb02e543
MT
320 device_has_virtuals ${device} && \
321 return ${EXIT_OK}
1848564d 322 device_is_bonded ${device} && \
fb02e543 323 return ${EXIT_OK}
81ed640c
MT
324 device_is_bridge_attached ${device} && \
325 return ${EXIT_OK}
1848564d 326
fb02e543 327 return ${EXIT_ERROR}
1848564d
MT
328}
329
1b7a1578
MT
330function device_hash() {
331 local device=${1}
332
37e4ec8b
MT
333 # Get mac address of device and remove all colons (:)
334 # that will result in a hash.
335 device=$(macify ${device})
336
337 echo "${device//:/}"
1b7a1578
MT
338}
339
340# Give the device a new name
341function device_set_name() {
1848564d 342 local source=$1
1578dae9 343 local destination=${2}
1848564d
MT
344
345 # Check if devices exists
346 if ! device_exists ${source} || device_exists ${destination}; then
347 return 4
348 fi
349
350 local up
351 if device_is_up ${source}; then
352 ip link set ${source} down
353 up=1
354 fi
355
356 ip link set ${source} name ${destination}
357
358 if [ "${up}" = "1" ]; then
359 ip link set ${destination} up
360 fi
361}
362
1848564d
MT
363# Set device up
364function device_set_up() {
5bb2429a 365 local device=${1}
1848564d 366
711ffac1
MT
367 # Silently fail if device was not found
368 [ -z "${device}" ] && return ${EXIT_ERROR}
369
1848564d
MT
370 # Do nothing if device is already up
371 device_is_up ${device} && return ${EXIT_OK}
372
81ed640c
MT
373 device_set_parent_up ${device}
374
375 log DEBUG "Setting up device '${device}'"
376
1848564d
MT
377 ip link set ${device} up
378}
379
81ed640c
MT
380function device_set_parent_up() {
381 local device=${1}
382 local parent
383
384 if device_is_virtual ${device}; then
8c6a8966 385 parent=$(virtual_get_parent ${device})
81ed640c
MT
386
387 device_is_up ${parent} && return ${EXIT_OK}
388
389 log DEBUG "Setting up parent device '${parent}' of '${device}'"
390
391 device_set_up ${parent}
392 return $?
393 fi
394
395 return ${EXIT_OK}
396}
397
1848564d
MT
398# Set device down
399function device_set_down() {
5bb2429a
MT
400 local device=${1}
401 assert isset device
1848564d 402
81ed640c
MT
403 local ret=${EXIT_OK}
404
405 if device_is_up ${device}; then
406 log DEBUG "Tearing down device '${device}'"
407
408 ip link set ${device} down
409 ret=$?
410 fi
411
412 device_set_parent_down ${device}
1848564d 413
81ed640c
MT
414 return ${ret}
415}
416
417function device_set_parent_down() {
418 local device=${1}
419 local parent
420
421 if device_is_virtual ${device}; then
8c6a8966 422 parent=$(virtual_get_parent ${device})
81ed640c
MT
423
424 device_is_up ${parent} || return ${EXIT_OK}
425
426 if device_is_free ${parent}; then
427 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
428
429 device_set_down ${parent}
430 fi
431 fi
432
433 return ${EXIT_OK}
1848564d
MT
434}
435
1848564d
MT
436function device_get_mtu() {
437 local device=${1}
438
439 if ! device_exists ${device}; then
440 error "Device '${device}' does not exist."
441 return ${EXIT_ERROR}
442 fi
443
f3e6fe50 444 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
445}
446
447# Set mtu to a device
448function device_set_mtu() {
1b7a1578 449 local device=${1}
1848564d
MT
450 local mtu=${2}
451
1b7a1578
MT
452 if ! device_exists ${device}; then
453 error "Device '${device}' does not exist."
454 return ${EXIT_ERROR}
455 fi
456
457 local oldmtu=$(device_get_mtu ${device})
458
459 if [ "${oldmtu}" = "${mtu}" ]; then
460 # No need to set mtu.
461 return ${EXIT_OK}
462 fi
463
464 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
465
1848564d 466 local up
1b7a1578
MT
467 if device_is_up ${device}; then
468 device_set_down ${device}
1848564d
MT
469 up=1
470 fi
471
1b7a1578 472 ip link set ${device} mtu ${mtu}
1848564d
MT
473 local ret=$?
474
475 if [ "${up}" = "1" ]; then
1b7a1578
MT
476 device_set_up ${device}
477 fi
478
479 if [ "${ret}" != "0" ]; then
480 error_log "Could not set mtu '${mtu}' on device '${device}'."
1848564d
MT
481 fi
482
483 return ${ret}
484}
485
486function device_discover() {
487 local device=${1}
488
1b7a1578
MT
489 log INFO "Running discovery process on device '${device}'."
490
1848564d 491 local hook
d61a01d4
MT
492 for hook in $(hook_zone_get_all); do
493 hook_zone_exec ${hook} discover ${device}
1848564d
MT
494 done
495}
496
38f61548 497function device_has_ip() {
1848564d
MT
498 local device=${1}
499 local addr=${2}
500
38f61548
MT
501 assert isset addr
502 assert device_exists ${device}
503
504 # IPv6 addresses must be fully imploded
505 local protocol=$(ip_detect_protocol ${addr})
506 case "${protocol}" in
507 ipv6)
508 addr=$(ipv6_implode ${addr})
509 ;;
510 esac
1848564d 511
38f61548 512 listmatch ${addr} $(device_get_addresses ${device})
1848564d 513}
4231f419 514
38f61548 515function device_get_addresses() {
4231f419 516 local device=${1}
4231f419 517
38f61548 518 assert device_exists ${device}
4231f419 519
38f61548
MT
520 local prot
521 local addr
522 local line
523 ip addr show ${device} | \
524 while read prot addr line; do
525 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
526 done
4231f419 527}
711ffac1 528
711ffac1
MT
529function __device_get_file() {
530 local device=${1}
531 local file=${2}
532
533 assert isset device
534 assert isset file
535
536 cat ${SYS_CLASS_NET}/${device}/${file}
537}
538
539function device_get_rx_bytes() {
540 local device=${1}
541
542 __device_get_file ${device} statistics/rx_bytes
543}
544
545function device_get_tx_bytes() {
546 local device=${1}
547
548 __device_get_file ${device} statistics/tx_bytes
549}
550
551function device_get_rx_packets() {
552 local device=${1}
553
554 __device_get_file ${device} statistics/rx_packets
555}
556
557function device_get_tx_packets() {
558 local device=${1}
559
560 __device_get_file ${device} statistics/tx_packets
561}
562
563function device_get_rx_errors() {
564 local device=${1}
565
566 __device_get_file ${device} statistics/rx_errors
567}
568
569function device_get_tx_errors() {
570 local device=${1}
571
572 __device_get_file ${device} statistics/tx_errors
573}