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