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 list_append_one devices
"${device}"
42 list_append devices $
(phy_list
)
44 # Add all serial devices
45 list_append devices $
(serial_list
)
47 # Return a sorted result
51 # Check if the device exists
55 # If device name was not found, exit.
56 [ -n "${device}" ] ||
return ${EXIT_ERROR}
58 # Check for a normal network device.
59 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
61 # If the check above did not find a result,
63 phy_exists "${device}" && return ${EXIT_OK}
65 # If the check above did not find a result,
66 # we check for serial devices.
67 serial_exists ${device}
70 device_matches_pattern() {
77 pattern="^
${pattern//N/[[:digit:]]+}$
"
79 [[ ${device} =~ ${pattern} ]] \
80 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
87 # Nothing to do, it device does not exist.
88 device_exists ${device} || return ${EXIT_OK}
90 # Shut down device before we delete it
91 device_set_down "${device}"
94 cmd_quiet ip link delete ${device}
97 if [ ${ret} -ne ${EXIT_OK} ]; then
98 log ERROR "device
: Could not delete device
'${device}': ${ret}"
109 local flags=$(__device_get_file ${device} flags)
111 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
118 # Check if the device is up
122 device_exists ${device} || return ${EXIT_ERROR}
124 device_has_flag ${device} 0x1
127 device_ifindex_to_name() {
131 local device device_idx
132 for device in $(list_directory "${SYS_CLASS_NET}"); do
133 device_idx=$(device_get_ifindex ${device})
135 if [ "${device_idx}" = "${idx}" ]; then
144 device_get_ifindex() {
148 local path="${SYS_CLASS_NET}/${1}/ifindex
"
150 # Check if file can be read.
151 [ -r "${path}" ] || return ${EXIT_ERROR}
156 # Check if the device is a bonding device
157 device_is_bonding() {
158 [ -d "/sys
/class
/net
/${1}/bonding
" ]
161 # Check if the device bonded in a bonding device
165 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
168 # Check if the device is a bridge
170 [ -d "/sys
/class
/net
/${1}/bridge
" ]
173 device_is_bridge_attached() {
175 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
178 device_is_wireless_monitor() {
182 device_is_wireless "${device}" && \
183 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
186 device_is_wireless_adhoc() {
190 device_is_wireless "${device}" && \
191 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
194 device_get_bridge() {
198 # Check if device is attached to a bridge.
199 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
201 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
202 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
204 local ifindex=$(<${ifindex_path})
207 device_ifindex_to_name ${ifindex}
210 # Check if the device is a vlan device
215 [ -e "${PROC_NET_VLAN}/${device}" ]
218 # Check if the device has vlan devices
223 if device_is_vlan ${device}; then
227 local vlans=$(device_get_vlans ${device})
228 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
235 # If no 8021q module has been loaded into the kernel,
236 # we cannot do anything.
237 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
239 local dev spacer1 id spacer2 parent
240 while read dev spacer1 id spacer2 parent
; do
241 [ "${parent}" = "${device}" ] ||
continue
244 done < ${PROC_NET_VLAN_CONFIG}
247 # Check if the device is a ppp device
251 local type=$
(__device_get_file
${device} type)
253 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
256 # Check if the device is a pointopoint device.
260 device_has_flag ${device} 0x10
263 # Check if the device is a loopback device
264 device_is_loopback() {
267 [ "${device}" = "lo
" ]
270 # Check if the device is a dummy device
271 # This is the worst possible check, but all I could come up with
275 [[ ${device} =~ ^dummy[0-9]+$ ]]
281 [[ ${device} =~ ^ipsec\- ]]
284 # Check if the device is a wireless device
285 device_is_wireless() {
288 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
294 local type=$(__device_get_file ${device} type)
296 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
302 local type=$
(__device_get_file
${device} type)
304 [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
310 if device_is_wireless "${device}"; then
311 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
326 # Returns true if a device is a tun device
330 [ -e "${SYS_CLASS_NET}/${device}/tun_flags
" ]
333 # Check if the device is a physical network interface
334 device_is_ethernet() {
337 device_is_ethernet_compatible "${device}" || \
340 device_is_loopback ${device} && \
343 device_is_bonding ${device} && \
346 device_is_bridge ${device} && \
349 device_is_ppp ${device} && \
352 device_is_vlan ${device} && \
355 device_is_dummy ${device} && \
358 device_is_tun ${device} && \
364 # Get the device type
368 # If the device does not exist (happens on udev remove events),
369 # we do not bother to run all checks.
370 if ! device_exists "${device}"; then
373 elif device_is_vlan ${device}; then
376 elif device_is_bonding ${device}; then
379 elif device_is_bridge ${device}; then
382 elif device_is_ppp ${device}; then
385 elif device_is_loopback ${device}; then
388 elif device_is_wireless_adhoc ${device}; then
389 echo "wireless-adhoc
"
391 elif device_is_wireless ${device}; then
394 elif device_is_dummy ${device}; then
397 elif device_is_tun ${device}; then
400 elif device_is_ethernet ${device}; then
403 elif device_is_serial ${device}; then
406 elif device_is_phy ${device}; then
410 echo "$
(device_tunnel_get_type
"${device}")"
414 # This function just checks the types a ip-tunnel device usually have
415 # so when we know that the device is an ip-tunnel device we save time
416 device_tunnel_get_type() {
419 # If the device does not exist (happens on udev remove events),
420 # we do not bother to run all checks.
421 if ! device_exists "${device}"; then
424 elif device_is_vti ${device}; then
427 elif device_is_vti6 ${device}; then
435 device_is_ethernet_compatible() {
438 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
439 local type="$
(__device_get_file
"${device}" "type")"
440 [[ "${type}" = "1" ]]
443 device_get_status() {
447 local status=${STATUS_DOWN}
449 if device_is_up ${device}; then
452 if ! device_has_carrier ${device}; then
453 status=${STATUS_NOCARRIER}
460 device_get_address() {
463 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
466 device_set_address() {
472 if ! device_exists "${device}"; then
473 error "Device
'${device}' does not exist.
"
477 # Do nothing if the address has not changed
478 local old_addr="$
(device_get_address
"${device}")"
479 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
483 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
486 if device_is_up "${device}"; then
487 device_set_down "${device}"
491 ip link set "${device}" address "${addr}"
494 if [ "${up}" = "1" ]; then
495 device_set_up "${device}"
498 if [ "${ret}" != "0" ]; then
499 error_log "Could not
set address
'${addr}' on device
'${device}'"
507 for device in $(list_directory "${SYS_CLASS_NET}"); do
508 # bonding_masters is no device
509 [ "${device}" = "bonding_masters
" ] && continue
517 # Check if a device has a cable plugged in
518 device_has_carrier() {
522 local carrier=$(__device_get_file ${device} carrier)
523 [ "${carrier}" = "1" ]
526 device_is_promisc() {
529 device_has_flag ${device} 0x200
532 device_set_promisc() {
536 assert device_exists ${device}
538 assert isoneof state on off
540 ip link set ${device} promisc ${state}
543 # Check if the device is free
545 ! device_is_used "$@
"
548 # Check if the device is used
552 device_has_vlans ${device} && \
554 device_is_bonded ${device} && \
556 device_is_bridge_attached ${device} && \
562 # Give the device a new name
565 local destination=${2}
567 # Check if devices exists
568 if ! device_exists ${source} || device_exists ${destination}; then
573 if device_is_up ${source}; then
574 ip link set ${source} down
578 ip link set ${source} name ${destination}
580 if [ "${up}" = "1" ]; then
581 ip link set ${destination} up
585 device_set_master() {
592 if ! cmd ip link set "${device}" master "${master}"; then
593 log ERROR "Could not
set master
${master} for device
${device}"
600 device_remove_master() {
604 if ! cmd ip link set "${device}" nomaster; then
605 log ERROR "Could not remove master
for device
${device}"
618 # Do nothing if device is already up
619 device_is_up ${device} && return ${EXIT_OK}
621 log INFO "Bringing up
${device}"
623 device_set_parent_up ${device}
624 if ! cmd ip link set ${device} up; then
629 if interrupt_use_smp_affinity; then
630 device_auto_configure_smp_affinity ${device}
636 device_set_parent_up() {
640 if device_is_vlan ${device}; then
641 parent=$(vlan_get_parent ${device})
643 device_is_up ${parent} && return ${EXIT_OK}
645 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
647 device_set_up ${parent}
661 if device_is_up ${device}; then
662 log INFO "Bringing down
${device}"
664 cmd ip link set ${device} down
668 device_set_parent_down ${device}
673 device_set_parent_down() {
677 if device_is_vlan ${device}; then
678 parent=$(vlan_get_parent ${device})
680 device_is_up ${parent} || return ${EXIT_OK}
682 if device_is_free ${parent}; then
683 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
685 device_set_down ${parent}
695 # Return an error if the device does not exist
696 device_exists ${device} || return ${EXIT_ERROR}
698 echo $(<${SYS_CLASS_NET}/${device}/mtu)
701 # Set mtu to a device
706 assert device_exists ${device}
708 # Handle bridges differently
709 if device_is_bridge ${device}; then
711 for port in $(bridge_get_members ${device}); do
712 device_set_mtu ${port} ${mtu}
716 log INFO "Setting MTU of
${device} to
${mtu}"
719 if device_is_up ${device}; then
720 device_set_down ${device}
725 if ! cmd ip link set ${device} mtu ${mtu}; then
728 log ERROR "Could not
set MTU
${mtu} on
${device}"
731 if [ "${up}" = "1" ]; then
732 device_set_up ${device}
738 device_adjust_mtu() {
742 local other_device="${2}"
744 local mtu="$
(device_get_mtu
"${other_device}")"
745 device_set_mtu "${device}" "${mtu}"
751 log INFO "Running discovery process on device
'${device}'.
"
754 for hook in $(hook_zone_get_all); do
755 hook_zone_exec ${hook} discover ${device}
764 # Flash for ten seconds by default
768 local background="false
"
777 seconds="$
(cli_get_val
"${arg}")"
780 done <<< "$
(args
"$@")"
782 assert isinteger seconds
784 if ! device_exists "${device}"; then
785 log ERROR "Cannot identify device
${device}: Does not exist
"
789 if ! device_is_ethernet "${device}"; then
790 log DEBUG "Cannot identify device
${device}: Not an ethernet device
"
791 return ${EXIT_NOT_SUPPORTED}
794 log DEBUG "Identifying device
${device}"
796 local command="ethtool
--identify ${device} ${seconds}"
799 if enabled background; then
800 cmd_background "${command}"
802 cmd_quiet "${command}"
814 assert device_exists ${device}
816 # IPv6 addresses must be fully imploded
817 local protocol=$(ip_detect_protocol ${addr})
818 case "${protocol}" in
820 addr=$(ipv6_format "${addr}")
824 list_match ${addr} $(device_get_addresses ${device})
827 device_get_addresses() {
830 assert device_exists ${device}
835 ip addr show ${device} | \
836 while read prot addr line; do
837 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
841 __device_get_file() {
845 fread "${SYS_CLASS_NET}/${device}/${file}"
848 __device_set_file() {
855 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
858 device_get_rx_bytes() {
861 __device_get_file ${device} statistics/rx_bytes
864 device_get_tx_bytes() {
867 __device_get_file ${device} statistics/tx_bytes
870 device_get_rx_packets() {
873 __device_get_file ${device} statistics/rx_packets
876 device_get_tx_packets() {
879 __device_get_file ${device} statistics/tx_packets
882 device_get_rx_errors() {
885 __device_get_file ${device} statistics/rx_errors
888 device_get_tx_errors() {
891 __device_get_file ${device} statistics/tx_errors
894 device_advertise_link_speeds() {
900 # Advertised modes in hex
905 local m="${DEVICE_LINK_SPEEDS[${mode}]}"
907 advertise="$
(( advertise | m
))"
911 # If nothing was selected, we reset and enable everything
912 if [ ${advertise} -eq 0 ]; then
916 # Enable auto-negotiation
917 cmd_quiet ethtool --change "${device}" autoneg on
919 # Set advertised link speeds
920 if ! cmd_quiet ethtool --change "${device}" advertise "0x$
(hex
"${advertise}")"; then
921 log ERROR "Could not
set link modes of
${device}: $@
"
925 log DEBUG "Set device link modes of
${device} to $@
"
932 local speed=$(__device_get_file ${device} speed)
934 # Exit for no output (i.e. no link detected)
935 isset speed || return ${EXIT_ERROR}
937 # Don't return anything for negative values
938 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
943 device_get_duplex() {
946 local duplex=$(__device_get_file ${device} duplex)
958 device_get_link_string() {
964 local speed="$
(device_get_speed
"${device}")"
966 list_append s "${speed} MBit
/s
"
969 local duplex="$
(device_get_duplex
"${device}")"
970 if isset duplex; then
971 list_append s "${duplex} duplex
"
977 device_auto_configure_smp_affinity() {
982 if lock_acquire "smp-affinity
" 60; then
983 device_set_smp_affinity ${device} auto
985 lock_release "smp-affinity
"
989 device_set_smp_affinity() {
995 # mode can be auto which will automatically try to find
996 # the least busy processor, or an integer for the desired
997 # processor that should handle this device
999 local num_processors=$(system_get_processors)
1001 if [ "${mode}" = "auto
" ]; then
1002 local processor=$(interrupt_choose_least_busy_processor)
1004 assert isinteger mode
1005 local processor=${mode}
1007 if [ ${processor} -gt ${num_processors} ]; then
1008 log ERROR "Processor
${processor} does not exist
"
1009 return ${EXIT_ERROR}
1013 local interrupts=$(interrupts_for_device ${device})
1014 if ! isset interrupts; then
1015 log DEBUG "${device} has no interrupts. Not changing SMP affinity
"
1021 for interrupt in ${interrupts}; do
1022 interrupt_set_smp_affinity ${interrupt} ${processor}
1025 # Find all queues and assign them to the next processor
1027 for queue in $(device_get_queues ${device}); do
1029 # Only handle receive queues
1031 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1032 interrupt_set_smp_affinity ${interrupt} ${processor}
1035 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1044 # Get the next available processor if in auto mode
1045 [ "${mode}" = "auto
" ] && processor=$(system_get_next_processor ${processor})
1051 device_get_queues() {
1056 list_directory "${SYS_CLASS_NET}/${device}/queues
"
1059 device_supports_multiqueue() {
1062 local num_queues=$(device_num_queues ${device})
1064 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1068 return ${EXIT_FALSE}
1071 device_num_queues() {
1075 isset type && assert isoneof type rx tx
1080 for q in $(device_get_queues ${device}); do
1081 case "${type},${q}" in
1097 device_queue_get_smp_affinity() {
1103 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1107 path="${path}/rps_cpus
"
1110 path="${path}/xps_cpus
"
1113 assert [ -r "${path}" ]
1115 __bitmap_to_processor_ids $(<${path})
1118 device_queue_set_smp_affinity() {
1123 local processor=${3}
1125 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus
"
1126 assert [ -w "${path}" ]
1128 log DEBUG "Setting SMP affinity of
${device} (${queue}) to processor ${processor}"
1130 __processor_id_to_bitmap ${processor} > ${path}
1133 # Tries to find a device which has the given IP address assigned
1134 device_get_by_assigned_ip_address() {
1141 # Read the first line of ip addr show to
1142 read -r device <<< $(ip addr show to "${ip}")
1144 # If we did not found a device we return with ${EXIT_ERROR}
1145 if ! isset device; then
1146 return ${EXIT_ERROR}
1149 # We get something like:
1150 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1151 # and we want upl0 so we take the second word and removing the :
1160 device_get_by_mac_address() {
1167 for device in $(device_list); do
1168 if [ "${mac}" = "$
(device_get_address
${device})" ]; then
1174 # We could not found a port to the given mac address so we return exit error
1175 return ${EXIT_ERROR}