]>
Commit | Line | Data |
---|---|---|
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 | 22 | 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=*) | |
2212045f | 33 | address=$(cli_get_val "${1}") |
99be6026 MT |
34 | ;; |
35 | --mtu=*) | |
2212045f | 36 | mtu=$(cli_get_val "${1}") |
99be6026 MT |
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 | 80 | bridge_delete() { |
99be6026 | 81 | local bridge=${1} |
837995e6 | 82 | assert isset bridge |
99be6026 MT |
83 | |
84 | device_delete ${bridge} | |
85 | } | |
86 | ||
1c6a4e30 | 87 | bridge_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. |
8c9205b1 | 107 | if list_match ${device} $(bridge_get_members ${bridge}); then |
837995e6 MT |
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 | 115 | # Actually connect bridge and device. |
0e523702 MT |
116 | if ! device_set_master "${device}" "${bridge}"; then |
117 | log ERROR "Could not attach ${device} to bridge ${bridge}" | |
118 | return ${EXIT_ERROR} | |
99be6026 | 119 | fi |
837995e6 | 120 | |
0e523702 MT |
121 | log DEBUG "${device} has been attached to bridge ${bridge}" |
122 | return ${EXIT_OK} | |
837995e6 MT |
123 | } |
124 | ||
1c6a4e30 | 125 | bridge_detach_device() { |
837995e6 | 126 | local bridge=${1} |
837995e6 | 127 | assert isset bridge |
99be6026 MT |
128 | |
129 | local device=${2} | |
837995e6 | 130 | assert isset device |
99be6026 MT |
131 | |
132 | # Check if bridge exists. | |
837995e6 | 133 | if ! device_exists ${bridge}; then |
99be6026 | 134 | log ERROR "bridge: bridge '${bridge}' does not exist to detach devices from" |
837995e6 MT |
135 | return ${EXIT_ERROR} |
136 | fi | |
137 | ||
99be6026 | 138 | # Check if device exists. |
837995e6 | 139 | if ! device_exists ${device}; then |
99be6026 MT |
140 | log ERROR "bridge: could not detach '${device}' from '${bridge}' because device does not exist" |
141 | return ${EXIT_ERROR} | |
000ec6d3 MT |
142 | fi |
143 | ||
99be6026 | 144 | # If device is not attched, exit silently. |
8c9205b1 | 145 | if ! list_match ${device} $(bridge_get_members ${bridge}); then |
000ec6d3 | 146 | return ${EXIT_OK} |
837995e6 MT |
147 | fi |
148 | ||
99be6026 | 149 | cmd_quiet ip link set ${device} nomaster |
73dd577c | 150 | local ret=$? |
99be6026 MT |
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 | |
837995e6 | 157 | |
99be6026 | 158 | return ${ret} |
837995e6 MT |
159 | } |
160 | ||
1c6a4e30 | 161 | bridge_get_members() { |
837995e6 MT |
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 | ||
1c6a4e30 | 175 | bridge_is_forwarding() { |
837995e6 MT |
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 | ||
1c6a4e30 | 196 | bridge_has_carrier() { |
837995e6 MT |
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 | } |