]>
git.ipfire.org Git - people/ms/network.git/blob - functions.bridge
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 function bridge_create
() {
30 while [ $# -gt 0 ]; do
33 address
=$
(cli_get_val
${1})
36 mtu
=$
(cli_get_val
${1})
39 error
"Unrecognized argument: ${1}"
46 if device_exists
${bridge}; then
47 log ERROR
"bridge: bridge '${bridge}' does already exist"
51 # Build the ip command.
52 local command="ip link add name ${bridge}"
54 # Add address, if we know it.
55 if ismac address
; then
56 command="${command} address ${address}"
59 # Add MTU if it has been set.
60 if isinteger mtu
; then
61 command="${command} mtu ${mtu}"
64 # Last argument is the device type.
65 command="${command} type bridge"
71 if [ ${ret} -eq ${EXIT_OK} ]; then
72 log DEBUG
"bridge: bridge '${bridge}' has been created"
74 log ERROR
"bridge: Could not create bridge '${bridge}': ${ret}"
80 function bridge_delete
() {
84 device_delete
${bridge}
87 function bridge_attach_device
() {
94 # Check if bridge exists.
95 if ! device_exists
${bridge}; then
96 log ERROR
"bridge: bridge '${bridge}' does not exist to attach devices to"
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"
106 # If device is already attached, exit silently.
107 if listmatch
${device} $
(bridge_get_members
${bridge}); then
111 # Actually connect bridge and device.
112 cmd_quiet ip link
set ${device} master
${bridge}
115 if [ ${ret} -eq ${EXIT_OK} ]; then
116 log DEBUG
"bridge: device '${device}' has been attached to bridge '${bridge}'"
118 log ERROR
"bridge: could not attach device '${device}' to bridge '${bridge}': ${ret}"
124 function bridge_detach_device
() {
131 # Check if bridge exists.
132 if ! device_exists
${bridge}; then
133 log ERROR
"bridge: bridge '${bridge}' does not exist to detach devices from"
137 # Check if device exists.
138 if ! device_exists
${device}; then
139 log ERROR
"bridge: could not detach '${device}' from '${bridge}' because device does not exist"
143 # If device is not attched, exit silently.
144 if ! listmatch
${device} $
(bridge_get_members
${bridge}); then
148 cmd_quiet ip link
set ${device} nomaster
151 if [ ${ret} -eq ${EXIT_OK} ]; then
152 log DEBUG
"bridge: device '${device}' has been detached from bridge '${bridge}'"
154 log ERROR
"bridge: could not detach device '${device}' from bridge '${bridge}': ${ret}"
160 function bridge_get_members
() {
166 for member
in ${SYS_CLASS_NET}/${bridge}/brif
/*; do
167 member
=$
(basename ${member})
168 if device_exists
${member}; then
174 function bridge_is_forwarding
() {
178 bridge_has_carrier
${zone} ||
return ${EXIT_ERROR}
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
189 seconds
=$
((${seconds} - 1))
195 function bridge_has_carrier
() {
198 local has_carrier
=${EXIT_ERROR}
201 for device
in ${SYS_CLASS_NET}/${zone}/brif
/*; do
202 device
=$
(basename ${device})
203 device_exists
${device} ||
continue
205 device_has_carrier
${device} && has_carrier
=${EXIT_OK}
208 return ${has_carrier}