]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.bridge
network: STP: Make protocol version configureable.
[people/arne_f/network.git] / functions.bridge
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
22 function 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
42 function 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 return ${EXIT_OK}
56 fi
57
58 # If device is not attached, exit silently
59 if ! listmatch ${device} $(bridge_get_members ${bridge}); then
60 return ${EXIT_OK}
61 fi
62
63 log INFO "Detaching device '${device}' from bridge '${bridge}'."
64
65 brctl delif ${bridge} ${device}
66 }
67
68 function bridge_get_members() {
69 local bridge=${1}
70
71 assert isset bridge
72
73 local member
74 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
75 member=$(basename ${member})
76 if device_exists ${member}; then
77 echo "${member}"
78 fi
79 done
80 }
81
82 function bridge_is_forwarding() {
83 local seconds=45
84 local zone=${1}
85
86 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
87
88 local device
89 while [ ${seconds} -gt 0 ]; do
90 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
91 [ -e "${device}/state" ] || continue
92 if [ "$(<${device}/state)" = "3" ]; then
93 return ${EXIT_OK}
94 fi
95 done
96 sleep 1
97 seconds=$((${seconds} - 1))
98 done
99
100 return ${EXIT_ERROR}
101 }
102
103 function bridge_has_carrier() {
104 local zone=${1}
105
106 local has_carrier=${EXIT_ERROR}
107
108 local device
109 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
110 device=$(basename ${device})
111 device_exists ${device} || continue
112
113 device_has_carrier ${device} && has_carrier=${EXIT_OK}
114 done
115
116 return ${has_carrier}
117 }