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)
139 # Check if the device is a loopback device
140 function device_is_loopback
() {
143 [ "${device}" = "lo" ]
146 # Check if the device is a wireless device
147 function device_is_wireless
() {
150 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
153 # Check if the device is a physical network interface
154 function device_is_real
() {
157 device_is_loopback
${device} && \
160 device_is_bonding
${device} && \
163 device_is_bridge
${device} && \
166 device_is_ppp
${device} && \
169 device_is_virtual
${device} && \
172 [ "$(__device_get_file ${device} type)" != "1" ] && \
178 # Get the device type
179 function device_get_type
() {
182 if device_is_virtual
${device}; then
185 elif device_is_bonding
${device}; then
188 elif device_is_bridge
${device}; then
191 elif device_is_ppp
${device}; then
194 elif device_is_loopback
${device}; then
197 elif device_is_wireless
${device}; then
200 elif device_is_real
${device}; then
208 function device_get_status
() {
213 local status
=${STATUS_UNKNOWN}
215 if ! device_has_carrier
${device}; then
216 status
=${STATUS_NOCARRIER}
217 elif device_is_up
${device}; then
219 elif device_is_down
${device}; then
220 status
=${STATUS_DOWN}
228 function device_get_address
() {
231 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
234 function device_set_address
() {
238 if ! device_exists
${device}; then
239 error
"Device '${device}' does not exist."
243 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
246 if device_is_up
${device}; then
247 device_set_down
${device}
251 ip link
set ${device} address
${addr}
254 if [ "${up}" = "1" ]; then
255 device_set_up
${device}
258 if [ "${ret}" != "0" ]; then
259 error_log
"Could not set address '${addr}' on device '${device}'."
265 function device_get
() {
269 for device
in ${SYS_CLASS_NET}/*; do
270 device
=$
(basename ${device})
272 # bonding_masters is no device
273 [ "${device}" = "bonding_masters" ] && continue
275 devices
="${devices} ${device}"
282 function devices_get_all
() {
286 # Check if a device has a cable plugged in
287 function device_has_carrier
() {
291 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
294 function device_is_promisc
() {
297 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
300 function device_set_promisc
() {
304 assert device_exists
${device}
306 assert isoneof state on off
308 ip link
set ${device} promisc
${state}
311 # Check if the device is free
312 function device_is_free
() {
316 # Check if the device is used
317 function device_is_used
() {
320 device_has_virtuals
${device} && \
322 device_is_bonded
${device} && \
324 device_is_bridge_attached
${device} && \
330 function device_hash
() {
333 # Get mac address of device and remove all colons (:)
334 # that will result in a hash.
335 device
=$
(macify
${device})
340 # Give the device a new name
341 function device_set_name
() {
343 local destination
=${2}
345 # Check if devices exists
346 if ! device_exists
${source} || device_exists
${destination}; then
351 if device_is_up
${source}; then
352 ip link
set ${source} down
356 ip link
set ${source} name
${destination}
358 if [ "${up}" = "1" ]; then
359 ip link
set ${destination} up
364 function device_set_up
() {
367 # Silently fail if device was not found
368 [ -z "${device}" ] && return ${EXIT_ERROR}
370 # Do nothing if device is already up
371 device_is_up
${device} && return ${EXIT_OK}
373 device_set_parent_up
${device}
375 log DEBUG
"Setting up device '${device}'"
377 ip link
set ${device} up
380 function device_set_parent_up
() {
384 if device_is_virtual
${device}; then
385 parent
=$
(virtual_get_parent
${device})
387 device_is_up
${parent} && return ${EXIT_OK}
389 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
391 device_set_up
${parent}
399 function device_set_down
() {
405 if device_is_up
${device}; then
406 log DEBUG
"Tearing down device '${device}'"
408 ip link
set ${device} down
412 device_set_parent_down
${device}
417 function device_set_parent_down
() {
421 if device_is_virtual
${device}; then
422 parent
=$
(virtual_get_parent
${device})
424 device_is_up
${parent} ||
return ${EXIT_OK}
426 if device_is_free
${parent}; then
427 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
429 device_set_down
${parent}
436 function device_get_mtu
() {
439 if ! device_exists
${device}; then
440 error
"Device '${device}' does not exist."
444 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
447 # Set mtu to a device
448 function device_set_mtu
() {
452 if ! device_exists
${device}; then
453 error
"Device '${device}' does not exist."
457 local oldmtu
=$
(device_get_mtu
${device})
459 if [ "${oldmtu}" = "${mtu}" ]; then
460 # No need to set mtu.
464 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
467 if device_is_up
${device}; then
468 device_set_down
${device}
472 ip link
set ${device} mtu
${mtu}
475 if [ "${up}" = "1" ]; then
476 device_set_up
${device}
479 if [ "${ret}" != "0" ]; then
480 error_log
"Could not set mtu '${mtu}' on device '${device}'."
486 function device_discover
() {
489 log INFO
"Running discovery process on device '${device}'."
492 for hook
in $
(hook_zone_get_all
); do
493 hook_zone_exec
${hook} discover
${device}
497 function device_has_ip
() {
502 assert device_exists
${device}
504 # IPv6 addresses must be fully imploded
505 local protocol
=$
(ip_detect_protocol
${addr})
506 case "${protocol}" in
508 addr
=$
(ipv6_implode
${addr})
512 listmatch
${addr} $
(device_get_addresses
${device})
515 function device_get_addresses
() {
518 assert device_exists
${device}
523 ip addr show
${device} | \
524 while read prot addr line
; do
525 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
529 function __device_get_file
() {
536 cat ${SYS_CLASS_NET}/${device}/${file}
539 function device_get_rx_bytes
() {
542 __device_get_file
${device} statistics
/rx_bytes
545 function device_get_tx_bytes
() {
548 __device_get_file
${device} statistics
/tx_bytes
551 function device_get_rx_packets
() {
554 __device_get_file
${device} statistics
/rx_packets
557 function device_get_tx_packets
() {
560 __device_get_file
${device} statistics
/tx_packets
563 function device_get_rx_errors
() {
566 __device_get_file
${device} statistics
/rx_errors
569 function device_get_tx_errors
() {
572 __device_get_file
${device} statistics
/tx_errors