| 1 |
#!/bin/sh |
| 2 |
[ -n "$VERBOSE" ] && set -x |
| 3 |
|
| 4 |
# Copyright (C) 2012, 2020 Natanael Copa <[email protected]> |
| 5 |
# Copyright (C) 2020 Ariadne Conill <[email protected]> |
| 6 |
# Copyright (C) 2020 Maximilian Wilhelm <[email protected]> |
| 7 |
# |
| 8 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
# purpose with or without fee is hereby granted, provided that the above |
| 10 |
# copyright notice and this permission notice appear in all copies. |
| 11 |
# |
| 12 |
# This software is provided 'as is' and without any warranty, express or |
| 13 |
# implied. In no event shall the authors be liable for any damages arising |
| 14 |
# from the use of this software. |
| 15 |
|
| 16 |
################################################################################ |
| 17 |
# Bridge management functions # |
| 18 |
################################################################################ |
| 19 |
|
| 20 |
all_ports_exist() { |
| 21 |
local i= |
| 22 |
for i in "$@"; do |
| 23 |
[ -d /sys/class/net/$i ] || return 1 |
| 24 |
done |
| 25 |
return 0 |
| 26 |
} |
| 27 |
|
| 28 |
wait_ports() { |
| 29 |
local timeout= waitports= |
| 30 |
[ -z "$IF_BRIDGE_WAITPORT" ] && return 0 |
| 31 |
set -- $IF_BRIDGE_WAITPORT |
| 32 |
timeout="$1" |
| 33 |
shift |
| 34 |
waitports="$@" |
| 35 |
[ -z "$waitports" ] && waitports="$PORTS" |
| 36 |
while ! all_ports_exist $waitports; do |
| 37 |
[ "$timeout" -eq 0 ] && return 0 |
| 38 |
timeout=$(($timeout - 1)) |
| 39 |
sleep 1 |
| 40 |
done |
| 41 |
} |
| 42 |
|
| 43 |
all_ports() { |
| 44 |
local i= |
| 45 |
for i in /sys/class/net/*/ifindex; do |
| 46 |
i=${i%/*} |
| 47 |
i=${i##*/} |
| 48 |
case "$i" in |
| 49 |
lo|$IFACE) continue;; |
| 50 |
*) echo $i;; |
| 51 |
esac |
| 52 |
done |
| 53 |
} |
| 54 |
|
| 55 |
add_ports() { |
| 56 |
local port= |
| 57 |
for port in $PORTS; do |
| 58 |
if [ -n "$IF_BRIDGE_HW" ]; then |
| 59 |
ip link set dev $port addr $IF_BRIDGE_HW |
| 60 |
fi |
| 61 |
ip link set dev $port master $IFACE && ip link set dev $port up |
| 62 |
done |
| 63 |
} |
| 64 |
|
| 65 |
del_ports() { |
| 66 |
local port= |
| 67 |
for port in $PORTS; do |
| 68 |
ip link set dev $port down |
| 69 |
ip link set dev $port nomaster |
| 70 |
done |
| 71 |
} |
| 72 |
|
| 73 |
set_bridge_opts_brctl() { |
| 74 |
[ -n "$IF_BRIDGE_AGEING" ] \ |
| 75 |
&& brctl setageing $IFACE $IF_BRIDGE_AGEING |
| 76 |
[ -n "$IF_BRIDGE_BRIDGEPRIO" ] \ |
| 77 |
&& brctl setbridgeprio $IFACE $IF_BRIDGE_BRIDGEPRIO |
| 78 |
[ -n "$IF_BRIDGE_FD" ] \ |
| 79 |
&& brctl setfd $IFACE $IF_BRIDGE_FD |
| 80 |
[ -n "$IF_BRIDGE_HELLO" ] \ |
| 81 |
&& brctl sethello $IFACE $IF_BRIDGE_HELLO |
| 82 |
[ -n "$IF_BRIDGE_MAXAGE" ] \ |
| 83 |
&& brctl setmaxage $IFACE $IF_BRIDGE_MAXAGE |
| 84 |
[ -n "$IF_BRIDGE_PATHCOST" ] \ |
| 85 |
&& brctl setpathcost $IFACE $IF_BRIDGE_PATHCOST |
| 86 |
[ -n "$IF_BRIDGE_PORTPRIO" ] \ |
| 87 |
&& brctl setportprio $IFACE $IF_BRIDGE_PORTPRIO |
| 88 |
[ -n "$IF_BRIDGE_STP" ] \ |
| 89 |
&& brctl stp $IFACE $IF_BRIDGE_STP |
| 90 |
} |
| 91 |
|
| 92 |
yesno() { |
| 93 |
case "$1" in |
| 94 |
yes|YES|true|TRUE|1) |
| 95 |
echo 1 |
| 96 |
;; |
| 97 |
*) |
| 98 |
echo 0 |
| 99 |
;; |
| 100 |
esac |
| 101 |
} |
| 102 |
|
| 103 |
set_bridge_opts_iproute2() { |
| 104 |
[ -n "$IF_BRIDGE_AGEING" ] \ |
| 105 |
&& ip link set dev $IFACE type bridge ageing_time $IF_BRIDGE_AGEING |
| 106 |
[ -n "$IF_BRIDGE_BRIDGEPRIO" ] \ |
| 107 |
&& ip link set dev $IFACE type bridge priority $IF_BRIDGE_BRIDGEPRIO |
| 108 |
[ -n "$IF_BRIDGE_FD" ] \ |
| 109 |
&& ip link set dev $IFACE type bridge forward_delay $IF_BRIDGE_FD |
| 110 |
[ -n "$IF_BRIDGE_HELLO" ] \ |
| 111 |
&& ip link set dev $IFACE type bridge hello_time $IF_BRIDGE_HELLO |
| 112 |
[ -n "$IF_BRIDGE_MAXAGE" ] \ |
| 113 |
&& ip link set dev $IFACE type bridge max_age $IF_BRIDGE_MAXAGE |
| 114 |
[ -n "$IF_BRIDGE_PATHCOST" ] \ |
| 115 |
&& bridge link set dev $IFACE cost $IF_BRIDGE_PATHCOST |
| 116 |
[ -n "$IF_BRIDGE_PORTPRIO" ] \ |
| 117 |
&& bridge link set dev $IFACE priority $IF_BRIDGE_PORTPRIO |
| 118 |
[ -n "$IF_BRIDGE_STP" ] \ |
| 119 |
&& ip link set dev $IFACE type bridge stp $(yesno $IF_BRIDGE_STP) |
| 120 |
[ -n "$IF_BRIDGE_VLAN_AWARE" ] \ |
| 121 |
&& ip link set dev $IFACE type bridge vlan_filtering $(yesno $IF_BRIDGE_VLAN_AWARE) |
| 122 |
} |
| 123 |
|
| 124 |
set_bridge_opts() { |
| 125 |
[ -x /sbin/bridge ] && set_bridge_opts_iproute2 && return 0 |
| 126 |
[ -x /sbin/brctl ] && set_bridge_opts_brctl && return 0 |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
all_ports_ready() { |
| 131 |
local port= |
| 132 |
for port in $PORTS; do |
| 133 |
case $(cat /sys/class/net/$IFACE/brif/$port/state) in |
| 134 |
""|0|3) ;; # 0 = disabled, 3 = forwarding |
| 135 |
[12]) return 1;; |
| 136 |
esac |
| 137 |
done |
| 138 |
return 0 |
| 139 |
} |
| 140 |
|
| 141 |
find_maxwait() { |
| 142 |
awk '{printf("%.f\n", 2 * $0 / 100); }' \ |
| 143 |
/sys/class/net/$IFACE/bridge/forward_delay |
| 144 |
} |
| 145 |
|
| 146 |
wait_bridge() { |
| 147 |
local timeout=$IF_BRIDGE_MAXWAIT |
| 148 |
if [ -z "$timeout" ]; then |
| 149 |
timeout=$(find_maxwait) |
| 150 |
fi |
| 151 |
ip link set dev $IFACE up |
| 152 |
while ! all_ports_ready; do |
| 153 |
[ $timeout -eq 0 ] && break |
| 154 |
timeout=$(($timeout - 1)) |
| 155 |
sleep 1 |
| 156 |
done |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
################################################################################ |
| 161 |
# Bridge port management functions # |
| 162 |
################################################################################ |
| 163 |
|
| 164 |
configure_access_port() { |
| 165 |
port="$1" |
| 166 |
vlan="$2" |
| 167 |
self="$3" |
| 168 |
|
| 169 |
# Cleans all existing VLANs (probably at least VLAN 1) |
| 170 |
bridge vlan show dev ${port} | tail -n +2 | grep -v '^$' | sed -e "s/^${port}//" | while read vid flags; do |
| 171 |
bridge vlan del vid "${vid}" dev "${port}" ${self} |
| 172 |
done |
| 173 |
|
| 174 |
bridge vlan add vid "${vlan}" pvid untagged dev "${port}" ${self} |
| 175 |
} |
| 176 |
|
| 177 |
configure_trunk_port() { |
| 178 |
port="$1" |
| 179 |
self="$2" |
| 180 |
|
| 181 |
# Working on the bridge itself? |
| 182 |
if [ "${self}" ]; then |
| 183 |
allow_untagged="${IF_BRIDGE_ALLOW_UNTAGGED}" |
| 184 |
pvid="${IF_BRIDGE_PVID}" |
| 185 |
vids="${IF_BRIDGE_VIDS}" |
| 186 |
else |
| 187 |
allow_untagged=$(ifquery -p bridge-allow-untagged ${port} 2>/dev/null || true) |
| 188 |
pvid=$(ifquery -p bridge-pvid ${port} 2>/dev/null || true) |
| 189 |
vids=$(ifquery -p bridge-vids ${port} 2>/dev/null || true) |
| 190 |
fi |
| 191 |
|
| 192 |
# If bridge-allow-untagged if set to off, remove untagged VLAN. If it's |
| 193 |
# one of our bridge-vids, it will be set again later. |
| 194 |
if [ "${allow_untagged}" -a "$(yesno ${allow_untagged})" = 0 ]; then |
| 195 |
untagged_vid=$(bridge vlan show dev ${port} | tail -n +2 | grep -v '^$' | sed -e "s/^${port}//" | awk '/Untagged/ { print $1 }') |
| 196 |
if [ "${untagged_vid}" ]; then |
| 197 |
bridge vlan del vid "${untagged_vid}" dev "${port}" ${self} |
| 198 |
fi |
| 199 |
fi |
| 200 |
|
| 201 |
# The vlan specified is to be considered a PVID at ingress. |
| 202 |
# Any untagged frames will be assigned to this VLAN. |
| 203 |
if [ "${pvid}" ]; then |
| 204 |
cur_pvid=$(bridge vlan show dev ${port} | tail -n +2 | grep -v '^$' | sed -e "s/^${port}//" | awk '/PVID/ { print $1 }') |
| 205 |
if [ "${cur_pvid}" ]; then |
| 206 |
bridge vlan del vid ${cur_pvid} dev "${port}" ${self} |
| 207 |
fi |
| 208 |
|
| 209 |
bridge vlan add vid "${pvid}" dev "${port}" pvid untagged ${self} |
| 210 |
fi |
| 211 |
|
| 212 |
# Add regular tagged VLANs |
| 213 |
for vid in ${vids}; do |
| 214 |
bridge vlan add vid $vid dev "${port}" ${self} |
| 215 |
done |
| 216 |
} |
| 217 |
|
| 218 |
# Configure VLANs on the bridge interface itself |
| 219 |
set_bridge_vlans() { |
| 220 |
# Shall the bridge interface be an untagged port? |
| 221 |
if [ "${IF_BRIDGE_ACCESS}" ]; then |
| 222 |
configure_access_port "${IFACE}" "${IF_BRIDGE_ACCESS}" "self" |
| 223 |
|
| 224 |
# Configure bridge interface as trunk port |
| 225 |
else |
| 226 |
configure_trunk_port "${IFACE}" "self" |
| 227 |
fi |
| 228 |
} |
| 229 |
|
| 230 |
# Configure VLANs on the bridge-ports |
| 231 |
set_bridge_port_vlans() { |
| 232 |
for port in ${PORTS}; do |
| 233 |
access_vlan=$(ifquery -p bridge-access ${port} 2>/dev/null || true) |
| 234 |
|
| 235 |
# Shall this prot interface be an untagged port? |
| 236 |
if [ "${access_vlan}" ]; then |
| 237 |
configure_access_port "${port}" "${access_vlan}" |
| 238 |
|
| 239 |
# Configure port as trunk |
| 240 |
else |
| 241 |
configure_trunk_port "${port}" |
| 242 |
fi |
| 243 |
done |
| 244 |
} |
| 245 |
|
| 246 |
case "$IF_BRIDGE_PORTS" in |
| 247 |
"") ;; |
| 248 |
none) PORTS="";; |
| 249 |
all) PORTS=$(all_ports);; |
| 250 |
*) PORTS="$IF_BRIDGE_PORTS";; |
| 251 |
esac |
| 252 |
|
| 253 |
case "$PHASE" in |
| 254 |
depend) |
| 255 |
# Called for the bridge interface |
| 256 |
if [ "${IF_BRIDGE_PORTS}" ]; then |
| 257 |
echo "$PORTS" |
| 258 |
fi |
| 259 |
;; |
| 260 |
|
| 261 |
create) |
| 262 |
# Called for the bridge interface |
| 263 |
if [ "${IF_BRIDGE_PORTS}" -a ! -d "/sys/class/net/${IFACE}" ]; then |
| 264 |
ip link add "${IFACE}" type bridge |
| 265 |
fi |
| 266 |
;; |
| 267 |
|
| 268 |
pre-up) |
| 269 |
# Called for the bridge interface |
| 270 |
if [ "${IF_BRIDGE_PORTS}" ]; then |
| 271 |
wait_ports |
| 272 |
set_bridge_opts |
| 273 |
set_bridge_vlans |
| 274 |
add_ports |
| 275 |
set_bridge_port_vlans |
| 276 |
wait_bridge |
| 277 |
|
| 278 |
# Called for a bridge member port |
| 279 |
elif [ "${IF_BRIDGE_VIDS}" -o "${IF_BRIDGE_PVID}" -o "${IF_BRIDGE_ACCESS}" -o "${IF_BRIDGE_ALLOW_UNTAGGED}" ]; then |
| 280 |
# Eventually we want to configure VLAN settings of member ports here. |
| 281 |
# The current execution model does not allow this, so this is a no-op |
| 282 |
# for now and we work around this by configuring ports while configuring |
| 283 |
# the bridge. |
| 284 |
true |
| 285 |
fi |
| 286 |
;; |
| 287 |
|
| 288 |
post-down) |
| 289 |
# Called for the bridge interface |
| 290 |
if [ "${IF_BRIDGE_PORTS}" ]; then |
| 291 |
del_ports |
| 292 |
ip link set dev $IFACE down |
| 293 |
fi |
| 294 |
;; |
| 295 |
|
| 296 |
destroy) |
| 297 |
# Called for the bridge interface |
| 298 |
if [ "${IF_BRIDGE_PORTS}" -a -d "/sys/class/net/${IFACE}" ]; then |
| 299 |
ip link del "${IFACE}" |
| 300 |
fi |
| 301 |
;; |
| 302 |
esac |