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