]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.bridge
network: Add some initialization handlers.
[people/arne_f/network.git] / functions.bridge
CommitLineData
837995e6
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22function bridge_attach_device() {
23 local bridge=${1}
24 local device=${2}
25
26 assert isset bridge
27 assert isset device
28
29 assert device_exists ${bridge}
30 assert device_exists ${device}
31
32 # If device is already attached, exit silently
33 if listmatch ${device} $(bridge_get_members ${bridge}); then
34 return ${EXIT_OK}
35 fi
36
37 log INFO "Attaching device '${device}' to bridge '${bridge}'."
38
39 brctl addif ${bridge} ${device}
40}
41
42function bridge_detach_device() {
43 local bridge=${1}
44 local device=${2}
45
46 assert isset bridge
47 assert isset device
48
49 if ! device_exists ${bridge}; then
50 error "Bridge '${bridge}' does not exist."
51 return ${EXIT_ERROR}
52 fi
53
54 if ! device_exists ${device}; then
55 error "Device '${device}' does not exist."
56 return ${EXIT_ERROR}
57 fi
58
59 log INFO "Detaching device '${device}' from bridge '${bridge}'."
60
61 brctl delif ${bridge} ${device}
62}
63
64function bridge_get_members() {
65 local bridge=${1}
66
67 assert isset bridge
68
69 local member
70 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
71 member=$(basename ${member})
72 if device_exists ${member}; then
73 echo "${member}"
74 fi
75 done
76}
77
78function bridge_is_forwarding() {
79 local seconds=45
80 local zone=${1}
81
82 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
83
84 local device
85 while [ ${seconds} -gt 0 ]; do
86 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
87 [ -e "${device}/state" ] || continue
88 if [ "$(<${device}/state)" = "3" ]; then
89 return ${EXIT_OK}
90 fi
91 done
92 sleep 1
93 seconds=$((${seconds} - 1))
94 done
95
96 return ${EXIT_ERROR}
97}
98
99function bridge_has_carrier() {
100 local zone=${1}
101
102 local has_carrier=${EXIT_ERROR}
103
104 local device
105 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
106 device=$(basename ${device})
107 device_exists ${device} || continue
108
109 device_has_carrier ${device} && has_carrier=${EXIT_OK}
110 done
111
112 return ${has_carrier}
113}