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 for device
in $
(list_directory
${SYS_CLASS_NET}); do
28 list_append_one devices
"${device}"
32 list_append devices $
(phy_list
)
34 # Add all serial devices
35 list_append devices $
(serial_list
)
37 # Return a sorted result
41 # Check if the device exists
45 # If device name was not found, exit.
46 [ -n "${device}" ] ||
return ${EXIT_ERROR}
48 # Check for a normal network device.
49 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
51 # If the check above did not find a result,
53 phy_exists "${device}" && return ${EXIT_OK}
55 # If the check above did not find a result,
56 # we check for serial devices.
57 serial_exists ${device}
60 device_matches_pattern() {
67 pattern="^
${pattern//N/[[:digit:]]+}$
"
69 [[ ${device} =~ ${pattern} ]] \
70 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
77 # Nothing to do, it device does not exist.
78 device_exists ${device} || return ${EXIT_OK}
81 cmd_quiet ip link delete ${device}
84 if [ ${ret} -ne ${EXIT_OK} ]; then
85 log ERROR "device
: Could not delete device
'${device}': ${ret}"
96 local flags=$(__device_get_file ${device} flags)
98 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
105 # Check if the device is up
109 device_exists ${device} || return ${EXIT_ERROR}
111 device_has_flag ${device} 0x1
114 device_ifindex_to_name() {
118 local device device_idx
119 for device in $(list_directory "${SYS_CLASS_NET}"); do
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 bonding device
144 device_is_bonding() {
145 [ -d "/sys
/class
/net
/${1}/bonding
" ]
148 # Check if the device bonded in a bonding device
152 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
155 # Check if the device is a bridge
157 [ -d "/sys
/class
/net
/${1}/bridge
" ]
160 device_is_bridge_attached() {
162 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
165 device_is_wireless_monitor() {
169 device_is_wireless "${device}" && \
170 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
173 device_is_wireless_adhoc() {
177 device_is_wireless "${device}" && \
178 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
181 device_get_bridge() {
185 # Check if device is attached to a bridge.
186 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
188 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
189 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
191 local ifindex=$(<${ifindex_path})
194 device_ifindex_to_name ${ifindex}
197 # Check if the device is a vlan device
202 [ -e "${PROC_NET_VLAN}/${device}" ]
205 # Check if the device has vlan devices
210 if device_is_vlan ${device}; then
214 local vlans=$(device_get_vlans ${device})
215 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
222 # If no 8021q module has been loaded into the kernel,
223 # we cannot do anything.
224 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
226 local dev spacer1 id spacer2 parent
227 while read dev spacer1 id spacer2 parent
; do
228 [ "${parent}" = "${device}" ] ||
continue
231 done < ${PROC_NET_VLAN_CONFIG}
234 # Check if the device is a ppp device
238 local type=$
(__device_get_file
${device} type)
240 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
243 # Check if the device is a pointopoint device.
247 device_has_flag ${device} 0x10
250 # Check if the device is a loopback device
251 device_is_loopback() {
254 [ "${device}" = "lo
" ]
257 # Check if the device is a dummy device
258 # This is the worst possible check, but all I could come up with
262 [[ ${device} =~ ^dummy[0-9]+$ ]]
268 [[ ${device} =~ ^ipsec\- ]]
271 # Check if the device is a wireless device
272 device_is_wireless() {
275 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
281 local type=$(__device_get_file ${device} type)
283 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
289 if device_is_wireless
"${device}"; then
290 print
"$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
305 # Returns true if a device is a tun device
309 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
312 # Check if the device is a physical network interface
313 device_is_ethernet
() {
316 device_is_ethernet_compatible
"${device}" || \
319 device_is_loopback
${device} && \
322 device_is_bonding
${device} && \
325 device_is_bridge
${device} && \
328 device_is_ppp
${device} && \
331 device_is_vlan
${device} && \
334 device_is_dummy
${device} && \
337 device_is_tun
${device} && \
343 # Get the device type
347 # If the device does not exist (happens on udev remove events),
348 # we do not bother to run all checks.
349 if ! device_exists
"${device}"; then
352 elif device_is_vlan
${device}; then
355 elif device_is_bonding
${device}; then
358 elif device_is_bridge
${device}; then
361 elif device_is_ppp
${device}; then
364 elif device_is_loopback
${device}; then
367 elif device_is_wireless_adhoc
${device}; then
368 echo "wireless-adhoc"
370 elif device_is_wireless
${device}; then
373 elif device_is_dummy
${device}; then
376 elif device_is_tun
${device}; then
379 elif device_is_ethernet
${device}; then
382 elif device_is_serial
${device}; then
385 elif device_is_phy
${device}; then
388 elif device_is_vti
${device}; then
396 device_is_ethernet_compatible
() {
399 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
400 local type="$(__device_get_file "${device}" "type")"
401 [[ "${type}" = "1" ]]
404 device_get_status
() {
408 local status
=${STATUS_DOWN}
410 if device_is_up
${device}; then
413 if ! device_has_carrier
${device}; then
414 status
=${STATUS_NOCARRIER}
421 device_get_address
() {
424 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
427 device_set_address
() {
433 if ! device_exists
"${device}"; then
434 error
"Device '${device}' does not exist."
438 # Do nothing if the address has not changed
439 local old_addr
="$(device_get_address "${device}")"
440 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
444 log DEBUG
"Setting address of '${device}' from '${old_addr}' to '${addr}'"
447 if device_is_up
"${device}"; then
448 device_set_down
"${device}"
452 ip link
set "${device}" address
"${addr}"
455 if [ "${up}" = "1" ]; then
456 device_set_up
"${device}"
459 if [ "${ret}" != "0" ]; then
460 error_log
"Could not set address '${addr}' on device '${device}'"
468 for device
in $
(list_directory
"${SYS_CLASS_NET}"); do
469 # bonding_masters is no device
470 [ "${device}" = "bonding_masters" ] && continue
478 # Check if a device has a cable plugged in
479 device_has_carrier
() {
483 local carrier
=$
(__device_get_file
${device} carrier
)
484 [ "${carrier}" = "1" ]
487 device_is_promisc
() {
490 device_has_flag
${device} 0x200
493 device_set_promisc
() {
497 assert device_exists
${device}
499 assert isoneof state on off
501 ip link
set ${device} promisc
${state}
504 # Check if the device is free
506 ! device_is_used
"$@"
509 # Check if the device is used
513 device_has_vlans
${device} && \
515 device_is_bonded
${device} && \
517 device_is_bridge_attached
${device} && \
523 # Give the device a new name
526 local destination
=${2}
528 # Check if devices exists
529 if ! device_exists
${source} || device_exists
${destination}; then
534 if device_is_up
${source}; then
535 ip link
set ${source} down
539 ip link
set ${source} name
${destination}
541 if [ "${up}" = "1" ]; then
542 ip link
set ${destination} up
546 device_set_master
() {
553 if ! cmd ip link
set "${device}" master
"${master}"; then
554 log ERROR
"Could not set master ${master} for device ${device}"
561 device_remove_master
() {
565 if ! cmd ip link
set "${device}" nomaster
; then
566 log ERROR
"Could not remove master for device ${device}"
579 # Do nothing if device is already up
580 device_is_up
${device} && return ${EXIT_OK}
582 log INFO
"Bringing up ${device}"
584 device_set_parent_up
${device}
585 if ! cmd ip link
set ${device} up
; then
590 if interrupt_use_smp_affinity
; then
591 device_auto_configure_smp_affinity
${device}
597 device_set_parent_up
() {
601 if device_is_vlan
${device}; then
602 parent
=$
(vlan_get_parent
${device})
604 device_is_up
${parent} && return ${EXIT_OK}
606 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
608 device_set_up
${parent}
622 if device_is_up
${device}; then
623 log INFO
"Bringing down ${device}"
625 cmd ip link
set ${device} down
629 device_set_parent_down
${device}
634 device_set_parent_down
() {
638 if device_is_vlan
${device}; then
639 parent
=$
(vlan_get_parent
${device})
641 device_is_up
${parent} ||
return ${EXIT_OK}
643 if device_is_free
${parent}; then
644 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
646 device_set_down
${parent}
656 # Return an error if the device does not exist
657 device_exists
${device} ||
return ${EXIT_ERROR}
659 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
662 # Set mtu to a device
667 assert device_exists
${device}
669 # Handle bridges differently
670 if device_is_bridge
${device}; then
672 for port
in $
(bridge_get_members
${device}); do
673 device_set_mtu
${port} ${mtu}
677 log INFO
"Setting MTU of ${device} to ${mtu}"
680 if device_is_up
${device}; then
681 device_set_down
${device}
686 if ! cmd ip link
set ${device} mtu
${mtu}; then
689 log ERROR
"Could not set MTU ${mtu} on ${device}"
692 if [ "${up}" = "1" ]; then
693 device_set_up
${device}
699 device_adjust_mtu
() {
703 local other_device
="${2}"
705 local mtu
="$(device_get_mtu "${other_device}")"
706 device_set_mtu
"${device}" "${mtu}"
712 log INFO
"Running discovery process on device '${device}'."
715 for hook
in $
(hook_zone_get_all
); do
716 hook_zone_exec
${hook} discover
${device}
725 # Flash for ten seconds by default
729 local background
="false"
738 seconds
="$(cli_get_val "${arg}")"
741 done <<< "$(args "$@
")"
743 assert isinteger seconds
745 if ! device_exists
"${device}"; then
746 log ERROR
"Cannot identify device ${device}: Does not exist"
750 if ! device_is_ethernet
"${device}"; then
751 log DEBUG
"Cannot identify device ${device}: Not an ethernet device"
752 return ${EXIT_NOT_SUPPORTED}
755 log DEBUG
"Identifying device ${device}"
757 local command="ethtool --identify ${device} ${seconds}"
760 if enabled background
; then
761 cmd_background
"${command}"
763 cmd_quiet
"${command}"
775 assert device_exists
${device}
777 # IPv6 addresses must be fully imploded
778 local protocol
=$
(ip_detect_protocol
${addr})
779 case "${protocol}" in
781 addr
=$
(ipv6_format
"${addr}")
785 list_match
${addr} $
(device_get_addresses
${device})
788 device_get_addresses
() {
791 assert device_exists
${device}
796 ip addr show
${device} | \
797 while read prot addr line
; do
798 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
802 __device_get_file
() {
806 fread
"${SYS_CLASS_NET}/${device}/${file}"
809 __device_set_file
() {
816 fappend
"${SYS_CLASS_NET}/${device}/${file}" "${value}"
819 device_get_rx_bytes
() {
822 __device_get_file
${device} statistics
/rx_bytes
825 device_get_tx_bytes
() {
828 __device_get_file
${device} statistics
/tx_bytes
831 device_get_rx_packets
() {
834 __device_get_file
${device} statistics
/rx_packets
837 device_get_tx_packets
() {
840 __device_get_file
${device} statistics
/tx_packets
843 device_get_rx_errors
() {
846 __device_get_file
${device} statistics
/rx_errors
849 device_get_tx_errors
() {
852 __device_get_file
${device} statistics
/tx_errors
858 local speed
=$
(__device_get_file
${device} speed
)
860 # Exit for no output (i.e. no link detected)
861 isset speed ||
return ${EXIT_ERROR}
863 # Don't return anything for negative values
864 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
869 device_get_duplex
() {
872 local duplex
=$
(__device_get_file
${device} duplex
)
884 device_get_link_string
() {
890 local speed
="$(device_get_speed "${device}")"
892 list_append s
"${speed} MBit/s"
895 local duplex
="$(device_get_duplex "${device}")"
896 if isset duplex
; then
897 list_append s
"${duplex} duplex"
903 device_auto_configure_smp_affinity
() {
908 if lock_acquire
"smp-affinity" 60; then
909 device_set_smp_affinity
${device} auto
911 lock_release
"smp-affinity"
915 device_set_smp_affinity
() {
921 # mode can be auto which will automatically try to find
922 # the least busy processor, or an integer for the desired
923 # processor that should handle this device
925 local num_processors
=$
(system_get_processors
)
927 if [ "${mode}" = "auto" ]; then
928 local processor
=$
(interrupt_choose_least_busy_processor
)
930 assert isinteger mode
931 local processor
=${mode}
933 if [ ${processor} -gt ${num_processors} ]; then
934 log ERROR
"Processor ${processor} does not exist"
939 local interrupts
=$
(interrupts_for_device
${device})
940 if ! isset interrupts
; then
941 log DEBUG
"${device} has no interrupts. Not changing SMP affinity"
947 for interrupt
in ${interrupts}; do
948 interrupt_set_smp_affinity
${interrupt} ${processor}
951 # Find all queues and assign them to the next processor
953 for queue
in $
(device_get_queues
${device}); do
955 # Only handle receive queues
957 for interrupt
in $
(interrupts_for_device_queue
${device} ${queue}); do
958 interrupt_set_smp_affinity
${interrupt} ${processor}
961 device_queue_set_smp_affinity
${device} ${queue} ${processor}
970 # Get the next available processor if in auto mode
971 [ "${mode}" = "auto" ] && processor
=$
(system_get_next_processor
${processor})
977 device_get_queues
() {
982 list_directory
"${SYS_CLASS_NET}/${device}/queues"
985 device_supports_multiqueue
() {
988 local num_queues
=$
(device_num_queues
${device})
990 if isset num_queues
&& [ ${num_queues} -gt 2 ]; then
997 device_num_queues
() {
1001 isset
type && assert isoneof
type rx tx
1006 for q
in $
(device_get_queues
${device}); do
1007 case "${type},${q}" in
1023 device_queue_get_smp_affinity
() {
1029 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}"
1033 path
="${path}/rps_cpus"
1036 path
="${path}/xps_cpus"
1039 assert
[ -r "${path}" ]
1041 __bitmap_to_processor_ids $
(<${path})
1044 device_queue_set_smp_affinity
() {
1049 local processor
=${3}
1051 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1052 assert
[ -w "${path}" ]
1054 log DEBUG
"Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1056 __processor_id_to_bitmap
${processor} > ${path}