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