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 [ -d "${SYS_CLASS_NET}/${device}" ]
71 function device_has_flag
() {
75 local flags
=$
(__device_get_file
${device} flags
)
77 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
84 # Check if the device is up
85 function device_is_up
() {
88 device_exists
${device} ||
return ${EXIT_ERROR}
90 device_has_flag
${device} 0x1
93 # Check if the device is a bonding device
94 function device_is_bonding
() {
95 [ -d "/sys/class/net/${1}/bonding" ]
98 # Check if the device bonded in a bonding device
99 function device_is_bonded
() {
102 [ -d "${SYS_CLASS_NET}/${device}/master" ]
105 # Check if the device is a bridge
106 function device_is_bridge
() {
107 [ -d "/sys/class/net/${1}/bridge" ]
110 function device_is_bridge_attached
() {
113 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
116 # Check if the device is a virtual device
117 function device_is_virtual
() {
120 [ -e "/proc/net/vlan/${device}" ]
123 # Check if the device has virtual devices
124 function device_has_virtuals
() {
127 if device_is_virtual
${device}; then
131 local virtuals
=$
(device_get_virtuals
${device})
132 [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
135 function device_get_virtuals() {
138 local dev spacer1 id spacer2 parent
139 while read dev spacer1 id spacer2 parent; do
140 [ "${parent}" = "${device}" ] && echo "${dev}"
141 done < /proc/net/vlan/config | sort
144 # Check if the device is a ppp device
145 function device_is_ppp() {
148 local type=$(__device_get_file ${device} type)
150 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
153 # Check if the device is a pointopoint device.
154 function device_is_ptp
() {
157 device_has_flag
${device} 0x10
160 # Check if the device is a loopback device
161 function device_is_loopback
() {
164 [ "${device}" = "lo" ]
167 # Check if the device is a wireless device
168 function device_is_wireless
() {
171 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
174 # Check if the device is a physical network interface
175 function device_is_ethernet
() {
178 device_is_loopback
${device} && \
181 device_is_bonding
${device} && \
184 device_is_bridge
${device} && \
187 device_is_ppp
${device} && \
190 device_is_virtual
${device} && \
193 [ "$(__device_get_file ${device} type)" != "1" ] && \
199 # Get the device type
200 function device_get_type
() {
203 if device_is_virtual
${device}; then
206 elif device_is_bonding
${device}; then
209 elif device_is_bridge
${device}; then
212 elif device_is_ppp
${device}; then
215 elif device_is_loopback
${device}; then
218 elif device_is_wireless
${device}; then
221 elif device_is_ethernet
${device}; then
229 function device_get_status
() {
233 local status
=${STATUS_DOWN}
235 if device_is_up
${device}; then
238 if ! device_has_carrier
${device}; then
239 status
=${STATUS_NOCARRIER}
246 function device_get_address
() {
249 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
252 function device_set_address
() {
256 if ! device_exists
${device}; then
257 error
"Device '${device}' does not exist."
261 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
264 if device_is_up
${device}; then
265 device_set_down
${device}
269 ip link
set ${device} address
${addr}
272 if [ "${up}" = "1" ]; then
273 device_set_up
${device}
276 if [ "${ret}" != "0" ]; then
277 error_log
"Could not set address '${addr}' on device '${device}'."
283 function device_get
() {
287 for device
in ${SYS_CLASS_NET}/*; do
288 device
=$
(basename ${device})
290 # bonding_masters is no device
291 [ "${device}" = "bonding_masters" ] && continue
293 devices
="${devices} ${device}"
300 function devices_get_all
() {
304 # Check if a device has a cable plugged in
305 function device_has_carrier
() {
309 local carrier
=$
(__device_get_file
${device} carrier
)
310 [ "${carrier}" = "1" ]
313 function device_is_promisc
() {
316 device_has_flag
${device} 0x200
319 function device_set_promisc
() {
323 assert device_exists
${device}
325 assert isoneof state on off
327 ip link
set ${device} promisc
${state}
330 # Check if the device is free
331 function device_is_free
() {
335 # Check if the device is used
336 function device_is_used
() {
339 device_has_virtuals
${device} && \
341 device_is_bonded
${device} && \
343 device_is_bridge_attached
${device} && \
349 function device_hash
() {
352 # Get mac address of device and remove all colons (:)
353 # that will result in a hash.
354 device
=$
(macify
${device})
359 # Give the device a new name
360 function device_set_name
() {
362 local destination
=${2}
364 # Check if devices exists
365 if ! device_exists
${source} || device_exists
${destination}; then
370 if device_is_up
${source}; then
371 ip link
set ${source} down
375 ip link
set ${source} name
${destination}
377 if [ "${up}" = "1" ]; then
378 ip link
set ${destination} up
383 function device_set_up
() {
386 # Silently fail if device was not found
387 [ -z "${device}" ] && return ${EXIT_ERROR}
389 # Do nothing if device is already up
390 device_is_up
${device} && return ${EXIT_OK}
392 device_set_parent_up
${device}
394 log DEBUG
"Setting up device '${device}'"
396 ip link
set ${device} up
399 function device_set_parent_up
() {
403 if device_is_virtual
${device}; then
404 parent
=$
(virtual_get_parent
${device})
406 device_is_up
${parent} && return ${EXIT_OK}
408 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
410 device_set_up
${parent}
418 function device_set_down
() {
424 if device_is_up
${device}; then
425 log DEBUG
"Tearing down device '${device}'"
427 ip link
set ${device} down
431 device_set_parent_down
${device}
436 function device_set_parent_down
() {
440 if device_is_virtual
${device}; then
441 parent
=$
(virtual_get_parent
${device})
443 device_is_up
${parent} ||
return ${EXIT_OK}
445 if device_is_free
${parent}; then
446 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
448 device_set_down
${parent}
455 function device_get_mtu
() {
458 if ! device_exists
${device}; then
459 error
"Device '${device}' does not exist."
463 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
466 # Set mtu to a device
467 function device_set_mtu
() {
471 if ! device_exists
${device}; then
472 error
"Device '${device}' does not exist."
476 local oldmtu
=$
(device_get_mtu
${device})
478 if [ "${oldmtu}" = "${mtu}" ]; then
479 # No need to set mtu.
483 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
486 if device_is_up
${device}; then
487 device_set_down
${device}
491 ip link
set ${device} mtu
${mtu}
494 if [ "${up}" = "1" ]; then
495 device_set_up
${device}
498 if [ "${ret}" != "0" ]; then
499 error_log
"Could not set mtu '${mtu}' on device '${device}'."
505 function device_discover
() {
508 log INFO
"Running discovery process on device '${device}'."
511 for hook
in $
(hook_zone_get_all
); do
512 hook_zone_exec
${hook} discover
${device}
516 function device_has_ip
() {
521 assert device_exists
${device}
523 # IPv6 addresses must be fully imploded
524 local protocol
=$
(ip_detect_protocol
${addr})
525 case "${protocol}" in
527 addr
=$
(ipv6_implode
${addr})
531 listmatch
${addr} $
(device_get_addresses
${device})
534 function device_get_addresses
() {
537 assert device_exists
${device}
542 ip addr show
${device} | \
543 while read prot addr line
; do
544 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
548 function __device_get_file
() {
555 local path
="${SYS_CLASS_NET}/${device}/${file}"
556 [ -r "${path}" ] ||
return ${EXIT_ERROR}
561 function device_get_rx_bytes
() {
564 __device_get_file
${device} statistics
/rx_bytes
567 function device_get_tx_bytes
() {
570 __device_get_file
${device} statistics
/tx_bytes
573 function device_get_rx_packets
() {
576 __device_get_file
${device} statistics
/rx_packets
579 function device_get_tx_packets
() {
582 __device_get_file
${device} statistics
/tx_packets
585 function device_get_rx_errors
() {
588 __device_get_file
${device} statistics
/rx_errors
591 function device_get_tx_errors
() {
594 __device_get_file
${device} statistics
/tx_errors
597 function device_get_speed
() {
600 __device_get_file
${device} speed
603 function device_get_duplex
() {
606 __device_get_file
${device} duplex