|
@@ -0,0 +1,328 @@
|
|
1
|
+#!/bin/sh
|
|
2
|
+
|
|
3
|
+mwan3_get_iface_id()
|
|
4
|
+{
|
|
5
|
+ let iface_count++
|
|
6
|
+ [ "$1" == "$INTERFACE" ] && iface_id=$iface_count
|
|
7
|
+}
|
|
8
|
+
|
|
9
|
+mwan3_get_route_args()
|
|
10
|
+{
|
|
11
|
+ route_args=$(ip -4 route list dev $DEVICE default | head -1 | sed '/.*via \([^ ]*\) .*$/!d;s//\1/;q' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
|
|
12
|
+ [ -n "$route_args" ] && route_args="via $route_args"
|
|
13
|
+ route_args="nexthop $route_args dev $DEVICE"
|
|
14
|
+}
|
|
15
|
+
|
|
16
|
+mwan3_set_general_iptables()
|
|
17
|
+{
|
|
18
|
+ if ! iptables -S mwan3_ifaces -t mangle &> /dev/null; then
|
|
19
|
+ iptables -N mwan3_ifaces -t mangle
|
|
20
|
+ fi
|
|
21
|
+
|
|
22
|
+ if ! iptables -S mwan3_rules -t mangle &> /dev/null; then
|
|
23
|
+ iptables -N mwan3_rules -t mangle
|
|
24
|
+ fi
|
|
25
|
+
|
|
26
|
+ if ! iptables -S mwan3_connected -t mangle &> /dev/null; then
|
|
27
|
+ iptables -N mwan3_connected -t mangle
|
|
28
|
+ fi
|
|
29
|
+
|
|
30
|
+ if ! iptables -S mwan3_hook -t mangle &> /dev/null; then
|
|
31
|
+ iptables -N mwan3_hook -t mangle
|
|
32
|
+ iptables -A mwan3_hook -t mangle -j CONNMARK --restore-mark --nfmask 0xff00 --ctmask 0xff00
|
|
33
|
+ iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_ifaces
|
|
34
|
+ iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_connected
|
|
35
|
+ iptables -A mwan3_hook -t mangle -m mark --mark 0x0/0xff00 -j mwan3_rules
|
|
36
|
+ iptables -A mwan3_hook -t mangle -j CONNMARK --save-mark --nfmask 0xff00 --ctmask 0xff00
|
|
37
|
+ fi
|
|
38
|
+
|
|
39
|
+ if ! iptables -S mwan3_track_hook -t mangle &> /dev/null; then
|
|
40
|
+ iptables -N mwan3_track_hook -t mangle
|
|
41
|
+ fi
|
|
42
|
+
|
|
43
|
+ if ! iptables -S PREROUTING -t mangle | grep mwan3_hook &> /dev/null; then
|
|
44
|
+ iptables -A PREROUTING -t mangle -j mwan3_hook
|
|
45
|
+ fi
|
|
46
|
+
|
|
47
|
+ if ! iptables -S OUTPUT -t mangle | grep mwan3_hook &> /dev/null; then
|
|
48
|
+ iptables -A OUTPUT -t mangle -j mwan3_hook
|
|
49
|
+ fi
|
|
50
|
+
|
|
51
|
+ if ! iptables -S OUTPUT -t mangle | grep mwan3_track_hook &> /dev/null; then
|
|
52
|
+ iptables -A OUTPUT -t mangle -j mwan3_track_hook
|
|
53
|
+ fi
|
|
54
|
+
|
|
55
|
+ iptables -F mwan3_rules -t mangle
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+mwan3_set_connected_iptables()
|
|
59
|
+{
|
|
60
|
+ local connected_networks
|
|
61
|
+
|
|
62
|
+ if iptables -S mwan3_connected -t mangle &> /dev/null; then
|
|
63
|
+ iptables -F mwan3_connected -t mangle
|
|
64
|
+
|
|
65
|
+ for connected_networks in $(ip -4 route | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}'); do
|
|
66
|
+ iptables -A mwan3_connected -t mangle -d $connected_networks -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
|
|
67
|
+ done
|
|
68
|
+
|
|
69
|
+ iptables -I mwan3_connected -t mangle -d 224.0.0.0/3 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
|
|
70
|
+ iptables -I mwan3_connected -t mangle -d 127.0.0.0/8 -m mark --mark 0x0/0xff00 -j MARK --set-xmark 0xff00/0xff00
|
|
71
|
+ fi
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+mwan3_set_iface_iptables()
|
|
75
|
+{
|
|
76
|
+ local local_net local_nets
|
|
77
|
+
|
|
78
|
+ local_net=$(ip -4 route list dev $DEVICE scope link | awk '{print $1}' | egrep '[0-9]{1,3}(\.[0-9]{1,3}){3}')
|
|
79
|
+
|
|
80
|
+ if ! iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
|
|
81
|
+ iptables -N mwan3_iface_$INTERFACE -t mangle
|
|
82
|
+ fi
|
|
83
|
+
|
|
84
|
+ iptables -F mwan3_iface_$INTERFACE -t mangle
|
|
85
|
+ iptables -D mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE &> /dev/null
|
|
86
|
+
|
|
87
|
+ if [ $ACTION == "ifup" ]; then
|
|
88
|
+ if [ -n "$local_net" ]; then
|
|
89
|
+ for local_nets in $local_net ; do
|
|
90
|
+ if [ $ACTION == "ifup" ]; then
|
|
91
|
+ iptables -I mwan3_iface_$INTERFACE -t mangle -s $local_net -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark 0xff00/0xff00
|
|
92
|
+ fi
|
|
93
|
+ done
|
|
94
|
+ fi
|
|
95
|
+
|
|
96
|
+ iptables -A mwan3_iface_$INTERFACE -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE" -j MARK --set-xmark $(($iface_id*256))/0xff00
|
|
97
|
+ iptables -A mwan3_ifaces -t mangle -i $DEVICE -m mark --mark 0x0/0xff00 -j mwan3_iface_$INTERFACE
|
|
98
|
+ fi
|
|
99
|
+
|
|
100
|
+ if [ $ACTION == "ifdown" ]; then
|
|
101
|
+ iptables -X mwan3_iface_$INTERFACE -t mangle
|
|
102
|
+ fi
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+mwan3_set_iface_route()
|
|
106
|
+{
|
|
107
|
+ ip -4 route flush table $iface_id
|
|
108
|
+ [ $ACTION == "ifup" ] && ip -4 route add table $iface_id default $route_args
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+mwan3_set_iface_rules()
|
|
112
|
+{
|
|
113
|
+ while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+1000)):'"')" ]; do
|
|
114
|
+ ip -4 rule del pref $(($iface_id+1000))
|
|
115
|
+ done
|
|
116
|
+
|
|
117
|
+ while [ -n "$(ip -4 rule list | awk '$1 == "'$(($iface_id+2000)):'"')" ]; do
|
|
118
|
+ ip -4 rule del pref $(($iface_id+2000))
|
|
119
|
+ done
|
|
120
|
+
|
|
121
|
+ while [ -n "$(ip -4 rule list | awk '$1 == "2254:"')" ]; do
|
|
122
|
+ ip -4 rule del pref 2254
|
|
123
|
+ done
|
|
124
|
+
|
|
125
|
+ [ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+1000)) iif $DEVICE lookup main
|
|
126
|
+ [ $ACTION == "ifup" ] && ip -4 rule add pref $(($iface_id+2000)) fwmark $(($iface_id*256))/0xff00 lookup $iface_id
|
|
127
|
+ ip rule add pref 2254 fwmark 0xfe00/0xff00 unreachable
|
|
128
|
+}
|
|
129
|
+
|
|
130
|
+mwan3_track()
|
|
131
|
+{
|
|
132
|
+ local track_ip track_ips reliability count timeout interval down up
|
|
133
|
+
|
|
134
|
+ mwan3_list_track_ips()
|
|
135
|
+ {
|
|
136
|
+ track_ips="$1 $track_ips"
|
|
137
|
+ }
|
|
138
|
+ config_list_foreach $INTERFACE track_ip mwan3_list_track_ips
|
|
139
|
+
|
|
140
|
+ if [ -n "$track_ips" ]; then
|
|
141
|
+ config_get reliability $INTERFACE reliability 1
|
|
142
|
+ config_get count $INTERFACE count 1
|
|
143
|
+ config_get timeout $INTERFACE timeout 4
|
|
144
|
+ config_get interval $INTERFACE interval 10
|
|
145
|
+ config_get down $INTERFACE down 5
|
|
146
|
+ config_get up $INTERFACE up 5
|
|
147
|
+
|
|
148
|
+ if ! iptables -S mwan3_track_$INTERFACE -t mangle &> /dev/null; then
|
|
149
|
+ iptables -N mwan3_track_$INTERFACE -t mangle
|
|
150
|
+ iptables -A mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE
|
|
151
|
+ fi
|
|
152
|
+
|
|
153
|
+ iptables -F mwan3_track_$INTERFACE -t mangle
|
|
154
|
+
|
|
155
|
+ for track_ip in $track_ips; do
|
|
156
|
+ iptables -A mwan3_track_$INTERFACE -t mangle -d $track_ip -j MARK --set-xmark 0xff00/0xff00
|
|
157
|
+ done
|
|
158
|
+
|
|
159
|
+ [ -x /usr/sbin/mwan3track ] && /usr/sbin/mwan3track $INTERFACE $DEVICE $reliability $count $timeout $interval $down $up $track_ips &
|
|
160
|
+ else
|
|
161
|
+ iptables -D mwan3_track_hook -t mangle -p icmp -m icmp --icmp-type 8 -m length --length 32 -j mwan3_track_$INTERFACE &> /dev/null
|
|
162
|
+ iptables -F mwan3_track_$INTERFACE -t mangle &> /dev/null
|
|
163
|
+ iptables -X mwan3_track_$INTERFACE -t mangle &> /dev/null
|
|
164
|
+ fi
|
|
165
|
+}
|
|
166
|
+
|
|
167
|
+mwan3_set_policy()
|
|
168
|
+{
|
|
169
|
+ local iface_count iface_id metric probability weight
|
|
170
|
+
|
|
171
|
+ config_get INTERFACE $1 interface
|
|
172
|
+ config_get metric $1 metric 1
|
|
173
|
+ config_get weight $1 weight 1
|
|
174
|
+
|
|
175
|
+ [ -n "$INTERFACE" ] || return 0
|
|
176
|
+
|
|
177
|
+ config_foreach mwan3_get_iface_id interface
|
|
178
|
+
|
|
179
|
+ [ -n "$iface_id" ] || return 0
|
|
180
|
+
|
|
181
|
+ if iptables -S mwan3_iface_$INTERFACE -t mangle &> /dev/null; then
|
|
182
|
+ if [ "$metric" -lt "$lowest_metric" ]; then
|
|
183
|
+
|
|
184
|
+ total_weight=$weight
|
|
185
|
+ iptables -F mwan3_policy_$policy -t mangle
|
|
186
|
+ iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "$INTERFACE $weight $weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
|
|
187
|
+
|
|
188
|
+ lowest_metric=$metric
|
|
189
|
+
|
|
190
|
+ elif [ "$metric" -eq "$lowest_metric" ]; then
|
|
191
|
+
|
|
192
|
+ total_weight=$(($total_weight+$weight))
|
|
193
|
+ probability=$(($weight*1000/$total_weight))
|
|
194
|
+
|
|
195
|
+ if [ "$probability" -lt 10 ]; then
|
|
196
|
+ probability="0.00$probability"
|
|
197
|
+ elif [ $probability -lt 100 ]; then
|
|
198
|
+ probability="0.0$probability"
|
|
199
|
+ elif [ $probability -lt 1000 ]; then
|
|
200
|
+ probability="0.$probability"
|
|
201
|
+ else
|
|
202
|
+ probability="1"
|
|
203
|
+ fi
|
|
204
|
+
|
|
205
|
+ probability="-m statistic --mode random --probability $probability"
|
|
206
|
+
|
|
207
|
+ iptables -I mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 $probability -m comment --comment "$INTERFACE $weight $total_weight" -j MARK --set-xmark $(($iface_id*256))/0xff00
|
|
208
|
+ fi
|
|
209
|
+ fi
|
|
210
|
+}
|
|
211
|
+
|
|
212
|
+mwan3_set_policies_iptables()
|
|
213
|
+{
|
|
214
|
+ local lowest_metric policy total_weight
|
|
215
|
+
|
|
216
|
+ policy=$1
|
|
217
|
+
|
|
218
|
+ if [ "$policy" != $(echo "$policy" | cut -c1-15) ]; then
|
|
219
|
+ logger -t mwan3 -p warn "Policy $policy exceeds max of 15 chars. Not setting policy" && return 0
|
|
220
|
+ fi
|
|
221
|
+
|
|
222
|
+ if ! iptables -S mwan3_policy_$policy -t mangle &> /dev/null; then
|
|
223
|
+ iptables -N mwan3_policy_$policy -t mangle
|
|
224
|
+ fi
|
|
225
|
+
|
|
226
|
+ iptables -F mwan3_policy_$policy -t mangle
|
|
227
|
+ iptables -A mwan3_policy_$policy -t mangle -m mark --mark 0x0/0xff00 -m comment --comment "unreachable" -j MARK --set-xmark 0xfe00/0xff00
|
|
228
|
+
|
|
229
|
+ lowest_metric=256
|
|
230
|
+ total_weight=0
|
|
231
|
+
|
|
232
|
+ config_list_foreach $policy use_member mwan3_set_policy
|
|
233
|
+
|
|
234
|
+ iptables -X $policy -t mangle &> /dev/null
|
|
235
|
+}
|
|
236
|
+
|
|
237
|
+mwan3_set_user_rules_iptables()
|
|
238
|
+{
|
|
239
|
+ local proto src_ip src_port dest_ip dest_port use_policy
|
|
240
|
+
|
|
241
|
+ config_get proto $1 proto all
|
|
242
|
+ config_get src_ip $1 src_ip 0.0.0.0/0
|
|
243
|
+ config_get src_port $1 src_port 0:65535
|
|
244
|
+ config_get dest_ip $1 dest_ip 0.0.0.0/0
|
|
245
|
+ config_get dest_port $1 dest_port 0:65535
|
|
246
|
+ config_get use_policy $1 use_policy
|
|
247
|
+
|
|
248
|
+ if [ -n "$use_policy" ]; then
|
|
249
|
+ if [ "$use_policy" == "default" ]; then
|
|
250
|
+ use_policy="MARK --set-xmark 0xff00/0xff00"
|
|
251
|
+ elif [ "$use_policy" == "unreachable" ]; then
|
|
252
|
+ use_policy="MARK --set-xmark 0xfe00/0xff00"
|
|
253
|
+ else
|
|
254
|
+ use_policy="mwan3_policy_$use_policy"
|
|
255
|
+ fi
|
|
256
|
+
|
|
257
|
+ case $proto in
|
|
258
|
+ tcp|udp)
|
|
259
|
+ iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m multiport --sports $src_port -m multiport --dports $dest_port -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
|
|
260
|
+ ;;
|
|
261
|
+ *)
|
|
262
|
+ iptables -A mwan3_rules -t mangle -p $proto -s $src_ip -d $dest_ip -m mark --mark 0/0xff00 -m comment --comment "$1" -j $use_policy &> /dev/null
|
|
263
|
+ ;;
|
|
264
|
+ esac
|
|
265
|
+ fi
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+mwan3_ifupdown()
|
|
269
|
+{
|
|
270
|
+ local counter enabled iface_count iface_id route_args wan_metric
|
|
271
|
+
|
|
272
|
+ [ -n "$DEVICE" ] || exit 0
|
|
273
|
+ [ -n "$INTERFACE" ] || exit 0
|
|
274
|
+ [ "$(uci get -P /var/state mwan3.$INTERFACE 2> /dev/null)" == "interface" ] || return 0
|
|
275
|
+
|
|
276
|
+ config_load mwan3
|
|
277
|
+ config_get enabled $INTERFACE enabled 0
|
|
278
|
+
|
|
279
|
+ counter=0
|
|
280
|
+
|
|
281
|
+ if [ $ACTION == "ifup" ]; then
|
|
282
|
+ [ "$enabled" -eq 1 ] || exit 0
|
|
283
|
+
|
|
284
|
+ while [ -z "$(ip -4 route list dev $DEVICE default | head -1)" -a "$counter" -lt 10 ]; do
|
|
285
|
+ sleep 1
|
|
286
|
+ let counter++
|
|
287
|
+ if [ "$counter" -ge 10 ]; then
|
|
288
|
+ logger -t mwan3 -p warn "Could not find gateway for interface $INTERFACE ($DEVICE)" && exit 0
|
|
289
|
+ fi
|
|
290
|
+ done
|
|
291
|
+
|
|
292
|
+ mwan3_get_route_args
|
|
293
|
+ fi
|
|
294
|
+
|
|
295
|
+ while [ "$(pgrep -f -o hotplug-call)" -ne $$ -a "$counter" -lt 60 ]; do
|
|
296
|
+ sleep 1
|
|
297
|
+ let counter++
|
|
298
|
+ if [ "$counter" -ge 60 ]; then
|
|
299
|
+ logger -t mwan3 -p warn "Timeout waiting for older hotplug processes to finish. $ACTION interface $INTERFACE ($DEVICE) aborted" && exit 0
|
|
300
|
+ fi
|
|
301
|
+ done
|
|
302
|
+
|
|
303
|
+ config_foreach mwan3_get_iface_id interface
|
|
304
|
+
|
|
305
|
+ [ -n "$iface_id" ] || exit 0
|
|
306
|
+ [ "$iface_count" -le 250 ] || exit 0
|
|
307
|
+ unset iface_count
|
|
308
|
+ unset counter
|
|
309
|
+
|
|
310
|
+ logger -t mwan3 -p notice "$ACTION interface $INTERFACE ($DEVICE)"
|
|
311
|
+
|
|
312
|
+ mwan3_set_general_iptables
|
|
313
|
+ mwan3_set_iface_iptables
|
|
314
|
+ mwan3_set_iface_route
|
|
315
|
+ mwan3_set_iface_rules
|
|
316
|
+
|
|
317
|
+ [ $ACTION == "ifup" ] && mwan3_track
|
|
318
|
+
|
|
319
|
+ config_foreach mwan3_set_policies_iptables policy
|
|
320
|
+ config_foreach mwan3_set_user_rules_iptables rule
|
|
321
|
+}
|
|
322
|
+
|
|
323
|
+case "$ACTION" in
|
|
324
|
+ ifup|ifdown)
|
|
325
|
+ mwan3_ifupdown
|
|
326
|
+ mwan3_set_connected_iptables
|
|
327
|
+ ;;
|
|
328
|
+esac
|