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