]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.bridge
Use autotools.
[people/ms/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
99be6026 22function bridge_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
80function bridge_delete() {
81 local bridge=${1}
837995e6 82 assert isset bridge
99be6026
MT
83
84 device_delete ${bridge}
85}
86
87function bridge_attach_device() {
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
99be6026
MT
111 # Actually connect bridge and device.
112 cmd_quiet ip link set ${device} master ${bridge}
113 local ret=$?
114
115 if [ ${ret} -eq ${EXIT_OK} ]; then
116 log DEBUG "bridge: device '${device}' has been attached to bridge '${bridge}'"
117 else
118 log ERROR "bridge: could not attach device '${device}' to bridge '${bridge}': ${ret}"
119 fi
837995e6 120
99be6026 121 return ${ret}
837995e6
MT
122}
123
124function bridge_detach_device() {
125 local bridge=${1}
837995e6 126 assert isset bridge
99be6026
MT
127
128 local device=${2}
837995e6 129 assert isset device
99be6026
MT
130
131 # Check if bridge exists.
837995e6 132 if ! device_exists ${bridge}; then
99be6026 133 log ERROR "bridge: bridge '${bridge}' does not exist to detach devices from"
837995e6
MT
134 return ${EXIT_ERROR}
135 fi
136
99be6026 137 # Check if device exists.
837995e6 138 if ! device_exists ${device}; then
99be6026
MT
139 log ERROR "bridge: could not detach '${device}' from '${bridge}' because device does not exist"
140 return ${EXIT_ERROR}
000ec6d3
MT
141 fi
142
99be6026 143 # If device is not attched, exit silently.
000ec6d3
MT
144 if ! listmatch ${device} $(bridge_get_members ${bridge}); then
145 return ${EXIT_OK}
837995e6
MT
146 fi
147
99be6026 148 cmd_quiet ip link set ${device} nomaster
73dd577c 149 local ret=$?
99be6026
MT
150
151 if [ ${ret} -eq ${EXIT_OK} ]; then
152 log DEBUG "bridge: device '${device}' has been detached from bridge '${bridge}'"
153 else
154 log ERROR "bridge: could not detach device '${device}' from bridge '${bridge}': ${ret}"
155 fi
837995e6 156
99be6026 157 return ${ret}
837995e6
MT
158}
159
160function bridge_get_members() {
161 local bridge=${1}
162
163 assert isset bridge
164
165 local member
166 for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
167 member=$(basename ${member})
168 if device_exists ${member}; then
169 echo "${member}"
170 fi
171 done
172}
173
174function bridge_is_forwarding() {
175 local seconds=45
176 local zone=${1}
177
178 bridge_has_carrier ${zone} || return ${EXIT_ERROR}
179
180 local device
181 while [ ${seconds} -gt 0 ]; do
182 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
183 [ -e "${device}/state" ] || continue
184 if [ "$(<${device}/state)" = "3" ]; then
185 return ${EXIT_OK}
186 fi
187 done
188 sleep 1
189 seconds=$((${seconds} - 1))
190 done
191
192 return ${EXIT_ERROR}
193}
194
195function bridge_has_carrier() {
196 local zone=${1}
197
198 local has_carrier=${EXIT_ERROR}
199
200 local device
201 for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
202 device=$(basename ${device})
203 device_exists ${device} || continue
204
205 device_has_carrier ${device} && has_carrier=${EXIT_OK}
206 done
207
208 return ${has_carrier}
209}