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