Project:
IPFire
Code Location:
git://git.ipfire.org/network.gitmaster
/
functions.bridge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2012 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################### function bridge_create() { local bridge=${1} assert isset bridge shift local address local mtu while [ $# -gt 0 ]; do case "${1}" in --address=*) address=$(cli_get_val ${1}) ;; --mtu=*) mtu=$(cli_get_val ${1}) ;; *) error "Unrecognized argument: ${1}" return ${EXIT_ERROR} ;; esac shift done if device_exists ${bridge}; then log ERROR "bridge: bridge '${bridge}' does already exist" return ${EXIT_ERROR} fi # Build the ip command. local command="ip link add name ${bridge}" # Add address, if we know it. if ismac address; then command="${command} address ${address}" fi # Add MTU if it has been set. if isinteger mtu; then command="${command} mtu ${mtu}" fi # Last argument is the device type. command="${command} type bridge" # Run the command. cmd_quiet ${command} local ret=$? if [ ${ret} -eq ${EXIT_OK} ]; then log DEBUG "bridge: bridge '${bridge}' has been created" else log ERROR "bridge: Could not create bridge '${bridge}': ${ret}" fi return ${ret} } function bridge_delete() { local bridge=${1} assert isset bridge device_delete ${bridge} } function bridge_attach_device() { local bridge=${1} assert isset bridge local device=${2} assert isset device # Check if bridge exists. if ! device_exists ${bridge}; then log ERROR "bridge: bridge '${bridge}' does not exist to attach devices to" return ${EXIT_ERROR} fi # Check if device exists. if ! device_exists ${device}; then log ERROR "bridge: could not attach '${device}' to '${bridge}' because device does not exist" return ${EXIT_ERROR} fi # If device is already attached, exit silently. if listmatch ${device} $(bridge_get_members ${bridge}); then return ${EXIT_OK} fi # Actually connect bridge and device. cmd_quiet ip link set ${device} master ${bridge} local ret=$? if [ ${ret} -eq ${EXIT_OK} ]; then log DEBUG "bridge: device '${device}' has been attached to bridge '${bridge}'" else log ERROR "bridge: could not attach device '${device}' to bridge '${bridge}': ${ret}" fi return ${ret} } function bridge_detach_device() { local bridge=${1} assert isset bridge local device=${2} assert isset device # Check if bridge exists. if ! device_exists ${bridge}; then log ERROR "bridge: bridge '${bridge}' does not exist to detach devices from" return ${EXIT_ERROR} fi # Check if device exists. if ! device_exists ${device}; then log ERROR "bridge: could not detach '${device}' from '${bridge}' because device does not exist" return ${EXIT_ERROR} fi # If device is not attched, exit silently. if ! listmatch ${device} $(bridge_get_members ${bridge}); then return ${EXIT_OK} fi cmd_quiet ip link set ${device} nomaster local ret=$? if [ ${ret} -eq ${EXIT_OK} ]; then log DEBUG "bridge: device '${device}' has been detached from bridge '${bridge}'" else log ERROR "bridge: could not detach device '${device}' from bridge '${bridge}': ${ret}" fi return ${ret} } function bridge_get_members() { local bridge=${1} assert isset bridge local member for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do member=$(basename ${member}) if device_exists ${member}; then echo "${member}" fi done } function bridge_is_forwarding() { local seconds=45 local zone=${1} bridge_has_carrier ${zone} || return ${EXIT_ERROR} local device while [ ${seconds} -gt 0 ]; do for device in ${SYS_CLASS_NET}/${zone}/brif/*; do [ -e "${device}/state" ] || continue if [ "$(<${device}/state)" = "3" ]; then return ${EXIT_OK} fi done sleep 1 seconds=$((${seconds} - 1)) done return ${EXIT_ERROR} } function bridge_has_carrier() { local zone=${1} local has_carrier=${EXIT_ERROR} local device for device in ${SYS_CLASS_NET}/${zone}/brif/*; do device=$(basename ${device}) device_exists ${device} || continue device_has_carrier ${device} && has_carrier=${EXIT_OK} done return ${has_carrier} }
