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 ###############################################################################
26 list_append devices $
(devices_get_all
)
29 list_append devices $
(phy_list
)
31 # Add all serial devices
32 list_append devices $
(serial_list
)
34 # Return a sorted result
38 # Check if the device exists
42 # If device name was not found, exit.
43 [ -n "${device}" ] ||
return ${EXIT_ERROR}
45 # Check for a normal network device.
46 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
48 # If the check above did not find a result,
50 phy_exists "${device}" && return ${EXIT_OK}
52 # If the check above did not find a result,
53 # we check for serial devices.
54 serial_exists ${device}
57 device_matches_pattern() {
64 pattern="^
${pattern//N/[[:digit:]]+}$
"
66 [[ ${device} =~ ${pattern} ]] \
67 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
74 # Nothing to do, it device does not exist.
75 device_exists ${device} || return ${EXIT_OK}
78 cmd_quiet ip link delete ${device}
81 if [ ${ret} -ne ${EXIT_OK} ]; then
82 log ERROR "device
: Could not delete device
'${device}': ${ret}"
93 local flags=$(__device_get_file ${device} flags)
95 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
102 # Check if the device is up
106 device_exists ${device} || return ${EXIT_ERROR}
108 device_has_flag ${device} 0x1
111 device_ifindex_to_name() {
115 local device device_idx
116 for device in ${SYS_CLASS_NET}/*; do
117 device=$(basename ${device})
118 device_exists ${device} || continue
120 device_idx=$(device_get_ifindex ${device})
122 if [ "${device_idx}" = "${idx}" ]; then
131 device_get_ifindex() {
135 local path="${SYS_CLASS_NET}/${1}/ifindex
"
137 # Check if file can be read.
138 [ -r "${path}" ] || return ${EXIT_ERROR}
143 # Check if the device is a batman-adv bridge
144 device_is_batman_adv() {
145 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
148 # Check if the device is a batman-adv slave port
149 device_is_batman_adv_slave() {
152 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
153 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
168 # Check if the device is a bonding device
169 device_is_bonding() {
170 [ -d "/sys
/class
/net
/${1}/bonding
" ]
173 # Check if the device bonded in a bonding device
177 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
180 # Check if the device is a bridge
182 [ -d "/sys
/class
/net
/${1}/bridge
" ]
185 device_is_bridge_attached() {
187 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
190 device_is_wireless_monitor() {
194 device_is_wireless "${device}" && \
195 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
198 device_is_wireless_adhoc() {
202 device_is_wireless "${device}" && \
203 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
206 device_get_bridge() {
210 # Check if device is attached to a bridge.
211 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
213 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
214 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
216 local ifindex=$(<${ifindex_path})
219 device_ifindex_to_name ${ifindex}
222 # Check if the device is a vlan device
227 [ -e "${PROC_NET_VLAN}/${device}" ]
230 # Check if the device has vlan devices
235 if device_is_vlan ${device}; then
239 local vlans=$(device_get_vlans ${device})
240 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
247 # If no 8021q module has been loaded into the kernel,
248 # we cannot do anything.
249 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
251 local dev spacer1 id spacer2 parent
252 while read dev spacer1 id spacer2 parent
; do
253 [ "${parent}" = "${device}" ] ||
continue
256 done < ${PROC_NET_VLAN_CONFIG}
259 # Check if the device is a ppp device
263 local type=$
(__device_get_file
${device} type)
265 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
268 # Check if the device is a pointopoint device.
272 device_has_flag ${device} 0x10
275 # Check if the device is a loopback device
276 device_is_loopback() {
279 [ "${device}" = "lo
" ]
282 # Check if the device is a dummy device
283 # This is the worst possible check, but all I could come up with
287 [[ ${device} =~ ^dummy[0-9]+$ ]]
290 # Check if the device is a wireless device
291 device_is_wireless() {
294 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
300 if device_is_wireless "${device}"; then
301 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
316 # Returns true if a device is a tun device
320 [ -e "${SYS_CLASS_NET}/${device}/tun_flags
" ]
323 # Check if the device is a physical network interface
324 device_is_ethernet() {
327 device_is_ethernet_compatible "${device}" || \
330 device_is_loopback ${device} && \
333 device_is_bonding ${device} && \
336 device_is_bridge ${device} && \
339 device_is_ppp ${device} && \
342 device_is_vlan ${device} && \
345 device_is_dummy ${device} && \
348 device_is_tun ${device} && \
354 # Get the device type
358 # If the device does not exist (happens on udev remove events),
359 # we do not bother to run all checks.
360 if ! device_exists "${device}"; then
363 elif device_is_vlan ${device}; then
366 elif device_is_bonding ${device}; then
369 elif device_is_bridge ${device}; then
372 elif device_is_ppp ${device}; then
375 elif device_is_batman_adv ${device}; then
378 elif device_is_loopback ${device}; then
381 elif device_is_wireless_adhoc ${device}; then
382 echo "wireless-adhoc
"
384 elif device_is_wireless ${device}; then
387 elif device_is_dummy ${device}; then
390 elif device_is_tun ${device}; then
393 elif device_is_ethernet ${device}; then
396 elif device_is_serial ${device}; then
399 elif device_is_phy ${device}; then
407 device_is_ethernet_compatible() {
410 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
411 local type="$
(__device_get_file
"${device}" "type")"
412 [[ "${type}" = "1" ]]
415 device_get_status() {
419 local status=${STATUS_DOWN}
421 if device_is_up ${device}; then
424 if ! device_has_carrier ${device}; then
425 status=${STATUS_NOCARRIER}
432 device_get_address() {
435 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
438 device_set_address() {
444 if ! device_exists "${device}"; then
445 error "Device
'${device}' does not exist.
"
449 # Do nothing if the address has not changed
450 local old_addr="$
(device_get_address
"${device}")"
451 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
455 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
458 if device_is_up "${device}"; then
459 device_set_down "${device}"
463 ip link set "${device}" address "${addr}"
466 if [ "${up}" = "1" ]; then
467 device_set_up "${device}"
470 if [ "${ret}" != "0" ]; then
471 error_log "Could not
set address
'${addr}' on device
'${device}'"
481 for device in ${SYS_CLASS_NET}/*; do
482 device=$(basename ${device})
484 # bonding_masters is no device
485 [ "${device}" = "bonding_masters
" ] && continue
487 devices="${devices} ${device}"
498 # Check if a device has a cable plugged in
499 device_has_carrier() {
503 local carrier=$(__device_get_file ${device} carrier)
504 [ "${carrier}" = "1" ]
507 device_is_promisc() {
510 device_has_flag ${device} 0x200
513 device_set_promisc() {
517 assert device_exists ${device}
519 assert isoneof state on off
521 ip link set ${device} promisc ${state}
524 # Check if the device is free
529 # Check if the device is used
533 device_has_vlans ${device} && \
535 device_is_bonded ${device} && \
537 device_is_bridge_attached ${device} && \
543 # Give the device a new name
546 local destination=${2}
548 # Check if devices exists
549 if ! device_exists ${source} || device_exists ${destination}; then
554 if device_is_up ${source}; then
555 ip link set ${source} down
559 ip link set ${source} name ${destination}
561 if [ "${up}" = "1" ]; then
562 ip link set ${destination} up
570 # Silently fail if device was not found
571 [ -z "${device}" ] && return ${EXIT_ERROR}
573 # Do nothing if device is already up
574 device_is_up ${device} && return ${EXIT_OK}
576 device_set_parent_up ${device}
578 log DEBUG "Setting up device
'${device}'"
580 ip link set ${device} up
583 if interrupt_use_smp_affinity; then
584 device_auto_configure_smp_affinity "${port}"
590 device_set_parent_up() {
594 if device_is_vlan ${device}; then
595 parent=$(vlan_get_parent ${device})
597 device_is_up ${parent} && return ${EXIT_OK}
599 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
601 device_set_up ${parent}
615 if device_is_up ${device}; then
616 log DEBUG "Tearing down device
'${device}'"
618 ip link set ${device} down
622 device_set_parent_down ${device}
627 device_set_parent_down() {
631 if device_is_vlan ${device}; then
632 parent=$(vlan_get_parent ${device})
634 device_is_up ${parent} || return ${EXIT_OK}
636 if device_is_free ${parent}; then
637 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
639 device_set_down ${parent}
649 if ! device_exists ${device}; then
650 error "Device
'${device}' does not exist.
"
654 echo $(<${SYS_CLASS_NET}/${device}/mtu)
657 # Set mtu to a device
662 if ! device_exists ${device}; then
663 error "Device
'${device}' does not exist.
"
667 local oldmtu=$(device_get_mtu ${device})
669 if [ "${oldmtu}" = "${mtu}" ]; then
670 # No need to set mtu.
674 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
677 if device_is_up ${device}; then
678 device_set_down ${device}
682 ip link set ${device} mtu ${mtu}
685 if [ "${up}" = "1" ]; then
686 device_set_up ${device}
689 if [ "${ret}" != "0" ]; then
690 error_log "Could not set mtu '${mtu}' on device '${device}'."
696 device_adjust_mtu() {
700 local other_device="${2}"
702 local mtu="$(device_get_mtu "${other_device}")"
703 device_set_mtu "${device}" "${mtu}"
709 log INFO "Running discovery process on device '${device}'."
712 for hook in $(hook_zone_get_all); do
713 hook_zone_exec ${hook} discover ${device}
722 # Flash for ten seconds by default
726 local background="false"
735 seconds="$(cli_get_val "${arg}")"
738 done <<< "$(args $@)"
740 assert isinteger seconds
742 if ! device_exists "${device}"; then
743 log ERROR "Cannot identify device ${device}: Does not exist"
747 if ! device_is_ethernet "${device}"; then
748 log DEBUG "Cannot identify device ${device}: Not an ethernet device"
749 return ${EXIT_NOT_SUPPORTED}
752 log DEBUG "Identifying device ${device}"
754 local command="ethtool --identify ${device} ${seconds}"
757 if enabled background; then
758 cmd_background "${command}"
760 cmd_quiet "${command}"
772 assert device_exists ${device}
774 # IPv6 addresses must be fully imploded
775 local protocol=$(ip_detect_protocol ${addr})
776 case "${protocol}" in
778 addr=$(ipv6_format "${addr}")
782 listmatch ${addr} $(device_get_addresses ${device})
785 device_get_addresses() {
788 assert device_exists ${device}
793 ip addr show ${device} | \
794 while read prot addr line; do
795 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
799 __device_get_file() {
806 local path="${SYS_CLASS_NET}/${device}/${file}"
807 [ -r "${path}" ] || return ${EXIT_ERROR}
812 __device_set_file() {
819 local path="${SYS_CLASS_NET}/${device}/${file}"
820 if [ ! -w "${path}" ]; then
821 log DEBUG "Cannot write to file '${file}' (${value})"
825 echo "${value}" > "${path}"
828 device_get_rx_bytes() {
831 __device_get_file ${device} statistics/rx_bytes
834 device_get_tx_bytes() {
837 __device_get_file ${device} statistics/tx_bytes
840 device_get_rx_packets() {
843 __device_get_file ${device} statistics/rx_packets
846 device_get_tx_packets() {
849 __device_get_file ${device} statistics/tx_packets
852 device_get_rx_errors() {
855 __device_get_file ${device} statistics/rx_errors
858 device_get_tx_errors() {
861 __device_get_file ${device} statistics/tx_errors
867 __device_get_file ${device} speed
870 device_get_duplex() {
873 __device_get_file ${device} duplex
876 device_get_link_string() {
882 local speed="$(device_get_speed "${device}")"
884 list_append s "${speed} MBit/s"
887 local duplex="$(device_get_duplex "${device}")"
888 if isset duplex; then
889 list_append s "${duplex} duplex"
895 device_auto_configure_smp_affinity() {
900 if lock_acquire "smp-affinity"; then
901 device_set_smp_affinity "${port}" auto
903 lock_release "smp-affinity"
907 device_set_smp_affinity() {
913 # mode can be auto which will automatically try to find
914 # the least busy processor, or an integer for the desired
915 # processor that should handle this device
917 local num_processors=$(system_get_processors)
919 if [ "${mode}" = "auto" ]; then
920 local processor=$(interrupt_choose_least_busy_processor)
922 assert isinteger mode
923 local processor=${mode}
925 if [ ${processor} -gt ${num_processors} ]; then
926 log ERROR "Processor ${processor} does not exist"
931 local interrupts=$(interrupts_for_device ${device})
932 if ! isset interrupts; then
933 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
939 for interrupt in ${interrupts}; do
940 interrupt_set_smp_affinity ${interrupt} ${processor}
943 # Find all queues and assign them to the next processor
945 for queue in $(device_get_queues ${device}); do
947 # Only handle receive queues
949 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
950 interrupt_set_smp_affinity ${interrupt} ${processor}
953 device_queue_set_smp_affinity ${device} ${queue} ${processor}
962 # Get the next available processor if in auto mode
963 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
969 device_get_queues() {
975 for queue in ${SYS_CLASS_NET}/${device}/queues/*; do
980 device_queue_set_smp_affinity() {
987 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
988 assert [ -w "${path}" ]
990 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
992 __processor_id_to_bitmap ${processor} > ${path}