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