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 if device_exists
${device}; then
33 for d
in $
(devices_get_all
); do
34 if [ "$(device_get_address ${d})" = "${device}" ]; then
48 if mac_is_valid
${device}; then
53 if device_exists
${device}; then
54 device_get_address
${device}
61 # Check if the device exists
62 function device_exists
() {
65 # If device name was not found, exit.
66 [ -n "${device}" ] ||
return ${EXIT_ERROR}
68 # Check for a normal network device.
69 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
71 # If the check above, did not find a result,
72 # we check for serial devices.
73 serial_exists ${device}
76 function device_matches_pattern() {
83 pattern="^
${pattern//N/[[:digit:]]+}$
"
85 [[ ${device} =~ ${pattern} ]] \
86 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
89 function device_delete() {
93 # Nothing to do, it device does not exist.
94 device_exists ${device} || return ${EXIT_OK}
97 cmd_quiet ip link delete ${device}
100 if [ ${ret} -ne ${EXIT_OK} ]; then
101 log ERROR "device
: Could not delete device
'${device}': ${ret}"
108 function device_has_flag() {
112 local flags=$(__device_get_file ${device} flags)
114 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
121 # Check if the device is up
122 function device_is_up() {
125 device_exists ${device} || return ${EXIT_ERROR}
127 device_has_flag ${device} 0x1
130 function device_ifindex_to_name() {
134 local device device_idx
135 for device in ${SYS_CLASS_NET}/*; do
136 device=$(basename ${device})
137 device_exists ${device} || continue
139 device_idx=$(device_get_ifindex ${device})
141 if [ "${device_idx}" = "${idx}" ]; then
150 function device_get_ifindex() {
154 local path="${SYS_CLASS_NET}/${1}/ifindex
"
156 # Check if file can be read.
157 [ -r "${path}" ] || return ${EXIT_ERROR}
162 # Check if the device is a batman-adv bridge
163 function device_is_batman_adv() {
164 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
167 # Check if the device is a batman-adv slave port
168 function device_is_batman_adv_slave() {
171 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
172 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
187 # Check if the device is a bonding device
188 function device_is_bonding() {
189 [ -d "/sys
/class
/net
/${1}/bonding
" ]
192 # Check if the device bonded in a bonding device
193 function device_is_bonded() {
196 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
199 # Check if the device is a bridge
200 function device_is_bridge() {
201 [ -d "/sys
/class
/net
/${1}/bridge
" ]
204 function device_is_bridge_attached() {
206 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
209 function device_is_wireless_monitor() {
213 device_is_wireless "${device}" && \
214 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
217 function device_is_wireless_adhoc() {
221 device_is_wireless "${device}" && \
222 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
225 function device_get_bridge() {
229 # Check if device is attached to a bridge.
230 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
232 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
233 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
235 local ifindex=$(<${ifindex_path})
238 device_ifindex_to_name ${ifindex}
241 # Check if the device is a vlan device
242 function device_is_vlan() {
246 [ -e "${PROC_NET_VLAN}/${device}" ]
249 # Check if the device has vlan devices
250 function device_has_vlans() {
254 if device_is_vlan ${device}; then
258 local vlans=$(device_get_vlans ${device})
259 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
262 function device_get_vlans
() {
266 # If no 8021q module has been loaded into the kernel,
267 # we cannot do anything.
268 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
270 local dev spacer1 id spacer2 parent
271 while read dev spacer1 id spacer2 parent
; do
272 [ "${parent}" = "${device}" ] ||
continue
275 done < ${PROC_NET_VLAN_CONFIG}
278 # Check if the device is a ppp device
279 function device_is_ppp
() {
282 local type=$
(__device_get_file
${device} type)
284 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
287 # Check if the device is a pointopoint device.
288 function device_is_ptp() {
291 device_has_flag ${device} 0x10
294 # Check if the device is a loopback device
295 function device_is_loopback() {
298 [ "${device}" = "lo
" ]
301 # Check if the device is a wireless device
302 function device_is_wireless() {
305 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
308 function device_get_phy() {
311 if device_is_wireless "${device}"; then
312 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
319 function device_is_serial() {
323 # Check if the device is a physical network interface
324 function device_is_ethernet() {
327 device_is_loopback ${device} && \
330 device_is_bonding ${device} && \
333 device_is_bridge ${device} && \
336 device_is_ppp ${device} && \
339 device_is_vlan ${device} && \
342 [ "$
(__device_get_file
${device} type)" != "1" ] && \
348 # Get the device type
349 function device_get_type() {
352 if 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_batman_adv ${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_ethernet ${device}; then
379 elif device_is_serial ${device}; then
387 function device_get_status() {
391 local status=${STATUS_DOWN}
393 if device_is_up ${device}; then
396 if ! device_has_carrier ${device}; then
397 status=${STATUS_NOCARRIER}
404 function device_get_address() {
407 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
410 function device_set_address() {
416 if ! device_exists "${device}"; then
417 error "Device
'${device}' does not exist.
"
421 # Do nothing if the address has not changed
422 local old_addr="$
(device_get_address
"${device}")"
423 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
427 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
430 if device_is_up "${device}"; then
431 device_set_down "${device}"
435 ip link set "${device}" address "${addr}"
438 if [ "${up}" = "1" ]; then
439 device_set_up "${device}"
442 if [ "${ret}" != "0" ]; then
443 error_log "Could not
set address
'${addr}' on device
'${device}'"
449 function device_get() {
453 for device in ${SYS_CLASS_NET}/*; do
454 device=$(basename ${device})
456 # bonding_masters is no device
457 [ "${device}" = "bonding_masters
" ] && continue
459 devices="${devices} ${device}"
466 function devices_get_all() {
470 # Check if a device has a cable plugged in
471 function device_has_carrier() {
475 local carrier=$(__device_get_file ${device} carrier)
476 [ "${carrier}" = "1" ]
479 function device_is_promisc() {
482 device_has_flag ${device} 0x200
485 function device_set_promisc() {
489 assert device_exists ${device}
491 assert isoneof state on off
493 ip link set ${device} promisc ${state}
496 # Check if the device is free
497 function device_is_free() {
501 # Check if the device is used
502 function device_is_used() {
505 device_has_vlans ${device} && \
507 device_is_bonded ${device} && \
509 device_is_bridge_attached ${device} && \
515 function device_hash() {
518 # Get mac address of device and remove all colons (:)
519 # that will result in a hash.
520 device=$(macify ${device})
525 # Give the device a new name
526 function device_set_name() {
528 local destination=${2}
530 # Check if devices exists
531 if ! device_exists ${source} || device_exists ${destination}; then
536 if device_is_up ${source}; then
537 ip link set ${source} down
541 ip link set ${source} name ${destination}
543 if [ "${up}" = "1" ]; then
544 ip link set ${destination} up
549 function device_set_up() {
552 # Silently fail if device was not found
553 [ -z "${device}" ] && return ${EXIT_ERROR}
555 # Do nothing if device is already up
556 device_is_up ${device} && return ${EXIT_OK}
558 device_set_parent_up ${device}
560 log DEBUG "Setting up device
'${device}'"
562 ip link set ${device} up
565 function device_set_parent_up() {
569 if device_is_vlan ${device}; then
570 parent=$(vlan_get_parent ${device})
572 device_is_up ${parent} && return ${EXIT_OK}
574 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
576 device_set_up ${parent}
584 function device_set_down() {
590 if device_is_up ${device}; then
591 log DEBUG "Tearing down device
'${device}'"
593 ip link set ${device} down
597 device_set_parent_down ${device}
602 function device_set_parent_down() {
606 if device_is_vlan ${device}; then
607 parent=$(vlan_get_parent ${device})
609 device_is_up ${parent} || return ${EXIT_OK}
611 if device_is_free ${parent}; then
612 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
614 device_set_down ${parent}
621 function device_get_mtu() {
624 if ! device_exists ${device}; then
625 error "Device
'${device}' does not exist.
"
629 echo $(<${SYS_CLASS_NET}/${device}/mtu)
632 # Set mtu to a device
633 function device_set_mtu() {
637 if ! device_exists ${device}; then
638 error "Device
'${device}' does not exist.
"
642 local oldmtu=$(device_get_mtu ${device})
644 if [ "${oldmtu}" = "${mtu}" ]; then
645 # No need to set mtu.
649 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
652 if device_is_up ${device}; then
653 device_set_down ${device}
657 ip link set ${device} mtu ${mtu}
660 if [ "${up}" = "1" ]; then
661 device_set_up ${device}
664 if [ "${ret}" != "0" ]; then
665 error_log "Could not set mtu '${mtu}' on device '${device}'."
671 function device_adjust_mtu() {
675 local other_device="${2}"
677 local mtu="$(device_get_mtu "${other_device}")"
678 device_set_mtu "${device}" "${mtu}"
681 function device_discover() {
684 log INFO "Running discovery process on device '${device}'."
687 for hook in $(hook_zone_get_all); do
688 hook_zone_exec ${hook} discover ${device}
692 function device_has_ip() {
697 assert device_exists ${device}
699 # IPv6 addresses must be fully imploded
700 local protocol=$(ip_detect_protocol ${addr})
701 case "${protocol}" in
703 addr=$(ipv6_implode ${addr})
707 listmatch ${addr} $(device_get_addresses ${device})
710 function device_get_addresses() {
713 assert device_exists ${device}
718 ip addr show ${device} | \
719 while read prot addr line; do
720 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
724 function __device_get_file() {
731 local path="${SYS_CLASS_NET}/${device}/${file}"
732 [ -r "${path}" ] || return ${EXIT_ERROR}
737 function __device_set_file() {
744 local path="${SYS_CLASS_NET}/${device}/${file}"
745 if [ ! -w "${path}" ]; then
746 log DEBUG "Cannot write to file '${file}' (${value})"
750 echo "${value}" > "${path}"
753 function device_get_rx_bytes() {
756 __device_get_file ${device} statistics/rx_bytes
759 function device_get_tx_bytes() {
762 __device_get_file ${device} statistics/tx_bytes
765 function device_get_rx_packets() {
768 __device_get_file ${device} statistics/rx_packets
771 function device_get_tx_packets() {
774 __device_get_file ${device} statistics/tx_packets
777 function device_get_rx_errors() {
780 __device_get_file ${device} statistics/rx_errors
783 function device_get_tx_errors() {
786 __device_get_file ${device} statistics/tx_errors
789 function device_get_speed() {
792 __device_get_file ${device} speed
795 function device_get_duplex() {
798 __device_get_file ${device} duplex