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 # Check if the device is up
72 function device_is_up
() {
75 device_exists
${device} ||
return ${EXIT_ERROR}
77 ip link show
${device} 2>/dev
/null |
grep -qE "<.*UP.*>"
80 # Check if the device is a bonding device
81 function device_is_bonding
() {
82 [ -d "/sys/class/net/${1}/bonding" ]
85 # Check if the device bonded in a bonding device
86 function device_is_bonded
() {
89 [ -d "${SYS_CLASS_NET}/${device}/master" ]
92 # Check if the device is a bridge
93 function device_is_bridge
() {
94 [ -d "/sys/class/net/${1}/bridge" ]
97 function device_is_bridge_attached
() {
100 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
103 # Check if the device is a virtual device
104 function device_is_virtual
() {
107 [ -e "/proc/net/vlan/${device}" ]
110 # Check if the device has virtual devices
111 function device_has_virtuals
() {
114 if device_is_virtual
${device}; then
118 if [ ! -e "/proc/net/vlan/config" ]; then
121 grep -q "${1}$" /proc
/net
/vlan
/config
124 # Check if the device is a ppp device
125 function device_is_ppp
() {
128 local type=$
(__device_get_file
${device} type)
129 if [ "${type}" = "512" ]; then
136 # Check if the device is a loopback device
137 function device_is_loopback
() {
140 [ "${device}" = "lo" ]
143 # Check if the device is a wireless device
144 function device_is_wireless
() {
147 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
150 # Check if the device is a physical network interface
151 function device_is_real
() {
154 device_is_loopback
${device} && \
157 device_is_bonding
${device} && \
160 device_is_bridge
${device} && \
163 device_is_ppp
${device} && \
166 device_is_virtual
${device} && \
169 [ "$(__device_get_file ${device} type)" != "1" ] && \
175 # Get the device type
176 function device_get_type
() {
179 if device_is_virtual
${device}; then
182 elif device_is_bonding
${device}; then
185 elif device_is_bridge
${device}; then
188 elif device_is_ppp
${device}; then
191 elif device_is_loopback
${device}; then
194 elif device_is_wireless
${device}; then
197 elif device_is_real
${device}; then
205 function device_get_status
() {
210 local status
=${STATUS_UNKNOWN}
212 if ! device_has_carrier
${device}; then
213 status
=${STATUS_NOCARRIER}
214 elif device_is_up
${device}; then
216 elif device_is_down
${device}; then
217 status
=${STATUS_DOWN}
225 function device_get_address
() {
228 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
231 function device_set_address
() {
235 if ! device_exists
${device}; then
236 error
"Device '${device}' does not exist."
240 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
243 if device_is_up
${device}; then
244 device_set_down
${device}
248 ip link
set ${device} address
${addr}
251 if [ "${up}" = "1" ]; then
252 device_set_up
${device}
255 if [ "${ret}" != "0" ]; then
256 error_log
"Could not set address '${addr}' on device '${device}'."
262 function device_get
() {
266 for device
in ${SYS_CLASS_NET}/*; do
267 device
=$
(basename ${device})
269 # bonding_masters is no device
270 [ "${device}" = "bonding_masters" ] && continue
272 devices
="${devices} ${device}"
279 function devices_get_all
() {
283 # Check if a device has a cable plugged in
284 function device_has_carrier
() {
288 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
291 function device_is_promisc
() {
294 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
297 function device_set_promisc
() {
301 assert device_exists
${device}
303 assert isoneof state on off
305 ip link
set ${device} promisc
${state}
308 # Check if the device is free
309 function device_is_free
() {
313 # Check if the device is used
314 function device_is_used
() {
317 device_has_virtuals
${device} && \
319 device_is_bonded
${device} && \
321 device_is_bridge_attached
${device} && \
327 function device_hash
() {
330 # Get mac address of device and remove all colons (:)
331 # that will result in a hash.
332 device
=$
(macify
${device})
337 # Give the device a new name
338 function device_set_name
() {
340 local destination
=${2}
342 # Check if devices exists
343 if ! device_exists
${source} || device_exists
${destination}; then
348 if device_is_up
${source}; then
349 ip link
set ${source} down
353 ip link
set ${source} name
${destination}
355 if [ "${up}" = "1" ]; then
356 ip link
set ${destination} up
361 function device_set_up
() {
364 # Silently fail if device was not found
365 [ -z "${device}" ] && return ${EXIT_ERROR}
367 # Do nothing if device is already up
368 device_is_up
${device} && return ${EXIT_OK}
370 device_set_parent_up
${device}
372 log DEBUG
"Setting up device '${device}'"
374 ip link
set ${device} up
377 function device_set_parent_up
() {
381 if device_is_virtual
${device}; then
382 parent
=$
(virtual_get_parent
${device})
384 device_is_up
${parent} && return ${EXIT_OK}
386 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
388 device_set_up
${parent}
396 function device_set_down
() {
402 if device_is_up
${device}; then
403 log DEBUG
"Tearing down device '${device}'"
405 ip link
set ${device} down
409 device_set_parent_down
${device}
414 function device_set_parent_down
() {
418 if device_is_virtual
${device}; then
419 parent
=$
(virtual_get_parent
${device})
421 device_is_up
${parent} ||
return ${EXIT_OK}
423 if device_is_free
${parent}; then
424 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
426 device_set_down
${parent}
433 function device_get_mtu
() {
436 if ! device_exists
${device}; then
437 error
"Device '${device}' does not exist."
441 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
444 # Set mtu to a device
445 function device_set_mtu
() {
449 if ! device_exists
${device}; then
450 error
"Device '${device}' does not exist."
454 local oldmtu
=$
(device_get_mtu
${device})
456 if [ "${oldmtu}" = "${mtu}" ]; then
457 # No need to set mtu.
461 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
464 if device_is_up
${device}; then
465 device_set_down
${device}
469 ip link
set ${device} mtu
${mtu}
472 if [ "${up}" = "1" ]; then
473 device_set_up
${device}
476 if [ "${ret}" != "0" ]; then
477 error_log
"Could not set mtu '${mtu}' on device '${device}'."
483 function device_discover
() {
486 log INFO
"Running discovery process on device '${device}'."
489 for hook
in $
(hook_zone_get_all
); do
490 hook_zone_exec
${hook} discover
${device}
494 function device_has_ip
() {
499 assert device_exists
${device}
501 # IPv6 addresses must be fully imploded
502 local protocol
=$
(ip_detect_protocol
${addr})
503 case "${protocol}" in
505 addr
=$
(ipv6_implode
${addr})
509 listmatch
${addr} $
(device_get_addresses
${device})
512 function device_get_addresses
() {
515 assert device_exists
${device}
520 ip addr show
${device} | \
521 while read prot addr line
; do
522 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
526 function __device_get_file
() {
533 cat ${SYS_CLASS_NET}/${device}/${file}
536 function device_get_rx_bytes
() {
539 __device_get_file
${device} statistics
/rx_bytes
542 function device_get_tx_bytes
() {
545 __device_get_file
${device} statistics
/tx_bytes
548 function device_get_rx_packets
() {
551 __device_get_file
${device} statistics
/rx_packets
554 function device_get_tx_packets
() {
557 __device_get_file
${device} statistics
/tx_packets
560 function device_get_rx_errors
() {
563 __device_get_file
${device} statistics
/rx_errors
566 function device_get_tx_errors
() {
569 __device_get_file
${device} statistics
/tx_errors