]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.bridge
5b817fbb355ad1639befe0bfb907c4cc769a767c
[people/stevee/network.git] / src / functions / functions.bridge
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 bridge_create() {
23 local bridge=${1}
24 assert isset bridge
25 shift
26
27 local address
28 local mtu
29
30 while [ $# -gt 0 ]; do
31 case "${1}" in
32 --address=*)
33 address=$(cli_get_val ${1})
34 ;;
35 --mtu=*)
36 mtu=$(cli_get_val ${1})
37 ;;
38 *)
39 error "Unrecognized argument: ${1}"
40 return ${EXIT_ERROR}
41 ;;
42 esac
43 shift
44 done
45
46 if device_exists ${bridge}; then
47 log ERROR "bridge: bridge '${bridge}' does already exist"
48 return ${EXIT_ERROR}
49 fi
50
51 # Build the ip command.
52 local command="ip link add name ${bridge}"
53
54 # Add address, if we know it.
55 if ismac address; then
56 command="${command} address ${address}"
57 fi
58
59 # Add MTU if it has been set.
60 if isinteger mtu; then
61 command="${command} mtu ${mtu}"
62 fi
63
64 # Last argument is the device type.
65 command="${command} type bridge"
66
67 # Run the command.
68 cmd_quiet ${command}
69 local ret=$?
70
71 if [ ${ret} -eq ${EXIT_OK} ]; then
72 log DEBUG "bridge: bridge '${bridge}' has been created"
73 else
74 log ERROR "bridge: Could not create bridge '${bridge}': ${ret}"
75 fi
76
77 return ${ret}
78 }
79
80 bridge_delete() {
81 local bridge=${1}
82 assert isset bridge
83
84 device_delete ${bridge}
85 }
86
87 bridge_attach_device() {
88 local bridge=${1}
89 assert isset bridge
90
91 local device=${2}
92 assert isset device
93
94 # Check if bridge exists.
95 if ! device_exists ${bridge}; then
96 log ERROR "bridge: bridge '${bridge}' does not exist to attach devices to"
97 return ${EXIT_ERROR}
98 fi
99
100 # Check if device exists.
101 if ! device_exists ${device}; then
102 log ERROR "bridge: could not attach '${device}' to '${bridge}' because device does not exist"
103 return ${EXIT_ERROR}
104 fi
105
106 # If device is already attached, exit silently.
107 if list_match ${device} $(bridge_get_members ${bridge}); then
108 return ${EXIT_OK}
109 fi
110
111 # Make sure that the MTU of the device that is to be attached
112 # to the bridge matches the MTU of the bridge.
113 device_adjust_mtu "${device}" "${bridge}"
114
115 # Actually connect bridge and device.
116 if ! device_set_master "${device}" "${bridge}"; then
117 log ERROR "Could not attach ${device} to bridge ${bridge}"
118 return ${EXIT_ERROR}
119 fi
120
121 log DEBUG "${device} has been attached to bridge ${bridge}"
122 return ${EXIT_OK}
123 }
124
125 bridge_detach_device() {
126 local bridge=${1}
127 assert isset bridge
128
129 local device=${2}
130 assert isset device
131
132 # Check if bridge exists.
133 if ! device_exists ${bridge}; then
134 log ERROR "bridge: bridge '${bridge}' does not exist to detach devices from"
135 return ${EXIT_ERROR}
136 fi
137
138 # Check if device exists.
139 if ! device_exists ${device}; then
140 log ERROR "bridge: could not detach '${device}' from '${bridge}' because device does not exist"
141 return ${EXIT_ERROR}
142 fi
143
144 # If device is not attched, exit silently.
145 if ! list_match ${device} $(bridge_get_members ${bridge}); then
146 return ${EXIT_OK}
147 fi
148
149 cmd_quiet ip link set ${device} nomaster
150 local ret=$?
151
152 if [ ${ret} -eq ${EXIT_OK} ]; then
153 log DEBUG "bridge: device '${device}' has been detached from bridge '${bridge}'"
154 else
155 log ERROR "bridge: could not detach device '${device}' from bridge '${bridge}': ${ret}"
156 fi
157
158 return ${ret}
159 }
160
161 bridge_get_members() {
162 local bridge=${1}
163
164 assert isset bridge
165
166 local member
167 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
168 member=$(basename ${member})
169 if device_exists ${member}; then
170 echo "${member}"
171 fi
172 done
173 }
174
175 bridge_is_forwarding() {
176 local seconds=45
177 local zone=${1}
178
179 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
180
181 local device
182 while [ ${seconds} -gt 0 ]; do
183 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
184 [ -e "${device}/state" ] || continue
185 if [ "$(<${device}/state)" = "3" ]; then
186 return ${EXIT_OK}
187 fi
188 done
189 sleep 1
190 seconds=$((${seconds} - 1))
191 done
192
193 return ${EXIT_ERROR}
194 }
195
196 bridge_has_carrier() {
197 local zone=${1}
198
199 local has_carrier=${EXIT_ERROR}
200
201 local device
202 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
203 device=$(basename ${device})
204 device_exists ${device} || continue
205
206 device_has_carrier ${device} && has_carrier=${EXIT_OK}
207 done
208
209 return ${has_carrier}
210 }