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