| 1 |
# bridge (net-misc/bridge-utils) module for net-scripts
|
| 2 |
# Version 1.0.2
|
| 3 |
# Copyright (c) 2004 Gentoo Foundation
|
| 4 |
# Distributed under the terms of the GNU General Public License V2
|
| 5 |
# Contributed by Roy Marples (uberlord@gentoo.org)
|
| 6 |
|
| 7 |
# Fix any potential localisation problems
|
| 8 |
# Note that LC_ALL trumps LC_anything_else according to locale(7)
|
| 9 |
brctl() {
|
| 10 |
LC_ALL=C /sbin/brctl "$@"
|
| 11 |
}
|
| 12 |
|
| 13 |
# char* bridge_provides(void)
|
| 14 |
#
|
| 15 |
# Returns a string to change module definition for starting up
|
| 16 |
bridge_provides() {
|
| 17 |
echo "bridge"
|
| 18 |
}
|
| 19 |
|
| 20 |
# void bridge_depend(void)
|
| 21 |
#
|
| 22 |
# Sets up the dependancies for the module
|
| 23 |
bridge_depend() {
|
| 24 |
after interface tuntap
|
| 25 |
before dhcp
|
| 26 |
}
|
| 27 |
|
| 28 |
# bool bridge_check_installed(void)
|
| 29 |
#
|
| 30 |
# Returns 1 if bridge is installed, otherwise 0
|
| 31 |
bridge_check_installed() {
|
| 32 |
[[ -x /sbin/brctl ]] && return 0
|
| 33 |
${1:-false} && eerror "For bridge support, emerge net-misc/bridge-utils"
|
| 34 |
return 1
|
| 35 |
}
|
| 36 |
|
| 37 |
# bool bridge_check_depends(void)
|
| 38 |
#
|
| 39 |
# Checks to see if we have the needed functions
|
| 40 |
bridge_check_depends() {
|
| 41 |
local f
|
| 42 |
|
| 43 |
for f in interface_variable interface_down interface_del_addresses interface_set_flag; do
|
| 44 |
[[ $( type -t ${f} ) == function ]] && continue
|
| 45 |
eerror "bridge: missing required function ${f}\n"
|
| 46 |
return 1
|
| 47 |
done
|
| 48 |
|
| 49 |
return 0
|
| 50 |
}
|
| 51 |
|
| 52 |
# char* bridge_get_vars(char *interface)
|
| 53 |
#
|
| 54 |
# Returns a string spaced with possible user set
|
| 55 |
# configuration variables
|
| 56 |
bridge_get_vars() {
|
| 57 |
echo "bridge_${1} brctl_${1}"
|
| 58 |
}
|
| 59 |
|
| 60 |
# char* bridge_get_ports(char *interface)
|
| 61 |
#
|
| 62 |
# Returns a string spaced with interfaces added to the given bridge
|
| 63 |
# The awk statement kinda makes sense - would be much easier if the
|
| 64 |
# brctl program exported the bridge name on each line :/
|
| 65 |
bridge_get_ports() {
|
| 66 |
brctl show | awk -v iface=${1} -v got=0 \
|
| 67 |
'$1 == iface || got == 1 { \
|
| 68 |
if ( got == 0 ) \
|
| 69 |
{ print $4; got=1; } \
|
| 70 |
else if ( NF == 1 ) \
|
| 71 |
{ print $1; } \
|
| 72 |
else \
|
| 73 |
{ got = 0; } }' \
|
| 74 |
| xargs
|
| 75 |
}
|
| 76 |
|
| 77 |
# bool bridge_start(char *iface)
|
| 78 |
#
|
| 79 |
# #set up bridge
|
| 80 |
bridge_pre_start() {
|
| 81 |
local iface=${1} ports i e x ifvar=$( interface_variable ${1} )
|
| 82 |
local -a opts
|
| 83 |
eval ports=\"\$\{bridge_${ifvar}\[@\]\}\"
|
| 84 |
|
| 85 |
[[ -z ${ports} ]] && return 0
|
| 86 |
|
| 87 |
# Destroy the bridge if it exists
|
| 88 |
bridge_stop ${iface}
|
| 89 |
|
| 90 |
ebegin "Creating bridge ${iface}"
|
| 91 |
e=$( brctl addbr ${iface} 2>&1 )
|
| 92 |
if [[ -n ${e} ]]; then
|
| 93 |
if [[ ${e} == "br_add_bridge: Package not installed" ]]; then
|
| 94 |
eend 1 "Bridging (802.1d) support is not present in this kernel"
|
| 95 |
else
|
| 96 |
eend 1 "${e}"
|
| 97 |
fi
|
| 98 |
return 1
|
| 99 |
fi
|
| 100 |
|
| 101 |
eval opts=( \"\$\{brctl_${ifvar}\[@\]\}\" )
|
| 102 |
for (( i=0; i<${#opts[@]}; i++ )); do
|
| 103 |
x=${opts[i]/ / ${iface} }
|
| 104 |
[[ ${x} == ${opts[i]} ]] && x="${x} ${iface}"
|
| 105 |
e=$( brctl ${x} 2>&1 1>/dev/null )
|
| 106 |
[[ -n ${e} ]] && ewarn "${e}"
|
| 107 |
done
|
| 108 |
eend 0
|
| 109 |
|
| 110 |
eindent
|
| 111 |
for i in ${ports}; do
|
| 112 |
# If the interface does not exist, it may be because another
|
| 113 |
# module needs to create it
|
| 114 |
net_start ${i} || continue
|
| 115 |
|
| 116 |
if ! interface_exists ${i} ; then
|
| 117 |
ewarn "interface ${i} does not exist"
|
| 118 |
continue
|
| 119 |
fi
|
| 120 |
|
| 121 |
ebegin "Adding port ${i}"
|
| 122 |
e=$( brctl addif ${iface} ${i} 2>&1 \
|
| 123 |
&& interface_del_addresses ${i} \
|
| 124 |
&& interface_set_flag ${i} promisc true \
|
| 125 |
&& interface_up ${i} )
|
| 126 |
if [[ -n ${e} ]]; then
|
| 127 |
eend 1 "${e}"
|
| 128 |
return 1
|
| 129 |
fi
|
| 130 |
done
|
| 131 |
eoutdent
|
| 132 |
|
| 133 |
return 0
|
| 134 |
}
|
| 135 |
|
| 136 |
# bool bridge_stop(char *iface)
|
| 137 |
#
|
| 138 |
# Removes the device
|
| 139 |
# returns 0
|
| 140 |
bridge_stop() {
|
| 141 |
local iface=${1} is_bridge=$( brctl show | awk -v iface=${1} '$1 == iface {print $1}' )
|
| 142 |
|
| 143 |
bridge_check_installed || return 1
|
| 144 |
[[ -z ${is_bridge} ]] && return 1
|
| 145 |
|
| 146 |
ebegin "Destroying bridge ${iface}"
|
| 147 |
interface_down ${iface}
|
| 148 |
|
| 149 |
local ports=$( bridge_get_ports ${1} ) i
|
| 150 |
eindent
|
| 151 |
for i in ${ports}; do
|
| 152 |
ebegin "Removing port ${i}"
|
| 153 |
interface_down ${i}
|
| 154 |
interface_set_flag ${i} promisc false
|
| 155 |
brctl delif ${iface} ${i} &>${devnull}
|
| 156 |
eend $?
|
| 157 |
done
|
| 158 |
eoutdent
|
| 159 |
|
| 160 |
brctl delbr ${iface} &>${devnull}
|
| 161 |
|
| 162 |
eend 0
|
| 163 |
return 0
|
| 164 |
}
|