2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
27 if device_exists
${device}; then
33 for d
in $
(devices_get_all
); do
34 if [ "$(device_get_address ${d})" = "${device}" ]; then
48 if mac_is_valid
${device}; then
53 if device_exists
${device}; then
54 device_get_address
${device}
61 # Check if the device exists
62 function device_exists
() {
65 # If device name was not found, exit.
66 [ -n "${device}" ] ||
return ${EXIT_ERROR}
68 # Check for a normal network device.
69 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
71 # If the check above, did not find a result,
72 # we check for serial devices.
73 serial_exists ${device}
76 function device_matches_pattern() {
83 pattern="^
${pattern//N/[[:digit:]]+}$
"
85 [[ ${device} =~ ${pattern} ]] \
86 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
89 function device_delete() {
93 # Nothing to do, it device does not exist.
94 device_exists ${device} || return ${EXIT_OK}
97 cmd_quiet ip link delete ${device}
100 if [ ${ret} -ne ${EXIT_OK} ]; then
101 log ERROR "device
: Could not delete device
'${device}': ${ret}"
108 function device_has_flag() {
112 local flags=$(__device_get_file ${device} flags)
114 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
121 # Check if the device is up
122 function device_is_up() {
125 device_exists ${device} || return ${EXIT_ERROR}
127 device_has_flag ${device} 0x1
130 function device_ifindex_to_name() {
134 local device device_idx
135 for device in ${SYS_CLASS_NET}/*; do
136 device=$(basename ${device})
137 device_exists ${device} || continue
139 device_idx=$(device_get_ifindex ${device})
141 if [ "${device_idx}" = "${idx}" ]; then
150 function device_get_ifindex() {
154 local path="${SYS_CLASS_NET}/${1}/ifindex
"
156 # Check if file can be read.
157 [ -r "${path}" ] || return ${EXIT_ERROR}
162 # Check if the device is a batman-adv bridge
163 function device_is_batman_adv() {
164 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
167 # Check if the device is a batman-adv slave port
168 function device_is_batman_adv_slave() {
171 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
172 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
187 # Check if the device is a bonding device
188 function device_is_bonding() {
189 [ -d "/sys
/class
/net
/${1}/bonding
" ]
192 # Check if the device bonded in a bonding device
193 function device_is_bonded() {
196 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
199 # Check if the device is a bridge
200 function device_is_bridge() {
201 [ -d "/sys
/class
/net
/${1}/bridge
" ]
204 function device_is_bridge_attached() {
206 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
209 function device_is_wireless_monitor() {
213 device_is_wireless "${device}" && \
214 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
217 function device_is_wireless_adhoc() {
221 device_is_wireless "${device}" && \
222 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
225 function device_get_bridge() {
229 # Check if device is attached to a bridge.
230 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
232 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
233 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
235 local ifindex=$(<${ifindex_path})
238 device_ifindex_to_name ${ifindex}
241 # Check if the device is a vlan device
242 function device_is_vlan() {
246 [ -e "${PROC_NET_VLAN}/${device}" ]
249 # Check if the device has vlan devices
250 function device_has_vlans() {
254 if device_is_vlan ${device}; then
258 local vlans=$(device_get_vlans ${device})
259 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
262 function device_get_vlans
() {
266 # If no 8021q module has been loaded into the kernel,
267 # we cannot do anything.
268 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
270 local dev spacer1 id spacer2 parent
271 while read dev spacer1 id spacer2 parent
; do
272 [ "${parent}" = "${device}" ] ||
continue
275 done < ${PROC_NET_VLAN_CONFIG}
278 # Check if the device is a ppp device
279 function device_is_ppp
() {
282 local type=$
(__device_get_file
${device} type)
284 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
287 # Check if the device is a pointopoint device.
288 function device_is_ptp() {
291 device_has_flag ${device} 0x10
294 # Check if the device is a loopback device
295 function device_is_loopback() {
298 [ "${device}" = "lo
" ]
301 # Check if the device is a dummy device
302 # This is the worst possible check, but all I could come up with
303 function device_is_dummy() {
306 [[ ${device} =~ ^dummy[0-9]+$ ]]
309 # Check if the device is a wireless device
310 function device_is_wireless() {
313 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
316 function device_get_phy() {
319 if device_is_wireless "${device}"; then
320 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
327 function device_is_serial() {
331 # Check if the device is a physical network interface
332 function device_is_ethernet() {
335 device_is_ethernet_compatible "${device}" || \
338 device_is_loopback ${device} && \
341 device_is_bonding ${device} && \
344 device_is_bridge ${device} && \
347 device_is_ppp ${device} && \
350 device_is_vlan ${device} && \
353 device_is_dummy ${device} && \
359 # Get the device type
360 function device_get_type() {
363 # If the device does not exist (happens on udev remove events),
364 # we do not bother to run all checks.
365 if ! device_exists "${device}"; then
368 elif device_is_vlan ${device}; then
371 elif device_is_bonding ${device}; then
374 elif device_is_bridge ${device}; then
377 elif device_is_ppp ${device}; then
380 elif device_is_batman_adv ${device}; then
383 elif device_is_loopback ${device}; then
386 elif device_is_wireless_adhoc ${device}; then
387 echo "wireless-adhoc
"
389 elif device_is_wireless ${device}; then
392 elif device_is_dummy ${device}; then
395 elif device_is_ethernet ${device}; then
398 elif device_is_serial ${device}; then
406 function device_is_ethernet_compatible() {
409 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
410 local type="$
(__device_get_file
"${device}" "type")"
411 [[ "${type}" = "1" ]]
414 function device_get_status() {
418 local status=${STATUS_DOWN}
420 if device_is_up ${device}; then
423 if ! device_has_carrier ${device}; then
424 status=${STATUS_NOCARRIER}
431 function device_get_address() {
434 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
437 function device_set_address() {
443 if ! device_exists "${device}"; then
444 error "Device
'${device}' does not exist.
"
448 # Do nothing if the address has not changed
449 local old_addr="$
(device_get_address
"${device}")"
450 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
454 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
457 if device_is_up "${device}"; then
458 device_set_down "${device}"
462 ip link set "${device}" address "${addr}"
465 if [ "${up}" = "1" ]; then
466 device_set_up "${device}"
469 if [ "${ret}" != "0" ]; then
470 error_log "Could not
set address
'${addr}' on device
'${device}'"
476 function device_get() {
480 for device in ${SYS_CLASS_NET}/*; do
481 device=$(basename ${device})
483 # bonding_masters is no device
484 [ "${device}" = "bonding_masters
" ] && continue
486 devices="${devices} ${device}"
493 function devices_get_all() {
497 # Check if a device has a cable plugged in
498 function device_has_carrier() {
502 local carrier=$(__device_get_file ${device} carrier)
503 [ "${carrier}" = "1" ]
506 function device_is_promisc() {
509 device_has_flag ${device} 0x200
512 function device_set_promisc() {
516 assert device_exists ${device}
518 assert isoneof state on off
520 ip link set ${device} promisc ${state}
523 # Check if the device is free
524 function device_is_free() {
528 # Check if the device is used
529 function device_is_used() {
532 device_has_vlans ${device} && \
534 device_is_bonded ${device} && \
536 device_is_bridge_attached ${device} && \
542 function device_hash() {
545 # Get mac address of device and remove all colons (:)
546 # that will result in a hash.
547 device=$(macify ${device})
552 # Give the device a new name
553 function device_set_name() {
555 local destination=${2}
557 # Check if devices exists
558 if ! device_exists ${source} || device_exists ${destination}; then
563 if device_is_up ${source}; then
564 ip link set ${source} down
568 ip link set ${source} name ${destination}
570 if [ "${up}" = "1" ]; then
571 ip link set ${destination} up
576 function device_set_up() {
579 # Silently fail if device was not found
580 [ -z "${device}" ] && return ${EXIT_ERROR}
582 # Do nothing if device is already up
583 device_is_up ${device} && return ${EXIT_OK}
585 device_set_parent_up ${device}
587 log DEBUG "Setting up device
'${device}'"
589 ip link set ${device} up
592 function device_set_parent_up() {
596 if device_is_vlan ${device}; then
597 parent=$(vlan_get_parent ${device})
599 device_is_up ${parent} && return ${EXIT_OK}
601 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
603 device_set_up ${parent}
611 function device_set_down() {
617 if device_is_up ${device}; then
618 log DEBUG "Tearing down device
'${device}'"
620 ip link set ${device} down
624 device_set_parent_down ${device}
629 function device_set_parent_down() {
633 if device_is_vlan ${device}; then
634 parent=$(vlan_get_parent ${device})
636 device_is_up ${parent} || return ${EXIT_OK}
638 if device_is_free ${parent}; then
639 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
641 device_set_down ${parent}
648 function device_get_mtu() {
651 if ! device_exists ${device}; then
652 error "Device
'${device}' does not exist.
"
656 echo $(<${SYS_CLASS_NET}/${device}/mtu)
659 # Set mtu to a device
660 function device_set_mtu() {
664 if ! device_exists ${device}; then
665 error "Device
'${device}' does not exist.
"
669 local oldmtu=$(device_get_mtu ${device})
671 if [ "${oldmtu}" = "${mtu}" ]; then
672 # No need to set mtu.
676 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
679 if device_is_up ${device}; then
680 device_set_down ${device}
684 ip link set ${device} mtu ${mtu}
687 if [ "${up}" = "1" ]; then
688 device_set_up ${device}
691 if [ "${ret}" != "0" ]; then
692 error_log "Could not set mtu '${mtu}' on device '${device}'."
698 function device_adjust_mtu() {
702 local other_device="${2}"
704 local mtu="$(device_get_mtu "${other_device}")"
705 device_set_mtu "${device}" "${mtu}"
708 function device_discover() {
711 log INFO "Running discovery process on device '${device}'."
714 for hook in $(hook_zone_get_all); do
715 hook_zone_exec ${hook} discover ${device}
719 function device_has_ip() {
724 assert device_exists ${device}
726 # IPv6 addresses must be fully imploded
727 local protocol=$(ip_detect_protocol ${addr})
728 case "${protocol}" in
730 addr=$(ipv6_implode ${addr})
734 listmatch ${addr} $(device_get_addresses ${device})
737 function device_get_addresses() {
740 assert device_exists ${device}
745 ip addr show ${device} | \
746 while read prot addr line; do
747 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
751 function __device_get_file() {
758 local path="${SYS_CLASS_NET}/${device}/${file}"
759 [ -r "${path}" ] || return ${EXIT_ERROR}
764 function __device_set_file() {
771 local path="${SYS_CLASS_NET}/${device}/${file}"
772 if [ ! -w "${path}" ]; then
773 log DEBUG "Cannot write to file '${file}' (${value})"
777 echo "${value}" > "${path}"
780 function device_get_rx_bytes() {
783 __device_get_file ${device} statistics/rx_bytes
786 function device_get_tx_bytes() {
789 __device_get_file ${device} statistics/tx_bytes
792 function device_get_rx_packets() {
795 __device_get_file ${device} statistics/rx_packets
798 function device_get_tx_packets() {
801 __device_get_file ${device} statistics/tx_packets
804 function device_get_rx_errors() {
807 __device_get_file ${device} statistics/rx_errors
810 function device_get_tx_errors() {
813 __device_get_file ${device} statistics/tx_errors
816 function device_get_speed() {
819 __device_get_file ${device} speed
822 function device_get_duplex() {
825 __device_get_file ${device} duplex
828 function device_get_link_string() {
834 local speed="$(device_get_speed "${device}")"
836 list_append s "${speed} MBit/s"
839 local duplex="$(device_get_duplex "${device}")"
840 if isset duplex; then
841 list_append s "${duplex} duplex"