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