-
Notifications
You must be signed in to change notification settings - Fork 1
/
determine_max_packet_size
executable file
·208 lines (192 loc) · 5.77 KB
/
determine_max_packet_size
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
#!/bin/sh
#
# determines the maximum packet size of the current connection to a specific host
# tested on Arch Linux (bash) 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
#
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) [-v] <domain>"
exit
fi
checkMinPacketSize=1232
checkMaxPacketSize=1500
if [ "$1" = "-v" ]; then
verbose="1"
host="$2"
else
verbose="0"
host="$1"
fi
if ! nslookup $host >/dev/null; then
echo "name resolution of $host failed."
exit
fi
if [ -e /lib/gluon/release ]; then
if ! batctl o|grep -q mesh-vpn; then
echo "This Gluon node has no VPN connection."
exit
fi
if ! test -x /bin/opkg; then
echo "This Gluon node doesn't have opkg. The necessary advanced ping utilities can't be installed."
exit
fi
for i in iputils-ping iputils-ping6; do
if ! opkg list-installed|grep -q $i; then
echo "This Gluon node doesn't have the necessary advanced ping utility yet. Please install it first:"
echo "> opkg update"
echo "> opkg install iputils-ping iputils-ping6"
exit
fi
done
fi
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
mtu_check () {
# if IPv6 is checked and there is either no IPv6 default route or there is none via br-wan on a device running Gluon
if [ "$1" = "IPv6" ] && ([ "$(ip -6 route show default|grep -q default; echo $? )" != "0" ] || ([ -e /lib/gluon/release ] && [ $(ip -6 route show default|grep -q "default.*br-wan"; echo $? ) != "0" ])); then
echo "There's no IPv6 connection as there is now default route."
return
fi
if [ -e /lib/gluon/release ]; then
ipv4addr="$(ip -4 addr show dev br-wan scope global | grep inet | head -1 | awk '{ print $2 }' | cut -d"/" -f1)"
ping4cmd="${ping4} -I ${ipv4addr} ${pingparams}"
ipv6addr="$(ip -6 addr show dev br-wan scope global | grep inet6 | grep -v "inet6 fd" | head -1 | awk '{ print $2 }' | cut -d"/" -f1)"
ping6cmd="${ping6} -I ${ipv6addr} ${pingparams}"
else
ping4cmd="${ping4} ${pingparams}"
ping6cmd="${ping6} ${pingparams}"
fi
if [ ${verbose} -eq 1 ];then
echo "####################################################"
echo "Checking the smallest packet size ${checkMinPacketSize}"
echo "####################################################"
echo ""
echo "----------------------------------------------------------"
fi
if [ "$1" = "IPv6" ]; then
if [ ${verbose} -eq 1 ];then
${ping6cmd} ${checkMinPacketSize} ${host}
else
${ping6cmd} ${checkMinPacketSize} ${host} > /dev/null
fi
else
if [ ${verbose} -eq 1 ];then
${ping4cmd} ${checkMinPacketSize} ${host}
else
${ping4cmd} ${checkMinPacketSize} ${host} > /dev/null
fi
fi
if [ $? -ne 0 ]; then
echo "Smallest packet size ${checkMinPacketSize} is not usable."
return
fi
if [ ${verbose} -eq 1 ];then
echo "----------------------------------------------------------"
echo ""
echo ""
fi
if [ ${verbose} -eq 1 ];then
echo "####################################################"
echo "Checking the biggest packet size ${checkMaxPacketSize}"
echo "####################################################"
echo ""
echo "----------------------------------------------------------"
fi
if [ "$1" = "IPv6" ]; then
if [ ${verbose} -eq 1 ];then
${ping6cmd} ${checkMaxPacketSize} ${host}
else
${ping6cmd} ${checkMaxPacketSize} ${host} > /dev/null
fi
else
if [ ${verbose} -eq 1 ];then
${ping4cmd} ${checkMaxPacketSize} ${host}
else
${ping4cmd} ${checkMaxPacketSize} ${host} > /dev/null
fi
fi
if [ $? -eq 0 ]; then
echo "$1: packet size: ${checkMaxPacketSize}"
return
fi
if [ ${verbose} -eq 1 ];then
echo "----------------------------------------------------------"
echo ""
echo ""
fi
if [ ${verbose} -eq 1 ];then
echo "####################################################"
echo "determining packet size..."
echo "####################################################"
echo ""
fi
step=$(((${checkMaxPacketSize} - ${checkMinPacketSize}) / 2))
[ $((${step} % 2)) -eq 0 ] || step=$((${step} + 1)) #falls ungerade um 1 erhöhen
checkPacketSize=$((${checkMinPacketSize} + ${step}))
while [ ${step} -ge 1 ]; do
[ ${verbose} -eq 1 ] && echo "Step: $step"
[ ${verbose} -eq 1 ] && echo "checkPacketSize: ${checkPacketSize}"
step=$(($step / 2))
if [ ${step} -ne 1 ];then
[ $((${step} % 2)) -eq 0 ] || step=$((${step} + 1))
fi
if [ ${verbose} -eq 1 ];then
echo ""
echo ""
echo "----------------------------------------------------------"
fi
if [ "$1" = "IPv6" ]; then
if [ ${verbose} -eq 1 ];then
${ping6cmd} ${checkPacketSize} ${host}
else
${ping6cmd} ${checkPacketSize} ${host} > /dev/null
fi
else
if [ ${verbose} -eq 1 ];then
${ping4cmd} ${checkPacketSize} ${host}
else
${ping4cmd} ${checkPacketSize} ${host} > /dev/null
fi
fi
if [ $? -eq 0 ]; then #ping ging
if [ "$1" = "IPv6" ]; then
PacketSize=${checkPacketSize}
else
PacketSize=${checkPacketSize}
fi
checkPacketSize=$((${checkPacketSize} + ${step}))
else
checkPacketSize=$((${checkPacketSize} - ${step}))
fi
if [ ${checkPacketSize} -gt ${checkMaxPacketSize} ]; then
continue
fi
if [ ${checkPacketSize} -lt ${checkMinPacketSize} ]; then
continue
fi
if [ ${verbose} -eq 1 ];then
echo "----------------------------------------------------------"
fi
done
if [ ${PacketSize} -ne 0 ]; then
PacketSize=$((${PacketSize} + 8))
echo "$1: determined packet size: $PacketSize byte (+ Transport-IP-Header: IPv4: 20 byte | IPv6: 40 byte)"
else
echo "$1: could not determine a packet size."
fi
}
mtu_check IPv4
mtu_check IPv6