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 function device_list
() {
65 list_append devices $
(devices_get_all
)
68 list_append devices $
(phy_list
)
70 # Add all serial devices
71 list_append devices $
(serial_list
)
73 # Return a sorted result
77 # Check if the device exists
78 function device_exists
() {
81 # If device name was not found, exit.
82 [ -n "${device}" ] ||
return ${EXIT_ERROR}
84 # Check for a normal network device.
85 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
87 # If the check above did not find a result,
89 phy_exists "${device}" && return ${EXIT_OK}
91 # If the check above did not find a result,
92 # we check for serial devices.
93 serial_exists ${device}
96 function device_matches_pattern() {
103 pattern="^
${pattern//N/[[:digit:]]+}$
"
105 [[ ${device} =~ ${pattern} ]] \
106 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
109 function device_delete() {
113 # Nothing to do, it device does not exist.
114 device_exists ${device} || return ${EXIT_OK}
117 cmd_quiet ip link delete ${device}
120 if [ ${ret} -ne ${EXIT_OK} ]; then
121 log ERROR "device
: Could not delete device
'${device}': ${ret}"
128 function device_has_flag() {
132 local flags=$(__device_get_file ${device} flags)
134 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
141 # Check if the device is up
142 function device_is_up() {
145 device_exists ${device} || return ${EXIT_ERROR}
147 device_has_flag ${device} 0x1
150 function device_ifindex_to_name() {
154 local device device_idx
155 for device in ${SYS_CLASS_NET}/*; do
156 device=$(basename ${device})
157 device_exists ${device} || continue
159 device_idx=$(device_get_ifindex ${device})
161 if [ "${device_idx}" = "${idx}" ]; then
170 function device_get_ifindex() {
174 local path="${SYS_CLASS_NET}/${1}/ifindex
"
176 # Check if file can be read.
177 [ -r "${path}" ] || return ${EXIT_ERROR}
182 # Check if the device is a batman-adv bridge
183 function device_is_batman_adv() {
184 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
187 # Check if the device is a batman-adv slave port
188 function device_is_batman_adv_slave() {
191 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
192 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
207 # Check if the device is a bonding device
208 function device_is_bonding() {
209 [ -d "/sys
/class
/net
/${1}/bonding
" ]
212 # Check if the device bonded in a bonding device
213 function device_is_bonded() {
216 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
219 # Check if the device is a bridge
220 function device_is_bridge() {
221 [ -d "/sys
/class
/net
/${1}/bridge
" ]
224 function device_is_bridge_attached() {
226 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
229 function device_is_wireless_monitor() {
233 device_is_wireless "${device}" && \
234 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
237 function device_is_wireless_adhoc() {
241 device_is_wireless "${device}" && \
242 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
245 function device_get_bridge() {
249 # Check if device is attached to a bridge.
250 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
252 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
253 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
255 local ifindex=$(<${ifindex_path})
258 device_ifindex_to_name ${ifindex}
261 # Check if the device is a vlan device
262 function device_is_vlan() {
266 [ -e "${PROC_NET_VLAN}/${device}" ]
269 # Check if the device has vlan devices
270 function device_has_vlans() {
274 if device_is_vlan ${device}; then
278 local vlans=$(device_get_vlans ${device})
279 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
282 function device_get_vlans
() {
286 # If no 8021q module has been loaded into the kernel,
287 # we cannot do anything.
288 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
290 local dev spacer1 id spacer2 parent
291 while read dev spacer1 id spacer2 parent
; do
292 [ "${parent}" = "${device}" ] ||
continue
295 done < ${PROC_NET_VLAN_CONFIG}
298 # Check if the device is a ppp device
299 function device_is_ppp
() {
302 local type=$
(__device_get_file
${device} type)
304 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
307 # Check if the device is a pointopoint device.
308 function device_is_ptp() {
311 device_has_flag ${device} 0x10
314 # Check if the device is a loopback device
315 function device_is_loopback() {
318 [ "${device}" = "lo
" ]
321 # Check if the device is a dummy device
322 # This is the worst possible check, but all I could come up with
323 function device_is_dummy() {
326 [[ ${device} =~ ^dummy[0-9]+$ ]]
329 # Check if the device is a wireless device
330 function device_is_wireless() {
333 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
336 function device_get_phy() {
339 if device_is_wireless "${device}"; then
340 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
347 function device_is_phy() {
351 function device_is_serial() {
355 # Check if the device is a physical network interface
356 function device_is_ethernet() {
359 device_is_ethernet_compatible "${device}" || \
362 device_is_loopback ${device} && \
365 device_is_bonding ${device} && \
368 device_is_bridge ${device} && \
371 device_is_ppp ${device} && \
374 device_is_vlan ${device} && \
377 device_is_dummy ${device} && \
383 # Get the device type
384 function device_get_type() {
387 # If the device does not exist (happens on udev remove events),
388 # we do not bother to run all checks.
389 if ! device_exists "${device}"; then
392 elif device_is_vlan ${device}; then
395 elif device_is_bonding ${device}; then
398 elif device_is_bridge ${device}; then
401 elif device_is_ppp ${device}; then
404 elif device_is_batman_adv ${device}; then
407 elif device_is_loopback ${device}; then
410 elif device_is_wireless_adhoc ${device}; then
411 echo "wireless-adhoc
"
413 elif device_is_wireless ${device}; then
416 elif device_is_dummy ${device}; then
419 elif device_is_ethernet ${device}; then
422 elif device_is_serial ${device}; then
425 elif device_is_phy ${device}; then
433 function device_is_ethernet_compatible() {
436 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
437 local type="$
(__device_get_file
"${device}" "type")"
438 [[ "${type}" = "1" ]]
441 function device_get_status() {
445 local status=${STATUS_DOWN}
447 if device_is_up ${device}; then
450 if ! device_has_carrier ${device}; then
451 status=${STATUS_NOCARRIER}
458 function device_get_address() {
461 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
464 function device_set_address() {
470 if ! device_exists "${device}"; then
471 error "Device
'${device}' does not exist.
"
475 # Do nothing if the address has not changed
476 local old_addr="$
(device_get_address
"${device}")"
477 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
481 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
484 if device_is_up "${device}"; then
485 device_set_down "${device}"
489 ip link set "${device}" address "${addr}"
492 if [ "${up}" = "1" ]; then
493 device_set_up "${device}"
496 if [ "${ret}" != "0" ]; then
497 error_log "Could not
set address
'${addr}' on device
'${device}'"
503 function device_get() {
507 for device in ${SYS_CLASS_NET}/*; do
508 device=$(basename ${device})
510 # bonding_masters is no device
511 [ "${device}" = "bonding_masters
" ] && continue
513 devices="${devices} ${device}"
520 function devices_get_all() {
524 # Check if a device has a cable plugged in
525 function device_has_carrier() {
529 local carrier=$(__device_get_file ${device} carrier)
530 [ "${carrier}" = "1" ]
533 function device_is_promisc() {
536 device_has_flag ${device} 0x200
539 function device_set_promisc() {
543 assert device_exists ${device}
545 assert isoneof state on off
547 ip link set ${device} promisc ${state}
550 # Check if the device is free
551 function device_is_free() {
555 # Check if the device is used
556 function device_is_used() {
559 device_has_vlans ${device} && \
561 device_is_bonded ${device} && \
563 device_is_bridge_attached ${device} && \
569 function device_hash() {
572 # Get mac address of device and remove all colons (:)
573 # that will result in a hash.
574 device=$(macify ${device})
579 # Give the device a new name
580 function device_set_name() {
582 local destination=${2}
584 # Check if devices exists
585 if ! device_exists ${source} || device_exists ${destination}; then
590 if device_is_up ${source}; then
591 ip link set ${source} down
595 ip link set ${source} name ${destination}
597 if [ "${up}" = "1" ]; then
598 ip link set ${destination} up
603 function device_set_up() {
606 # Silently fail if device was not found
607 [ -z "${device}" ] && return ${EXIT_ERROR}
609 # Do nothing if device is already up
610 device_is_up ${device} && return ${EXIT_OK}
612 device_set_parent_up ${device}
614 log DEBUG "Setting up device
'${device}'"
616 ip link set ${device} up
619 function device_set_parent_up() {
623 if device_is_vlan ${device}; then
624 parent=$(vlan_get_parent ${device})
626 device_is_up ${parent} && return ${EXIT_OK}
628 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
630 device_set_up ${parent}
638 function device_set_down() {
644 if device_is_up ${device}; then
645 log DEBUG "Tearing down device
'${device}'"
647 ip link set ${device} down
651 device_set_parent_down ${device}
656 function device_set_parent_down() {
660 if device_is_vlan ${device}; then
661 parent=$(vlan_get_parent ${device})
663 device_is_up ${parent} || return ${EXIT_OK}
665 if device_is_free ${parent}; then
666 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
668 device_set_down ${parent}
675 function device_get_mtu() {
678 if ! device_exists ${device}; then
679 error "Device
'${device}' does not exist.
"
683 echo $(<${SYS_CLASS_NET}/${device}/mtu)
686 # Set mtu to a device
687 function device_set_mtu() {
691 if ! device_exists ${device}; then
692 error "Device
'${device}' does not exist.
"
696 local oldmtu=$(device_get_mtu ${device})
698 if [ "${oldmtu}" = "${mtu}" ]; then
699 # No need to set mtu.
703 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
706 if device_is_up ${device}; then
707 device_set_down ${device}
711 ip link set ${device} mtu ${mtu}
714 if [ "${up}" = "1" ]; then
715 device_set_up ${device}
718 if [ "${ret}" != "0" ]; then
719 error_log "Could not set mtu '${mtu}' on device '${device}'."
725 function device_adjust_mtu() {
729 local other_device="${2}"
731 local mtu="$(device_get_mtu "${other_device}")"
732 device_set_mtu "${device}" "${mtu}"
735 function device_discover() {
738 log INFO "Running discovery process on device '${device}'."
741 for hook in $(hook_zone_get_all); do
742 hook_zone_exec ${hook} discover ${device}
746 function device_has_ip() {
751 assert device_exists ${device}
753 # IPv6 addresses must be fully imploded
754 local protocol=$(ip_detect_protocol ${addr})
755 case "${protocol}" in
757 addr=$(ipv6_implode ${addr})
761 listmatch ${addr} $(device_get_addresses ${device})
764 function device_get_addresses() {
767 assert device_exists ${device}
772 ip addr show ${device} | \
773 while read prot addr line; do
774 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
778 function __device_get_file() {
785 local path="${SYS_CLASS_NET}/${device}/${file}"
786 [ -r "${path}" ] || return ${EXIT_ERROR}
791 function __device_set_file() {
798 local path="${SYS_CLASS_NET}/${device}/${file}"
799 if [ ! -w "${path}" ]; then
800 log DEBUG "Cannot write to file '${file}' (${value})"
804 echo "${value}" > "${path}"
807 function device_get_rx_bytes() {
810 __device_get_file ${device} statistics/rx_bytes
813 function device_get_tx_bytes() {
816 __device_get_file ${device} statistics/tx_bytes
819 function device_get_rx_packets() {
822 __device_get_file ${device} statistics/rx_packets
825 function device_get_tx_packets() {
828 __device_get_file ${device} statistics/tx_packets
831 function device_get_rx_errors() {
834 __device_get_file ${device} statistics/rx_errors
837 function device_get_tx_errors() {
840 __device_get_file ${device} statistics/tx_errors
843 function device_get_speed() {
846 __device_get_file ${device} speed
849 function device_get_duplex() {
852 __device_get_file ${device} duplex
855 function device_get_link_string() {
861 local speed="$(device_get_speed "${device}")"
863 list_append s "${speed} MBit/s"
866 local duplex="$(device_get_duplex "${device}")"
867 if isset duplex; then
868 list_append s "${duplex} duplex"