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 ###############################################################################
22 declare -A DEVICE_LINK_SPEEDS
=(
29 [10000BaseT-Full
]=0x1000
37 for device
in $
(list_directory
${SYS_CLASS_NET}); do
38 if device_exists
"${device}"; then
44 list_append devices $
(phy_list
)
46 # Add all serial devices
47 list_append devices $
(serial_list
)
49 # Return a sorted result
53 # Check if the device exists
57 # If device name was not found, exit.
58 [ -n "${device}" ] ||
return ${EXIT_ERROR}
60 # Check for a normal network device.
61 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
63 # If the check above did not find a result,
65 phy_exists "${device}" && return ${EXIT_OK}
67 # If the check above did not find a result,
68 # we check for serial devices.
69 serial_exists ${device}
72 device_matches_pattern() {
79 pattern="^
${pattern//N/[[:digit:]]+}$
"
81 [[ ${device} =~ ${pattern} ]] \
82 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
89 # Nothing to do, it device does not exist.
90 device_exists ${device} || return ${EXIT_OK}
92 # Shut down device before we delete it
93 device_set_down "${device}"
96 cmd_quiet ip link delete ${device}
99 if [ ${ret} -ne ${EXIT_OK} ]; then
100 log ERROR "device
: Could not delete device
'${device}': ${ret}"
111 local flags=$(__device_get_file ${device} flags)
113 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
120 # Check if the device is up
124 device_exists ${device} || return ${EXIT_ERROR}
126 device_has_flag ${device} 0x1
129 device_ifindex_to_name() {
133 local device device_idx
134 for device in $(list_directory "${SYS_CLASS_NET}"); do
135 device_idx=$(device_get_ifindex ${device})
137 if [ "${device_idx}" = "${idx}" ]; then
146 device_get_ifindex() {
150 local path="${SYS_CLASS_NET}/${1}/ifindex
"
152 # Check if file can be read.
153 [ -r "${path}" ] || return ${EXIT_ERROR}
158 # Check if the device is a bonding device
159 device_is_bonding() {
160 [ -d "/sys
/class
/net
/${1}/bonding
" ]
163 # Check if the device bonded in a bonding device
167 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
170 # Check if the device is a bridge
172 [ -d "/sys
/class
/net
/${1}/bridge
" ]
175 device_is_bridge_attached() {
177 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
180 device_is_wireless_monitor() {
184 device_is_wireless "${device}" && \
185 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
188 device_is_wireless_adhoc() {
192 device_is_wireless "${device}" && \
193 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
196 device_get_bridge() {
200 # Check if device is attached to a bridge.
201 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
203 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
204 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
206 local ifindex=$(<${ifindex_path})
209 device_ifindex_to_name ${ifindex}
212 # Check if the device is a vlan device
217 [ -e "${PROC_NET_VLAN}/${device}" ]
220 # Check if the device has vlan devices
225 if device_is_vlan ${device}; then
229 local vlans=$(device_get_vlans ${device})
230 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
237 # If no 8021q module has been loaded into the kernel,
238 # we cannot do anything.
239 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
241 local dev spacer1 id spacer2 parent
242 while read dev spacer1 id spacer2 parent
; do
243 [ "${parent}" = "${device}" ] ||
continue
246 done < ${PROC_NET_VLAN_CONFIG}
249 # Check if the device is a ppp device
253 local type=$
(__device_get_file
${device} type)
255 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
258 # Check if the device is a pointopoint device.
262 device_has_flag ${device} 0x10
265 # Check if the device is a loopback device
266 device_is_loopback() {
269 [ "${device}" = "lo
" ]
272 # Check if the device is a dummy device
273 # This is the worst possible check, but all I could come up with
277 [[ ${device} =~ ^dummy[0-9]+$ ]]
283 [[ ${device} =~ ^ipsec\- ]]
286 # Check if the device is a wireless device
287 device_is_wireless() {
290 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
296 local type=$(__device_get_file ${device} type)
298 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
304 local type=$
(__device_get_file
${device} type)
306 [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
312 if device_is_wireless "${device}"; then
313 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
328 # Returns true if a device is a tun device
332 [ -e "${SYS_CLASS_NET}/${device}/tun_flags
" ]
335 # Check if the device is a physical network interface
336 device_is_ethernet() {
339 device_is_ethernet_compatible "${device}" || \
342 device_is_loopback ${device} && \
345 device_is_bonding ${device} && \
348 device_is_bridge ${device} && \
351 device_is_ppp ${device} && \
354 device_is_vlan ${device} && \
357 device_is_dummy ${device} && \
360 device_is_tun ${device} && \
366 # Get the device type
370 # If the device does not exist (happens on udev remove events),
371 # we do not bother to run all checks.
372 if ! device_exists "${device}"; then
375 elif device_is_vlan ${device}; then
378 elif device_is_bonding ${device}; then
381 elif device_is_bridge ${device}; then
384 elif device_is_ppp ${device}; then
387 elif device_is_loopback ${device}; then
390 elif device_is_wireless_adhoc ${device}; then
391 echo "wireless-adhoc
"
393 elif device_is_wireless ${device}; then
396 elif device_is_dummy ${device}; then
399 elif device_is_tun ${device}; then
402 elif device_is_ethernet ${device}; then
405 elif device_is_serial ${device}; then
408 elif device_is_phy ${device}; then
412 echo "$
(device_tunnel_get_type
"${device}")"
416 # This function just checks the types a ip-tunnel device usually have
417 # so when we know that the device is an ip-tunnel device we save time
418 device_tunnel_get_type() {
421 # If the device does not exist (happens on udev remove events),
422 # we do not bother to run all checks.
423 if ! device_exists "${device}"; then
426 elif device_is_vti ${device}; then
429 elif device_is_vti6 ${device}; then
437 device_is_ethernet_compatible() {
440 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
441 local type="$
(__device_get_file
"${device}" "type")"
442 [[ "${type}" = "1" ]]
445 device_get_status() {
449 local status=${STATUS_DOWN}
451 if device_is_up ${device}; then
454 if ! device_has_carrier ${device}; then
455 status=${STATUS_NOCARRIER}
462 device_get_address() {
465 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
468 device_set_address() {
474 if ! device_exists "${device}"; then
475 error "Device
'${device}' does not exist.
"
479 # Do nothing if the address has not changed
480 local old_addr="$
(device_get_address
"${device}")"
481 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
485 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
488 if device_is_up "${device}"; then
489 device_set_down "${device}"
493 ip link set "${device}" address "${addr}"
496 if [ "${up}" = "1" ]; then
497 device_set_up "${device}"
500 if [ "${ret}" != "0" ]; then
501 error_log "Could not
set address
'${addr}' on device
'${device}'"
509 for device in $(list_directory "${SYS_CLASS_NET}"); do
510 # bonding_masters is no device
511 [ "${device}" = "bonding_masters
" ] && continue
519 # Check if a device has a cable plugged in
520 device_has_carrier() {
524 local carrier=$(__device_get_file ${device} carrier)
525 [ "${carrier}" = "1" ]
528 device_is_promisc() {
531 device_has_flag ${device} 0x200
534 device_set_promisc() {
538 assert device_exists ${device}
540 assert isoneof state on off
542 ip link set ${device} promisc ${state}
545 # Check if the device is free
547 ! device_is_used "$@
"
550 # Check if the device is used
554 device_has_vlans ${device} && \
556 device_is_bonded ${device} && \
558 device_is_bridge_attached ${device} && \
564 # Give the device a new name
567 local destination=${2}
569 # Check if devices exists
570 if ! device_exists ${source} || device_exists ${destination}; then
575 if device_is_up ${source}; then
576 ip link set ${source} down
580 ip link set ${source} name ${destination}
582 if [ "${up}" = "1" ]; then
583 ip link set ${destination} up
587 device_set_master() {
594 if ! cmd ip link set "${device}" master "${master}"; then
595 log ERROR "Could not
set master
${master} for device
${device}"
602 device_remove_master() {
606 if ! cmd ip link set "${device}" nomaster; then
607 log ERROR "Could not remove master
for device
${device}"
620 # Do nothing if device is already up
621 device_is_up ${device} && return ${EXIT_OK}
623 log INFO "Bringing up
${device}"
625 device_set_parent_up ${device}
626 if ! cmd ip link set ${device} up; then
631 if interrupt_use_smp_affinity; then
632 device_auto_configure_smp_affinity ${device}
638 device_set_parent_up() {
642 if device_is_vlan ${device}; then
643 parent=$(vlan_get_parent ${device})
645 device_is_up ${parent} && return ${EXIT_OK}
647 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
649 device_set_up ${parent}
663 if device_is_up ${device}; then
664 log INFO "Bringing down
${device}"
666 cmd ip link set ${device} down
670 device_set_parent_down ${device}
675 device_set_parent_down() {
679 if device_is_vlan ${device}; then
680 parent=$(vlan_get_parent ${device})
682 device_is_up ${parent} || return ${EXIT_OK}
684 if device_is_free ${parent}; then
685 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
687 device_set_down ${parent}
697 # Return an error if the device does not exist
698 device_exists ${device} || return ${EXIT_ERROR}
700 echo $(<${SYS_CLASS_NET}/${device}/mtu)
703 # Set mtu to a device
708 assert device_exists ${device}
710 # Handle bridges differently
711 if device_is_bridge ${device}; then
713 for port in $(bridge_get_members ${device}); do
714 device_set_mtu ${port} ${mtu}
718 log INFO "Setting MTU of
${device} to
${mtu}"
721 if device_is_up ${device}; then
722 device_set_down ${device}
727 if ! cmd ip link set ${device} mtu ${mtu}; then
730 log ERROR "Could not
set MTU
${mtu} on
${device}"
733 if [ "${up}" = "1" ]; then
734 device_set_up ${device}
740 device_adjust_mtu() {
744 local other_device="${2}"
746 local mtu="$
(device_get_mtu
"${other_device}")"
747 device_set_mtu "${device}" "${mtu}"
753 log INFO "Running discovery process on device
'${device}'.
"
756 for hook in $(hook_zone_get_all); do
757 hook_zone_exec ${hook} discover ${device}
766 # Flash for ten seconds by default
770 local background="false
"
779 seconds="$
(cli_get_val
"${arg}")"
782 done <<< "$
(args
"$@")"
784 assert isinteger seconds
786 if ! device_exists "${device}"; then
787 log ERROR "Cannot identify device
${device}: Does not exist
"
791 if ! device_is_ethernet "${device}"; then
792 log DEBUG "Cannot identify device
${device}: Not an ethernet device
"
793 return ${EXIT_NOT_SUPPORTED}
796 log DEBUG "Identifying device
${device}"
798 local command="ethtool
--identify ${device} ${seconds}"
801 if enabled background; then
802 cmd_background "${command}"
804 cmd_quiet "${command}"
816 assert device_exists ${device}
818 # IPv6 addresses must be fully imploded
819 local protocol=$(ip_detect_protocol ${addr})
820 case "${protocol}" in
822 addr=$(ipv6_format "${addr}")
826 list_match ${addr} $(device_get_addresses ${device})
829 device_get_addresses() {
832 assert device_exists ${device}
837 ip addr show ${device} | \
838 while read prot addr line; do
839 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
843 __device_get_file() {
847 fread "${SYS_CLASS_NET}/${device}/${file}"
850 __device_set_file() {
857 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
860 device_get_rx_bytes() {
863 __device_get_file ${device} statistics/rx_bytes
866 device_get_tx_bytes() {
869 __device_get_file ${device} statistics/tx_bytes
872 device_get_rx_packets() {
875 __device_get_file ${device} statistics/rx_packets
878 device_get_tx_packets() {
881 __device_get_file ${device} statistics/tx_packets
884 device_get_rx_errors() {
887 __device_get_file ${device} statistics/rx_errors
890 device_get_tx_errors() {
893 __device_get_file ${device} statistics/tx_errors
896 device_advertise_link_speeds() {
902 # Advertised modes in hex
907 local m="${DEVICE_LINK_SPEEDS[${mode}]}"
909 advertise="$
(( advertise | m
))"
913 # If nothing was selected, we reset and enable everything
914 if [ ${advertise} -eq 0 ]; then
918 # Enable auto-negotiation
919 cmd_quiet ethtool --change "${device}" autoneg on
921 # Set advertised link speeds
922 if ! cmd_quiet ethtool --change "${device}" advertise "0x$
(hex
"${advertise}")"; then
923 log ERROR "Could not
set link modes of
${device}: $@
"
927 log DEBUG "Set device link modes of
${device} to $@
"
934 local speed=$(__device_get_file ${device} speed)
936 # Exit for no output (i.e. no link detected)
937 isset speed || return ${EXIT_ERROR}
939 # Don't return anything for negative values
940 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
945 device_get_duplex() {
948 local duplex=$(__device_get_file ${device} duplex)
960 device_get_link_string() {
966 local speed="$
(device_get_speed
"${device}")"
968 list_append s "${speed} MBit
/s
"
971 local duplex="$
(device_get_duplex
"${device}")"
972 if isset duplex; then
973 list_append s "${duplex} duplex
"
979 device_auto_configure_smp_affinity() {
984 if lock_acquire "smp-affinity
" 60; then
985 device_set_smp_affinity ${device} auto
987 lock_release "smp-affinity
"
991 device_set_smp_affinity() {
997 # mode can be auto which will automatically try to find
998 # the least busy processor, or an integer for the desired
999 # processor that should handle this device
1001 local num_processors=$(system_get_processors)
1003 if [ "${mode}" = "auto
" ]; then
1004 local processor=$(interrupt_choose_least_busy_processor)
1006 assert isinteger mode
1007 local processor=${mode}
1009 if [ ${processor} -gt ${num_processors} ]; then
1010 log ERROR "Processor
${processor} does not exist
"
1011 return ${EXIT_ERROR}
1015 local interrupts=$(interrupts_for_device ${device})
1016 if ! isset interrupts; then
1017 log DEBUG "${device} has no interrupts. Not changing SMP affinity
"
1023 for interrupt in ${interrupts}; do
1024 interrupt_set_smp_affinity ${interrupt} ${processor}
1027 # Find all queues and assign them to the next processor
1029 for queue in $(device_get_queues ${device}); do
1031 # Only handle receive queues
1033 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1034 interrupt_set_smp_affinity ${interrupt} ${processor}
1037 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1046 # Get the next available processor if in auto mode
1047 [ "${mode}" = "auto
" ] && processor=$(system_get_next_processor ${processor})
1053 device_get_queues() {
1058 list_directory "${SYS_CLASS_NET}/${device}/queues
"
1061 device_supports_multiqueue() {
1064 local num_queues=$(device_num_queues ${device})
1066 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1070 return ${EXIT_FALSE}
1073 device_num_queues() {
1077 isset type && assert isoneof type rx tx
1082 for q in $(device_get_queues ${device}); do
1083 case "${type},${q}" in
1099 device_queue_get_smp_affinity() {
1105 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1109 path="${path}/rps_cpus
"
1112 path="${path}/xps_cpus
"
1115 assert [ -r "${path}" ]
1117 __bitmap_to_processor_ids $(<${path})
1120 device_queue_set_smp_affinity() {
1125 local processor=${3}
1127 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus
"
1128 assert [ -w "${path}" ]
1130 log DEBUG "Setting SMP affinity of
${device} (${queue}) to processor ${processor}"
1132 __processor_id_to_bitmap ${processor} > ${path}
1135 # Tries to find a device which has the given IP address assigned
1136 device_get_by_assigned_ip_address() {
1143 # Read the first line of ip addr show to
1144 read -r device <<< $(ip addr show to "${ip}")
1146 # If we did not found a device we return with ${EXIT_ERROR}
1147 if ! isset device; then
1148 return ${EXIT_ERROR}
1151 # We get something like:
1152 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1153 # and we want upl0 so we take the second word and removing the :
1162 device_get_by_mac_address() {
1169 for device in $(device_list); do
1170 if [ "${mac}" = "$
(device_get_address
${device})" ]; then
1176 # We could not found a port to the given mac address so we return exit error
1177 return ${EXIT_ERROR}