applications/luci-splash: Add limited IPv6 Support
[project/luci.git] / applications / luci-splash / root / etc / init.d / luci_splash
1 #!/bin/sh /etc/rc.common
2
3 START=70
4 EXTRA_COMMANDS=clear_leases
5 LIMIT_DOWN=0
6 LIMIT_DOWN_BURST=0
7 LIMIT_UP=0
8
9 IPT_REPLAY=/var/run/luci_splash.iptlog
10 LOCK=/var/run/luci_splash.lock
11 [ -x /usr/sbin/ip6tables ] && [ -f /proc/net/ipv6_route ] && HAS_IPV6=1
12
13 silent() {
14         "$@" 2>/dev/null
15 }
16
17 ipt_log() {
18         iptables -I "$@"
19         echo iptables -D "$@" >> $IPT_REPLAY
20 }
21
22 ipt6_log() {
23         [ "$HAS_IPV6" = 1 ] || return
24         ip6tables -I "$@"
25         echo ip6tables -D "$@" >> $IPT_REPLAY
26 }
27
28
29 iface_add() {
30         local cfg="$1"
31
32         config_get zone "$cfg" zone
33         [ -n "$zone" ] || return 0
34
35         config_get net "$cfg" network
36         [ -n "$net" ] || return 0
37
38         config_get ifname "$net" ifname
39         [ -n "$ifname" ] || return 0
40
41         config_get ipaddr "$net" ipaddr
42         [ -n "$ipaddr" ] || return 0
43
44         config_get ip6addr "$net" ip6addr
45         #[ -n "$ipaddr" ] || return 0
46
47         config_get netmask "$net" netmask
48         [ -n "$netmask" ] || return 0
49
50         config_get type "$net" type
51
52         parentiface="$(uci -q get network.${net}.ifname)"
53
54         [ -n "$parentiface" ] && [ ! "$type" = "bridge" ] && {
55                 parentiface=${parentiface#@}
56                 config_get parentproto   "$parentiface" proto
57                 config_get parentipaddr  "$parentiface" ipaddr
58                 config_get parentnetmask "$parentiface" netmask
59         }
60
61         eval "$(ipcalc.sh $ipaddr $netmask)"
62
63         ### Add interface specific chain entry rules
64         ipt_log "zone_${zone}_prerouting" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_prerouting -t nat
65         ipt_log "zone_${zone}_forward"    -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_forwarding -t filter
66
67         if [ "$HAS_IPV6" = 1 ]; then
68                 ipt6_log "zone_${zone}_forward"    -i "${ifname%:*}" -s "$ip6addr" -j luci_splash_forwarding -t filter
69         fi
70
71         ### Allow traffic to the same subnet
72         iptables -t nat    -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
73         iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
74
75         ### Allow traffic to the mesh subnet
76         [ "$parentproto" = "static" -a -n "$parentipaddr" ] && {
77                 iptables -t nat    -I luci_splash_prerouting -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
78                 iptables -t filter -I luci_splash_forwarding -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
79         }
80
81         qos_iface_add "$ifname"
82 }
83
84 iface_del() {
85         config_get zone "$1" zone
86         [ -n "$zone" ] || return 0
87
88         config_get net "$1" network
89         [ -n "$net" ] || return 0
90
91         config_get ifname "$net" ifname
92         [ -n "$ifname" ] || return 0
93
94         # Clear interface specific rules
95         [ -s $IPT_REPLAY ] && {
96                 grep -- "-i ${ifname%:*}" $IPT_REPLAY | while read ln; do silent $ln; done
97                 sed -ie "/-i ${ifname%:*}/d" $IPT_REPLAY
98         }
99
100         qos_iface_del "$ifname"
101 }
102
103 mac_add() {
104         config_get mac "$1" mac
105         append MACS "$mac"
106 }
107
108 subnet_add() {
109         local cfg="$1"
110
111         config_get ipaddr  "$cfg" ipaddr
112         config_get netmask "$cfg" netmask
113
114         [ -n "$ipaddr" ] && {
115                 iptables -t nat    -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
116                 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
117         }
118 }
119
120 qos_iface_add() {
121         local iface="$1"
122
123         # 77 -> download root qdisc
124         # 78 -> upload root qdisc
125         # 79 -> fwmark: client->inet
126         # 80 -> fwmark: inet->client
127
128         silent tc qdisc del dev "$iface" root handle 77:
129
130         if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
131                 tc qdisc add dev "$iface" root handle 77: htb
132
133                 # assume maximum rate of 20.000 kilobit for wlan
134                 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
135
136                 # set download limit and burst
137                 tc class add dev "$iface" parent 77:1 classid 77:10 htb \
138                         rate ${LIMIT_DOWN}kbit ceil ${LIMIT_DOWN_BURST}kbit prio 2
139
140                 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
141
142                 # adding ingress can result in "File exists" if qos-scripts are active
143                 silent tc qdisc add dev "$iface" ingress
144
145                 # set client download speed
146                 tc filter add dev "$iface" parent 77: protocol ip prio 2 \
147                         handle 80 fw flowid 77:10
148
149                 # set client upload speed
150                 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
151                         handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
152         fi
153 }
154
155 qos_iface_del() {
156         local iface="$1"
157
158         silent tc qdisc del dev "$iface" root handle 77:
159         silent tc qdisc del dev "$iface" root handle 78:
160         silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
161 }
162
163 boot() {
164         ### Setup splash-relay
165         uci get uhttpd.splash 2>/dev/null || {
166 uci batch <<EOF
167         set uhttpd.splash=uhttpd
168         set uhttpd.splash.home="/www/cgi-bin/splash/"
169         set uhttpd.splash.interpreter=".sh=/bin/ash"
170         set uhttpd.splash.listen_http="8082"
171         set uhttpd.splash.index_page="splash.sh"
172         set uhttpd.splash.error_page="/splash.sh"
173
174         commit uhttpd
175 EOF
176         }
177
178         ### We are started by the firewall include
179         exit 0
180 }
181
182 start() {
183         lock $LOCK
184
185         include /lib/network
186         scan_interfaces
187         config_load luci_splash
188
189         ### Find QoS limits
190         config_get LIMIT_UP general limit_up
191         config_get LIMIT_DOWN general limit_down
192         config_get LIMIT_DOWN_BURST general limit_down_burst
193
194         LIMIT_UP="$((8*${LIMIT_UP:-0}))"
195         LIMIT_DOWN="$((8*${LIMIT_DOWN:-0}))"
196         LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:+$((8*$LIMIT_DOWN_BURST))}"
197         LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN / 5 * 6))}"
198
199         ### Load required modules
200         [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
201                 silent insmod act_police
202                 silent insmod cls_fw
203                 silent insmod cls_u32
204                 silent insmod sch_htb
205                 silent insmod sch_sfq
206                 silent insmod sch_ingress
207         }
208
209         ### Create subchains
210         iptables -t nat    -N luci_splash_prerouting
211         iptables -t nat    -N luci_splash_leases
212         iptables -t filter -N luci_splash_forwarding
213         iptables -t filter -N luci_splash_filter
214
215         if [ "$HAS_IPV6" = 1 ]; then
216                 ip6tables -t filter -N luci_splash_forwarding
217                 ip6tables -t filter -N luci_splash_filter
218         fi
219
220         ### Clear iptables replay log
221         [ -s $IPT_REPLAY ] && . $IPT_REPLAY
222         echo -n > $IPT_REPLAY
223
224         ### Build the main and portal rule
225         config_foreach iface_add iface
226         config_foreach subnet_add subnet
227
228         ### Add interface independant prerouting rules
229         iptables -t nat -A luci_splash_prerouting -j luci_splash_leases
230         iptables -t nat -A luci_splash_leases -p udp --dport 53 -j REDIRECT --to-ports 53
231         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
232
233         ### Add interface independant forwarding rules
234         iptables -t filter -A luci_splash_forwarding -j luci_splash_filter
235         iptables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
236         iptables -t filter -A luci_splash_filter -j REJECT --reject-with icmp-net-prohibited
237
238         if [ "$HAS_IPV6" = 1 ]; then
239                 ip6tables -t filter -A luci_splash_forwarding -j luci_splash_filter
240                 ip6tables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
241                 ip6tables -t filter -A luci_splash_filter -j REJECT --reject-with adm-prohibited
242         fi
243
244         ### Add QoS chain
245         [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
246                 iptables -t mangle -N luci_splash_mark_out
247                 iptables -t mangle -N luci_splash_mark_in
248                 iptables -t mangle -I PREROUTING  -j luci_splash_mark_out
249                 iptables -t mangle -I POSTROUTING -j luci_splash_mark_in
250                 if [ "$HAS_IPV6" = 1 ]; then
251                         ip6tables -t mangle -N luci_splash_mark_out
252                         ip6tables -t mangle -N luci_splash_mark_in
253                         ip6tables -t mangle -I PREROUTING  -j luci_splash_mark_out
254                         ip6tables -t mangle -I POSTROUTING -j luci_splash_mark_in
255                 fi
256         }
257
258         ### Find active mac addresses
259         MACS=""
260         config_foreach mac_add lease
261         config_foreach mac_add blacklist
262         config_foreach mac_add whitelist
263
264         ### Add crontab entry
265         test -f /etc/crontabs/root || touch /etc/crontabs/root
266         grep -q luci-splash /etc/crontabs/root || {
267                 echo '*/5 * * * *       /usr/sbin/luci-splash sync' >> /etc/crontabs/root
268         }
269
270         lock -u $LOCK
271
272         ### Populate iptables
273         [ -n "$MACS" ] && luci-splash add-rules $MACS
274 }
275
276 stop() {
277         lock $LOCK
278
279         include /lib/network
280         scan_interfaces
281         config_load luci_splash
282
283         ### Clear interface rules
284         config_foreach iface_del iface
285
286         silent iptables -t mangle -D PREROUTING  -j luci_splash_mark_out
287         silent iptables -t mangle -D POSTROUTING -j luci_splash_mark_in
288
289         if [ "$HAS_IPV6" = 1 ]; then
290                 silent ip6tables -t mangle -D PREROUTING  -j luci_splash_mark_out
291                 silent ip6tables -t mangle -D POSTROUTING -j luci_splash_mark_in
292         fi
293
294         ### Clear subchains
295         silent iptables -t nat    -F luci_splash_prerouting
296         silent iptables -t nat    -F luci_splash_leases
297         silent iptables -t filter -F luci_splash_forwarding
298         silent iptables -t filter -F luci_splash_filter
299         silent iptables -t mangle -F luci_splash_mark_out
300         silent iptables -t mangle -F luci_splash_mark_in
301
302         if [ "$HAS_IPV6" = 1 ]; then
303                 ip6tables -t filter -F luci_splash_forwarding
304                 ip6tables -t filter -F luci_splash_filter
305                 ip6tables -t mangle -F luci_splash_mark_out
306                 ip6tables -t mangle -F luci_splash_mark_in
307         fi
308
309         ### Delete subchains
310         silent iptables -t nat    -X luci_splash_prerouting
311         silent iptables -t nat    -X luci_splash_leases
312         silent iptables -t filter -X luci_splash_forwarding
313         silent iptables -t filter -X luci_splash_filter
314         silent iptables -t mangle -X luci_splash_mark_out
315         silent iptables -t mangle -X luci_splash_mark_in
316         if [ "$HAS_IPV6" = 1 ]; then
317                 ip6tables -t filter -X luci_splash_forwarding
318                 ip6tables -t filter -X luci_splash_filter
319                 ip6tables -t mangle -X luci_splash_mark_out
320                 ip6tables -t mangle -X luci_splash_mark_in
321         fi
322         sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
323
324         lock -u $LOCK
325 }
326
327 clear_leases() {
328         ### Find active mac addresses
329         MACS=""
330         config_foreach mac_add lease
331
332         ### Clear leases
333         [ -n "$MACS" ] && luci-splash remove $MACS
334 }
335