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}
80 # Shut down device before we delete it
81 device_set_down "${device}"
84 cmd_quiet ip link delete ${device}
87 if [ ${ret} -ne ${EXIT_OK} ]; then
88 log ERROR "device
: Could not delete device
'${device}': ${ret}"
99 local flags=$(__device_get_file ${device} flags)
101 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
108 # Check if the device is up
112 device_exists ${device} || return ${EXIT_ERROR}
114 device_has_flag ${device} 0x1
117 device_ifindex_to_name() {
121 local device device_idx
122 for device in $(list_directory "${SYS_CLASS_NET}"); do
123 device_idx=$(device_get_ifindex ${device})
125 if [ "${device_idx}" = "${idx}" ]; then
134 device_get_ifindex() {
138 local path="${SYS_CLASS_NET}/${1}/ifindex
"
140 # Check if file can be read.
141 [ -r "${path}" ] || return ${EXIT_ERROR}
146 # Check if the device is a bonding device
147 device_is_bonding() {
148 [ -d "/sys
/class
/net
/${1}/bonding
" ]
151 # Check if the device bonded in a bonding device
155 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
158 # Check if the device is a bridge
160 [ -d "/sys
/class
/net
/${1}/bridge
" ]
163 device_is_bridge_attached() {
165 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
168 device_is_wireless_monitor() {
172 device_is_wireless "${device}" && \
173 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
176 device_is_wireless_adhoc() {
180 device_is_wireless "${device}" && \
181 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
184 device_get_bridge() {
188 # Check if device is attached to a bridge.
189 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
191 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
192 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
194 local ifindex=$(<${ifindex_path})
197 device_ifindex_to_name ${ifindex}
200 # Check if the device is a vlan device
205 [ -e "${PROC_NET_VLAN}/${device}" ]
208 # Check if the device has vlan devices
213 if device_is_vlan ${device}; then
217 local vlans=$(device_get_vlans ${device})
218 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
225 # If no 8021q module has been loaded into the kernel,
226 # we cannot do anything.
227 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
229 local dev spacer1 id spacer2 parent
230 while read dev spacer1 id spacer2 parent
; do
231 [ "${parent}" = "${device}" ] ||
continue
234 done < ${PROC_NET_VLAN_CONFIG}
237 # Check if the device is a ppp device
241 local type=$
(__device_get_file
${device} type)
243 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
246 # Check if the device is a pointopoint device.
250 device_has_flag ${device} 0x10
253 # Check if the device is a loopback device
254 device_is_loopback() {
257 [ "${device}" = "lo
" ]
260 # Check if the device is a dummy device
261 # This is the worst possible check, but all I could come up with
265 [[ ${device} =~ ^dummy[0-9]+$ ]]
271 [[ ${device} =~ ^ipsec\- ]]
274 # Check if the device is a wireless device
275 device_is_wireless() {
278 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
284 local type=$(__device_get_file ${device} type)
286 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
292 if device_is_wireless
"${device}"; then
293 print
"$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
308 # Returns true if a device is a tun device
312 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
315 # Check if the device is a physical network interface
316 device_is_ethernet
() {
319 device_is_ethernet_compatible
"${device}" || \
322 device_is_loopback
${device} && \
325 device_is_bonding
${device} && \
328 device_is_bridge
${device} && \
331 device_is_ppp
${device} && \
334 device_is_vlan
${device} && \
337 device_is_dummy
${device} && \
340 device_is_tun
${device} && \
346 # Get the device type
350 # If the device does not exist (happens on udev remove events),
351 # we do not bother to run all checks.
352 if ! device_exists
"${device}"; then
355 elif device_is_vlan
${device}; then
358 elif device_is_bonding
${device}; then
361 elif device_is_bridge
${device}; then
364 elif device_is_ppp
${device}; then
367 elif device_is_loopback
${device}; then
370 elif device_is_wireless_adhoc
${device}; then
371 echo "wireless-adhoc"
373 elif device_is_wireless
${device}; then
376 elif device_is_dummy
${device}; then
379 elif device_is_tun
${device}; then
382 elif device_is_ethernet
${device}; then
385 elif device_is_serial
${device}; then
388 elif device_is_phy
${device}; then
391 elif device_is_vti
${device}; then
399 device_is_ethernet_compatible
() {
402 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
403 local type="$(__device_get_file "${device}" "type")"
404 [[ "${type}" = "1" ]]
407 device_get_status
() {
411 local status
=${STATUS_DOWN}
413 if device_is_up
${device}; then
416 if ! device_has_carrier
${device}; then
417 status
=${STATUS_NOCARRIER}
424 device_get_address
() {
427 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
430 device_set_address
() {
436 if ! device_exists
"${device}"; then
437 error
"Device '${device}' does not exist."
441 # Do nothing if the address has not changed
442 local old_addr
="$(device_get_address "${device}")"
443 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
447 log DEBUG
"Setting address of '${device}' from '${old_addr}' to '${addr}'"
450 if device_is_up
"${device}"; then
451 device_set_down
"${device}"
455 ip link
set "${device}" address
"${addr}"
458 if [ "${up}" = "1" ]; then
459 device_set_up
"${device}"
462 if [ "${ret}" != "0" ]; then
463 error_log
"Could not set address '${addr}' on device '${device}'"
471 for device
in $
(list_directory
"${SYS_CLASS_NET}"); do
472 # bonding_masters is no device
473 [ "${device}" = "bonding_masters" ] && continue
481 # Check if a device has a cable plugged in
482 device_has_carrier
() {
486 local carrier
=$
(__device_get_file
${device} carrier
)
487 [ "${carrier}" = "1" ]
490 device_is_promisc
() {
493 device_has_flag
${device} 0x200
496 device_set_promisc
() {
500 assert device_exists
${device}
502 assert isoneof state on off
504 ip link
set ${device} promisc
${state}
507 # Check if the device is free
509 ! device_is_used
"$@"
512 # Check if the device is used
516 device_has_vlans
${device} && \
518 device_is_bonded
${device} && \
520 device_is_bridge_attached
${device} && \
526 # Give the device a new name
529 local destination
=${2}
531 # Check if devices exists
532 if ! device_exists
${source} || device_exists
${destination}; then
537 if device_is_up
${source}; then
538 ip link
set ${source} down
542 ip link
set ${source} name
${destination}
544 if [ "${up}" = "1" ]; then
545 ip link
set ${destination} up
549 device_set_master
() {
556 if ! cmd ip link
set "${device}" master
"${master}"; then
557 log ERROR
"Could not set master ${master} for device ${device}"
564 device_remove_master
() {
568 if ! cmd ip link
set "${device}" nomaster
; then
569 log ERROR
"Could not remove master for device ${device}"
582 # Do nothing if device is already up
583 device_is_up
${device} && return ${EXIT_OK}
585 log INFO
"Bringing up ${device}"
587 device_set_parent_up
${device}
588 if ! cmd ip link
set ${device} up
; then
593 if interrupt_use_smp_affinity
; then
594 device_auto_configure_smp_affinity
${device}
600 device_set_parent_up
() {
604 if device_is_vlan
${device}; then
605 parent
=$
(vlan_get_parent
${device})
607 device_is_up
${parent} && return ${EXIT_OK}
609 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
611 device_set_up
${parent}
625 if device_is_up
${device}; then
626 log INFO
"Bringing down ${device}"
628 cmd ip link
set ${device} down
632 device_set_parent_down
${device}
637 device_set_parent_down
() {
641 if device_is_vlan
${device}; then
642 parent
=$
(vlan_get_parent
${device})
644 device_is_up
${parent} ||
return ${EXIT_OK}
646 if device_is_free
${parent}; then
647 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
649 device_set_down
${parent}
659 # Return an error if the device does not exist
660 device_exists
${device} ||
return ${EXIT_ERROR}
662 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
665 # Set mtu to a device
670 assert device_exists
${device}
672 # Handle bridges differently
673 if device_is_bridge
${device}; then
675 for port
in $
(bridge_get_members
${device}); do
676 device_set_mtu
${port} ${mtu}
680 log INFO
"Setting MTU of ${device} to ${mtu}"
683 if device_is_up
${device}; then
684 device_set_down
${device}
689 if ! cmd ip link
set ${device} mtu
${mtu}; then
692 log ERROR
"Could not set MTU ${mtu} on ${device}"
695 if [ "${up}" = "1" ]; then
696 device_set_up
${device}
702 device_adjust_mtu
() {
706 local other_device
="${2}"
708 local mtu
="$(device_get_mtu "${other_device}")"
709 device_set_mtu
"${device}" "${mtu}"
715 log INFO
"Running discovery process on device '${device}'."
718 for hook
in $
(hook_zone_get_all
); do
719 hook_zone_exec
${hook} discover
${device}
728 # Flash for ten seconds by default
732 local background
="false"
741 seconds
="$(cli_get_val "${arg}")"
744 done <<< "$(args "$@
")"
746 assert isinteger seconds
748 if ! device_exists
"${device}"; then
749 log ERROR
"Cannot identify device ${device}: Does not exist"
753 if ! device_is_ethernet
"${device}"; then
754 log DEBUG
"Cannot identify device ${device}: Not an ethernet device"
755 return ${EXIT_NOT_SUPPORTED}
758 log DEBUG
"Identifying device ${device}"
760 local command="ethtool --identify ${device} ${seconds}"
763 if enabled background
; then
764 cmd_background
"${command}"
766 cmd_quiet
"${command}"
778 assert device_exists
${device}
780 # IPv6 addresses must be fully imploded
781 local protocol
=$
(ip_detect_protocol
${addr})
782 case "${protocol}" in
784 addr
=$
(ipv6_format
"${addr}")
788 list_match
${addr} $
(device_get_addresses
${device})
791 device_get_addresses
() {
794 assert device_exists
${device}
799 ip addr show
${device} | \
800 while read prot addr line
; do
801 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
805 __device_get_file
() {
809 fread
"${SYS_CLASS_NET}/${device}/${file}"
812 __device_set_file
() {
819 fappend
"${SYS_CLASS_NET}/${device}/${file}" "${value}"
822 device_get_rx_bytes
() {
825 __device_get_file
${device} statistics
/rx_bytes
828 device_get_tx_bytes
() {
831 __device_get_file
${device} statistics
/tx_bytes
834 device_get_rx_packets
() {
837 __device_get_file
${device} statistics
/rx_packets
840 device_get_tx_packets
() {
843 __device_get_file
${device} statistics
/tx_packets
846 device_get_rx_errors
() {
849 __device_get_file
${device} statistics
/rx_errors
852 device_get_tx_errors
() {
855 __device_get_file
${device} statistics
/tx_errors
861 local speed
=$
(__device_get_file
${device} speed
)
863 # Exit for no output (i.e. no link detected)
864 isset speed ||
return ${EXIT_ERROR}
866 # Don't return anything for negative values
867 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
872 device_get_duplex
() {
875 local duplex
=$
(__device_get_file
${device} duplex
)
887 device_get_link_string
() {
893 local speed
="$(device_get_speed "${device}")"
895 list_append s
"${speed} MBit/s"
898 local duplex
="$(device_get_duplex "${device}")"
899 if isset duplex
; then
900 list_append s
"${duplex} duplex"
906 device_auto_configure_smp_affinity
() {
911 if lock_acquire
"smp-affinity" 60; then
912 device_set_smp_affinity
${device} auto
914 lock_release
"smp-affinity"
918 device_set_smp_affinity
() {
924 # mode can be auto which will automatically try to find
925 # the least busy processor, or an integer for the desired
926 # processor that should handle this device
928 local num_processors
=$
(system_get_processors
)
930 if [ "${mode}" = "auto" ]; then
931 local processor
=$
(interrupt_choose_least_busy_processor
)
933 assert isinteger mode
934 local processor
=${mode}
936 if [ ${processor} -gt ${num_processors} ]; then
937 log ERROR
"Processor ${processor} does not exist"
942 local interrupts
=$
(interrupts_for_device
${device})
943 if ! isset interrupts
; then
944 log DEBUG
"${device} has no interrupts. Not changing SMP affinity"
950 for interrupt
in ${interrupts}; do
951 interrupt_set_smp_affinity
${interrupt} ${processor}
954 # Find all queues and assign them to the next processor
956 for queue
in $
(device_get_queues
${device}); do
958 # Only handle receive queues
960 for interrupt
in $
(interrupts_for_device_queue
${device} ${queue}); do
961 interrupt_set_smp_affinity
${interrupt} ${processor}
964 device_queue_set_smp_affinity
${device} ${queue} ${processor}
973 # Get the next available processor if in auto mode
974 [ "${mode}" = "auto" ] && processor
=$
(system_get_next_processor
${processor})
980 device_get_queues
() {
985 list_directory
"${SYS_CLASS_NET}/${device}/queues"
988 device_supports_multiqueue
() {
991 local num_queues
=$
(device_num_queues
${device})
993 if isset num_queues
&& [ ${num_queues} -gt 2 ]; then
1000 device_num_queues
() {
1004 isset
type && assert isoneof
type rx tx
1009 for q
in $
(device_get_queues
${device}); do
1010 case "${type},${q}" in
1026 device_queue_get_smp_affinity
() {
1032 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}"
1036 path
="${path}/rps_cpus"
1039 path
="${path}/xps_cpus"
1042 assert
[ -r "${path}" ]
1044 __bitmap_to_processor_ids $
(<${path})
1047 device_queue_set_smp_affinity
() {
1052 local processor
=${3}
1054 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1055 assert
[ -w "${path}" ]
1057 log DEBUG
"Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1059 __processor_id_to_bitmap
${processor} > ${path}
1062 # Tries to find a device which has the given IP address assigned
1063 device_get_by_assigned_ip_address
() {
1070 # Read the first line of ip addr show to
1071 read -r device
<<< $
(ip addr show to
"${ip}")
1073 # If we did not found a device we return with ${EXIT_ERROR}
1074 if ! isset device
; then
1075 return ${EXIT_ERROR}
1078 # We get something like:
1079 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1080 # and we want upl0 so we take the second word and removing the :