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