-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinet.sh
424 lines (336 loc) · 10.5 KB
/
pinet.sh
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#! /bin/sh
#
# Manage network bridge for QEMU emulator
#
# SPDX-License-Identifier: Unlicense
#
# Blame: Jordan Hrycaj <[email protected]>
readonly self=`basename $0 .sh`
readonly prefix=`dirname $0`
# -----------------------------------------------------------------------------
# Setup
# -----------------------------------------------------------------------------
set -e
# Command line usage message
ARGSUSAGE="[options] [--] [instance]"
. $prefix/lib/variables.sh
. $prefix/lib/functions.sh
. $prefix/lib/sudo-bridge.sh
# Reset global variables
unset ADDBRIDGE RMBRIDGE OUTBOUND INBOUND BRINFO
readonly short_stdopts="Ddvh"
readonly long_stdopts="dry-run,debug,verbose,help"
readonly bridge=q0br
readonly tappfx=q0tap
readonly wlanpfx=q1tap
set +e
# -----------------------------------------------------------------------------
# Command line helper functions
# -----------------------------------------------------------------------------
pinet_debug_vardump () {
[ -z "$DEBUG" ] ||
mesg `dump_vars \
HELP DEBUG NOISY RUNPFX OPTIONS \
ADDBRIDGE RMBRIDGE OUTBOUND INBOUND BRINFO`
}
pinet_info () {
local qemu_cmd=`list_first $qemu_system_cmd`
cat <<EOF
This tool scans the network interface table on the host system for active
virtual QEMU guest <lan> interfaces. Then the tool adds these interfaces to
a bridge. Seen from a virtual guest, all active interfaces are connected.
Additional non-virtual interfaces can be added to the bridge. If this is
an outbound interface connected to <lan> interfaces of physical Raspberry
PIs, these systems join the virtual network.
Adminstator Privilege
This tool uses "sudo" for executing "ip link" commands with administrator
privileges, needed to set up and administer the bridge.
Using the options --router-interface, --wlan-activate, ot --wlan-remove,
the "ip addr" command is run with administrator privileges via "sudo".
EOF
}
pinet_help () {
echo "Virtual bridged network setup"
[ -z "$NOISY" ] ||
pinet_info
disclaimer_once
local cmdl_arg="use particular WLAN instance"
local _fi="for instance <id>"
# use readonly for checking option letter uniqueness
readonly _a="set up bridge for active virtual QEMU <lan> interfaces"
readonly _A="remove virtual <lan> interfaces bridge"
readonly _o="add non-virtual interface to bridge, implies --add-bridge"
readonly _r="add ip address to bridge, implies --add-bridge"
readonly _w="add ip address to host end of WLAN interface $_fi"
readonly _W="flush ip addresses from all host ends of WLAN interfaces"
local n=18
local f="%8s -%s, --%-${n}s -- %s\n"
echo
echo "Usage: $self $ARGSUSAGE"
echo
echo "Instance: <id> or <alias> -- $cmdl_arg"
echo
printf "$f" Options: b add-bridge "$_a"
printf "$f" "" B remove-bridge "$_A"
printf "$f" "" r router-interface "$_r"
printf "$f" "" o outbound-interface "$_o"
echo
printf "$f" "" w wlan-activate "$_w"
printf "$f" "" W wlan-remove "$_W"
echo
stdopts_help "$short_stdopts" "$n"
[ -z "$NOISY" ] ||
echo
exit
}
pinet_parse_options () {
local so="${short_stdopts}bBo:rwW"
local lo="${long_stdopts},add-bridge,remove-bridge"
lo="${lo},outbound-interface:,router-interface"
lo="${lo},wlan-activate,wlan-remove"
getopt -Q -o "$so" -l "$lo" -n "$self" -s sh -- "$@" || usage
eval set -- `getopt -o "$so" -l "$lo" -n"$self" -s sh -- "$@"`
stdopts_filter "$@"
eval set -- $OPTIONS
# parse remaining option arguments
while true
do
case "$1" in
-b|--add-bridge) ADDBRIDGE=set; shift ; continue ;;
-B|--remove-bridge) RMBRIDGE=set ; shift ; continue ;;
-o|--outbound-interface) OUTBOUND="$2"; shift 2; continue ;;
-r|--router-interface) INBOUND=set ; shift ; continue ;;
-w|--wlan-activate) WLANON=set ; shift ; continue ;;
-W|--wlan-remove) WLANOFF=set ; shift ; continue ;;
--) shift; break ;;
*) fatal "parse_options: unexpected case \"$1\""
esac
done
[ -z "$HELP" ] ||
pinet_help
# [ 0 -eq $# ] ||
# usage "No more commands line arguments"
# set QID variable
verify_set_instance "$@"
# implied options for set expressions
[ -z "$OUTBOUND$INBOUND" ] || ADDBRIDGE=set
# implied options for unset expressions
[ -n "$RMBRIDGE$WLANON$WLANOFF" ] || BRINFO=set
# imcompatible option combinations
[ -z "$ADDBRIDGE" -o -z "$RMBRIDGE" ] ||
usage "Incompatible options --add-bridge and --remove-bridge"
[ -z "$WLANON" -o -z "$WLANOFF" ] ||
usage "Incompatible options --wlan-activate and --wlan-remove"
[ -z "$WLANON" -o 0 -lt $# ] ||
usage "Option --wlan-activate requries instance argument"
[ -n "$WLANON" -o 0 -eq $# ] ||
usage "Unsupported instance argument <$QID> without option"
}
# -----------------------------------------------------------------------------
# Helper functions
# -----------------------------------------------------------------------------
pinet_print_wlan_address () {
awk '$1 == "address" && $2 ~ /^[0-9./]*$/ {
split ($2, ip, ".")
split (ip [4], wd, "/")
ip4 = wd [1] == 1 ? 2 : 1
print ip [1] "." ip [2] "." ip [3] "." ip4 "/" wd [2]
}' "$raspios_base_d/etc/network/interfaces.d/wlan"
}
pinet_print_wlan_ifcs () {
ip link show |
awk '$2 ~ /^'"$wlanpfx"'[0-9][0-9]*:$/ {
print substr ($2, 1, length ($2) - 1)
}'
}
pinet_flush_wlan_ifcs () {
local ifc=
for ifc in `pinet_print_wlan_ifcs`
do
doadm_interface_flush_ip "$ifc"
done
}
pinet_print_router_address () {
instance_lan_address "$id" | sed 's|\.[0-9]*$|.1/24|'
}
pinet_print_qemu_ifcs () {
ip link show |
awk '$2 ~ /^'"$tappfx"'[0-9][0-9]*:$/ {
print substr ($2, 1, length ($2) - 1)
}'
}
pinet_print_bridged_ifcs () {
ip link show |
awk '/ master '"$bridge"' / && $2 ~ /:$/ {
print substr ($2, 1, length ($2) - 1)
}'
}
pinet_print_bridged_or_qemu () {
{
pinet_print_qemu_ifcs
pinet_print_bridged_ifcs
} | sort -u
}
# print first ifc
pinet_print_ifc_ip () { # syntax: <interface>
local ifc="$1"
ip addr show dev "$ifc" | sort |
awk '$1 == "inet" {
split ($2, ip, "/")
print ip [1]
exit
}'
}
pinet_ifexists_ok () { # syntax: <interface>
local ifc="$1"
ip link show dev "$ifc" >/dev/null 2>&1
}
pinet_ifip_ok () { # syntax: <interface> <ip>
local ifc="$1"
local ip="$2"
ip addr show dev "$ifc" | grep -q "$ip"
}
pinet_ifup_ok () { # syntax: <interface>
local ifc="$1"
ip link show dev "$ifc" | grep -q ',LOWER_UP'
}
pinet_bridged_ok () { # syntax: <interface> <bridge>
local ifc="$1"
local br="$2"
ip link show dev "$ifc" | grep -q " master $br "
}
# -----------------------------------------------------------------------------
# MAIN
# -----------------------------------------------------------------------------
verify_not_root_user
pinet_parse_options "$@"
pinet_debug_vardump
verify_required_commands
verify_important_commands
# -----------------------------------------------------------------------------
# Add WLAN ip address
# -----------------------------------------------------------------------------
if [ -n "$WLANON" ]
then
wifc="$wlanpfx$QID"
if pinet_ifexists_ok "$wifc"
then
pinet_flush_wlan_ifcs
ipw=`pinet_print_wlan_address`
doadm_interface_up "$wifc"
doadm_interface_add_ip "$wifc" "$ipw"
else
croak "Cannot access WLAN interface \"$wifc\" for instance <$QID>"
fi
fi
# -----------------------------------------------------------------------------
# Flush WLAN ip address
# -----------------------------------------------------------------------------
if [ -n "$WLANOFF" ]
then
pinet_flush_wlan_ifcs
fi
# -----------------------------------------------------------------------------
# Create bridge
# -----------------------------------------------------------------------------
if [ -n "$ADDBRIDGE" ]
then
# create bridge and take it up
ip link show dev "$bridge" >/dev/null 2>&1 ||
doadm_bridge_add "$bridge"
doadm_interface_up "$bridge"
if [ -n "$INBOUND" ]
then
ipw=`pinet_print_router_address`
ip addr show dev "$bridge" | grep -q "$ipw" ||
doadm_interface_add_ip "$bridge" "$ipw"
fi
# add virtual TAP interfaces to the bridge
ifc_list=`pinet_print_qemu_ifcs`
for ifc in $ifc_list
do
doadm_interface_up "$ifc"
doadm_bridge_add_interface "$bridge" "$ifc"
done
# add additional interface
[ -z "$OUTBOUND" ] ||
doadm_bridge_add_interface "$bridge" "$OUTBOUND"
fi
# -----------------------------------------------------------------------------
# Remove bridge
# -----------------------------------------------------------------------------
if [ -n "$RMBRIDGE" ]
then
if pinet_ifexists_ok "$bridge"
then
doadm_bridge_flush "$bridge"
fi
fi
# -----------------------------------------------------------------------------
# List bridge interfaces
# -----------------------------------------------------------------------------
if [ -n "$BRINFO" ]
then
ipw=`pinet_print_wlan_address`
fmt="%2s %10s %-13s %-10s %-4s %7s %8s %s\n"
echo
printf "$fmt" id name lan/ip interface " wlan" virtual bridged " up"
echo --------------------------------------------------------------------
if pinet_ifexists_ok "$bridge"
then
ipa=`pinet_print_ifc_ip "$bridge"`
if pinet_ifup_ok "$bridge"
then
up=yes
else
up=no
fi
printf "$fmt" \
"" bridge "$ipa" " $bridge" "" "no " "" " $up"
fi
ifc_list=`pinet_print_bridged_or_qemu`
for ifc in $ifc_list
do
vok=no
vid=
vname=
ipa=
wif=
case "$ifc" in
$tappfx*)
vok=yes
vid=`expr "$ifc" : "$tappfx\(.*\)"`
vname=`instance_to_name "$vid"`
ipa=`instance_lan_address "$vid"`
wif=`echo "$ifc" | sed "s/^$tappfx/$wlanpfx/"`
;;
*) ipa=`pinet_print_ifc_ip "$ifc"`
esac
if pinet_bridged_ok "$ifc" "$bridge"
then
brok=yes
else
brok=no
fi
if pinet_ifup_ok "$ifc"
then
up=yes
else
up=no
vip=
fi
if [ -n "$wif" ] && pinet_ifip_ok "$wif" "$ipw"
then
wok=yes
else
wok=
fi
printf "$fmt" \
"$vid" "$vname" "$ipa" " $ifc" " $wok" \
"$vok " "$brok " " $up"
done
echo
fi
# -----------------------------------------------------------------------------
# End
# -----------------------------------------------------------------------------