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 ip link show
${device} 2>/dev
/null |
grep -qE "<.*POINTOPOINT.*>"
131 # Check if the device is a loopback device
132 function device_is_loopback
() {
133 local device
=$
(devicify
${1})
134 [ "${device}" = "lo" ]
137 # Check if the device is a physical network interface
138 function device_is_real
() {
141 device_is_loopback
${device} && \
144 device_is_bonding
${device} && \
147 device_is_bridge
${device} && \
150 device_is_ppp
${device} && \
153 device_is_virtual
${device} && \
156 [ "$(__device_get_file ${device} type)" != "1" ] && \
162 # Get the device type
163 function device_get_type
() {
164 local device
=$
(devicify
${1})
166 if device_is_virtual
${device}; then
169 elif device_is_bonding
${device}; then
172 elif device_is_bridge
${device}; then
175 elif device_is_ppp
${device}; then
178 elif device_is_loopback
${device}; then
181 elif device_is_real
${device}; then
189 function device_get_status
() {
194 local status
=${STATUS_UNKNOWN}
196 if ! device_has_carrier
${device}; then
197 status
=${STATUS_NOCARRIER}
198 elif device_is_up
${device}; then
200 elif device_is_down
${device}; then
201 status
=${STATUS_DOWN}
209 function device_get_address
() {
212 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
215 function device_set_address
() {
219 if ! device_exists
${device}; then
220 error
"Device '${device}' does not exist."
224 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
227 if device_is_up
${device}; then
228 device_set_down
${device}
232 ip link
set ${device} address
${addr}
235 if [ "${up}" = "1" ]; then
236 device_set_up
${device}
239 if [ "${ret}" != "0" ]; then
240 error_log
"Could not set address '${addr}' on device '${device}'."
246 function device_get
() {
250 for device
in ${SYS_CLASS_NET}/*; do
251 device
=$
(basename ${device})
253 # bonding_masters is no device
254 [ "${device}" = "bonding_masters" ] && continue
256 devices
="${devices} ${device}"
263 function devices_get_all
() {
267 # Check if a device has a cable plugged in
268 function device_has_carrier
() {
269 local device
=$
(devicify
${1})
270 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
273 function device_is_promisc
() {
276 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
279 function device_set_promisc
() {
283 assert device_exists
${device}
285 assert isoneof state on off
287 ip link
set ${device} promisc
${state}
290 # Check if the device is free
291 function device_is_free
() {
295 # Check if the device is used
296 function device_is_used
() {
297 local device
=$
(devicify
${1})
299 device_has_virtuals
${device} && \
301 device_is_bonded
${device} && \
303 device_is_bridge_attached
${device} && \
309 function device_hash
() {
312 # Get mac address of device and remove all colons (:)
313 # that will result in a hash.
314 device
=$
(macify
${device})
319 # Give the device a new name
320 function device_set_name
() {
322 local destination
=${2}
324 # Check if devices exists
325 if ! device_exists
${source} || device_exists
${destination}; then
330 if device_is_up
${source}; then
331 ip link
set ${source} down
335 ip link
set ${source} name
${destination}
337 if [ "${up}" = "1" ]; then
338 ip link
set ${destination} up
343 function device_set_up
() {
344 local device
=$
(devicify
${1})
346 # Silently fail if device was not found
347 [ -z "${device}" ] && return ${EXIT_ERROR}
349 # Do nothing if device is already up
350 device_is_up
${device} && return ${EXIT_OK}
352 device_set_parent_up
${device}
354 log DEBUG
"Setting up device '${device}'"
356 ip link
set ${device} up
359 function device_set_parent_up
() {
363 if device_is_virtual
${device}; then
364 parent
=$
(virtual_get_parent
${device})
366 device_is_up
${parent} && return ${EXIT_OK}
368 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
370 device_set_up
${parent}
378 function device_set_down
() {
379 local device
=$
(devicify
${1})
383 if device_is_up
${device}; then
384 log DEBUG
"Tearing down device '${device}'"
386 ip link
set ${device} down
390 device_set_parent_down
${device}
395 function device_set_parent_down
() {
399 if device_is_virtual
${device}; then
400 parent
=$
(virtual_get_parent
${device})
402 device_is_up
${parent} ||
return ${EXIT_OK}
404 if device_is_free
${parent}; then
405 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
407 device_set_down
${parent}
414 function device_get_mtu
() {
417 if ! device_exists
${device}; then
418 error
"Device '${device}' does not exist."
422 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
425 # Set mtu to a device
426 function device_set_mtu
() {
430 if ! device_exists
${device}; then
431 error
"Device '${device}' does not exist."
435 local oldmtu
=$
(device_get_mtu
${device})
437 if [ "${oldmtu}" = "${mtu}" ]; then
438 # No need to set mtu.
442 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
445 if device_is_up
${device}; then
446 device_set_down
${device}
450 ip link
set ${device} mtu
${mtu}
453 if [ "${up}" = "1" ]; then
454 device_set_up
${device}
457 if [ "${ret}" != "0" ]; then
458 error_log
"Could not set mtu '${mtu}' on device '${device}'."
464 function device_discover
() {
467 log INFO
"Running discovery process on device '${device}'."
470 for hook
in $
(hook_zone_get_all
); do
471 hook_zone_exec
${hook} discover
${device}
475 function device_has_ip
() {
480 assert device_exists
${device}
482 # IPv6 addresses must be fully imploded
483 local protocol
=$
(ip_detect_protocol
${addr})
484 case "${protocol}" in
486 addr
=$
(ipv6_implode
${addr})
490 listmatch
${addr} $
(device_get_addresses
${device})
493 function device_get_addresses
() {
496 assert device_exists
${device}
501 ip addr show
${device} | \
502 while read prot addr line
; do
503 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
507 function __device_get_file
() {
514 cat ${SYS_CLASS_NET}/${device}/${file}
517 function device_get_rx_bytes
() {
520 __device_get_file
${device} statistics
/rx_bytes
523 function device_get_tx_bytes
() {
526 __device_get_file
${device} statistics
/tx_bytes
529 function device_get_rx_packets
() {
532 __device_get_file
${device} statistics
/rx_packets
535 function device_get_tx_packets
() {
538 __device_get_file
${device} statistics
/tx_packets
541 function device_get_rx_errors
() {
544 __device_get_file
${device} statistics
/rx_errors
547 function device_get_tx_errors
() {
550 __device_get_file
${device} statistics
/tx_errors