-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetermine_max_packet_size
More file actions
executable file
·268 lines (226 loc) · 7.44 KB
/
Copy pathdetermine_max_packet_size
File metadata and controls
executable file
·268 lines (226 loc) · 7.44 KB
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
#!/bin/sh
#
# determines the maximum packet size of the current connection to a specific host
# tested on Arch Linux (bash/dash) and Gluon (busybox ash)
#
# based on script written by @pinguinpfleger (Kai Hauser) for Freifunk Suedpfalz
# https://github.com/freifunk-suedpfalz/check_icmp_packetsize
# license: GPLv3
#
set -u # treat unset variables as an error
# ---------- configuration ----------
checkMinPacketSize=1232
checkMaxPacketSize=1500
# ---------- argument parsing ----------
if [ "$#" -eq 0 ]; then
echo "Usage: $(basename "$0") [-v] <host-or-ip>" >&2
exit 1
fi
_verbose=0
_host=""
if [ "$1" = "-v" ]; then
_verbose=1
if [ "$#" -lt 2 ]; then
echo "Usage: $(basename "$0") [-v] <host-or-ip>" >&2
exit 1
fi
_host=$2
else
_host=$1
fi
# ---------- helpers (all take arguments, no globals) ----------
is_ipv4() {
printf '%s\n' "$1" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
}
is_ipv6() {
printf '%s\n' "$1" | grep -Eq '^[0-9A-Fa-f]*:[0-9A-Fa-f:]*$'
}
# Portable DNS check. Tries modern nslookup querytype first, falls back to
# generic nslookup + grep for BusyBox/minimal systems.
has_dns_record() {
__host="$1"
__rtype="$2"
# Attempt 1: GNU nslookup / recent BusyBox (-querytype)
if ! __out=$(nslookup -querytype="$__rtype" "$__host" 2>/dev/null); then
:
elif printf '%s\n' "$__out" | grep -A1 "Name:" | grep -q "Address:"; then
return 0
fi
# Attempt 2: alternate syntax (-type)
if ! __out=$(nslookup -type="$__rtype" "$__host" 2>/dev/null); then
:
elif printf '%s\n' "$__out" | grep -A1 "Name:" | grep -q "Address:"; then
return 0
fi
# Fallback: generic nslookup (older BusyBox). A valid record section contains
# a "Name:" line followed by an "Address:" line. NXDOMAIN output has no "Name:".
if ! __out=$(nslookup "$__host" 2>/dev/null); then
return 1
fi
# shellcheck disable=SC3057
case "$__rtype" in
A|AAAA)
printf '%s\n' "$__out" | grep -q "^Name:"
;;
*)
return 1
;;
esac
}
# ---------- general DNS sanity check ----------
# Some corporate resolvers return SERVFAIL for AAAA even when A works.
# Only fail if NEITHER A nor AAAA resolves.
if ! is_ipv4 "$_host" && ! is_ipv6 "$_host"; then
if ! has_dns_record "$_host" "A" && ! has_dns_record "$_host" "AAAA"; then
echo "Error: Name resolution of '$_host' failed entirely." >&2
exit 20
fi
fi
# ---------- Gluon environment checks ----------
if [ -e /lib/gluon/release ]; then
if ! batctl o 2>/dev/null | grep -q mesh-vpn; then
echo "This Gluon node has no VPN connection." >&2
exit 3
fi
if ! [ -x /bin/opkg ]; then
echo "This Gluon node doesn't have opkg. The necessary advanced ping utilities can't be installed." >&2
exit 4
fi
for _pkg in iputils-ping iputils-ping6; do
if ! opkg list-installed 2>/dev/null | grep -q "$_pkg"; then
echo "This Gluon node doesn't have the necessary advanced ping utility yet. Please install it first:" >&2
echo "> opkg update && opkg install iputils-ping iputils-ping6" >&2
exit 5
fi
done
fi
# ---------- ping command selection ----------
if ping 2>&1 | grep -q interval; then
pingparams="-q -c 2 -W 1 -M do -i 0.2 -s"
else
pingparams="-q -c 2 -W 1 -M do -s"
fi
ping4="ping"
if command -v ping6 >/dev/null 2>&1; then
ping6="ping6"
else
ping6="ping -6"
ping4="ping -4"
fi
# ---------- safe ping execution (POSIX/ash compatible) ----------
# Instead of storing commands in a string and relying on unquoted expansion,
# we build positional parameters with "set --" and execute via "$@".
# This avoids SC2086 word-splitting issues while staying ash-compatible.
_p4addr=""
_p6addr=""
ping_host() {
__proto="$1"
__size="$2"
__target="$3"
__verb="$4"
if [ "$__proto" = "IPv4" ]; then
if [ -n "$_p4addr" ]; then
set -- "$ping4" -I "$_p4addr" $pingparams "$__size" "$__target"
else
set -- "$ping4" $pingparams "$__size" "$__target"
fi
else
if [ -n "$_p6addr" ]; then
set -- "$ping6" -I "$_p6addr" $pingparams "$__size" "$__target"
else
set -- "$ping6" $pingparams "$__size" "$__target"
fi
fi
if [ "$__verb" -eq 1 ]; then
"$@"
else
"$@" >/dev/null 2>&1
fi
}
# ---------- core function ----------
mtu_check() {
_proto="$1"
# 1. Literal / DNS checks
if [ "$_proto" = "IPv4" ]; then
if is_ipv6 "$_host"; then return 10; fi
if ! is_ipv4 "$_host" && ! has_dns_record "$_host" "A"; then
echo "IPv4: No A record found for '$_host'."
return 20
fi
else
if is_ipv4 "$_host"; then return 10; fi
if ! is_ipv6 "$_host" && ! has_dns_record "$_host" "AAAA"; then
echo "IPv6: No AAAA record found for '$_host'."
return 20
fi
fi
# 2. Route Logic
if [ "$_proto" = "IPv6" ]; then
if ! ip -6 route show default 2>/dev/null | grep -q "^default"; then
echo "IPv6: No default route available." >&2
return 10
fi
if [ -e /lib/gluon/release ] && ! ip -6 route show default 2>/dev/null | grep -q "br-wan"; then
echo "IPv6: Default route not on br-wan (Gluon), skipping." >&2
return 10
fi
fi
# 3. Bind to br-wan IPs on Gluon
_p4addr=""
_p6addr=""
if [ -e /lib/gluon/release ]; then
_p4addr=$(ip -4 addr show dev br-wan scope global 2>/dev/null | grep inet | head -1 | awk '{print $2}' | cut -d/ -f1)
_p6addr=$(ip -6 addr show dev br-wan scope global 2>/dev/null | grep inet6 | grep -v "inet6 fd" | head -1 | awk '{print $2}' | cut -d/ -f1)
fi
[ "$_verbose" -eq 1 ] && echo "--- Checking $_proto for $_host ---"
# 4. Initial Connectivity Check
if ! ping_host "$_proto" "$checkMinPacketSize" "$_host" 0; then
echo "$_proto: Smallest packet size $checkMinPacketSize is not usable (Host unreachable or ICMP blocked)."
return 30
fi
# 5. Max Size Check
if ping_host "$_proto" "$checkMaxPacketSize" "$_host" 0; then
echo "$_proto: Packet size: $checkMaxPacketSize"
return 0
fi
# 6. Binary Search
_low=$checkMinPacketSize
_high=$checkMaxPacketSize
# FIX from v3: initialize with the KNOWN GOOD minimum
_best=$checkMinPacketSize
while [ $((_high - _low)) -gt 1 ]; do
_mid=$(( (_low + _high) / 2 ))
[ "$_verbose" -eq 1 ] && echo "Testing size: $_mid"
if ping_host "$_proto" "$_mid" "$_host" 0; then
_low=$_mid
_best=$_mid
else
_high=$_mid
fi
done
# _best is the max ping -s payload. +8 = ICMP echo header.
# On-wire total = payload + ICMP(8) + IPv4(20) / IPv6(40).
_icmp_total=$((_best + 8))
if [ "$_proto" = "IPv4" ]; then
_wire_total=$((_icmp_total + 20))
else
_wire_total=$((_icmp_total + 40))
fi
echo "$_proto: ping -s payload: $_best (ICMP: $_icmp_total, on-wire: $_wire_total)"
return 0
}
# ---------- run ----------
mtu_check "IPv4"
_rc4=$?
mtu_check "IPv6"
_rc6=$?
# Exit with the highest error code if both protocols fail.
# "Skipped" (10, 20) counts as failure for this purpose.
if [ "$_rc4" -ne 0 ] && [ "$_rc6" -ne 0 ]; then
if [ "$_rc4" -ge "$_rc6" ]; then
exit "$_rc4"
else
exit "$_rc6"
fi
fi
exit 0