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