]> git.ipfire.org Git - people/stevee/network.git/blob - functions.bridge
ipv{6,4}: Simplify some functions and introduce new ones.
[people/stevee/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
31 if ! device_exists ${device}; then
32 log WARN "Could not attach '${device}' to '${bridge}' becase device does not exist."
33 return ${EXIT_ERROR}
34 fi
35
36 # If device is already attached, exit silently
37 if listmatch ${device} $(bridge_get_members ${bridge}); then
38 return ${EXIT_OK}
39 fi
40
41 log INFO "Attaching device '${device}' to bridge '${bridge}'."
42
43 brctl addif ${bridge} ${device}
44 }
45
46 function bridge_detach_device() {
47 local bridge=${1}
48 local device=${2}
49
50 assert isset bridge
51 assert isset device
52
53 if ! device_exists ${bridge}; then
54 error "Bridge '${bridge}' does not exist."
55 return ${EXIT_ERROR}
56 fi
57
58 if ! device_exists ${device}; then
59 return ${EXIT_OK}
60 fi
61
62 # If device is not attached, exit silently
63 if ! listmatch ${device} $(bridge_get_members ${bridge}); then
64 return ${EXIT_OK}
65 fi
66
67 log INFO "Detaching device '${device}' from bridge '${bridge}'."
68
69 brctl delif ${bridge} ${device}
70 }
71
72 function bridge_get_members() {
73 local bridge=${1}
74
75 assert isset bridge
76
77 local member
78 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
79 member=$(basename ${member})
80 if device_exists ${member}; then
81 echo "${member}"
82 fi
83 done
84 }
85
86 function bridge_is_forwarding() {
87 local seconds=45
88 local zone=${1}
89
90 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
91
92 local device
93 while [ ${seconds} -gt 0 ]; do
94 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
95 [ -e "${device}/state" ] || continue
96 if [ "$(<${device}/state)" = "3" ]; then
97 return ${EXIT_OK}
98 fi
99 done
100 sleep 1
101 seconds=$((${seconds} - 1))
102 done
103
104 return ${EXIT_ERROR}
105 }
106
107 function bridge_has_carrier() {
108 local zone=${1}
109
110 local has_carrier=${EXIT_ERROR}
111
112 local device
113 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
114 device=$(basename ${device})
115 device_exists ${device} || continue
116
117 device_has_carrier ${device} && has_carrier=${EXIT_OK}
118 done
119
120 return ${has_carrier}
121 }