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