]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.bridge
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / functions / functions.bridge
CommitLineData
837995e6
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
99be6026 5# Copyright (C) 2012 IPFire Network Development Team #
837995e6
MT
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
1c6a4e30 22bridge_create() {
837995e6 23 local bridge=${1}
99be6026
MT
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
837995e6 45
99be6026
MT
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
1c6a4e30 80bridge_delete() {
99be6026 81 local bridge=${1}
837995e6 82 assert isset bridge
99be6026
MT
83
84 device_delete ${bridge}
85}
86
1c6a4e30 87bridge_attach_device() {
99be6026
MT
88 local bridge=${1}
89 assert isset bridge
90
91 local device=${2}
837995e6
MT
92 assert isset device
93
99be6026
MT
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
2320875e 99
99be6026 100 # Check if device exists.
2320875e 101 if ! device_exists ${device}; then
99be6026 102 log ERROR "bridge: could not attach '${device}' to '${bridge}' because device does not exist"
2320875e
MT
103 return ${EXIT_ERROR}
104 fi
837995e6 105
99be6026 106 # If device is already attached, exit silently.
837995e6
MT
107 if listmatch ${device} $(bridge_get_members ${bridge}); then
108 return ${EXIT_OK}
109 fi
110
3ee5ccb1
MT
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
99be6026
MT
115 # Actually connect bridge and device.
116 cmd_quiet ip link set ${device} master ${bridge}
117 local ret=$?
118
119 if [ ${ret} -eq ${EXIT_OK} ]; then
120 log DEBUG "bridge: device '${device}' has been attached to bridge '${bridge}'"
121 else
122 log ERROR "bridge: could not attach device '${device}' to bridge '${bridge}': ${ret}"
123 fi
837995e6 124
99be6026 125 return ${ret}
837995e6
MT
126}
127
1c6a4e30 128bridge_detach_device() {
837995e6 129 local bridge=${1}
837995e6 130 assert isset bridge
99be6026
MT
131
132 local device=${2}
837995e6 133 assert isset device
99be6026
MT
134
135 # Check if bridge exists.
837995e6 136 if ! device_exists ${bridge}; then
99be6026 137 log ERROR "bridge: bridge '${bridge}' does not exist to detach devices from"
837995e6
MT
138 return ${EXIT_ERROR}
139 fi
140
99be6026 141 # Check if device exists.
837995e6 142 if ! device_exists ${device}; then
99be6026
MT
143 log ERROR "bridge: could not detach '${device}' from '${bridge}' because device does not exist"
144 return ${EXIT_ERROR}
000ec6d3
MT
145 fi
146
99be6026 147 # If device is not attched, exit silently.
000ec6d3
MT
148 if ! listmatch ${device} $(bridge_get_members ${bridge}); then
149 return ${EXIT_OK}
837995e6
MT
150 fi
151
99be6026 152 cmd_quiet ip link set ${device} nomaster
73dd577c 153 local ret=$?
99be6026
MT
154
155 if [ ${ret} -eq ${EXIT_OK} ]; then
156 log DEBUG "bridge: device '${device}' has been detached from bridge '${bridge}'"
157 else
158 log ERROR "bridge: could not detach device '${device}' from bridge '${bridge}': ${ret}"
159 fi
837995e6 160
99be6026 161 return ${ret}
837995e6
MT
162}
163
1c6a4e30 164bridge_get_members() {
837995e6
MT
165 local bridge=${1}
166
167 assert isset bridge
168
169 local member
170 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
171 member=$(basename ${member})
172 if device_exists ${member}; then
173 echo "${member}"
174 fi
175 done
176}
177
1c6a4e30 178bridge_is_forwarding() {
837995e6
MT
179 local seconds=45
180 local zone=${1}
181
182 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
183
184 local device
185 while [ ${seconds} -gt 0 ]; do
186 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
187 [ -e "${device}/state" ] || continue
188 if [ "$(<${device}/state)" = "3" ]; then
189 return ${EXIT_OK}
190 fi
191 done
192 sleep 1
193 seconds=$((${seconds} - 1))
194 done
195
196 return ${EXIT_ERROR}
197}
198
1c6a4e30 199bridge_has_carrier() {
837995e6
MT
200 local zone=${1}
201
202 local has_carrier=${EXIT_ERROR}
203
204 local device
205 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
206 device=$(basename ${device})
207 device_exists ${device} || continue
208
209 device_has_carrier ${device} && has_carrier=${EXIT_OK}
210 done
211
212 return ${has_carrier}
213}