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 # Check if the device is a physical network interface
317 device_is_ethernet() {
320 device_is_ethernet_compatible "${device}" || \
323 device_is_loopback ${device} && \
326 device_is_bonding ${device} && \
329 device_is_bridge ${device} && \
332 device_is_ppp ${device} && \
335 device_is_vlan ${device} && \
338 device_is_dummy ${device} && \
344 # Get the device type
348 # If the device does not exist (happens on udev remove events),
349 # we do not bother to run all checks.
350 if ! device_exists "${device}"; then
353 elif device_is_vlan ${device}; then
356 elif device_is_bonding ${device}; then
359 elif device_is_bridge ${device}; then
362 elif device_is_ppp ${device}; then
365 elif device_is_batman_adv ${device}; then
368 elif device_is_loopback ${device}; then
371 elif device_is_wireless_adhoc ${device}; then
372 echo "wireless-adhoc
"
374 elif device_is_wireless ${device}; then
377 elif device_is_dummy ${device}; then
380 elif device_is_ethernet ${device}; then
383 elif device_is_serial ${device}; then
386 elif device_is_phy ${device}; then
394 device_is_ethernet_compatible() {
397 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
398 local type="$
(__device_get_file
"${device}" "type")"
399 [[ "${type}" = "1" ]]
402 device_get_status() {
406 local status=${STATUS_DOWN}
408 if device_is_up ${device}; then
411 if ! device_has_carrier ${device}; then
412 status=${STATUS_NOCARRIER}
419 device_get_address() {
422 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
425 device_set_address() {
431 if ! device_exists "${device}"; then
432 error "Device
'${device}' does not exist.
"
436 # Do nothing if the address has not changed
437 local old_addr="$
(device_get_address
"${device}")"
438 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
442 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
445 if device_is_up "${device}"; then
446 device_set_down "${device}"
450 ip link set "${device}" address "${addr}"
453 if [ "${up}" = "1" ]; then
454 device_set_up "${device}"
457 if [ "${ret}" != "0" ]; then
458 error_log "Could not
set address
'${addr}' on device
'${device}'"
468 for device in ${SYS_CLASS_NET}/*; do
469 device=$(basename ${device})
471 # bonding_masters is no device
472 [ "${device}" = "bonding_masters
" ] && continue
474 devices="${devices} ${device}"
485 # Check if a device has a cable plugged in
486 device_has_carrier() {
490 local carrier=$(__device_get_file ${device} carrier)
491 [ "${carrier}" = "1" ]
494 device_is_promisc() {
497 device_has_flag ${device} 0x200
500 device_set_promisc() {
504 assert device_exists ${device}
506 assert isoneof state on off
508 ip link set ${device} promisc ${state}
511 # Check if the device is free
516 # Check if the device is used
520 device_has_vlans ${device} && \
522 device_is_bonded ${device} && \
524 device_is_bridge_attached ${device} && \
530 # Give the device a new name
533 local destination=${2}
535 # Check if devices exists
536 if ! device_exists ${source} || device_exists ${destination}; then
541 if device_is_up ${source}; then
542 ip link set ${source} down
546 ip link set ${source} name ${destination}
548 if [ "${up}" = "1" ]; then
549 ip link set ${destination} up
557 # Silently fail if device was not found
558 [ -z "${device}" ] && return ${EXIT_ERROR}
560 # Do nothing if device is already up
561 device_is_up ${device} && return ${EXIT_OK}
563 device_set_parent_up ${device}
565 log DEBUG "Setting up device
'${device}'"
567 ip link set ${device} up
570 device_set_parent_up() {
574 if device_is_vlan ${device}; then
575 parent=$(vlan_get_parent ${device})
577 device_is_up ${parent} && return ${EXIT_OK}
579 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
581 device_set_up ${parent}
595 if device_is_up ${device}; then
596 log DEBUG "Tearing down device
'${device}'"
598 ip link set ${device} down
602 device_set_parent_down ${device}
607 device_set_parent_down() {
611 if device_is_vlan ${device}; then
612 parent=$(vlan_get_parent ${device})
614 device_is_up ${parent} || return ${EXIT_OK}
616 if device_is_free ${parent}; then
617 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
619 device_set_down ${parent}
629 if ! device_exists ${device}; then
630 error "Device
'${device}' does not exist.
"
634 echo $(<${SYS_CLASS_NET}/${device}/mtu)
637 # Set mtu to a device
642 if ! device_exists ${device}; then
643 error "Device
'${device}' does not exist.
"
647 local oldmtu=$(device_get_mtu ${device})
649 if [ "${oldmtu}" = "${mtu}" ]; then
650 # No need to set mtu.
654 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
657 if device_is_up ${device}; then
658 device_set_down ${device}
662 ip link set ${device} mtu ${mtu}
665 if [ "${up}" = "1" ]; then
666 device_set_up ${device}
669 if [ "${ret}" != "0" ]; then
670 error_log "Could not set mtu '${mtu}' on device '${device}'."
676 device_adjust_mtu() {
680 local other_device="${2}"
682 local mtu="$(device_get_mtu "${other_device}")"
683 device_set_mtu "${device}" "${mtu}"
689 log INFO "Running discovery process on device '${device}'."
692 for hook in $(hook_zone_get_all); do
693 hook_zone_exec ${hook} discover ${device}
702 assert device_exists ${device}
704 # IPv6 addresses must be fully imploded
705 local protocol=$(ip_detect_protocol ${addr})
706 case "${protocol}" in
708 addr=$(ipv6_implode ${addr})
712 listmatch ${addr} $(device_get_addresses ${device})
715 device_get_addresses() {
718 assert device_exists ${device}
723 ip addr show ${device} | \
724 while read prot addr line; do
725 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
729 __device_get_file() {
736 local path="${SYS_CLASS_NET}/${device}/${file}"
737 [ -r "${path}" ] || return ${EXIT_ERROR}
742 __device_set_file() {
749 local path="${SYS_CLASS_NET}/${device}/${file}"
750 if [ ! -w "${path}" ]; then
751 log DEBUG "Cannot write to file '${file}' (${value})"
755 echo "${value}" > "${path}"
758 device_get_rx_bytes() {
761 __device_get_file ${device} statistics/rx_bytes
764 device_get_tx_bytes() {
767 __device_get_file ${device} statistics/tx_bytes
770 device_get_rx_packets() {
773 __device_get_file ${device} statistics/rx_packets
776 device_get_tx_packets() {
779 __device_get_file ${device} statistics/tx_packets
782 device_get_rx_errors() {
785 __device_get_file ${device} statistics/rx_errors
788 device_get_tx_errors() {
791 __device_get_file ${device} statistics/tx_errors
797 __device_get_file ${device} speed
800 device_get_duplex() {
803 __device_get_file ${device} duplex
806 device_get_link_string() {
812 local speed="$(device_get_speed "${device}")"
814 list_append s "${speed} MBit/s"
817 local duplex="$(device_get_duplex "${device}")"
818 if isset duplex; then
819 list_append s "${duplex} duplex"